output: outmac -- Fix few nits during merge
[nasm.git] / parser.c
blob423d7855ff8b17ea69d16360c93e202e1efbe69d
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;
64 static struct location *location; /* Pointer to current line's segment,offset */
66 void parser_global_info(struct location * locp)
68 location = locp;
71 static int prefix_slot(int prefix)
73 switch (prefix) {
74 case P_WAIT:
75 return PPS_WAIT;
76 case R_CS:
77 case R_DS:
78 case R_SS:
79 case R_ES:
80 case R_FS:
81 case R_GS:
82 return PPS_SEG;
83 case P_LOCK:
84 return PPS_LOCK;
85 case P_REP:
86 case P_REPE:
87 case P_REPZ:
88 case P_REPNE:
89 case P_REPNZ:
90 case P_XACQUIRE:
91 case P_XRELEASE:
92 case P_BND:
93 case P_NOBND:
94 return PPS_REP;
95 case P_O16:
96 case P_O32:
97 case P_O64:
98 case P_OSP:
99 return PPS_OSIZE;
100 case P_A16:
101 case P_A32:
102 case P_A64:
103 case P_ASP:
104 return PPS_ASIZE;
105 case P_EVEX:
106 case P_VEX3:
107 case P_VEX2:
108 return PPS_VEX;
109 default:
110 nasm_error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
111 return -1;
115 static void process_size_override(insn *result, operand *op)
117 if (tasm_compatible_mode) {
118 switch ((int)tokval.t_integer) {
119 /* For TASM compatibility a size override inside the
120 * brackets changes the size of the operand, not the
121 * address type of the operand as it does in standard
122 * NASM syntax. Hence:
124 * mov eax,[DWORD val]
126 * is valid syntax in TASM compatibility mode. Note that
127 * you lose the ability to override the default address
128 * type for the instruction, but we never use anything
129 * but 32-bit flat model addressing in our code.
131 case S_BYTE:
132 op->type |= BITS8;
133 break;
134 case S_WORD:
135 op->type |= BITS16;
136 break;
137 case S_DWORD:
138 case S_LONG:
139 op->type |= BITS32;
140 break;
141 case S_QWORD:
142 op->type |= BITS64;
143 break;
144 case S_TWORD:
145 op->type |= BITS80;
146 break;
147 case S_OWORD:
148 op->type |= BITS128;
149 break;
150 default:
151 nasm_error(ERR_NONFATAL,
152 "invalid operand size specification");
153 break;
155 } else {
156 /* Standard NASM compatible syntax */
157 switch ((int)tokval.t_integer) {
158 case S_NOSPLIT:
159 op->eaflags |= EAF_TIMESTWO;
160 break;
161 case S_REL:
162 op->eaflags |= EAF_REL;
163 break;
164 case S_ABS:
165 op->eaflags |= EAF_ABS;
166 break;
167 case S_BYTE:
168 op->disp_size = 8;
169 op->eaflags |= EAF_BYTEOFFS;
170 break;
171 case P_A16:
172 case P_A32:
173 case P_A64:
174 if (result->prefixes[PPS_ASIZE] &&
175 result->prefixes[PPS_ASIZE] != tokval.t_integer)
176 nasm_error(ERR_NONFATAL,
177 "conflicting address size specifications");
178 else
179 result->prefixes[PPS_ASIZE] = tokval.t_integer;
180 break;
181 case S_WORD:
182 op->disp_size = 16;
183 op->eaflags |= EAF_WORDOFFS;
184 break;
185 case S_DWORD:
186 case S_LONG:
187 op->disp_size = 32;
188 op->eaflags |= EAF_WORDOFFS;
189 break;
190 case S_QWORD:
191 op->disp_size = 64;
192 op->eaflags |= EAF_WORDOFFS;
193 break;
194 default:
195 nasm_error(ERR_NONFATAL, "invalid size specification in"
196 " effective address");
197 break;
203 * when two or more decorators follow a register operand,
204 * consecutive decorators are parsed here.
205 * opmask and zeroing decorators can be placed in any order.
206 * e.g. zmm1 {k2}{z} or zmm2 {z}{k3}
207 * decorator(s) are placed at the end of an operand.
209 static bool parse_braces(decoflags_t *decoflags)
211 int i;
212 bool recover = false;
214 i = tokval.t_type;
215 do {
216 if (i == TOKEN_OPMASK) {
217 if (*decoflags & OPMASK_MASK) {
218 nasm_error(ERR_NONFATAL, "opmask k%"PRIu64" is already set",
219 *decoflags & OPMASK_MASK);
220 *decoflags &= ~OPMASK_MASK;
222 *decoflags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
223 } else if (i == TOKEN_DECORATOR) {
224 switch (tokval.t_integer) {
225 case BRC_Z:
227 * according to AVX512 spec, only zeroing/merging decorator
228 * is supported with opmask
230 *decoflags |= GEN_Z(0);
231 break;
232 default:
233 nasm_error(ERR_NONFATAL, "{%s} is not an expected decorator",
234 tokval.t_charptr);
235 break;
237 } else if (i == ',' || i == TOKEN_EOS){
238 break;
239 } else {
240 nasm_error(ERR_NONFATAL, "only a series of valid decorators"
241 " expected");
242 recover = true;
243 break;
245 i = stdscan(NULL, &tokval);
246 } while(1);
248 return recover;
251 static int parse_mref(operand *op, const expr *e)
253 int b, i, s; /* basereg, indexreg, scale */
254 int64_t o; /* offset */
256 b = i = -1;
257 o = s = 0;
259 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
260 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
262 if (is_gpr && e->value == 1)
263 b = e->type; /* It can be basereg */
264 else /* No, it has to be indexreg */
265 i = e->type, s = e->value;
266 e++;
268 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
269 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
271 if (b != -1) /* If the first was the base, ... */
272 i = e->type, s = e->value; /* second has to be indexreg */
274 else if (!is_gpr || e->value != 1) {
275 /* If both want to be index */
276 nasm_error(ERR_NONFATAL,
277 "invalid effective address: two index registers");
278 return -1;
279 } else
280 b = e->type;
281 e++;
283 if (e->type != 0) { /* is there an offset? */
284 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
285 nasm_error(ERR_NONFATAL,
286 "beroset-p-603-invalid effective address");
287 return -1;
288 } else {
289 if (e->type == EXPR_UNKNOWN) {
290 op->opflags |= OPFLAG_UNKNOWN;
291 o = 0; /* doesn't matter what */
292 op->wrt = NO_SEG; /* nor this */
293 op->segment = NO_SEG; /* or this */
294 while (e->type)
295 e++; /* go to the end of the line */
296 } else {
297 if (e->type == EXPR_SIMPLE) {
298 o = e->value;
299 e++;
301 if (e->type == EXPR_WRT) {
302 op->wrt = e->value;
303 e++;
304 } else
305 op->wrt = NO_SEG;
307 * Look for a segment base type.
309 if (e->type && e->type < EXPR_SEGBASE) {
310 nasm_error(ERR_NONFATAL,
311 "beroset-p-630-invalid effective address");
312 return -1;
314 while (e->type && e->value == 0)
315 e++;
316 if (e->type && e->value != 1) {
317 nasm_error(ERR_NONFATAL,
318 "beroset-p-637-invalid effective address");
319 return -1;
321 if (e->type) {
322 op->segment = e->type - EXPR_SEGBASE;
323 e++;
324 } else
325 op->segment = NO_SEG;
326 while (e->type && e->value == 0)
327 e++;
328 if (e->type) {
329 nasm_error(ERR_NONFATAL,
330 "beroset-p-650-invalid effective address");
331 return -1;
335 } else {
336 o = 0;
337 op->wrt = NO_SEG;
338 op->segment = NO_SEG;
341 if (e->type != 0) { /* there'd better be nothing left! */
342 nasm_error(ERR_NONFATAL,
343 "beroset-p-663-invalid effective address");
344 return -1;
347 op->basereg = b;
348 op->indexreg = i;
349 op->scale = s;
350 op->offset = o;
351 return 0;
354 static void mref_set_optype(operand *op)
356 int b = op->basereg;
357 int i = op->indexreg;
358 int s = op->scale;
360 /* It is memory, but it can match any r/m operand */
361 op->type |= MEMORY_ANY;
363 if (b == -1 && (i == -1 || s == 0)) {
364 int is_rel = globalbits == 64 &&
365 !(op->eaflags & EAF_ABS) &&
366 ((globalrel &&
367 !(op->eaflags & EAF_FSGS)) ||
368 (op->eaflags & EAF_REL));
370 op->type |= is_rel ? IP_REL : MEM_OFFS;
373 if (i != -1) {
374 opflags_t iclass = nasm_reg_flags[i];
376 if (is_class(XMMREG,iclass))
377 op->type |= XMEM;
378 else if (is_class(YMMREG,iclass))
379 op->type |= YMEM;
380 else if (is_class(ZMMREG,iclass))
381 op->type |= ZMEM;
385 insn *parse_line(int pass, char *buffer, insn *result, ldfunc ldef)
387 bool insn_is_label = false;
388 struct eval_hints hints;
389 int opnum;
390 int critical;
391 bool first;
392 bool recover;
394 restart_parse:
395 first = true;
396 result->forw_ref = false;
398 stdscan_reset();
399 stdscan_set(buffer);
400 i = stdscan(NULL, &tokval);
402 result->label = NULL; /* Assume no label */
403 result->eops = NULL; /* must do this, whatever happens */
404 result->operands = 0; /* must initialize this */
405 result->evex_rm = 0; /* Ensure EVEX rounding mode is reset */
406 result->evex_brerop = -1; /* Reset EVEX broadcasting/ER op position */
408 /* Ignore blank lines */
409 if (i == TOKEN_EOS)
410 goto fail;
412 if (i != TOKEN_ID &&
413 i != TOKEN_INSN &&
414 i != TOKEN_PREFIX &&
415 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
416 nasm_error(ERR_NONFATAL,
417 "label or instruction expected at start of line");
418 goto fail;
421 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
422 /* there's a label here */
423 first = false;
424 result->label = tokval.t_charptr;
425 i = stdscan(NULL, &tokval);
426 if (i == ':') { /* skip over the optional colon */
427 i = stdscan(NULL, &tokval);
428 } else if (i == 0) {
429 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
430 "label alone on a line without a colon might be in error");
432 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
434 * FIXME: location->segment could be NO_SEG, in which case
435 * it is possible we should be passing 'abs_seg'. Look into this.
436 * Work out whether that is *really* what we should be doing.
437 * Generally fix things. I think this is right as it is, but
438 * am still not certain.
440 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
441 location->offset, NULL, true, false);
445 /* Just a label here */
446 if (i == TOKEN_EOS)
447 goto fail;
449 nasm_build_assert(P_none != 0);
450 memset(result->prefixes, P_none, sizeof(result->prefixes));
451 result->times = 1L;
453 while (i == TOKEN_PREFIX ||
454 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
455 first = false;
458 * Handle special case: the TIMES prefix.
460 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
461 expr *value;
463 i = stdscan(NULL, &tokval);
464 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
465 i = tokval.t_type;
466 if (!value) /* Error in evaluator */
467 goto fail;
468 if (!is_simple(value)) {
469 nasm_error(ERR_NONFATAL,
470 "non-constant argument supplied to TIMES");
471 result->times = 1L;
472 } else {
473 result->times = value->value;
474 if (value->value < 0 && pass0 == 2) {
475 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
476 value->value);
477 result->times = 0;
480 } else {
481 int slot = prefix_slot(tokval.t_integer);
482 if (result->prefixes[slot]) {
483 if (result->prefixes[slot] == tokval.t_integer)
484 nasm_error(ERR_WARNING | ERR_PASS1,
485 "instruction has redundant prefixes");
486 else
487 nasm_error(ERR_NONFATAL,
488 "instruction has conflicting prefixes");
490 result->prefixes[slot] = tokval.t_integer;
491 i = stdscan(NULL, &tokval);
495 if (i != TOKEN_INSN) {
496 int j;
497 enum prefixes pfx;
499 for (j = 0; j < MAXPREFIX; j++) {
500 if ((pfx = result->prefixes[j]) != P_none)
501 break;
504 if (i == 0 && pfx != P_none) {
506 * Instruction prefixes are present, but no actual
507 * instruction. This is allowed: at this point we
508 * invent a notional instruction of RESB 0.
510 result->opcode = I_RESB;
511 result->operands = 1;
512 result->oprs[0].type = IMMEDIATE;
513 result->oprs[0].offset = 0L;
514 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
515 return result;
516 } else {
517 nasm_error(ERR_NONFATAL, "parser: instruction expected");
518 goto fail;
522 result->opcode = tokval.t_integer;
523 result->condition = tokval.t_inttwo;
526 * INCBIN cannot be satisfied with incorrectly
527 * evaluated operands, since the correct values _must_ be known
528 * on the first pass. Hence, even in pass one, we set the
529 * `critical' flag on calling evaluate(), so that it will bomb
530 * out on undefined symbols.
532 if (result->opcode == I_INCBIN) {
533 critical = (pass0 < 2 ? 1 : 2);
535 } else
536 critical = (pass == 2 ? 2 : 0);
538 if (result->opcode == I_DB || result->opcode == I_DW ||
539 result->opcode == I_DD || result->opcode == I_DQ ||
540 result->opcode == I_DT || result->opcode == I_DO ||
541 result->opcode == I_DY || result->opcode == I_DZ ||
542 result->opcode == I_INCBIN) {
543 extop *eop, **tail = &result->eops, **fixptr;
544 int oper_num = 0;
545 int32_t sign;
547 result->eops_float = false;
550 * Begin to read the DB/DW/DD/DQ/DT/DO/DY/DZ/INCBIN operands.
552 while (1) {
553 i = stdscan(NULL, &tokval);
554 if (i == TOKEN_EOS)
555 break;
556 else if (first && i == ':') {
557 insn_is_label = true;
558 goto restart_parse;
560 first = false;
561 fixptr = tail;
562 eop = *tail = nasm_malloc(sizeof(extop));
563 tail = &eop->next;
564 eop->next = NULL;
565 eop->type = EOT_NOTHING;
566 oper_num++;
567 sign = +1;
570 * is_comma_next() here is to distinguish this from
571 * a string used as part of an expression...
573 if (i == TOKEN_STR && is_comma_next()) {
574 eop->type = EOT_DB_STRING;
575 eop->stringval = tokval.t_charptr;
576 eop->stringlen = tokval.t_inttwo;
577 i = stdscan(NULL, &tokval); /* eat the comma */
578 } else if (i == TOKEN_STRFUNC) {
579 bool parens = false;
580 const char *funcname = tokval.t_charptr;
581 enum strfunc func = tokval.t_integer;
582 i = stdscan(NULL, &tokval);
583 if (i == '(') {
584 parens = true;
585 i = stdscan(NULL, &tokval);
587 if (i != TOKEN_STR) {
588 nasm_error(ERR_NONFATAL,
589 "%s must be followed by a string constant",
590 funcname);
591 eop->type = EOT_NOTHING;
592 } else {
593 eop->type = EOT_DB_STRING_FREE;
594 eop->stringlen =
595 string_transform(tokval.t_charptr, tokval.t_inttwo,
596 &eop->stringval, func);
597 if (eop->stringlen == (size_t)-1) {
598 nasm_error(ERR_NONFATAL, "invalid string for transform");
599 eop->type = EOT_NOTHING;
602 if (parens && i && i != ')') {
603 i = stdscan(NULL, &tokval);
604 if (i != ')') {
605 nasm_error(ERR_NONFATAL, "unterminated %s function",
606 funcname);
609 if (i && i != ',')
610 i = stdscan(NULL, &tokval);
611 } else if (i == '-' || i == '+') {
612 char *save = stdscan_get();
613 int token = i;
614 sign = (i == '-') ? -1 : 1;
615 i = stdscan(NULL, &tokval);
616 if (i != TOKEN_FLOAT) {
617 stdscan_set(save);
618 i = tokval.t_type = token;
619 goto is_expression;
620 } else {
621 goto is_float;
623 } else if (i == TOKEN_FLOAT) {
624 is_float:
625 eop->type = EOT_DB_STRING;
626 result->eops_float = true;
628 eop->stringlen = idata_bytes(result->opcode);
629 if (eop->stringlen > 16) {
630 nasm_error(ERR_NONFATAL, "floating-point constant"
631 " encountered in DY or DZ instruction");
632 eop->stringlen = 0;
633 } else if (eop->stringlen < 1) {
634 nasm_error(ERR_NONFATAL, "floating-point constant"
635 " encountered in unknown instruction");
637 * fix suggested by Pedro Gimeno... original line was:
638 * eop->type = EOT_NOTHING;
640 eop->stringlen = 0;
643 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
644 tail = &eop->next;
645 *fixptr = eop;
646 eop->stringval = (char *)eop + sizeof(extop);
647 if (!eop->stringlen ||
648 !float_const(tokval.t_charptr, sign,
649 (uint8_t *)eop->stringval,
650 eop->stringlen, nasm_error))
651 eop->type = EOT_NOTHING;
652 i = stdscan(NULL, &tokval); /* eat the comma */
653 } else {
654 /* anything else, assume it is an expression */
655 expr *value;
657 is_expression:
658 value = evaluate(stdscan, NULL, &tokval, NULL,
659 critical, nasm_error, NULL);
660 i = tokval.t_type;
661 if (!value) /* Error in evaluator */
662 goto fail;
663 if (is_unknown(value)) {
664 eop->type = EOT_DB_NUMBER;
665 eop->offset = 0; /* doesn't matter what we put */
666 eop->segment = eop->wrt = NO_SEG; /* likewise */
667 } else if (is_reloc(value)) {
668 eop->type = EOT_DB_NUMBER;
669 eop->offset = reloc_value(value);
670 eop->segment = reloc_seg(value);
671 eop->wrt = reloc_wrt(value);
672 } else {
673 nasm_error(ERR_NONFATAL,
674 "operand %d: expression is not simple"
675 " or relocatable", oper_num);
680 * We're about to call stdscan(), which will eat the
681 * comma that we're currently sitting on between
682 * arguments. However, we'd better check first that it
683 * _is_ a comma.
685 if (i == TOKEN_EOS) /* also could be EOL */
686 break;
687 if (i != ',') {
688 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
689 oper_num);
690 goto fail;
694 if (result->opcode == I_INCBIN) {
696 * Correct syntax for INCBIN is that there should be
697 * one string operand, followed by one or two numeric
698 * operands.
700 if (!result->eops || result->eops->type != EOT_DB_STRING)
701 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
702 else if (result->eops->next &&
703 result->eops->next->type != EOT_DB_NUMBER)
704 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
705 " non-numeric");
706 else if (result->eops->next && result->eops->next->next &&
707 result->eops->next->next->type != EOT_DB_NUMBER)
708 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
709 " non-numeric");
710 else if (result->eops->next && result->eops->next->next &&
711 result->eops->next->next->next)
712 nasm_error(ERR_NONFATAL,
713 "`incbin': more than three parameters");
714 else
715 return result;
717 * If we reach here, one of the above errors happened.
718 * Throw the instruction away.
720 goto fail;
721 } else /* DB ... */ if (oper_num == 0)
722 nasm_error(ERR_WARNING | ERR_PASS1,
723 "no operand for data declaration");
724 else
725 result->operands = oper_num;
727 return result;
731 * Now we begin to parse the operands. There may be up to four
732 * of these, separated by commas, and terminated by a zero token.
735 for (opnum = 0; opnum < MAX_OPERANDS; opnum++) {
736 operand *op = &result->oprs[opnum];
737 expr *value; /* used most of the time */
738 bool mref; /* is this going to be a memory ref? */
739 bool bracket; /* is it a [] mref, or a & mref? */
740 bool mib; /* compound (mib) mref? */
741 int setsize = 0;
742 decoflags_t brace_flags = 0; /* flags for decorators in braces */
744 op->disp_size = 0; /* have to zero this whatever */
745 op->eaflags = 0; /* and this */
746 op->opflags = 0;
747 op->decoflags = 0;
749 i = stdscan(NULL, &tokval);
750 if (i == TOKEN_EOS)
751 break; /* end of operands: get out of here */
752 else if (first && i == ':') {
753 insn_is_label = true;
754 goto restart_parse;
756 first = false;
757 op->type = 0; /* so far, no override */
758 while (i == TOKEN_SPECIAL) { /* size specifiers */
759 switch ((int)tokval.t_integer) {
760 case S_BYTE:
761 if (!setsize) /* we want to use only the first */
762 op->type |= BITS8;
763 setsize = 1;
764 break;
765 case S_WORD:
766 if (!setsize)
767 op->type |= BITS16;
768 setsize = 1;
769 break;
770 case S_DWORD:
771 case S_LONG:
772 if (!setsize)
773 op->type |= BITS32;
774 setsize = 1;
775 break;
776 case S_QWORD:
777 if (!setsize)
778 op->type |= BITS64;
779 setsize = 1;
780 break;
781 case S_TWORD:
782 if (!setsize)
783 op->type |= BITS80;
784 setsize = 1;
785 break;
786 case S_OWORD:
787 if (!setsize)
788 op->type |= BITS128;
789 setsize = 1;
790 break;
791 case S_YWORD:
792 if (!setsize)
793 op->type |= BITS256;
794 setsize = 1;
795 break;
796 case S_ZWORD:
797 if (!setsize)
798 op->type |= BITS512;
799 setsize = 1;
800 break;
801 case S_TO:
802 op->type |= TO;
803 break;
804 case S_STRICT:
805 op->type |= STRICT;
806 break;
807 case S_FAR:
808 op->type |= FAR;
809 break;
810 case S_NEAR:
811 op->type |= NEAR;
812 break;
813 case S_SHORT:
814 op->type |= SHORT;
815 break;
816 default:
817 nasm_error(ERR_NONFATAL, "invalid operand size specification");
819 i = stdscan(NULL, &tokval);
822 if (i == '[' || i == '&') { /* memory reference */
823 mref = true;
824 bracket = (i == '[');
825 i = stdscan(NULL, &tokval); /* then skip the colon */
826 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
827 process_size_override(result, op);
828 i = stdscan(NULL, &tokval);
830 /* when a comma follows an opening bracket - [ , eax*4] */
831 if (i == ',') {
832 /* treat as if there is a zero displacement virtually */
833 tokval.t_type = TOKEN_NUM;
834 tokval.t_integer = 0;
835 stdscan_set(stdscan_get() - 1); /* rewind the comma */
837 } else { /* immediate operand, or register */
838 mref = false;
839 bracket = false; /* placate optimisers */
842 if ((op->type & FAR) && !mref &&
843 result->opcode != I_JMP && result->opcode != I_CALL) {
844 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
847 value = evaluate(stdscan, NULL, &tokval,
848 &op->opflags,
849 critical, nasm_error, &hints);
850 i = tokval.t_type;
851 if (op->opflags & OPFLAG_FORWARD) {
852 result->forw_ref = true;
854 if (!value) /* Error in evaluator */
855 goto fail;
856 if (i == ':' && mref) { /* it was seg:offset */
858 * Process the segment override.
860 if (value[1].type != 0 ||
861 value->value != 1 ||
862 !IS_SREG(value->type))
863 nasm_error(ERR_NONFATAL, "invalid segment override");
864 else if (result->prefixes[PPS_SEG])
865 nasm_error(ERR_NONFATAL,
866 "instruction has conflicting segment overrides");
867 else {
868 result->prefixes[PPS_SEG] = value->type;
869 if (IS_FSGS(value->type))
870 op->eaflags |= EAF_FSGS;
873 i = stdscan(NULL, &tokval); /* then skip the colon */
874 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
875 process_size_override(result, op);
876 i = stdscan(NULL, &tokval);
878 value = evaluate(stdscan, NULL, &tokval,
879 &op->opflags,
880 critical, nasm_error, &hints);
881 i = tokval.t_type;
882 if (op->opflags & OPFLAG_FORWARD) {
883 result->forw_ref = true;
885 /* and get the offset */
886 if (!value) /* Error in evaluator */
887 goto fail;
890 mib = false;
891 if (mref && bracket && i == ',') {
892 /* [seg:base+offset,index*scale] syntax (mib) */
894 operand o1, o2; /* Partial operands */
896 if (parse_mref(&o1, value))
897 goto fail;
899 i = stdscan(NULL, &tokval); /* Eat comma */
900 value = evaluate(stdscan, NULL, &tokval, &op->opflags,
901 critical, nasm_error, &hints);
902 i = tokval.t_type;
903 if (!value)
904 goto fail;
906 if (parse_mref(&o2, value))
907 goto fail;
909 if (o2.basereg != -1 && o2.indexreg == -1) {
910 o2.indexreg = o2.basereg;
911 o2.scale = 1;
912 o2.basereg = -1;
915 if (o1.indexreg != -1 || o2.basereg != -1 || o2.offset != 0 ||
916 o2.segment != NO_SEG || o2.wrt != NO_SEG) {
917 nasm_error(ERR_NONFATAL, "invalid mib expression");
918 goto fail;
921 op->basereg = o1.basereg;
922 op->indexreg = o2.indexreg;
923 op->scale = o2.scale;
924 op->offset = o1.offset;
925 op->segment = o1.segment;
926 op->wrt = o1.wrt;
928 if (op->basereg != -1) {
929 op->hintbase = op->basereg;
930 op->hinttype = EAH_MAKEBASE;
931 } else if (op->indexreg != -1) {
932 op->hintbase = op->indexreg;
933 op->hinttype = EAH_NOTBASE;
934 } else {
935 op->hintbase = -1;
936 op->hinttype = EAH_NOHINT;
939 mib = true;
942 recover = false;
943 if (mref && bracket) { /* find ] at the end */
944 if (i != ']') {
945 nasm_error(ERR_NONFATAL, "parser: expecting ]");
946 recover = true;
947 } else { /* we got the required ] */
948 i = stdscan(NULL, &tokval);
949 if ((i == TOKEN_DECORATOR) || (i == TOKEN_OPMASK)) {
951 * according to AVX512 spec, broacast or opmask decorator
952 * is expected for memory reference operands
954 if (tokval.t_flag & TFLAG_BRDCAST) {
955 brace_flags |= GEN_BRDCAST(0) |
956 VAL_BRNUM(tokval.t_integer - BRC_1TO2);
957 i = stdscan(NULL, &tokval);
958 } else if (i == TOKEN_OPMASK) {
959 brace_flags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
960 i = stdscan(NULL, &tokval);
961 } else {
962 nasm_error(ERR_NONFATAL, "broadcast or opmask "
963 "decorator expected inside braces");
964 recover = true;
968 if (i != 0 && i != ',') {
969 nasm_error(ERR_NONFATAL, "comma or end of line expected");
970 recover = true;
973 } else { /* immediate operand */
974 if (i != 0 && i != ',' && i != ':' &&
975 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
976 nasm_error(ERR_NONFATAL, "comma, colon, decorator or end of "
977 "line expected after operand");
978 recover = true;
979 } else if (i == ':') {
980 op->type |= COLON;
981 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
982 /* parse opmask (and zeroing) after an operand */
983 recover = parse_braces(&brace_flags);
986 if (recover) {
987 do { /* error recovery */
988 i = stdscan(NULL, &tokval);
989 } while (i != 0 && i != ',');
993 * now convert the exprs returned from evaluate()
994 * into operand descriptions...
996 op->decoflags |= brace_flags;
998 if (mref) { /* it's a memory reference */
999 /* A mib reference was fully parsed already */
1000 if (!mib) {
1001 if (parse_mref(op, value))
1002 goto fail;
1003 op->hintbase = hints.base;
1004 op->hinttype = hints.type;
1006 mref_set_optype(op);
1007 } else { /* it's not a memory reference */
1008 if (is_just_unknown(value)) { /* it's immediate but unknown */
1009 op->type |= IMMEDIATE;
1010 op->opflags |= OPFLAG_UNKNOWN;
1011 op->offset = 0; /* don't care */
1012 op->segment = NO_SEG; /* don't care again */
1013 op->wrt = NO_SEG; /* still don't care */
1015 if(optimizing >= 0 && !(op->type & STRICT)) {
1016 /* Be optimistic */
1017 op->type |=
1018 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
1020 } else if (is_reloc(value)) { /* it's immediate */
1021 op->type |= IMMEDIATE;
1022 op->offset = reloc_value(value);
1023 op->segment = reloc_seg(value);
1024 op->wrt = reloc_wrt(value);
1026 if (is_simple(value)) {
1027 uint64_t n = reloc_value(value);
1028 if (n == 1)
1029 op->type |= UNITY;
1030 if (optimizing >= 0 &&
1031 !(op->type & STRICT)) {
1032 if ((uint32_t) (n + 128) <= 255)
1033 op->type |= SBYTEDWORD;
1034 if ((uint16_t) (n + 128) <= 255)
1035 op->type |= SBYTEWORD;
1036 if (n <= 0xFFFFFFFF)
1037 op->type |= UDWORD;
1038 if (n + 0x80000000 <= 0xFFFFFFFF)
1039 op->type |= SDWORD;
1042 } else if(value->type == EXPR_RDSAE) {
1044 * it's not an operand but a rounding or SAE decorator.
1045 * put the decorator information in the (opflag_t) type field
1046 * of previous operand.
1048 opnum--; op--;
1049 switch (value->value) {
1050 case BRC_RN:
1051 case BRC_RU:
1052 case BRC_RD:
1053 case BRC_RZ:
1054 case BRC_SAE:
1055 op->decoflags |= (value->value == BRC_SAE ? SAE : ER);
1056 result->evex_rm = value->value;
1057 break;
1058 default:
1059 nasm_error(ERR_NONFATAL, "invalid decorator");
1060 break;
1062 } else { /* it's a register */
1063 opflags_t rs;
1065 if (value->type >= EXPR_SIMPLE || value->value != 1) {
1066 nasm_error(ERR_NONFATAL, "invalid operand type");
1067 goto fail;
1071 * check that its only 1 register, not an expression...
1073 for (i = 1; value[i].type; i++)
1074 if (value[i].value) {
1075 nasm_error(ERR_NONFATAL, "invalid operand type");
1076 goto fail;
1079 /* clear overrides, except TO which applies to FPU regs */
1080 if (op->type & ~TO) {
1082 * we want to produce a warning iff the specified size
1083 * is different from the register size
1085 rs = op->type & SIZE_MASK;
1086 } else
1087 rs = 0;
1089 op->type &= TO;
1090 op->type |= REGISTER;
1091 op->type |= nasm_reg_flags[value->type];
1092 op->decoflags |= brace_flags;
1093 op->basereg = value->type;
1095 if (rs && (op->type & SIZE_MASK) != rs)
1096 nasm_error(ERR_WARNING | ERR_PASS1,
1097 "register size specification ignored");
1101 /* remember the position of operand having broadcasting/ER mode */
1102 if (op->decoflags & (BRDCAST_MASK | ER | SAE))
1103 result->evex_brerop = opnum;
1106 result->operands = opnum; /* set operand count */
1108 /* clear remaining operands */
1109 while (opnum < MAX_OPERANDS)
1110 result->oprs[opnum++].type = 0;
1113 * Transform RESW, RESD, RESQ, REST, RESO, RESY, RESZ into RESB.
1115 switch (result->opcode) {
1116 case I_RESW:
1117 result->opcode = I_RESB;
1118 result->oprs[0].offset *= 2;
1119 break;
1120 case I_RESD:
1121 result->opcode = I_RESB;
1122 result->oprs[0].offset *= 4;
1123 break;
1124 case I_RESQ:
1125 result->opcode = I_RESB;
1126 result->oprs[0].offset *= 8;
1127 break;
1128 case I_REST:
1129 result->opcode = I_RESB;
1130 result->oprs[0].offset *= 10;
1131 break;
1132 case I_RESO:
1133 result->opcode = I_RESB;
1134 result->oprs[0].offset *= 16;
1135 break;
1136 case I_RESY:
1137 result->opcode = I_RESB;
1138 result->oprs[0].offset *= 32;
1139 break;
1140 case I_RESZ:
1141 result->opcode = I_RESB;
1142 result->oprs[0].offset *= 64;
1143 break;
1144 default:
1145 break;
1148 return result;
1150 fail:
1151 result->opcode = I_none;
1152 return result;
1155 static int is_comma_next(void)
1157 struct tokenval tv;
1158 char *p;
1159 int i;
1161 p = stdscan_get();
1162 i = stdscan(NULL, &tv);
1163 stdscan_set(p);
1165 return (i == ',' || i == ';' || !i);
1168 void cleanup_insn(insn * i)
1170 extop *e;
1172 while ((e = i->eops)) {
1173 i->eops = e->next;
1174 if (e->type == EOT_DB_STRING_FREE)
1175 nasm_free(e->stringval);
1176 nasm_free(e);