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 static int prefix_slot(enum prefixes prefix
)
75 error(ERR_PANIC
, "Invalid value %d passed to prefix_slot()", prefix
);
80 static void process_size_override(insn
* result
, int operand
)
82 if (tasm_compatible_mode
) {
83 switch ((int)tokval
.t_integer
) {
84 /* For TASM compatibility a size override inside the
85 * brackets changes the size of the operand, not the
86 * address type of the operand as it does in standard
91 * is valid syntax in TASM compatibility mode. Note that
92 * you lose the ability to override the default address
93 * type for the instruction, but we never use anything
94 * but 32-bit flat model addressing in our code.
97 result
->oprs
[operand
].type
|= BITS8
;
100 result
->oprs
[operand
].type
|= BITS16
;
104 result
->oprs
[operand
].type
|= BITS32
;
107 result
->oprs
[operand
].type
|= BITS64
;
110 result
->oprs
[operand
].type
|= BITS80
;
113 result
->oprs
[operand
].type
|= BITS128
;
117 "invalid operand size specification");
121 /* Standard NASM compatible syntax */
122 switch ((int)tokval
.t_integer
) {
124 result
->oprs
[operand
].eaflags
|= EAF_TIMESTWO
;
127 result
->oprs
[operand
].eaflags
|= EAF_REL
;
130 result
->oprs
[operand
].eaflags
|= EAF_ABS
;
133 result
->oprs
[operand
].disp_size
= 8;
134 result
->oprs
[operand
].eaflags
|= EAF_BYTEOFFS
;
139 if (result
->prefixes
[PPS_ASIZE
] &&
140 result
->prefixes
[PPS_ASIZE
] != tokval
.t_integer
)
142 "conflicting address size specifications");
144 result
->prefixes
[PPS_ASIZE
] = tokval
.t_integer
;
147 result
->oprs
[operand
].disp_size
= 16;
148 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
152 result
->oprs
[operand
].disp_size
= 32;
153 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
156 result
->oprs
[operand
].disp_size
= 64;
157 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
160 error(ERR_NONFATAL
, "invalid size specification in"
161 " effective address");
167 insn
*parse_line(int pass
, char *buffer
, insn
* result
,
168 efunc errfunc
, evalfunc evaluate
, ldfunc ldef
)
172 struct eval_hints hints
;
175 result
->forw_ref
= false;
179 stdscan_bufptr
= buffer
;
180 i
= stdscan(NULL
, &tokval
);
182 result
->label
= NULL
; /* Assume no label */
183 result
->eops
= NULL
; /* must do this, whatever happens */
184 result
->operands
= 0; /* must initialize this */
186 if (i
== 0) { /* blank line - ignore */
187 result
->opcode
= -1; /* and no instruction either */
190 if (i
!= TOKEN_ID
&& i
!= TOKEN_INSN
&& i
!= TOKEN_PREFIX
&&
191 (i
!= TOKEN_REG
|| (REG_SREG
& ~reg_flags
[tokval
.t_integer
]))) {
192 error(ERR_NONFATAL
, "label or instruction expected"
193 " at start of line");
198 if (i
== TOKEN_ID
) { /* there's a label here */
199 result
->label
= tokval
.t_charptr
;
200 i
= stdscan(NULL
, &tokval
);
201 if (i
== ':') { /* skip over the optional colon */
202 i
= stdscan(NULL
, &tokval
);
204 error(ERR_WARNING
| ERR_WARN_OL
| ERR_PASS1
,
205 "label alone on a line without a colon might be in error");
207 if (i
!= TOKEN_INSN
|| tokval
.t_integer
!= I_EQU
) {
209 * FIXME: location->segment could be NO_SEG, in which case
210 * it is possible we should be passing 'abs_seg'. Look into this.
211 * Work out whether that is *really* what we should be doing.
212 * Generally fix things. I think this is right as it is, but
213 * am still not certain.
215 ldef(result
->label
, in_abs_seg
? abs_seg
: location
->segment
,
216 location
->offset
, NULL
, true, false, outfmt
, errfunc
);
221 result
->opcode
= -1; /* this line contains just a label */
225 for (j
= 0; j
< MAXPREFIX
; j
++)
226 result
->prefixes
[j
] = P_none
;
229 while (i
== TOKEN_PREFIX
||
230 (i
== TOKEN_REG
&& !(REG_SREG
& ~reg_flags
[tokval
.t_integer
])))
233 * Handle special case: the TIMES prefix.
235 if (i
== TOKEN_PREFIX
&& tokval
.t_integer
== P_TIMES
) {
238 i
= stdscan(NULL
, &tokval
);
240 evaluate(stdscan
, NULL
, &tokval
, NULL
, pass0
, error
, NULL
);
242 if (!value
) { /* but, error in evaluator */
243 result
->opcode
= -1; /* unrecoverable parse error: */
244 return result
; /* ignore this instruction */
246 if (!is_simple(value
)) {
248 "non-constant argument supplied to TIMES");
251 result
->times
= value
->value
;
252 if (value
->value
< 0) {
253 error(ERR_NONFATAL
, "TIMES value %d is negative",
259 int slot
= prefix_slot(tokval
.t_integer
);
260 if (result
->prefixes
[slot
]) {
262 "instruction has conflicting prefixes");
264 result
->prefixes
[slot
] = tokval
.t_integer
;
265 i
= stdscan(NULL
, &tokval
);
269 if (i
!= TOKEN_INSN
) {
273 for (j
= 0; j
< MAXPREFIX
; j
++)
274 if ((pfx
= result
->prefixes
[j
]) != P_none
)
277 if (i
== 0 && pfx
!= P_none
) {
279 * Instruction prefixes are present, but no actual
280 * instruction. This is allowed: at this point we
281 * invent a notional instruction of RESB 0.
283 result
->opcode
= I_RESB
;
284 result
->operands
= 1;
285 result
->oprs
[0].type
= IMMEDIATE
;
286 result
->oprs
[0].offset
= 0L;
287 result
->oprs
[0].segment
= result
->oprs
[0].wrt
= NO_SEG
;
290 error(ERR_NONFATAL
, "parser: instruction expected");
296 result
->opcode
= tokval
.t_integer
;
297 result
->condition
= tokval
.t_inttwo
;
300 * RESB, RESW and RESD cannot be satisfied with incorrectly
301 * evaluated operands, since the correct values _must_ be known
302 * on the first pass. Hence, even in pass one, we set the
303 * `critical' flag on calling evaluate(), so that it will bomb
304 * out on undefined symbols. Nasty, but there's nothing we can
307 * For the moment, EQU has the same difficulty, so we'll
310 if (result
->opcode
== I_RESB
|| result
->opcode
== I_RESW
||
311 result
->opcode
== I_RESD
|| result
->opcode
== I_RESQ
||
312 result
->opcode
== I_REST
|| result
->opcode
== I_RESO
||
313 result
->opcode
== I_EQU
|| result
->opcode
== I_INCBIN
) {
316 critical
= (pass
== 2 ? 2 : 0);
318 if (result
->opcode
== I_DB
|| result
->opcode
== I_DW
||
319 result
->opcode
== I_DD
|| result
->opcode
== I_DQ
||
320 result
->opcode
== I_DT
|| result
->opcode
== I_DO
||
321 result
->opcode
== I_INCBIN
) {
322 extop
*eop
, **tail
= &result
->eops
, **fixptr
;
325 result
->eops_float
= false;
328 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
331 i
= stdscan(NULL
, &tokval
);
335 eop
= *tail
= nasm_malloc(sizeof(extop
));
338 eop
->type
= EOT_NOTHING
;
341 if (i
== TOKEN_NUM
&& tokval
.t_charptr
&& is_comma_next()) {
342 eop
->type
= EOT_DB_STRING
;
343 eop
->stringval
= tokval
.t_charptr
;
344 eop
->stringlen
= tokval
.t_inttwo
;
345 i
= stdscan(NULL
, &tokval
); /* eat the comma */
349 if ((i
== TOKEN_FLOAT
&& is_comma_next())
350 || i
== '-' || i
== '+') {
353 if (i
== '+' || i
== '-') {
354 char *save
= stdscan_bufptr
;
356 sign
= (i
== '-') ? -1 : 1;
357 i
= stdscan(NULL
, &tokval
);
358 if (i
!= TOKEN_FLOAT
|| !is_comma_next()) {
359 stdscan_bufptr
= save
;
360 i
= tokval
.t_type
= token
;
364 if (i
== TOKEN_FLOAT
) {
365 eop
->type
= EOT_DB_STRING
;
366 result
->eops_float
= true;
367 switch (result
->opcode
) {
384 error(ERR_NONFATAL
, "floating-point constant"
385 " encountered in `db' instruction");
387 * fix suggested by Pedro Gimeno... original line
389 * eop->type = EOT_NOTHING;
394 eop
= nasm_realloc(eop
, sizeof(extop
) + eop
->stringlen
);
397 eop
->stringval
= (char *)eop
+ sizeof(extop
);
398 if (!eop
->stringlen
||
399 !float_const(tokval
.t_charptr
, sign
,
400 (uint8_t *)eop
->stringval
,
401 eop
->stringlen
, error
))
402 eop
->type
= EOT_NOTHING
;
403 i
= stdscan(NULL
, &tokval
); /* eat the comma */
411 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
,
412 critical
, error
, NULL
);
414 if (!value
) { /* error in evaluator */
415 result
->opcode
= -1; /* unrecoverable parse error: */
416 return result
; /* ignore this instruction */
418 if (is_unknown(value
)) {
419 eop
->type
= EOT_DB_NUMBER
;
420 eop
->offset
= 0; /* doesn't matter what we put */
421 eop
->segment
= eop
->wrt
= NO_SEG
; /* likewise */
422 } else if (is_reloc(value
)) {
423 eop
->type
= EOT_DB_NUMBER
;
424 eop
->offset
= reloc_value(value
);
425 eop
->segment
= reloc_seg(value
);
426 eop
->wrt
= reloc_wrt(value
);
429 "operand %d: expression is not simple"
430 " or relocatable", oper_num
);
435 * We're about to call stdscan(), which will eat the
436 * comma that we're currently sitting on between
437 * arguments. However, we'd better check first that it
440 if (i
== 0) /* also could be EOL */
443 error(ERR_NONFATAL
, "comma expected after operand %d",
445 result
->opcode
= -1; /* unrecoverable parse error: */
446 return result
; /* ignore this instruction */
450 if (result
->opcode
== I_INCBIN
) {
452 * Correct syntax for INCBIN is that there should be
453 * one string operand, followed by one or two numeric
456 if (!result
->eops
|| result
->eops
->type
!= EOT_DB_STRING
)
457 error(ERR_NONFATAL
, "`incbin' expects a file name");
458 else if (result
->eops
->next
&&
459 result
->eops
->next
->type
!= EOT_DB_NUMBER
)
460 error(ERR_NONFATAL
, "`incbin': second parameter is",
462 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
463 result
->eops
->next
->next
->type
!= EOT_DB_NUMBER
)
464 error(ERR_NONFATAL
, "`incbin': third parameter is",
466 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
467 result
->eops
->next
->next
->next
)
469 "`incbin': more than three parameters");
473 * If we reach here, one of the above errors happened.
474 * Throw the instruction away.
478 } else /* DB ... */ if (oper_num
== 0)
479 error(ERR_WARNING
| ERR_PASS1
,
480 "no operand for data declaration");
482 result
->operands
= oper_num
;
487 /* right. Now we begin to parse the operands. There may be up to four
488 * of these, separated by commas, and terminated by a zero token. */
490 for (operand
= 0; operand
< MAX_OPERANDS
; operand
++) {
491 expr
*value
; /* used most of the time */
492 int mref
; /* is this going to be a memory ref? */
493 int bracket
; /* is it a [] mref, or a & mref? */
496 result
->oprs
[operand
].disp_size
= 0; /* have to zero this whatever */
497 result
->oprs
[operand
].eaflags
= 0; /* and this */
498 result
->oprs
[operand
].opflags
= 0;
500 i
= stdscan(NULL
, &tokval
);
502 break; /* end of operands: get out of here */
503 result
->oprs
[operand
].type
= 0; /* so far, no override */
504 while (i
== TOKEN_SPECIAL
) { /* size specifiers */
505 switch ((int)tokval
.t_integer
) {
507 if (!setsize
) /* we want to use only the first */
508 result
->oprs
[operand
].type
|= BITS8
;
513 result
->oprs
[operand
].type
|= BITS16
;
519 result
->oprs
[operand
].type
|= BITS32
;
524 result
->oprs
[operand
].type
|= BITS64
;
529 result
->oprs
[operand
].type
|= BITS80
;
534 result
->oprs
[operand
].type
|= BITS128
;
538 result
->oprs
[operand
].type
|= TO
;
541 result
->oprs
[operand
].type
|= STRICT
;
544 result
->oprs
[operand
].type
|= FAR
;
547 result
->oprs
[operand
].type
|= NEAR
;
550 result
->oprs
[operand
].type
|= SHORT
;
553 error(ERR_NONFATAL
, "invalid operand size specification");
555 i
= stdscan(NULL
, &tokval
);
558 if (i
== '[' || i
== '&') { /* memory reference */
560 bracket
= (i
== '[');
561 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
562 while (i
== TOKEN_SPECIAL
|| i
== TOKEN_PREFIX
) {
563 process_size_override(result
, operand
);
564 i
= stdscan(NULL
, &tokval
);
566 } else { /* immediate operand, or register */
568 bracket
= false; /* placate optimisers */
571 if ((result
->oprs
[operand
].type
& FAR
) && !mref
&&
572 result
->opcode
!= I_JMP
&& result
->opcode
!= I_CALL
) {
573 error(ERR_NONFATAL
, "invalid use of FAR operand specifier");
576 value
= evaluate(stdscan
, NULL
, &tokval
,
577 &result
->oprs
[operand
].opflags
,
578 critical
, error
, &hints
);
580 if (result
->oprs
[operand
].opflags
& OPFLAG_FORWARD
) {
581 result
->forw_ref
= true;
583 if (!value
) { /* error in evaluator */
584 result
->opcode
= -1; /* unrecoverable parse error: */
585 return result
; /* ignore this instruction */
587 if (i
== ':' && mref
) { /* it was seg:offset */
589 * Process the segment override.
591 if (value
[1].type
!= 0 || value
->value
!= 1 ||
592 REG_SREG
& ~reg_flags
[value
->type
])
593 error(ERR_NONFATAL
, "invalid segment override");
594 else if (result
->prefixes
[PPS_SEG
])
596 "instruction has conflicting segment overrides");
598 result
->prefixes
[PPS_SEG
] = value
->type
;
599 if (!(REG_FSGS
& ~reg_flags
[value
->type
]))
600 result
->oprs
[operand
].eaflags
|= EAF_FSGS
;
603 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
604 while (i
== TOKEN_SPECIAL
|| i
== TOKEN_PREFIX
) {
605 process_size_override(result
, operand
);
606 i
= stdscan(NULL
, &tokval
);
608 value
= evaluate(stdscan
, NULL
, &tokval
,
609 &result
->oprs
[operand
].opflags
,
610 critical
, error
, &hints
);
612 if (result
->oprs
[operand
].opflags
& OPFLAG_FORWARD
) {
613 result
->forw_ref
= true;
615 /* and get the offset */
616 if (!value
) { /* but, error in evaluator */
617 result
->opcode
= -1; /* unrecoverable parse error: */
618 return result
; /* ignore this instruction */
621 if (mref
&& bracket
) { /* find ] at the end */
623 error(ERR_NONFATAL
, "parser: expecting ]");
624 do { /* error recovery again */
625 i
= stdscan(NULL
, &tokval
);
626 } while (i
!= 0 && i
!= ',');
627 } else /* we got the required ] */
628 i
= stdscan(NULL
, &tokval
);
629 } else { /* immediate operand */
630 if (i
!= 0 && i
!= ',' && i
!= ':') {
631 error(ERR_NONFATAL
, "comma or end of line expected");
632 do { /* error recovery */
633 i
= stdscan(NULL
, &tokval
);
634 } while (i
!= 0 && i
!= ',');
635 } else if (i
== ':') {
636 result
->oprs
[operand
].type
|= COLON
;
640 /* now convert the exprs returned from evaluate() into operand
643 if (mref
) { /* it's a memory reference */
645 int b
, i
, s
; /* basereg, indexreg, scale */
646 int64_t o
; /* offset */
648 b
= i
= -1, o
= s
= 0;
649 result
->oprs
[operand
].hintbase
= hints
.base
;
650 result
->oprs
[operand
].hinttype
= hints
.type
;
652 if (e
->type
&& e
->type
<= EXPR_REG_END
) { /* this bit's a register */
653 if (e
->value
== 1) /* in fact it can be basereg */
655 else /* no, it has to be indexreg */
656 i
= e
->type
, s
= e
->value
;
659 if (e
->type
&& e
->type
<= EXPR_REG_END
) { /* it's a 2nd register */
660 if (b
!= -1) /* If the first was the base, ... */
661 i
= e
->type
, s
= e
->value
; /* second has to be indexreg */
663 else if (e
->value
!= 1) { /* If both want to be index */
665 "beroset-p-592-invalid effective address");
672 if (e
->type
!= 0) { /* is there an offset? */
673 if (e
->type
<= EXPR_REG_END
) { /* in fact, is there an error? */
675 "beroset-p-603-invalid effective address");
679 if (e
->type
== EXPR_UNKNOWN
) {
680 o
= 0; /* doesn't matter what */
681 result
->oprs
[operand
].wrt
= NO_SEG
; /* nor this */
682 result
->oprs
[operand
].segment
= NO_SEG
; /* or this */
684 e
++; /* go to the end of the line */
686 if (e
->type
== EXPR_SIMPLE
) {
690 if (e
->type
== EXPR_WRT
) {
691 result
->oprs
[operand
].wrt
= e
->value
;
694 result
->oprs
[operand
].wrt
= NO_SEG
;
696 * Look for a segment base type.
698 if (e
->type
&& e
->type
< EXPR_SEGBASE
) {
700 "beroset-p-630-invalid effective address");
704 while (e
->type
&& e
->value
== 0)
706 if (e
->type
&& e
->value
!= 1) {
708 "beroset-p-637-invalid effective address");
713 result
->oprs
[operand
].segment
=
714 e
->type
- EXPR_SEGBASE
;
717 result
->oprs
[operand
].segment
= NO_SEG
;
718 while (e
->type
&& e
->value
== 0)
722 "beroset-p-650-invalid effective address");
730 result
->oprs
[operand
].wrt
= NO_SEG
;
731 result
->oprs
[operand
].segment
= NO_SEG
;
734 if (e
->type
!= 0) { /* there'd better be nothing left! */
736 "beroset-p-663-invalid effective address");
741 /* It is memory, but it can match any r/m operand */
742 result
->oprs
[operand
].type
|= MEMORY_ANY
;
744 if (b
== -1 && (i
== -1 || s
== 0)) {
745 int is_rel
= globalbits
== 64 &&
746 !(result
->oprs
[operand
].eaflags
& EAF_ABS
) &&
748 !(result
->oprs
[operand
].eaflags
& EAF_FSGS
)) ||
749 (result
->oprs
[operand
].eaflags
& EAF_REL
));
751 result
->oprs
[operand
].type
|= is_rel
? IP_REL
: MEM_OFFS
;
753 result
->oprs
[operand
].basereg
= b
;
754 result
->oprs
[operand
].indexreg
= i
;
755 result
->oprs
[operand
].scale
= s
;
756 result
->oprs
[operand
].offset
= o
;
757 } else { /* it's not a memory reference */
759 if (is_just_unknown(value
)) { /* it's immediate but unknown */
760 result
->oprs
[operand
].type
|= IMMEDIATE
;
761 result
->oprs
[operand
].offset
= 0; /* don't care */
762 result
->oprs
[operand
].segment
= NO_SEG
; /* don't care again */
763 result
->oprs
[operand
].wrt
= NO_SEG
; /* still don't care */
764 } else if (is_reloc(value
)) { /* it's immediate */
765 result
->oprs
[operand
].type
|= IMMEDIATE
;
766 result
->oprs
[operand
].offset
= reloc_value(value
);
767 result
->oprs
[operand
].segment
= reloc_seg(value
);
768 result
->oprs
[operand
].wrt
= reloc_wrt(value
);
769 if (is_simple(value
)) {
770 if (reloc_value(value
) == 1)
771 result
->oprs
[operand
].type
|= UNITY
;
772 if (optimizing
>= 0 &&
773 !(result
->oprs
[operand
].type
& STRICT
)) {
774 if (reloc_value(value
) >= -128 &&
775 reloc_value(value
) <= 127)
776 result
->oprs
[operand
].type
|= SBYTE
;
779 } else { /* it's a register */
781 if (value
->type
>= EXPR_SIMPLE
|| value
->value
!= 1) {
782 error(ERR_NONFATAL
, "invalid operand type");
788 * check that its only 1 register, not an expression...
790 for (i
= 1; value
[i
].type
; i
++)
791 if (value
[i
].value
) {
792 error(ERR_NONFATAL
, "invalid operand type");
797 /* clear overrides, except TO which applies to FPU regs */
798 if (result
->oprs
[operand
].type
& ~TO
) {
800 * we want to produce a warning iff the specified size
801 * is different from the register size
803 i
= result
->oprs
[operand
].type
& SIZE_MASK
;
807 result
->oprs
[operand
].type
&= TO
;
808 result
->oprs
[operand
].type
|= REGISTER
;
809 result
->oprs
[operand
].type
|= reg_flags
[value
->type
];
810 result
->oprs
[operand
].basereg
= value
->type
;
812 if (i
&& (result
->oprs
[operand
].type
& SIZE_MASK
) != i
)
813 error(ERR_WARNING
| ERR_PASS1
,
814 "register size specification ignored");
819 result
->operands
= operand
; /* set operand count */
821 /* clear remaining operands */
822 while (operand
< MAX_OPERANDS
)
823 result
->oprs
[operand
++].type
= 0;
826 * Transform RESW, RESD, RESQ, REST, RESO into RESB.
828 switch (result
->opcode
) {
830 result
->opcode
= I_RESB
;
831 result
->oprs
[0].offset
*= 2;
834 result
->opcode
= I_RESB
;
835 result
->oprs
[0].offset
*= 4;
838 result
->opcode
= I_RESB
;
839 result
->oprs
[0].offset
*= 8;
842 result
->opcode
= I_RESB
;
843 result
->oprs
[0].offset
*= 10;
846 result
->opcode
= I_RESB
;
847 result
->oprs
[0].offset
*= 16;
856 static int is_comma_next(void)
863 i
= stdscan(NULL
, &tv
);
865 return (i
== ',' || i
== ';' || !i
);
868 void cleanup_insn(insn
* i
)
874 i
->eops
= i
->eops
->next
;