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;
181 result->forw_ref = false;
185 stdscan_bufptr = buffer;
186 i = stdscan(NULL, &tokval);
188 result->label = NULL; /* Assume no label */
189 result->eops = NULL; /* must do this, whatever happens */
190 result->operands = 0; /* must initialize this */
192 if (i == 0) { /* blank line - ignore */
193 result->opcode = -1; /* and no instruction either */
196 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
197 (i != TOKEN_REG || (REG_SREG & ~nasm_reg_flags[tokval.t_integer]))) {
198 error(ERR_NONFATAL, "label or instruction expected"
199 " at start of line");
204 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
205 /* there's a label here */
207 result->label = tokval.t_charptr;
208 i = stdscan(NULL, &tokval);
209 if (i == ':') { /* skip over the optional colon */
210 i = stdscan(NULL, &tokval);
212 error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
213 "label alone on a line without a colon might be in error");
215 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
217 * FIXME: location->segment could be NO_SEG, in which case
218 * it is possible we should be passing 'abs_seg'. Look into this.
219 * Work out whether that is *really* what we should be doing.
220 * Generally fix things. I think this is right as it is, but
221 * am still not certain.
223 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
224 location->offset, NULL, true, false, outfmt, errfunc);
229 result->opcode = -1; /* this line contains just a label */
233 for (j = 0; j < MAXPREFIX; j++)
234 result->prefixes[j] = P_none;
237 while (i == TOKEN_PREFIX ||
238 (i == TOKEN_REG && !(REG_SREG & ~nasm_reg_flags[tokval.t_integer])))
243 * Handle special case: the TIMES prefix.
245 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
248 i = stdscan(NULL, &tokval);
250 evaluate(stdscan, NULL, &tokval, NULL, pass0, error, NULL);
252 if (!value) { /* but, error in evaluator */
253 result->opcode = -1; /* unrecoverable parse error: */
254 return result; /* ignore this instruction */
256 if (!is_simple(value)) {
258 "non-constant argument supplied to TIMES");
261 result->times = value->value;
262 if (value->value < 0 && pass0 == 2) {
263 error(ERR_NONFATAL, "TIMES value %d is negative",
269 int slot = prefix_slot(tokval.t_integer);
270 if (result->prefixes[slot]) {
271 if (result->prefixes[slot] == tokval.t_integer)
273 "instruction has redundant prefixes");
276 "instruction has conflicting prefixes");
278 result->prefixes[slot] = tokval.t_integer;
279 i = stdscan(NULL, &tokval);
283 if (i != TOKEN_INSN) {
287 for (j = 0; j < MAXPREFIX; j++)
288 if ((pfx = result->prefixes[j]) != P_none)
291 if (i == 0 && pfx != P_none) {
293 * Instruction prefixes are present, but no actual
294 * instruction. This is allowed: at this point we
295 * invent a notional instruction of RESB 0.
297 result->opcode = I_RESB;
298 result->operands = 1;
299 result->oprs[0].type = IMMEDIATE;
300 result->oprs[0].offset = 0L;
301 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
304 error(ERR_NONFATAL, "parser: instruction expected");
310 result->opcode = tokval.t_integer;
311 result->condition = tokval.t_inttwo;
314 * INCBIN cannot be satisfied with incorrectly
315 * evaluated operands, since the correct values _must_ be known
316 * on the first pass. Hence, even in pass one, we set the
317 * `critical' flag on calling evaluate(), so that it will bomb
318 * out on undefined symbols.
320 if (result->opcode == I_INCBIN) {
321 critical = (pass0 < 2 ? 1 : 2);
324 critical = (pass == 2 ? 2 : 0);
326 if (result->opcode == I_DB || result->opcode == I_DW ||
327 result->opcode == I_DD || result->opcode == I_DQ ||
328 result->opcode == I_DT || result->opcode == I_DO ||
329 result->opcode == I_DY || result->opcode == I_INCBIN) {
330 extop *eop, **tail = &result->eops, **fixptr;
334 result->eops_float = false;
337 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
340 i = stdscan(NULL, &tokval);
343 else if (first && i == ':') {
344 insn_is_label = true;
349 eop = *tail = nasm_malloc(sizeof(extop));
352 eop->type = EOT_NOTHING;
356 /* is_comma_next() here is to distinguish this from
357 a string used as part of an expression... */
358 if (i == TOKEN_STR && is_comma_next()) {
359 eop->type = EOT_DB_STRING;
360 eop->stringval = tokval.t_charptr;
361 eop->stringlen = tokval.t_inttwo;
362 i = stdscan(NULL, &tokval); /* eat the comma */
363 } else if (i == TOKEN_STRFUNC) {
365 const char *funcname = tokval.t_charptr;
366 enum strfunc func = tokval.t_integer;
367 i = stdscan(NULL, &tokval);
370 i = stdscan(NULL, &tokval);
372 if (i != TOKEN_STR) {
374 "%s must be followed by a string constant",
376 eop->type = EOT_NOTHING;
378 eop->type = EOT_DB_STRING_FREE;
380 string_transform(tokval.t_charptr, tokval.t_inttwo,
381 &eop->stringval, func);
382 if (eop->stringlen == (size_t)-1) {
383 error(ERR_NONFATAL, "invalid string for transform");
384 eop->type = EOT_NOTHING;
387 if (parens && i && i != ')') {
388 i = stdscan(NULL, &tokval);
390 error(ERR_NONFATAL, "unterminated %s function",
395 i = stdscan(NULL, &tokval);
396 } else if (i == '-' || i == '+') {
397 char *save = stdscan_bufptr;
399 sign = (i == '-') ? -1 : 1;
400 i = stdscan(NULL, &tokval);
401 if (i != TOKEN_FLOAT) {
402 stdscan_bufptr = save;
403 i = tokval.t_type = token;
408 } else if (i == TOKEN_FLOAT) {
410 eop->type = EOT_DB_STRING;
411 result->eops_float = true;
412 switch (result->opcode) {
432 error(ERR_NONFATAL, "floating-point constant"
433 " encountered in DY instruction");
437 error(ERR_NONFATAL, "floating-point constant"
438 " encountered in unknown instruction");
440 * fix suggested by Pedro Gimeno... original line
442 * eop->type = EOT_NOTHING;
447 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
450 eop->stringval = (char *)eop + sizeof(extop);
451 if (!eop->stringlen ||
452 !float_const(tokval.t_charptr, sign,
453 (uint8_t *)eop->stringval,
454 eop->stringlen, error))
455 eop->type = EOT_NOTHING;
456 i = stdscan(NULL, &tokval); /* eat the comma */
458 /* anything else, assume it is an expression */
462 value = evaluate(stdscan, NULL, &tokval, NULL,
463 critical, error, NULL);
465 if (!value) { /* error in evaluator */
466 result->opcode = -1; /* unrecoverable parse error: */
467 return result; /* ignore this instruction */
469 if (is_unknown(value)) {
470 eop->type = EOT_DB_NUMBER;
471 eop->offset = 0; /* doesn't matter what we put */
472 eop->segment = eop->wrt = NO_SEG; /* likewise */
473 } else if (is_reloc(value)) {
474 eop->type = EOT_DB_NUMBER;
475 eop->offset = reloc_value(value);
476 eop->segment = reloc_seg(value);
477 eop->wrt = reloc_wrt(value);
480 "operand %d: expression is not simple"
481 " or relocatable", oper_num);
486 * We're about to call stdscan(), which will eat the
487 * comma that we're currently sitting on between
488 * arguments. However, we'd better check first that it
491 if (i == 0) /* also could be EOL */
494 error(ERR_NONFATAL, "comma expected after operand %d",
496 result->opcode = -1; /* unrecoverable parse error: */
497 return result; /* ignore this instruction */
501 if (result->opcode == I_INCBIN) {
503 * Correct syntax for INCBIN is that there should be
504 * one string operand, followed by one or two numeric
507 if (!result->eops || result->eops->type != EOT_DB_STRING)
508 error(ERR_NONFATAL, "`incbin' expects a file name");
509 else if (result->eops->next &&
510 result->eops->next->type != EOT_DB_NUMBER)
511 error(ERR_NONFATAL, "`incbin': second parameter is",
513 else if (result->eops->next && result->eops->next->next &&
514 result->eops->next->next->type != EOT_DB_NUMBER)
515 error(ERR_NONFATAL, "`incbin': third parameter is",
517 else if (result->eops->next && result->eops->next->next &&
518 result->eops->next->next->next)
520 "`incbin': more than three parameters");
524 * If we reach here, one of the above errors happened.
525 * Throw the instruction away.
529 } else /* DB ... */ if (oper_num == 0)
530 error(ERR_WARNING | ERR_PASS1,
531 "no operand for data declaration");
533 result->operands = oper_num;
538 /* right. Now we begin to parse the operands. There may be up to four
539 * of these, separated by commas, and terminated by a zero token. */
541 for (operand = 0; operand < MAX_OPERANDS; operand++) {
542 expr *value; /* used most of the time */
543 int mref; /* is this going to be a memory ref? */
544 int bracket; /* is it a [] mref, or a & mref? */
547 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
548 result->oprs[operand].eaflags = 0; /* and this */
549 result->oprs[operand].opflags = 0;
551 i = stdscan(NULL, &tokval);
553 break; /* end of operands: get out of here */
554 else if (first && i == ':') {
555 insn_is_label = true;
559 result->oprs[operand].type = 0; /* so far, no override */
560 while (i == TOKEN_SPECIAL) { /* size specifiers */
561 switch ((int)tokval.t_integer) {
563 if (!setsize) /* we want to use only the first */
564 result->oprs[operand].type |= BITS8;
569 result->oprs[operand].type |= BITS16;
575 result->oprs[operand].type |= BITS32;
580 result->oprs[operand].type |= BITS64;
585 result->oprs[operand].type |= BITS80;
590 result->oprs[operand].type |= BITS128;
595 result->oprs[operand].type |= BITS256;
599 result->oprs[operand].type |= TO;
602 result->oprs[operand].type |= STRICT;
605 result->oprs[operand].type |= FAR;
608 result->oprs[operand].type |= NEAR;
611 result->oprs[operand].type |= SHORT;
614 error(ERR_NONFATAL, "invalid operand size specification");
616 i = stdscan(NULL, &tokval);
619 if (i == '[' || i == '&') { /* memory reference */
621 bracket = (i == '[');
622 i = stdscan(NULL, &tokval); /* then skip the colon */
623 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
624 process_size_override(result, operand);
625 i = stdscan(NULL, &tokval);
627 } else { /* immediate operand, or register */
629 bracket = false; /* placate optimisers */
632 if ((result->oprs[operand].type & FAR) && !mref &&
633 result->opcode != I_JMP && result->opcode != I_CALL) {
634 error(ERR_NONFATAL, "invalid use of FAR operand specifier");
637 value = evaluate(stdscan, NULL, &tokval,
638 &result->oprs[operand].opflags,
639 critical, error, &hints);
641 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
642 result->forw_ref = true;
644 if (!value) { /* error in evaluator */
645 result->opcode = -1; /* unrecoverable parse error: */
646 return result; /* ignore this instruction */
648 if (i == ':' && mref) { /* it was seg:offset */
650 * Process the segment override.
652 if (value[1].type != 0 || value->value != 1 ||
653 REG_SREG & ~nasm_reg_flags[value->type])
654 error(ERR_NONFATAL, "invalid segment override");
655 else if (result->prefixes[PPS_SEG])
657 "instruction has conflicting segment overrides");
659 result->prefixes[PPS_SEG] = value->type;
660 if (!(REG_FSGS & ~nasm_reg_flags[value->type]))
661 result->oprs[operand].eaflags |= EAF_FSGS;
664 i = stdscan(NULL, &tokval); /* then skip the colon */
665 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
666 process_size_override(result, operand);
667 i = stdscan(NULL, &tokval);
669 value = evaluate(stdscan, NULL, &tokval,
670 &result->oprs[operand].opflags,
671 critical, error, &hints);
673 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
674 result->forw_ref = true;
676 /* and get the offset */
677 if (!value) { /* but, error in evaluator */
678 result->opcode = -1; /* unrecoverable parse error: */
679 return result; /* ignore this instruction */
684 if (mref && bracket) { /* find ] at the end */
686 error(ERR_NONFATAL, "parser: expecting ]");
688 } else { /* we got the required ] */
689 i = stdscan(NULL, &tokval);
690 if (i != 0 && i != ',') {
691 error(ERR_NONFATAL, "comma or end of line expected");
695 } else { /* immediate operand */
696 if (i != 0 && i != ',' && i != ':') {
697 error(ERR_NONFATAL, "comma, colon or end of line expected");
699 } else if (i == ':') {
700 result->oprs[operand].type |= COLON;
704 do { /* error recovery */
705 i = stdscan(NULL, &tokval);
706 } while (i != 0 && i != ',');
709 /* now convert the exprs returned from evaluate() into operand
712 if (mref) { /* it's a memory reference */
714 int b, i, s; /* basereg, indexreg, scale */
715 int64_t o; /* offset */
717 b = i = -1, o = s = 0;
718 result->oprs[operand].hintbase = hints.base;
719 result->oprs[operand].hinttype = hints.type;
721 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
722 if (e->value == 1) /* in fact it can be basereg */
724 else /* no, it has to be indexreg */
725 i = e->type, s = e->value;
728 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
729 if (b != -1) /* If the first was the base, ... */
730 i = e->type, s = e->value; /* second has to be indexreg */
732 else if (e->value != 1) { /* If both want to be index */
734 "beroset-p-592-invalid effective address");
741 if (e->type != 0) { /* is there an offset? */
742 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
744 "beroset-p-603-invalid effective address");
748 if (e->type == EXPR_UNKNOWN) {
749 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
750 o = 0; /* doesn't matter what */
751 result->oprs[operand].wrt = NO_SEG; /* nor this */
752 result->oprs[operand].segment = NO_SEG; /* or this */
754 e++; /* go to the end of the line */
756 if (e->type == EXPR_SIMPLE) {
760 if (e->type == EXPR_WRT) {
761 result->oprs[operand].wrt = e->value;
764 result->oprs[operand].wrt = NO_SEG;
766 * Look for a segment base type.
768 if (e->type && e->type < EXPR_SEGBASE) {
770 "beroset-p-630-invalid effective address");
774 while (e->type && e->value == 0)
776 if (e->type && e->value != 1) {
778 "beroset-p-637-invalid effective address");
783 result->oprs[operand].segment =
784 e->type - EXPR_SEGBASE;
787 result->oprs[operand].segment = NO_SEG;
788 while (e->type && e->value == 0)
792 "beroset-p-650-invalid effective address");
800 result->oprs[operand].wrt = NO_SEG;
801 result->oprs[operand].segment = NO_SEG;
804 if (e->type != 0) { /* there'd better be nothing left! */
806 "beroset-p-663-invalid effective address");
811 /* It is memory, but it can match any r/m operand */
812 result->oprs[operand].type |= MEMORY_ANY;
814 if (b == -1 && (i == -1 || s == 0)) {
815 int is_rel = globalbits == 64 &&
816 !(result->oprs[operand].eaflags & EAF_ABS) &&
818 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
819 (result->oprs[operand].eaflags & EAF_REL));
821 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
823 result->oprs[operand].basereg = b;
824 result->oprs[operand].indexreg = i;
825 result->oprs[operand].scale = s;
826 result->oprs[operand].offset = o;
827 } else { /* it's not a memory reference */
828 if (is_just_unknown(value)) { /* it's immediate but unknown */
829 result->oprs[operand].type |= IMMEDIATE;
830 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
831 result->oprs[operand].offset = 0; /* don't care */
832 result->oprs[operand].segment = NO_SEG; /* don't care again */
833 result->oprs[operand].wrt = NO_SEG; /* still don't care */
835 if(optimizing >= 0 && !(result->oprs[operand].type & STRICT))
838 result->oprs[operand].type |= SBYTE16 | SBYTE32 | SBYTE64;
840 } else if (is_reloc(value)) { /* it's immediate */
841 result->oprs[operand].type |= IMMEDIATE;
842 result->oprs[operand].offset = reloc_value(value);
843 result->oprs[operand].segment = reloc_seg(value);
844 result->oprs[operand].wrt = reloc_wrt(value);
845 if (is_simple(value)) {
846 if (reloc_value(value) == 1)
847 result->oprs[operand].type |= UNITY;
848 if (optimizing >= 0 &&
849 !(result->oprs[operand].type & STRICT)) {
850 int64_t v64 = reloc_value(value);
851 int32_t v32 = (int32_t)v64;
852 int16_t v16 = (int16_t)v32;
854 if (v64 >= -128 && v64 <= 127)
855 result->oprs[operand].type |= SBYTE64;
856 if (v32 >= -128 && v32 <= 127)
857 result->oprs[operand].type |= SBYTE32;
858 if (v16 >= -128 && v16 <= 127)
859 result->oprs[operand].type |= SBYTE16;
862 } else { /* it's a register */
865 if (value->type >= EXPR_SIMPLE || value->value != 1) {
866 error(ERR_NONFATAL, "invalid operand type");
872 * check that its only 1 register, not an expression...
874 for (i = 1; value[i].type; i++)
875 if (value[i].value) {
876 error(ERR_NONFATAL, "invalid operand type");
881 /* clear overrides, except TO which applies to FPU regs */
882 if (result->oprs[operand].type & ~TO) {
884 * we want to produce a warning iff the specified size
885 * is different from the register size
887 rs = result->oprs[operand].type & SIZE_MASK;
891 result->oprs[operand].type &= TO;
892 result->oprs[operand].type |= REGISTER;
893 result->oprs[operand].type |= nasm_reg_flags[value->type];
894 result->oprs[operand].basereg = value->type;
896 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
897 error(ERR_WARNING | ERR_PASS1,
898 "register size specification ignored");
903 result->operands = operand; /* set operand count */
905 /* clear remaining operands */
906 while (operand < MAX_OPERANDS)
907 result->oprs[operand++].type = 0;
910 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
912 switch (result->opcode) {
914 result->opcode = I_RESB;
915 result->oprs[0].offset *= 2;
918 result->opcode = I_RESB;
919 result->oprs[0].offset *= 4;
922 result->opcode = I_RESB;
923 result->oprs[0].offset *= 8;
926 result->opcode = I_RESB;
927 result->oprs[0].offset *= 10;
930 result->opcode = I_RESB;
931 result->oprs[0].offset *= 16;
934 result->opcode = I_RESB;
935 result->oprs[0].offset *= 32;
944 static int is_comma_next(void)
951 i = stdscan(NULL, &tv);
953 return (i == ',' || i == ';' || !i);
956 void cleanup_insn(insn * i)
960 while ((e = i->eops)) {
962 if (e->type == EOT_DB_STRING_FREE)
963 nasm_free(e->stringval);