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
22 static long reg_flags
[] = { /* sizes and special flags */
23 0, REG8
, REG_AL
, REG_AX
, REG8
, REG8
, REG16
, REG16
, REG8
, REG_CL
,
24 REG_CREG
, REG_CREG
, REG_CREG
, REG_CR4
, REG_CS
, REG_CX
, REG8
,
25 REG16
, REG8
, REG_DREG
, REG_DREG
, REG_DREG
, REG_DREG
, REG_DREG
,
26 REG_DREG
, REG_DESS
, REG_DX
, REG_EAX
, REG32
, REG32
, REG_ECX
,
27 REG32
, REG32
, REG_DESS
, REG32
, REG32
, REG_FSGS
, REG_FSGS
,
28 MMXREG
, MMXREG
, MMXREG
, MMXREG
, MMXREG
, MMXREG
, MMXREG
, MMXREG
,
29 REG16
, REG16
, REG_DESS
, FPU0
, FPUREG
, FPUREG
, FPUREG
, FPUREG
,
30 FPUREG
, FPUREG
, FPUREG
, REG_TREG
, REG_TREG
, REG_TREG
, REG_TREG
,
34 enum { /* special tokens */
35 S_BYTE
, S_DWORD
, S_FAR
, S_LONG
, S_NEAR
, S_NOSPLIT
, S_QWORD
,
36 S_SHORT
, S_TO
, S_TWORD
, S_WORD
39 static int is_comma_next (void);
42 static struct tokenval tokval
;
45 insn
*parse_line (int pass
, char *buffer
, insn
*result
,
46 efunc errfunc
, evalfunc evaluate
, evalinfofunc einfo
) {
49 struct eval_hints hints
;
51 result
->forw_ref
= FALSE
;
56 stdscan_bufptr
= buffer
;
57 i
= stdscan(NULL
, &tokval
);
59 result
->eops
= NULL
; /* must do this, whatever happens */
60 result
->operands
= 0; /* must initialise this */
62 if (i
==0) { /* blank line - ignore */
63 result
->label
= NULL
; /* so, no label on it */
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"
76 if (i
== TOKEN_ID
) { /* there's a label here */
77 result
->label
= tokval
.t_charptr
;
78 einfo (result
->label
, 0L, 0L);
79 i
= stdscan(NULL
, &tokval
);
80 if (i
== ':') { /* skip over the optional colon */
81 i
= stdscan(NULL
, &tokval
);
82 } else if (i
== 0 && pass
== 1) {
83 error (ERR_WARNING
|ERR_WARN_OL
,
84 "label alone on a line without a colon might be in error");
86 } else /* no label; so, moving swiftly on */
90 result
->opcode
= -1; /* this line contains just a label */
97 while (i
== TOKEN_PREFIX
||
98 (i
==TOKEN_REG
&& !(REG_SREG
& ~reg_flags
[tokval
.t_integer
]))) {
100 * Handle special case: the TIMES prefix.
102 if (i
== TOKEN_PREFIX
&& tokval
.t_integer
== P_TIMES
) {
105 i
= stdscan(NULL
, &tokval
);
106 value
= evaluate (stdscan
, NULL
, &tokval
, NULL
, pass
, error
, NULL
);
108 if (!value
) { /* but, error in evaluator */
109 result
->opcode
= -1; /* unrecoverable parse error: */
110 return result
; /* ignore this instruction */
112 if (!is_simple (value
)) {
114 "non-constant argument supplied to TIMES");
117 result
->times
= value
->value
;
118 if (value
->value
< 0)
119 error(ERR_NONFATAL
, "TIMES value %d is negative",
123 if (result
->nprefix
== MAXPREFIX
)
125 "instruction has more than %d prefixes", MAXPREFIX
);
127 result
->prefixes
[result
->nprefix
++] = tokval
.t_integer
;
128 i
= stdscan(NULL
, &tokval
);
132 if (i
!= TOKEN_INSN
) {
133 if (result
->nprefix
> 0 && i
== 0) {
135 * Instruction prefixes are present, but no actual
136 * instruction. This is allowed: at this point we
137 * invent a notional instruction of RESB 0.
139 result
->opcode
= I_RESB
;
140 result
->operands
= 1;
141 result
->oprs
[0].type
= IMMEDIATE
;
142 result
->oprs
[0].offset
= 0L;
143 result
->oprs
[0].segment
= result
->oprs
[0].wrt
= NO_SEG
;
146 error (ERR_NONFATAL
, "parser: instruction expected");
152 result
->opcode
= tokval
.t_integer
;
153 result
->condition
= tokval
.t_inttwo
;
156 * RESB, RESW and RESD cannot be satisfied with incorrectly
157 * evaluated operands, since the correct values _must_ be known
158 * on the first pass. Hence, even in pass one, we set the
159 * `critical' flag on calling evaluate(), so that it will bomb
160 * out on undefined symbols. Nasty, but there's nothing we can
163 * For the moment, EQU has the same difficulty, so we'll
166 if (result
->opcode
== I_RESB
||
167 result
->opcode
== I_RESW
||
168 result
->opcode
== I_RESD
||
169 result
->opcode
== I_RESQ
||
170 result
->opcode
== I_REST
||
171 result
->opcode
== I_EQU
)
174 critical
= (pass
==2 ? 2 : 0);
176 if (result
->opcode
== I_DB
||
177 result
->opcode
== I_DW
||
178 result
->opcode
== I_DD
||
179 result
->opcode
== I_DQ
||
180 result
->opcode
== I_DT
||
181 result
->opcode
== I_INCBIN
) {
182 extop
*eop
, **tail
= &result
->eops
;
186 * Begin to read the DB/DW/DD/DQ/DT operands.
189 i
= stdscan(NULL
, &tokval
);
192 eop
= *tail
= nasm_malloc(sizeof(extop
));
195 eop
->type
= EOT_NOTHING
;
198 if (i
== TOKEN_NUM
&& tokval
.t_charptr
&& is_comma_next()) {
199 eop
->type
= EOT_DB_STRING
;
200 eop
->stringval
= tokval
.t_charptr
;
201 eop
->stringlen
= tokval
.t_inttwo
;
202 i
= stdscan(NULL
, &tokval
); /* eat the comma */
206 if (i
== TOKEN_FLOAT
|| i
== '-') {
210 char *save
= stdscan_bufptr
;
211 i
= stdscan(NULL
, &tokval
);
213 if (i
!= TOKEN_FLOAT
) {
214 stdscan_bufptr
= save
;
215 i
= tokval
.t_type
= '-';
219 if (i
== TOKEN_FLOAT
) {
220 eop
->type
= EOT_DB_STRING
;
221 if (result
->opcode
== I_DD
)
223 else if (result
->opcode
== I_DQ
)
225 else if (result
->opcode
== I_DT
)
228 error(ERR_NONFATAL
, "floating-point constant"
229 " encountered in `D%c' instruction",
230 result
->opcode
== I_DW
? 'W' : 'B');
231 eop
->type
= EOT_NOTHING
;
233 eop
= nasm_realloc(eop
, sizeof(extop
)+eop
->stringlen
);
234 eop
->stringval
= (char *)eop
+ sizeof(extop
);
235 if (!float_const (tokval
.t_charptr
, sign
,
236 (unsigned char *)eop
->stringval
,
237 eop
->stringlen
, error
))
238 eop
->type
= EOT_NOTHING
;
239 i
= stdscan(NULL
, &tokval
); /* eat the comma */
244 /* anything else */ {
246 value
= evaluate (stdscan
, NULL
, &tokval
, NULL
,
247 critical
, error
, NULL
);
249 if (!value
) { /* error in evaluator */
250 result
->opcode
= -1;/* unrecoverable parse error: */
251 return result
; /* ignore this instruction */
253 if (is_unknown(value
)) {
254 eop
->type
= EOT_DB_NUMBER
;
255 eop
->offset
= 0; /* doesn't matter what we put */
256 eop
->segment
= eop
->wrt
= NO_SEG
; /* likewise */
257 } else if (is_reloc(value
)) {
258 eop
->type
= EOT_DB_NUMBER
;
259 eop
->offset
= reloc_value(value
);
260 eop
->segment
= reloc_seg(value
);
261 eop
->wrt
= reloc_wrt(value
);
264 "operand %d: expression is not simple"
265 " or relocatable", oper_num
);
270 * We're about to call stdscan(), which will eat the
271 * comma that we're currently sitting on between
272 * arguments. However, we'd better check first that it
275 if (i
== 0) /* also could be EOL */
278 error (ERR_NONFATAL
, "comma expected after operand %d",
280 result
->opcode
= -1;/* unrecoverable parse error: */
281 return result
; /* ignore this instruction */
285 if (result
->opcode
== I_INCBIN
) {
287 * Correct syntax for INCBIN is that there should be
288 * one string operand, followed by one or two numeric
291 if (!result
->eops
|| result
->eops
->type
!= EOT_DB_STRING
)
292 error (ERR_NONFATAL
, "`incbin' expects a file name");
293 else if (result
->eops
->next
&&
294 result
->eops
->next
->type
!= EOT_DB_NUMBER
)
295 error (ERR_NONFATAL
, "`incbin': second parameter is",
297 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
298 result
->eops
->next
->next
->type
!= EOT_DB_NUMBER
)
299 error (ERR_NONFATAL
, "`incbin': third parameter is",
301 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
302 result
->eops
->next
->next
->next
)
303 error (ERR_NONFATAL
, "`incbin': more than three parameters");
307 * If we reach here, one of the above errors happened.
308 * Throw the instruction away.
317 /* right. Now we begin to parse the operands. There may be up to three
318 * of these, separated by commas, and terminated by a zero token. */
320 for (operand
= 0; operand
< 3; operand
++) {
321 expr
*value
; /* used most of the time */
322 int mref
; /* is this going to be a memory ref? */
323 int bracket
; /* is it a [] mref, or a & mref? */
325 result
->oprs
[operand
].addr_size
= 0;/* have to zero this whatever */
326 result
->oprs
[operand
].eaflags
= 0; /* and this */
327 i
= stdscan(NULL
, &tokval
);
328 if (i
== 0) break; /* end of operands: get out of here */
329 result
->oprs
[operand
].type
= 0; /* so far, no override */
330 while (i
== TOKEN_SPECIAL
) {/* size specifiers */
331 switch ((int)tokval
.t_integer
) {
333 result
->oprs
[operand
].type
|= BITS8
;
336 result
->oprs
[operand
].type
|= BITS16
;
340 result
->oprs
[operand
].type
|= BITS32
;
343 result
->oprs
[operand
].type
|= BITS64
;
346 result
->oprs
[operand
].type
|= BITS80
;
349 result
->oprs
[operand
].type
|= TO
;
352 result
->oprs
[operand
].type
|= FAR
;
355 result
->oprs
[operand
].type
|= NEAR
;
358 result
->oprs
[operand
].type
|= SHORT
;
361 i
= stdscan(NULL
, &tokval
);
364 if (i
== '[' || i
== '&') { /* memory reference */
366 bracket
= (i
== '[');
367 i
= stdscan(NULL
, &tokval
);
368 if (i
== TOKEN_SPECIAL
) { /* check for address size override */
369 switch ((int)tokval
.t_integer
) {
371 result
->oprs
[operand
].eaflags
|= EAF_TIMESTWO
;
374 result
->oprs
[operand
].eaflags
|= EAF_BYTEOFFS
;
377 result
->oprs
[operand
].addr_size
= 16;
378 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
382 result
->oprs
[operand
].addr_size
= 32;
383 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
386 error (ERR_NONFATAL
, "invalid size specification in"
387 " effective address");
389 i
= stdscan(NULL
, &tokval
);
391 } else { /* immediate operand, or register */
393 bracket
= FALSE
; /* placate optimisers */
396 value
= evaluate (stdscan
, NULL
, &tokval
,
397 &result
->forw_ref
, critical
, error
, &hints
);
399 if (!value
) { /* error in evaluator */
400 result
->opcode
= -1; /* unrecoverable parse error: */
401 return result
; /* ignore this instruction */
403 if (i
== ':' && mref
) { /* it was seg:offset */
405 * Process the segment override.
407 if (value
[1].type
!=0 || value
->value
!=1 ||
408 REG_SREG
& ~reg_flags
[value
->type
])
409 error (ERR_NONFATAL
, "invalid segment override");
410 else if (result
->nprefix
== MAXPREFIX
)
412 "instruction has more than %d prefixes",
415 result
->prefixes
[result
->nprefix
++] = value
->type
;
417 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
418 if (i
== TOKEN_SPECIAL
) { /* another check for size override */
419 switch ((int)tokval
.t_integer
) {
421 result
->oprs
[operand
].addr_size
= 16;
425 result
->oprs
[operand
].addr_size
= 32;
428 error (ERR_NONFATAL
, "invalid size specification in"
429 " effective address");
431 i
= stdscan(NULL
, &tokval
);
433 value
= evaluate (stdscan
, NULL
, &tokval
,
434 &result
->forw_ref
, critical
, error
, &hints
);
436 /* and get the offset */
437 if (!value
) { /* but, error in evaluator */
438 result
->opcode
= -1; /* unrecoverable parse error: */
439 return result
; /* ignore this instruction */
442 if (mref
&& bracket
) { /* find ] at the end */
444 error (ERR_NONFATAL
, "parser: expecting ]");
445 do { /* error recovery again */
446 i
= stdscan(NULL
, &tokval
);
447 } while (i
!= 0 && i
!= ',');
448 } else /* we got the required ] */
449 i
= stdscan(NULL
, &tokval
);
450 } else { /* immediate operand */
451 if (i
!= 0 && i
!= ',' && i
!= ':') {
452 error (ERR_NONFATAL
, "comma or end of line expected");
453 do { /* error recovery */
454 i
= stdscan(NULL
, &tokval
);
455 } while (i
!= 0 && i
!= ',');
456 } else if (i
== ':') {
457 result
->oprs
[operand
].type
|= COLON
;
461 /* now convert the exprs returned from evaluate() into operand
464 if (mref
) { /* it's a memory reference */
466 int b
, i
, s
; /* basereg, indexreg, scale */
469 b
= i
= -1, o
= s
= 0;
470 result
->oprs
[operand
].hintbase
= hints
.base
;
471 result
->oprs
[operand
].hinttype
= hints
.type
;
473 if (e
->type
<= EXPR_REG_END
) { /* this bit's a register */
474 if (e
->value
== 1) /* in fact it can be basereg */
476 else /* no, it has to be indexreg */
477 i
= e
->type
, s
= e
->value
;
480 if (e
->type
&& e
->type
<= EXPR_REG_END
) {/* it's a 2nd register */
481 if (e
->value
!= 1) { /* it has to be indexreg */
482 if (i
!= -1) { /* but it can't be */
483 error(ERR_NONFATAL
, "invalid effective address");
487 i
= e
->type
, s
= e
->value
;
488 } else { /* it can be basereg */
489 if (b
!= -1) /* or can it? */
496 if (e
->type
!= 0) { /* is there an offset? */
497 if (e
->type
<= EXPR_REG_END
) {/* in fact, is there an error? */
498 error (ERR_NONFATAL
, "invalid effective address");
502 if (e
->type
== EXPR_UNKNOWN
) {
503 o
= 0; /* doesn't matter what */
504 result
->oprs
[operand
].wrt
= NO_SEG
; /* nor this */
505 result
->oprs
[operand
].segment
= NO_SEG
; /* or this */
506 while (e
->type
) e
++; /* go to the end of the line */
508 if (e
->type
== EXPR_SIMPLE
) {
512 if (e
->type
== EXPR_WRT
) {
513 result
->oprs
[operand
].wrt
= e
->value
;
516 result
->oprs
[operand
].wrt
= NO_SEG
;
518 * Look for a segment base type.
520 if (e
->type
&& e
->type
< EXPR_SEGBASE
) {
521 error (ERR_NONFATAL
, "invalid effective address");
525 while (e
->type
&& e
->value
== 0)
527 if (e
->type
&& e
->value
!= 1) {
528 error (ERR_NONFATAL
, "invalid effective address");
533 result
->oprs
[operand
].segment
=
534 e
->type
- EXPR_SEGBASE
;
537 result
->oprs
[operand
].segment
= NO_SEG
;
538 while (e
->type
&& e
->value
== 0)
541 error (ERR_NONFATAL
, "invalid effective address");
549 result
->oprs
[operand
].wrt
= NO_SEG
;
550 result
->oprs
[operand
].segment
= NO_SEG
;
553 if (e
->type
!= 0) { /* there'd better be nothing left! */
554 error (ERR_NONFATAL
, "invalid effective address");
559 result
->oprs
[operand
].type
|= MEMORY
;
560 if (b
==-1 && (i
==-1 || s
==0))
561 result
->oprs
[operand
].type
|= MEM_OFFS
;
562 result
->oprs
[operand
].basereg
= b
;
563 result
->oprs
[operand
].indexreg
= i
;
564 result
->oprs
[operand
].scale
= s
;
565 result
->oprs
[operand
].offset
= o
;
566 } else { /* it's not a memory reference */
567 if (is_just_unknown(value
)) { /* it's immediate but unknown */
568 result
->oprs
[operand
].type
|= IMMEDIATE
;
569 result
->oprs
[operand
].offset
= 0; /* don't care */
570 result
->oprs
[operand
].segment
= NO_SEG
; /* don't care again */
571 result
->oprs
[operand
].wrt
= NO_SEG
;/* still don't care */
572 } else if (is_reloc(value
)) { /* it's immediate */
573 result
->oprs
[operand
].type
|= IMMEDIATE
;
574 result
->oprs
[operand
].offset
= reloc_value(value
);
575 result
->oprs
[operand
].segment
= reloc_seg(value
);
576 result
->oprs
[operand
].wrt
= reloc_wrt(value
);
577 if (is_simple(value
) && reloc_value(value
)==1)
578 result
->oprs
[operand
].type
|= UNITY
;
579 } else { /* it's a register */
580 if (value
->type
>=EXPR_SIMPLE
|| value
->value
!=1) {
581 error (ERR_NONFATAL
, "invalid operand type");
585 /* clear overrides, except TO which applies to FPU regs */
586 result
->oprs
[operand
].type
&= TO
;
587 result
->oprs
[operand
].type
|= REGISTER
;
588 result
->oprs
[operand
].type
|= reg_flags
[value
->type
];
589 result
->oprs
[operand
].basereg
= value
->type
;
594 result
->operands
= operand
; /* set operand count */
596 while (operand
<3) /* clear remaining operands */
597 result
->oprs
[operand
++].type
= 0;
600 * Transform RESW, RESD, RESQ, REST into RESB.
602 switch (result
->opcode
) {
603 case I_RESW
: result
->opcode
=I_RESB
; result
->oprs
[0].offset
*=2; break;
604 case I_RESD
: result
->opcode
=I_RESB
; result
->oprs
[0].offset
*=4; break;
605 case I_RESQ
: result
->opcode
=I_RESB
; result
->oprs
[0].offset
*=8; break;
606 case I_REST
: result
->opcode
=I_RESB
; result
->oprs
[0].offset
*=10; break;
612 static int is_comma_next (void) {
618 i
= stdscan (NULL
, &tv
);
620 return (i
== ',' || i
== ';' || !i
);
623 void cleanup_insn (insn
*i
) {
628 i
->eops
= i
->eops
->next
;