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)
74 error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
79 static void process_size_override(insn * result, int operand)
81 if (tasm_compatible_mode) {
82 switch ((int)tokval.t_integer) {
83 /* For TASM compatibility a size override inside the
84 * brackets changes the size of the operand, not the
85 * address type of the operand as it does in standard
90 * is valid syntax in TASM compatibility mode. Note that
91 * you lose the ability to override the default address
92 * type for the instruction, but we never use anything
93 * but 32-bit flat model addressing in our code.
96 result->oprs[operand].type |= BITS8;
99 result->oprs[operand].type |= BITS16;
103 result->oprs[operand].type |= BITS32;
106 result->oprs[operand].type |= BITS64;
109 result->oprs[operand].type |= BITS80;
112 result->oprs[operand].type |= BITS128;
116 "invalid operand size specification");
120 /* Standard NASM compatible syntax */
121 switch ((int)tokval.t_integer) {
123 result->oprs[operand].eaflags |= EAF_TIMESTWO;
126 result->oprs[operand].eaflags |= EAF_REL;
129 result->oprs[operand].eaflags |= EAF_ABS;
132 result->oprs[operand].disp_size = 8;
133 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
138 if (result->prefixes[PPS_ASIZE] &&
139 result->prefixes[PPS_ASIZE] != tokval.t_integer)
141 "conflicting address size specifications");
143 result->prefixes[PPS_ASIZE] = tokval.t_integer;
146 result->oprs[operand].disp_size = 16;
147 result->oprs[operand].eaflags |= EAF_WORDOFFS;
151 result->oprs[operand].disp_size = 32;
152 result->oprs[operand].eaflags |= EAF_WORDOFFS;
155 result->oprs[operand].disp_size = 64;
156 result->oprs[operand].eaflags |= EAF_WORDOFFS;
159 error(ERR_NONFATAL, "invalid size specification in"
160 " effective address");
166 insn *parse_line(int pass, char *buffer, insn * result,
167 efunc errfunc, evalfunc evaluate, ldfunc ldef)
171 struct eval_hints hints;
174 bool insn_is_label = false;
178 result->forw_ref = false;
182 stdscan_bufptr = buffer;
183 i = stdscan(NULL, &tokval);
185 result->label = NULL; /* Assume no label */
186 result->eops = NULL; /* must do this, whatever happens */
187 result->operands = 0; /* must initialize this */
189 if (i == 0) { /* blank line - ignore */
190 result->opcode = -1; /* and no instruction either */
193 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
194 (i != TOKEN_REG || (REG_SREG & ~nasm_reg_flags[tokval.t_integer]))) {
195 error(ERR_NONFATAL, "label or instruction expected"
196 " at start of line");
201 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
202 /* there's a label here */
204 result->label = tokval.t_charptr;
205 i = stdscan(NULL, &tokval);
206 if (i == ':') { /* skip over the optional colon */
207 i = stdscan(NULL, &tokval);
209 error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
210 "label alone on a line without a colon might be in error");
212 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
214 * FIXME: location->segment could be NO_SEG, in which case
215 * it is possible we should be passing 'abs_seg'. Look into this.
216 * Work out whether that is *really* what we should be doing.
217 * Generally fix things. I think this is right as it is, but
218 * am still not certain.
220 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
221 location->offset, NULL, true, false, outfmt, errfunc);
226 result->opcode = -1; /* this line contains just a label */
230 for (j = 0; j < MAXPREFIX; j++)
231 result->prefixes[j] = P_none;
234 while (i == TOKEN_PREFIX ||
235 (i == TOKEN_REG && !(REG_SREG & ~nasm_reg_flags[tokval.t_integer])))
240 * Handle special case: the TIMES prefix.
242 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
245 i = stdscan(NULL, &tokval);
247 evaluate(stdscan, NULL, &tokval, NULL, pass0, error, NULL);
249 if (!value) { /* but, error in evaluator */
250 result->opcode = -1; /* unrecoverable parse error: */
251 return result; /* ignore this instruction */
253 if (!is_simple(value)) {
255 "non-constant argument supplied to TIMES");
258 result->times = value->value;
259 if (value->value < 0) {
260 error(ERR_NONFATAL, "TIMES value %d is negative",
266 int slot = prefix_slot(tokval.t_integer);
267 if (result->prefixes[slot]) {
268 if (result->prefixes[slot] == tokval.t_integer)
270 "instruction has redundant prefixes");
273 "instruction has conflicting prefixes");
275 result->prefixes[slot] = tokval.t_integer;
276 i = stdscan(NULL, &tokval);
280 if (i != TOKEN_INSN) {
284 for (j = 0; j < MAXPREFIX; j++)
285 if ((pfx = result->prefixes[j]) != P_none)
288 if (i == 0 && pfx != P_none) {
290 * Instruction prefixes are present, but no actual
291 * instruction. This is allowed: at this point we
292 * invent a notional instruction of RESB 0.
294 result->opcode = I_RESB;
295 result->operands = 1;
296 result->oprs[0].type = IMMEDIATE;
297 result->oprs[0].offset = 0L;
298 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
301 error(ERR_NONFATAL, "parser: instruction expected");
307 result->opcode = tokval.t_integer;
308 result->condition = tokval.t_inttwo;
311 * RESB, RESW and RESD cannot be satisfied with incorrectly
312 * evaluated operands, since the correct values _must_ be known
313 * on the first pass. Hence, even in pass one, we set the
314 * `critical' flag on calling evaluate(), so that it will bomb
315 * out on undefined symbols. Nasty, but there's nothing we can
318 * For the moment, EQU has the same difficulty, so we'll
321 if (result->opcode == I_RESB || result->opcode == I_RESW ||
322 result->opcode == I_RESD || result->opcode == I_RESQ ||
323 result->opcode == I_REST || result->opcode == I_RESO ||
324 result->opcode == I_RESY ||
325 result->opcode == I_INCBIN) {
326 critical = (pass0 < 2 ? 1 : 2);
329 critical = (pass == 2 ? 2 : 0);
331 if (result->opcode == I_DB || result->opcode == I_DW ||
332 result->opcode == I_DD || result->opcode == I_DQ ||
333 result->opcode == I_DT || result->opcode == I_DO ||
334 result->opcode == I_DY || result->opcode == I_INCBIN) {
335 extop *eop, **tail = &result->eops, **fixptr;
339 result->eops_float = false;
342 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
345 i = stdscan(NULL, &tokval);
348 else if (first && i == ':') {
349 insn_is_label = true;
354 eop = *tail = nasm_malloc(sizeof(extop));
357 eop->type = EOT_NOTHING;
361 /* is_comma_next() here is to distinguish this from
362 a string used as part of an expression... */
363 if (i == TOKEN_STR && is_comma_next()) {
364 eop->type = EOT_DB_STRING;
365 eop->stringval = tokval.t_charptr;
366 eop->stringlen = tokval.t_inttwo;
367 i = stdscan(NULL, &tokval); /* eat the comma */
368 } else if (i == TOKEN_STRFUNC) {
370 const char *funcname = tokval.t_charptr;
371 enum strfunc func = tokval.t_integer;
372 i = stdscan(NULL, &tokval);
375 i = stdscan(NULL, &tokval);
377 if (i != TOKEN_STR) {
379 "%s must be followed by a string constant",
381 eop->type = EOT_NOTHING;
383 eop->type = EOT_DB_STRING_FREE;
385 string_transform(tokval.t_charptr, tokval.t_inttwo,
386 &eop->stringval, func);
387 if (eop->stringlen == (size_t)-1) {
388 error(ERR_NONFATAL, "invalid string for transform");
389 eop->type = EOT_NOTHING;
392 if (parens && i && i != ')') {
393 i = stdscan(NULL, &tokval);
395 error(ERR_NONFATAL, "unterminated %s function",
400 i = stdscan(NULL, &tokval);
401 } else if (i == '-' || i == '+') {
402 char *save = stdscan_bufptr;
404 sign = (i == '-') ? -1 : 1;
405 i = stdscan(NULL, &tokval);
406 if (i != TOKEN_FLOAT) {
407 stdscan_bufptr = save;
408 i = tokval.t_type = token;
413 } else if (i == TOKEN_FLOAT) {
415 eop->type = EOT_DB_STRING;
416 result->eops_float = true;
417 switch (result->opcode) {
437 error(ERR_NONFATAL, "floating-point constant"
438 " encountered in DY instruction");
442 error(ERR_NONFATAL, "floating-point constant"
443 " encountered in unknown instruction");
445 * fix suggested by Pedro Gimeno... original line
447 * eop->type = EOT_NOTHING;
452 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
455 eop->stringval = (char *)eop + sizeof(extop);
456 if (!eop->stringlen ||
457 !float_const(tokval.t_charptr, sign,
458 (uint8_t *)eop->stringval,
459 eop->stringlen, error))
460 eop->type = EOT_NOTHING;
461 i = stdscan(NULL, &tokval); /* eat the comma */
463 /* anything else, assume it is an expression */
467 value = evaluate(stdscan, NULL, &tokval, NULL,
468 critical, error, NULL);
470 if (!value) { /* error in evaluator */
471 result->opcode = -1; /* unrecoverable parse error: */
472 return result; /* ignore this instruction */
474 if (is_unknown(value)) {
475 eop->type = EOT_DB_NUMBER;
476 eop->offset = 0; /* doesn't matter what we put */
477 eop->segment = eop->wrt = NO_SEG; /* likewise */
478 } else if (is_reloc(value)) {
479 eop->type = EOT_DB_NUMBER;
480 eop->offset = reloc_value(value);
481 eop->segment = reloc_seg(value);
482 eop->wrt = reloc_wrt(value);
485 "operand %d: expression is not simple"
486 " or relocatable", oper_num);
491 * We're about to call stdscan(), which will eat the
492 * comma that we're currently sitting on between
493 * arguments. However, we'd better check first that it
496 if (i == 0) /* also could be EOL */
499 error(ERR_NONFATAL, "comma expected after operand %d",
501 result->opcode = -1; /* unrecoverable parse error: */
502 return result; /* ignore this instruction */
506 if (result->opcode == I_INCBIN) {
508 * Correct syntax for INCBIN is that there should be
509 * one string operand, followed by one or two numeric
512 if (!result->eops || result->eops->type != EOT_DB_STRING)
513 error(ERR_NONFATAL, "`incbin' expects a file name");
514 else if (result->eops->next &&
515 result->eops->next->type != EOT_DB_NUMBER)
516 error(ERR_NONFATAL, "`incbin': second parameter is",
518 else if (result->eops->next && result->eops->next->next &&
519 result->eops->next->next->type != EOT_DB_NUMBER)
520 error(ERR_NONFATAL, "`incbin': third parameter is",
522 else if (result->eops->next && result->eops->next->next &&
523 result->eops->next->next->next)
525 "`incbin': more than three parameters");
529 * If we reach here, one of the above errors happened.
530 * Throw the instruction away.
534 } else /* DB ... */ if (oper_num == 0)
535 error(ERR_WARNING | ERR_PASS1,
536 "no operand for data declaration");
538 result->operands = oper_num;
543 /* right. Now we begin to parse the operands. There may be up to four
544 * of these, separated by commas, and terminated by a zero token. */
546 for (operand = 0; operand < MAX_OPERANDS; operand++) {
547 expr *value; /* used most of the time */
548 int mref; /* is this going to be a memory ref? */
549 int bracket; /* is it a [] mref, or a & mref? */
552 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
553 result->oprs[operand].eaflags = 0; /* and this */
554 result->oprs[operand].opflags = 0;
556 i = stdscan(NULL, &tokval);
558 break; /* end of operands: get out of here */
559 else if (first && i == ':') {
560 insn_is_label = true;
564 result->oprs[operand].type = 0; /* so far, no override */
565 while (i == TOKEN_SPECIAL) { /* size specifiers */
566 switch ((int)tokval.t_integer) {
568 if (!setsize) /* we want to use only the first */
569 result->oprs[operand].type |= BITS8;
574 result->oprs[operand].type |= BITS16;
580 result->oprs[operand].type |= BITS32;
585 result->oprs[operand].type |= BITS64;
590 result->oprs[operand].type |= BITS80;
595 result->oprs[operand].type |= BITS128;
600 result->oprs[operand].type |= BITS256;
604 result->oprs[operand].type |= TO;
607 result->oprs[operand].type |= STRICT;
610 result->oprs[operand].type |= FAR;
613 result->oprs[operand].type |= NEAR;
616 result->oprs[operand].type |= SHORT;
619 error(ERR_NONFATAL, "invalid operand size specification");
621 i = stdscan(NULL, &tokval);
624 if (i == '[' || i == '&') { /* memory reference */
626 bracket = (i == '[');
627 i = stdscan(NULL, &tokval); /* then skip the colon */
628 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
629 process_size_override(result, operand);
630 i = stdscan(NULL, &tokval);
632 } else { /* immediate operand, or register */
634 bracket = false; /* placate optimisers */
637 if ((result->oprs[operand].type & FAR) && !mref &&
638 result->opcode != I_JMP && result->opcode != I_CALL) {
639 error(ERR_NONFATAL, "invalid use of FAR operand specifier");
642 value = evaluate(stdscan, NULL, &tokval,
643 &result->oprs[operand].opflags,
644 critical, error, &hints);
646 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
647 result->forw_ref = true;
649 if (!value) { /* error in evaluator */
650 result->opcode = -1; /* unrecoverable parse error: */
651 return result; /* ignore this instruction */
653 if (i == ':' && mref) { /* it was seg:offset */
655 * Process the segment override.
657 if (value[1].type != 0 || value->value != 1 ||
658 REG_SREG & ~nasm_reg_flags[value->type])
659 error(ERR_NONFATAL, "invalid segment override");
660 else if (result->prefixes[PPS_SEG])
662 "instruction has conflicting segment overrides");
664 result->prefixes[PPS_SEG] = value->type;
665 if (!(REG_FSGS & ~nasm_reg_flags[value->type]))
666 result->oprs[operand].eaflags |= EAF_FSGS;
669 i = stdscan(NULL, &tokval); /* then skip the colon */
670 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
671 process_size_override(result, operand);
672 i = stdscan(NULL, &tokval);
674 value = evaluate(stdscan, NULL, &tokval,
675 &result->oprs[operand].opflags,
676 critical, error, &hints);
678 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
679 result->forw_ref = true;
681 /* and get the offset */
682 if (!value) { /* but, error in evaluator */
683 result->opcode = -1; /* unrecoverable parse error: */
684 return result; /* ignore this instruction */
687 if (mref && bracket) { /* find ] at the end */
689 error(ERR_NONFATAL, "parser: expecting ]");
690 do { /* error recovery again */
691 i = stdscan(NULL, &tokval);
692 } while (i != 0 && i != ',');
693 } else /* we got the required ] */
694 i = stdscan(NULL, &tokval);
695 } else { /* immediate operand */
696 if (i != 0 && i != ',' && i != ':') {
697 error(ERR_NONFATAL, "comma or end of line expected");
698 do { /* error recovery */
699 i = stdscan(NULL, &tokval);
700 } while (i != 0 && i != ',');
701 } else if (i == ':') {
702 result->oprs[operand].type |= COLON;
706 /* now convert the exprs returned from evaluate() into operand
709 if (mref) { /* it's a memory reference */
711 int b, i, s; /* basereg, indexreg, scale */
712 int64_t o; /* offset */
714 b = i = -1, o = s = 0;
715 result->oprs[operand].hintbase = hints.base;
716 result->oprs[operand].hinttype = hints.type;
718 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
719 if (e->value == 1) /* in fact it can be basereg */
721 else /* no, it has to be indexreg */
722 i = e->type, s = e->value;
725 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
726 if (b != -1) /* If the first was the base, ... */
727 i = e->type, s = e->value; /* second has to be indexreg */
729 else if (e->value != 1) { /* If both want to be index */
731 "beroset-p-592-invalid effective address");
738 if (e->type != 0) { /* is there an offset? */
739 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
741 "beroset-p-603-invalid effective address");
745 if (e->type == EXPR_UNKNOWN) {
746 o = 0; /* doesn't matter what */
747 result->oprs[operand].wrt = NO_SEG; /* nor this */
748 result->oprs[operand].segment = NO_SEG; /* or this */
750 e++; /* go to the end of the line */
752 if (e->type == EXPR_SIMPLE) {
756 if (e->type == EXPR_WRT) {
757 result->oprs[operand].wrt = e->value;
760 result->oprs[operand].wrt = NO_SEG;
762 * Look for a segment base type.
764 if (e->type && e->type < EXPR_SEGBASE) {
766 "beroset-p-630-invalid effective address");
770 while (e->type && e->value == 0)
772 if (e->type && e->value != 1) {
774 "beroset-p-637-invalid effective address");
779 result->oprs[operand].segment =
780 e->type - EXPR_SEGBASE;
783 result->oprs[operand].segment = NO_SEG;
784 while (e->type && e->value == 0)
788 "beroset-p-650-invalid effective address");
796 result->oprs[operand].wrt = NO_SEG;
797 result->oprs[operand].segment = NO_SEG;
800 if (e->type != 0) { /* there'd better be nothing left! */
802 "beroset-p-663-invalid effective address");
807 /* It is memory, but it can match any r/m operand */
808 result->oprs[operand].type |= MEMORY_ANY;
810 if (b == -1 && (i == -1 || s == 0)) {
811 int is_rel = globalbits == 64 &&
812 !(result->oprs[operand].eaflags & EAF_ABS) &&
814 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
815 (result->oprs[operand].eaflags & EAF_REL));
817 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
819 result->oprs[operand].basereg = b;
820 result->oprs[operand].indexreg = i;
821 result->oprs[operand].scale = s;
822 result->oprs[operand].offset = o;
823 } else { /* it's not a memory reference */
825 if (is_just_unknown(value)) { /* it's immediate but unknown */
826 result->oprs[operand].type |= IMMEDIATE;
827 result->oprs[operand].offset = 0; /* don't care */
828 result->oprs[operand].segment = NO_SEG; /* don't care again */
829 result->oprs[operand].wrt = NO_SEG; /* still don't care */
830 } else if (is_reloc(value)) { /* it's immediate */
831 result->oprs[operand].type |= IMMEDIATE;
832 result->oprs[operand].offset = reloc_value(value);
833 result->oprs[operand].segment = reloc_seg(value);
834 result->oprs[operand].wrt = reloc_wrt(value);
835 if (is_simple(value)) {
836 if (reloc_value(value) == 1)
837 result->oprs[operand].type |= UNITY;
838 if (optimizing >= 0 &&
839 !(result->oprs[operand].type & STRICT)) {
840 int64_t v64 = reloc_value(value);
841 int32_t v32 = (int32_t)v64;
842 int16_t v16 = (int16_t)v32;
844 if (v64 >= -128 && v64 <= 127)
845 result->oprs[operand].type |= SBYTE64;
846 if (v32 >= -128 && v32 <= 127)
847 result->oprs[operand].type |= SBYTE32;
848 if (v16 >= -128 && v16 <= 127)
849 result->oprs[operand].type |= SBYTE16;
852 } else { /* it's a register */
855 if (value->type >= EXPR_SIMPLE || value->value != 1) {
856 error(ERR_NONFATAL, "invalid operand type");
862 * check that its only 1 register, not an expression...
864 for (i = 1; value[i].type; i++)
865 if (value[i].value) {
866 error(ERR_NONFATAL, "invalid operand type");
871 /* clear overrides, except TO which applies to FPU regs */
872 if (result->oprs[operand].type & ~TO) {
874 * we want to produce a warning iff the specified size
875 * is different from the register size
877 rs = result->oprs[operand].type & SIZE_MASK;
881 result->oprs[operand].type &= TO;
882 result->oprs[operand].type |= REGISTER;
883 result->oprs[operand].type |= nasm_reg_flags[value->type];
884 result->oprs[operand].basereg = value->type;
886 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
887 error(ERR_WARNING | ERR_PASS1,
888 "register size specification ignored");
893 result->operands = operand; /* set operand count */
895 /* clear remaining operands */
896 while (operand < MAX_OPERANDS)
897 result->oprs[operand++].type = 0;
900 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
902 switch (result->opcode) {
904 result->opcode = I_RESB;
905 result->oprs[0].offset *= 2;
908 result->opcode = I_RESB;
909 result->oprs[0].offset *= 4;
912 result->opcode = I_RESB;
913 result->oprs[0].offset *= 8;
916 result->opcode = I_RESB;
917 result->oprs[0].offset *= 10;
920 result->opcode = I_RESB;
921 result->oprs[0].offset *= 16;
924 result->opcode = I_RESB;
925 result->oprs[0].offset *= 32;
934 static int is_comma_next(void)
941 i = stdscan(NULL, &tv);
943 return (i == ',' || i == ';' || !i);
946 void cleanup_insn(insn * i)
950 while ((e = i->eops)) {
952 if (e->type == EOT_DB_STRING_FREE)
953 nasm_free(e->stringval);