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
27 extern int in_abs_seg
; /* ABSOLUTE segment flag */
28 extern int32_t abs_seg
; /* ABSOLUTE segment */
29 extern int32_t abs_offset
; /* ABSOLUTE segment offset */
31 #include "regflags.c" /* List of register flags */
33 static int is_comma_next(void);
36 static struct tokenval tokval
;
38 static struct ofmt
*outfmt
; /* Structure of addresses of output routines */
39 static struct location
*location
; /* Pointer to current line's segment,offset */
41 void parser_global_info(struct ofmt
*output
, struct location
* locp
)
47 insn
*parse_line(int pass
, char *buffer
, insn
* result
,
48 efunc errfunc
, evalfunc evaluate
, ldfunc ldef
)
52 struct eval_hints hints
;
54 result
->forw_ref
= false;
58 stdscan_bufptr
= buffer
;
59 i
= stdscan(NULL
, &tokval
);
61 result
->label
= NULL
; /* Assume no label */
62 result
->eops
= NULL
; /* must do this, whatever happens */
63 result
->operands
= 0; /* must initialize this */
65 if (i
== 0) { /* blank line - ignore */
66 result
->opcode
= -1; /* and no instruction either */
69 if (i
!= TOKEN_ID
&& i
!= TOKEN_INSN
&& i
!= TOKEN_PREFIX
&&
70 (i
!= TOKEN_REG
|| (REG_SREG
& ~reg_flags
[tokval
.t_integer
]))) {
71 error(ERR_NONFATAL
, "label or instruction expected"
77 if (i
== TOKEN_ID
) { /* there's a label here */
78 result
->label
= tokval
.t_charptr
;
79 i
= stdscan(NULL
, &tokval
);
80 if (i
== ':') { /* skip over the optional colon */
81 i
= stdscan(NULL
, &tokval
);
83 error(ERR_WARNING
| ERR_WARN_OL
| ERR_PASS1
,
84 "label alone on a line without a colon might be in error");
86 if (i
!= TOKEN_INSN
|| tokval
.t_integer
!= I_EQU
) {
88 * FIXME: location->segment could be NO_SEG, in which case
89 * it is possible we should be passing 'abs_seg'. Look into this.
90 * Work out whether that is *really* what we should be doing.
91 * Generally fix things. I think this is right as it is, but
92 * am still not certain.
94 ldef(result
->label
, in_abs_seg
? abs_seg
: location
->segment
,
95 location
->offset
, NULL
, true, false, outfmt
, errfunc
);
100 result
->opcode
= -1; /* this line contains just a label */
107 while (i
== TOKEN_PREFIX
||
108 (i
== TOKEN_REG
&& !(REG_SREG
& ~reg_flags
[tokval
.t_integer
])))
111 * Handle special case: the TIMES prefix.
113 if (i
== TOKEN_PREFIX
&& tokval
.t_integer
== P_TIMES
) {
116 i
= stdscan(NULL
, &tokval
);
118 evaluate(stdscan
, NULL
, &tokval
, NULL
, pass0
, error
, NULL
);
120 if (!value
) { /* but, error in evaluator */
121 result
->opcode
= -1; /* unrecoverable parse error: */
122 return result
; /* ignore this instruction */
124 if (!is_simple(value
)) {
126 "non-constant argument supplied to TIMES");
129 result
->times
= value
->value
;
130 if (value
->value
< 0) {
131 error(ERR_NONFATAL
, "TIMES value %d is negative",
137 if (result
->nprefix
== MAXPREFIX
)
139 "instruction has more than %d prefixes", MAXPREFIX
);
141 result
->prefixes
[result
->nprefix
++] = tokval
.t_integer
;
142 i
= stdscan(NULL
, &tokval
);
146 if (i
!= TOKEN_INSN
) {
147 if (result
->nprefix
> 0 && i
== 0) {
149 * Instruction prefixes are present, but no actual
150 * instruction. This is allowed: at this point we
151 * invent a notional instruction of RESB 0.
153 result
->opcode
= I_RESB
;
154 result
->operands
= 1;
155 result
->oprs
[0].type
= IMMEDIATE
;
156 result
->oprs
[0].offset
= 0L;
157 result
->oprs
[0].segment
= result
->oprs
[0].wrt
= NO_SEG
;
160 error(ERR_NONFATAL
, "parser: instruction expected");
166 result
->opcode
= tokval
.t_integer
;
167 result
->condition
= tokval
.t_inttwo
;
170 * RESB, RESW and RESD cannot be satisfied with incorrectly
171 * evaluated operands, since the correct values _must_ be known
172 * on the first pass. Hence, even in pass one, we set the
173 * `critical' flag on calling evaluate(), so that it will bomb
174 * out on undefined symbols. Nasty, but there's nothing we can
177 * For the moment, EQU has the same difficulty, so we'll
180 if (result
->opcode
== I_RESB
|| result
->opcode
== I_RESW
||
181 result
->opcode
== I_RESD
|| result
->opcode
== I_RESQ
||
182 result
->opcode
== I_REST
|| result
->opcode
== I_RESO
||
183 result
->opcode
== I_EQU
|| result
->opcode
== I_INCBIN
) {
186 critical
= (pass
== 2 ? 2 : 0);
188 if (result
->opcode
== I_DB
|| result
->opcode
== I_DW
||
189 result
->opcode
== I_DD
|| result
->opcode
== I_DQ
||
190 result
->opcode
== I_DT
|| result
->opcode
== I_DO
||
191 result
->opcode
== I_INCBIN
) {
192 extop
*eop
, **tail
= &result
->eops
, **fixptr
;
195 result
->eops_float
= false;
198 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
201 i
= stdscan(NULL
, &tokval
);
205 eop
= *tail
= nasm_malloc(sizeof(extop
));
208 eop
->type
= EOT_NOTHING
;
211 if (i
== TOKEN_NUM
&& tokval
.t_charptr
&& is_comma_next()) {
212 eop
->type
= EOT_DB_STRING
;
213 eop
->stringval
= tokval
.t_charptr
;
214 eop
->stringlen
= tokval
.t_inttwo
;
215 i
= stdscan(NULL
, &tokval
); /* eat the comma */
219 if ((i
== TOKEN_FLOAT
&& is_comma_next())
220 || i
== '-' || i
== '+') {
223 if (i
== '+' || i
== '-') {
224 char *save
= stdscan_bufptr
;
226 sign
= (i
== '-') ? -1 : 1;
227 i
= stdscan(NULL
, &tokval
);
228 if (i
!= TOKEN_FLOAT
|| !is_comma_next()) {
229 stdscan_bufptr
= save
;
230 i
= tokval
.t_type
= token
;
234 if (i
== TOKEN_FLOAT
) {
235 eop
->type
= EOT_DB_STRING
;
236 result
->eops_float
= true;
237 switch (result
->opcode
) {
254 error(ERR_NONFATAL
, "floating-point constant"
255 " encountered in `db' instruction");
257 * fix suggested by Pedro Gimeno... original line
259 * eop->type = EOT_NOTHING;
264 eop
= nasm_realloc(eop
, sizeof(extop
) + eop
->stringlen
);
267 eop
->stringval
= (char *)eop
+ sizeof(extop
);
268 if (!eop
->stringlen
||
269 !float_const(tokval
.t_charptr
, sign
,
270 (uint8_t *)eop
->stringval
,
271 eop
->stringlen
, error
))
272 eop
->type
= EOT_NOTHING
;
273 i
= stdscan(NULL
, &tokval
); /* eat the comma */
281 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
,
282 critical
, error
, NULL
);
284 if (!value
) { /* error in evaluator */
285 result
->opcode
= -1; /* unrecoverable parse error: */
286 return result
; /* ignore this instruction */
288 if (is_unknown(value
)) {
289 eop
->type
= EOT_DB_NUMBER
;
290 eop
->offset
= 0; /* doesn't matter what we put */
291 eop
->segment
= eop
->wrt
= NO_SEG
; /* likewise */
292 } else if (is_reloc(value
)) {
293 eop
->type
= EOT_DB_NUMBER
;
294 eop
->offset
= reloc_value(value
);
295 eop
->segment
= reloc_seg(value
);
296 eop
->wrt
= reloc_wrt(value
);
299 "operand %d: expression is not simple"
300 " or relocatable", oper_num
);
305 * We're about to call stdscan(), which will eat the
306 * comma that we're currently sitting on between
307 * arguments. However, we'd better check first that it
310 if (i
== 0) /* also could be EOL */
313 error(ERR_NONFATAL
, "comma expected after operand %d",
315 result
->opcode
= -1; /* unrecoverable parse error: */
316 return result
; /* ignore this instruction */
320 if (result
->opcode
== I_INCBIN
) {
322 * Correct syntax for INCBIN is that there should be
323 * one string operand, followed by one or two numeric
326 if (!result
->eops
|| result
->eops
->type
!= EOT_DB_STRING
)
327 error(ERR_NONFATAL
, "`incbin' expects a file name");
328 else if (result
->eops
->next
&&
329 result
->eops
->next
->type
!= EOT_DB_NUMBER
)
330 error(ERR_NONFATAL
, "`incbin': second parameter is",
332 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
333 result
->eops
->next
->next
->type
!= EOT_DB_NUMBER
)
334 error(ERR_NONFATAL
, "`incbin': third parameter is",
336 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
337 result
->eops
->next
->next
->next
)
339 "`incbin': more than three parameters");
343 * If we reach here, one of the above errors happened.
344 * Throw the instruction away.
348 } else /* DB ... */ if (oper_num
== 0)
349 error(ERR_WARNING
| ERR_PASS1
,
350 "no operand for data declaration");
352 result
->operands
= oper_num
;
357 /* right. Now we begin to parse the operands. There may be up to four
358 * of these, separated by commas, and terminated by a zero token. */
360 for (operand
= 0; operand
< MAX_OPERANDS
; operand
++) {
361 expr
*value
; /* used most of the time */
362 int mref
; /* is this going to be a memory ref? */
363 int bracket
; /* is it a [] mref, or a & mref? */
366 result
->oprs
[operand
].addr_size
= 0; /* have to zero this whatever */
367 result
->oprs
[operand
].eaflags
= 0; /* and this */
368 result
->oprs
[operand
].opflags
= 0;
370 i
= stdscan(NULL
, &tokval
);
372 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
;
404 result
->oprs
[operand
].type
|= BITS128
;
408 result
->oprs
[operand
].type
|= TO
;
411 result
->oprs
[operand
].type
|= STRICT
;
414 result
->oprs
[operand
].type
|= FAR
;
417 result
->oprs
[operand
].type
|= NEAR
;
420 result
->oprs
[operand
].type
|= SHORT
;
423 error(ERR_NONFATAL
, "invalid operand size specification");
425 i
= stdscan(NULL
, &tokval
);
428 if (i
== '[' || i
== '&') { /* memory reference */
430 bracket
= (i
== '[');
431 while ((i
= stdscan(NULL
, &tokval
)) == TOKEN_SPECIAL
) {
432 /* check for address directives */
433 if (tasm_compatible_mode
) {
434 switch ((int)tokval
.t_integer
) {
435 /* For TASM compatibility a size override inside the
436 * brackets changes the size of the operand, not the
437 * address type of the operand as it does in standard
438 * NASM syntax. Hence:
440 * mov eax,[DWORD val]
442 * is valid syntax in TASM compatibility mode. Note that
443 * you lose the ability to override the default address
444 * type for the instruction, but we never use anything
445 * but 32-bit flat model addressing in our code.
448 result
->oprs
[operand
].type
|= BITS8
;
451 result
->oprs
[operand
].type
|= BITS16
;
455 result
->oprs
[operand
].type
|= BITS32
;
458 result
->oprs
[operand
].type
|= BITS64
;
461 result
->oprs
[operand
].type
|= BITS80
;
464 result
->oprs
[operand
].type
|= BITS128
;
468 "invalid operand size specification");
471 /* Standard NASM compatible syntax */
472 switch ((int)tokval
.t_integer
) {
474 result
->oprs
[operand
].eaflags
|= EAF_TIMESTWO
;
477 result
->oprs
[operand
].eaflags
|= EAF_REL
;
480 result
->oprs
[operand
].eaflags
|= EAF_ABS
;
483 result
->oprs
[operand
].eaflags
|= EAF_BYTEOFFS
;
486 result
->oprs
[operand
].addr_size
= 16;
487 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
491 result
->oprs
[operand
].addr_size
= 32;
492 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
495 result
->oprs
[operand
].addr_size
= 64;
496 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
499 error(ERR_NONFATAL
, "invalid size specification in"
500 " effective address");
504 } else { /* immediate operand, or register */
506 bracket
= false; /* placate optimisers */
509 if ((result
->oprs
[operand
].type
& FAR
) && !mref
&&
510 result
->opcode
!= I_JMP
&& result
->opcode
!= I_CALL
) {
511 error(ERR_NONFATAL
, "invalid use of FAR operand specifier");
514 value
= evaluate(stdscan
, NULL
, &tokval
,
515 &result
->oprs
[operand
].opflags
,
516 critical
, error
, &hints
);
518 if (result
->oprs
[operand
].opflags
& OPFLAG_FORWARD
) {
519 result
->forw_ref
= true;
521 if (!value
) { /* error in evaluator */
522 result
->opcode
= -1; /* unrecoverable parse error: */
523 return result
; /* ignore this instruction */
525 if (i
== ':' && mref
) { /* it was seg:offset */
527 * Process the segment override.
529 if (value
[1].type
!= 0 || value
->value
!= 1 ||
530 REG_SREG
& ~reg_flags
[value
->type
])
531 error(ERR_NONFATAL
, "invalid segment override");
532 else if (result
->nprefix
== MAXPREFIX
)
534 "instruction has more than %d prefixes", MAXPREFIX
);
536 result
->prefixes
[result
->nprefix
++] = value
->type
;
537 if (!(REG_FSGS
& ~reg_flags
[value
->type
]))
538 result
->oprs
[operand
].eaflags
|= EAF_FSGS
;
541 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
542 if (i
== TOKEN_SPECIAL
) { /* another check for size override */
543 switch ((int)tokval
.t_integer
) {
545 result
->oprs
[operand
].addr_size
= 16;
549 result
->oprs
[operand
].addr_size
= 32;
552 result
->oprs
[operand
].addr_size
= 64;
555 error(ERR_NONFATAL
, "invalid size specification in"
556 " effective address");
558 i
= stdscan(NULL
, &tokval
);
560 value
= evaluate(stdscan
, NULL
, &tokval
,
561 &result
->oprs
[operand
].opflags
,
562 critical
, error
, &hints
);
564 if (result
->oprs
[operand
].opflags
& OPFLAG_FORWARD
) {
565 result
->forw_ref
= true;
567 /* and get the offset */
568 if (!value
) { /* but, error in evaluator */
569 result
->opcode
= -1; /* unrecoverable parse error: */
570 return result
; /* ignore this instruction */
573 if (mref
&& bracket
) { /* find ] at the end */
575 error(ERR_NONFATAL
, "parser: expecting ]");
576 do { /* error recovery again */
577 i
= stdscan(NULL
, &tokval
);
578 } while (i
!= 0 && i
!= ',');
579 } else /* we got the required ] */
580 i
= stdscan(NULL
, &tokval
);
581 } else { /* immediate operand */
582 if (i
!= 0 && i
!= ',' && i
!= ':') {
583 error(ERR_NONFATAL
, "comma or end of line expected");
584 do { /* error recovery */
585 i
= stdscan(NULL
, &tokval
);
586 } while (i
!= 0 && i
!= ',');
587 } else if (i
== ':') {
588 result
->oprs
[operand
].type
|= COLON
;
592 /* now convert the exprs returned from evaluate() into operand
595 if (mref
) { /* it's a memory reference */
597 int b
, i
, s
; /* basereg, indexreg, scale */
598 int64_t o
; /* offset */
600 b
= i
= -1, o
= s
= 0;
601 result
->oprs
[operand
].hintbase
= hints
.base
;
602 result
->oprs
[operand
].hinttype
= hints
.type
;
604 if (e
->type
&& e
->type
<= EXPR_REG_END
) { /* this bit's a register */
605 if (e
->value
== 1) /* in fact it can be basereg */
607 else /* no, it has to be indexreg */
608 i
= e
->type
, s
= e
->value
;
611 if (e
->type
&& e
->type
<= EXPR_REG_END
) { /* it's a 2nd register */
612 if (b
!= -1) /* If the first was the base, ... */
613 i
= e
->type
, s
= e
->value
; /* second has to be indexreg */
615 else if (e
->value
!= 1) { /* If both want to be index */
617 "beroset-p-592-invalid effective address");
624 if (e
->type
!= 0) { /* is there an offset? */
625 if (e
->type
<= EXPR_REG_END
) { /* in fact, is there an error? */
627 "beroset-p-603-invalid effective address");
631 if (e
->type
== EXPR_UNKNOWN
) {
632 o
= 0; /* doesn't matter what */
633 result
->oprs
[operand
].wrt
= NO_SEG
; /* nor this */
634 result
->oprs
[operand
].segment
= NO_SEG
; /* or this */
636 e
++; /* go to the end of the line */
638 if (e
->type
== EXPR_SIMPLE
) {
642 if (e
->type
== EXPR_WRT
) {
643 result
->oprs
[operand
].wrt
= e
->value
;
646 result
->oprs
[operand
].wrt
= NO_SEG
;
648 * Look for a segment base type.
650 if (e
->type
&& e
->type
< EXPR_SEGBASE
) {
652 "beroset-p-630-invalid effective address");
656 while (e
->type
&& e
->value
== 0)
658 if (e
->type
&& e
->value
!= 1) {
660 "beroset-p-637-invalid effective address");
665 result
->oprs
[operand
].segment
=
666 e
->type
- EXPR_SEGBASE
;
669 result
->oprs
[operand
].segment
= NO_SEG
;
670 while (e
->type
&& e
->value
== 0)
674 "beroset-p-650-invalid effective address");
682 result
->oprs
[operand
].wrt
= NO_SEG
;
683 result
->oprs
[operand
].segment
= NO_SEG
;
686 if (e
->type
!= 0) { /* there'd better be nothing left! */
688 "beroset-p-663-invalid effective address");
693 /* It is memory, but it can match any r/m operand */
694 result
->oprs
[operand
].type
|= MEMORY_ANY
;
696 if (b
== -1 && (i
== -1 || s
== 0)) {
697 int is_rel
= globalbits
== 64 &&
698 !(result
->oprs
[operand
].eaflags
& EAF_ABS
) &&
700 !(result
->oprs
[operand
].eaflags
& EAF_FSGS
)) ||
701 (result
->oprs
[operand
].eaflags
& EAF_REL
));
703 result
->oprs
[operand
].type
|= is_rel
? IP_REL
: MEM_OFFS
;
705 result
->oprs
[operand
].basereg
= b
;
706 result
->oprs
[operand
].indexreg
= i
;
707 result
->oprs
[operand
].scale
= s
;
708 result
->oprs
[operand
].offset
= o
;
709 } else { /* it's not a memory reference */
711 if (is_just_unknown(value
)) { /* it's immediate but unknown */
712 result
->oprs
[operand
].type
|= IMMEDIATE
;
713 result
->oprs
[operand
].offset
= 0; /* don't care */
714 result
->oprs
[operand
].segment
= NO_SEG
; /* don't care again */
715 result
->oprs
[operand
].wrt
= NO_SEG
; /* still don't care */
716 } else if (is_reloc(value
)) { /* it's immediate */
717 result
->oprs
[operand
].type
|= IMMEDIATE
;
718 result
->oprs
[operand
].offset
= reloc_value(value
);
719 result
->oprs
[operand
].segment
= reloc_seg(value
);
720 result
->oprs
[operand
].wrt
= reloc_wrt(value
);
721 if (is_simple(value
)) {
722 if (reloc_value(value
) == 1)
723 result
->oprs
[operand
].type
|= UNITY
;
724 if (optimizing
>= 0 &&
725 !(result
->oprs
[operand
].type
& STRICT
)) {
726 if (reloc_value(value
) >= -128 &&
727 reloc_value(value
) <= 127)
728 result
->oprs
[operand
].type
|= SBYTE
;
731 } else { /* it's a register */
733 if (value
->type
>= EXPR_SIMPLE
|| value
->value
!= 1) {
734 error(ERR_NONFATAL
, "invalid operand type");
740 * check that its only 1 register, not an expression...
742 for (i
= 1; value
[i
].type
; i
++)
743 if (value
[i
].value
) {
744 error(ERR_NONFATAL
, "invalid operand type");
749 /* clear overrides, except TO which applies to FPU regs */
750 if (result
->oprs
[operand
].type
& ~TO
) {
752 * we want to produce a warning iff the specified size
753 * is different from the register size
755 i
= result
->oprs
[operand
].type
& SIZE_MASK
;
759 result
->oprs
[operand
].type
&= TO
;
760 result
->oprs
[operand
].type
|= REGISTER
;
761 result
->oprs
[operand
].type
|= reg_flags
[value
->type
];
762 result
->oprs
[operand
].basereg
= value
->type
;
764 if (i
&& (result
->oprs
[operand
].type
& SIZE_MASK
) != i
)
765 error(ERR_WARNING
| ERR_PASS1
,
766 "register size specification ignored");
771 result
->operands
= operand
; /* set operand count */
773 while (operand
< 3) /* clear remaining operands */
774 result
->oprs
[operand
++].type
= 0;
777 * Transform RESW, RESD, RESQ, REST, RESO into RESB.
779 switch (result
->opcode
) {
781 result
->opcode
= I_RESB
;
782 result
->oprs
[0].offset
*= 2;
785 result
->opcode
= I_RESB
;
786 result
->oprs
[0].offset
*= 4;
789 result
->opcode
= I_RESB
;
790 result
->oprs
[0].offset
*= 8;
793 result
->opcode
= I_RESB
;
794 result
->oprs
[0].offset
*= 10;
797 result
->opcode
= I_RESB
;
798 result
->oprs
[0].offset
*= 16;
807 static int is_comma_next(void)
814 i
= stdscan(NULL
, &tv
);
816 return (i
== ',' || i
== ';' || !i
);
819 void cleanup_insn(insn
* i
)
825 i
->eops
= i
->eops
->next
;