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
24 extern int in_abs_seg
; /* ABSOLUTE segment flag */
25 extern int32_t abs_seg
; /* ABSOLUTE segment */
26 extern int32_t abs_offset
; /* ABSOLUTE segment offset */
28 #include "regflags.c" /* List of register flags */
30 static int is_comma_next(void);
33 static struct tokenval tokval
;
35 static struct ofmt
*outfmt
; /* Structure of addresses of output routines */
36 static loc_t
*location
; /* Pointer to current line's segment,offset */
38 void parser_global_info(struct ofmt
*output
, loc_t
* locp
)
44 insn
*parse_line(int pass
, char *buffer
, insn
* result
,
45 efunc errfunc
, evalfunc evaluate
, ldfunc ldef
)
49 struct eval_hints hints
;
51 result
->forw_ref
= FALSE
;
55 stdscan_bufptr
= buffer
;
56 i
= stdscan(NULL
, &tokval
);
58 result
->label
= NULL
; /* Assume no label */
59 result
->eops
= NULL
; /* must do this, whatever happens */
60 result
->operands
= 0; /* must initialize this */
62 if (i
== 0) { /* blank line - ignore */
63 result
->opcode
= -1; /* and no instruction either */
66 if (i
!= TOKEN_ID
&& i
!= TOKEN_INSN
&& i
!= TOKEN_PREFIX
&&
67 (i
!= TOKEN_REG
|| (REG_SREG
& ~reg_flags
[tokval
.t_integer
]))) {
68 error(ERR_NONFATAL
, "label or instruction expected"
74 if (i
== TOKEN_ID
) { /* there's a label here */
75 result
->label
= tokval
.t_charptr
;
76 i
= stdscan(NULL
, &tokval
);
77 if (i
== ':') { /* skip over the optional colon */
78 i
= stdscan(NULL
, &tokval
);
80 error(ERR_WARNING
| ERR_WARN_OL
| ERR_PASS1
,
81 "label alone on a line without a colon might be in error");
83 if (i
!= TOKEN_INSN
|| tokval
.t_integer
!= I_EQU
) {
85 * FIXME: location->segment could be NO_SEG, in which case
86 * it is possible we should be passing 'abs_seg'. Look into this.
87 * Work out whether that is *really* what we should be doing.
88 * Generally fix things. I think this is right as it is, but
89 * am still not certain.
91 ldef(result
->label
, in_abs_seg
? abs_seg
: location
->segment
,
92 location
->offset
, NULL
, TRUE
, FALSE
, outfmt
, errfunc
);
97 result
->opcode
= -1; /* this line contains just a label */
104 while (i
== TOKEN_PREFIX
||
105 (i
== TOKEN_REG
&& !(REG_SREG
& ~reg_flags
[tokval
.t_integer
])))
108 * Handle special case: the TIMES prefix.
110 if (i
== TOKEN_PREFIX
&& tokval
.t_integer
== P_TIMES
) {
113 i
= stdscan(NULL
, &tokval
);
115 evaluate(stdscan
, NULL
, &tokval
, NULL
, pass0
, error
, NULL
);
117 if (!value
) { /* but, error in evaluator */
118 result
->opcode
= -1; /* unrecoverable parse error: */
119 return result
; /* ignore this instruction */
121 if (!is_simple(value
)) {
123 "non-constant argument supplied to TIMES");
126 result
->times
= value
->value
;
127 if (value
->value
< 0) {
128 error(ERR_NONFATAL
, "TIMES value %d is negative",
134 if (result
->nprefix
== MAXPREFIX
)
136 "instruction has more than %d prefixes", MAXPREFIX
);
138 result
->prefixes
[result
->nprefix
++] = tokval
.t_integer
;
139 i
= stdscan(NULL
, &tokval
);
143 if (i
!= TOKEN_INSN
) {
144 if (result
->nprefix
> 0 && i
== 0) {
146 * Instruction prefixes are present, but no actual
147 * instruction. This is allowed: at this point we
148 * invent a notional instruction of RESB 0.
150 result
->opcode
= I_RESB
;
151 result
->operands
= 1;
152 result
->oprs
[0].type
= IMMEDIATE
;
153 result
->oprs
[0].offset
= 0L;
154 result
->oprs
[0].segment
= result
->oprs
[0].wrt
= NO_SEG
;
157 error(ERR_NONFATAL
, "parser: instruction expected");
163 result
->opcode
= tokval
.t_integer
;
164 result
->condition
= tokval
.t_inttwo
;
167 * RESB, RESW and RESD cannot be satisfied with incorrectly
168 * evaluated operands, since the correct values _must_ be known
169 * on the first pass. Hence, even in pass one, we set the
170 * `critical' flag on calling evaluate(), so that it will bomb
171 * out on undefined symbols. Nasty, but there's nothing we can
174 * For the moment, EQU has the same difficulty, so we'll
177 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 */
180 critical
= (pass
== 2 ? 2 : 0);
182 if (result
->opcode
== I_DB
||
183 result
->opcode
== I_DW
||
184 result
->opcode
== I_DD
||
185 result
->opcode
== I_DQ
||
186 result
->opcode
== I_DT
|| result
->opcode
== I_INCBIN
) {
187 extop
*eop
, **tail
= &result
->eops
, **fixptr
;
190 result
->eops_float
= FALSE
;
193 * Begin to read the DB/DW/DD/DQ/DT/INCBIN operands.
196 i
= stdscan(NULL
, &tokval
);
200 eop
= *tail
= nasm_malloc(sizeof(extop
));
203 eop
->type
= EOT_NOTHING
;
206 if (i
== TOKEN_NUM
&& tokval
.t_charptr
&& is_comma_next()) {
207 eop
->type
= EOT_DB_STRING
;
208 eop
->stringval
= tokval
.t_charptr
;
209 eop
->stringlen
= tokval
.t_inttwo
;
210 i
= stdscan(NULL
, &tokval
); /* eat the comma */
214 if ((i
== TOKEN_FLOAT
&& is_comma_next()) || i
== '-') {
218 char *save
= stdscan_bufptr
;
219 i
= stdscan(NULL
, &tokval
);
221 if (i
!= TOKEN_FLOAT
|| !is_comma_next()) {
222 stdscan_bufptr
= save
;
223 i
= tokval
.t_type
= '-';
227 if (i
== TOKEN_FLOAT
) {
228 eop
->type
= EOT_DB_STRING
;
229 result
->eops_float
= TRUE
;
230 if (result
->opcode
== I_DD
)
232 else if (result
->opcode
== I_DQ
)
234 else if (result
->opcode
== I_DT
)
237 error(ERR_NONFATAL
, "floating-point constant"
238 " encountered in `D%c' instruction",
239 result
->opcode
== I_DW
? 'W' : 'B');
241 * fix suggested by Pedro Gimeno... original line
243 * eop->type = EOT_NOTHING;
248 nasm_realloc(eop
, sizeof(extop
) + eop
->stringlen
);
251 eop
->stringval
= (char *)eop
+ sizeof(extop
);
252 if (eop
->stringlen
< 4 ||
253 !float_const(tokval
.t_charptr
, sign
,
254 (uint8_t *)eop
->stringval
,
255 eop
->stringlen
, error
))
256 eop
->type
= EOT_NOTHING
;
257 i
= stdscan(NULL
, &tokval
); /* eat the comma */
265 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
,
266 critical
, error
, NULL
);
268 if (!value
) { /* error in evaluator */
269 result
->opcode
= -1; /* unrecoverable parse error: */
270 return result
; /* ignore this instruction */
272 if (is_unknown(value
)) {
273 eop
->type
= EOT_DB_NUMBER
;
274 eop
->offset
= 0; /* doesn't matter what we put */
275 eop
->segment
= eop
->wrt
= NO_SEG
; /* likewise */
276 } else if (is_reloc(value
)) {
277 eop
->type
= EOT_DB_NUMBER
;
278 eop
->offset
= reloc_value(value
);
279 eop
->segment
= reloc_seg(value
);
280 eop
->wrt
= reloc_wrt(value
);
283 "operand %d: expression is not simple"
284 " or relocatable", oper_num
);
289 * We're about to call stdscan(), which will eat the
290 * comma that we're currently sitting on between
291 * arguments. However, we'd better check first that it
294 if (i
== 0) /* also could be EOL */
297 error(ERR_NONFATAL
, "comma expected after operand %d",
299 result
->opcode
= -1; /* unrecoverable parse error: */
300 return result
; /* ignore this instruction */
304 if (result
->opcode
== I_INCBIN
) {
306 * Correct syntax for INCBIN is that there should be
307 * one string operand, followed by one or two numeric
310 if (!result
->eops
|| result
->eops
->type
!= EOT_DB_STRING
)
311 error(ERR_NONFATAL
, "`incbin' expects a file name");
312 else if (result
->eops
->next
&&
313 result
->eops
->next
->type
!= EOT_DB_NUMBER
)
314 error(ERR_NONFATAL
, "`incbin': second parameter is",
316 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
317 result
->eops
->next
->next
->type
!= EOT_DB_NUMBER
)
318 error(ERR_NONFATAL
, "`incbin': third parameter is",
320 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
321 result
->eops
->next
->next
->next
)
323 "`incbin': more than three parameters");
327 * If we reach here, one of the above errors happened.
328 * Throw the instruction away.
332 } else /* DB ... */ if (oper_num
== 0)
333 error(ERR_WARNING
| ERR_PASS1
,
334 "no operand for data declaration");
336 result
->operands
= oper_num
;
341 /* right. Now we begin to parse the operands. There may be up to three
342 * of these, separated by commas, and terminated by a zero token. */
344 for (operand
= 0; operand
< 3; operand
++) {
345 expr
*value
; /* used most of the time */
346 int mref
; /* is this going to be a memory ref? */
347 int bracket
; /* is it a [] mref, or a & mref? */
350 result
->oprs
[operand
].addr_size
= 0; /* have to zero this whatever */
351 result
->oprs
[operand
].eaflags
= 0; /* and this */
352 result
->oprs
[operand
].opflags
= 0;
354 i
= stdscan(NULL
, &tokval
);
356 break; /* end of operands: get out of here */
357 result
->oprs
[operand
].type
= 0; /* so far, no override */
358 while (i
== TOKEN_SPECIAL
) { /* size specifiers */
359 switch ((int)tokval
.t_integer
) {
361 if (!setsize
) /* we want to use only the first */
362 result
->oprs
[operand
].type
|= BITS8
;
367 result
->oprs
[operand
].type
|= BITS16
;
373 result
->oprs
[operand
].type
|= BITS32
;
378 result
->oprs
[operand
].type
|= BITS64
;
383 result
->oprs
[operand
].type
|= BITS80
;
387 result
->oprs
[operand
].type
|= TO
;
390 result
->oprs
[operand
].type
|= STRICT
;
393 result
->oprs
[operand
].type
|= FAR
;
396 result
->oprs
[operand
].type
|= NEAR
;
399 result
->oprs
[operand
].type
|= SHORT
;
402 error(ERR_NONFATAL
, "invalid operand size specification");
404 i
= stdscan(NULL
, &tokval
);
407 if (i
== '[' || i
== '&') { /* memory reference */
409 bracket
= (i
== '[');
410 while ((i
= stdscan(NULL
, &tokval
)) == TOKEN_SPECIAL
) {
411 /* check for address directives */
412 if (tasm_compatible_mode
) {
413 switch ((int)tokval
.t_integer
) {
414 /* For TASM compatibility a size override inside the
415 * brackets changes the size of the operand, not the
416 * address type of the operand as it does in standard
417 * NASM syntax. Hence:
419 * mov eax,[DWORD val]
421 * is valid syntax in TASM compatibility mode. Note that
422 * you lose the ability to override the default address
423 * type for the instruction, but we never use anything
424 * but 32-bit flat model addressing in our code.
427 result
->oprs
[operand
].type
|= BITS8
;
430 result
->oprs
[operand
].type
|= BITS16
;
434 result
->oprs
[operand
].type
|= BITS32
;
437 result
->oprs
[operand
].type
|= BITS64
;
440 result
->oprs
[operand
].type
|= BITS80
;
444 "invalid operand size specification");
447 /* Standard NASM compatible syntax */
448 switch ((int)tokval
.t_integer
) {
450 result
->oprs
[operand
].eaflags
|= EAF_TIMESTWO
;
453 result
->oprs
[operand
].eaflags
|= EAF_REL
;
456 result
->oprs
[operand
].eaflags
|= EAF_ABS
;
459 result
->oprs
[operand
].eaflags
|= EAF_BYTEOFFS
;
462 result
->oprs
[operand
].addr_size
= 16;
463 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
467 result
->oprs
[operand
].addr_size
= 32;
468 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
471 result
->oprs
[operand
].addr_size
= 64;
472 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
475 error(ERR_NONFATAL
, "invalid size specification in"
476 " effective address");
480 } else { /* immediate operand, or register */
482 bracket
= FALSE
; /* placate optimisers */
485 if ((result
->oprs
[operand
].type
& FAR
) && !mref
&&
486 result
->opcode
!= I_JMP
&& result
->opcode
!= I_CALL
) {
487 error(ERR_NONFATAL
, "invalid use of FAR operand specifier");
490 value
= evaluate(stdscan
, NULL
, &tokval
,
491 &result
->oprs
[operand
].opflags
,
492 critical
, error
, &hints
);
494 if (result
->oprs
[operand
].opflags
& OPFLAG_FORWARD
) {
495 result
->forw_ref
= TRUE
;
497 if (!value
) { /* error in evaluator */
498 result
->opcode
= -1; /* unrecoverable parse error: */
499 return result
; /* ignore this instruction */
501 if (i
== ':' && mref
) { /* it was seg:offset */
503 * Process the segment override.
505 if (value
[1].type
!= 0 || value
->value
!= 1 ||
506 REG_SREG
& ~reg_flags
[value
->type
])
507 error(ERR_NONFATAL
, "invalid segment override");
508 else if (result
->nprefix
== MAXPREFIX
)
510 "instruction has more than %d prefixes", MAXPREFIX
);
512 result
->prefixes
[result
->nprefix
++] = value
->type
;
513 if (!(REG_FSGS
& ~reg_flags
[value
->type
]))
514 result
->oprs
[operand
].eaflags
|= EAF_FSGS
;
517 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
518 if (i
== TOKEN_SPECIAL
) { /* another check for size override */
519 switch ((int)tokval
.t_integer
) {
521 result
->oprs
[operand
].addr_size
= 16;
525 result
->oprs
[operand
].addr_size
= 32;
528 result
->oprs
[operand
].addr_size
= 64;
531 error(ERR_NONFATAL
, "invalid size specification in"
532 " effective address");
534 i
= stdscan(NULL
, &tokval
);
536 value
= evaluate(stdscan
, NULL
, &tokval
,
537 &result
->oprs
[operand
].opflags
,
538 critical
, error
, &hints
);
540 if (result
->oprs
[operand
].opflags
& OPFLAG_FORWARD
) {
541 result
->forw_ref
= TRUE
;
543 /* and get the offset */
544 if (!value
) { /* but, error in evaluator */
545 result
->opcode
= -1; /* unrecoverable parse error: */
546 return result
; /* ignore this instruction */
549 if (mref
&& bracket
) { /* find ] at the end */
551 error(ERR_NONFATAL
, "parser: expecting ]");
552 do { /* error recovery again */
553 i
= stdscan(NULL
, &tokval
);
554 } while (i
!= 0 && i
!= ',');
555 } else /* we got the required ] */
556 i
= stdscan(NULL
, &tokval
);
557 } else { /* immediate operand */
558 if (i
!= 0 && i
!= ',' && i
!= ':') {
559 error(ERR_NONFATAL
, "comma or end of line expected");
560 do { /* error recovery */
561 i
= stdscan(NULL
, &tokval
);
562 } while (i
!= 0 && i
!= ',');
563 } else if (i
== ':') {
564 result
->oprs
[operand
].type
|= COLON
;
568 /* now convert the exprs returned from evaluate() into operand
571 if (mref
) { /* it's a memory reference */
573 int b
, i
, s
; /* basereg, indexreg, scale */
574 int64_t o
; /* offset */
576 b
= i
= -1, o
= s
= 0;
577 result
->oprs
[operand
].hintbase
= hints
.base
;
578 result
->oprs
[operand
].hinttype
= hints
.type
;
580 if (e
->type
&& e
->type
<= EXPR_REG_END
) { /* this bit's a register */
581 if (e
->value
== 1) /* in fact it can be basereg */
583 else /* no, it has to be indexreg */
584 i
= e
->type
, s
= e
->value
;
587 if (e
->type
&& e
->type
<= EXPR_REG_END
) { /* it's a 2nd register */
588 if (b
!= -1) /* If the first was the base, ... */
589 i
= e
->type
, s
= e
->value
; /* second has to be indexreg */
591 else if (e
->value
!= 1) { /* If both want to be index */
593 "beroset-p-592-invalid effective address");
600 if (e
->type
!= 0) { /* is there an offset? */
601 if (e
->type
<= EXPR_REG_END
) { /* in fact, is there an error? */
603 "beroset-p-603-invalid effective address");
607 if (e
->type
== EXPR_UNKNOWN
) {
608 o
= 0; /* doesn't matter what */
609 result
->oprs
[operand
].wrt
= NO_SEG
; /* nor this */
610 result
->oprs
[operand
].segment
= NO_SEG
; /* or this */
612 e
++; /* go to the end of the line */
614 if (e
->type
== EXPR_SIMPLE
) {
618 if (e
->type
== EXPR_WRT
) {
619 result
->oprs
[operand
].wrt
= e
->value
;
622 result
->oprs
[operand
].wrt
= NO_SEG
;
624 * Look for a segment base type.
626 if (e
->type
&& e
->type
< EXPR_SEGBASE
) {
628 "beroset-p-630-invalid effective address");
632 while (e
->type
&& e
->value
== 0)
634 if (e
->type
&& e
->value
!= 1) {
636 "beroset-p-637-invalid effective address");
641 result
->oprs
[operand
].segment
=
642 e
->type
- EXPR_SEGBASE
;
645 result
->oprs
[operand
].segment
= NO_SEG
;
646 while (e
->type
&& e
->value
== 0)
650 "beroset-p-650-invalid effective address");
658 result
->oprs
[operand
].wrt
= NO_SEG
;
659 result
->oprs
[operand
].segment
= NO_SEG
;
662 if (e
->type
!= 0) { /* there'd better be nothing left! */
664 "beroset-p-663-invalid effective address");
669 result
->oprs
[operand
].type
|= MEMORY
;
671 if (b
== -1 && (i
== -1 || s
== 0)) {
672 int is_rel
= globalbits
== 64 &&
673 !(result
->oprs
[operand
].eaflags
& EAF_ABS
) &&
675 !(result
->oprs
[operand
].eaflags
& EAF_FSGS
)) ||
676 (result
->oprs
[operand
].eaflags
& EAF_REL
));
678 result
->oprs
[operand
].type
|= is_rel
? IP_REL
: MEM_OFFS
;
680 result
->oprs
[operand
].basereg
= b
;
681 result
->oprs
[operand
].indexreg
= i
;
682 result
->oprs
[operand
].scale
= s
;
683 result
->oprs
[operand
].offset
= o
;
684 } else { /* it's not a memory reference */
686 if (is_just_unknown(value
)) { /* it's immediate but unknown */
687 result
->oprs
[operand
].type
|= IMMEDIATE
;
688 result
->oprs
[operand
].offset
= 0; /* don't care */
689 result
->oprs
[operand
].segment
= NO_SEG
; /* don't care again */
690 result
->oprs
[operand
].wrt
= NO_SEG
; /* still don't care */
691 } else if (is_reloc(value
)) { /* it's immediate */
692 result
->oprs
[operand
].type
|= IMMEDIATE
;
693 result
->oprs
[operand
].offset
= reloc_value(value
);
694 result
->oprs
[operand
].segment
= reloc_seg(value
);
695 result
->oprs
[operand
].wrt
= reloc_wrt(value
);
696 if (is_simple(value
)) {
697 if (reloc_value(value
) == 1)
698 result
->oprs
[operand
].type
|= UNITY
;
699 if (optimizing
>= 0 &&
700 !(result
->oprs
[operand
].type
& STRICT
)) {
701 if (reloc_value(value
) >= -128 &&
702 reloc_value(value
) <= 127)
703 result
->oprs
[operand
].type
|= SBYTE
;
706 } else { /* it's a register */
708 if (value
->type
>= EXPR_SIMPLE
|| value
->value
!= 1) {
709 error(ERR_NONFATAL
, "invalid operand type");
715 * check that its only 1 register, not an expression...
717 for (i
= 1; value
[i
].type
; i
++)
718 if (value
[i
].value
) {
719 error(ERR_NONFATAL
, "invalid operand type");
724 /* clear overrides, except TO which applies to FPU regs */
725 if (result
->oprs
[operand
].type
& ~TO
) {
727 * we want to produce a warning iff the specified size
728 * is different from the register size
730 i
= result
->oprs
[operand
].type
& SIZE_MASK
;
734 result
->oprs
[operand
].type
&= TO
;
735 result
->oprs
[operand
].type
|= REGISTER
;
736 result
->oprs
[operand
].type
|= reg_flags
[value
->type
];
737 result
->oprs
[operand
].basereg
= value
->type
;
739 if (i
&& (result
->oprs
[operand
].type
& SIZE_MASK
) != i
)
740 error(ERR_WARNING
| ERR_PASS1
,
741 "register size specification ignored");
746 result
->operands
= operand
; /* set operand count */
748 while (operand
< 3) /* clear remaining operands */
749 result
->oprs
[operand
++].type
= 0;
752 * Transform RESW, RESD, RESQ, REST into RESB.
754 switch (result
->opcode
) {
756 result
->opcode
= I_RESB
;
757 result
->oprs
[0].offset
*= 2;
760 result
->opcode
= I_RESB
;
761 result
->oprs
[0].offset
*= 4;
764 result
->opcode
= I_RESB
;
765 result
->oprs
[0].offset
*= 8;
768 result
->opcode
= I_RESB
;
769 result
->oprs
[0].offset
*= 10;
776 static int is_comma_next(void)
783 i
= stdscan(NULL
, &tv
);
785 return (i
== ',' || i
== ';' || !i
);
788 void cleanup_insn(insn
* i
)
794 i
->eops
= i
->eops
->next
;