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
25 extern int in_abs_seg
; /* ABSOLUTE segment flag */
26 extern int32_t abs_seg
; /* ABSOLUTE segment */
27 extern int32_t abs_offset
; /* ABSOLUTE segment offset */
29 #include "regflags.c" /* List of register flags */
31 static int is_comma_next(void);
34 static struct tokenval tokval
;
36 static struct ofmt
*outfmt
; /* Structure of addresses of output routines */
37 static loc_t
*location
; /* Pointer to current line's segment,offset */
39 void parser_global_info(struct ofmt
*output
, loc_t
* locp
)
45 insn
*parse_line(int pass
, char *buffer
, insn
* result
,
46 efunc errfunc
, evalfunc evaluate
, ldfunc ldef
)
50 struct eval_hints hints
;
52 result
->forw_ref
= FALSE
;
56 stdscan_bufptr
= buffer
;
57 i
= stdscan(NULL
, &tokval
);
59 result
->label
= NULL
; /* Assume no label */
60 result
->eops
= NULL
; /* must do this, whatever happens */
61 result
->operands
= 0; /* must initialize this */
63 if (i
== 0) { /* blank line - ignore */
64 result
->opcode
= -1; /* and no instruction either */
67 if (i
!= TOKEN_ID
&& i
!= TOKEN_INSN
&& i
!= TOKEN_PREFIX
&&
68 (i
!= TOKEN_REG
|| (REG_SREG
& ~reg_flags
[tokval
.t_integer
]))) {
69 error(ERR_NONFATAL
, "label or instruction expected"
75 if (i
== TOKEN_ID
) { /* there's a label here */
76 result
->label
= tokval
.t_charptr
;
77 i
= stdscan(NULL
, &tokval
);
78 if (i
== ':') { /* skip over the optional colon */
79 i
= stdscan(NULL
, &tokval
);
81 error(ERR_WARNING
| ERR_WARN_OL
| ERR_PASS1
,
82 "label alone on a line without a colon might be in error");
84 if (i
!= TOKEN_INSN
|| tokval
.t_integer
!= I_EQU
) {
86 * FIXME: location->segment could be NO_SEG, in which case
87 * it is possible we should be passing 'abs_seg'. Look into this.
88 * Work out whether that is *really* what we should be doing.
89 * Generally fix things. I think this is right as it is, but
90 * am still not certain.
92 ldef(result
->label
, in_abs_seg
? abs_seg
: location
->segment
,
93 location
->offset
, NULL
, TRUE
, FALSE
, outfmt
, errfunc
);
98 result
->opcode
= -1; /* this line contains just a label */
105 while (i
== TOKEN_PREFIX
||
106 (i
== TOKEN_REG
&& !(REG_SREG
& ~reg_flags
[tokval
.t_integer
])))
109 * Handle special case: the TIMES prefix.
111 if (i
== TOKEN_PREFIX
&& tokval
.t_integer
== P_TIMES
) {
114 i
= stdscan(NULL
, &tokval
);
116 evaluate(stdscan
, NULL
, &tokval
, NULL
, pass0
, error
, NULL
);
118 if (!value
) { /* but, error in evaluator */
119 result
->opcode
= -1; /* unrecoverable parse error: */
120 return result
; /* ignore this instruction */
122 if (!is_simple(value
)) {
124 "non-constant argument supplied to TIMES");
127 result
->times
= value
->value
;
128 if (value
->value
< 0) {
129 error(ERR_NONFATAL
, "TIMES value %d is negative",
135 if (result
->nprefix
== MAXPREFIX
)
137 "instruction has more than %d prefixes", MAXPREFIX
);
139 result
->prefixes
[result
->nprefix
++] = tokval
.t_integer
;
140 i
= stdscan(NULL
, &tokval
);
144 if (i
!= TOKEN_INSN
) {
145 if (result
->nprefix
> 0 && i
== 0) {
147 * Instruction prefixes are present, but no actual
148 * instruction. This is allowed: at this point we
149 * invent a notional instruction of RESB 0.
151 result
->opcode
= I_RESB
;
152 result
->operands
= 1;
153 result
->oprs
[0].type
= IMMEDIATE
;
154 result
->oprs
[0].offset
= 0L;
155 result
->oprs
[0].segment
= result
->oprs
[0].wrt
= NO_SEG
;
158 error(ERR_NONFATAL
, "parser: instruction expected");
164 result
->opcode
= tokval
.t_integer
;
165 result
->condition
= tokval
.t_inttwo
;
168 * RESB, RESW and RESD cannot be satisfied with incorrectly
169 * evaluated operands, since the correct values _must_ be known
170 * on the first pass. Hence, even in pass one, we set the
171 * `critical' flag on calling evaluate(), so that it will bomb
172 * out on undefined symbols. Nasty, but there's nothing we can
175 * For the moment, EQU has the same difficulty, so we'll
178 if (result
->opcode
== I_RESB
|| result
->opcode
== I_RESW
|| result
->opcode
== I_RESD
|| result
->opcode
== I_RESQ
|| result
->opcode
== I_REST
|| result
->opcode
== I_EQU
|| result
->opcode
== I_INCBIN
) { /* fbk */
181 critical
= (pass
== 2 ? 2 : 0);
183 if (result
->opcode
== I_DB
||
184 result
->opcode
== I_DW
||
185 result
->opcode
== I_DD
||
186 result
->opcode
== I_DQ
||
187 result
->opcode
== I_DT
|| result
->opcode
== I_INCBIN
) {
188 extop
*eop
, **tail
= &result
->eops
, **fixptr
;
191 result
->eops_float
= FALSE
;
194 * Begin to read the DB/DW/DD/DQ/DT/INCBIN operands.
197 i
= stdscan(NULL
, &tokval
);
201 eop
= *tail
= nasm_malloc(sizeof(extop
));
204 eop
->type
= EOT_NOTHING
;
207 if (i
== TOKEN_NUM
&& tokval
.t_charptr
&& is_comma_next()) {
208 eop
->type
= EOT_DB_STRING
;
209 eop
->stringval
= tokval
.t_charptr
;
210 eop
->stringlen
= tokval
.t_inttwo
;
211 i
= stdscan(NULL
, &tokval
); /* eat the comma */
215 if ((i
== TOKEN_FLOAT
&& is_comma_next()) || i
== '-') {
219 char *save
= stdscan_bufptr
;
220 i
= stdscan(NULL
, &tokval
);
222 if (i
!= TOKEN_FLOAT
|| !is_comma_next()) {
223 stdscan_bufptr
= save
;
224 i
= tokval
.t_type
= '-';
228 if (i
== TOKEN_FLOAT
) {
229 eop
->type
= EOT_DB_STRING
;
230 result
->eops_float
= TRUE
;
231 if (result
->opcode
== I_DD
)
233 else if (result
->opcode
== I_DQ
)
235 else if (result
->opcode
== I_DT
)
238 error(ERR_NONFATAL
, "floating-point constant"
239 " encountered in `D%c' instruction",
240 result
->opcode
== I_DW
? 'W' : 'B');
242 * fix suggested by Pedro Gimeno... original line
244 * eop->type = EOT_NOTHING;
249 nasm_realloc(eop
, sizeof(extop
) + eop
->stringlen
);
252 eop
->stringval
= (char *)eop
+ sizeof(extop
);
253 if (eop
->stringlen
< 4 ||
254 !float_const(tokval
.t_charptr
, sign
,
255 (uint8_t *)eop
->stringval
,
256 eop
->stringlen
, error
))
257 eop
->type
= EOT_NOTHING
;
258 i
= stdscan(NULL
, &tokval
); /* eat the comma */
266 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
,
267 critical
, error
, NULL
);
269 if (!value
) { /* error in evaluator */
270 result
->opcode
= -1; /* unrecoverable parse error: */
271 return result
; /* ignore this instruction */
273 if (is_unknown(value
)) {
274 eop
->type
= EOT_DB_NUMBER
;
275 eop
->offset
= 0; /* doesn't matter what we put */
276 eop
->segment
= eop
->wrt
= NO_SEG
; /* likewise */
277 } else if (is_reloc(value
)) {
278 eop
->type
= EOT_DB_NUMBER
;
279 eop
->offset
= reloc_value(value
);
280 eop
->segment
= reloc_seg(value
);
281 eop
->wrt
= reloc_wrt(value
);
284 "operand %d: expression is not simple"
285 " or relocatable", oper_num
);
290 * We're about to call stdscan(), which will eat the
291 * comma that we're currently sitting on between
292 * arguments. However, we'd better check first that it
295 if (i
== 0) /* also could be EOL */
298 error(ERR_NONFATAL
, "comma expected after operand %d",
300 result
->opcode
= -1; /* unrecoverable parse error: */
301 return result
; /* ignore this instruction */
305 if (result
->opcode
== I_INCBIN
) {
307 * Correct syntax for INCBIN is that there should be
308 * one string operand, followed by one or two numeric
311 if (!result
->eops
|| result
->eops
->type
!= EOT_DB_STRING
)
312 error(ERR_NONFATAL
, "`incbin' expects a file name");
313 else if (result
->eops
->next
&&
314 result
->eops
->next
->type
!= EOT_DB_NUMBER
)
315 error(ERR_NONFATAL
, "`incbin': second parameter is",
317 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
318 result
->eops
->next
->next
->type
!= EOT_DB_NUMBER
)
319 error(ERR_NONFATAL
, "`incbin': third parameter is",
321 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
322 result
->eops
->next
->next
->next
)
324 "`incbin': more than three parameters");
328 * If we reach here, one of the above errors happened.
329 * Throw the instruction away.
333 } else /* DB ... */ if (oper_num
== 0)
334 error(ERR_WARNING
| ERR_PASS1
,
335 "no operand for data declaration");
337 result
->operands
= oper_num
;
342 /* right. Now we begin to parse the operands. There may be up to three
343 * of these, separated by commas, and terminated by a zero token. */
345 for (operand
= 0; operand
< 3; operand
++) {
346 expr
*value
; /* used most of the time */
347 int mref
; /* is this going to be a memory ref? */
348 int bracket
; /* is it a [] mref, or a & mref? */
351 result
->oprs
[operand
].addr_size
= 0; /* have to zero this whatever */
352 result
->oprs
[operand
].eaflags
= 0; /* and this */
353 result
->oprs
[operand
].opflags
= 0;
355 i
= stdscan(NULL
, &tokval
);
357 break; /* end of operands: get out of here */
358 result
->oprs
[operand
].type
= 0; /* so far, no override */
359 while (i
== TOKEN_SPECIAL
) { /* size specifiers */
360 switch ((int)tokval
.t_integer
) {
362 if (!setsize
) /* we want to use only the first */
363 result
->oprs
[operand
].type
|= BITS8
;
368 result
->oprs
[operand
].type
|= BITS16
;
374 result
->oprs
[operand
].type
|= BITS32
;
379 result
->oprs
[operand
].type
|= BITS64
;
384 result
->oprs
[operand
].type
|= BITS80
;
388 result
->oprs
[operand
].type
|= TO
;
391 result
->oprs
[operand
].type
|= STRICT
;
394 result
->oprs
[operand
].type
|= FAR
;
397 result
->oprs
[operand
].type
|= NEAR
;
400 result
->oprs
[operand
].type
|= SHORT
;
403 error(ERR_NONFATAL
, "invalid operand size specification");
405 i
= stdscan(NULL
, &tokval
);
408 if (i
== '[' || i
== '&') { /* memory reference */
410 bracket
= (i
== '[');
411 while ((i
= stdscan(NULL
, &tokval
)) == TOKEN_SPECIAL
) {
412 /* check for address directives */
413 if (tasm_compatible_mode
) {
414 switch ((int)tokval
.t_integer
) {
415 /* For TASM compatibility a size override inside the
416 * brackets changes the size of the operand, not the
417 * address type of the operand as it does in standard
418 * NASM syntax. Hence:
420 * mov eax,[DWORD val]
422 * is valid syntax in TASM compatibility mode. Note that
423 * you lose the ability to override the default address
424 * type for the instruction, but we never use anything
425 * but 32-bit flat model addressing in our code.
428 result
->oprs
[operand
].type
|= BITS8
;
431 result
->oprs
[operand
].type
|= BITS16
;
435 result
->oprs
[operand
].type
|= BITS32
;
438 result
->oprs
[operand
].type
|= BITS64
;
441 result
->oprs
[operand
].type
|= BITS80
;
445 "invalid operand size specification");
448 /* Standard NASM compatible syntax */
449 switch ((int)tokval
.t_integer
) {
451 result
->oprs
[operand
].eaflags
|= EAF_TIMESTWO
;
454 result
->oprs
[operand
].eaflags
|= EAF_REL
;
457 result
->oprs
[operand
].eaflags
|= EAF_ABS
;
460 result
->oprs
[operand
].eaflags
|= EAF_BYTEOFFS
;
463 result
->oprs
[operand
].addr_size
= 16;
464 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
468 result
->oprs
[operand
].addr_size
= 32;
469 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
472 result
->oprs
[operand
].addr_size
= 64;
473 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
476 error(ERR_NONFATAL
, "invalid size specification in"
477 " effective address");
481 } else { /* immediate operand, or register */
483 bracket
= FALSE
; /* placate optimisers */
486 if ((result
->oprs
[operand
].type
& FAR
) && !mref
&&
487 result
->opcode
!= I_JMP
&& result
->opcode
!= I_CALL
) {
488 error(ERR_NONFATAL
, "invalid use of FAR operand specifier");
491 value
= evaluate(stdscan
, NULL
, &tokval
,
492 &result
->oprs
[operand
].opflags
,
493 critical
, error
, &hints
);
495 if (result
->oprs
[operand
].opflags
& OPFLAG_FORWARD
) {
496 result
->forw_ref
= TRUE
;
498 if (!value
) { /* error in evaluator */
499 result
->opcode
= -1; /* unrecoverable parse error: */
500 return result
; /* ignore this instruction */
502 if (i
== ':' && mref
) { /* it was seg:offset */
504 * Process the segment override.
506 if (value
[1].type
!= 0 || value
->value
!= 1 ||
507 REG_SREG
& ~reg_flags
[value
->type
])
508 error(ERR_NONFATAL
, "invalid segment override");
509 else if (result
->nprefix
== MAXPREFIX
)
511 "instruction has more than %d prefixes", MAXPREFIX
);
513 result
->prefixes
[result
->nprefix
++] = value
->type
;
514 if (!(REG_FSGS
& ~reg_flags
[value
->type
]))
515 result
->oprs
[operand
].eaflags
|= EAF_FSGS
;
518 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
519 if (i
== TOKEN_SPECIAL
) { /* another check for size override */
520 switch ((int)tokval
.t_integer
) {
522 result
->oprs
[operand
].addr_size
= 16;
526 result
->oprs
[operand
].addr_size
= 32;
529 result
->oprs
[operand
].addr_size
= 64;
532 error(ERR_NONFATAL
, "invalid size specification in"
533 " effective address");
535 i
= stdscan(NULL
, &tokval
);
537 value
= evaluate(stdscan
, NULL
, &tokval
,
538 &result
->oprs
[operand
].opflags
,
539 critical
, error
, &hints
);
541 if (result
->oprs
[operand
].opflags
& OPFLAG_FORWARD
) {
542 result
->forw_ref
= TRUE
;
544 /* and get the offset */
545 if (!value
) { /* but, error in evaluator */
546 result
->opcode
= -1; /* unrecoverable parse error: */
547 return result
; /* ignore this instruction */
550 if (mref
&& bracket
) { /* find ] at the end */
552 error(ERR_NONFATAL
, "parser: expecting ]");
553 do { /* error recovery again */
554 i
= stdscan(NULL
, &tokval
);
555 } while (i
!= 0 && i
!= ',');
556 } else /* we got the required ] */
557 i
= stdscan(NULL
, &tokval
);
558 } else { /* immediate operand */
559 if (i
!= 0 && i
!= ',' && i
!= ':') {
560 error(ERR_NONFATAL
, "comma or end of line expected");
561 do { /* error recovery */
562 i
= stdscan(NULL
, &tokval
);
563 } while (i
!= 0 && i
!= ',');
564 } else if (i
== ':') {
565 result
->oprs
[operand
].type
|= COLON
;
569 /* now convert the exprs returned from evaluate() into operand
572 if (mref
) { /* it's a memory reference */
574 int b
, i
, s
; /* basereg, indexreg, scale */
575 int64_t o
; /* offset */
577 b
= i
= -1, o
= s
= 0;
578 result
->oprs
[operand
].hintbase
= hints
.base
;
579 result
->oprs
[operand
].hinttype
= hints
.type
;
581 if (e
->type
&& e
->type
<= EXPR_REG_END
) { /* this bit's a register */
582 if (e
->value
== 1) /* in fact it can be basereg */
584 else /* no, it has to be indexreg */
585 i
= e
->type
, s
= e
->value
;
588 if (e
->type
&& e
->type
<= EXPR_REG_END
) { /* it's a 2nd register */
589 if (b
!= -1) /* If the first was the base, ... */
590 i
= e
->type
, s
= e
->value
; /* second has to be indexreg */
592 else if (e
->value
!= 1) { /* If both want to be index */
594 "beroset-p-592-invalid effective address");
601 if (e
->type
!= 0) { /* is there an offset? */
602 if (e
->type
<= EXPR_REG_END
) { /* in fact, is there an error? */
604 "beroset-p-603-invalid effective address");
608 if (e
->type
== EXPR_UNKNOWN
) {
609 o
= 0; /* doesn't matter what */
610 result
->oprs
[operand
].wrt
= NO_SEG
; /* nor this */
611 result
->oprs
[operand
].segment
= NO_SEG
; /* or this */
613 e
++; /* go to the end of the line */
615 if (e
->type
== EXPR_SIMPLE
) {
619 if (e
->type
== EXPR_WRT
) {
620 result
->oprs
[operand
].wrt
= e
->value
;
623 result
->oprs
[operand
].wrt
= NO_SEG
;
625 * Look for a segment base type.
627 if (e
->type
&& e
->type
< EXPR_SEGBASE
) {
629 "beroset-p-630-invalid effective address");
633 while (e
->type
&& e
->value
== 0)
635 if (e
->type
&& e
->value
!= 1) {
637 "beroset-p-637-invalid effective address");
642 result
->oprs
[operand
].segment
=
643 e
->type
- EXPR_SEGBASE
;
646 result
->oprs
[operand
].segment
= NO_SEG
;
647 while (e
->type
&& e
->value
== 0)
651 "beroset-p-650-invalid effective address");
659 result
->oprs
[operand
].wrt
= NO_SEG
;
660 result
->oprs
[operand
].segment
= NO_SEG
;
663 if (e
->type
!= 0) { /* there'd better be nothing left! */
665 "beroset-p-663-invalid effective address");
670 result
->oprs
[operand
].type
|= MEMORY
;
672 if (b
== -1 && (i
== -1 || s
== 0)) {
673 int is_rel
= globalbits
== 64 &&
674 !(result
->oprs
[operand
].eaflags
& EAF_ABS
) &&
676 !(result
->oprs
[operand
].eaflags
& EAF_FSGS
)) ||
677 (result
->oprs
[operand
].eaflags
& EAF_REL
));
679 result
->oprs
[operand
].type
|= is_rel
? IP_REL
: MEM_OFFS
;
681 result
->oprs
[operand
].basereg
= b
;
682 result
->oprs
[operand
].indexreg
= i
;
683 result
->oprs
[operand
].scale
= s
;
684 result
->oprs
[operand
].offset
= o
;
685 } else { /* it's not a memory reference */
687 if (is_just_unknown(value
)) { /* it's immediate but unknown */
688 result
->oprs
[operand
].type
|= IMMEDIATE
;
689 result
->oprs
[operand
].offset
= 0; /* don't care */
690 result
->oprs
[operand
].segment
= NO_SEG
; /* don't care again */
691 result
->oprs
[operand
].wrt
= NO_SEG
; /* still don't care */
692 } else if (is_reloc(value
)) { /* it's immediate */
693 result
->oprs
[operand
].type
|= IMMEDIATE
;
694 result
->oprs
[operand
].offset
= reloc_value(value
);
695 result
->oprs
[operand
].segment
= reloc_seg(value
);
696 result
->oprs
[operand
].wrt
= reloc_wrt(value
);
697 if (is_simple(value
)) {
698 if (reloc_value(value
) == 1)
699 result
->oprs
[operand
].type
|= UNITY
;
700 if (optimizing
>= 0 &&
701 !(result
->oprs
[operand
].type
& STRICT
)) {
702 if (reloc_value(value
) >= -128 &&
703 reloc_value(value
) <= 127)
704 result
->oprs
[operand
].type
|= SBYTE
;
707 } else { /* it's a register */
709 if (value
->type
>= EXPR_SIMPLE
|| value
->value
!= 1) {
710 error(ERR_NONFATAL
, "invalid operand type");
716 * check that its only 1 register, not an expression...
718 for (i
= 1; value
[i
].type
; i
++)
719 if (value
[i
].value
) {
720 error(ERR_NONFATAL
, "invalid operand type");
725 /* clear overrides, except TO which applies to FPU regs */
726 if (result
->oprs
[operand
].type
& ~TO
) {
728 * we want to produce a warning iff the specified size
729 * is different from the register size
731 i
= result
->oprs
[operand
].type
& SIZE_MASK
;
735 result
->oprs
[operand
].type
&= TO
;
736 result
->oprs
[operand
].type
|= REGISTER
;
737 result
->oprs
[operand
].type
|= reg_flags
[value
->type
];
738 result
->oprs
[operand
].basereg
= value
->type
;
740 if (i
&& (result
->oprs
[operand
].type
& SIZE_MASK
) != i
)
741 error(ERR_WARNING
| ERR_PASS1
,
742 "register size specification ignored");
747 result
->operands
= operand
; /* set operand count */
749 while (operand
< 3) /* clear remaining operands */
750 result
->oprs
[operand
++].type
= 0;
753 * Transform RESW, RESD, RESQ, REST into RESB.
755 switch (result
->opcode
) {
757 result
->opcode
= I_RESB
;
758 result
->oprs
[0].offset
*= 2;
761 result
->opcode
= I_RESB
;
762 result
->oprs
[0].offset
*= 4;
765 result
->opcode
= I_RESB
;
766 result
->oprs
[0].offset
*= 8;
769 result
->opcode
= I_RESB
;
770 result
->oprs
[0].offset
*= 10;
777 static int is_comma_next(void)
784 i
= stdscan(NULL
, &tv
);
786 return (i
== ',' || i
== ';' || !i
);
789 void cleanup_insn(insn
* i
)
795 i
->eops
= i
->eops
->next
;