Build: Suppress warning messages
[nasm/avx512.git] / parser.c
blob7112781cae64c81f5cf01f8b4f2b9cfb42fb948d
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 return PPS_REP;
94 case P_O16:
95 case P_O32:
96 case P_O64:
97 case P_OSP:
98 return PPS_OSIZE;
99 case P_A16:
100 case P_A32:
101 case P_A64:
102 case P_ASP:
103 return PPS_ASIZE;
104 case P_EVEX:
105 return PPS_EVEX;
106 default:
107 nasm_error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
108 return -1;
112 static void process_size_override(insn *result, operand *op)
114 if (tasm_compatible_mode) {
115 switch ((int)tokval.t_integer) {
116 /* For TASM compatibility a size override inside the
117 * brackets changes the size of the operand, not the
118 * address type of the operand as it does in standard
119 * NASM syntax. Hence:
121 * mov eax,[DWORD val]
123 * is valid syntax in TASM compatibility mode. Note that
124 * you lose the ability to override the default address
125 * type for the instruction, but we never use anything
126 * but 32-bit flat model addressing in our code.
128 case S_BYTE:
129 op->type |= BITS8;
130 break;
131 case S_WORD:
132 op->type |= BITS16;
133 break;
134 case S_DWORD:
135 case S_LONG:
136 op->type |= BITS32;
137 break;
138 case S_QWORD:
139 op->type |= BITS64;
140 break;
141 case S_TWORD:
142 op->type |= BITS80;
143 break;
144 case S_OWORD:
145 op->type |= BITS128;
146 break;
147 default:
148 nasm_error(ERR_NONFATAL,
149 "invalid operand size specification");
150 break;
152 } else {
153 /* Standard NASM compatible syntax */
154 switch ((int)tokval.t_integer) {
155 case S_NOSPLIT:
156 op->eaflags |= EAF_TIMESTWO;
157 break;
158 case S_REL:
159 op->eaflags |= EAF_REL;
160 break;
161 case S_ABS:
162 op->eaflags |= EAF_ABS;
163 break;
164 case S_BYTE:
165 op->disp_size = 8;
166 op->eaflags |= EAF_BYTEOFFS;
167 break;
168 case P_A16:
169 case P_A32:
170 case P_A64:
171 if (result->prefixes[PPS_ASIZE] &&
172 result->prefixes[PPS_ASIZE] != tokval.t_integer)
173 nasm_error(ERR_NONFATAL,
174 "conflicting address size specifications");
175 else
176 result->prefixes[PPS_ASIZE] = tokval.t_integer;
177 break;
178 case S_WORD:
179 op->disp_size = 16;
180 op->eaflags |= EAF_WORDOFFS;
181 break;
182 case S_DWORD:
183 case S_LONG:
184 op->disp_size = 32;
185 op->eaflags |= EAF_WORDOFFS;
186 break;
187 case S_QWORD:
188 op->disp_size = 64;
189 op->eaflags |= EAF_WORDOFFS;
190 break;
191 default:
192 nasm_error(ERR_NONFATAL, "invalid size specification in"
193 " effective address");
194 break;
200 * when two or more decorators follow a register operand,
201 * consecutive decorators are parsed here.
202 * opmask and zeroing decorators can be placed in any order.
203 * e.g. zmm1 {k2}{z} or zmm2 {z,k3}
204 * decorator(s) are placed at the end of an operand.
206 static bool parse_braces(decoflags_t *decoflags)
208 int i;
209 bool recover = false;
211 i = tokval.t_type;
212 do {
213 if (i == TOKEN_OPMASK) {
214 if (*decoflags & OPMASK_MASK) {
215 nasm_error(ERR_NONFATAL, "opmask k%"PRIu64" is already set",
216 *decoflags & OPMASK_MASK);
217 *decoflags &= ~OPMASK_MASK;
219 *decoflags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
220 } else if (i == TOKEN_DECORATOR) {
221 switch (tokval.t_integer) {
222 case BRC_Z:
224 * according to AVX512 spec, only zeroing/merging decorator
225 * is supported with opmask
227 *decoflags |= GEN_Z(0);
228 break;
229 default:
230 nasm_error(ERR_NONFATAL, "{%s} is not an expected decorator",
231 tokval.t_charptr);
232 break;
234 } else if (i == ',' || i == TOKEN_EOS){
235 break;
236 } else {
237 nasm_error(ERR_NONFATAL, "only a series of valid decorators"
238 " expected");
239 recover = true;
240 break;
242 i = stdscan(NULL, &tokval);
243 } while(1);
245 return recover;
248 static int parse_mref(operand *op, const expr *e)
250 int b, i, s; /* basereg, indexreg, scale */
251 int64_t o; /* offset */
253 b = i = -1;
254 o = s = 0;
256 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
257 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
259 if (is_gpr && e->value == 1)
260 b = e->type; /* It can be basereg */
261 else /* No, it has to be indexreg */
262 i = e->type, s = e->value;
263 e++;
265 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
266 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
268 if (b != -1) /* If the first was the base, ... */
269 i = e->type, s = e->value; /* second has to be indexreg */
271 else if (!is_gpr || e->value != 1) {
272 /* If both want to be index */
273 nasm_error(ERR_NONFATAL,
274 "invalid effective address: two index registers");
275 return -1;
276 } else
277 b = e->type;
278 e++;
280 if (e->type != 0) { /* is there an offset? */
281 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
282 nasm_error(ERR_NONFATAL,
283 "beroset-p-603-invalid effective address");
284 return -1;
285 } else {
286 if (e->type == EXPR_UNKNOWN) {
287 op->opflags |= OPFLAG_UNKNOWN;
288 o = 0; /* doesn't matter what */
289 op->wrt = NO_SEG; /* nor this */
290 op->segment = NO_SEG; /* or this */
291 while (e->type)
292 e++; /* go to the end of the line */
293 } else {
294 if (e->type == EXPR_SIMPLE) {
295 o = e->value;
296 e++;
298 if (e->type == EXPR_WRT) {
299 op->wrt = e->value;
300 e++;
301 } else
302 op->wrt = NO_SEG;
304 * Look for a segment base type.
306 if (e->type && e->type < EXPR_SEGBASE) {
307 nasm_error(ERR_NONFATAL,
308 "beroset-p-630-invalid effective address");
309 return -1;
311 while (e->type && e->value == 0)
312 e++;
313 if (e->type && e->value != 1) {
314 nasm_error(ERR_NONFATAL,
315 "beroset-p-637-invalid effective address");
316 return -1;
318 if (e->type) {
319 op->segment = e->type - EXPR_SEGBASE;
320 e++;
321 } else
322 op->segment = NO_SEG;
323 while (e->type && e->value == 0)
324 e++;
325 if (e->type) {
326 nasm_error(ERR_NONFATAL,
327 "beroset-p-650-invalid effective address");
328 return -1;
332 } else {
333 o = 0;
334 op->wrt = NO_SEG;
335 op->segment = NO_SEG;
338 if (e->type != 0) { /* there'd better be nothing left! */
339 nasm_error(ERR_NONFATAL,
340 "beroset-p-663-invalid effective address");
341 return -1;
344 op->basereg = b;
345 op->indexreg = i;
346 op->scale = s;
347 op->offset = o;
348 return 0;
351 static void mref_set_optype(operand *op)
353 int b = op->basereg;
354 int i = op->indexreg;
355 int s = op->scale;
357 /* It is memory, but it can match any r/m operand */
358 op->type |= MEMORY_ANY;
360 if (b == -1 && (i == -1 || s == 0)) {
361 int is_rel = globalbits == 64 &&
362 !(op->eaflags & EAF_ABS) &&
363 ((globalrel &&
364 !(op->eaflags & EAF_FSGS)) ||
365 (op->eaflags & EAF_REL));
367 op->type |= is_rel ? IP_REL : MEM_OFFS;
370 if (i != -1) {
371 opflags_t iclass = nasm_reg_flags[i];
373 if (is_class(XMMREG,iclass))
374 op->type |= XMEM;
375 else if (is_class(YMMREG,iclass))
376 op->type |= YMEM;
377 else if (is_class(ZMMREG,iclass))
378 op->type |= ZMEM;
382 insn *parse_line(int pass, char *buffer, insn *result, ldfunc ldef)
384 bool insn_is_label = false;
385 struct eval_hints hints;
386 int opnum;
387 int critical;
388 bool first;
389 bool recover;
391 restart_parse:
392 first = true;
393 result->forw_ref = false;
395 stdscan_reset();
396 stdscan_set(buffer);
397 i = stdscan(NULL, &tokval);
399 result->label = NULL; /* Assume no label */
400 result->eops = NULL; /* must do this, whatever happens */
401 result->operands = 0; /* must initialize this */
402 result->evex_rm = 0; /* Ensure EVEX rounding mode is reset */
403 result->evex_brerop = -1; /* Reset EVEX broadcasting/ER op position */
405 /* Ignore blank lines */
406 if (i == TOKEN_EOS)
407 goto fail;
409 if (i != TOKEN_ID &&
410 i != TOKEN_INSN &&
411 i != TOKEN_PREFIX &&
412 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
413 nasm_error(ERR_NONFATAL,
414 "label or instruction expected at start of line");
415 goto fail;
418 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
419 /* there's a label here */
420 first = false;
421 result->label = tokval.t_charptr;
422 i = stdscan(NULL, &tokval);
423 if (i == ':') { /* skip over the optional colon */
424 i = stdscan(NULL, &tokval);
425 } else if (i == 0) {
426 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
427 "label alone on a line without a colon might be in error");
429 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
431 * FIXME: location->segment could be NO_SEG, in which case
432 * it is possible we should be passing 'abs_seg'. Look into this.
433 * Work out whether that is *really* what we should be doing.
434 * Generally fix things. I think this is right as it is, but
435 * am still not certain.
437 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
438 location->offset, NULL, true, false);
442 /* Just a label here */
443 if (i == TOKEN_EOS)
444 goto fail;
446 nasm_build_assert(P_none != 0);
447 memset(result->prefixes, P_none, sizeof(result->prefixes));
448 result->times = 1L;
450 while (i == TOKEN_PREFIX ||
451 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
452 first = false;
455 * Handle special case: the TIMES prefix.
457 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
458 expr *value;
460 i = stdscan(NULL, &tokval);
461 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
462 i = tokval.t_type;
463 if (!value) /* Error in evaluator */
464 goto fail;
465 if (!is_simple(value)) {
466 nasm_error(ERR_NONFATAL,
467 "non-constant argument supplied to TIMES");
468 result->times = 1L;
469 } else {
470 result->times = value->value;
471 if (value->value < 0 && pass0 == 2) {
472 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
473 value->value);
474 result->times = 0;
477 } else {
478 int slot = prefix_slot(tokval.t_integer);
479 if (result->prefixes[slot]) {
480 if (result->prefixes[slot] == tokval.t_integer)
481 nasm_error(ERR_WARNING | ERR_PASS1,
482 "instruction has redundant prefixes");
483 else
484 nasm_error(ERR_NONFATAL,
485 "instruction has conflicting prefixes");
487 result->prefixes[slot] = tokval.t_integer;
488 i = stdscan(NULL, &tokval);
492 if (i != TOKEN_INSN) {
493 int j;
494 enum prefixes pfx;
496 for (j = 0; j < MAXPREFIX; j++) {
497 if ((pfx = result->prefixes[j]) != P_none)
498 break;
501 if (i == 0 && pfx != P_none) {
503 * Instruction prefixes are present, but no actual
504 * instruction. This is allowed: at this point we
505 * invent a notional instruction of RESB 0.
507 result->opcode = I_RESB;
508 result->operands = 1;
509 result->oprs[0].type = IMMEDIATE;
510 result->oprs[0].offset = 0L;
511 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
512 return result;
513 } else {
514 nasm_error(ERR_NONFATAL, "parser: instruction expected");
515 goto fail;
519 result->opcode = tokval.t_integer;
520 result->condition = tokval.t_inttwo;
523 * INCBIN cannot be satisfied with incorrectly
524 * evaluated operands, since the correct values _must_ be known
525 * on the first pass. Hence, even in pass one, we set the
526 * `critical' flag on calling evaluate(), so that it will bomb
527 * out on undefined symbols.
529 if (result->opcode == I_INCBIN) {
530 critical = (pass0 < 2 ? 1 : 2);
532 } else
533 critical = (pass == 2 ? 2 : 0);
535 if (result->opcode == I_DB || result->opcode == I_DW ||
536 result->opcode == I_DD || result->opcode == I_DQ ||
537 result->opcode == I_DT || result->opcode == I_DO ||
538 result->opcode == I_DY || result->opcode == I_INCBIN) {
539 extop *eop, **tail = &result->eops, **fixptr;
540 int oper_num = 0;
541 int32_t sign;
543 result->eops_float = false;
546 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
548 while (1) {
549 i = stdscan(NULL, &tokval);
550 if (i == TOKEN_EOS)
551 break;
552 else if (first && i == ':') {
553 insn_is_label = true;
554 goto restart_parse;
556 first = false;
557 fixptr = tail;
558 eop = *tail = nasm_malloc(sizeof(extop));
559 tail = &eop->next;
560 eop->next = NULL;
561 eop->type = EOT_NOTHING;
562 oper_num++;
563 sign = +1;
566 * is_comma_next() here is to distinguish this from
567 * a string used as part of an expression...
569 if (i == TOKEN_STR && is_comma_next()) {
570 eop->type = EOT_DB_STRING;
571 eop->stringval = tokval.t_charptr;
572 eop->stringlen = tokval.t_inttwo;
573 i = stdscan(NULL, &tokval); /* eat the comma */
574 } else if (i == TOKEN_STRFUNC) {
575 bool parens = false;
576 const char *funcname = tokval.t_charptr;
577 enum strfunc func = tokval.t_integer;
578 i = stdscan(NULL, &tokval);
579 if (i == '(') {
580 parens = true;
581 i = stdscan(NULL, &tokval);
583 if (i != TOKEN_STR) {
584 nasm_error(ERR_NONFATAL,
585 "%s must be followed by a string constant",
586 funcname);
587 eop->type = EOT_NOTHING;
588 } else {
589 eop->type = EOT_DB_STRING_FREE;
590 eop->stringlen =
591 string_transform(tokval.t_charptr, tokval.t_inttwo,
592 &eop->stringval, func);
593 if (eop->stringlen == (size_t)-1) {
594 nasm_error(ERR_NONFATAL, "invalid string for transform");
595 eop->type = EOT_NOTHING;
598 if (parens && i && i != ')') {
599 i = stdscan(NULL, &tokval);
600 if (i != ')') {
601 nasm_error(ERR_NONFATAL, "unterminated %s function",
602 funcname);
605 if (i && i != ',')
606 i = stdscan(NULL, &tokval);
607 } else if (i == '-' || i == '+') {
608 char *save = stdscan_get();
609 int token = i;
610 sign = (i == '-') ? -1 : 1;
611 i = stdscan(NULL, &tokval);
612 if (i != TOKEN_FLOAT) {
613 stdscan_set(save);
614 i = tokval.t_type = token;
615 goto is_expression;
616 } else {
617 goto is_float;
619 } else if (i == TOKEN_FLOAT) {
620 is_float:
621 eop->type = EOT_DB_STRING;
622 result->eops_float = true;
624 eop->stringlen = idata_bytes(result->opcode);
625 if (eop->stringlen > 16) {
626 nasm_error(ERR_NONFATAL, "floating-point constant"
627 " encountered in DY instruction");
628 eop->stringlen = 0;
629 } else if (eop->stringlen < 1) {
630 nasm_error(ERR_NONFATAL, "floating-point constant"
631 " encountered in unknown instruction");
633 * fix suggested by Pedro Gimeno... original line was:
634 * eop->type = EOT_NOTHING;
636 eop->stringlen = 0;
639 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
640 tail = &eop->next;
641 *fixptr = eop;
642 eop->stringval = (char *)eop + sizeof(extop);
643 if (!eop->stringlen ||
644 !float_const(tokval.t_charptr, sign,
645 (uint8_t *)eop->stringval,
646 eop->stringlen, nasm_error))
647 eop->type = EOT_NOTHING;
648 i = stdscan(NULL, &tokval); /* eat the comma */
649 } else {
650 /* anything else, assume it is an expression */
651 expr *value;
653 is_expression:
654 value = evaluate(stdscan, NULL, &tokval, NULL,
655 critical, nasm_error, NULL);
656 i = tokval.t_type;
657 if (!value) /* Error in evaluator */
658 goto fail;
659 if (is_unknown(value)) {
660 eop->type = EOT_DB_NUMBER;
661 eop->offset = 0; /* doesn't matter what we put */
662 eop->segment = eop->wrt = NO_SEG; /* likewise */
663 } else if (is_reloc(value)) {
664 eop->type = EOT_DB_NUMBER;
665 eop->offset = reloc_value(value);
666 eop->segment = reloc_seg(value);
667 eop->wrt = reloc_wrt(value);
668 } else {
669 nasm_error(ERR_NONFATAL,
670 "operand %d: expression is not simple"
671 " or relocatable", oper_num);
676 * We're about to call stdscan(), which will eat the
677 * comma that we're currently sitting on between
678 * arguments. However, we'd better check first that it
679 * _is_ a comma.
681 if (i == TOKEN_EOS) /* also could be EOL */
682 break;
683 if (i != ',') {
684 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
685 oper_num);
686 goto fail;
690 if (result->opcode == I_INCBIN) {
692 * Correct syntax for INCBIN is that there should be
693 * one string operand, followed by one or two numeric
694 * operands.
696 if (!result->eops || result->eops->type != EOT_DB_STRING)
697 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
698 else if (result->eops->next &&
699 result->eops->next->type != EOT_DB_NUMBER)
700 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
701 " non-numeric");
702 else if (result->eops->next && result->eops->next->next &&
703 result->eops->next->next->type != EOT_DB_NUMBER)
704 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
705 " non-numeric");
706 else if (result->eops->next && result->eops->next->next &&
707 result->eops->next->next->next)
708 nasm_error(ERR_NONFATAL,
709 "`incbin': more than three parameters");
710 else
711 return result;
713 * If we reach here, one of the above errors happened.
714 * Throw the instruction away.
716 goto fail;
717 } else /* DB ... */ if (oper_num == 0)
718 nasm_error(ERR_WARNING | ERR_PASS1,
719 "no operand for data declaration");
720 else
721 result->operands = oper_num;
723 return result;
727 * Now we begin to parse the operands. There may be up to four
728 * of these, separated by commas, and terminated by a zero token.
731 for (opnum = 0; opnum < MAX_OPERANDS; opnum++) {
732 operand *op = &result->oprs[opnum];
733 expr *value; /* used most of the time */
734 bool mref; /* is this going to be a memory ref? */
735 bool bracket; /* is it a [] mref, or a & mref? */
736 bool mib; /* compound (mib) mref? */
737 int setsize = 0;
738 decoflags_t brace_flags = 0; /* flags for decorators in braces */
740 op->disp_size = 0; /* have to zero this whatever */
741 op->eaflags = 0; /* and this */
742 op->opflags = 0;
743 op->decoflags = 0;
745 i = stdscan(NULL, &tokval);
746 if (i == TOKEN_EOS)
747 break; /* end of operands: get out of here */
748 else if (first && i == ':') {
749 insn_is_label = true;
750 goto restart_parse;
752 first = false;
753 op->type = 0; /* so far, no override */
754 while (i == TOKEN_SPECIAL) { /* size specifiers */
755 switch ((int)tokval.t_integer) {
756 case S_BYTE:
757 if (!setsize) /* we want to use only the first */
758 op->type |= BITS8;
759 setsize = 1;
760 break;
761 case S_WORD:
762 if (!setsize)
763 op->type |= BITS16;
764 setsize = 1;
765 break;
766 case S_DWORD:
767 case S_LONG:
768 if (!setsize)
769 op->type |= BITS32;
770 setsize = 1;
771 break;
772 case S_QWORD:
773 if (!setsize)
774 op->type |= BITS64;
775 setsize = 1;
776 break;
777 case S_TWORD:
778 if (!setsize)
779 op->type |= BITS80;
780 setsize = 1;
781 break;
782 case S_OWORD:
783 if (!setsize)
784 op->type |= BITS128;
785 setsize = 1;
786 break;
787 case S_YWORD:
788 if (!setsize)
789 op->type |= BITS256;
790 setsize = 1;
791 break;
792 case S_ZWORD:
793 if (!setsize)
794 op->type |= BITS512;
795 setsize = 1;
796 break;
797 case S_TO:
798 op->type |= TO;
799 break;
800 case S_STRICT:
801 op->type |= STRICT;
802 break;
803 case S_FAR:
804 op->type |= FAR;
805 break;
806 case S_NEAR:
807 op->type |= NEAR;
808 break;
809 case S_SHORT:
810 op->type |= SHORT;
811 break;
812 default:
813 nasm_error(ERR_NONFATAL, "invalid operand size specification");
815 i = stdscan(NULL, &tokval);
818 if (i == '[' || i == '&') { /* memory reference */
819 mref = true;
820 bracket = (i == '[');
821 i = stdscan(NULL, &tokval); /* then skip the colon */
822 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
823 process_size_override(result, op);
824 i = stdscan(NULL, &tokval);
826 /* when a comma follows an opening bracket - [ , eax*4] */
827 if (i == ',') {
828 /* treat as if there is a zero displacement virtually */
829 tokval.t_type = TOKEN_NUM;
830 tokval.t_integer = 0;
831 stdscan_set(stdscan_get() - 1); /* rewind the comma */
833 } else { /* immediate operand, or register */
834 mref = false;
835 bracket = false; /* placate optimisers */
838 if ((op->type & FAR) && !mref &&
839 result->opcode != I_JMP && result->opcode != I_CALL) {
840 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
843 value = evaluate(stdscan, NULL, &tokval,
844 &op->opflags,
845 critical, nasm_error, &hints);
846 i = tokval.t_type;
847 if (op->opflags & OPFLAG_FORWARD) {
848 result->forw_ref = true;
850 if (!value) /* Error in evaluator */
851 goto fail;
852 if (i == ':' && mref) { /* it was seg:offset */
854 * Process the segment override.
856 if (value[1].type != 0 ||
857 value->value != 1 ||
858 !IS_SREG(value->type))
859 nasm_error(ERR_NONFATAL, "invalid segment override");
860 else if (result->prefixes[PPS_SEG])
861 nasm_error(ERR_NONFATAL,
862 "instruction has conflicting segment overrides");
863 else {
864 result->prefixes[PPS_SEG] = value->type;
865 if (IS_FSGS(value->type))
866 op->eaflags |= EAF_FSGS;
869 i = stdscan(NULL, &tokval); /* then skip the colon */
870 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
871 process_size_override(result, op);
872 i = stdscan(NULL, &tokval);
874 value = evaluate(stdscan, NULL, &tokval,
875 &op->opflags,
876 critical, nasm_error, &hints);
877 i = tokval.t_type;
878 if (op->opflags & OPFLAG_FORWARD) {
879 result->forw_ref = true;
881 /* and get the offset */
882 if (!value) /* Error in evaluator */
883 goto fail;
886 mib = false;
887 if (mref && bracket && i == ',') {
888 /* [seg:base+offset,index*scale] syntax (mib) */
890 operand o1, o2; /* Partial operands */
892 if (parse_mref(&o1, value))
893 goto fail;
895 i = stdscan(NULL, &tokval); /* Eat comma */
896 value = evaluate(stdscan, NULL, &tokval, &op->opflags,
897 critical, nasm_error, &hints);
898 i = tokval.t_type;
900 if (parse_mref(&o2, value))
901 goto fail;
903 if (o2.basereg != -1 && o2.indexreg == -1) {
904 o2.indexreg = o2.basereg;
905 o2.scale = 1;
906 o2.basereg = -1;
909 if (o1.indexreg != -1 || o2.basereg != -1 || o2.offset != 0 ||
910 o2.segment != NO_SEG || o2.wrt != NO_SEG) {
911 nasm_error(ERR_NONFATAL, "invalid mib expression");
912 goto fail;
915 op->basereg = o1.basereg;
916 op->indexreg = o2.indexreg;
917 op->scale = o2.scale;
918 op->offset = o1.offset;
919 op->segment = o1.segment;
920 op->wrt = o1.wrt;
922 if (op->basereg != -1) {
923 op->hintbase = op->basereg;
924 op->hinttype = EAH_MAKEBASE;
925 } else if (op->indexreg != -1) {
926 op->hintbase = op->indexreg;
927 op->hinttype = EAH_NOTBASE;
928 } else {
929 op->hintbase = -1;
930 op->hinttype = EAH_NOHINT;
933 mib = true;
936 recover = false;
937 if (mref && bracket) { /* find ] at the end */
938 if (i != ']') {
939 nasm_error(ERR_NONFATAL, "parser: expecting ]");
940 recover = true;
941 } else { /* we got the required ] */
942 i = stdscan(NULL, &tokval);
943 if ((i == TOKEN_DECORATOR) || (i == TOKEN_OPMASK)) {
945 * according to AVX512 spec, broacast or opmask decorator
946 * is expected for memory reference operands
948 if (tokval.t_flag & TFLAG_BRDCAST) {
949 brace_flags |= GEN_BRDCAST(0);
950 i = stdscan(NULL, &tokval);
951 } else if (i == TOKEN_OPMASK) {
952 brace_flags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
953 i = stdscan(NULL, &tokval);
954 } else {
955 nasm_error(ERR_NONFATAL, "broadcast or opmask "
956 "decorator expected inside braces");
957 recover = true;
961 if (i != 0 && i != ',') {
962 nasm_error(ERR_NONFATAL, "comma or end of line expected");
963 recover = true;
966 } else { /* immediate operand */
967 if (i != 0 && i != ',' && i != ':' &&
968 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
969 nasm_error(ERR_NONFATAL, "comma, colon, decorator or end of "
970 "line expected after operand");
971 recover = true;
972 } else if (i == ':') {
973 op->type |= COLON;
974 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
975 /* parse opmask (and zeroing) after an operand */
976 recover = parse_braces(&brace_flags);
979 if (recover) {
980 do { /* error recovery */
981 i = stdscan(NULL, &tokval);
982 } while (i != 0 && i != ',');
986 * now convert the exprs returned from evaluate()
987 * into operand descriptions...
989 op->decoflags |= brace_flags;
991 if (mref) { /* it's a memory reference */
992 /* A mib reference was fully parsed already */
993 if (!mib) {
994 if (parse_mref(op, value))
995 goto fail;
996 op->hintbase = hints.base;
997 op->hinttype = hints.type;
999 mref_set_optype(op);
1000 } else { /* it's not a memory reference */
1001 if (is_just_unknown(value)) { /* it's immediate but unknown */
1002 op->type |= IMMEDIATE;
1003 op->opflags |= OPFLAG_UNKNOWN;
1004 op->offset = 0; /* don't care */
1005 op->segment = NO_SEG; /* don't care again */
1006 op->wrt = NO_SEG; /* still don't care */
1008 if(optimizing >= 0 && !(op->type & STRICT)) {
1009 /* Be optimistic */
1010 op->type |=
1011 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
1013 } else if (is_reloc(value)) { /* it's immediate */
1014 op->type |= IMMEDIATE;
1015 op->offset = reloc_value(value);
1016 op->segment = reloc_seg(value);
1017 op->wrt = reloc_wrt(value);
1019 if (is_simple(value)) {
1020 uint64_t n = reloc_value(value);
1021 if (n == 1)
1022 op->type |= UNITY;
1023 if (optimizing >= 0 &&
1024 !(op->type & STRICT)) {
1025 if ((uint32_t) (n + 128) <= 255)
1026 op->type |= SBYTEDWORD;
1027 if ((uint16_t) (n + 128) <= 255)
1028 op->type |= SBYTEWORD;
1029 if (n <= 0xFFFFFFFF)
1030 op->type |= UDWORD;
1031 if (n + 0x80000000 <= 0xFFFFFFFF)
1032 op->type |= SDWORD;
1035 } else if(value->type == EXPR_RDSAE) {
1037 * it's not an operand but a rounding or SAE decorator.
1038 * put the decorator information in the (opflag_t) type field
1039 * of previous operand.
1041 opnum--; op--;
1042 switch (value->value) {
1043 case BRC_RN:
1044 case BRC_RU:
1045 case BRC_RD:
1046 case BRC_RZ:
1047 case BRC_SAE:
1048 op->decoflags |= (value->value == BRC_SAE ? SAE : ER);
1049 result->evex_rm = value->value;
1050 break;
1051 default:
1052 nasm_error(ERR_NONFATAL, "invalid decorator");
1053 break;
1055 } else { /* it's a register */
1056 opflags_t rs;
1058 if (value->type >= EXPR_SIMPLE || value->value != 1) {
1059 nasm_error(ERR_NONFATAL, "invalid operand type");
1060 goto fail;
1064 * check that its only 1 register, not an expression...
1066 for (i = 1; value[i].type; i++)
1067 if (value[i].value) {
1068 nasm_error(ERR_NONFATAL, "invalid operand type");
1069 goto fail;
1072 /* clear overrides, except TO which applies to FPU regs */
1073 if (op->type & ~TO) {
1075 * we want to produce a warning iff the specified size
1076 * is different from the register size
1078 rs = op->type & SIZE_MASK;
1079 } else
1080 rs = 0;
1082 op->type &= TO;
1083 op->type |= REGISTER;
1084 op->type |= nasm_reg_flags[value->type];
1085 op->decoflags |= brace_flags;
1086 op->basereg = value->type;
1088 if (rs && (op->type & SIZE_MASK) != rs)
1089 nasm_error(ERR_WARNING | ERR_PASS1,
1090 "register size specification ignored");
1094 /* remember the position of operand having broadcasting/ER mode */
1095 if (op->decoflags & (BRDCAST_MASK | ER | SAE))
1096 result->evex_brerop = opnum;
1099 result->operands = opnum; /* set operand count */
1101 /* clear remaining operands */
1102 while (opnum < MAX_OPERANDS)
1103 result->oprs[opnum++].type = 0;
1106 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
1108 switch (result->opcode) {
1109 case I_RESW:
1110 result->opcode = I_RESB;
1111 result->oprs[0].offset *= 2;
1112 break;
1113 case I_RESD:
1114 result->opcode = I_RESB;
1115 result->oprs[0].offset *= 4;
1116 break;
1117 case I_RESQ:
1118 result->opcode = I_RESB;
1119 result->oprs[0].offset *= 8;
1120 break;
1121 case I_REST:
1122 result->opcode = I_RESB;
1123 result->oprs[0].offset *= 10;
1124 break;
1125 case I_RESO:
1126 result->opcode = I_RESB;
1127 result->oprs[0].offset *= 16;
1128 break;
1129 case I_RESY:
1130 result->opcode = I_RESB;
1131 result->oprs[0].offset *= 32;
1132 break;
1133 default:
1134 break;
1137 return result;
1139 fail:
1140 result->opcode = I_none;
1141 return result;
1144 static int is_comma_next(void)
1146 struct tokenval tv;
1147 char *p;
1148 int i;
1150 p = stdscan_get();
1151 i = stdscan(NULL, &tv);
1152 stdscan_set(p);
1154 return (i == ',' || i == ';' || !i);
1157 void cleanup_insn(insn * i)
1159 extop *e;
1161 while ((e = i->eops)) {
1162 i->eops = e->next;
1163 if (e->type == EOT_DB_STRING_FREE)
1164 nasm_free(e->stringval);
1165 nasm_free(e);