Merge remote-tracking branch 'origin/nasm-2.12.xx'
[nasm.git] / parser.c
blob6210cc69b3a34cbfd2a65926bae8dac633422618
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2013 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 int i;
62 static struct tokenval tokval;
64 static int prefix_slot(int prefix)
66 switch (prefix) {
67 case P_WAIT:
68 return PPS_WAIT;
69 case R_CS:
70 case R_DS:
71 case R_SS:
72 case R_ES:
73 case R_FS:
74 case R_GS:
75 return PPS_SEG;
76 case P_LOCK:
77 return PPS_LOCK;
78 case P_REP:
79 case P_REPE:
80 case P_REPZ:
81 case P_REPNE:
82 case P_REPNZ:
83 case P_XACQUIRE:
84 case P_XRELEASE:
85 case P_BND:
86 case P_NOBND:
87 return PPS_REP;
88 case P_O16:
89 case P_O32:
90 case P_O64:
91 case P_OSP:
92 return PPS_OSIZE;
93 case P_A16:
94 case P_A32:
95 case P_A64:
96 case P_ASP:
97 return PPS_ASIZE;
98 case P_EVEX:
99 case P_VEX3:
100 case P_VEX2:
101 return PPS_VEX;
102 default:
103 nasm_panic(0, "Invalid value %d passed to prefix_slot()", prefix);
104 return -1;
108 static void process_size_override(insn *result, operand *op)
110 if (tasm_compatible_mode) {
111 switch ((int)tokval.t_integer) {
112 /* For TASM compatibility a size override inside the
113 * brackets changes the size of the operand, not the
114 * address type of the operand as it does in standard
115 * NASM syntax. Hence:
117 * mov eax,[DWORD val]
119 * is valid syntax in TASM compatibility mode. Note that
120 * you lose the ability to override the default address
121 * type for the instruction, but we never use anything
122 * but 32-bit flat model addressing in our code.
124 case S_BYTE:
125 op->type |= BITS8;
126 break;
127 case S_WORD:
128 op->type |= BITS16;
129 break;
130 case S_DWORD:
131 case S_LONG:
132 op->type |= BITS32;
133 break;
134 case S_QWORD:
135 op->type |= BITS64;
136 break;
137 case S_TWORD:
138 op->type |= BITS80;
139 break;
140 case S_OWORD:
141 op->type |= BITS128;
142 break;
143 default:
144 nasm_error(ERR_NONFATAL,
145 "invalid operand size specification");
146 break;
148 } else {
149 /* Standard NASM compatible syntax */
150 switch ((int)tokval.t_integer) {
151 case S_NOSPLIT:
152 op->eaflags |= EAF_TIMESTWO;
153 break;
154 case S_REL:
155 op->eaflags |= EAF_REL;
156 break;
157 case S_ABS:
158 op->eaflags |= EAF_ABS;
159 break;
160 case S_BYTE:
161 op->disp_size = 8;
162 op->eaflags |= EAF_BYTEOFFS;
163 break;
164 case P_A16:
165 case P_A32:
166 case P_A64:
167 if (result->prefixes[PPS_ASIZE] &&
168 result->prefixes[PPS_ASIZE] != tokval.t_integer)
169 nasm_error(ERR_NONFATAL,
170 "conflicting address size specifications");
171 else
172 result->prefixes[PPS_ASIZE] = tokval.t_integer;
173 break;
174 case S_WORD:
175 op->disp_size = 16;
176 op->eaflags |= EAF_WORDOFFS;
177 break;
178 case S_DWORD:
179 case S_LONG:
180 op->disp_size = 32;
181 op->eaflags |= EAF_WORDOFFS;
182 break;
183 case S_QWORD:
184 op->disp_size = 64;
185 op->eaflags |= EAF_WORDOFFS;
186 break;
187 default:
188 nasm_error(ERR_NONFATAL, "invalid size specification in"
189 " effective address");
190 break;
196 * when two or more decorators follow a register operand,
197 * consecutive decorators are parsed here.
198 * opmask and zeroing decorators can be placed in any order.
199 * e.g. zmm1 {k2}{z} or zmm2 {z}{k3}
200 * decorator(s) are placed at the end of an operand.
202 static bool parse_braces(decoflags_t *decoflags)
204 int i;
205 bool recover = false;
207 i = tokval.t_type;
208 do {
209 if (i == TOKEN_OPMASK) {
210 if (*decoflags & OPMASK_MASK) {
211 nasm_error(ERR_NONFATAL, "opmask k%"PRIu64" is already set",
212 *decoflags & OPMASK_MASK);
213 *decoflags &= ~OPMASK_MASK;
215 *decoflags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
216 } else if (i == TOKEN_DECORATOR) {
217 switch (tokval.t_integer) {
218 case BRC_Z:
220 * according to AVX512 spec, only zeroing/merging decorator
221 * is supported with opmask
223 *decoflags |= GEN_Z(0);
224 break;
225 default:
226 nasm_error(ERR_NONFATAL, "{%s} is not an expected decorator",
227 tokval.t_charptr);
228 break;
230 } else if (i == ',' || i == TOKEN_EOS){
231 break;
232 } else {
233 nasm_error(ERR_NONFATAL, "only a series of valid decorators"
234 " expected");
235 recover = true;
236 break;
238 i = stdscan(NULL, &tokval);
239 } while(1);
241 return recover;
244 static int parse_mref(operand *op, const expr *e)
246 int b, i, s; /* basereg, indexreg, scale */
247 int64_t o; /* offset */
249 b = i = -1;
250 o = s = 0;
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++;
276 if (e->type != 0) { /* is there an offset? */
277 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
278 nasm_error(ERR_NONFATAL,
279 "beroset-p-603-invalid effective address");
280 return -1;
281 } else {
282 if (e->type == EXPR_UNKNOWN) {
283 op->opflags |= OPFLAG_UNKNOWN;
284 o = 0; /* doesn't matter what */
285 op->wrt = NO_SEG; /* nor this */
286 op->segment = NO_SEG; /* or this */
287 while (e->type)
288 e++; /* go to the end of the line */
289 } else {
290 if (e->type == EXPR_SIMPLE) {
291 o = e->value;
292 e++;
294 if (e->type == EXPR_WRT) {
295 op->wrt = e->value;
296 e++;
297 } else
298 op->wrt = NO_SEG;
300 * Look for a segment base type.
302 if (e->type && e->type < EXPR_SEGBASE) {
303 nasm_error(ERR_NONFATAL,
304 "beroset-p-630-invalid effective address");
305 return -1;
307 while (e->type && e->value == 0)
308 e++;
309 if (e->type && e->value != 1) {
310 nasm_error(ERR_NONFATAL,
311 "beroset-p-637-invalid effective address");
312 return -1;
314 if (e->type) {
315 op->segment = e->type - EXPR_SEGBASE;
316 e++;
317 } else
318 op->segment = NO_SEG;
319 while (e->type && e->value == 0)
320 e++;
321 if (e->type) {
322 nasm_error(ERR_NONFATAL,
323 "beroset-p-650-invalid effective address");
324 return -1;
328 } else {
329 o = 0;
330 op->wrt = NO_SEG;
331 op->segment = NO_SEG;
334 if (e->type != 0) { /* there'd better be nothing left! */
335 nasm_error(ERR_NONFATAL,
336 "beroset-p-663-invalid effective address");
337 return -1;
340 op->basereg = b;
341 op->indexreg = i;
342 op->scale = s;
343 op->offset = o;
344 return 0;
347 static void mref_set_optype(operand *op)
349 int b = op->basereg;
350 int i = op->indexreg;
351 int s = op->scale;
353 /* It is memory, but it can match any r/m operand */
354 op->type |= MEMORY_ANY;
356 if (b == -1 && (i == -1 || s == 0)) {
357 int is_rel = globalbits == 64 &&
358 !(op->eaflags & EAF_ABS) &&
359 ((globalrel &&
360 !(op->eaflags & EAF_FSGS)) ||
361 (op->eaflags & EAF_REL));
363 op->type |= is_rel ? IP_REL : MEM_OFFS;
366 if (i != -1) {
367 opflags_t iclass = nasm_reg_flags[i];
369 if (is_class(XMMREG,iclass))
370 op->type |= XMEM;
371 else if (is_class(YMMREG,iclass))
372 op->type |= YMEM;
373 else if (is_class(ZMMREG,iclass))
374 op->type |= ZMEM;
378 insn *parse_line(int pass, char *buffer, insn *result, ldfunc ldef)
380 bool insn_is_label = false;
381 struct eval_hints hints;
382 int opnum;
383 int critical;
384 bool first;
385 bool recover;
387 restart_parse:
388 first = true;
389 result->forw_ref = false;
391 stdscan_reset();
392 stdscan_set(buffer);
393 i = stdscan(NULL, &tokval);
395 result->label = NULL; /* Assume no label */
396 result->eops = NULL; /* must do this, whatever happens */
397 result->operands = 0; /* must initialize this */
398 result->evex_rm = 0; /* Ensure EVEX rounding mode is reset */
399 result->evex_brerop = -1; /* Reset EVEX broadcasting/ER op position */
401 /* Ignore blank lines */
402 if (i == TOKEN_EOS)
403 goto fail;
405 if (i != TOKEN_ID &&
406 i != TOKEN_INSN &&
407 i != TOKEN_PREFIX &&
408 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
409 nasm_error(ERR_NONFATAL,
410 "label or instruction expected at start of line");
411 goto fail;
414 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
415 /* there's a label here */
416 first = false;
417 result->label = tokval.t_charptr;
418 i = stdscan(NULL, &tokval);
419 if (i == ':') { /* skip over the optional colon */
420 i = stdscan(NULL, &tokval);
421 } else if (i == 0) {
422 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
423 "label alone on a line without a colon might be in error");
425 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
427 * FIXME: location.segment could be NO_SEG, in which case
428 * it is possible we should be passing 'abs_seg'. Look into this.
429 * Work out whether that is *really* what we should be doing.
430 * Generally fix things. I think this is right as it is, but
431 * am still not certain.
433 ldef(result->label, in_abs_seg ? abs_seg : location.segment,
434 location.offset, NULL, true, false);
438 /* Just a label here */
439 if (i == TOKEN_EOS)
440 goto fail;
442 nasm_build_assert(P_none != 0);
443 memset(result->prefixes, P_none, sizeof(result->prefixes));
444 result->times = 1L;
446 while (i == TOKEN_PREFIX ||
447 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
448 first = false;
451 * Handle special case: the TIMES prefix.
453 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
454 expr *value;
456 i = stdscan(NULL, &tokval);
457 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, NULL);
458 i = tokval.t_type;
459 if (!value) /* Error in evaluator */
460 goto fail;
461 if (!is_simple(value)) {
462 nasm_error(ERR_NONFATAL,
463 "non-constant argument supplied to TIMES");
464 result->times = 1L;
465 } else {
466 result->times = value->value;
467 if (value->value < 0 && pass0 == 2) {
468 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
469 value->value);
470 result->times = 0;
473 } else {
474 int slot = prefix_slot(tokval.t_integer);
475 if (result->prefixes[slot]) {
476 if (result->prefixes[slot] == tokval.t_integer)
477 nasm_error(ERR_WARNING | ERR_PASS1,
478 "instruction has redundant prefixes");
479 else
480 nasm_error(ERR_NONFATAL,
481 "instruction has conflicting prefixes");
483 result->prefixes[slot] = tokval.t_integer;
484 i = stdscan(NULL, &tokval);
488 if (i != TOKEN_INSN) {
489 int j;
490 enum prefixes pfx;
492 for (j = 0; j < MAXPREFIX; j++) {
493 if ((pfx = result->prefixes[j]) != P_none)
494 break;
497 if (i == 0 && pfx != P_none) {
499 * Instruction prefixes are present, but no actual
500 * instruction. This is allowed: at this point we
501 * invent a notional instruction of RESB 0.
503 result->opcode = I_RESB;
504 result->operands = 1;
505 result->oprs[0].type = IMMEDIATE;
506 result->oprs[0].offset = 0L;
507 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
508 return result;
509 } else {
510 nasm_error(ERR_NONFATAL, "parser: instruction expected");
511 goto fail;
515 result->opcode = tokval.t_integer;
516 result->condition = tokval.t_inttwo;
519 * INCBIN cannot be satisfied with incorrectly
520 * evaluated operands, since the correct values _must_ be known
521 * on the first pass. Hence, even in pass one, we set the
522 * `critical' flag on calling evaluate(), so that it will bomb
523 * out on undefined symbols.
525 if (result->opcode == I_INCBIN) {
526 critical = (pass0 < 2 ? 1 : 2);
528 } else
529 critical = (pass == 2 ? 2 : 0);
531 if (result->opcode == I_DB || result->opcode == I_DW ||
532 result->opcode == I_DD || result->opcode == I_DQ ||
533 result->opcode == I_DT || result->opcode == I_DO ||
534 result->opcode == I_DY || result->opcode == I_DZ ||
535 result->opcode == I_INCBIN) {
536 extop *eop, **tail = &result->eops, **fixptr;
537 int oper_num = 0;
538 int32_t sign;
540 result->eops_float = false;
543 * Begin to read the DB/DW/DD/DQ/DT/DO/DY/DZ/INCBIN operands.
545 while (1) {
546 i = stdscan(NULL, &tokval);
547 if (i == TOKEN_EOS)
548 break;
549 else if (first && i == ':') {
550 insn_is_label = true;
551 goto restart_parse;
553 first = false;
554 fixptr = tail;
555 eop = *tail = nasm_malloc(sizeof(extop));
556 tail = &eop->next;
557 eop->next = NULL;
558 eop->type = EOT_NOTHING;
559 oper_num++;
560 sign = +1;
563 * is_comma_next() here is to distinguish this from
564 * a string used as part of an expression...
566 if (i == TOKEN_STR && is_comma_next()) {
567 eop->type = EOT_DB_STRING;
568 eop->stringval = tokval.t_charptr;
569 eop->stringlen = tokval.t_inttwo;
570 i = stdscan(NULL, &tokval); /* eat the comma */
571 } else if (i == TOKEN_STRFUNC) {
572 bool parens = false;
573 const char *funcname = tokval.t_charptr;
574 enum strfunc func = tokval.t_integer;
575 i = stdscan(NULL, &tokval);
576 if (i == '(') {
577 parens = true;
578 i = stdscan(NULL, &tokval);
580 if (i != TOKEN_STR) {
581 nasm_error(ERR_NONFATAL,
582 "%s must be followed by a string constant",
583 funcname);
584 eop->type = EOT_NOTHING;
585 } else {
586 eop->type = EOT_DB_STRING_FREE;
587 eop->stringlen =
588 string_transform(tokval.t_charptr, tokval.t_inttwo,
589 &eop->stringval, func);
590 if (eop->stringlen == (size_t)-1) {
591 nasm_error(ERR_NONFATAL, "invalid string for transform");
592 eop->type = EOT_NOTHING;
595 if (parens && i && i != ')') {
596 i = stdscan(NULL, &tokval);
597 if (i != ')') {
598 nasm_error(ERR_NONFATAL, "unterminated %s function",
599 funcname);
602 if (i && i != ',')
603 i = stdscan(NULL, &tokval);
604 } else if (i == '-' || i == '+') {
605 char *save = stdscan_get();
606 int token = i;
607 sign = (i == '-') ? -1 : 1;
608 i = stdscan(NULL, &tokval);
609 if (i != TOKEN_FLOAT) {
610 stdscan_set(save);
611 i = tokval.t_type = token;
612 goto is_expression;
613 } else {
614 goto is_float;
616 } else if (i == TOKEN_FLOAT) {
617 is_float:
618 eop->type = EOT_DB_STRING;
619 result->eops_float = true;
621 eop->stringlen = idata_bytes(result->opcode);
622 if (eop->stringlen > 16) {
623 nasm_error(ERR_NONFATAL, "floating-point constant"
624 " encountered in DY or DZ instruction");
625 eop->stringlen = 0;
626 } else if (eop->stringlen < 1) {
627 nasm_error(ERR_NONFATAL, "floating-point constant"
628 " encountered in unknown instruction");
630 * fix suggested by Pedro Gimeno... original line was:
631 * eop->type = EOT_NOTHING;
633 eop->stringlen = 0;
636 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
637 tail = &eop->next;
638 *fixptr = eop;
639 eop->stringval = (char *)eop + sizeof(extop);
640 if (!eop->stringlen ||
641 !float_const(tokval.t_charptr, sign,
642 (uint8_t *)eop->stringval, eop->stringlen))
643 eop->type = EOT_NOTHING;
644 i = stdscan(NULL, &tokval); /* eat the comma */
645 } else {
646 /* anything else, assume it is an expression */
647 expr *value;
649 is_expression:
650 value = evaluate(stdscan, NULL, &tokval, NULL,
651 critical, NULL);
652 i = tokval.t_type;
653 if (!value) /* Error in evaluator */
654 goto fail;
655 if (is_unknown(value)) {
656 eop->type = EOT_DB_NUMBER;
657 eop->offset = 0; /* doesn't matter what we put */
658 eop->segment = eop->wrt = NO_SEG; /* likewise */
659 } else if (is_reloc(value)) {
660 eop->type = EOT_DB_NUMBER;
661 eop->offset = reloc_value(value);
662 eop->segment = reloc_seg(value);
663 eop->wrt = reloc_wrt(value);
664 } else {
665 nasm_error(ERR_NONFATAL,
666 "operand %d: expression is not simple"
667 " or relocatable", oper_num);
672 * We're about to call stdscan(), which will eat the
673 * comma that we're currently sitting on between
674 * arguments. However, we'd better check first that it
675 * _is_ a comma.
677 if (i == TOKEN_EOS) /* also could be EOL */
678 break;
679 if (i != ',') {
680 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
681 oper_num);
682 goto fail;
686 if (result->opcode == I_INCBIN) {
688 * Correct syntax for INCBIN is that there should be
689 * one string operand, followed by one or two numeric
690 * operands.
692 if (!result->eops || result->eops->type != EOT_DB_STRING)
693 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
694 else if (result->eops->next &&
695 result->eops->next->type != EOT_DB_NUMBER)
696 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
697 " non-numeric");
698 else if (result->eops->next && result->eops->next->next &&
699 result->eops->next->next->type != EOT_DB_NUMBER)
700 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
701 " non-numeric");
702 else if (result->eops->next && result->eops->next->next &&
703 result->eops->next->next->next)
704 nasm_error(ERR_NONFATAL,
705 "`incbin': more than three parameters");
706 else
707 return result;
709 * If we reach here, one of the above errors happened.
710 * Throw the instruction away.
712 goto fail;
713 } else /* DB ... */ if (oper_num == 0)
714 nasm_error(ERR_WARNING | ERR_PASS1,
715 "no operand for data declaration");
716 else
717 result->operands = oper_num;
719 return result;
723 * Now we begin to parse the operands. There may be up to four
724 * of these, separated by commas, and terminated by a zero token.
727 for (opnum = 0; opnum < MAX_OPERANDS; opnum++) {
728 operand *op = &result->oprs[opnum];
729 expr *value; /* used most of the time */
730 bool mref; /* is this going to be a memory ref? */
731 bool bracket; /* is it a [] mref, or a & mref? */
732 bool mib; /* compound (mib) mref? */
733 int setsize = 0;
734 decoflags_t brace_flags = 0; /* flags for decorators in braces */
736 op->disp_size = 0; /* have to zero this whatever */
737 op->eaflags = 0; /* and this */
738 op->opflags = 0;
739 op->decoflags = 0;
741 i = stdscan(NULL, &tokval);
742 if (i == TOKEN_EOS)
743 break; /* end of operands: get out of here */
744 else if (first && i == ':') {
745 insn_is_label = true;
746 goto restart_parse;
748 first = false;
749 op->type = 0; /* so far, no override */
750 while (i == TOKEN_SPECIAL) { /* size specifiers */
751 switch ((int)tokval.t_integer) {
752 case S_BYTE:
753 if (!setsize) /* we want to use only the first */
754 op->type |= BITS8;
755 setsize = 1;
756 break;
757 case S_WORD:
758 if (!setsize)
759 op->type |= BITS16;
760 setsize = 1;
761 break;
762 case S_DWORD:
763 case S_LONG:
764 if (!setsize)
765 op->type |= BITS32;
766 setsize = 1;
767 break;
768 case S_QWORD:
769 if (!setsize)
770 op->type |= BITS64;
771 setsize = 1;
772 break;
773 case S_TWORD:
774 if (!setsize)
775 op->type |= BITS80;
776 setsize = 1;
777 break;
778 case S_OWORD:
779 if (!setsize)
780 op->type |= BITS128;
781 setsize = 1;
782 break;
783 case S_YWORD:
784 if (!setsize)
785 op->type |= BITS256;
786 setsize = 1;
787 break;
788 case S_ZWORD:
789 if (!setsize)
790 op->type |= BITS512;
791 setsize = 1;
792 break;
793 case S_TO:
794 op->type |= TO;
795 break;
796 case S_STRICT:
797 op->type |= STRICT;
798 break;
799 case S_FAR:
800 op->type |= FAR;
801 break;
802 case S_NEAR:
803 op->type |= NEAR;
804 break;
805 case S_SHORT:
806 op->type |= SHORT;
807 break;
808 default:
809 nasm_error(ERR_NONFATAL, "invalid operand size specification");
811 i = stdscan(NULL, &tokval);
814 if (i == '[' || i == '&') { /* memory reference */
815 mref = true;
816 bracket = (i == '[');
817 i = stdscan(NULL, &tokval); /* then skip the colon */
818 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
819 process_size_override(result, op);
820 i = stdscan(NULL, &tokval);
822 /* when a comma follows an opening bracket - [ , eax*4] */
823 if (i == ',') {
824 /* treat as if there is a zero displacement virtually */
825 tokval.t_type = TOKEN_NUM;
826 tokval.t_integer = 0;
827 stdscan_set(stdscan_get() - 1); /* rewind the comma */
829 } else { /* immediate operand, or register */
830 mref = false;
831 bracket = false; /* placate optimisers */
834 if ((op->type & FAR) && !mref &&
835 result->opcode != I_JMP && result->opcode != I_CALL) {
836 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
839 value = evaluate(stdscan, NULL, &tokval,
840 &op->opflags, critical, &hints);
841 i = tokval.t_type;
842 if (op->opflags & OPFLAG_FORWARD) {
843 result->forw_ref = true;
845 if (!value) /* Error in evaluator */
846 goto fail;
847 if (i == ':' && mref) { /* it was seg:offset */
849 * Process the segment override.
851 if (value[1].type != 0 ||
852 value->value != 1 ||
853 !IS_SREG(value->type))
854 nasm_error(ERR_NONFATAL, "invalid segment override");
855 else if (result->prefixes[PPS_SEG])
856 nasm_error(ERR_NONFATAL,
857 "instruction has conflicting segment overrides");
858 else {
859 result->prefixes[PPS_SEG] = value->type;
860 if (IS_FSGS(value->type))
861 op->eaflags |= EAF_FSGS;
864 i = stdscan(NULL, &tokval); /* then skip the colon */
865 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
866 process_size_override(result, op);
867 i = stdscan(NULL, &tokval);
869 value = evaluate(stdscan, NULL, &tokval,
870 &op->opflags, critical, &hints);
871 i = tokval.t_type;
872 if (op->opflags & OPFLAG_FORWARD) {
873 result->forw_ref = true;
875 /* and get the offset */
876 if (!value) /* Error in evaluator */
877 goto fail;
880 mib = false;
881 if (mref && bracket && i == ',') {
882 /* [seg:base+offset,index*scale] syntax (mib) */
884 operand o1, o2; /* Partial operands */
886 if (parse_mref(&o1, value))
887 goto fail;
889 i = stdscan(NULL, &tokval); /* Eat comma */
890 value = evaluate(stdscan, NULL, &tokval, &op->opflags,
891 critical, &hints);
892 i = tokval.t_type;
893 if (!value)
894 goto fail;
896 if (parse_mref(&o2, value))
897 goto fail;
899 if (o2.basereg != -1 && o2.indexreg == -1) {
900 o2.indexreg = o2.basereg;
901 o2.scale = 1;
902 o2.basereg = -1;
905 if (o1.indexreg != -1 || o2.basereg != -1 || o2.offset != 0 ||
906 o2.segment != NO_SEG || o2.wrt != NO_SEG) {
907 nasm_error(ERR_NONFATAL, "invalid mib expression");
908 goto fail;
911 op->basereg = o1.basereg;
912 op->indexreg = o2.indexreg;
913 op->scale = o2.scale;
914 op->offset = o1.offset;
915 op->segment = o1.segment;
916 op->wrt = o1.wrt;
918 if (op->basereg != -1) {
919 op->hintbase = op->basereg;
920 op->hinttype = EAH_MAKEBASE;
921 } else if (op->indexreg != -1) {
922 op->hintbase = op->indexreg;
923 op->hinttype = EAH_NOTBASE;
924 } else {
925 op->hintbase = -1;
926 op->hinttype = EAH_NOHINT;
929 mib = true;
932 recover = false;
933 if (mref && bracket) { /* find ] at the end */
934 if (i != ']') {
935 nasm_error(ERR_NONFATAL, "parser: expecting ]");
936 recover = true;
937 } else { /* we got the required ] */
938 i = stdscan(NULL, &tokval);
939 if ((i == TOKEN_DECORATOR) || (i == TOKEN_OPMASK)) {
941 * according to AVX512 spec, broacast or opmask decorator
942 * is expected for memory reference operands
944 if (tokval.t_flag & TFLAG_BRDCAST) {
945 brace_flags |= GEN_BRDCAST(0) |
946 VAL_BRNUM(tokval.t_integer - BRC_1TO2);
947 i = stdscan(NULL, &tokval);
948 } else if (i == TOKEN_OPMASK) {
949 brace_flags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
950 i = stdscan(NULL, &tokval);
951 } else {
952 nasm_error(ERR_NONFATAL, "broadcast or opmask "
953 "decorator expected inside braces");
954 recover = true;
958 if (i != 0 && i != ',') {
959 nasm_error(ERR_NONFATAL, "comma or end of line expected");
960 recover = true;
963 } else { /* immediate operand */
964 if (i != 0 && i != ',' && i != ':' &&
965 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
966 nasm_error(ERR_NONFATAL, "comma, colon, decorator or end of "
967 "line expected after operand");
968 recover = true;
969 } else if (i == ':') {
970 op->type |= COLON;
971 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
972 /* parse opmask (and zeroing) after an operand */
973 recover = parse_braces(&brace_flags);
976 if (recover) {
977 do { /* error recovery */
978 i = stdscan(NULL, &tokval);
979 } while (i != 0 && i != ',');
983 * now convert the exprs returned from evaluate()
984 * into operand descriptions...
986 op->decoflags |= brace_flags;
988 if (mref) { /* it's a memory reference */
989 /* A mib reference was fully parsed already */
990 if (!mib) {
991 if (parse_mref(op, value))
992 goto fail;
993 op->hintbase = hints.base;
994 op->hinttype = hints.type;
996 mref_set_optype(op);
997 } else { /* it's not a memory reference */
998 if (is_just_unknown(value)) { /* it's immediate but unknown */
999 op->type |= IMMEDIATE;
1000 op->opflags |= OPFLAG_UNKNOWN;
1001 op->offset = 0; /* don't care */
1002 op->segment = NO_SEG; /* don't care again */
1003 op->wrt = NO_SEG; /* still don't care */
1005 if(optimizing >= 0 && !(op->type & STRICT)) {
1006 /* Be optimistic */
1007 op->type |=
1008 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
1010 } else if (is_reloc(value)) { /* it's immediate */
1011 op->type |= IMMEDIATE;
1012 op->offset = reloc_value(value);
1013 op->segment = reloc_seg(value);
1014 op->wrt = reloc_wrt(value);
1016 if (is_simple(value)) {
1017 uint64_t n = reloc_value(value);
1018 if (n == 1)
1019 op->type |= UNITY;
1020 if (optimizing >= 0 &&
1021 !(op->type & STRICT)) {
1022 if ((uint32_t) (n + 128) <= 255)
1023 op->type |= SBYTEDWORD;
1024 if ((uint16_t) (n + 128) <= 255)
1025 op->type |= SBYTEWORD;
1026 if (n <= 0xFFFFFFFF)
1027 op->type |= UDWORD;
1028 if (n + 0x80000000 <= 0xFFFFFFFF)
1029 op->type |= SDWORD;
1032 } else if(value->type == EXPR_RDSAE) {
1034 * it's not an operand but a rounding or SAE decorator.
1035 * put the decorator information in the (opflag_t) type field
1036 * of previous operand.
1038 opnum--; op--;
1039 switch (value->value) {
1040 case BRC_RN:
1041 case BRC_RU:
1042 case BRC_RD:
1043 case BRC_RZ:
1044 case BRC_SAE:
1045 op->decoflags |= (value->value == BRC_SAE ? SAE : ER);
1046 result->evex_rm = value->value;
1047 break;
1048 default:
1049 nasm_error(ERR_NONFATAL, "invalid decorator");
1050 break;
1052 } else { /* it's a register */
1053 opflags_t rs;
1055 if (value->type >= EXPR_SIMPLE || value->value != 1) {
1056 nasm_error(ERR_NONFATAL, "invalid operand type");
1057 goto fail;
1061 * check that its only 1 register, not an expression...
1063 for (i = 1; value[i].type; i++)
1064 if (value[i].value) {
1065 nasm_error(ERR_NONFATAL, "invalid operand type");
1066 goto fail;
1069 /* clear overrides, except TO which applies to FPU regs */
1070 if (op->type & ~TO) {
1072 * we want to produce a warning iff the specified size
1073 * is different from the register size
1075 rs = op->type & SIZE_MASK;
1076 } else
1077 rs = 0;
1079 op->type &= TO;
1080 op->type |= REGISTER;
1081 op->type |= nasm_reg_flags[value->type];
1082 op->decoflags |= brace_flags;
1083 op->basereg = value->type;
1085 if (rs && (op->type & SIZE_MASK) != rs)
1086 nasm_error(ERR_WARNING | ERR_PASS1,
1087 "register size specification ignored");
1091 /* remember the position of operand having broadcasting/ER mode */
1092 if (op->decoflags & (BRDCAST_MASK | ER | SAE))
1093 result->evex_brerop = opnum;
1096 result->operands = opnum; /* set operand count */
1098 /* clear remaining operands */
1099 while (opnum < MAX_OPERANDS)
1100 result->oprs[opnum++].type = 0;
1103 * Transform RESW, RESD, RESQ, REST, RESO, RESY, RESZ into RESB.
1105 switch (result->opcode) {
1106 case I_RESW:
1107 result->opcode = I_RESB;
1108 result->oprs[0].offset *= 2;
1109 break;
1110 case I_RESD:
1111 result->opcode = I_RESB;
1112 result->oprs[0].offset *= 4;
1113 break;
1114 case I_RESQ:
1115 result->opcode = I_RESB;
1116 result->oprs[0].offset *= 8;
1117 break;
1118 case I_REST:
1119 result->opcode = I_RESB;
1120 result->oprs[0].offset *= 10;
1121 break;
1122 case I_RESO:
1123 result->opcode = I_RESB;
1124 result->oprs[0].offset *= 16;
1125 break;
1126 case I_RESY:
1127 result->opcode = I_RESB;
1128 result->oprs[0].offset *= 32;
1129 break;
1130 case I_RESZ:
1131 result->opcode = I_RESB;
1132 result->oprs[0].offset *= 64;
1133 break;
1134 default:
1135 break;
1138 return result;
1140 fail:
1141 result->opcode = I_none;
1142 return result;
1145 static int is_comma_next(void)
1147 struct tokenval tv;
1148 char *p;
1149 int i;
1151 p = stdscan_get();
1152 i = stdscan(NULL, &tv);
1153 stdscan_set(p);
1155 return (i == ',' || i == ';' || !i);
1158 void cleanup_insn(insn * i)
1160 extop *e;
1162 while ((e = i->eops)) {
1163 i->eops = e->next;
1164 if (e->type == EOT_DB_STRING_FREE)
1165 nasm_free(e->stringval);
1166 nasm_free(e);