nasmlib/file: move memory-mapping functions out of file.c
[nasm.git] / asm / parser.c
blob5f87c463d34f321933dab141a59d17e310810360
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * parser.c source line parser for the Netwide Assembler
38 #include "compiler.h"
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <stddef.h>
43 #include <string.h>
44 #include <ctype.h>
46 #include "nasm.h"
47 #include "insns.h"
48 #include "nasmlib.h"
49 #include "stdscan.h"
50 #include "eval.h"
51 #include "parser.h"
52 #include "float.h"
53 #include "tables.h"
55 extern int in_abs_seg; /* ABSOLUTE segment flag */
56 extern int32_t abs_seg; /* ABSOLUTE segment */
57 extern int32_t abs_offset; /* ABSOLUTE segment offset */
59 static int is_comma_next(void);
61 static struct tokenval tokval;
63 static int prefix_slot(int prefix)
65 switch (prefix) {
66 case P_WAIT:
67 return PPS_WAIT;
68 case R_CS:
69 case R_DS:
70 case R_SS:
71 case R_ES:
72 case R_FS:
73 case R_GS:
74 return PPS_SEG;
75 case P_LOCK:
76 return PPS_LOCK;
77 case P_REP:
78 case P_REPE:
79 case P_REPZ:
80 case P_REPNE:
81 case P_REPNZ:
82 case P_XACQUIRE:
83 case P_XRELEASE:
84 case P_BND:
85 case P_NOBND:
86 return PPS_REP;
87 case P_O16:
88 case P_O32:
89 case P_O64:
90 case P_OSP:
91 return PPS_OSIZE;
92 case P_A16:
93 case P_A32:
94 case P_A64:
95 case P_ASP:
96 return PPS_ASIZE;
97 case P_EVEX:
98 case P_VEX3:
99 case P_VEX2:
100 return PPS_VEX;
101 default:
102 nasm_panic(0, "Invalid value %d passed to prefix_slot()", prefix);
103 return -1;
107 static void process_size_override(insn *result, operand *op)
109 if (tasm_compatible_mode) {
110 switch ((int)tokval.t_integer) {
111 /* For TASM compatibility a size override inside the
112 * brackets changes the size of the operand, not the
113 * address type of the operand as it does in standard
114 * NASM syntax. Hence:
116 * mov eax,[DWORD val]
118 * is valid syntax in TASM compatibility mode. Note that
119 * you lose the ability to override the default address
120 * type for the instruction, but we never use anything
121 * but 32-bit flat model addressing in our code.
123 case S_BYTE:
124 op->type |= BITS8;
125 break;
126 case S_WORD:
127 op->type |= BITS16;
128 break;
129 case S_DWORD:
130 case S_LONG:
131 op->type |= BITS32;
132 break;
133 case S_QWORD:
134 op->type |= BITS64;
135 break;
136 case S_TWORD:
137 op->type |= BITS80;
138 break;
139 case S_OWORD:
140 op->type |= BITS128;
141 break;
142 default:
143 nasm_error(ERR_NONFATAL,
144 "invalid operand size specification");
145 break;
147 } else {
148 /* Standard NASM compatible syntax */
149 switch ((int)tokval.t_integer) {
150 case S_NOSPLIT:
151 op->eaflags |= EAF_TIMESTWO;
152 break;
153 case S_REL:
154 op->eaflags |= EAF_REL;
155 break;
156 case S_ABS:
157 op->eaflags |= EAF_ABS;
158 break;
159 case S_BYTE:
160 op->disp_size = 8;
161 op->eaflags |= EAF_BYTEOFFS;
162 break;
163 case P_A16:
164 case P_A32:
165 case P_A64:
166 if (result->prefixes[PPS_ASIZE] &&
167 result->prefixes[PPS_ASIZE] != tokval.t_integer)
168 nasm_error(ERR_NONFATAL,
169 "conflicting address size specifications");
170 else
171 result->prefixes[PPS_ASIZE] = tokval.t_integer;
172 break;
173 case S_WORD:
174 op->disp_size = 16;
175 op->eaflags |= EAF_WORDOFFS;
176 break;
177 case S_DWORD:
178 case S_LONG:
179 op->disp_size = 32;
180 op->eaflags |= EAF_WORDOFFS;
181 break;
182 case S_QWORD:
183 op->disp_size = 64;
184 op->eaflags |= EAF_WORDOFFS;
185 break;
186 default:
187 nasm_error(ERR_NONFATAL, "invalid size specification in"
188 " effective address");
189 break;
195 * when two or more decorators follow a register operand,
196 * consecutive decorators are parsed here.
197 * opmask and zeroing decorators can be placed in any order.
198 * e.g. zmm1 {k2}{z} or zmm2 {z}{k3}
199 * decorator(s) are placed at the end of an operand.
201 static bool parse_braces(decoflags_t *decoflags)
203 int i;
204 bool recover = false;
206 i = tokval.t_type;
207 do {
208 if (i == TOKEN_OPMASK) {
209 if (*decoflags & OPMASK_MASK) {
210 nasm_error(ERR_NONFATAL, "opmask k%"PRIu64" is already set",
211 *decoflags & OPMASK_MASK);
212 *decoflags &= ~OPMASK_MASK;
214 *decoflags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
215 } else if (i == TOKEN_DECORATOR) {
216 switch (tokval.t_integer) {
217 case BRC_Z:
219 * according to AVX512 spec, only zeroing/merging decorator
220 * is supported with opmask
222 *decoflags |= GEN_Z(0);
223 break;
224 default:
225 nasm_error(ERR_NONFATAL, "{%s} is not an expected decorator",
226 tokval.t_charptr);
227 break;
229 } else if (i == ',' || i == TOKEN_EOS){
230 break;
231 } else {
232 nasm_error(ERR_NONFATAL, "only a series of valid decorators"
233 " expected");
234 recover = true;
235 break;
237 i = stdscan(NULL, &tokval);
238 } while(1);
240 return recover;
243 static int parse_mref(operand *op, const expr *e)
245 int b, i, s; /* basereg, indexreg, scale */
246 int64_t o; /* offset */
248 b = i = -1;
249 o = s = 0;
250 op->segment = op->wrt = NO_SEG;
252 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
253 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
255 if (is_gpr && e->value == 1)
256 b = e->type; /* It can be basereg */
257 else /* No, it has to be indexreg */
258 i = e->type, s = e->value;
259 e++;
261 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
262 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
264 if (b != -1) /* If the first was the base, ... */
265 i = e->type, s = e->value; /* second has to be indexreg */
267 else if (!is_gpr || e->value != 1) {
268 /* If both want to be index */
269 nasm_error(ERR_NONFATAL,
270 "invalid effective address: two index registers");
271 return -1;
272 } else
273 b = e->type;
274 e++;
277 if (e->type) { /* is there an offset? */
278 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
279 nasm_error(ERR_NONFATAL,
280 "invalid effective address: impossible register");
281 return -1;
282 } else {
283 if (e->type == EXPR_UNKNOWN) {
284 op->opflags |= OPFLAG_UNKNOWN;
285 o = 0; /* doesn't matter what */
286 while (e->type)
287 e++; /* go to the end of the line */
288 } else {
289 if (e->type == EXPR_SIMPLE) {
290 o = e->value;
291 e++;
293 if (e->type == EXPR_WRT) {
294 op->wrt = e->value;
295 e++;
298 * Look for a segment base type.
300 for (; e->type; e++) {
301 if (!e->value)
302 continue;
304 if (e->type <= EXPR_REG_END) {
305 nasm_error(ERR_NONFATAL,
306 "invalid effective address: too many registers");
307 return -1;
308 } else if (e->type < EXPR_SEGBASE) {
309 nasm_error(ERR_NONFATAL,
310 "invalid effective address: bad subexpression type");
311 return -1;
312 } else if (e->value == 1) {
313 if (op->segment != NO_SEG) {
314 nasm_error(ERR_NONFATAL,
315 "invalid effective address: multiple base segments");
316 return -1;
318 op->segment = e->type - EXPR_SEGBASE;
319 } else if (e->value == -1 &&
320 e->type == location.segment + EXPR_SEGBASE &&
321 !(op->opflags & OPFLAG_RELATIVE)) {
322 op->opflags |= OPFLAG_RELATIVE;
323 } else {
324 nasm_error(ERR_NONFATAL,
325 "invalid effective address: impossible segment base multiplier");
326 return -1;
333 nasm_assert(!e->type); /* We should be at the end */
335 op->basereg = b;
336 op->indexreg = i;
337 op->scale = s;
338 op->offset = o;
339 return 0;
342 static void mref_set_optype(operand *op)
344 int b = op->basereg;
345 int i = op->indexreg;
346 int s = op->scale;
348 /* It is memory, but it can match any r/m operand */
349 op->type |= MEMORY_ANY;
351 if (b == -1 && (i == -1 || s == 0)) {
352 int is_rel = globalbits == 64 &&
353 !(op->eaflags & EAF_ABS) &&
354 ((globalrel &&
355 !(op->eaflags & EAF_FSGS)) ||
356 (op->eaflags & EAF_REL));
358 op->type |= is_rel ? IP_REL : MEM_OFFS;
361 if (i != -1) {
362 opflags_t iclass = nasm_reg_flags[i];
364 if (is_class(XMMREG,iclass))
365 op->type |= XMEM;
366 else if (is_class(YMMREG,iclass))
367 op->type |= YMEM;
368 else if (is_class(ZMMREG,iclass))
369 op->type |= ZMEM;
374 * Convert an expression vector returned from evaluate() into an
375 * extop structure. Return zero on success.
377 static int value_to_extop(expr * vect, extop *eop, int32_t myseg)
379 eop->type = EOT_DB_NUMBER;
380 eop->offset = 0;
381 eop->segment = eop->wrt = NO_SEG;
382 eop->relative = false;
384 for (; vect->type; vect++) {
385 if (!vect->value) /* zero term, safe to ignore */
386 continue;
388 if (vect->type <= EXPR_REG_END) /* false if a register is present */
389 return -1;
391 if (vect->type == EXPR_UNKNOWN) /* something we can't resolve yet */
392 return 0;
394 if (vect->type == EXPR_SIMPLE) {
395 /* Simple number expression */
396 eop->offset += vect->value;
397 continue;
399 if (eop->wrt == NO_SEG && !eop->relative && vect->type == EXPR_WRT) {
400 /* WRT term */
401 eop->wrt = vect->value;
402 continue;
405 if (!eop->relative &&
406 vect->type == EXPR_SEGBASE + myseg && vect->value == -1) {
407 /* Expression of the form: foo - $ */
408 eop->relative = true;
409 continue;
412 if (eop->segment == NO_SEG && vect->type >= EXPR_SEGBASE &&
413 vect->value == 1) {
414 eop->segment = vect->type - EXPR_SEGBASE;
415 continue;
418 /* Otherwise, badness */
419 return -1;
422 /* We got to the end and it was all okay */
423 return 0;
426 insn *parse_line(int pass, char *buffer, insn *result, ldfunc ldef)
428 bool insn_is_label = false;
429 struct eval_hints hints;
430 int opnum;
431 int critical;
432 bool first;
433 bool recover;
434 int i;
436 restart_parse:
437 first = true;
438 result->forw_ref = false;
440 stdscan_reset();
441 stdscan_set(buffer);
442 i = stdscan(NULL, &tokval);
444 result->label = NULL; /* Assume no label */
445 result->eops = NULL; /* must do this, whatever happens */
446 result->operands = 0; /* must initialize this */
447 result->evex_rm = 0; /* Ensure EVEX rounding mode is reset */
448 result->evex_brerop = -1; /* Reset EVEX broadcasting/ER op position */
450 /* Ignore blank lines */
451 if (i == TOKEN_EOS)
452 goto fail;
454 if (i != TOKEN_ID &&
455 i != TOKEN_INSN &&
456 i != TOKEN_PREFIX &&
457 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
458 nasm_error(ERR_NONFATAL,
459 "label or instruction expected at start of line");
460 goto fail;
463 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
464 /* there's a label here */
465 first = false;
466 result->label = tokval.t_charptr;
467 i = stdscan(NULL, &tokval);
468 if (i == ':') { /* skip over the optional colon */
469 i = stdscan(NULL, &tokval);
470 } else if (i == 0) {
471 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
472 "label alone on a line without a colon might be in error");
474 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
476 * FIXME: location.segment could be NO_SEG, in which case
477 * it is possible we should be passing 'abs_seg'. Look into this.
478 * Work out whether that is *really* what we should be doing.
479 * Generally fix things. I think this is right as it is, but
480 * am still not certain.
482 ldef(result->label, in_abs_seg ? abs_seg : location.segment,
483 location.offset, NULL, true, false);
487 /* Just a label here */
488 if (i == TOKEN_EOS)
489 goto fail;
491 nasm_static_assert(P_none == 0);
492 memset(result->prefixes, P_none, sizeof(result->prefixes));
493 result->times = 1L;
495 while (i == TOKEN_PREFIX ||
496 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
497 first = false;
500 * Handle special case: the TIMES prefix.
502 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
503 expr *value;
505 i = stdscan(NULL, &tokval);
506 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, NULL);
507 i = tokval.t_type;
508 if (!value) /* Error in evaluator */
509 goto fail;
510 if (!is_simple(value)) {
511 nasm_error(ERR_NONFATAL,
512 "non-constant argument supplied to TIMES");
513 result->times = 1L;
514 } else {
515 result->times = value->value;
516 if (value->value < 0 && pass0 == 2) {
517 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
518 value->value);
519 result->times = 0;
522 } else {
523 int slot = prefix_slot(tokval.t_integer);
524 if (result->prefixes[slot]) {
525 if (result->prefixes[slot] == tokval.t_integer)
526 nasm_error(ERR_WARNING | ERR_PASS1,
527 "instruction has redundant prefixes");
528 else
529 nasm_error(ERR_NONFATAL,
530 "instruction has conflicting prefixes");
532 result->prefixes[slot] = tokval.t_integer;
533 i = stdscan(NULL, &tokval);
537 if (i != TOKEN_INSN) {
538 int j;
539 enum prefixes pfx;
541 for (j = 0; j < MAXPREFIX; j++) {
542 if ((pfx = result->prefixes[j]) != P_none)
543 break;
546 if (i == 0 && pfx != P_none) {
548 * Instruction prefixes are present, but no actual
549 * instruction. This is allowed: at this point we
550 * invent a notional instruction of RESB 0.
552 result->opcode = I_RESB;
553 result->operands = 1;
554 result->oprs[0].type = IMMEDIATE;
555 result->oprs[0].offset = 0L;
556 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
557 return result;
558 } else {
559 nasm_error(ERR_NONFATAL, "parser: instruction expected");
560 goto fail;
564 result->opcode = tokval.t_integer;
565 result->condition = tokval.t_inttwo;
568 * INCBIN cannot be satisfied with incorrectly
569 * evaluated operands, since the correct values _must_ be known
570 * on the first pass. Hence, even in pass one, we set the
571 * `critical' flag on calling evaluate(), so that it will bomb
572 * out on undefined symbols.
574 if (result->opcode == I_INCBIN) {
575 critical = (pass0 < 2 ? 1 : 2);
577 } else
578 critical = (pass == 2 ? 2 : 0);
580 if (result->opcode == I_DB || result->opcode == I_DW ||
581 result->opcode == I_DD || result->opcode == I_DQ ||
582 result->opcode == I_DT || result->opcode == I_DO ||
583 result->opcode == I_DY || result->opcode == I_DZ ||
584 result->opcode == I_INCBIN) {
585 extop *eop, **tail = &result->eops, **fixptr;
586 int oper_num = 0;
587 int32_t sign;
589 result->eops_float = false;
592 * Begin to read the DB/DW/DD/DQ/DT/DO/DY/DZ/INCBIN operands.
594 while (1) {
595 i = stdscan(NULL, &tokval);
596 if (i == TOKEN_EOS)
597 break;
598 else if (first && i == ':') {
599 insn_is_label = true;
600 goto restart_parse;
602 first = false;
603 fixptr = tail;
604 eop = *tail = nasm_malloc(sizeof(extop));
605 tail = &eop->next;
606 eop->next = NULL;
607 eop->type = EOT_NOTHING;
608 oper_num++;
609 sign = +1;
612 * is_comma_next() here is to distinguish this from
613 * a string used as part of an expression...
615 if (i == TOKEN_STR && is_comma_next()) {
616 eop->type = EOT_DB_STRING;
617 eop->stringval = tokval.t_charptr;
618 eop->stringlen = tokval.t_inttwo;
619 i = stdscan(NULL, &tokval); /* eat the comma */
620 } else if (i == TOKEN_STRFUNC) {
621 bool parens = false;
622 const char *funcname = tokval.t_charptr;
623 enum strfunc func = tokval.t_integer;
624 i = stdscan(NULL, &tokval);
625 if (i == '(') {
626 parens = true;
627 i = stdscan(NULL, &tokval);
629 if (i != TOKEN_STR) {
630 nasm_error(ERR_NONFATAL,
631 "%s must be followed by a string constant",
632 funcname);
633 eop->type = EOT_NOTHING;
634 } else {
635 eop->type = EOT_DB_STRING_FREE;
636 eop->stringlen =
637 string_transform(tokval.t_charptr, tokval.t_inttwo,
638 &eop->stringval, func);
639 if (eop->stringlen == (size_t)-1) {
640 nasm_error(ERR_NONFATAL, "invalid string for transform");
641 eop->type = EOT_NOTHING;
644 if (parens && i && i != ')') {
645 i = stdscan(NULL, &tokval);
646 if (i != ')') {
647 nasm_error(ERR_NONFATAL, "unterminated %s function",
648 funcname);
651 if (i && i != ',')
652 i = stdscan(NULL, &tokval);
653 } else if (i == '-' || i == '+') {
654 char *save = stdscan_get();
655 int token = i;
656 sign = (i == '-') ? -1 : 1;
657 i = stdscan(NULL, &tokval);
658 if (i != TOKEN_FLOAT) {
659 stdscan_set(save);
660 i = tokval.t_type = token;
661 goto is_expression;
662 } else {
663 goto is_float;
665 } else if (i == TOKEN_FLOAT) {
666 is_float:
667 eop->type = EOT_DB_STRING;
668 result->eops_float = true;
670 eop->stringlen = idata_bytes(result->opcode);
671 if (eop->stringlen > 16) {
672 nasm_error(ERR_NONFATAL, "floating-point constant"
673 " encountered in DY or DZ instruction");
674 eop->stringlen = 0;
675 } else if (eop->stringlen < 1) {
676 nasm_error(ERR_NONFATAL, "floating-point constant"
677 " encountered in unknown instruction");
679 * fix suggested by Pedro Gimeno... original line was:
680 * eop->type = EOT_NOTHING;
682 eop->stringlen = 0;
685 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
686 tail = &eop->next;
687 *fixptr = eop;
688 eop->stringval = (char *)eop + sizeof(extop);
689 if (!eop->stringlen ||
690 !float_const(tokval.t_charptr, sign,
691 (uint8_t *)eop->stringval, eop->stringlen))
692 eop->type = EOT_NOTHING;
693 i = stdscan(NULL, &tokval); /* eat the comma */
694 } else {
695 /* anything else, assume it is an expression */
696 expr *value;
698 is_expression:
699 value = evaluate(stdscan, NULL, &tokval, NULL,
700 critical, NULL);
701 i = tokval.t_type;
702 if (!value) /* Error in evaluator */
703 goto fail;
704 if (value_to_extop(value, eop, location.segment)) {
705 nasm_error(ERR_NONFATAL,
706 "operand %d: expression is not simple or relocatable",
707 oper_num);
712 * We're about to call stdscan(), which will eat the
713 * comma that we're currently sitting on between
714 * arguments. However, we'd better check first that it
715 * _is_ a comma.
717 if (i == TOKEN_EOS) /* also could be EOL */
718 break;
719 if (i != ',') {
720 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
721 oper_num);
722 goto fail;
726 if (result->opcode == I_INCBIN) {
728 * Correct syntax for INCBIN is that there should be
729 * one string operand, followed by one or two numeric
730 * operands.
732 if (!result->eops || result->eops->type != EOT_DB_STRING)
733 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
734 else if (result->eops->next &&
735 result->eops->next->type != EOT_DB_NUMBER)
736 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
737 " non-numeric");
738 else if (result->eops->next && result->eops->next->next &&
739 result->eops->next->next->type != EOT_DB_NUMBER)
740 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
741 " non-numeric");
742 else if (result->eops->next && result->eops->next->next &&
743 result->eops->next->next->next)
744 nasm_error(ERR_NONFATAL,
745 "`incbin': more than three parameters");
746 else
747 return result;
749 * If we reach here, one of the above errors happened.
750 * Throw the instruction away.
752 goto fail;
753 } else /* DB ... */ if (oper_num == 0)
754 nasm_error(ERR_WARNING | ERR_PASS1,
755 "no operand for data declaration");
756 else
757 result->operands = oper_num;
759 return result;
763 * Now we begin to parse the operands. There may be up to four
764 * of these, separated by commas, and terminated by a zero token.
767 for (opnum = 0; opnum < MAX_OPERANDS; opnum++) {
768 operand *op = &result->oprs[opnum];
769 expr *value; /* used most of the time */
770 bool mref; /* is this going to be a memory ref? */
771 bool bracket; /* is it a [] mref, or a & mref? */
772 bool mib; /* compound (mib) mref? */
773 int setsize = 0;
774 decoflags_t brace_flags = 0; /* flags for decorators in braces */
776 op->disp_size = 0; /* have to zero this whatever */
777 op->eaflags = 0; /* and this */
778 op->opflags = 0;
779 op->decoflags = 0;
781 i = stdscan(NULL, &tokval);
782 if (i == TOKEN_EOS)
783 break; /* end of operands: get out of here */
784 else if (first && i == ':') {
785 insn_is_label = true;
786 goto restart_parse;
788 first = false;
789 op->type = 0; /* so far, no override */
790 while (i == TOKEN_SPECIAL) { /* size specifiers */
791 switch ((int)tokval.t_integer) {
792 case S_BYTE:
793 if (!setsize) /* we want to use only the first */
794 op->type |= BITS8;
795 setsize = 1;
796 break;
797 case S_WORD:
798 if (!setsize)
799 op->type |= BITS16;
800 setsize = 1;
801 break;
802 case S_DWORD:
803 case S_LONG:
804 if (!setsize)
805 op->type |= BITS32;
806 setsize = 1;
807 break;
808 case S_QWORD:
809 if (!setsize)
810 op->type |= BITS64;
811 setsize = 1;
812 break;
813 case S_TWORD:
814 if (!setsize)
815 op->type |= BITS80;
816 setsize = 1;
817 break;
818 case S_OWORD:
819 if (!setsize)
820 op->type |= BITS128;
821 setsize = 1;
822 break;
823 case S_YWORD:
824 if (!setsize)
825 op->type |= BITS256;
826 setsize = 1;
827 break;
828 case S_ZWORD:
829 if (!setsize)
830 op->type |= BITS512;
831 setsize = 1;
832 break;
833 case S_TO:
834 op->type |= TO;
835 break;
836 case S_STRICT:
837 op->type |= STRICT;
838 break;
839 case S_FAR:
840 op->type |= FAR;
841 break;
842 case S_NEAR:
843 op->type |= NEAR;
844 break;
845 case S_SHORT:
846 op->type |= SHORT;
847 break;
848 default:
849 nasm_error(ERR_NONFATAL, "invalid operand size specification");
851 i = stdscan(NULL, &tokval);
854 if (i == '[' || i == '&') { /* memory reference */
855 mref = true;
856 bracket = (i == '[');
857 i = stdscan(NULL, &tokval); /* then skip the colon */
858 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
859 process_size_override(result, op);
860 i = stdscan(NULL, &tokval);
862 /* when a comma follows an opening bracket - [ , eax*4] */
863 if (i == ',') {
864 /* treat as if there is a zero displacement virtually */
865 tokval.t_type = TOKEN_NUM;
866 tokval.t_integer = 0;
867 stdscan_set(stdscan_get() - 1); /* rewind the comma */
869 } else { /* immediate operand, or register */
870 mref = false;
871 bracket = false; /* placate optimisers */
874 if ((op->type & FAR) && !mref &&
875 result->opcode != I_JMP && result->opcode != I_CALL) {
876 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
879 value = evaluate(stdscan, NULL, &tokval,
880 &op->opflags, critical, &hints);
881 i = tokval.t_type;
882 if (op->opflags & OPFLAG_FORWARD) {
883 result->forw_ref = true;
885 if (!value) /* Error in evaluator */
886 goto fail;
887 if (i == ':' && mref) { /* it was seg:offset */
889 * Process the segment override.
891 if (value[1].type != 0 ||
892 value->value != 1 ||
893 !IS_SREG(value->type))
894 nasm_error(ERR_NONFATAL, "invalid segment override");
895 else if (result->prefixes[PPS_SEG])
896 nasm_error(ERR_NONFATAL,
897 "instruction has conflicting segment overrides");
898 else {
899 result->prefixes[PPS_SEG] = value->type;
900 if (IS_FSGS(value->type))
901 op->eaflags |= EAF_FSGS;
904 i = stdscan(NULL, &tokval); /* then skip the colon */
905 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
906 process_size_override(result, op);
907 i = stdscan(NULL, &tokval);
909 value = evaluate(stdscan, NULL, &tokval,
910 &op->opflags, critical, &hints);
911 i = tokval.t_type;
912 if (op->opflags & OPFLAG_FORWARD) {
913 result->forw_ref = true;
915 /* and get the offset */
916 if (!value) /* Error in evaluator */
917 goto fail;
920 mib = false;
921 if (mref && bracket && i == ',') {
922 /* [seg:base+offset,index*scale] syntax (mib) */
924 operand o1, o2; /* Partial operands */
926 if (parse_mref(&o1, value))
927 goto fail;
929 i = stdscan(NULL, &tokval); /* Eat comma */
930 value = evaluate(stdscan, NULL, &tokval, &op->opflags,
931 critical, &hints);
932 i = tokval.t_type;
933 if (!value)
934 goto fail;
936 if (parse_mref(&o2, value))
937 goto fail;
939 if (o2.basereg != -1 && o2.indexreg == -1) {
940 o2.indexreg = o2.basereg;
941 o2.scale = 1;
942 o2.basereg = -1;
945 if (o1.indexreg != -1 || o2.basereg != -1 || o2.offset != 0 ||
946 o2.segment != NO_SEG || o2.wrt != NO_SEG) {
947 nasm_error(ERR_NONFATAL, "invalid mib expression");
948 goto fail;
951 op->basereg = o1.basereg;
952 op->indexreg = o2.indexreg;
953 op->scale = o2.scale;
954 op->offset = o1.offset;
955 op->segment = o1.segment;
956 op->wrt = o1.wrt;
958 if (op->basereg != -1) {
959 op->hintbase = op->basereg;
960 op->hinttype = EAH_MAKEBASE;
961 } else if (op->indexreg != -1) {
962 op->hintbase = op->indexreg;
963 op->hinttype = EAH_NOTBASE;
964 } else {
965 op->hintbase = -1;
966 op->hinttype = EAH_NOHINT;
969 mib = true;
972 recover = false;
973 if (mref && bracket) { /* find ] at the end */
974 if (i != ']') {
975 nasm_error(ERR_NONFATAL, "parser: expecting ]");
976 recover = true;
977 } else { /* we got the required ] */
978 i = stdscan(NULL, &tokval);
979 if ((i == TOKEN_DECORATOR) || (i == TOKEN_OPMASK)) {
981 * according to AVX512 spec, broacast or opmask decorator
982 * is expected for memory reference operands
984 if (tokval.t_flag & TFLAG_BRDCAST) {
985 brace_flags |= GEN_BRDCAST(0) |
986 VAL_BRNUM(tokval.t_integer - BRC_1TO2);
987 i = stdscan(NULL, &tokval);
988 } else if (i == TOKEN_OPMASK) {
989 brace_flags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
990 i = stdscan(NULL, &tokval);
991 } else {
992 nasm_error(ERR_NONFATAL, "broadcast or opmask "
993 "decorator expected inside braces");
994 recover = true;
998 if (i != 0 && i != ',') {
999 nasm_error(ERR_NONFATAL, "comma or end of line expected");
1000 recover = true;
1003 } else { /* immediate operand */
1004 if (i != 0 && i != ',' && i != ':' &&
1005 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
1006 nasm_error(ERR_NONFATAL, "comma, colon, decorator or end of "
1007 "line expected after operand");
1008 recover = true;
1009 } else if (i == ':') {
1010 op->type |= COLON;
1011 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
1012 /* parse opmask (and zeroing) after an operand */
1013 recover = parse_braces(&brace_flags);
1016 if (recover) {
1017 do { /* error recovery */
1018 i = stdscan(NULL, &tokval);
1019 } while (i != 0 && i != ',');
1023 * now convert the exprs returned from evaluate()
1024 * into operand descriptions...
1026 op->decoflags |= brace_flags;
1028 if (mref) { /* it's a memory reference */
1029 /* A mib reference was fully parsed already */
1030 if (!mib) {
1031 if (parse_mref(op, value))
1032 goto fail;
1033 op->hintbase = hints.base;
1034 op->hinttype = hints.type;
1036 mref_set_optype(op);
1037 } else { /* it's not a memory reference */
1038 if (is_just_unknown(value)) { /* it's immediate but unknown */
1039 op->type |= IMMEDIATE;
1040 op->opflags |= OPFLAG_UNKNOWN;
1041 op->offset = 0; /* don't care */
1042 op->segment = NO_SEG; /* don't care again */
1043 op->wrt = NO_SEG; /* still don't care */
1045 if(optimizing >= 0 && !(op->type & STRICT)) {
1046 /* Be optimistic */
1047 op->type |=
1048 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
1050 } else if (is_reloc(value)) { /* it's immediate */
1051 op->type |= IMMEDIATE;
1052 op->offset = reloc_value(value);
1053 op->segment = reloc_seg(value);
1054 op->wrt = reloc_wrt(value);
1055 op->opflags |= is_self_relative(value) ? OPFLAG_RELATIVE : 0;
1057 if (is_simple(value)) {
1058 uint64_t n = reloc_value(value);
1059 if (n == 1)
1060 op->type |= UNITY;
1061 if (optimizing >= 0 &&
1062 !(op->type & STRICT)) {
1063 if ((uint32_t) (n + 128) <= 255)
1064 op->type |= SBYTEDWORD;
1065 if ((uint16_t) (n + 128) <= 255)
1066 op->type |= SBYTEWORD;
1067 if (n <= 0xFFFFFFFF)
1068 op->type |= UDWORD;
1069 if (n + 0x80000000 <= 0xFFFFFFFF)
1070 op->type |= SDWORD;
1073 } else if (value->type == EXPR_RDSAE) {
1075 * it's not an operand but a rounding or SAE decorator.
1076 * put the decorator information in the (opflag_t) type field
1077 * of previous operand.
1079 opnum--; op--;
1080 switch (value->value) {
1081 case BRC_RN:
1082 case BRC_RU:
1083 case BRC_RD:
1084 case BRC_RZ:
1085 case BRC_SAE:
1086 op->decoflags |= (value->value == BRC_SAE ? SAE : ER);
1087 result->evex_rm = value->value;
1088 break;
1089 default:
1090 nasm_error(ERR_NONFATAL, "invalid decorator");
1091 break;
1093 } else { /* it's a register */
1094 opflags_t rs;
1096 if (value->type >= EXPR_SIMPLE || value->value != 1) {
1097 nasm_error(ERR_NONFATAL, "invalid operand type");
1098 goto fail;
1102 * check that its only 1 register, not an expression...
1104 for (i = 1; value[i].type; i++)
1105 if (value[i].value) {
1106 nasm_error(ERR_NONFATAL, "invalid operand type");
1107 goto fail;
1110 /* clear overrides, except TO which applies to FPU regs */
1111 if (op->type & ~TO) {
1113 * we want to produce a warning iff the specified size
1114 * is different from the register size
1116 rs = op->type & SIZE_MASK;
1117 } else
1118 rs = 0;
1120 op->type &= TO;
1121 op->type |= REGISTER;
1122 op->type |= nasm_reg_flags[value->type];
1123 op->decoflags |= brace_flags;
1124 op->basereg = value->type;
1126 if (rs && (op->type & SIZE_MASK) != rs)
1127 nasm_error(ERR_WARNING | ERR_PASS1,
1128 "register size specification ignored");
1132 /* remember the position of operand having broadcasting/ER mode */
1133 if (op->decoflags & (BRDCAST_MASK | ER | SAE))
1134 result->evex_brerop = opnum;
1137 result->operands = opnum; /* set operand count */
1139 /* clear remaining operands */
1140 while (opnum < MAX_OPERANDS)
1141 result->oprs[opnum++].type = 0;
1144 * Transform RESW, RESD, RESQ, REST, RESO, RESY, RESZ into RESB.
1146 switch (result->opcode) {
1147 case I_RESW:
1148 result->opcode = I_RESB;
1149 result->oprs[0].offset *= 2;
1150 break;
1151 case I_RESD:
1152 result->opcode = I_RESB;
1153 result->oprs[0].offset *= 4;
1154 break;
1155 case I_RESQ:
1156 result->opcode = I_RESB;
1157 result->oprs[0].offset *= 8;
1158 break;
1159 case I_REST:
1160 result->opcode = I_RESB;
1161 result->oprs[0].offset *= 10;
1162 break;
1163 case I_RESO:
1164 result->opcode = I_RESB;
1165 result->oprs[0].offset *= 16;
1166 break;
1167 case I_RESY:
1168 result->opcode = I_RESB;
1169 result->oprs[0].offset *= 32;
1170 break;
1171 case I_RESZ:
1172 result->opcode = I_RESB;
1173 result->oprs[0].offset *= 64;
1174 break;
1175 default:
1176 break;
1179 return result;
1181 fail:
1182 result->opcode = I_none;
1183 return result;
1186 static int is_comma_next(void)
1188 struct tokenval tv;
1189 char *p;
1190 int i;
1192 p = stdscan_get();
1193 i = stdscan(NULL, &tv);
1194 stdscan_set(p);
1196 return (i == ',' || i == ';' || !i);
1199 void cleanup_insn(insn * i)
1201 extop *e;
1203 while ((e = i->eops)) {
1204 i->eops = e->next;
1205 if (e->type == EOT_DB_STRING_FREE)
1206 nasm_free(e->stringval);
1207 nasm_free(e);