nasm_build_assert() -> nasm_static_assert()
[nasm.git] / asm / parser.c
blob5fc466792bbc5d5986755869269b40c28c625f5e
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2016 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;
251 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
252 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
254 if (is_gpr && e->value == 1)
255 b = e->type; /* It can be basereg */
256 else /* No, it has to be indexreg */
257 i = e->type, s = e->value;
258 e++;
260 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
261 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
263 if (b != -1) /* If the first was the base, ... */
264 i = e->type, s = e->value; /* second has to be indexreg */
266 else if (!is_gpr || e->value != 1) {
267 /* If both want to be index */
268 nasm_error(ERR_NONFATAL,
269 "invalid effective address: two index registers");
270 return -1;
271 } else
272 b = e->type;
273 e++;
275 if (e->type != 0) { /* is there an offset? */
276 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
277 nasm_error(ERR_NONFATAL,
278 "beroset-p-603-invalid effective address");
279 return -1;
280 } else {
281 if (e->type == EXPR_UNKNOWN) {
282 op->opflags |= OPFLAG_UNKNOWN;
283 o = 0; /* doesn't matter what */
284 op->wrt = NO_SEG; /* nor this */
285 op->segment = NO_SEG; /* or this */
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++;
296 } else
297 op->wrt = NO_SEG;
299 * Look for a segment base type.
301 if (e->type && e->type < EXPR_SEGBASE) {
302 nasm_error(ERR_NONFATAL,
303 "beroset-p-630-invalid effective address");
304 return -1;
306 while (e->type && e->value == 0)
307 e++;
308 if (e->type && e->value != 1) {
309 nasm_error(ERR_NONFATAL,
310 "beroset-p-637-invalid effective address");
311 return -1;
313 if (e->type) {
314 op->segment = e->type - EXPR_SEGBASE;
315 e++;
316 } else
317 op->segment = NO_SEG;
318 while (e->type && e->value == 0)
319 e++;
320 if (e->type) {
321 nasm_error(ERR_NONFATAL,
322 "beroset-p-650-invalid effective address");
323 return -1;
327 } else {
328 o = 0;
329 op->wrt = NO_SEG;
330 op->segment = NO_SEG;
333 if (e->type != 0) { /* there'd better be nothing left! */
334 nasm_error(ERR_NONFATAL,
335 "beroset-p-663-invalid effective address");
336 return -1;
339 op->basereg = b;
340 op->indexreg = i;
341 op->scale = s;
342 op->offset = o;
343 return 0;
346 static void mref_set_optype(operand *op)
348 int b = op->basereg;
349 int i = op->indexreg;
350 int s = op->scale;
352 /* It is memory, but it can match any r/m operand */
353 op->type |= MEMORY_ANY;
355 if (b == -1 && (i == -1 || s == 0)) {
356 int is_rel = globalbits == 64 &&
357 !(op->eaflags & EAF_ABS) &&
358 ((globalrel &&
359 !(op->eaflags & EAF_FSGS)) ||
360 (op->eaflags & EAF_REL));
362 op->type |= is_rel ? IP_REL : MEM_OFFS;
365 if (i != -1) {
366 opflags_t iclass = nasm_reg_flags[i];
368 if (is_class(XMMREG,iclass))
369 op->type |= XMEM;
370 else if (is_class(YMMREG,iclass))
371 op->type |= YMEM;
372 else if (is_class(ZMMREG,iclass))
373 op->type |= ZMEM;
378 * Convert an expression vector returned from evaluate() into an
379 * extop structure. Return zero on success.
381 static int value_to_extop(expr * vect, extop *eop, int32_t myseg)
383 eop->type = EOT_DB_NUMBER;
384 eop->offset = 0;
385 eop->segment = eop->wrt = NO_SEG;
386 eop->relative = false;
388 for (; vect->type; vect++) {
389 if (!vect->value) /* zero term, safe to ignore */
390 continue;
392 if (vect->type <= EXPR_REG_END) /* false if a register is present */
393 return -1;
395 if (vect->type == EXPR_UNKNOWN) /* something we can't resolve yet */
396 return 0;
398 if (vect->type == EXPR_SIMPLE) {
399 /* Simple number expression */
400 eop->offset += vect->value;
401 continue;
403 if (eop->wrt == NO_SEG && !eop->relative && vect->type == EXPR_WRT) {
404 /* WRT term */
405 eop->wrt = vect->value;
406 continue;
409 if (eop->wrt == NO_SEG && !eop->relative &&
410 vect->type == EXPR_SEGBASE + myseg && vect->value == -1) {
411 /* Expression of the form: foo - $ */
412 eop->relative = true;
413 continue;
416 if (eop->segment == NO_SEG && vect->type >= EXPR_SEGBASE &&
417 vect->value == 1) {
418 eop->segment = vect->type - EXPR_SEGBASE;
419 continue;
422 /* Otherwise, badness */
423 return -1;
426 /* We got to the end and it was all okay */
427 return 0;
430 insn *parse_line(int pass, char *buffer, insn *result, ldfunc ldef)
432 bool insn_is_label = false;
433 struct eval_hints hints;
434 int opnum;
435 int critical;
436 bool first;
437 bool recover;
438 int i;
440 restart_parse:
441 first = true;
442 result->forw_ref = false;
444 stdscan_reset();
445 stdscan_set(buffer);
446 i = stdscan(NULL, &tokval);
448 result->label = NULL; /* Assume no label */
449 result->eops = NULL; /* must do this, whatever happens */
450 result->operands = 0; /* must initialize this */
451 result->evex_rm = 0; /* Ensure EVEX rounding mode is reset */
452 result->evex_brerop = -1; /* Reset EVEX broadcasting/ER op position */
454 /* Ignore blank lines */
455 if (i == TOKEN_EOS)
456 goto fail;
458 if (i != TOKEN_ID &&
459 i != TOKEN_INSN &&
460 i != TOKEN_PREFIX &&
461 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
462 nasm_error(ERR_NONFATAL,
463 "label or instruction expected at start of line");
464 goto fail;
467 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
468 /* there's a label here */
469 first = false;
470 result->label = tokval.t_charptr;
471 i = stdscan(NULL, &tokval);
472 if (i == ':') { /* skip over the optional colon */
473 i = stdscan(NULL, &tokval);
474 } else if (i == 0) {
475 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
476 "label alone on a line without a colon might be in error");
478 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
480 * FIXME: location.segment could be NO_SEG, in which case
481 * it is possible we should be passing 'abs_seg'. Look into this.
482 * Work out whether that is *really* what we should be doing.
483 * Generally fix things. I think this is right as it is, but
484 * am still not certain.
486 ldef(result->label, in_abs_seg ? abs_seg : location.segment,
487 location.offset, NULL, true, false);
491 /* Just a label here */
492 if (i == TOKEN_EOS)
493 goto fail;
495 nasm_static_assert(P_none == 0);
496 memset(result->prefixes, P_none, sizeof(result->prefixes));
497 result->times = 1L;
499 while (i == TOKEN_PREFIX ||
500 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
501 first = false;
504 * Handle special case: the TIMES prefix.
506 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
507 expr *value;
509 i = stdscan(NULL, &tokval);
510 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, NULL);
511 i = tokval.t_type;
512 if (!value) /* Error in evaluator */
513 goto fail;
514 if (!is_simple(value)) {
515 nasm_error(ERR_NONFATAL,
516 "non-constant argument supplied to TIMES");
517 result->times = 1L;
518 } else {
519 result->times = value->value;
520 if (value->value < 0 && pass0 == 2) {
521 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
522 value->value);
523 result->times = 0;
526 } else {
527 int slot = prefix_slot(tokval.t_integer);
528 if (result->prefixes[slot]) {
529 if (result->prefixes[slot] == tokval.t_integer)
530 nasm_error(ERR_WARNING | ERR_PASS1,
531 "instruction has redundant prefixes");
532 else
533 nasm_error(ERR_NONFATAL,
534 "instruction has conflicting prefixes");
536 result->prefixes[slot] = tokval.t_integer;
537 i = stdscan(NULL, &tokval);
541 if (i != TOKEN_INSN) {
542 int j;
543 enum prefixes pfx;
545 for (j = 0; j < MAXPREFIX; j++) {
546 if ((pfx = result->prefixes[j]) != P_none)
547 break;
550 if (i == 0 && pfx != P_none) {
552 * Instruction prefixes are present, but no actual
553 * instruction. This is allowed: at this point we
554 * invent a notional instruction of RESB 0.
556 result->opcode = I_RESB;
557 result->operands = 1;
558 result->oprs[0].type = IMMEDIATE;
559 result->oprs[0].offset = 0L;
560 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
561 return result;
562 } else {
563 nasm_error(ERR_NONFATAL, "parser: instruction expected");
564 goto fail;
568 result->opcode = tokval.t_integer;
569 result->condition = tokval.t_inttwo;
572 * INCBIN cannot be satisfied with incorrectly
573 * evaluated operands, since the correct values _must_ be known
574 * on the first pass. Hence, even in pass one, we set the
575 * `critical' flag on calling evaluate(), so that it will bomb
576 * out on undefined symbols.
578 if (result->opcode == I_INCBIN) {
579 critical = (pass0 < 2 ? 1 : 2);
581 } else
582 critical = (pass == 2 ? 2 : 0);
584 if (result->opcode == I_DB || result->opcode == I_DW ||
585 result->opcode == I_DD || result->opcode == I_DQ ||
586 result->opcode == I_DT || result->opcode == I_DO ||
587 result->opcode == I_DY || result->opcode == I_DZ ||
588 result->opcode == I_INCBIN) {
589 extop *eop, **tail = &result->eops, **fixptr;
590 int oper_num = 0;
591 int32_t sign;
593 result->eops_float = false;
596 * Begin to read the DB/DW/DD/DQ/DT/DO/DY/DZ/INCBIN operands.
598 while (1) {
599 i = stdscan(NULL, &tokval);
600 if (i == TOKEN_EOS)
601 break;
602 else if (first && i == ':') {
603 insn_is_label = true;
604 goto restart_parse;
606 first = false;
607 fixptr = tail;
608 eop = *tail = nasm_malloc(sizeof(extop));
609 tail = &eop->next;
610 eop->next = NULL;
611 eop->type = EOT_NOTHING;
612 oper_num++;
613 sign = +1;
616 * is_comma_next() here is to distinguish this from
617 * a string used as part of an expression...
619 if (i == TOKEN_STR && is_comma_next()) {
620 eop->type = EOT_DB_STRING;
621 eop->stringval = tokval.t_charptr;
622 eop->stringlen = tokval.t_inttwo;
623 i = stdscan(NULL, &tokval); /* eat the comma */
624 } else if (i == TOKEN_STRFUNC) {
625 bool parens = false;
626 const char *funcname = tokval.t_charptr;
627 enum strfunc func = tokval.t_integer;
628 i = stdscan(NULL, &tokval);
629 if (i == '(') {
630 parens = true;
631 i = stdscan(NULL, &tokval);
633 if (i != TOKEN_STR) {
634 nasm_error(ERR_NONFATAL,
635 "%s must be followed by a string constant",
636 funcname);
637 eop->type = EOT_NOTHING;
638 } else {
639 eop->type = EOT_DB_STRING_FREE;
640 eop->stringlen =
641 string_transform(tokval.t_charptr, tokval.t_inttwo,
642 &eop->stringval, func);
643 if (eop->stringlen == (size_t)-1) {
644 nasm_error(ERR_NONFATAL, "invalid string for transform");
645 eop->type = EOT_NOTHING;
648 if (parens && i && i != ')') {
649 i = stdscan(NULL, &tokval);
650 if (i != ')') {
651 nasm_error(ERR_NONFATAL, "unterminated %s function",
652 funcname);
655 if (i && i != ',')
656 i = stdscan(NULL, &tokval);
657 } else if (i == '-' || i == '+') {
658 char *save = stdscan_get();
659 int token = i;
660 sign = (i == '-') ? -1 : 1;
661 i = stdscan(NULL, &tokval);
662 if (i != TOKEN_FLOAT) {
663 stdscan_set(save);
664 i = tokval.t_type = token;
665 goto is_expression;
666 } else {
667 goto is_float;
669 } else if (i == TOKEN_FLOAT) {
670 is_float:
671 eop->type = EOT_DB_STRING;
672 result->eops_float = true;
674 eop->stringlen = idata_bytes(result->opcode);
675 if (eop->stringlen > 16) {
676 nasm_error(ERR_NONFATAL, "floating-point constant"
677 " encountered in DY or DZ instruction");
678 eop->stringlen = 0;
679 } else if (eop->stringlen < 1) {
680 nasm_error(ERR_NONFATAL, "floating-point constant"
681 " encountered in unknown instruction");
683 * fix suggested by Pedro Gimeno... original line was:
684 * eop->type = EOT_NOTHING;
686 eop->stringlen = 0;
689 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
690 tail = &eop->next;
691 *fixptr = eop;
692 eop->stringval = (char *)eop + sizeof(extop);
693 if (!eop->stringlen ||
694 !float_const(tokval.t_charptr, sign,
695 (uint8_t *)eop->stringval, eop->stringlen))
696 eop->type = EOT_NOTHING;
697 i = stdscan(NULL, &tokval); /* eat the comma */
698 } else {
699 /* anything else, assume it is an expression */
700 expr *value;
702 is_expression:
703 value = evaluate(stdscan, NULL, &tokval, NULL,
704 critical, NULL);
705 i = tokval.t_type;
706 if (!value) /* Error in evaluator */
707 goto fail;
708 if (value_to_extop(value, eop, location.segment)) {
709 nasm_error(ERR_NONFATAL,
710 "operand %d: expression is not simple or relocatable",
711 oper_num);
716 * We're about to call stdscan(), which will eat the
717 * comma that we're currently sitting on between
718 * arguments. However, we'd better check first that it
719 * _is_ a comma.
721 if (i == TOKEN_EOS) /* also could be EOL */
722 break;
723 if (i != ',') {
724 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
725 oper_num);
726 goto fail;
730 if (result->opcode == I_INCBIN) {
732 * Correct syntax for INCBIN is that there should be
733 * one string operand, followed by one or two numeric
734 * operands.
736 if (!result->eops || result->eops->type != EOT_DB_STRING)
737 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
738 else if (result->eops->next &&
739 result->eops->next->type != EOT_DB_NUMBER)
740 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
741 " non-numeric");
742 else if (result->eops->next && result->eops->next->next &&
743 result->eops->next->next->type != EOT_DB_NUMBER)
744 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
745 " non-numeric");
746 else if (result->eops->next && result->eops->next->next &&
747 result->eops->next->next->next)
748 nasm_error(ERR_NONFATAL,
749 "`incbin': more than three parameters");
750 else
751 return result;
753 * If we reach here, one of the above errors happened.
754 * Throw the instruction away.
756 goto fail;
757 } else /* DB ... */ if (oper_num == 0)
758 nasm_error(ERR_WARNING | ERR_PASS1,
759 "no operand for data declaration");
760 else
761 result->operands = oper_num;
763 return result;
767 * Now we begin to parse the operands. There may be up to four
768 * of these, separated by commas, and terminated by a zero token.
771 for (opnum = 0; opnum < MAX_OPERANDS; opnum++) {
772 operand *op = &result->oprs[opnum];
773 expr *value; /* used most of the time */
774 bool mref; /* is this going to be a memory ref? */
775 bool bracket; /* is it a [] mref, or a & mref? */
776 bool mib; /* compound (mib) mref? */
777 int setsize = 0;
778 decoflags_t brace_flags = 0; /* flags for decorators in braces */
780 op->disp_size = 0; /* have to zero this whatever */
781 op->eaflags = 0; /* and this */
782 op->opflags = 0;
783 op->decoflags = 0;
785 i = stdscan(NULL, &tokval);
786 if (i == TOKEN_EOS)
787 break; /* end of operands: get out of here */
788 else if (first && i == ':') {
789 insn_is_label = true;
790 goto restart_parse;
792 first = false;
793 op->type = 0; /* so far, no override */
794 while (i == TOKEN_SPECIAL) { /* size specifiers */
795 switch ((int)tokval.t_integer) {
796 case S_BYTE:
797 if (!setsize) /* we want to use only the first */
798 op->type |= BITS8;
799 setsize = 1;
800 break;
801 case S_WORD:
802 if (!setsize)
803 op->type |= BITS16;
804 setsize = 1;
805 break;
806 case S_DWORD:
807 case S_LONG:
808 if (!setsize)
809 op->type |= BITS32;
810 setsize = 1;
811 break;
812 case S_QWORD:
813 if (!setsize)
814 op->type |= BITS64;
815 setsize = 1;
816 break;
817 case S_TWORD:
818 if (!setsize)
819 op->type |= BITS80;
820 setsize = 1;
821 break;
822 case S_OWORD:
823 if (!setsize)
824 op->type |= BITS128;
825 setsize = 1;
826 break;
827 case S_YWORD:
828 if (!setsize)
829 op->type |= BITS256;
830 setsize = 1;
831 break;
832 case S_ZWORD:
833 if (!setsize)
834 op->type |= BITS512;
835 setsize = 1;
836 break;
837 case S_TO:
838 op->type |= TO;
839 break;
840 case S_STRICT:
841 op->type |= STRICT;
842 break;
843 case S_FAR:
844 op->type |= FAR;
845 break;
846 case S_NEAR:
847 op->type |= NEAR;
848 break;
849 case S_SHORT:
850 op->type |= SHORT;
851 break;
852 default:
853 nasm_error(ERR_NONFATAL, "invalid operand size specification");
855 i = stdscan(NULL, &tokval);
858 if (i == '[' || i == '&') { /* memory reference */
859 mref = true;
860 bracket = (i == '[');
861 i = stdscan(NULL, &tokval); /* then skip the colon */
862 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
863 process_size_override(result, op);
864 i = stdscan(NULL, &tokval);
866 /* when a comma follows an opening bracket - [ , eax*4] */
867 if (i == ',') {
868 /* treat as if there is a zero displacement virtually */
869 tokval.t_type = TOKEN_NUM;
870 tokval.t_integer = 0;
871 stdscan_set(stdscan_get() - 1); /* rewind the comma */
873 } else { /* immediate operand, or register */
874 mref = false;
875 bracket = false; /* placate optimisers */
878 if ((op->type & FAR) && !mref &&
879 result->opcode != I_JMP && result->opcode != I_CALL) {
880 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
883 value = evaluate(stdscan, NULL, &tokval,
884 &op->opflags, critical, &hints);
885 i = tokval.t_type;
886 if (op->opflags & OPFLAG_FORWARD) {
887 result->forw_ref = true;
889 if (!value) /* Error in evaluator */
890 goto fail;
891 if (i == ':' && mref) { /* it was seg:offset */
893 * Process the segment override.
895 if (value[1].type != 0 ||
896 value->value != 1 ||
897 !IS_SREG(value->type))
898 nasm_error(ERR_NONFATAL, "invalid segment override");
899 else if (result->prefixes[PPS_SEG])
900 nasm_error(ERR_NONFATAL,
901 "instruction has conflicting segment overrides");
902 else {
903 result->prefixes[PPS_SEG] = value->type;
904 if (IS_FSGS(value->type))
905 op->eaflags |= EAF_FSGS;
908 i = stdscan(NULL, &tokval); /* then skip the colon */
909 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
910 process_size_override(result, op);
911 i = stdscan(NULL, &tokval);
913 value = evaluate(stdscan, NULL, &tokval,
914 &op->opflags, critical, &hints);
915 i = tokval.t_type;
916 if (op->opflags & OPFLAG_FORWARD) {
917 result->forw_ref = true;
919 /* and get the offset */
920 if (!value) /* Error in evaluator */
921 goto fail;
924 mib = false;
925 if (mref && bracket && i == ',') {
926 /* [seg:base+offset,index*scale] syntax (mib) */
928 operand o1, o2; /* Partial operands */
930 if (parse_mref(&o1, value))
931 goto fail;
933 i = stdscan(NULL, &tokval); /* Eat comma */
934 value = evaluate(stdscan, NULL, &tokval, &op->opflags,
935 critical, &hints);
936 i = tokval.t_type;
937 if (!value)
938 goto fail;
940 if (parse_mref(&o2, value))
941 goto fail;
943 if (o2.basereg != -1 && o2.indexreg == -1) {
944 o2.indexreg = o2.basereg;
945 o2.scale = 1;
946 o2.basereg = -1;
949 if (o1.indexreg != -1 || o2.basereg != -1 || o2.offset != 0 ||
950 o2.segment != NO_SEG || o2.wrt != NO_SEG) {
951 nasm_error(ERR_NONFATAL, "invalid mib expression");
952 goto fail;
955 op->basereg = o1.basereg;
956 op->indexreg = o2.indexreg;
957 op->scale = o2.scale;
958 op->offset = o1.offset;
959 op->segment = o1.segment;
960 op->wrt = o1.wrt;
962 if (op->basereg != -1) {
963 op->hintbase = op->basereg;
964 op->hinttype = EAH_MAKEBASE;
965 } else if (op->indexreg != -1) {
966 op->hintbase = op->indexreg;
967 op->hinttype = EAH_NOTBASE;
968 } else {
969 op->hintbase = -1;
970 op->hinttype = EAH_NOHINT;
973 mib = true;
976 recover = false;
977 if (mref && bracket) { /* find ] at the end */
978 if (i != ']') {
979 nasm_error(ERR_NONFATAL, "parser: expecting ]");
980 recover = true;
981 } else { /* we got the required ] */
982 i = stdscan(NULL, &tokval);
983 if ((i == TOKEN_DECORATOR) || (i == TOKEN_OPMASK)) {
985 * according to AVX512 spec, broacast or opmask decorator
986 * is expected for memory reference operands
988 if (tokval.t_flag & TFLAG_BRDCAST) {
989 brace_flags |= GEN_BRDCAST(0) |
990 VAL_BRNUM(tokval.t_integer - BRC_1TO2);
991 i = stdscan(NULL, &tokval);
992 } else if (i == TOKEN_OPMASK) {
993 brace_flags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
994 i = stdscan(NULL, &tokval);
995 } else {
996 nasm_error(ERR_NONFATAL, "broadcast or opmask "
997 "decorator expected inside braces");
998 recover = true;
1002 if (i != 0 && i != ',') {
1003 nasm_error(ERR_NONFATAL, "comma or end of line expected");
1004 recover = true;
1007 } else { /* immediate operand */
1008 if (i != 0 && i != ',' && i != ':' &&
1009 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
1010 nasm_error(ERR_NONFATAL, "comma, colon, decorator or end of "
1011 "line expected after operand");
1012 recover = true;
1013 } else if (i == ':') {
1014 op->type |= COLON;
1015 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
1016 /* parse opmask (and zeroing) after an operand */
1017 recover = parse_braces(&brace_flags);
1020 if (recover) {
1021 do { /* error recovery */
1022 i = stdscan(NULL, &tokval);
1023 } while (i != 0 && i != ',');
1027 * now convert the exprs returned from evaluate()
1028 * into operand descriptions...
1030 op->decoflags |= brace_flags;
1032 if (mref) { /* it's a memory reference */
1033 /* A mib reference was fully parsed already */
1034 if (!mib) {
1035 if (parse_mref(op, value))
1036 goto fail;
1037 op->hintbase = hints.base;
1038 op->hinttype = hints.type;
1040 mref_set_optype(op);
1041 } else { /* it's not a memory reference */
1042 if (is_just_unknown(value)) { /* it's immediate but unknown */
1043 op->type |= IMMEDIATE;
1044 op->opflags |= OPFLAG_UNKNOWN;
1045 op->offset = 0; /* don't care */
1046 op->segment = NO_SEG; /* don't care again */
1047 op->wrt = NO_SEG; /* still don't care */
1049 if(optimizing >= 0 && !(op->type & STRICT)) {
1050 /* Be optimistic */
1051 op->type |=
1052 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
1054 } else if (is_reloc(value)) { /* it's immediate */
1055 op->type |= IMMEDIATE;
1056 op->offset = reloc_value(value);
1057 op->segment = reloc_seg(value);
1058 op->wrt = reloc_wrt(value);
1060 if (is_simple(value)) {
1061 uint64_t n = reloc_value(value);
1062 if (n == 1)
1063 op->type |= UNITY;
1064 if (optimizing >= 0 &&
1065 !(op->type & STRICT)) {
1066 if ((uint32_t) (n + 128) <= 255)
1067 op->type |= SBYTEDWORD;
1068 if ((uint16_t) (n + 128) <= 255)
1069 op->type |= SBYTEWORD;
1070 if (n <= 0xFFFFFFFF)
1071 op->type |= UDWORD;
1072 if (n + 0x80000000 <= 0xFFFFFFFF)
1073 op->type |= SDWORD;
1076 } else if(value->type == EXPR_RDSAE) {
1078 * it's not an operand but a rounding or SAE decorator.
1079 * put the decorator information in the (opflag_t) type field
1080 * of previous operand.
1082 opnum--; op--;
1083 switch (value->value) {
1084 case BRC_RN:
1085 case BRC_RU:
1086 case BRC_RD:
1087 case BRC_RZ:
1088 case BRC_SAE:
1089 op->decoflags |= (value->value == BRC_SAE ? SAE : ER);
1090 result->evex_rm = value->value;
1091 break;
1092 default:
1093 nasm_error(ERR_NONFATAL, "invalid decorator");
1094 break;
1096 } else { /* it's a register */
1097 opflags_t rs;
1099 if (value->type >= EXPR_SIMPLE || value->value != 1) {
1100 nasm_error(ERR_NONFATAL, "invalid operand type");
1101 goto fail;
1105 * check that its only 1 register, not an expression...
1107 for (i = 1; value[i].type; i++)
1108 if (value[i].value) {
1109 nasm_error(ERR_NONFATAL, "invalid operand type");
1110 goto fail;
1113 /* clear overrides, except TO which applies to FPU regs */
1114 if (op->type & ~TO) {
1116 * we want to produce a warning iff the specified size
1117 * is different from the register size
1119 rs = op->type & SIZE_MASK;
1120 } else
1121 rs = 0;
1123 op->type &= TO;
1124 op->type |= REGISTER;
1125 op->type |= nasm_reg_flags[value->type];
1126 op->decoflags |= brace_flags;
1127 op->basereg = value->type;
1129 if (rs && (op->type & SIZE_MASK) != rs)
1130 nasm_error(ERR_WARNING | ERR_PASS1,
1131 "register size specification ignored");
1135 /* remember the position of operand having broadcasting/ER mode */
1136 if (op->decoflags & (BRDCAST_MASK | ER | SAE))
1137 result->evex_brerop = opnum;
1140 result->operands = opnum; /* set operand count */
1142 /* clear remaining operands */
1143 while (opnum < MAX_OPERANDS)
1144 result->oprs[opnum++].type = 0;
1147 * Transform RESW, RESD, RESQ, REST, RESO, RESY, RESZ into RESB.
1149 switch (result->opcode) {
1150 case I_RESW:
1151 result->opcode = I_RESB;
1152 result->oprs[0].offset *= 2;
1153 break;
1154 case I_RESD:
1155 result->opcode = I_RESB;
1156 result->oprs[0].offset *= 4;
1157 break;
1158 case I_RESQ:
1159 result->opcode = I_RESB;
1160 result->oprs[0].offset *= 8;
1161 break;
1162 case I_REST:
1163 result->opcode = I_RESB;
1164 result->oprs[0].offset *= 10;
1165 break;
1166 case I_RESO:
1167 result->opcode = I_RESB;
1168 result->oprs[0].offset *= 16;
1169 break;
1170 case I_RESY:
1171 result->opcode = I_RESB;
1172 result->oprs[0].offset *= 32;
1173 break;
1174 case I_RESZ:
1175 result->opcode = I_RESB;
1176 result->oprs[0].offset *= 64;
1177 break;
1178 default:
1179 break;
1182 return result;
1184 fail:
1185 result->opcode = I_none;
1186 return result;
1189 static int is_comma_next(void)
1191 struct tokenval tv;
1192 char *p;
1193 int i;
1195 p = stdscan_get();
1196 i = stdscan(NULL, &tv);
1197 stdscan_set(p);
1199 return (i == ',' || i == ';' || !i);
1202 void cleanup_insn(insn * i)
1204 extop *e;
1206 while ((e = i->eops)) {
1207 i->eops = e->next;
1208 if (e->type == EOT_DB_STRING_FREE)
1209 nasm_free(e->stringval);
1210 nasm_free(e);