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 enum { /* special tokens */
31 S_BYTE
, S_DWORD
, S_FAR
, S_LONG
, S_NEAR
, S_NOSPLIT
, S_QWORD
,
32 S_SHORT
, S_STRICT
, S_TO
, S_TWORD
, S_WORD
35 static int is_comma_next(void);
38 static struct tokenval tokval
;
40 static struct ofmt
*outfmt
; /* Structure of addresses of output routines */
41 static loc_t
*location
; /* Pointer to current line's segment,offset */
43 void parser_global_info(struct ofmt
*output
, loc_t
* locp
)
49 insn
*parse_line(int pass
, char *buffer
, insn
* result
,
50 efunc errfunc
, evalfunc evaluate
, ldfunc ldef
)
54 struct eval_hints hints
;
56 result
->forw_ref
= FALSE
;
60 stdscan_bufptr
= buffer
;
61 i
= stdscan(NULL
, &tokval
);
63 result
->label
= NULL
; /* Assume no label */
64 result
->eops
= NULL
; /* must do this, whatever happens */
65 result
->operands
= 0; /* must initialize this */
67 if (i
== 0) { /* blank line - ignore */
68 result
->opcode
= -1; /* and no instruction either */
71 if (i
!= TOKEN_ID
&& i
!= TOKEN_INSN
&& i
!= TOKEN_PREFIX
&&
72 (i
!= TOKEN_REG
|| (REG_SREG
& ~reg_flags
[tokval
.t_integer
]))) {
73 error(ERR_NONFATAL
, "label or instruction expected"
79 if (i
== TOKEN_ID
) { /* there's a label here */
80 result
->label
= tokval
.t_charptr
;
81 i
= stdscan(NULL
, &tokval
);
82 if (i
== ':') { /* skip over the optional colon */
83 i
= stdscan(NULL
, &tokval
);
85 error(ERR_WARNING
| ERR_WARN_OL
| ERR_PASS1
,
86 "label alone on a line without a colon might be in error");
88 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
);
120 evaluate(stdscan
, NULL
, &tokval
, NULL
, pass0
, error
, NULL
);
122 if (!value
) { /* but, error in evaluator */
123 result
->opcode
= -1; /* unrecoverable parse error: */
124 return result
; /* ignore this instruction */
126 if (!is_simple(value
)) {
128 "non-constant argument supplied to TIMES");
131 result
->times
= value
->value
;
132 if (value
->value
< 0) {
133 error(ERR_NONFATAL
, "TIMES value %d is negative",
139 if (result
->nprefix
== MAXPREFIX
)
141 "instruction has more than %d prefixes", MAXPREFIX
);
143 result
->prefixes
[result
->nprefix
++] = tokval
.t_integer
;
144 i
= stdscan(NULL
, &tokval
);
148 if (i
!= TOKEN_INSN
) {
149 if (result
->nprefix
> 0 && i
== 0) {
151 * Instruction prefixes are present, but no actual
152 * instruction. This is allowed: at this point we
153 * invent a notional instruction of RESB 0.
155 result
->opcode
= I_RESB
;
156 result
->operands
= 1;
157 result
->oprs
[0].type
= IMMEDIATE
;
158 result
->oprs
[0].offset
= 0L;
159 result
->oprs
[0].segment
= result
->oprs
[0].wrt
= NO_SEG
;
162 error(ERR_NONFATAL
, "parser: instruction expected");
168 result
->opcode
= tokval
.t_integer
;
169 result
->condition
= tokval
.t_inttwo
;
172 * RESB, RESW and RESD cannot be satisfied with incorrectly
173 * evaluated operands, since the correct values _must_ be known
174 * on the first pass. Hence, even in pass one, we set the
175 * `critical' flag on calling evaluate(), so that it will bomb
176 * out on undefined symbols. Nasty, but there's nothing we can
179 * For the moment, EQU has the same difficulty, so we'll
182 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 */
185 critical
= (pass
== 2 ? 2 : 0);
187 if (result
->opcode
== I_DB
||
188 result
->opcode
== I_DW
||
189 result
->opcode
== I_DD
||
190 result
->opcode
== I_DQ
||
191 result
->opcode
== I_DT
|| 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/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()) || i
== '-') {
223 char *save
= stdscan_bufptr
;
224 i
= stdscan(NULL
, &tokval
);
226 if (i
!= TOKEN_FLOAT
|| !is_comma_next()) {
227 stdscan_bufptr
= save
;
228 i
= tokval
.t_type
= '-';
232 if (i
== TOKEN_FLOAT
) {
233 eop
->type
= EOT_DB_STRING
;
234 result
->eops_float
= TRUE
;
235 if (result
->opcode
== I_DD
)
237 else if (result
->opcode
== I_DQ
)
239 else if (result
->opcode
== I_DT
)
242 error(ERR_NONFATAL
, "floating-point constant"
243 " encountered in `D%c' instruction",
244 result
->opcode
== I_DW
? 'W' : 'B');
246 * fix suggested by Pedro Gimeno... original line
248 * eop->type = EOT_NOTHING;
253 nasm_realloc(eop
, sizeof(extop
) + eop
->stringlen
);
256 eop
->stringval
= (char *)eop
+ sizeof(extop
);
257 if (eop
->stringlen
< 4 ||
258 !float_const(tokval
.t_charptr
, sign
,
259 (uint8_t *)eop
->stringval
,
260 eop
->stringlen
, error
))
261 eop
->type
= EOT_NOTHING
;
262 i
= stdscan(NULL
, &tokval
); /* eat the comma */
270 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
,
271 critical
, error
, NULL
);
273 if (!value
) { /* error in evaluator */
274 result
->opcode
= -1; /* unrecoverable parse error: */
275 return result
; /* ignore this instruction */
277 if (is_unknown(value
)) {
278 eop
->type
= EOT_DB_NUMBER
;
279 eop
->offset
= 0; /* doesn't matter what we put */
280 eop
->segment
= eop
->wrt
= NO_SEG
; /* likewise */
281 } else if (is_reloc(value
)) {
282 eop
->type
= EOT_DB_NUMBER
;
283 eop
->offset
= reloc_value(value
);
284 eop
->segment
= reloc_seg(value
);
285 eop
->wrt
= reloc_wrt(value
);
288 "operand %d: expression is not simple"
289 " or relocatable", oper_num
);
294 * We're about to call stdscan(), which will eat the
295 * comma that we're currently sitting on between
296 * arguments. However, we'd better check first that it
299 if (i
== 0) /* also could be EOL */
302 error(ERR_NONFATAL
, "comma expected after operand %d",
304 result
->opcode
= -1; /* unrecoverable parse error: */
305 return result
; /* ignore this instruction */
309 if (result
->opcode
== I_INCBIN
) {
311 * Correct syntax for INCBIN is that there should be
312 * one string operand, followed by one or two numeric
315 if (!result
->eops
|| result
->eops
->type
!= EOT_DB_STRING
)
316 error(ERR_NONFATAL
, "`incbin' expects a file name");
317 else if (result
->eops
->next
&&
318 result
->eops
->next
->type
!= EOT_DB_NUMBER
)
319 error(ERR_NONFATAL
, "`incbin': second parameter is",
321 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
322 result
->eops
->next
->next
->type
!= EOT_DB_NUMBER
)
323 error(ERR_NONFATAL
, "`incbin': third parameter is",
325 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
326 result
->eops
->next
->next
->next
)
328 "`incbin': more than three parameters");
332 * If we reach here, one of the above errors happened.
333 * Throw the instruction away.
337 } else /* DB ... */ if (oper_num
== 0)
338 error(ERR_WARNING
| ERR_PASS1
,
339 "no operand for data declaration");
341 result
->operands
= oper_num
;
346 /* right. Now we begin to parse the operands. There may be up to three
347 * of these, separated by commas, and terminated by a zero token. */
349 for (operand
= 0; operand
< 3; operand
++) {
350 expr
*value
; /* used most of the time */
351 int mref
; /* is this going to be a memory ref? */
352 int bracket
; /* is it a [] mref, or a & mref? */
355 result
->oprs
[operand
].addr_size
= 0; /* have to zero this whatever */
356 result
->oprs
[operand
].eaflags
= 0; /* and this */
357 result
->oprs
[operand
].opflags
= 0;
359 i
= stdscan(NULL
, &tokval
);
361 break; /* end of operands: get out of here */
362 result
->oprs
[operand
].type
= 0; /* so far, no override */
363 while (i
== TOKEN_SPECIAL
) { /* size specifiers */
364 switch ((int)tokval
.t_integer
) {
366 if (!setsize
) /* we want to use only the first */
367 result
->oprs
[operand
].type
|= BITS8
;
372 result
->oprs
[operand
].type
|= BITS16
;
378 result
->oprs
[operand
].type
|= BITS32
;
383 result
->oprs
[operand
].type
|= BITS64
;
388 result
->oprs
[operand
].type
|= BITS80
;
392 result
->oprs
[operand
].type
|= TO
;
395 result
->oprs
[operand
].type
|= STRICT
;
398 result
->oprs
[operand
].type
|= FAR
;
401 result
->oprs
[operand
].type
|= NEAR
;
404 result
->oprs
[operand
].type
|= SHORT
;
407 error(ERR_NONFATAL
, "invalid operand size specification");
409 i
= stdscan(NULL
, &tokval
);
412 if (i
== '[' || i
== '&') { /* memory reference */
414 bracket
= (i
== '[');
415 i
= stdscan(NULL
, &tokval
);
416 if (i
== TOKEN_SPECIAL
) { /* check for address size override */
417 if (tasm_compatible_mode
) {
418 switch ((int)tokval
.t_integer
) {
419 /* For TASM compatibility a size override inside the
420 * brackets changes the size of the operand, not the
421 * address type of the operand as it does in standard
422 * NASM syntax. Hence:
424 * mov eax,[DWORD val]
426 * is valid syntax in TASM compatibility mode. Note that
427 * you lose the ability to override the default address
428 * type for the instruction, but we never use anything
429 * but 32-bit flat model addressing in our code.
432 result
->oprs
[operand
].type
|= BITS8
;
435 result
->oprs
[operand
].type
|= BITS16
;
439 result
->oprs
[operand
].type
|= BITS32
;
442 result
->oprs
[operand
].type
|= BITS64
;
445 result
->oprs
[operand
].type
|= BITS80
;
449 "invalid operand size specification");
452 /* Standard NASM compatible syntax */
453 switch ((int)tokval
.t_integer
) {
455 result
->oprs
[operand
].eaflags
|= EAF_TIMESTWO
;
458 result
->oprs
[operand
].eaflags
|= EAF_BYTEOFFS
;
461 result
->oprs
[operand
].addr_size
= 16;
462 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
466 result
->oprs
[operand
].addr_size
= 32;
467 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
470 error(ERR_NONFATAL
, "invalid size specification in"
471 " effective address");
474 i
= stdscan(NULL
, &tokval
);
476 } else { /* immediate operand, or register */
478 bracket
= FALSE
; /* placate optimisers */
481 if ((result
->oprs
[operand
].type
& FAR
) && !mref
&&
482 result
->opcode
!= I_JMP
&& result
->opcode
!= I_CALL
) {
483 error(ERR_NONFATAL
, "invalid use of FAR operand specifier");
486 value
= evaluate(stdscan
, NULL
, &tokval
,
487 &result
->oprs
[operand
].opflags
,
488 critical
, error
, &hints
);
490 if (result
->oprs
[operand
].opflags
& OPFLAG_FORWARD
) {
491 result
->forw_ref
= TRUE
;
493 if (!value
) { /* error in evaluator */
494 result
->opcode
= -1; /* unrecoverable parse error: */
495 return result
; /* ignore this instruction */
497 if (i
== ':' && mref
) { /* it was seg:offset */
499 * Process the segment override.
501 if (value
[1].type
!= 0 || value
->value
!= 1 ||
502 REG_SREG
& ~reg_flags
[value
->type
])
503 error(ERR_NONFATAL
, "invalid segment override");
504 else if (result
->nprefix
== MAXPREFIX
)
506 "instruction has more than %d prefixes", MAXPREFIX
);
508 result
->prefixes
[result
->nprefix
++] = value
->type
;
510 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
511 if (i
== TOKEN_SPECIAL
) { /* another check for size override */
512 switch ((int)tokval
.t_integer
) {
514 result
->oprs
[operand
].addr_size
= 16;
518 result
->oprs
[operand
].addr_size
= 32;
521 error(ERR_NONFATAL
, "invalid size specification in"
522 " effective address");
524 i
= stdscan(NULL
, &tokval
);
526 value
= evaluate(stdscan
, NULL
, &tokval
,
527 &result
->oprs
[operand
].opflags
,
528 critical
, error
, &hints
);
530 if (result
->oprs
[operand
].opflags
& OPFLAG_FORWARD
) {
531 result
->forw_ref
= TRUE
;
533 /* and get the offset */
534 if (!value
) { /* but, error in evaluator */
535 result
->opcode
= -1; /* unrecoverable parse error: */
536 return result
; /* ignore this instruction */
539 if (mref
&& bracket
) { /* find ] at the end */
541 error(ERR_NONFATAL
, "parser: expecting ]");
542 do { /* error recovery again */
543 i
= stdscan(NULL
, &tokval
);
544 } while (i
!= 0 && i
!= ',');
545 } else /* we got the required ] */
546 i
= stdscan(NULL
, &tokval
);
547 } else { /* immediate operand */
548 if (i
!= 0 && i
!= ',' && i
!= ':') {
549 error(ERR_NONFATAL
, "comma or end of line expected");
550 do { /* error recovery */
551 i
= stdscan(NULL
, &tokval
);
552 } while (i
!= 0 && i
!= ',');
553 } else if (i
== ':') {
554 result
->oprs
[operand
].type
|= COLON
;
558 /* now convert the exprs returned from evaluate() into operand
561 if (mref
) { /* it's a memory reference */
563 int b
, i
, s
; /* basereg, indexreg, scale */
564 int32_t o
; /* offset */
566 b
= i
= -1, o
= s
= 0;
567 result
->oprs
[operand
].hintbase
= hints
.base
;
568 result
->oprs
[operand
].hinttype
= hints
.type
;
570 if (e
->type
&& e
->type
<= EXPR_REG_END
) { /* this bit's a register */
571 if (e
->value
== 1) /* in fact it can be basereg */
573 else /* no, it has to be indexreg */
574 i
= e
->type
, s
= e
->value
;
577 if (e
->type
&& e
->type
<= EXPR_REG_END
) { /* it's a 2nd register */
578 if (b
!= -1) /* If the first was the base, ... */
579 i
= e
->type
, s
= e
->value
; /* second has to be indexreg */
581 else if (e
->value
!= 1) { /* If both want to be index */
583 "beroset-p-592-invalid effective address");
590 if (e
->type
!= 0) { /* is there an offset? */
591 if (e
->type
<= EXPR_REG_END
) { /* in fact, is there an error? */
593 "beroset-p-603-invalid effective address");
597 if (e
->type
== EXPR_UNKNOWN
) {
598 o
= 0; /* doesn't matter what */
599 result
->oprs
[operand
].wrt
= NO_SEG
; /* nor this */
600 result
->oprs
[operand
].segment
= NO_SEG
; /* or this */
602 e
++; /* go to the end of the line */
604 if (e
->type
== EXPR_SIMPLE
) {
608 if (e
->type
== EXPR_WRT
) {
609 result
->oprs
[operand
].wrt
= e
->value
;
612 result
->oprs
[operand
].wrt
= NO_SEG
;
614 * Look for a segment base type.
616 if (e
->type
&& e
->type
< EXPR_SEGBASE
) {
618 "beroset-p-630-invalid effective address");
622 while (e
->type
&& e
->value
== 0)
624 if (e
->type
&& e
->value
!= 1) {
626 "beroset-p-637-invalid effective address");
631 result
->oprs
[operand
].segment
=
632 e
->type
- EXPR_SEGBASE
;
635 result
->oprs
[operand
].segment
= NO_SEG
;
636 while (e
->type
&& e
->value
== 0)
640 "beroset-p-650-invalid effective address");
648 result
->oprs
[operand
].wrt
= NO_SEG
;
649 result
->oprs
[operand
].segment
= NO_SEG
;
652 if (e
->type
!= 0) { /* there'd better be nothing left! */
654 "beroset-p-663-invalid effective address");
659 result
->oprs
[operand
].type
|= MEMORY
;
660 if (b
== -1 && (i
== -1 || s
== 0))
661 result
->oprs
[operand
].type
|= MEM_OFFS
;
662 result
->oprs
[operand
].basereg
= b
;
663 result
->oprs
[operand
].indexreg
= i
;
664 result
->oprs
[operand
].scale
= s
;
665 result
->oprs
[operand
].offset
= o
;
666 } else { /* it's not a memory reference */
668 if (is_just_unknown(value
)) { /* it's immediate but unknown */
669 result
->oprs
[operand
].type
|= IMMEDIATE
;
670 result
->oprs
[operand
].offset
= 0; /* don't care */
671 result
->oprs
[operand
].segment
= NO_SEG
; /* don't care again */
672 result
->oprs
[operand
].wrt
= NO_SEG
; /* still don't care */
673 } else if (is_reloc(value
)) { /* it's immediate */
674 result
->oprs
[operand
].type
|= IMMEDIATE
;
675 result
->oprs
[operand
].offset
= reloc_value(value
);
676 result
->oprs
[operand
].segment
= reloc_seg(value
);
677 result
->oprs
[operand
].wrt
= reloc_wrt(value
);
678 if (is_simple(value
)) {
679 if (reloc_value(value
) == 1)
680 result
->oprs
[operand
].type
|= UNITY
;
681 if (optimizing
>= 0 &&
682 !(result
->oprs
[operand
].type
& STRICT
)) {
683 if (reloc_value(value
) >= -128 &&
684 reloc_value(value
) <= 127)
685 result
->oprs
[operand
].type
|= SBYTE
;
688 } else { /* it's a register */
690 if (value
->type
>= EXPR_SIMPLE
|| value
->value
!= 1) {
691 error(ERR_NONFATAL
, "invalid operand type");
697 * check that its only 1 register, not an expression...
699 for (i
= 1; value
[i
].type
; i
++)
700 if (value
[i
].value
) {
701 error(ERR_NONFATAL
, "invalid operand type");
706 /* clear overrides, except TO which applies to FPU regs */
707 if (result
->oprs
[operand
].type
& ~TO
) {
709 * we want to produce a warning iff the specified size
710 * is different from the register size
712 i
= result
->oprs
[operand
].type
& SIZE_MASK
;
716 result
->oprs
[operand
].type
&= TO
;
717 result
->oprs
[operand
].type
|= REGISTER
;
718 result
->oprs
[operand
].type
|= reg_flags
[value
->type
];
719 result
->oprs
[operand
].basereg
= value
->type
;
721 if (i
&& (result
->oprs
[operand
].type
& SIZE_MASK
) != i
)
722 error(ERR_WARNING
| ERR_PASS1
,
723 "register size specification ignored");
728 result
->operands
= operand
; /* set operand count */
730 while (operand
< 3) /* clear remaining operands */
731 result
->oprs
[operand
++].type
= 0;
734 * Transform RESW, RESD, RESQ, REST into RESB.
736 switch (result
->opcode
) {
738 result
->opcode
= I_RESB
;
739 result
->oprs
[0].offset
*= 2;
742 result
->opcode
= I_RESB
;
743 result
->oprs
[0].offset
*= 4;
746 result
->opcode
= I_RESB
;
747 result
->oprs
[0].offset
*= 8;
750 result
->opcode
= I_RESB
;
751 result
->oprs
[0].offset
*= 10;
758 static int is_comma_next(void)
765 i
= stdscan(NULL
, &tv
);
767 return (i
== ',' || i
== ';' || !i
);
770 void cleanup_insn(insn
* i
)
776 i
->eops
= i
->eops
->next
;