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
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 bool insn_is_label = false;
179 result->forw_ref = false;
183 stdscan_bufptr = buffer;
184 i = stdscan(NULL, &tokval);
186 result->label = NULL; /* Assume no label */
187 result->eops = NULL; /* must do this, whatever happens */
188 result->operands = 0; /* must initialize this */
190 if (i == 0) { /* blank line - ignore */
191 result->opcode = -1; /* and no instruction either */
194 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
195 (i != TOKEN_REG || (REG_SREG & ~reg_flags[tokval.t_integer]))) {
196 error(ERR_NONFATAL, "label or instruction expected"
197 " at start of line");
202 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
203 /* there's a label here */
205 result->label = tokval.t_charptr;
206 i = stdscan(NULL, &tokval);
207 if (i == ':') { /* skip over the optional colon */
208 i = stdscan(NULL, &tokval);
210 error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
211 "label alone on a line without a colon might be in error");
213 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
215 * FIXME: location->segment could be NO_SEG, in which case
216 * it is possible we should be passing 'abs_seg'. Look into this.
217 * Work out whether that is *really* what we should be doing.
218 * Generally fix things. I think this is right as it is, but
219 * am still not certain.
221 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
222 location->offset, NULL, true, false, outfmt, errfunc);
227 result->opcode = -1; /* this line contains just a label */
231 for (j = 0; j < MAXPREFIX; j++)
232 result->prefixes[j] = P_none;
235 while (i == TOKEN_PREFIX ||
236 (i == TOKEN_REG && !(REG_SREG & ~reg_flags[tokval.t_integer])))
241 * Handle special case: the TIMES prefix.
243 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
246 i = stdscan(NULL, &tokval);
248 evaluate(stdscan, NULL, &tokval, NULL, pass0, error, NULL);
250 if (!value) { /* but, error in evaluator */
251 result->opcode = -1; /* unrecoverable parse error: */
252 return result; /* ignore this instruction */
254 if (!is_simple(value)) {
256 "non-constant argument supplied to TIMES");
259 result->times = value->value;
260 if (value->value < 0) {
261 error(ERR_NONFATAL, "TIMES value %d is negative",
267 int slot = prefix_slot(tokval.t_integer);
268 if (result->prefixes[slot]) {
269 if (result->prefixes[slot] == tokval.t_integer)
271 "instruction has redundant prefixes");
274 "instruction has conflicting prefixes");
276 result->prefixes[slot] = tokval.t_integer;
277 i = stdscan(NULL, &tokval);
281 if (i != TOKEN_INSN) {
285 for (j = 0; j < MAXPREFIX; j++)
286 if ((pfx = result->prefixes[j]) != P_none)
289 if (i == 0 && pfx != P_none) {
291 * Instruction prefixes are present, but no actual
292 * instruction. This is allowed: at this point we
293 * invent a notional instruction of RESB 0.
295 result->opcode = I_RESB;
296 result->operands = 1;
297 result->oprs[0].type = IMMEDIATE;
298 result->oprs[0].offset = 0L;
299 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
302 error(ERR_NONFATAL, "parser: instruction expected");
308 result->opcode = tokval.t_integer;
309 result->condition = tokval.t_inttwo;
312 * RESB, RESW and RESD cannot be satisfied with incorrectly
313 * evaluated operands, since the correct values _must_ be known
314 * on the first pass. Hence, even in pass one, we set the
315 * `critical' flag on calling evaluate(), so that it will bomb
316 * out on undefined symbols. Nasty, but there's nothing we can
319 * For the moment, EQU has the same difficulty, so we'll
322 if (result->opcode == I_RESB || result->opcode == I_RESW ||
323 result->opcode == I_RESD || result->opcode == I_RESQ ||
324 result->opcode == I_REST || result->opcode == I_RESO ||
325 result->opcode == I_EQU || 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_INCBIN) {
335 extop *eop, **tail = &result->eops, **fixptr;
338 result->eops_float = false;
341 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
344 i = stdscan(NULL, &tokval);
347 else if (first && i == ':') {
348 insn_is_label = true;
353 eop = *tail = nasm_malloc(sizeof(extop));
356 eop->type = EOT_NOTHING;
359 if (i == TOKEN_NUM && tokval.t_charptr && is_comma_next()) {
360 eop->type = EOT_DB_STRING;
361 eop->stringval = tokval.t_charptr;
362 eop->stringlen = tokval.t_inttwo;
363 i = stdscan(NULL, &tokval); /* eat the comma */
367 if ((i == TOKEN_FLOAT && is_comma_next())
368 || i == '-' || i == '+') {
371 if (i == '+' || i == '-') {
372 char *save = stdscan_bufptr;
374 sign = (i == '-') ? -1 : 1;
375 i = stdscan(NULL, &tokval);
376 if (i != TOKEN_FLOAT || !is_comma_next()) {
377 stdscan_bufptr = save;
378 i = tokval.t_type = token;
382 if (i == TOKEN_FLOAT) {
383 eop->type = EOT_DB_STRING;
384 result->eops_float = true;
385 switch (result->opcode) {
405 error(ERR_NONFATAL, "floating-point constant"
406 " encountered in unknown instruction");
408 * fix suggested by Pedro Gimeno... original line
410 * eop->type = EOT_NOTHING;
415 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
418 eop->stringval = (char *)eop + sizeof(extop);
419 if (!eop->stringlen ||
420 !float_const(tokval.t_charptr, sign,
421 (uint8_t *)eop->stringval,
422 eop->stringlen, error))
423 eop->type = EOT_NOTHING;
424 i = stdscan(NULL, &tokval); /* eat the comma */
432 value = evaluate(stdscan, NULL, &tokval, NULL,
433 critical, error, NULL);
435 if (!value) { /* error in evaluator */
436 result->opcode = -1; /* unrecoverable parse error: */
437 return result; /* ignore this instruction */
439 if (is_unknown(value)) {
440 eop->type = EOT_DB_NUMBER;
441 eop->offset = 0; /* doesn't matter what we put */
442 eop->segment = eop->wrt = NO_SEG; /* likewise */
443 } else if (is_reloc(value)) {
444 eop->type = EOT_DB_NUMBER;
445 eop->offset = reloc_value(value);
446 eop->segment = reloc_seg(value);
447 eop->wrt = reloc_wrt(value);
450 "operand %d: expression is not simple"
451 " or relocatable", oper_num);
456 * We're about to call stdscan(), which will eat the
457 * comma that we're currently sitting on between
458 * arguments. However, we'd better check first that it
461 if (i == 0) /* also could be EOL */
464 error(ERR_NONFATAL, "comma expected after operand %d",
466 result->opcode = -1; /* unrecoverable parse error: */
467 return result; /* ignore this instruction */
471 if (result->opcode == I_INCBIN) {
473 * Correct syntax for INCBIN is that there should be
474 * one string operand, followed by one or two numeric
477 if (!result->eops || result->eops->type != EOT_DB_STRING)
478 error(ERR_NONFATAL, "`incbin' expects a file name");
479 else if (result->eops->next &&
480 result->eops->next->type != EOT_DB_NUMBER)
481 error(ERR_NONFATAL, "`incbin': second parameter is",
483 else if (result->eops->next && result->eops->next->next &&
484 result->eops->next->next->type != EOT_DB_NUMBER)
485 error(ERR_NONFATAL, "`incbin': third parameter is",
487 else if (result->eops->next && result->eops->next->next &&
488 result->eops->next->next->next)
490 "`incbin': more than three parameters");
494 * If we reach here, one of the above errors happened.
495 * Throw the instruction away.
499 } else /* DB ... */ if (oper_num == 0)
500 error(ERR_WARNING | ERR_PASS1,
501 "no operand for data declaration");
503 result->operands = oper_num;
508 /* right. Now we begin to parse the operands. There may be up to four
509 * of these, separated by commas, and terminated by a zero token. */
511 for (operand = 0; operand < MAX_OPERANDS; operand++) {
512 expr *value; /* used most of the time */
513 int mref; /* is this going to be a memory ref? */
514 int bracket; /* is it a [] mref, or a & mref? */
517 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
518 result->oprs[operand].eaflags = 0; /* and this */
519 result->oprs[operand].opflags = 0;
521 i = stdscan(NULL, &tokval);
523 break; /* end of operands: get out of here */
524 else if (first && i == ':') {
525 insn_is_label = true;
529 result->oprs[operand].type = 0; /* so far, no override */
530 while (i == TOKEN_SPECIAL) { /* size specifiers */
531 switch ((int)tokval.t_integer) {
533 if (!setsize) /* we want to use only the first */
534 result->oprs[operand].type |= BITS8;
539 result->oprs[operand].type |= BITS16;
545 result->oprs[operand].type |= BITS32;
550 result->oprs[operand].type |= BITS64;
555 result->oprs[operand].type |= BITS80;
560 result->oprs[operand].type |= BITS128;
564 result->oprs[operand].type |= TO;
567 result->oprs[operand].type |= STRICT;
570 result->oprs[operand].type |= FAR;
573 result->oprs[operand].type |= NEAR;
576 result->oprs[operand].type |= SHORT;
579 error(ERR_NONFATAL, "invalid operand size specification");
581 i = stdscan(NULL, &tokval);
584 if (i == '[' || i == '&') { /* memory reference */
586 bracket = (i == '[');
587 i = stdscan(NULL, &tokval); /* then skip the colon */
588 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
589 process_size_override(result, operand);
590 i = stdscan(NULL, &tokval);
592 } else { /* immediate operand, or register */
594 bracket = false; /* placate optimisers */
597 if ((result->oprs[operand].type & FAR) && !mref &&
598 result->opcode != I_JMP && result->opcode != I_CALL) {
599 error(ERR_NONFATAL, "invalid use of FAR operand specifier");
602 value = evaluate(stdscan, NULL, &tokval,
603 &result->oprs[operand].opflags,
604 critical, error, &hints);
606 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
607 result->forw_ref = true;
609 if (!value) { /* error in evaluator */
610 result->opcode = -1; /* unrecoverable parse error: */
611 return result; /* ignore this instruction */
613 if (i == ':' && mref) { /* it was seg:offset */
615 * Process the segment override.
617 if (value[1].type != 0 || value->value != 1 ||
618 REG_SREG & ~reg_flags[value->type])
619 error(ERR_NONFATAL, "invalid segment override");
620 else if (result->prefixes[PPS_SEG])
622 "instruction has conflicting segment overrides");
624 result->prefixes[PPS_SEG] = value->type;
625 if (!(REG_FSGS & ~reg_flags[value->type]))
626 result->oprs[operand].eaflags |= EAF_FSGS;
629 i = stdscan(NULL, &tokval); /* then skip the colon */
630 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
631 process_size_override(result, operand);
632 i = stdscan(NULL, &tokval);
634 value = evaluate(stdscan, NULL, &tokval,
635 &result->oprs[operand].opflags,
636 critical, error, &hints);
638 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
639 result->forw_ref = true;
641 /* and get the offset */
642 if (!value) { /* but, error in evaluator */
643 result->opcode = -1; /* unrecoverable parse error: */
644 return result; /* ignore this instruction */
647 if (mref && bracket) { /* find ] at the end */
649 error(ERR_NONFATAL, "parser: expecting ]");
650 do { /* error recovery again */
651 i = stdscan(NULL, &tokval);
652 } while (i != 0 && i != ',');
653 } else /* we got the required ] */
654 i = stdscan(NULL, &tokval);
655 } else { /* immediate operand */
656 if (i != 0 && i != ',' && i != ':') {
657 error(ERR_NONFATAL, "comma or end of line expected");
658 do { /* error recovery */
659 i = stdscan(NULL, &tokval);
660 } while (i != 0 && i != ',');
661 } else if (i == ':') {
662 result->oprs[operand].type |= COLON;
666 /* now convert the exprs returned from evaluate() into operand
669 if (mref) { /* it's a memory reference */
671 int b, i, s; /* basereg, indexreg, scale */
672 int64_t o; /* offset */
674 b = i = -1, o = s = 0;
675 result->oprs[operand].hintbase = hints.base;
676 result->oprs[operand].hinttype = hints.type;
678 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
679 if (e->value == 1) /* in fact it can be basereg */
681 else /* no, it has to be indexreg */
682 i = e->type, s = e->value;
685 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
686 if (b != -1) /* If the first was the base, ... */
687 i = e->type, s = e->value; /* second has to be indexreg */
689 else if (e->value != 1) { /* If both want to be index */
691 "beroset-p-592-invalid effective address");
698 if (e->type != 0) { /* is there an offset? */
699 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
701 "beroset-p-603-invalid effective address");
705 if (e->type == EXPR_UNKNOWN) {
706 o = 0; /* doesn't matter what */
707 result->oprs[operand].wrt = NO_SEG; /* nor this */
708 result->oprs[operand].segment = NO_SEG; /* or this */
710 e++; /* go to the end of the line */
712 if (e->type == EXPR_SIMPLE) {
716 if (e->type == EXPR_WRT) {
717 result->oprs[operand].wrt = e->value;
720 result->oprs[operand].wrt = NO_SEG;
722 * Look for a segment base type.
724 if (e->type && e->type < EXPR_SEGBASE) {
726 "beroset-p-630-invalid effective address");
730 while (e->type && e->value == 0)
732 if (e->type && e->value != 1) {
734 "beroset-p-637-invalid effective address");
739 result->oprs[operand].segment =
740 e->type - EXPR_SEGBASE;
743 result->oprs[operand].segment = NO_SEG;
744 while (e->type && e->value == 0)
748 "beroset-p-650-invalid effective address");
756 result->oprs[operand].wrt = NO_SEG;
757 result->oprs[operand].segment = NO_SEG;
760 if (e->type != 0) { /* there'd better be nothing left! */
762 "beroset-p-663-invalid effective address");
767 /* It is memory, but it can match any r/m operand */
768 result->oprs[operand].type |= MEMORY_ANY;
770 if (b == -1 && (i == -1 || s == 0)) {
771 int is_rel = globalbits == 64 &&
772 !(result->oprs[operand].eaflags & EAF_ABS) &&
774 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
775 (result->oprs[operand].eaflags & EAF_REL));
777 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
779 result->oprs[operand].basereg = b;
780 result->oprs[operand].indexreg = i;
781 result->oprs[operand].scale = s;
782 result->oprs[operand].offset = o;
783 } else { /* it's not a memory reference */
785 if (is_just_unknown(value)) { /* it's immediate but unknown */
786 result->oprs[operand].type |= IMMEDIATE;
787 result->oprs[operand].offset = 0; /* don't care */
788 result->oprs[operand].segment = NO_SEG; /* don't care again */
789 result->oprs[operand].wrt = NO_SEG; /* still don't care */
790 } else if (is_reloc(value)) { /* it's immediate */
791 result->oprs[operand].type |= IMMEDIATE;
792 result->oprs[operand].offset = reloc_value(value);
793 result->oprs[operand].segment = reloc_seg(value);
794 result->oprs[operand].wrt = reloc_wrt(value);
795 if (is_simple(value)) {
796 if (reloc_value(value) == 1)
797 result->oprs[operand].type |= UNITY;
798 if (optimizing >= 0 &&
799 !(result->oprs[operand].type & STRICT)) {
800 if (reloc_value(value) >= -128 &&
801 reloc_value(value) <= 127)
802 result->oprs[operand].type |= SBYTE;
805 } else { /* it's a register */
808 if (value->type >= EXPR_SIMPLE || value->value != 1) {
809 error(ERR_NONFATAL, "invalid operand type");
815 * check that its only 1 register, not an expression...
817 for (i = 1; value[i].type; i++)
818 if (value[i].value) {
819 error(ERR_NONFATAL, "invalid operand type");
824 /* clear overrides, except TO which applies to FPU regs */
825 if (result->oprs[operand].type & ~TO) {
827 * we want to produce a warning iff the specified size
828 * is different from the register size
830 rs = result->oprs[operand].type & SIZE_MASK;
834 result->oprs[operand].type &= TO;
835 result->oprs[operand].type |= REGISTER;
836 result->oprs[operand].type |= reg_flags[value->type];
837 result->oprs[operand].basereg = value->type;
839 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
840 error(ERR_WARNING | ERR_PASS1,
841 "register size specification ignored");
846 result->operands = operand; /* set operand count */
848 /* clear remaining operands */
849 while (operand < MAX_OPERANDS)
850 result->oprs[operand++].type = 0;
853 * Transform RESW, RESD, RESQ, REST, RESO into RESB.
855 switch (result->opcode) {
857 result->opcode = I_RESB;
858 result->oprs[0].offset *= 2;
861 result->opcode = I_RESB;
862 result->oprs[0].offset *= 4;
865 result->opcode = I_RESB;
866 result->oprs[0].offset *= 8;
869 result->opcode = I_RESB;
870 result->oprs[0].offset *= 10;
873 result->opcode = I_RESB;
874 result->oprs[0].offset *= 16;
883 static int is_comma_next(void)
890 i = stdscan(NULL, &tv);
892 return (i == ',' || i == ';' || !i);
895 void cleanup_insn(insn * i)
901 i->eops = i->eops->next;