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
23 extern int in_abs_seg
; /* ABSOLUTE segment flag */
24 extern long abs_seg
; /* ABSOLUTE segment */
25 extern long abs_offset
; /* ABSOLUTE segment offset */
27 #include "regflags.c" /* List of register flags */
29 enum { /* special tokens */
30 S_BYTE
, S_DWORD
, S_FAR
, S_LONG
, S_NEAR
, S_NOSPLIT
, S_QWORD
,
31 S_SHORT
, S_STRICT
, S_TO
, S_TWORD
, S_WORD
34 static int is_comma_next (void);
37 static struct tokenval tokval
;
39 static struct ofmt
*outfmt
; /* Structure of addresses of output routines */
40 static loc_t
*location
; /* Pointer to current line's segment,offset */
42 void parser_global_info (struct ofmt
*output
, loc_t
*locp
)
48 insn
*parse_line (int pass
, char *buffer
, insn
*result
,
49 efunc errfunc
, evalfunc evaluate
, ldfunc ldef
)
53 struct eval_hints hints
;
55 result
->forw_ref
= FALSE
;
59 stdscan_bufptr
= buffer
;
60 i
= stdscan(NULL
, &tokval
);
62 result
->label
= NULL
; /* Assume no label */
63 result
->eops
= NULL
; /* must do this, whatever happens */
64 result
->operands
= 0; /* must initialise this */
66 if (i
==0) { /* blank line - ignore */
67 result
->opcode
= -1; /* and no instruction either */
70 if (i
!= TOKEN_ID
&& i
!= TOKEN_INSN
&& i
!= TOKEN_PREFIX
&&
71 (i
!=TOKEN_REG
|| (REG_SREG
& ~reg_flags
[tokval
.t_integer
]))) {
72 error (ERR_NONFATAL
, "label or instruction expected"
78 if (i
== TOKEN_ID
) { /* there's a label here */
79 result
->label
= tokval
.t_charptr
;
80 i
= stdscan(NULL
, &tokval
);
81 if (i
== ':') { /* skip over the optional colon */
82 i
= stdscan(NULL
, &tokval
);
84 error (ERR_WARNING
|ERR_WARN_OL
|ERR_PASS1
,
85 "label alone on a line without a colon might be in error");
87 if (i
!= TOKEN_INSN
|| tokval
.t_integer
!= I_EQU
)
90 * FIXME: location->segment could be NO_SEG, in which case
91 * it is possible we should be passing 'abs_seg'. Look into this.
92 * Work out whether that is *really* what we should be doing.
93 * Generally fix things. I think this is right as it is, but
94 * am still not certain.
96 ldef (result
->label
, in_abs_seg
?abs_seg
:location
->segment
,
97 location
->offset
, NULL
, TRUE
, FALSE
, outfmt
, errfunc
);
102 result
->opcode
= -1; /* this line contains just a label */
109 while (i
== TOKEN_PREFIX
||
110 (i
==TOKEN_REG
&& !(REG_SREG
& ~reg_flags
[tokval
.t_integer
])))
113 * Handle special case: the TIMES prefix.
115 if (i
== TOKEN_PREFIX
&& tokval
.t_integer
== P_TIMES
) {
118 i
= stdscan(NULL
, &tokval
);
119 value
= evaluate (stdscan
, NULL
, &tokval
, NULL
, pass0
, error
, NULL
);
121 if (!value
) { /* but, error in evaluator */
122 result
->opcode
= -1; /* unrecoverable parse error: */
123 return result
; /* ignore this instruction */
125 if (!is_simple (value
)) {
127 "non-constant argument supplied to TIMES");
130 result
->times
= value
->value
;
131 if (value
->value
< 0) {
132 error(ERR_NONFATAL
, "TIMES value %d is negative",
138 if (result
->nprefix
== MAXPREFIX
)
140 "instruction has more than %d prefixes", MAXPREFIX
);
142 result
->prefixes
[result
->nprefix
++] = tokval
.t_integer
;
143 i
= stdscan(NULL
, &tokval
);
147 if (i
!= TOKEN_INSN
) {
148 if (result
->nprefix
> 0 && i
== 0) {
150 * Instruction prefixes are present, but no actual
151 * instruction. This is allowed: at this point we
152 * invent a notional instruction of RESB 0.
154 result
->opcode
= I_RESB
;
155 result
->operands
= 1;
156 result
->oprs
[0].type
= IMMEDIATE
;
157 result
->oprs
[0].offset
= 0L;
158 result
->oprs
[0].segment
= result
->oprs
[0].wrt
= NO_SEG
;
161 error (ERR_NONFATAL
, "parser: instruction expected");
167 result
->opcode
= tokval
.t_integer
;
168 result
->condition
= tokval
.t_inttwo
;
171 * RESB, RESW and RESD cannot be satisfied with incorrectly
172 * evaluated operands, since the correct values _must_ be known
173 * on the first pass. Hence, even in pass one, we set the
174 * `critical' flag on calling evaluate(), so that it will bomb
175 * out on undefined symbols. Nasty, but there's nothing we can
178 * For the moment, EQU has the same difficulty, so we'll
181 if (result
->opcode
== I_RESB
||
182 result
->opcode
== I_RESW
||
183 result
->opcode
== I_RESD
||
184 result
->opcode
== I_RESQ
||
185 result
->opcode
== I_REST
||
186 result
->opcode
== I_EQU
||
187 result
->opcode
== I_INCBIN
) /* fbk */
192 critical
= (pass
==2 ? 2 : 0);
194 if (result
->opcode
== I_DB
||
195 result
->opcode
== I_DW
||
196 result
->opcode
== I_DD
||
197 result
->opcode
== I_DQ
||
198 result
->opcode
== I_DT
||
199 result
->opcode
== I_INCBIN
)
201 extop
*eop
, **tail
= &result
->eops
, **fixptr
;
204 result
->eops_float
= FALSE
;
207 * Begin to read the DB/DW/DD/DQ/DT/INCBIN operands.
210 i
= stdscan(NULL
, &tokval
);
214 eop
= *tail
= nasm_malloc(sizeof(extop
));
217 eop
->type
= EOT_NOTHING
;
220 if (i
== TOKEN_NUM
&& tokval
.t_charptr
&& is_comma_next()) {
221 eop
->type
= EOT_DB_STRING
;
222 eop
->stringval
= tokval
.t_charptr
;
223 eop
->stringlen
= tokval
.t_inttwo
;
224 i
= stdscan(NULL
, &tokval
); /* eat the comma */
228 if ((i
== TOKEN_FLOAT
&& is_comma_next()) || i
== '-') {
232 char *save
= stdscan_bufptr
;
233 i
= stdscan(NULL
, &tokval
);
235 if (i
!= TOKEN_FLOAT
|| !is_comma_next()) {
236 stdscan_bufptr
= save
;
237 i
= tokval
.t_type
= '-';
241 if (i
== TOKEN_FLOAT
) {
242 eop
->type
= EOT_DB_STRING
;
243 result
->eops_float
= TRUE
;
244 if (result
->opcode
== I_DD
)
246 else if (result
->opcode
== I_DQ
)
248 else if (result
->opcode
== I_DT
)
251 error(ERR_NONFATAL
, "floating-point constant"
252 " encountered in `D%c' instruction",
253 result
->opcode
== I_DW
? 'W' : 'B');
255 * fix suggested by Pedro Gimeno... original line
257 * eop->type = EOT_NOTHING;
261 eop
= nasm_realloc(eop
, sizeof(extop
)+eop
->stringlen
);
264 eop
->stringval
= (char *)eop
+ sizeof(extop
);
265 if (eop
->stringlen
< 4 ||
266 !float_const (tokval
.t_charptr
, sign
,
267 (unsigned char *)eop
->stringval
,
268 eop
->stringlen
, error
))
269 eop
->type
= EOT_NOTHING
;
270 i
= stdscan(NULL
, &tokval
); /* eat the comma */
278 value
= evaluate (stdscan
, NULL
, &tokval
, NULL
,
279 critical
, error
, NULL
);
281 if (!value
) { /* error in evaluator */
282 result
->opcode
= -1;/* unrecoverable parse error: */
283 return result
; /* ignore this instruction */
285 if (is_unknown(value
)) {
286 eop
->type
= EOT_DB_NUMBER
;
287 eop
->offset
= 0; /* doesn't matter what we put */
288 eop
->segment
= eop
->wrt
= NO_SEG
; /* likewise */
289 } else if (is_reloc(value
)) {
290 eop
->type
= EOT_DB_NUMBER
;
291 eop
->offset
= reloc_value(value
);
292 eop
->segment
= reloc_seg(value
);
293 eop
->wrt
= reloc_wrt(value
);
296 "operand %d: expression is not simple"
297 " or relocatable", oper_num
);
302 * We're about to call stdscan(), which will eat the
303 * comma that we're currently sitting on between
304 * arguments. However, we'd better check first that it
307 if (i
== 0) /* also could be EOL */
310 error (ERR_NONFATAL
, "comma expected after operand %d",
312 result
->opcode
= -1;/* unrecoverable parse error: */
313 return result
; /* ignore this instruction */
317 if (result
->opcode
== I_INCBIN
) {
319 * Correct syntax for INCBIN is that there should be
320 * one string operand, followed by one or two numeric
323 if (!result
->eops
|| result
->eops
->type
!= EOT_DB_STRING
)
324 error (ERR_NONFATAL
, "`incbin' expects a file name");
325 else if (result
->eops
->next
&&
326 result
->eops
->next
->type
!= EOT_DB_NUMBER
)
327 error (ERR_NONFATAL
, "`incbin': second parameter is",
329 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
330 result
->eops
->next
->next
->type
!= EOT_DB_NUMBER
)
331 error (ERR_NONFATAL
, "`incbin': third parameter is",
333 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
334 result
->eops
->next
->next
->next
)
335 error (ERR_NONFATAL
, "`incbin': more than three parameters");
339 * If we reach here, one of the above errors happened.
340 * Throw the instruction away.
346 error (ERR_WARNING
|ERR_PASS1
,
347 "no operand for data declaration");
349 result
->operands
= oper_num
;
354 /* right. Now we begin to parse the operands. There may be up to three
355 * of these, separated by commas, and terminated by a zero token. */
357 for (operand
= 0; operand
< 3; operand
++) {
358 expr
*value
; /* used most of the time */
359 int mref
; /* is this going to be a memory ref? */
360 int bracket
; /* is it a [] mref, or a & mref? */
363 result
->oprs
[operand
].addr_size
= 0;/* have to zero this whatever */
364 result
->oprs
[operand
].eaflags
= 0; /* and this */
365 result
->oprs
[operand
].opflags
= 0;
367 i
= stdscan(NULL
, &tokval
);
368 if (i
== 0) break; /* end of operands: get out of here */
369 result
->oprs
[operand
].type
= 0; /* so far, no override */
370 while (i
== TOKEN_SPECIAL
) {/* size specifiers */
371 switch ((int)tokval
.t_integer
) {
373 if (!setsize
) /* we want to use only the first */
374 result
->oprs
[operand
].type
|= BITS8
;
379 result
->oprs
[operand
].type
|= BITS16
;
385 result
->oprs
[operand
].type
|= BITS32
;
390 result
->oprs
[operand
].type
|= BITS64
;
395 result
->oprs
[operand
].type
|= BITS80
;
399 result
->oprs
[operand
].type
|= TO
;
402 result
->oprs
[operand
].type
|= STRICT
;
405 result
->oprs
[operand
].type
|= FAR
;
408 result
->oprs
[operand
].type
|= NEAR
;
411 result
->oprs
[operand
].type
|= SHORT
;
414 error (ERR_NONFATAL
, "invalid operand size specification");
416 i
= stdscan(NULL
, &tokval
);
419 if (i
== '[' || i
== '&') { /* memory reference */
421 bracket
= (i
== '[');
422 i
= stdscan(NULL
, &tokval
);
423 if (i
== TOKEN_SPECIAL
) { /* check for address size override */
424 if (tasm_compatible_mode
) {
425 switch ((int)tokval
.t_integer
) {
426 /* For TASM compatibility a size override inside the
427 * brackets changes the size of the operand, not the
428 * address type of the operand as it does in standard
429 * NASM syntax. Hence:
431 * mov eax,[DWORD val]
433 * is valid syntax in TASM compatibility mode. Note that
434 * you lose the ability to override the default address
435 * type for the instruction, but we never use anything
436 * but 32-bit flat model addressing in our code.
439 result
->oprs
[operand
].type
|= BITS8
;
442 result
->oprs
[operand
].type
|= BITS16
;
446 result
->oprs
[operand
].type
|= BITS32
;
449 result
->oprs
[operand
].type
|= BITS64
;
452 result
->oprs
[operand
].type
|= BITS80
;
455 error (ERR_NONFATAL
, "invalid operand size specification");
458 /* Standard NASM compatible syntax */
459 switch ((int)tokval
.t_integer
) {
461 result
->oprs
[operand
].eaflags
|= EAF_TIMESTWO
;
464 result
->oprs
[operand
].eaflags
|= EAF_BYTEOFFS
;
467 result
->oprs
[operand
].addr_size
= 16;
468 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
472 result
->oprs
[operand
].addr_size
= 32;
473 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
476 error (ERR_NONFATAL
, "invalid size specification in"
477 " effective address");
480 i
= stdscan(NULL
, &tokval
);
482 } else { /* immediate operand, or register */
484 bracket
= FALSE
; /* placate optimisers */
487 if((result
->oprs
[operand
].type
& FAR
) && !mref
&&
488 result
->opcode
!= I_JMP
&& result
->opcode
!= I_CALL
)
490 error (ERR_NONFATAL
, "invalid use of FAR operand specifier");
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 if (!value
) { /* error in evaluator */
501 result
->opcode
= -1; /* unrecoverable parse error: */
502 return result
; /* ignore this instruction */
504 if (i
== ':' && mref
) { /* it was seg:offset */
506 * Process the segment override.
508 if (value
[1].type
!=0 || value
->value
!=1 ||
509 REG_SREG
& ~reg_flags
[value
->type
])
510 error (ERR_NONFATAL
, "invalid segment override");
511 else if (result
->nprefix
== MAXPREFIX
)
513 "instruction has more than %d prefixes",
516 result
->prefixes
[result
->nprefix
++] = value
->type
;
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 error (ERR_NONFATAL
, "invalid size specification in"
530 " effective address");
532 i
= stdscan(NULL
, &tokval
);
534 value
= evaluate (stdscan
, NULL
, &tokval
,
535 &result
->oprs
[operand
].opflags
,
536 critical
, error
, &hints
);
538 if (result
->oprs
[operand
].opflags
& OPFLAG_FORWARD
) {
539 result
->forw_ref
= TRUE
;
541 /* and get the offset */
542 if (!value
) { /* but, error in evaluator */
543 result
->opcode
= -1; /* unrecoverable parse error: */
544 return result
; /* ignore this instruction */
547 if (mref
&& bracket
) { /* find ] at the end */
549 error (ERR_NONFATAL
, "parser: expecting ]");
550 do { /* error recovery again */
551 i
= stdscan(NULL
, &tokval
);
552 } while (i
!= 0 && i
!= ',');
553 } else /* we got the required ] */
554 i
= stdscan(NULL
, &tokval
);
555 } else { /* immediate operand */
556 if (i
!= 0 && i
!= ',' && i
!= ':') {
557 error (ERR_NONFATAL
, "comma or end of line expected");
558 do { /* error recovery */
559 i
= stdscan(NULL
, &tokval
);
560 } while (i
!= 0 && i
!= ',');
561 } else if (i
== ':') {
562 result
->oprs
[operand
].type
|= COLON
;
566 /* now convert the exprs returned from evaluate() into operand
569 if (mref
) { /* it's a memory reference */
571 int b
, i
, s
; /* basereg, indexreg, scale */
574 b
= i
= -1, o
= s
= 0;
575 result
->oprs
[operand
].hintbase
= hints
.base
;
576 result
->oprs
[operand
].hinttype
= hints
.type
;
578 if (e
->type
&& e
->type
<= EXPR_REG_END
) /* this bit's a register */
580 if (e
->value
== 1) /* in fact it can be basereg */
582 else /* no, it has to be indexreg */
583 i
= e
->type
, s
= e
->value
;
586 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 error(ERR_NONFATAL
, "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 error (ERR_NONFATAL
, "beroset-p-603-invalid effective address");
610 if (e
->type
== EXPR_UNKNOWN
) {
611 o
= 0; /* doesn't matter what */
612 result
->oprs
[operand
].wrt
= NO_SEG
; /* nor this */
613 result
->oprs
[operand
].segment
= NO_SEG
; /* or this */
614 while (e
->type
) e
++; /* go to the end of the line */
618 if (e
->type
== EXPR_SIMPLE
) {
622 if (e
->type
== EXPR_WRT
) {
623 result
->oprs
[operand
].wrt
= e
->value
;
626 result
->oprs
[operand
].wrt
= NO_SEG
;
628 * Look for a segment base type.
630 if (e
->type
&& e
->type
< EXPR_SEGBASE
) {
631 error (ERR_NONFATAL
, "beroset-p-630-invalid effective address");
635 while (e
->type
&& e
->value
== 0)
637 if (e
->type
&& e
->value
!= 1) {
638 error (ERR_NONFATAL
, "beroset-p-637-invalid effective address");
643 result
->oprs
[operand
].segment
=
644 e
->type
- EXPR_SEGBASE
;
647 result
->oprs
[operand
].segment
= NO_SEG
;
648 while (e
->type
&& e
->value
== 0)
651 error (ERR_NONFATAL
, "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! */
664 error (ERR_NONFATAL
, "beroset-p-663-invalid effective address");
669 result
->oprs
[operand
].type
|= MEMORY
;
670 if (b
==-1 && (i
==-1 || s
==0))
671 result
->oprs
[operand
].type
|= MEM_OFFS
;
672 result
->oprs
[operand
].basereg
= b
;
673 result
->oprs
[operand
].indexreg
= i
;
674 result
->oprs
[operand
].scale
= s
;
675 result
->oprs
[operand
].offset
= o
;
677 else /* it's not a memory reference */
679 if (is_just_unknown(value
)) { /* it's immediate but unknown */
680 result
->oprs
[operand
].type
|= IMMEDIATE
;
681 result
->oprs
[operand
].offset
= 0; /* don't care */
682 result
->oprs
[operand
].segment
= NO_SEG
; /* don't care again */
683 result
->oprs
[operand
].wrt
= NO_SEG
;/* still don't care */
685 else if (is_reloc(value
)) /* it's immediate */
687 result
->oprs
[operand
].type
|= IMMEDIATE
;
688 result
->oprs
[operand
].offset
= reloc_value(value
);
689 result
->oprs
[operand
].segment
= reloc_seg(value
);
690 result
->oprs
[operand
].wrt
= reloc_wrt(value
);
691 if (is_simple(value
)) {
692 if (reloc_value(value
)==1)
693 result
->oprs
[operand
].type
|= UNITY
;
695 !(result
->oprs
[operand
].type
& STRICT
)) {
696 if (reloc_value(value
) >= -128 &&
697 reloc_value(value
) <= 127)
698 result
->oprs
[operand
].type
|= SBYTE
;
702 else /* it's a register */
704 if (value
->type
>=EXPR_SIMPLE
|| value
->value
!=1) {
705 error (ERR_NONFATAL
, "invalid operand type");
711 * check that its only 1 register, not an expression...
713 for (i
= 1; value
[i
].type
; i
++)
714 if (value
[i
].value
) {
715 error (ERR_NONFATAL
, "invalid operand type");
720 /* clear overrides, except TO which applies to FPU regs */
721 if (result
->oprs
[operand
].type
& ~TO
) {
723 * we want to produce a warning iff the specified size
724 * is different from the register size
726 i
= result
->oprs
[operand
].type
& SIZE_MASK
;
731 result
->oprs
[operand
].type
&= TO
;
732 result
->oprs
[operand
].type
|= REGISTER
;
733 result
->oprs
[operand
].type
|= reg_flags
[value
->type
];
734 result
->oprs
[operand
].basereg
= value
->type
;
736 if (i
&& (result
->oprs
[operand
].type
& SIZE_MASK
) != i
)
737 error (ERR_WARNING
|ERR_PASS1
,
738 "register size specification ignored");
743 result
->operands
= operand
; /* set operand count */
745 while (operand
<3) /* clear remaining operands */
746 result
->oprs
[operand
++].type
= 0;
749 * Transform RESW, RESD, RESQ, REST into RESB.
751 switch (result
->opcode
) {
752 case I_RESW
: result
->opcode
=I_RESB
; result
->oprs
[0].offset
*=2; break;
753 case I_RESD
: result
->opcode
=I_RESB
; result
->oprs
[0].offset
*=4; break;
754 case I_RESQ
: result
->opcode
=I_RESB
; result
->oprs
[0].offset
*=8; break;
755 case I_REST
: result
->opcode
=I_RESB
; result
->oprs
[0].offset
*=10; break;
761 static int is_comma_next (void)
768 i
= stdscan (NULL
, &tv
);
770 return (i
== ',' || i
== ';' || !i
);
773 void cleanup_insn (insn
*i
)
779 i
->eops
= i
->eops
->next
;