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
27 extern int in_abs_seg; /* ABSOLUTE segment flag */
28 extern int32_t abs_seg; /* ABSOLUTE segment */
29 extern int32_t abs_offset; /* ABSOLUTE segment offset */
31 #include "regflags.c" /* List of register flags */
33 static int is_comma_next(void);
36 static struct tokenval tokval;
38 static struct ofmt *outfmt; /* Structure of addresses of output routines */
39 static struct location *location; /* Pointer to current line's segment,offset */
41 void parser_global_info(struct ofmt *output, struct location * locp)
47 static int prefix_slot(enum prefixes prefix)
75 error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
80 static void process_size_override(insn * result, int operand)
82 if (tasm_compatible_mode) {
83 switch ((int)tokval.t_integer) {
84 /* For TASM compatibility a size override inside the
85 * brackets changes the size of the operand, not the
86 * address type of the operand as it does in standard
91 * is valid syntax in TASM compatibility mode. Note that
92 * you lose the ability to override the default address
93 * type for the instruction, but we never use anything
94 * but 32-bit flat model addressing in our code.
97 result->oprs[operand].type |= BITS8;
100 result->oprs[operand].type |= BITS16;
104 result->oprs[operand].type |= BITS32;
107 result->oprs[operand].type |= BITS64;
110 result->oprs[operand].type |= BITS80;
113 result->oprs[operand].type |= BITS128;
117 "invalid operand size specification");
121 /* Standard NASM compatible syntax */
122 switch ((int)tokval.t_integer) {
124 result->oprs[operand].eaflags |= EAF_TIMESTWO;
127 result->oprs[operand].eaflags |= EAF_REL;
130 result->oprs[operand].eaflags |= EAF_ABS;
133 result->oprs[operand].disp_size = 8;
134 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
139 if (result->prefixes[PPS_ASIZE] &&
140 result->prefixes[PPS_ASIZE] != tokval.t_integer)
142 "conflicting address size specifications");
144 result->prefixes[PPS_ASIZE] = tokval.t_integer;
147 result->oprs[operand].disp_size = 16;
148 result->oprs[operand].eaflags |= EAF_WORDOFFS;
152 result->oprs[operand].disp_size = 32;
153 result->oprs[operand].eaflags |= EAF_WORDOFFS;
156 result->oprs[operand].disp_size = 64;
157 result->oprs[operand].eaflags |= EAF_WORDOFFS;
160 error(ERR_NONFATAL, "invalid size specification in"
161 " effective address");
167 insn *parse_line(int pass, char *buffer, insn * result,
168 efunc errfunc, evalfunc evaluate, ldfunc ldef)
172 struct eval_hints hints;
175 result->forw_ref = false;
179 stdscan_bufptr = buffer;
180 i = stdscan(NULL, &tokval);
182 result->label = NULL; /* Assume no label */
183 result->eops = NULL; /* must do this, whatever happens */
184 result->operands = 0; /* must initialize this */
186 if (i == 0) { /* blank line - ignore */
187 result->opcode = -1; /* and no instruction either */
190 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
191 (i != TOKEN_REG || (REG_SREG & ~reg_flags[tokval.t_integer]))) {
192 error(ERR_NONFATAL, "label or instruction expected"
193 " at start of line");
198 if (i == TOKEN_ID) { /* there's a label here */
199 result->label = tokval.t_charptr;
200 i = stdscan(NULL, &tokval);
201 if (i == ':') { /* skip over the optional colon */
202 i = stdscan(NULL, &tokval);
204 error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
205 "label alone on a line without a colon might be in error");
207 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
209 * FIXME: location->segment could be NO_SEG, in which case
210 * it is possible we should be passing 'abs_seg'. Look into this.
211 * Work out whether that is *really* what we should be doing.
212 * Generally fix things. I think this is right as it is, but
213 * am still not certain.
215 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
216 location->offset, NULL, true, false, outfmt, errfunc);
221 result->opcode = -1; /* this line contains just a label */
225 for (j = 0; j < MAXPREFIX; j++)
226 result->prefixes[j] = P_none;
229 while (i == TOKEN_PREFIX ||
230 (i == TOKEN_REG && !(REG_SREG & ~reg_flags[tokval.t_integer])))
233 * Handle special case: the TIMES prefix.
235 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
238 i = stdscan(NULL, &tokval);
240 evaluate(stdscan, NULL, &tokval, NULL, pass0, error, NULL);
242 if (!value) { /* but, error in evaluator */
243 result->opcode = -1; /* unrecoverable parse error: */
244 return result; /* ignore this instruction */
246 if (!is_simple(value)) {
248 "non-constant argument supplied to TIMES");
251 result->times = value->value;
252 if (value->value < 0) {
253 error(ERR_NONFATAL, "TIMES value %d is negative",
259 int slot = prefix_slot(tokval.t_integer);
260 if (result->prefixes[slot]) {
261 if (result->prefixes[slot] == tokval.t_integer)
263 "instruction has redundant prefixes");
266 "instruction has conflicting prefixes");
268 result->prefixes[slot] = tokval.t_integer;
269 i = stdscan(NULL, &tokval);
273 if (i != TOKEN_INSN) {
277 for (j = 0; j < MAXPREFIX; j++)
278 if ((pfx = result->prefixes[j]) != P_none)
281 if (i == 0 && pfx != P_none) {
283 * Instruction prefixes are present, but no actual
284 * instruction. This is allowed: at this point we
285 * invent a notional instruction of RESB 0.
287 result->opcode = I_RESB;
288 result->operands = 1;
289 result->oprs[0].type = IMMEDIATE;
290 result->oprs[0].offset = 0L;
291 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
294 error(ERR_NONFATAL, "parser: instruction expected");
300 result->opcode = tokval.t_integer;
301 result->condition = tokval.t_inttwo;
304 * RESB, RESW and RESD cannot be satisfied with incorrectly
305 * evaluated operands, since the correct values _must_ be known
306 * on the first pass. Hence, even in pass one, we set the
307 * `critical' flag on calling evaluate(), so that it will bomb
308 * out on undefined symbols. Nasty, but there's nothing we can
311 * For the moment, EQU has the same difficulty, so we'll
314 if (result->opcode == I_RESB || result->opcode == I_RESW ||
315 result->opcode == I_RESD || result->opcode == I_RESQ ||
316 result->opcode == I_REST || result->opcode == I_RESO ||
317 result->opcode == I_EQU || result->opcode == I_INCBIN) {
320 critical = (pass == 2 ? 2 : 0);
322 if (result->opcode == I_DB || result->opcode == I_DW ||
323 result->opcode == I_DD || result->opcode == I_DQ ||
324 result->opcode == I_DT || result->opcode == I_DO ||
325 result->opcode == I_INCBIN) {
326 extop *eop, **tail = &result->eops, **fixptr;
329 result->eops_float = false;
332 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
335 i = stdscan(NULL, &tokval);
339 eop = *tail = nasm_malloc(sizeof(extop));
342 eop->type = EOT_NOTHING;
345 if (i == TOKEN_NUM && tokval.t_charptr && is_comma_next()) {
346 eop->type = EOT_DB_STRING;
347 eop->stringval = tokval.t_charptr;
348 eop->stringlen = tokval.t_inttwo;
349 i = stdscan(NULL, &tokval); /* eat the comma */
353 if ((i == TOKEN_FLOAT && is_comma_next())
354 || i == '-' || i == '+') {
357 if (i == '+' || i == '-') {
358 char *save = stdscan_bufptr;
360 sign = (i == '-') ? -1 : 1;
361 i = stdscan(NULL, &tokval);
362 if (i != TOKEN_FLOAT || !is_comma_next()) {
363 stdscan_bufptr = save;
364 i = tokval.t_type = token;
368 if (i == TOKEN_FLOAT) {
369 eop->type = EOT_DB_STRING;
370 result->eops_float = true;
371 switch (result->opcode) {
391 error(ERR_NONFATAL, "floating-point constant"
392 " encountered in unknown instruction");
394 * fix suggested by Pedro Gimeno... original line
396 * eop->type = EOT_NOTHING;
401 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
404 eop->stringval = (char *)eop + sizeof(extop);
405 if (!eop->stringlen ||
406 !float_const(tokval.t_charptr, sign,
407 (uint8_t *)eop->stringval,
408 eop->stringlen, error))
409 eop->type = EOT_NOTHING;
410 i = stdscan(NULL, &tokval); /* eat the comma */
418 value = evaluate(stdscan, NULL, &tokval, NULL,
419 critical, error, NULL);
421 if (!value) { /* error in evaluator */
422 result->opcode = -1; /* unrecoverable parse error: */
423 return result; /* ignore this instruction */
425 if (is_unknown(value)) {
426 eop->type = EOT_DB_NUMBER;
427 eop->offset = 0; /* doesn't matter what we put */
428 eop->segment = eop->wrt = NO_SEG; /* likewise */
429 } else if (is_reloc(value)) {
430 eop->type = EOT_DB_NUMBER;
431 eop->offset = reloc_value(value);
432 eop->segment = reloc_seg(value);
433 eop->wrt = reloc_wrt(value);
436 "operand %d: expression is not simple"
437 " or relocatable", oper_num);
442 * We're about to call stdscan(), which will eat the
443 * comma that we're currently sitting on between
444 * arguments. However, we'd better check first that it
447 if (i == 0) /* also could be EOL */
450 error(ERR_NONFATAL, "comma expected after operand %d",
452 result->opcode = -1; /* unrecoverable parse error: */
453 return result; /* ignore this instruction */
457 if (result->opcode == I_INCBIN) {
459 * Correct syntax for INCBIN is that there should be
460 * one string operand, followed by one or two numeric
463 if (!result->eops || result->eops->type != EOT_DB_STRING)
464 error(ERR_NONFATAL, "`incbin' expects a file name");
465 else if (result->eops->next &&
466 result->eops->next->type != EOT_DB_NUMBER)
467 error(ERR_NONFATAL, "`incbin': second parameter is",
469 else if (result->eops->next && result->eops->next->next &&
470 result->eops->next->next->type != EOT_DB_NUMBER)
471 error(ERR_NONFATAL, "`incbin': third parameter is",
473 else if (result->eops->next && result->eops->next->next &&
474 result->eops->next->next->next)
476 "`incbin': more than three parameters");
480 * If we reach here, one of the above errors happened.
481 * Throw the instruction away.
485 } else /* DB ... */ if (oper_num == 0)
486 error(ERR_WARNING | ERR_PASS1,
487 "no operand for data declaration");
489 result->operands = oper_num;
494 /* right. Now we begin to parse the operands. There may be up to four
495 * of these, separated by commas, and terminated by a zero token. */
497 for (operand = 0; operand < MAX_OPERANDS; operand++) {
498 expr *value; /* used most of the time */
499 int mref; /* is this going to be a memory ref? */
500 int bracket; /* is it a [] mref, or a & mref? */
503 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
504 result->oprs[operand].eaflags = 0; /* and this */
505 result->oprs[operand].opflags = 0;
507 i = stdscan(NULL, &tokval);
509 break; /* end of operands: get out of here */
510 result->oprs[operand].type = 0; /* so far, no override */
511 while (i == TOKEN_SPECIAL) { /* size specifiers */
512 switch ((int)tokval.t_integer) {
514 if (!setsize) /* we want to use only the first */
515 result->oprs[operand].type |= BITS8;
520 result->oprs[operand].type |= BITS16;
526 result->oprs[operand].type |= BITS32;
531 result->oprs[operand].type |= BITS64;
536 result->oprs[operand].type |= BITS80;
541 result->oprs[operand].type |= BITS128;
545 result->oprs[operand].type |= TO;
548 result->oprs[operand].type |= STRICT;
551 result->oprs[operand].type |= FAR;
554 result->oprs[operand].type |= NEAR;
557 result->oprs[operand].type |= SHORT;
560 error(ERR_NONFATAL, "invalid operand size specification");
562 i = stdscan(NULL, &tokval);
565 if (i == '[' || i == '&') { /* memory reference */
567 bracket = (i == '[');
568 i = stdscan(NULL, &tokval); /* then skip the colon */
569 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
570 process_size_override(result, operand);
571 i = stdscan(NULL, &tokval);
573 } else { /* immediate operand, or register */
575 bracket = false; /* placate optimisers */
578 if ((result->oprs[operand].type & FAR) && !mref &&
579 result->opcode != I_JMP && result->opcode != I_CALL) {
580 error(ERR_NONFATAL, "invalid use of FAR operand specifier");
583 value = evaluate(stdscan, NULL, &tokval,
584 &result->oprs[operand].opflags,
585 critical, error, &hints);
587 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
588 result->forw_ref = true;
590 if (!value) { /* error in evaluator */
591 result->opcode = -1; /* unrecoverable parse error: */
592 return result; /* ignore this instruction */
594 if (i == ':' && mref) { /* it was seg:offset */
596 * Process the segment override.
598 if (value[1].type != 0 || value->value != 1 ||
599 REG_SREG & ~reg_flags[value->type])
600 error(ERR_NONFATAL, "invalid segment override");
601 else if (result->prefixes[PPS_SEG])
603 "instruction has conflicting segment overrides");
605 result->prefixes[PPS_SEG] = value->type;
606 if (!(REG_FSGS & ~reg_flags[value->type]))
607 result->oprs[operand].eaflags |= EAF_FSGS;
610 i = stdscan(NULL, &tokval); /* then skip the colon */
611 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
612 process_size_override(result, operand);
613 i = stdscan(NULL, &tokval);
615 value = evaluate(stdscan, NULL, &tokval,
616 &result->oprs[operand].opflags,
617 critical, error, &hints);
619 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
620 result->forw_ref = true;
622 /* and get the offset */
623 if (!value) { /* but, error in evaluator */
624 result->opcode = -1; /* unrecoverable parse error: */
625 return result; /* ignore this instruction */
628 if (mref && bracket) { /* find ] at the end */
630 error(ERR_NONFATAL, "parser: expecting ]");
631 do { /* error recovery again */
632 i = stdscan(NULL, &tokval);
633 } while (i != 0 && i != ',');
634 } else /* we got the required ] */
635 i = stdscan(NULL, &tokval);
636 } else { /* immediate operand */
637 if (i != 0 && i != ',' && i != ':') {
638 error(ERR_NONFATAL, "comma or end of line expected");
639 do { /* error recovery */
640 i = stdscan(NULL, &tokval);
641 } while (i != 0 && i != ',');
642 } else if (i == ':') {
643 result->oprs[operand].type |= COLON;
647 /* now convert the exprs returned from evaluate() into operand
650 if (mref) { /* it's a memory reference */
652 int b, i, s; /* basereg, indexreg, scale */
653 int64_t o; /* offset */
655 b = i = -1, o = s = 0;
656 result->oprs[operand].hintbase = hints.base;
657 result->oprs[operand].hinttype = hints.type;
659 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
660 if (e->value == 1) /* in fact it can be basereg */
662 else /* no, it has to be indexreg */
663 i = e->type, s = e->value;
666 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
667 if (b != -1) /* If the first was the base, ... */
668 i = e->type, s = e->value; /* second has to be indexreg */
670 else if (e->value != 1) { /* If both want to be index */
672 "beroset-p-592-invalid effective address");
679 if (e->type != 0) { /* is there an offset? */
680 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
682 "beroset-p-603-invalid effective address");
686 if (e->type == EXPR_UNKNOWN) {
687 o = 0; /* doesn't matter what */
688 result->oprs[operand].wrt = NO_SEG; /* nor this */
689 result->oprs[operand].segment = NO_SEG; /* or this */
691 e++; /* go to the end of the line */
693 if (e->type == EXPR_SIMPLE) {
697 if (e->type == EXPR_WRT) {
698 result->oprs[operand].wrt = e->value;
701 result->oprs[operand].wrt = NO_SEG;
703 * Look for a segment base type.
705 if (e->type && e->type < EXPR_SEGBASE) {
707 "beroset-p-630-invalid effective address");
711 while (e->type && e->value == 0)
713 if (e->type && e->value != 1) {
715 "beroset-p-637-invalid effective address");
720 result->oprs[operand].segment =
721 e->type - EXPR_SEGBASE;
724 result->oprs[operand].segment = NO_SEG;
725 while (e->type && e->value == 0)
729 "beroset-p-650-invalid effective address");
737 result->oprs[operand].wrt = NO_SEG;
738 result->oprs[operand].segment = NO_SEG;
741 if (e->type != 0) { /* there'd better be nothing left! */
743 "beroset-p-663-invalid effective address");
748 /* It is memory, but it can match any r/m operand */
749 result->oprs[operand].type |= MEMORY_ANY;
751 if (b == -1 && (i == -1 || s == 0)) {
752 int is_rel = globalbits == 64 &&
753 !(result->oprs[operand].eaflags & EAF_ABS) &&
755 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
756 (result->oprs[operand].eaflags & EAF_REL));
758 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
760 result->oprs[operand].basereg = b;
761 result->oprs[operand].indexreg = i;
762 result->oprs[operand].scale = s;
763 result->oprs[operand].offset = o;
764 } else { /* it's not a memory reference */
766 if (is_just_unknown(value)) { /* it's immediate but unknown */
767 result->oprs[operand].type |= IMMEDIATE;
768 result->oprs[operand].offset = 0; /* don't care */
769 result->oprs[operand].segment = NO_SEG; /* don't care again */
770 result->oprs[operand].wrt = NO_SEG; /* still don't care */
771 } else if (is_reloc(value)) { /* it's immediate */
772 result->oprs[operand].type |= IMMEDIATE;
773 result->oprs[operand].offset = reloc_value(value);
774 result->oprs[operand].segment = reloc_seg(value);
775 result->oprs[operand].wrt = reloc_wrt(value);
776 if (is_simple(value)) {
777 if (reloc_value(value) == 1)
778 result->oprs[operand].type |= UNITY;
779 if (optimizing >= 0 &&
780 !(result->oprs[operand].type & STRICT)) {
781 if (reloc_value(value) >= -128 &&
782 reloc_value(value) <= 127)
783 result->oprs[operand].type |= SBYTE;
786 } else { /* it's a register */
788 if (value->type >= EXPR_SIMPLE || value->value != 1) {
789 error(ERR_NONFATAL, "invalid operand type");
795 * check that its only 1 register, not an expression...
797 for (i = 1; value[i].type; i++)
798 if (value[i].value) {
799 error(ERR_NONFATAL, "invalid operand type");
804 /* clear overrides, except TO which applies to FPU regs */
805 if (result->oprs[operand].type & ~TO) {
807 * we want to produce a warning iff the specified size
808 * is different from the register size
810 i = result->oprs[operand].type & SIZE_MASK;
814 result->oprs[operand].type &= TO;
815 result->oprs[operand].type |= REGISTER;
816 result->oprs[operand].type |= reg_flags[value->type];
817 result->oprs[operand].basereg = value->type;
819 if (i && (result->oprs[operand].type & SIZE_MASK) != i)
820 error(ERR_WARNING | ERR_PASS1,
821 "register size specification ignored");
826 result->operands = operand; /* set operand count */
828 /* clear remaining operands */
829 while (operand < MAX_OPERANDS)
830 result->oprs[operand++].type = 0;
833 * Transform RESW, RESD, RESQ, REST, RESO into RESB.
835 switch (result->opcode) {
837 result->opcode = I_RESB;
838 result->oprs[0].offset *= 2;
841 result->opcode = I_RESB;
842 result->oprs[0].offset *= 4;
845 result->opcode = I_RESB;
846 result->oprs[0].offset *= 8;
849 result->opcode = I_RESB;
850 result->oprs[0].offset *= 10;
853 result->opcode = I_RESB;
854 result->oprs[0].offset *= 16;
863 static int is_comma_next(void)
870 i = stdscan(NULL, &tv);
872 return (i == ',' || i == ';' || !i);
875 void cleanup_insn(insn * i)
881 i->eops = i->eops->next;