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 license given in the file "LICENSE"
6 * distributed in the NASM archive.
8 * initial version 27/iii/95 by Simon Tatham
28 extern int in_abs_seg; /* ABSOLUTE segment flag */
29 extern int32_t abs_seg; /* ABSOLUTE segment */
30 extern int32_t abs_offset; /* ABSOLUTE segment offset */
32 static int is_comma_next(void);
35 static struct tokenval tokval;
37 static struct ofmt *outfmt; /* Structure of addresses of output routines */
38 static struct location *location; /* Pointer to current line's segment,offset */
40 void parser_global_info(struct ofmt *output, struct location * locp)
46 static int prefix_slot(enum prefixes prefix)
76 error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
81 static void process_size_override(insn * result, int operand)
83 if (tasm_compatible_mode) {
84 switch ((int)tokval.t_integer) {
85 /* For TASM compatibility a size override inside the
86 * brackets changes the size of the operand, not the
87 * address type of the operand as it does in standard
92 * is valid syntax in TASM compatibility mode. Note that
93 * you lose the ability to override the default address
94 * type for the instruction, but we never use anything
95 * but 32-bit flat model addressing in our code.
98 result->oprs[operand].type |= BITS8;
101 result->oprs[operand].type |= BITS16;
105 result->oprs[operand].type |= BITS32;
108 result->oprs[operand].type |= BITS64;
111 result->oprs[operand].type |= BITS80;
114 result->oprs[operand].type |= BITS128;
118 "invalid operand size specification");
122 /* Standard NASM compatible syntax */
123 switch ((int)tokval.t_integer) {
125 result->oprs[operand].eaflags |= EAF_TIMESTWO;
128 result->oprs[operand].eaflags |= EAF_REL;
131 result->oprs[operand].eaflags |= EAF_ABS;
134 result->oprs[operand].disp_size = 8;
135 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
140 if (result->prefixes[PPS_ASIZE] &&
141 result->prefixes[PPS_ASIZE] != tokval.t_integer)
143 "conflicting address size specifications");
145 result->prefixes[PPS_ASIZE] = tokval.t_integer;
148 result->oprs[operand].disp_size = 16;
149 result->oprs[operand].eaflags |= EAF_WORDOFFS;
153 result->oprs[operand].disp_size = 32;
154 result->oprs[operand].eaflags |= EAF_WORDOFFS;
157 result->oprs[operand].disp_size = 64;
158 result->oprs[operand].eaflags |= EAF_WORDOFFS;
161 error(ERR_NONFATAL, "invalid size specification in"
162 " effective address");
168 insn *parse_line(int pass, char *buffer, insn * result,
169 efunc errfunc, evalfunc evaluate, ldfunc ldef)
173 struct eval_hints hints;
176 bool insn_is_label = false;
180 result->forw_ref = false;
184 stdscan_bufptr = buffer;
185 i = stdscan(NULL, &tokval);
187 result->label = NULL; /* Assume no label */
188 result->eops = NULL; /* must do this, whatever happens */
189 result->operands = 0; /* must initialize this */
191 if (i == 0) { /* blank line - ignore */
192 result->opcode = -1; /* and no instruction either */
195 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
196 (i != TOKEN_REG || (REG_SREG & ~nasm_reg_flags[tokval.t_integer]))) {
197 error(ERR_NONFATAL, "label or instruction expected"
198 " at start of line");
203 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
204 /* there's a label here */
206 result->label = tokval.t_charptr;
207 i = stdscan(NULL, &tokval);
208 if (i == ':') { /* skip over the optional colon */
209 i = stdscan(NULL, &tokval);
211 error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
212 "label alone on a line without a colon might be in error");
214 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
216 * FIXME: location->segment could be NO_SEG, in which case
217 * it is possible we should be passing 'abs_seg'. Look into this.
218 * Work out whether that is *really* what we should be doing.
219 * Generally fix things. I think this is right as it is, but
220 * am still not certain.
222 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
223 location->offset, NULL, true, false, outfmt, errfunc);
228 result->opcode = -1; /* this line contains just a label */
232 for (j = 0; j < MAXPREFIX; j++)
233 result->prefixes[j] = P_none;
236 while (i == TOKEN_PREFIX ||
237 (i == TOKEN_REG && !(REG_SREG & ~nasm_reg_flags[tokval.t_integer])))
242 * Handle special case: the TIMES prefix.
244 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
247 i = stdscan(NULL, &tokval);
249 evaluate(stdscan, NULL, &tokval, NULL, pass0, error, NULL);
251 if (!value) { /* but, error in evaluator */
252 result->opcode = -1; /* unrecoverable parse error: */
253 return result; /* ignore this instruction */
255 if (!is_simple(value)) {
257 "non-constant argument supplied to TIMES");
260 result->times = value->value;
261 if (value->value < 0 && pass0 == 2) {
262 error(ERR_NONFATAL, "TIMES value %d is negative",
268 int slot = prefix_slot(tokval.t_integer);
269 if (result->prefixes[slot]) {
270 if (result->prefixes[slot] == tokval.t_integer)
272 "instruction has redundant prefixes");
275 "instruction has conflicting prefixes");
277 result->prefixes[slot] = tokval.t_integer;
278 i = stdscan(NULL, &tokval);
282 if (i != TOKEN_INSN) {
286 for (j = 0; j < MAXPREFIX; j++)
287 if ((pfx = result->prefixes[j]) != P_none)
290 if (i == 0 && pfx != P_none) {
292 * Instruction prefixes are present, but no actual
293 * instruction. This is allowed: at this point we
294 * invent a notional instruction of RESB 0.
296 result->opcode = I_RESB;
297 result->operands = 1;
298 result->oprs[0].type = IMMEDIATE;
299 result->oprs[0].offset = 0L;
300 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
303 error(ERR_NONFATAL, "parser: instruction expected");
309 result->opcode = tokval.t_integer;
310 result->condition = tokval.t_inttwo;
313 * INCBIN cannot be satisfied with incorrectly
314 * evaluated operands, since the correct values _must_ be known
315 * on the first pass. Hence, even in pass one, we set the
316 * `critical' flag on calling evaluate(), so that it will bomb
317 * out on undefined symbols.
319 if (result->opcode == I_INCBIN) {
320 critical = (pass0 < 2 ? 1 : 2);
323 critical = (pass == 2 ? 2 : 0);
325 if (result->opcode == I_DB || result->opcode == I_DW ||
326 result->opcode == I_DD || result->opcode == I_DQ ||
327 result->opcode == I_DT || result->opcode == I_DO ||
328 result->opcode == I_DY || result->opcode == I_INCBIN) {
329 extop *eop, **tail = &result->eops, **fixptr;
333 result->eops_float = false;
336 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
339 i = stdscan(NULL, &tokval);
342 else if (first && i == ':') {
343 insn_is_label = true;
348 eop = *tail = nasm_malloc(sizeof(extop));
351 eop->type = EOT_NOTHING;
355 /* is_comma_next() here is to distinguish this from
356 a string used as part of an expression... */
357 if (i == TOKEN_STR && is_comma_next()) {
358 eop->type = EOT_DB_STRING;
359 eop->stringval = tokval.t_charptr;
360 eop->stringlen = tokval.t_inttwo;
361 i = stdscan(NULL, &tokval); /* eat the comma */
362 } else if (i == TOKEN_STRFUNC) {
364 const char *funcname = tokval.t_charptr;
365 enum strfunc func = tokval.t_integer;
366 i = stdscan(NULL, &tokval);
369 i = stdscan(NULL, &tokval);
371 if (i != TOKEN_STR) {
373 "%s must be followed by a string constant",
375 eop->type = EOT_NOTHING;
377 eop->type = EOT_DB_STRING_FREE;
379 string_transform(tokval.t_charptr, tokval.t_inttwo,
380 &eop->stringval, func);
381 if (eop->stringlen == (size_t)-1) {
382 error(ERR_NONFATAL, "invalid string for transform");
383 eop->type = EOT_NOTHING;
386 if (parens && i && i != ')') {
387 i = stdscan(NULL, &tokval);
389 error(ERR_NONFATAL, "unterminated %s function",
394 i = stdscan(NULL, &tokval);
395 } else if (i == '-' || i == '+') {
396 char *save = stdscan_bufptr;
398 sign = (i == '-') ? -1 : 1;
399 i = stdscan(NULL, &tokval);
400 if (i != TOKEN_FLOAT) {
401 stdscan_bufptr = save;
402 i = tokval.t_type = token;
407 } else if (i == TOKEN_FLOAT) {
409 eop->type = EOT_DB_STRING;
410 result->eops_float = true;
411 switch (result->opcode) {
431 error(ERR_NONFATAL, "floating-point constant"
432 " encountered in DY instruction");
436 error(ERR_NONFATAL, "floating-point constant"
437 " encountered in unknown instruction");
439 * fix suggested by Pedro Gimeno... original line
441 * eop->type = EOT_NOTHING;
446 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
449 eop->stringval = (char *)eop + sizeof(extop);
450 if (!eop->stringlen ||
451 !float_const(tokval.t_charptr, sign,
452 (uint8_t *)eop->stringval,
453 eop->stringlen, error))
454 eop->type = EOT_NOTHING;
455 i = stdscan(NULL, &tokval); /* eat the comma */
457 /* anything else, assume it is an expression */
461 value = evaluate(stdscan, NULL, &tokval, NULL,
462 critical, error, NULL);
464 if (!value) { /* error in evaluator */
465 result->opcode = -1; /* unrecoverable parse error: */
466 return result; /* ignore this instruction */
468 if (is_unknown(value)) {
469 eop->type = EOT_DB_NUMBER;
470 eop->offset = 0; /* doesn't matter what we put */
471 eop->segment = eop->wrt = NO_SEG; /* likewise */
472 } else if (is_reloc(value)) {
473 eop->type = EOT_DB_NUMBER;
474 eop->offset = reloc_value(value);
475 eop->segment = reloc_seg(value);
476 eop->wrt = reloc_wrt(value);
479 "operand %d: expression is not simple"
480 " or relocatable", oper_num);
485 * We're about to call stdscan(), which will eat the
486 * comma that we're currently sitting on between
487 * arguments. However, we'd better check first that it
490 if (i == 0) /* also could be EOL */
493 error(ERR_NONFATAL, "comma expected after operand %d",
495 result->opcode = -1; /* unrecoverable parse error: */
496 return result; /* ignore this instruction */
500 if (result->opcode == I_INCBIN) {
502 * Correct syntax for INCBIN is that there should be
503 * one string operand, followed by one or two numeric
506 if (!result->eops || result->eops->type != EOT_DB_STRING)
507 error(ERR_NONFATAL, "`incbin' expects a file name");
508 else if (result->eops->next &&
509 result->eops->next->type != EOT_DB_NUMBER)
510 error(ERR_NONFATAL, "`incbin': second parameter is",
512 else if (result->eops->next && result->eops->next->next &&
513 result->eops->next->next->type != EOT_DB_NUMBER)
514 error(ERR_NONFATAL, "`incbin': third parameter is",
516 else if (result->eops->next && result->eops->next->next &&
517 result->eops->next->next->next)
519 "`incbin': more than three parameters");
523 * If we reach here, one of the above errors happened.
524 * Throw the instruction away.
528 } else /* DB ... */ if (oper_num == 0)
529 error(ERR_WARNING | ERR_PASS1,
530 "no operand for data declaration");
532 result->operands = oper_num;
537 /* right. Now we begin to parse the operands. There may be up to four
538 * of these, separated by commas, and terminated by a zero token. */
540 for (operand = 0; operand < MAX_OPERANDS; operand++) {
541 expr *value; /* used most of the time */
542 int mref; /* is this going to be a memory ref? */
543 int bracket; /* is it a [] mref, or a & mref? */
546 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
547 result->oprs[operand].eaflags = 0; /* and this */
548 result->oprs[operand].opflags = 0;
550 i = stdscan(NULL, &tokval);
552 break; /* end of operands: get out of here */
553 else if (first && i == ':') {
554 insn_is_label = true;
558 result->oprs[operand].type = 0; /* so far, no override */
559 while (i == TOKEN_SPECIAL) { /* size specifiers */
560 switch ((int)tokval.t_integer) {
562 if (!setsize) /* we want to use only the first */
563 result->oprs[operand].type |= BITS8;
568 result->oprs[operand].type |= BITS16;
574 result->oprs[operand].type |= BITS32;
579 result->oprs[operand].type |= BITS64;
584 result->oprs[operand].type |= BITS80;
589 result->oprs[operand].type |= BITS128;
594 result->oprs[operand].type |= BITS256;
598 result->oprs[operand].type |= TO;
601 result->oprs[operand].type |= STRICT;
604 result->oprs[operand].type |= FAR;
607 result->oprs[operand].type |= NEAR;
610 result->oprs[operand].type |= SHORT;
613 error(ERR_NONFATAL, "invalid operand size specification");
615 i = stdscan(NULL, &tokval);
618 if (i == '[' || i == '&') { /* memory reference */
620 bracket = (i == '[');
621 i = stdscan(NULL, &tokval); /* then skip the colon */
622 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
623 process_size_override(result, operand);
624 i = stdscan(NULL, &tokval);
626 } else { /* immediate operand, or register */
628 bracket = false; /* placate optimisers */
631 if ((result->oprs[operand].type & FAR) && !mref &&
632 result->opcode != I_JMP && result->opcode != I_CALL) {
633 error(ERR_NONFATAL, "invalid use of FAR operand specifier");
636 value = evaluate(stdscan, NULL, &tokval,
637 &result->oprs[operand].opflags,
638 critical, error, &hints);
640 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
641 result->forw_ref = true;
643 if (!value) { /* error in evaluator */
644 result->opcode = -1; /* unrecoverable parse error: */
645 return result; /* ignore this instruction */
647 if (i == ':' && mref) { /* it was seg:offset */
649 * Process the segment override.
651 if (value[1].type != 0 || value->value != 1 ||
652 REG_SREG & ~nasm_reg_flags[value->type])
653 error(ERR_NONFATAL, "invalid segment override");
654 else if (result->prefixes[PPS_SEG])
656 "instruction has conflicting segment overrides");
658 result->prefixes[PPS_SEG] = value->type;
659 if (!(REG_FSGS & ~nasm_reg_flags[value->type]))
660 result->oprs[operand].eaflags |= EAF_FSGS;
663 i = stdscan(NULL, &tokval); /* then skip the colon */
664 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
665 process_size_override(result, operand);
666 i = stdscan(NULL, &tokval);
668 value = evaluate(stdscan, NULL, &tokval,
669 &result->oprs[operand].opflags,
670 critical, error, &hints);
672 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
673 result->forw_ref = true;
675 /* and get the offset */
676 if (!value) { /* but, error in evaluator */
677 result->opcode = -1; /* unrecoverable parse error: */
678 return result; /* ignore this instruction */
681 if (mref && bracket) { /* find ] at the end */
683 error(ERR_NONFATAL, "parser: expecting ]");
684 do { /* error recovery again */
685 i = stdscan(NULL, &tokval);
686 } while (i != 0 && i != ',');
687 } else /* we got the required ] */
688 i = stdscan(NULL, &tokval);
689 } else { /* immediate operand */
690 if (i != 0 && i != ',' && i != ':') {
691 error(ERR_NONFATAL, "comma or end of line expected");
692 do { /* error recovery */
693 i = stdscan(NULL, &tokval);
694 } while (i != 0 && i != ',');
695 } else if (i == ':') {
696 result->oprs[operand].type |= COLON;
700 /* now convert the exprs returned from evaluate() into operand
703 if (mref) { /* it's a memory reference */
705 int b, i, s; /* basereg, indexreg, scale */
706 int64_t o; /* offset */
708 b = i = -1, o = s = 0;
709 result->oprs[operand].hintbase = hints.base;
710 result->oprs[operand].hinttype = hints.type;
712 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
713 if (e->value == 1) /* in fact it can be basereg */
715 else /* no, it has to be indexreg */
716 i = e->type, s = e->value;
719 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
720 if (b != -1) /* If the first was the base, ... */
721 i = e->type, s = e->value; /* second has to be indexreg */
723 else if (e->value != 1) { /* If both want to be index */
725 "beroset-p-592-invalid effective address");
732 if (e->type != 0) { /* is there an offset? */
733 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
735 "beroset-p-603-invalid effective address");
739 if (e->type == EXPR_UNKNOWN) {
740 o = 0; /* doesn't matter what */
741 result->oprs[operand].wrt = NO_SEG; /* nor this */
742 result->oprs[operand].segment = NO_SEG; /* or this */
744 e++; /* go to the end of the line */
746 if (e->type == EXPR_SIMPLE) {
750 if (e->type == EXPR_WRT) {
751 result->oprs[operand].wrt = e->value;
754 result->oprs[operand].wrt = NO_SEG;
756 * Look for a segment base type.
758 if (e->type && e->type < EXPR_SEGBASE) {
760 "beroset-p-630-invalid effective address");
764 while (e->type && e->value == 0)
766 if (e->type && e->value != 1) {
768 "beroset-p-637-invalid effective address");
773 result->oprs[operand].segment =
774 e->type - EXPR_SEGBASE;
777 result->oprs[operand].segment = NO_SEG;
778 while (e->type && e->value == 0)
782 "beroset-p-650-invalid effective address");
790 result->oprs[operand].wrt = NO_SEG;
791 result->oprs[operand].segment = NO_SEG;
794 if (e->type != 0) { /* there'd better be nothing left! */
796 "beroset-p-663-invalid effective address");
801 /* It is memory, but it can match any r/m operand */
802 result->oprs[operand].type |= MEMORY_ANY;
804 if (b == -1 && (i == -1 || s == 0)) {
805 int is_rel = globalbits == 64 &&
806 !(result->oprs[operand].eaflags & EAF_ABS) &&
808 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
809 (result->oprs[operand].eaflags & EAF_REL));
811 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
813 result->oprs[operand].basereg = b;
814 result->oprs[operand].indexreg = i;
815 result->oprs[operand].scale = s;
816 result->oprs[operand].offset = o;
817 } else { /* it's not a memory reference */
819 if (is_just_unknown(value)) { /* it's immediate but unknown */
820 result->oprs[operand].type |= IMMEDIATE;
821 result->oprs[operand].offset = 0; /* don't care */
822 result->oprs[operand].segment = NO_SEG; /* don't care again */
823 result->oprs[operand].wrt = NO_SEG; /* still don't care */
824 } else if (is_reloc(value)) { /* it's immediate */
825 result->oprs[operand].type |= IMMEDIATE;
826 result->oprs[operand].offset = reloc_value(value);
827 result->oprs[operand].segment = reloc_seg(value);
828 result->oprs[operand].wrt = reloc_wrt(value);
829 if (is_simple(value)) {
830 if (reloc_value(value) == 1)
831 result->oprs[operand].type |= UNITY;
832 if (optimizing >= 0 &&
833 !(result->oprs[operand].type & STRICT)) {
834 int64_t v64 = reloc_value(value);
835 int32_t v32 = (int32_t)v64;
836 int16_t v16 = (int16_t)v32;
838 if (v64 >= -128 && v64 <= 127)
839 result->oprs[operand].type |= SBYTE64;
840 if (v32 >= -128 && v32 <= 127)
841 result->oprs[operand].type |= SBYTE32;
842 if (v16 >= -128 && v16 <= 127)
843 result->oprs[operand].type |= SBYTE16;
846 } else { /* it's a register */
849 if (value->type >= EXPR_SIMPLE || value->value != 1) {
850 error(ERR_NONFATAL, "invalid operand type");
856 * check that its only 1 register, not an expression...
858 for (i = 1; value[i].type; i++)
859 if (value[i].value) {
860 error(ERR_NONFATAL, "invalid operand type");
865 /* clear overrides, except TO which applies to FPU regs */
866 if (result->oprs[operand].type & ~TO) {
868 * we want to produce a warning iff the specified size
869 * is different from the register size
871 rs = result->oprs[operand].type & SIZE_MASK;
875 result->oprs[operand].type &= TO;
876 result->oprs[operand].type |= REGISTER;
877 result->oprs[operand].type |= nasm_reg_flags[value->type];
878 result->oprs[operand].basereg = value->type;
880 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
881 error(ERR_WARNING | ERR_PASS1,
882 "register size specification ignored");
887 result->operands = operand; /* set operand count */
889 /* clear remaining operands */
890 while (operand < MAX_OPERANDS)
891 result->oprs[operand++].type = 0;
894 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
896 switch (result->opcode) {
898 result->opcode = I_RESB;
899 result->oprs[0].offset *= 2;
902 result->opcode = I_RESB;
903 result->oprs[0].offset *= 4;
906 result->opcode = I_RESB;
907 result->oprs[0].offset *= 8;
910 result->opcode = I_RESB;
911 result->oprs[0].offset *= 10;
914 result->opcode = I_RESB;
915 result->oprs[0].offset *= 16;
918 result->opcode = I_RESB;
919 result->oprs[0].offset *= 32;
928 static int is_comma_next(void)
935 i = stdscan(NULL, &tv);
937 return (i == ',' || i == ';' || !i);
940 void cleanup_insn(insn * i)
944 while ((e = i->eops)) {
946 if (e->type == EOT_DB_STRING_FREE)
947 nasm_free(e->stringval);