Merge branch 'master' into elfmerge
[nasm.git] / parser.c
blobc5d6050f683688e3c49455c6aa546b84b491e27e
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>
45 #include <inttypes.h>
47 #include "nasm.h"
48 #include "insns.h"
49 #include "nasmlib.h"
50 #include "stdscan.h"
51 #include "eval.h"
52 #include "parser.h"
53 #include "float.h"
54 #include "tables.h"
56 extern int in_abs_seg; /* ABSOLUTE segment flag */
57 extern int32_t abs_seg; /* ABSOLUTE segment */
58 extern int32_t abs_offset; /* ABSOLUTE segment offset */
60 static int is_comma_next(void);
62 static int i;
63 static struct tokenval tokval;
65 static int prefix_slot(int prefix)
67 switch (prefix) {
68 case P_WAIT:
69 return PPS_WAIT;
70 case R_CS:
71 case R_DS:
72 case R_SS:
73 case R_ES:
74 case R_FS:
75 case R_GS:
76 return PPS_SEG;
77 case P_LOCK:
78 return PPS_LOCK;
79 case P_REP:
80 case P_REPE:
81 case P_REPZ:
82 case P_REPNE:
83 case P_REPNZ:
84 case P_XACQUIRE:
85 case P_XRELEASE:
86 case P_BND:
87 case P_NOBND:
88 return PPS_REP;
89 case P_O16:
90 case P_O32:
91 case P_O64:
92 case P_OSP:
93 return PPS_OSIZE;
94 case P_A16:
95 case P_A32:
96 case P_A64:
97 case P_ASP:
98 return PPS_ASIZE;
99 case P_EVEX:
100 case P_VEX3:
101 case P_VEX2:
102 return PPS_VEX;
103 default:
104 nasm_error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
105 return -1;
109 static void process_size_override(insn *result, operand *op)
111 if (tasm_compatible_mode) {
112 switch ((int)tokval.t_integer) {
113 /* For TASM compatibility a size override inside the
114 * brackets changes the size of the operand, not the
115 * address type of the operand as it does in standard
116 * NASM syntax. Hence:
118 * mov eax,[DWORD val]
120 * is valid syntax in TASM compatibility mode. Note that
121 * you lose the ability to override the default address
122 * type for the instruction, but we never use anything
123 * but 32-bit flat model addressing in our code.
125 case S_BYTE:
126 op->type |= BITS8;
127 break;
128 case S_WORD:
129 op->type |= BITS16;
130 break;
131 case S_DWORD:
132 case S_LONG:
133 op->type |= BITS32;
134 break;
135 case S_QWORD:
136 op->type |= BITS64;
137 break;
138 case S_TWORD:
139 op->type |= BITS80;
140 break;
141 case S_OWORD:
142 op->type |= BITS128;
143 break;
144 default:
145 nasm_error(ERR_NONFATAL,
146 "invalid operand size specification");
147 break;
149 } else {
150 /* Standard NASM compatible syntax */
151 switch ((int)tokval.t_integer) {
152 case S_NOSPLIT:
153 op->eaflags |= EAF_TIMESTWO;
154 break;
155 case S_REL:
156 op->eaflags |= EAF_REL;
157 break;
158 case S_ABS:
159 op->eaflags |= EAF_ABS;
160 break;
161 case S_BYTE:
162 op->disp_size = 8;
163 op->eaflags |= EAF_BYTEOFFS;
164 break;
165 case P_A16:
166 case P_A32:
167 case P_A64:
168 if (result->prefixes[PPS_ASIZE] &&
169 result->prefixes[PPS_ASIZE] != tokval.t_integer)
170 nasm_error(ERR_NONFATAL,
171 "conflicting address size specifications");
172 else
173 result->prefixes[PPS_ASIZE] = tokval.t_integer;
174 break;
175 case S_WORD:
176 op->disp_size = 16;
177 op->eaflags |= EAF_WORDOFFS;
178 break;
179 case S_DWORD:
180 case S_LONG:
181 op->disp_size = 32;
182 op->eaflags |= EAF_WORDOFFS;
183 break;
184 case S_QWORD:
185 op->disp_size = 64;
186 op->eaflags |= EAF_WORDOFFS;
187 break;
188 default:
189 nasm_error(ERR_NONFATAL, "invalid size specification in"
190 " effective address");
191 break;
197 * when two or more decorators follow a register operand,
198 * consecutive decorators are parsed here.
199 * opmask and zeroing decorators can be placed in any order.
200 * e.g. zmm1 {k2}{z} or zmm2 {z}{k3}
201 * decorator(s) are placed at the end of an operand.
203 static bool parse_braces(decoflags_t *decoflags)
205 int i;
206 bool recover = false;
208 i = tokval.t_type;
209 do {
210 if (i == TOKEN_OPMASK) {
211 if (*decoflags & OPMASK_MASK) {
212 nasm_error(ERR_NONFATAL, "opmask k%"PRIu64" is already set",
213 *decoflags & OPMASK_MASK);
214 *decoflags &= ~OPMASK_MASK;
216 *decoflags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
217 } else if (i == TOKEN_DECORATOR) {
218 switch (tokval.t_integer) {
219 case BRC_Z:
221 * according to AVX512 spec, only zeroing/merging decorator
222 * is supported with opmask
224 *decoflags |= GEN_Z(0);
225 break;
226 default:
227 nasm_error(ERR_NONFATAL, "{%s} is not an expected decorator",
228 tokval.t_charptr);
229 break;
231 } else if (i == ',' || i == TOKEN_EOS){
232 break;
233 } else {
234 nasm_error(ERR_NONFATAL, "only a series of valid decorators"
235 " expected");
236 recover = true;
237 break;
239 i = stdscan(NULL, &tokval);
240 } while(1);
242 return recover;
245 static int parse_mref(operand *op, const expr *e)
247 int b, i, s; /* basereg, indexreg, scale */
248 int64_t o; /* offset */
250 b = i = -1;
251 o = s = 0;
253 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
254 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
256 if (is_gpr && e->value == 1)
257 b = e->type; /* It can be basereg */
258 else /* No, it has to be indexreg */
259 i = e->type, s = e->value;
260 e++;
262 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
263 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
265 if (b != -1) /* If the first was the base, ... */
266 i = e->type, s = e->value; /* second has to be indexreg */
268 else if (!is_gpr || e->value != 1) {
269 /* If both want to be index */
270 nasm_error(ERR_NONFATAL,
271 "invalid effective address: two index registers");
272 return -1;
273 } else
274 b = e->type;
275 e++;
277 if (e->type != 0) { /* is there an offset? */
278 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
279 nasm_error(ERR_NONFATAL,
280 "beroset-p-603-invalid effective address");
281 return -1;
282 } else {
283 if (e->type == EXPR_UNKNOWN) {
284 op->opflags |= OPFLAG_UNKNOWN;
285 o = 0; /* doesn't matter what */
286 op->wrt = NO_SEG; /* nor this */
287 op->segment = NO_SEG; /* or this */
288 while (e->type)
289 e++; /* go to the end of the line */
290 } else {
291 if (e->type == EXPR_SIMPLE) {
292 o = e->value;
293 e++;
295 if (e->type == EXPR_WRT) {
296 op->wrt = e->value;
297 e++;
298 } else
299 op->wrt = NO_SEG;
301 * Look for a segment base type.
303 if (e->type && e->type < EXPR_SEGBASE) {
304 nasm_error(ERR_NONFATAL,
305 "beroset-p-630-invalid effective address");
306 return -1;
308 while (e->type && e->value == 0)
309 e++;
310 if (e->type && e->value != 1) {
311 nasm_error(ERR_NONFATAL,
312 "beroset-p-637-invalid effective address");
313 return -1;
315 if (e->type) {
316 op->segment = e->type - EXPR_SEGBASE;
317 e++;
318 } else
319 op->segment = NO_SEG;
320 while (e->type && e->value == 0)
321 e++;
322 if (e->type) {
323 nasm_error(ERR_NONFATAL,
324 "beroset-p-650-invalid effective address");
325 return -1;
329 } else {
330 o = 0;
331 op->wrt = NO_SEG;
332 op->segment = NO_SEG;
335 if (e->type != 0) { /* there'd better be nothing left! */
336 nasm_error(ERR_NONFATAL,
337 "beroset-p-663-invalid effective address");
338 return -1;
341 op->basereg = b;
342 op->indexreg = i;
343 op->scale = s;
344 op->offset = o;
345 return 0;
348 static void mref_set_optype(operand *op)
350 int b = op->basereg;
351 int i = op->indexreg;
352 int s = op->scale;
354 /* It is memory, but it can match any r/m operand */
355 op->type |= MEMORY_ANY;
357 if (b == -1 && (i == -1 || s == 0)) {
358 int is_rel = globalbits == 64 &&
359 !(op->eaflags & EAF_ABS) &&
360 ((globalrel &&
361 !(op->eaflags & EAF_FSGS)) ||
362 (op->eaflags & EAF_REL));
364 op->type |= is_rel ? IP_REL : MEM_OFFS;
367 if (i != -1) {
368 opflags_t iclass = nasm_reg_flags[i];
370 if (is_class(XMMREG,iclass))
371 op->type |= XMEM;
372 else if (is_class(YMMREG,iclass))
373 op->type |= YMEM;
374 else if (is_class(ZMMREG,iclass))
375 op->type |= ZMEM;
379 insn *parse_line(int pass, char *buffer, insn *result, ldfunc ldef)
381 bool insn_is_label = false;
382 struct eval_hints hints;
383 int opnum;
384 int critical;
385 bool first;
386 bool recover;
388 restart_parse:
389 first = true;
390 result->forw_ref = false;
392 stdscan_reset();
393 stdscan_set(buffer);
394 i = stdscan(NULL, &tokval);
396 result->label = NULL; /* Assume no label */
397 result->eops = NULL; /* must do this, whatever happens */
398 result->operands = 0; /* must initialize this */
399 result->evex_rm = 0; /* Ensure EVEX rounding mode is reset */
400 result->evex_brerop = -1; /* Reset EVEX broadcasting/ER op position */
402 /* Ignore blank lines */
403 if (i == TOKEN_EOS)
404 goto fail;
406 if (i != TOKEN_ID &&
407 i != TOKEN_INSN &&
408 i != TOKEN_PREFIX &&
409 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
410 nasm_error(ERR_NONFATAL,
411 "label or instruction expected at start of line");
412 goto fail;
415 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
416 /* there's a label here */
417 first = false;
418 result->label = tokval.t_charptr;
419 i = stdscan(NULL, &tokval);
420 if (i == ':') { /* skip over the optional colon */
421 i = stdscan(NULL, &tokval);
422 } else if (i == 0) {
423 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
424 "label alone on a line without a colon might be in error");
426 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
428 * FIXME: location.segment could be NO_SEG, in which case
429 * it is possible we should be passing 'abs_seg'. Look into this.
430 * Work out whether that is *really* what we should be doing.
431 * Generally fix things. I think this is right as it is, but
432 * am still not certain.
434 ldef(result->label, in_abs_seg ? abs_seg : location.segment,
435 location.offset, NULL, true, false);
439 /* Just a label here */
440 if (i == TOKEN_EOS)
441 goto fail;
443 nasm_build_assert(P_none != 0);
444 memset(result->prefixes, P_none, sizeof(result->prefixes));
445 result->times = 1L;
447 while (i == TOKEN_PREFIX ||
448 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
449 first = false;
452 * Handle special case: the TIMES prefix.
454 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
455 expr *value;
457 i = stdscan(NULL, &tokval);
458 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, NULL);
459 i = tokval.t_type;
460 if (!value) /* Error in evaluator */
461 goto fail;
462 if (!is_simple(value)) {
463 nasm_error(ERR_NONFATAL,
464 "non-constant argument supplied to TIMES");
465 result->times = 1L;
466 } else {
467 result->times = value->value;
468 if (value->value < 0 && pass0 == 2) {
469 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
470 value->value);
471 result->times = 0;
474 } else {
475 int slot = prefix_slot(tokval.t_integer);
476 if (result->prefixes[slot]) {
477 if (result->prefixes[slot] == tokval.t_integer)
478 nasm_error(ERR_WARNING | ERR_PASS1,
479 "instruction has redundant prefixes");
480 else
481 nasm_error(ERR_NONFATAL,
482 "instruction has conflicting prefixes");
484 result->prefixes[slot] = tokval.t_integer;
485 i = stdscan(NULL, &tokval);
489 if (i != TOKEN_INSN) {
490 int j;
491 enum prefixes pfx;
493 for (j = 0; j < MAXPREFIX; j++) {
494 if ((pfx = result->prefixes[j]) != P_none)
495 break;
498 if (i == 0 && pfx != P_none) {
500 * Instruction prefixes are present, but no actual
501 * instruction. This is allowed: at this point we
502 * invent a notional instruction of RESB 0.
504 result->opcode = I_RESB;
505 result->operands = 1;
506 result->oprs[0].type = IMMEDIATE;
507 result->oprs[0].offset = 0L;
508 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
509 return result;
510 } else {
511 nasm_error(ERR_NONFATAL, "parser: instruction expected");
512 goto fail;
516 result->opcode = tokval.t_integer;
517 result->condition = tokval.t_inttwo;
520 * INCBIN cannot be satisfied with incorrectly
521 * evaluated operands, since the correct values _must_ be known
522 * on the first pass. Hence, even in pass one, we set the
523 * `critical' flag on calling evaluate(), so that it will bomb
524 * out on undefined symbols.
526 if (result->opcode == I_INCBIN) {
527 critical = (pass0 < 2 ? 1 : 2);
529 } else
530 critical = (pass == 2 ? 2 : 0);
532 if (result->opcode == I_DB || result->opcode == I_DW ||
533 result->opcode == I_DD || result->opcode == I_DQ ||
534 result->opcode == I_DT || result->opcode == I_DO ||
535 result->opcode == I_DY || result->opcode == I_DZ ||
536 result->opcode == I_INCBIN) {
537 extop *eop, **tail = &result->eops, **fixptr;
538 int oper_num = 0;
539 int32_t sign;
541 result->eops_float = false;
544 * Begin to read the DB/DW/DD/DQ/DT/DO/DY/DZ/INCBIN operands.
546 while (1) {
547 i = stdscan(NULL, &tokval);
548 if (i == TOKEN_EOS)
549 break;
550 else if (first && i == ':') {
551 insn_is_label = true;
552 goto restart_parse;
554 first = false;
555 fixptr = tail;
556 eop = *tail = nasm_malloc(sizeof(extop));
557 tail = &eop->next;
558 eop->next = NULL;
559 eop->type = EOT_NOTHING;
560 oper_num++;
561 sign = +1;
564 * is_comma_next() here is to distinguish this from
565 * a string used as part of an expression...
567 if (i == TOKEN_STR && is_comma_next()) {
568 eop->type = EOT_DB_STRING;
569 eop->stringval = tokval.t_charptr;
570 eop->stringlen = tokval.t_inttwo;
571 i = stdscan(NULL, &tokval); /* eat the comma */
572 } else if (i == TOKEN_STRFUNC) {
573 bool parens = false;
574 const char *funcname = tokval.t_charptr;
575 enum strfunc func = tokval.t_integer;
576 i = stdscan(NULL, &tokval);
577 if (i == '(') {
578 parens = true;
579 i = stdscan(NULL, &tokval);
581 if (i != TOKEN_STR) {
582 nasm_error(ERR_NONFATAL,
583 "%s must be followed by a string constant",
584 funcname);
585 eop->type = EOT_NOTHING;
586 } else {
587 eop->type = EOT_DB_STRING_FREE;
588 eop->stringlen =
589 string_transform(tokval.t_charptr, tokval.t_inttwo,
590 &eop->stringval, func);
591 if (eop->stringlen == (size_t)-1) {
592 nasm_error(ERR_NONFATAL, "invalid string for transform");
593 eop->type = EOT_NOTHING;
596 if (parens && i && i != ')') {
597 i = stdscan(NULL, &tokval);
598 if (i != ')') {
599 nasm_error(ERR_NONFATAL, "unterminated %s function",
600 funcname);
603 if (i && i != ',')
604 i = stdscan(NULL, &tokval);
605 } else if (i == '-' || i == '+') {
606 char *save = stdscan_get();
607 int token = i;
608 sign = (i == '-') ? -1 : 1;
609 i = stdscan(NULL, &tokval);
610 if (i != TOKEN_FLOAT) {
611 stdscan_set(save);
612 i = tokval.t_type = token;
613 goto is_expression;
614 } else {
615 goto is_float;
617 } else if (i == TOKEN_FLOAT) {
618 is_float:
619 eop->type = EOT_DB_STRING;
620 result->eops_float = true;
622 eop->stringlen = idata_bytes(result->opcode);
623 if (eop->stringlen > 16) {
624 nasm_error(ERR_NONFATAL, "floating-point constant"
625 " encountered in DY or DZ instruction");
626 eop->stringlen = 0;
627 } else if (eop->stringlen < 1) {
628 nasm_error(ERR_NONFATAL, "floating-point constant"
629 " encountered in unknown instruction");
631 * fix suggested by Pedro Gimeno... original line was:
632 * eop->type = EOT_NOTHING;
634 eop->stringlen = 0;
637 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
638 tail = &eop->next;
639 *fixptr = eop;
640 eop->stringval = (char *)eop + sizeof(extop);
641 if (!eop->stringlen ||
642 !float_const(tokval.t_charptr, sign,
643 (uint8_t *)eop->stringval, eop->stringlen))
644 eop->type = EOT_NOTHING;
645 i = stdscan(NULL, &tokval); /* eat the comma */
646 } else {
647 /* anything else, assume it is an expression */
648 expr *value;
650 is_expression:
651 value = evaluate(stdscan, NULL, &tokval, NULL,
652 critical, NULL);
653 i = tokval.t_type;
654 if (!value) /* Error in evaluator */
655 goto fail;
656 if (is_unknown(value)) {
657 eop->type = EOT_DB_NUMBER;
658 eop->offset = 0; /* doesn't matter what we put */
659 eop->segment = eop->wrt = NO_SEG; /* likewise */
660 } else if (is_reloc(value)) {
661 eop->type = EOT_DB_NUMBER;
662 eop->offset = reloc_value(value);
663 eop->segment = reloc_seg(value);
664 eop->wrt = reloc_wrt(value);
665 } else {
666 nasm_error(ERR_NONFATAL,
667 "operand %d: expression is not simple"
668 " or relocatable", oper_num);
673 * We're about to call stdscan(), which will eat the
674 * comma that we're currently sitting on between
675 * arguments. However, we'd better check first that it
676 * _is_ a comma.
678 if (i == TOKEN_EOS) /* also could be EOL */
679 break;
680 if (i != ',') {
681 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
682 oper_num);
683 goto fail;
687 if (result->opcode == I_INCBIN) {
689 * Correct syntax for INCBIN is that there should be
690 * one string operand, followed by one or two numeric
691 * operands.
693 if (!result->eops || result->eops->type != EOT_DB_STRING)
694 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
695 else if (result->eops->next &&
696 result->eops->next->type != EOT_DB_NUMBER)
697 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
698 " non-numeric");
699 else if (result->eops->next && result->eops->next->next &&
700 result->eops->next->next->type != EOT_DB_NUMBER)
701 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
702 " non-numeric");
703 else if (result->eops->next && result->eops->next->next &&
704 result->eops->next->next->next)
705 nasm_error(ERR_NONFATAL,
706 "`incbin': more than three parameters");
707 else
708 return result;
710 * If we reach here, one of the above errors happened.
711 * Throw the instruction away.
713 goto fail;
714 } else /* DB ... */ if (oper_num == 0)
715 nasm_error(ERR_WARNING | ERR_PASS1,
716 "no operand for data declaration");
717 else
718 result->operands = oper_num;
720 return result;
724 * Now we begin to parse the operands. There may be up to four
725 * of these, separated by commas, and terminated by a zero token.
728 for (opnum = 0; opnum < MAX_OPERANDS; opnum++) {
729 operand *op = &result->oprs[opnum];
730 expr *value; /* used most of the time */
731 bool mref; /* is this going to be a memory ref? */
732 bool bracket; /* is it a [] mref, or a & mref? */
733 bool mib; /* compound (mib) mref? */
734 int setsize = 0;
735 decoflags_t brace_flags = 0; /* flags for decorators in braces */
737 op->disp_size = 0; /* have to zero this whatever */
738 op->eaflags = 0; /* and this */
739 op->opflags = 0;
740 op->decoflags = 0;
742 i = stdscan(NULL, &tokval);
743 if (i == TOKEN_EOS)
744 break; /* end of operands: get out of here */
745 else if (first && i == ':') {
746 insn_is_label = true;
747 goto restart_parse;
749 first = false;
750 op->type = 0; /* so far, no override */
751 while (i == TOKEN_SPECIAL) { /* size specifiers */
752 switch ((int)tokval.t_integer) {
753 case S_BYTE:
754 if (!setsize) /* we want to use only the first */
755 op->type |= BITS8;
756 setsize = 1;
757 break;
758 case S_WORD:
759 if (!setsize)
760 op->type |= BITS16;
761 setsize = 1;
762 break;
763 case S_DWORD:
764 case S_LONG:
765 if (!setsize)
766 op->type |= BITS32;
767 setsize = 1;
768 break;
769 case S_QWORD:
770 if (!setsize)
771 op->type |= BITS64;
772 setsize = 1;
773 break;
774 case S_TWORD:
775 if (!setsize)
776 op->type |= BITS80;
777 setsize = 1;
778 break;
779 case S_OWORD:
780 if (!setsize)
781 op->type |= BITS128;
782 setsize = 1;
783 break;
784 case S_YWORD:
785 if (!setsize)
786 op->type |= BITS256;
787 setsize = 1;
788 break;
789 case S_ZWORD:
790 if (!setsize)
791 op->type |= BITS512;
792 setsize = 1;
793 break;
794 case S_TO:
795 op->type |= TO;
796 break;
797 case S_STRICT:
798 op->type |= STRICT;
799 break;
800 case S_FAR:
801 op->type |= FAR;
802 break;
803 case S_NEAR:
804 op->type |= NEAR;
805 break;
806 case S_SHORT:
807 op->type |= SHORT;
808 break;
809 default:
810 nasm_error(ERR_NONFATAL, "invalid operand size specification");
812 i = stdscan(NULL, &tokval);
815 if (i == '[' || i == '&') { /* memory reference */
816 mref = true;
817 bracket = (i == '[');
818 i = stdscan(NULL, &tokval); /* then skip the colon */
819 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
820 process_size_override(result, op);
821 i = stdscan(NULL, &tokval);
823 /* when a comma follows an opening bracket - [ , eax*4] */
824 if (i == ',') {
825 /* treat as if there is a zero displacement virtually */
826 tokval.t_type = TOKEN_NUM;
827 tokval.t_integer = 0;
828 stdscan_set(stdscan_get() - 1); /* rewind the comma */
830 } else { /* immediate operand, or register */
831 mref = false;
832 bracket = false; /* placate optimisers */
835 if ((op->type & FAR) && !mref &&
836 result->opcode != I_JMP && result->opcode != I_CALL) {
837 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
840 value = evaluate(stdscan, NULL, &tokval,
841 &op->opflags, critical, &hints);
842 i = tokval.t_type;
843 if (op->opflags & OPFLAG_FORWARD) {
844 result->forw_ref = true;
846 if (!value) /* Error in evaluator */
847 goto fail;
848 if (i == ':' && mref) { /* it was seg:offset */
850 * Process the segment override.
852 if (value[1].type != 0 ||
853 value->value != 1 ||
854 !IS_SREG(value->type))
855 nasm_error(ERR_NONFATAL, "invalid segment override");
856 else if (result->prefixes[PPS_SEG])
857 nasm_error(ERR_NONFATAL,
858 "instruction has conflicting segment overrides");
859 else {
860 result->prefixes[PPS_SEG] = value->type;
861 if (IS_FSGS(value->type))
862 op->eaflags |= EAF_FSGS;
865 i = stdscan(NULL, &tokval); /* then skip the colon */
866 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
867 process_size_override(result, op);
868 i = stdscan(NULL, &tokval);
870 value = evaluate(stdscan, NULL, &tokval,
871 &op->opflags, critical, &hints);
872 i = tokval.t_type;
873 if (op->opflags & OPFLAG_FORWARD) {
874 result->forw_ref = true;
876 /* and get the offset */
877 if (!value) /* Error in evaluator */
878 goto fail;
881 mib = false;
882 if (mref && bracket && i == ',') {
883 /* [seg:base+offset,index*scale] syntax (mib) */
885 operand o1, o2; /* Partial operands */
887 if (parse_mref(&o1, value))
888 goto fail;
890 i = stdscan(NULL, &tokval); /* Eat comma */
891 value = evaluate(stdscan, NULL, &tokval, &op->opflags,
892 critical, &hints);
893 i = tokval.t_type;
894 if (!value)
895 goto fail;
897 if (parse_mref(&o2, value))
898 goto fail;
900 if (o2.basereg != -1 && o2.indexreg == -1) {
901 o2.indexreg = o2.basereg;
902 o2.scale = 1;
903 o2.basereg = -1;
906 if (o1.indexreg != -1 || o2.basereg != -1 || o2.offset != 0 ||
907 o2.segment != NO_SEG || o2.wrt != NO_SEG) {
908 nasm_error(ERR_NONFATAL, "invalid mib expression");
909 goto fail;
912 op->basereg = o1.basereg;
913 op->indexreg = o2.indexreg;
914 op->scale = o2.scale;
915 op->offset = o1.offset;
916 op->segment = o1.segment;
917 op->wrt = o1.wrt;
919 if (op->basereg != -1) {
920 op->hintbase = op->basereg;
921 op->hinttype = EAH_MAKEBASE;
922 } else if (op->indexreg != -1) {
923 op->hintbase = op->indexreg;
924 op->hinttype = EAH_NOTBASE;
925 } else {
926 op->hintbase = -1;
927 op->hinttype = EAH_NOHINT;
930 mib = true;
933 recover = false;
934 if (mref && bracket) { /* find ] at the end */
935 if (i != ']') {
936 nasm_error(ERR_NONFATAL, "parser: expecting ]");
937 recover = true;
938 } else { /* we got the required ] */
939 i = stdscan(NULL, &tokval);
940 if ((i == TOKEN_DECORATOR) || (i == TOKEN_OPMASK)) {
942 * according to AVX512 spec, broacast or opmask decorator
943 * is expected for memory reference operands
945 if (tokval.t_flag & TFLAG_BRDCAST) {
946 brace_flags |= GEN_BRDCAST(0) |
947 VAL_BRNUM(tokval.t_integer - BRC_1TO2);
948 i = stdscan(NULL, &tokval);
949 } else if (i == TOKEN_OPMASK) {
950 brace_flags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
951 i = stdscan(NULL, &tokval);
952 } else {
953 nasm_error(ERR_NONFATAL, "broadcast or opmask "
954 "decorator expected inside braces");
955 recover = true;
959 if (i != 0 && i != ',') {
960 nasm_error(ERR_NONFATAL, "comma or end of line expected");
961 recover = true;
964 } else { /* immediate operand */
965 if (i != 0 && i != ',' && i != ':' &&
966 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
967 nasm_error(ERR_NONFATAL, "comma, colon, decorator or end of "
968 "line expected after operand");
969 recover = true;
970 } else if (i == ':') {
971 op->type |= COLON;
972 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
973 /* parse opmask (and zeroing) after an operand */
974 recover = parse_braces(&brace_flags);
977 if (recover) {
978 do { /* error recovery */
979 i = stdscan(NULL, &tokval);
980 } while (i != 0 && i != ',');
984 * now convert the exprs returned from evaluate()
985 * into operand descriptions...
987 op->decoflags |= brace_flags;
989 if (mref) { /* it's a memory reference */
990 /* A mib reference was fully parsed already */
991 if (!mib) {
992 if (parse_mref(op, value))
993 goto fail;
994 op->hintbase = hints.base;
995 op->hinttype = hints.type;
997 mref_set_optype(op);
998 } else { /* it's not a memory reference */
999 if (is_just_unknown(value)) { /* it's immediate but unknown */
1000 op->type |= IMMEDIATE;
1001 op->opflags |= OPFLAG_UNKNOWN;
1002 op->offset = 0; /* don't care */
1003 op->segment = NO_SEG; /* don't care again */
1004 op->wrt = NO_SEG; /* still don't care */
1006 if(optimizing >= 0 && !(op->type & STRICT)) {
1007 /* Be optimistic */
1008 op->type |=
1009 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
1011 } else if (is_reloc(value)) { /* it's immediate */
1012 op->type |= IMMEDIATE;
1013 op->offset = reloc_value(value);
1014 op->segment = reloc_seg(value);
1015 op->wrt = reloc_wrt(value);
1017 if (is_simple(value)) {
1018 uint64_t n = reloc_value(value);
1019 if (n == 1)
1020 op->type |= UNITY;
1021 if (optimizing >= 0 &&
1022 !(op->type & STRICT)) {
1023 if ((uint32_t) (n + 128) <= 255)
1024 op->type |= SBYTEDWORD;
1025 if ((uint16_t) (n + 128) <= 255)
1026 op->type |= SBYTEWORD;
1027 if (n <= 0xFFFFFFFF)
1028 op->type |= UDWORD;
1029 if (n + 0x80000000 <= 0xFFFFFFFF)
1030 op->type |= SDWORD;
1033 } else if(value->type == EXPR_RDSAE) {
1035 * it's not an operand but a rounding or SAE decorator.
1036 * put the decorator information in the (opflag_t) type field
1037 * of previous operand.
1039 opnum--; op--;
1040 switch (value->value) {
1041 case BRC_RN:
1042 case BRC_RU:
1043 case BRC_RD:
1044 case BRC_RZ:
1045 case BRC_SAE:
1046 op->decoflags |= (value->value == BRC_SAE ? SAE : ER);
1047 result->evex_rm = value->value;
1048 break;
1049 default:
1050 nasm_error(ERR_NONFATAL, "invalid decorator");
1051 break;
1053 } else { /* it's a register */
1054 opflags_t rs;
1056 if (value->type >= EXPR_SIMPLE || value->value != 1) {
1057 nasm_error(ERR_NONFATAL, "invalid operand type");
1058 goto fail;
1062 * check that its only 1 register, not an expression...
1064 for (i = 1; value[i].type; i++)
1065 if (value[i].value) {
1066 nasm_error(ERR_NONFATAL, "invalid operand type");
1067 goto fail;
1070 /* clear overrides, except TO which applies to FPU regs */
1071 if (op->type & ~TO) {
1073 * we want to produce a warning iff the specified size
1074 * is different from the register size
1076 rs = op->type & SIZE_MASK;
1077 } else
1078 rs = 0;
1080 op->type &= TO;
1081 op->type |= REGISTER;
1082 op->type |= nasm_reg_flags[value->type];
1083 op->decoflags |= brace_flags;
1084 op->basereg = value->type;
1086 if (rs && (op->type & SIZE_MASK) != rs)
1087 nasm_error(ERR_WARNING | ERR_PASS1,
1088 "register size specification ignored");
1092 /* remember the position of operand having broadcasting/ER mode */
1093 if (op->decoflags & (BRDCAST_MASK | ER | SAE))
1094 result->evex_brerop = opnum;
1097 result->operands = opnum; /* set operand count */
1099 /* clear remaining operands */
1100 while (opnum < MAX_OPERANDS)
1101 result->oprs[opnum++].type = 0;
1104 * Transform RESW, RESD, RESQ, REST, RESO, RESY, RESZ into RESB.
1106 switch (result->opcode) {
1107 case I_RESW:
1108 result->opcode = I_RESB;
1109 result->oprs[0].offset *= 2;
1110 break;
1111 case I_RESD:
1112 result->opcode = I_RESB;
1113 result->oprs[0].offset *= 4;
1114 break;
1115 case I_RESQ:
1116 result->opcode = I_RESB;
1117 result->oprs[0].offset *= 8;
1118 break;
1119 case I_REST:
1120 result->opcode = I_RESB;
1121 result->oprs[0].offset *= 10;
1122 break;
1123 case I_RESO:
1124 result->opcode = I_RESB;
1125 result->oprs[0].offset *= 16;
1126 break;
1127 case I_RESY:
1128 result->opcode = I_RESB;
1129 result->oprs[0].offset *= 32;
1130 break;
1131 case I_RESZ:
1132 result->opcode = I_RESB;
1133 result->oprs[0].offset *= 64;
1134 break;
1135 default:
1136 break;
1139 return result;
1141 fail:
1142 result->opcode = I_none;
1143 return result;
1146 static int is_comma_next(void)
1148 struct tokenval tv;
1149 char *p;
1150 int i;
1152 p = stdscan_get();
1153 i = stdscan(NULL, &tv);
1154 stdscan_set(p);
1156 return (i == ',' || i == ';' || !i);
1159 void cleanup_insn(insn * i)
1161 extop *e;
1163 while ((e = i->eops)) {
1164 i->eops = e->next;
1165 if (e->type == EOT_DB_STRING_FREE)
1166 nasm_free(e->stringval);
1167 nasm_free(e);