1 /* parser.c source line parser for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
8 * initial version 27/iii/95 by Simon Tatham
22 static long reg_flags
[] = { /* sizes and special flags */
23 0, REG8
, REG_AL
, REG_AX
, REG8
, REG8
, REG16
, REG16
, REG8
, REG_CL
,
24 REG_CREG
, REG_CREG
, REG_CREG
, REG_CR4
, REG_CS
, REG_CX
, REG8
,
25 REG16
, REG8
, REG_DREG
, REG_DREG
, REG_DREG
, REG_DREG
, REG_DREG
,
26 REG_DREG
, REG_DESS
, REG_DX
, REG_EAX
, REG32
, REG32
, REG_ECX
,
27 REG32
, REG32
, REG_DESS
, REG32
, REG32
, REG_FSGS
, REG_FSGS
,
28 MMXREG
, MMXREG
, MMXREG
, MMXREG
, MMXREG
, MMXREG
, MMXREG
, MMXREG
,
29 REG16
, REG16
, REG_DESS
, FPU0
, FPUREG
, FPUREG
, FPUREG
, FPUREG
,
30 FPUREG
, FPUREG
, FPUREG
, REG_TREG
, REG_TREG
, REG_TREG
, REG_TREG
,
34 enum { /* special tokens */
35 S_BYTE
, S_DWORD
, S_FAR
, S_LONG
, S_NEAR
, S_NOSPLIT
, S_QWORD
,
36 S_SHORT
, S_TO
, S_TWORD
, S_WORD
39 static int is_comma_next (void);
42 static struct tokenval tokval
;
44 static struct ofmt
*outfmt
; /* Structure of addresses of output routines */
45 static loc_t
*location
; /* Pointer to current line's segment,offset */
47 void parser_global_info (struct ofmt
*output
, loc_t
*locp
)
53 insn
*parse_line (int pass
, char *buffer
, insn
*result
,
54 efunc errfunc
, evalfunc evaluate
, ldfunc ldef
)
58 struct eval_hints hints
;
60 result
->forw_ref
= FALSE
;
64 stdscan_bufptr
= buffer
;
65 i
= stdscan(NULL
, &tokval
);
67 result
->label
= NULL
; /* Assume no label */
68 result
->eops
= NULL
; /* must do this, whatever happens */
69 result
->operands
= 0; /* must initialise this */
71 if (i
==0) { /* blank line - ignore */
72 result
->opcode
= -1; /* and no instruction either */
75 if (i
!= TOKEN_ID
&& i
!= TOKEN_INSN
&& i
!= TOKEN_PREFIX
&&
76 (i
!=TOKEN_REG
|| (REG_SREG
& ~reg_flags
[tokval
.t_integer
]))) {
77 error (ERR_NONFATAL
, "label or instruction expected"
83 if (i
== TOKEN_ID
) { /* there's a label here */
84 result
->label
= tokval
.t_charptr
;
85 i
= stdscan(NULL
, &tokval
);
86 if (i
== ':') { /* skip over the optional colon */
87 i
= stdscan(NULL
, &tokval
);
89 error (ERR_WARNING
|ERR_WARN_OL
|ERR_PASS1
,
90 "label alone on a line without a colon might be in error");
92 if (i
!= TOKEN_INSN
|| tokval
.t_integer
!= I_EQU
)
95 * FIXME: location->segment could be NO_SEG, in which case
96 * it is possible we should be passing 'abs_seg'. Look into this.
97 * Work out whether that is *really* what we should be doing.
98 * Generally fix things. I think this is right as it is, but
99 * am still not certain.
101 ldef (result
->label
, location
->segment
,
102 location
->offset
, NULL
, TRUE
, FALSE
, outfmt
, errfunc
);
107 result
->opcode
= -1; /* this line contains just a label */
114 while (i
== TOKEN_PREFIX
||
115 (i
==TOKEN_REG
&& !(REG_SREG
& ~reg_flags
[tokval
.t_integer
])))
118 * Handle special case: the TIMES prefix.
120 if (i
== TOKEN_PREFIX
&& tokval
.t_integer
== P_TIMES
) {
123 i
= stdscan(NULL
, &tokval
);
124 value
= evaluate (stdscan
, NULL
, &tokval
, NULL
, pass
, error
, NULL
);
126 if (!value
) { /* but, error in evaluator */
127 result
->opcode
= -1; /* unrecoverable parse error: */
128 return result
; /* ignore this instruction */
130 if (!is_simple (value
)) {
132 "non-constant argument supplied to TIMES");
135 result
->times
= value
->value
;
136 if (value
->value
< 0) {
137 error(ERR_NONFATAL
, "TIMES value %d is negative",
143 if (result
->nprefix
== MAXPREFIX
)
145 "instruction has more than %d prefixes", MAXPREFIX
);
147 result
->prefixes
[result
->nprefix
++] = tokval
.t_integer
;
148 i
= stdscan(NULL
, &tokval
);
152 if (i
!= TOKEN_INSN
) {
153 if (result
->nprefix
> 0 && i
== 0) {
155 * Instruction prefixes are present, but no actual
156 * instruction. This is allowed: at this point we
157 * invent a notional instruction of RESB 0.
159 result
->opcode
= I_RESB
;
160 result
->operands
= 1;
161 result
->oprs
[0].type
= IMMEDIATE
;
162 result
->oprs
[0].offset
= 0L;
163 result
->oprs
[0].segment
= result
->oprs
[0].wrt
= NO_SEG
;
166 error (ERR_NONFATAL
, "parser: instruction expected");
172 result
->opcode
= tokval
.t_integer
;
173 result
->condition
= tokval
.t_inttwo
;
176 * RESB, RESW and RESD cannot be satisfied with incorrectly
177 * evaluated operands, since the correct values _must_ be known
178 * on the first pass. Hence, even in pass one, we set the
179 * `critical' flag on calling evaluate(), so that it will bomb
180 * out on undefined symbols. Nasty, but there's nothing we can
183 * For the moment, EQU has the same difficulty, so we'll
186 if (result
->opcode
== I_RESB
||
187 result
->opcode
== I_RESW
||
188 result
->opcode
== I_RESD
||
189 result
->opcode
== I_RESQ
||
190 result
->opcode
== I_REST
||
191 result
->opcode
== I_EQU
)
196 critical
= (pass
==2 ? 2 : 0);
198 if (result
->opcode
== I_DB
||
199 result
->opcode
== I_DW
||
200 result
->opcode
== I_DD
||
201 result
->opcode
== I_DQ
||
202 result
->opcode
== I_DT
||
203 result
->opcode
== I_INCBIN
)
205 extop
*eop
, **tail
= &result
->eops
, **fixptr
;
208 result
->eops_float
= FALSE
;
211 * Begin to read the DB/DW/DD/DQ/DT/INCBIN operands.
214 i
= stdscan(NULL
, &tokval
);
218 eop
= *tail
= nasm_malloc(sizeof(extop
));
221 eop
->type
= EOT_NOTHING
;
224 if (i
== TOKEN_NUM
&& tokval
.t_charptr
&& is_comma_next()) {
225 eop
->type
= EOT_DB_STRING
;
226 eop
->stringval
= tokval
.t_charptr
;
227 eop
->stringlen
= tokval
.t_inttwo
;
228 i
= stdscan(NULL
, &tokval
); /* eat the comma */
232 if ((i
== TOKEN_FLOAT
&& is_comma_next()) || i
== '-') {
236 char *save
= stdscan_bufptr
;
237 i
= stdscan(NULL
, &tokval
);
239 if (i
!= TOKEN_FLOAT
|| !is_comma_next()) {
240 stdscan_bufptr
= save
;
241 i
= tokval
.t_type
= '-';
245 if (i
== TOKEN_FLOAT
) {
246 eop
->type
= EOT_DB_STRING
;
247 result
->eops_float
= TRUE
;
248 if (result
->opcode
== I_DD
)
250 else if (result
->opcode
== I_DQ
)
252 else if (result
->opcode
== I_DT
)
255 error(ERR_NONFATAL
, "floating-point constant"
256 " encountered in `D%c' instruction",
257 result
->opcode
== I_DW
? 'W' : 'B');
259 * fix suggested by Pedro Gimeno... original line
261 * eop->type = EOT_NOTHING;
265 eop
= nasm_realloc(eop
, sizeof(extop
)+eop
->stringlen
);
268 eop
->stringval
= (char *)eop
+ sizeof(extop
);
269 if (eop
->stringlen
< 4 ||
270 !float_const (tokval
.t_charptr
, sign
,
271 (unsigned char *)eop
->stringval
,
272 eop
->stringlen
, error
))
273 eop
->type
= EOT_NOTHING
;
274 i
= stdscan(NULL
, &tokval
); /* eat the comma */
282 value
= evaluate (stdscan
, NULL
, &tokval
, NULL
,
283 critical
, error
, NULL
);
285 if (!value
) { /* error in evaluator */
286 result
->opcode
= -1;/* unrecoverable parse error: */
287 return result
; /* ignore this instruction */
289 if (is_unknown(value
)) {
290 eop
->type
= EOT_DB_NUMBER
;
291 eop
->offset
= 0; /* doesn't matter what we put */
292 eop
->segment
= eop
->wrt
= NO_SEG
; /* likewise */
293 } else if (is_reloc(value
)) {
294 eop
->type
= EOT_DB_NUMBER
;
295 eop
->offset
= reloc_value(value
);
296 eop
->segment
= reloc_seg(value
);
297 eop
->wrt
= reloc_wrt(value
);
300 "operand %d: expression is not simple"
301 " or relocatable", oper_num
);
306 * We're about to call stdscan(), which will eat the
307 * comma that we're currently sitting on between
308 * arguments. However, we'd better check first that it
311 if (i
== 0) /* also could be EOL */
314 error (ERR_NONFATAL
, "comma expected after operand %d",
316 result
->opcode
= -1;/* unrecoverable parse error: */
317 return result
; /* ignore this instruction */
321 if (result
->opcode
== I_INCBIN
) {
323 * Correct syntax for INCBIN is that there should be
324 * one string operand, followed by one or two numeric
327 if (!result
->eops
|| result
->eops
->type
!= EOT_DB_STRING
)
328 error (ERR_NONFATAL
, "`incbin' expects a file name");
329 else if (result
->eops
->next
&&
330 result
->eops
->next
->type
!= EOT_DB_NUMBER
)
331 error (ERR_NONFATAL
, "`incbin': second parameter is",
333 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
334 result
->eops
->next
->next
->type
!= EOT_DB_NUMBER
)
335 error (ERR_NONFATAL
, "`incbin': third parameter is",
337 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
338 result
->eops
->next
->next
->next
)
339 error (ERR_NONFATAL
, "`incbin': more than three parameters");
343 * If we reach here, one of the above errors happened.
344 * Throw the instruction away.
350 error (ERR_WARNING
|ERR_PASS1
,
351 "no operand for data declaration");
353 result
->operands
= oper_num
;
358 /* right. Now we begin to parse the operands. There may be up to three
359 * of these, separated by commas, and terminated by a zero token. */
361 for (operand
= 0; operand
< 3; operand
++) {
362 expr
*value
; /* used most of the time */
363 int mref
; /* is this going to be a memory ref? */
364 int bracket
; /* is it a [] mref, or a & mref? */
367 result
->oprs
[operand
].addr_size
= 0;/* have to zero this whatever */
368 result
->oprs
[operand
].eaflags
= 0; /* and this */
369 result
->oprs
[operand
].opflags
= 0;
371 i
= stdscan(NULL
, &tokval
);
372 if (i
== 0) break; /* end of operands: get out of here */
373 result
->oprs
[operand
].type
= 0; /* so far, no override */
374 while (i
== TOKEN_SPECIAL
) {/* size specifiers */
375 switch ((int)tokval
.t_integer
) {
377 if (!setsize
) /* we want to use only the first */
378 result
->oprs
[operand
].type
|= BITS8
;
383 result
->oprs
[operand
].type
|= BITS16
;
389 result
->oprs
[operand
].type
|= BITS32
;
394 result
->oprs
[operand
].type
|= BITS64
;
399 result
->oprs
[operand
].type
|= BITS80
;
403 result
->oprs
[operand
].type
|= TO
;
406 result
->oprs
[operand
].type
|= FAR
;
409 result
->oprs
[operand
].type
|= NEAR
;
412 result
->oprs
[operand
].type
|= SHORT
;
415 error (ERR_NONFATAL
, "invalid operand size specification");
417 i
= stdscan(NULL
, &tokval
);
420 if (i
== '[' || i
== '&') { /* memory reference */
422 bracket
= (i
== '[');
423 i
= stdscan(NULL
, &tokval
);
424 if (i
== TOKEN_SPECIAL
) { /* check for address size override */
425 switch ((int)tokval
.t_integer
) {
427 result
->oprs
[operand
].eaflags
|= EAF_TIMESTWO
;
430 result
->oprs
[operand
].eaflags
|= EAF_BYTEOFFS
;
433 result
->oprs
[operand
].addr_size
= 16;
434 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
438 result
->oprs
[operand
].addr_size
= 32;
439 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
442 error (ERR_NONFATAL
, "invalid size specification in"
443 " effective address");
445 i
= stdscan(NULL
, &tokval
);
447 } else { /* immediate operand, or register */
449 bracket
= FALSE
; /* placate optimisers */
452 value
= evaluate (stdscan
, NULL
, &tokval
,
453 &result
->oprs
[operand
].opflags
,
454 critical
, error
, &hints
);
456 if (result
->oprs
[operand
].opflags
& OPFLAG_FORWARD
) {
457 result
->forw_ref
= TRUE
;
459 if (!value
) { /* error in evaluator */
460 result
->opcode
= -1; /* unrecoverable parse error: */
461 return result
; /* ignore this instruction */
463 if (i
== ':' && mref
) { /* it was seg:offset */
465 * Process the segment override.
467 if (value
[1].type
!=0 || value
->value
!=1 ||
468 REG_SREG
& ~reg_flags
[value
->type
])
469 error (ERR_NONFATAL
, "invalid segment override");
470 else if (result
->nprefix
== MAXPREFIX
)
472 "instruction has more than %d prefixes",
475 result
->prefixes
[result
->nprefix
++] = value
->type
;
477 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
478 if (i
== TOKEN_SPECIAL
) { /* another check for size override */
479 switch ((int)tokval
.t_integer
) {
481 result
->oprs
[operand
].addr_size
= 16;
485 result
->oprs
[operand
].addr_size
= 32;
488 error (ERR_NONFATAL
, "invalid size specification in"
489 " effective address");
491 i
= stdscan(NULL
, &tokval
);
493 value
= evaluate (stdscan
, NULL
, &tokval
,
494 &result
->oprs
[operand
].opflags
,
495 critical
, error
, &hints
);
497 if (result
->oprs
[operand
].opflags
& OPFLAG_FORWARD
) {
498 result
->forw_ref
= TRUE
;
500 /* and get the offset */
501 if (!value
) { /* but, error in evaluator */
502 result
->opcode
= -1; /* unrecoverable parse error: */
503 return result
; /* ignore this instruction */
506 if (mref
&& bracket
) { /* find ] at the end */
508 error (ERR_NONFATAL
, "parser: expecting ]");
509 do { /* error recovery again */
510 i
= stdscan(NULL
, &tokval
);
511 } while (i
!= 0 && i
!= ',');
512 } else /* we got the required ] */
513 i
= stdscan(NULL
, &tokval
);
514 } else { /* immediate operand */
515 if (i
!= 0 && i
!= ',' && i
!= ':') {
516 error (ERR_NONFATAL
, "comma or end of line expected");
517 do { /* error recovery */
518 i
= stdscan(NULL
, &tokval
);
519 } while (i
!= 0 && i
!= ',');
520 } else if (i
== ':') {
521 result
->oprs
[operand
].type
|= COLON
;
525 /* now convert the exprs returned from evaluate() into operand
528 if (mref
) { /* it's a memory reference */
530 int b
, i
, s
; /* basereg, indexreg, scale */
533 b
= i
= -1, o
= s
= 0;
534 result
->oprs
[operand
].hintbase
= hints
.base
;
535 result
->oprs
[operand
].hinttype
= hints
.type
;
537 if (e
->type
<= EXPR_REG_END
) { /* this bit's a register */
538 if (e
->value
== 1) /* in fact it can be basereg */
540 else /* no, it has to be indexreg */
541 i
= e
->type
, s
= e
->value
;
544 if (e
->type
&& e
->type
<= EXPR_REG_END
) /* it's a 2nd register */
546 if (b
!= -1) /* If the first was the base, ... */
547 i
= e
->type
, s
= e
->value
; /* second has to be indexreg */
549 else if (e
->value
!= 1) /* If both want to be index */
551 error(ERR_NONFATAL
, "invalid effective address");
559 if (e
->type
!= 0) { /* is there an offset? */
560 if (e
->type
<= EXPR_REG_END
) /* in fact, is there an error? */
562 error (ERR_NONFATAL
, "invalid effective address");
568 if (e
->type
== EXPR_UNKNOWN
) {
569 o
= 0; /* doesn't matter what */
570 result
->oprs
[operand
].wrt
= NO_SEG
; /* nor this */
571 result
->oprs
[operand
].segment
= NO_SEG
; /* or this */
572 while (e
->type
) e
++; /* go to the end of the line */
576 if (e
->type
== EXPR_SIMPLE
) {
580 if (e
->type
== EXPR_WRT
) {
581 result
->oprs
[operand
].wrt
= e
->value
;
584 result
->oprs
[operand
].wrt
= NO_SEG
;
586 * Look for a segment base type.
588 if (e
->type
&& e
->type
< EXPR_SEGBASE
) {
589 error (ERR_NONFATAL
, "invalid effective address");
593 while (e
->type
&& e
->value
== 0)
595 if (e
->type
&& e
->value
!= 1) {
596 error (ERR_NONFATAL
, "invalid effective address");
601 result
->oprs
[operand
].segment
=
602 e
->type
- EXPR_SEGBASE
;
605 result
->oprs
[operand
].segment
= NO_SEG
;
606 while (e
->type
&& e
->value
== 0)
609 error (ERR_NONFATAL
, "invalid effective address");
617 result
->oprs
[operand
].wrt
= NO_SEG
;
618 result
->oprs
[operand
].segment
= NO_SEG
;
621 if (e
->type
!= 0) { /* there'd better be nothing left! */
622 error (ERR_NONFATAL
, "invalid effective address");
627 result
->oprs
[operand
].type
|= MEMORY
;
628 if (b
==-1 && (i
==-1 || s
==0))
629 result
->oprs
[operand
].type
|= MEM_OFFS
;
630 result
->oprs
[operand
].basereg
= b
;
631 result
->oprs
[operand
].indexreg
= i
;
632 result
->oprs
[operand
].scale
= s
;
633 result
->oprs
[operand
].offset
= o
;
635 else /* it's not a memory reference */
637 if (is_just_unknown(value
)) { /* it's immediate but unknown */
638 result
->oprs
[operand
].type
|= IMMEDIATE
;
639 result
->oprs
[operand
].offset
= 0; /* don't care */
640 result
->oprs
[operand
].segment
= NO_SEG
; /* don't care again */
641 result
->oprs
[operand
].wrt
= NO_SEG
;/* still don't care */
643 else if (is_reloc(value
)) /* it's immediate */
645 result
->oprs
[operand
].type
|= IMMEDIATE
;
646 result
->oprs
[operand
].offset
= reloc_value(value
);
647 result
->oprs
[operand
].segment
= reloc_seg(value
);
648 result
->oprs
[operand
].wrt
= reloc_wrt(value
);
649 if (is_simple(value
) && reloc_value(value
)==1)
650 result
->oprs
[operand
].type
|= UNITY
;
652 else /* it's a register */
656 if (value
->type
>=EXPR_SIMPLE
|| value
->value
!=1) {
657 error (ERR_NONFATAL
, "invalid operand type");
663 * check that its only 1 register, not an expression...
665 for (i
= 1; value
[i
].type
; i
++)
666 if (value
[i
].value
) {
667 error (ERR_NONFATAL
, "invalid operand type");
672 /* clear overrides, except TO which applies to FPU regs */
673 if (result
->oprs
[operand
].type
& ~TO
) {
675 * we want to produce a warning iff the specified size
676 * is different from the register size
678 i
= result
->oprs
[operand
].type
& SIZE_MASK
;
683 result
->oprs
[operand
].type
&= TO
;
684 result
->oprs
[operand
].type
|= REGISTER
;
685 result
->oprs
[operand
].type
|= reg_flags
[value
->type
];
686 result
->oprs
[operand
].basereg
= value
->type
;
688 if (i
&& (result
->oprs
[operand
].type
& SIZE_MASK
) != i
)
689 error (ERR_WARNING
|ERR_PASS1
,
690 "register size specification ignored");
695 result
->operands
= operand
; /* set operand count */
697 while (operand
<3) /* clear remaining operands */
698 result
->oprs
[operand
++].type
= 0;
701 * Transform RESW, RESD, RESQ, REST into RESB.
703 switch (result
->opcode
) {
704 case I_RESW
: result
->opcode
=I_RESB
; result
->oprs
[0].offset
*=2; break;
705 case I_RESD
: result
->opcode
=I_RESB
; result
->oprs
[0].offset
*=4; break;
706 case I_RESQ
: result
->opcode
=I_RESB
; result
->oprs
[0].offset
*=8; break;
707 case I_REST
: result
->opcode
=I_RESB
; result
->oprs
[0].offset
*=10; break;
713 static int is_comma_next (void)
720 i
= stdscan (NULL
, &tv
);
722 return (i
== ',' || i
== ';' || !i
);
725 void cleanup_insn (insn
*i
)
731 i
->eops
= i
->eops
->next
;