parse: factor out mref parsing
[nasm/avx512.git] / parser.c
blob180a4395619e3eb6d30fb295d012443e73a67f63
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 return PPS_REP;
93 case P_O16:
94 case P_O32:
95 case P_O64:
96 case P_OSP:
97 return PPS_OSIZE;
98 case P_A16:
99 case P_A32:
100 case P_A64:
101 case P_ASP:
102 return PPS_ASIZE;
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%lu 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,
246 const struct eval_hints *hints, decoflags_t brace_flags)
248 int b, i, s; /* basereg, indexreg, scale */
249 int64_t o; /* offset */
251 b = i = -1, o = s = 0;
252 op->hintbase = hints->base;
253 op->hinttype = hints->type;
255 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
256 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
258 if (is_gpr && e->value == 1)
259 b = e->type; /* It can be basereg */
260 else /* No, it has to be indexreg */
261 i = e->type, s = e->value;
262 e++;
264 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
265 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
267 if (b != -1) /* If the first was the base, ... */
268 i = e->type, s = e->value; /* second has to be indexreg */
270 else if (!is_gpr || e->value != 1) {
271 /* If both want to be index */
272 nasm_error(ERR_NONFATAL,
273 "invalid effective address: two index registers");
274 return -1;
275 } else
276 b = e->type;
277 e++;
279 if (e->type != 0) { /* is there an offset? */
280 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
281 nasm_error(ERR_NONFATAL,
282 "beroset-p-603-invalid effective address");
283 return -1;
284 } else {
285 if (e->type == EXPR_UNKNOWN) {
286 op->opflags |= OPFLAG_UNKNOWN;
287 o = 0; /* doesn't matter what */
288 op->wrt = NO_SEG; /* nor this */
289 op->segment = NO_SEG; /* or this */
290 while (e->type)
291 e++; /* go to the end of the line */
292 } else {
293 if (e->type == EXPR_SIMPLE) {
294 o = e->value;
295 e++;
297 if (e->type == EXPR_WRT) {
298 op->wrt = e->value;
299 e++;
300 } else
301 op->wrt = NO_SEG;
303 * Look for a segment base type.
305 if (e->type && e->type < EXPR_SEGBASE) {
306 nasm_error(ERR_NONFATAL,
307 "beroset-p-630-invalid effective address");
308 return -1;
310 while (e->type && e->value == 0)
311 e++;
312 if (e->type && e->value != 1) {
313 nasm_error(ERR_NONFATAL,
314 "beroset-p-637-invalid effective address");
315 return -1;
317 if (e->type) {
318 op->segment =
319 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 /* It is memory, but it can match any r/m operand */
345 op->type |= MEMORY_ANY;
347 if (b == -1 && (i == -1 || s == 0)) {
348 int is_rel = globalbits == 64 &&
349 !(op->eaflags & EAF_ABS) &&
350 ((globalrel &&
351 !(op->eaflags & EAF_FSGS)) ||
352 (op->eaflags & EAF_REL));
354 op->type |= is_rel ? IP_REL : MEM_OFFS;
357 if (i != -1) {
358 opflags_t iclass = nasm_reg_flags[i];
360 if (is_class(XMMREG,iclass))
361 op->type |= XMEM;
362 else if (is_class(YMMREG,iclass))
363 op->type |= YMEM;
364 else if (is_class(ZMMREG,iclass))
365 op->type |= ZMEM;
368 op->basereg = b;
369 op->indexreg = i;
370 op->scale = s;
371 op->offset = o;
372 op->decoflags |= brace_flags;
373 return 0;
376 insn *parse_line(int pass, char *buffer, insn *result, ldfunc ldef)
378 bool insn_is_label = false;
379 struct eval_hints hints;
380 int opnum;
381 int critical;
382 bool first;
383 bool recover;
385 restart_parse:
386 first = true;
387 result->forw_ref = false;
389 stdscan_reset();
390 stdscan_set(buffer);
391 i = stdscan(NULL, &tokval);
393 result->label = NULL; /* Assume no label */
394 result->eops = NULL; /* must do this, whatever happens */
395 result->operands = 0; /* must initialize this */
396 result->evex_rm = 0; /* Ensure EVEX rounding mode is reset */
397 result->evex_brerop = -1; /* Reset EVEX broadcasting/ER op position */
399 /* Ignore blank lines */
400 if (i == TOKEN_EOS)
401 goto fail;
403 if (i != TOKEN_ID &&
404 i != TOKEN_INSN &&
405 i != TOKEN_PREFIX &&
406 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
407 nasm_error(ERR_NONFATAL,
408 "label or instruction expected at start of line");
409 goto fail;
412 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
413 /* there's a label here */
414 first = false;
415 result->label = tokval.t_charptr;
416 i = stdscan(NULL, &tokval);
417 if (i == ':') { /* skip over the optional colon */
418 i = stdscan(NULL, &tokval);
419 } else if (i == 0) {
420 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
421 "label alone on a line without a colon might be in error");
423 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
425 * FIXME: location->segment could be NO_SEG, in which case
426 * it is possible we should be passing 'abs_seg'. Look into this.
427 * Work out whether that is *really* what we should be doing.
428 * Generally fix things. I think this is right as it is, but
429 * am still not certain.
431 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
432 location->offset, NULL, true, false);
436 /* Just a label here */
437 if (i == TOKEN_EOS)
438 goto fail;
440 nasm_build_assert(P_none != 0);
441 memset(result->prefixes, P_none, sizeof(result->prefixes));
442 result->times = 1L;
444 while (i == TOKEN_PREFIX ||
445 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
446 first = false;
449 * Handle special case: the TIMES prefix.
451 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
452 expr *value;
454 i = stdscan(NULL, &tokval);
455 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
456 i = tokval.t_type;
457 if (!value) /* Error in evaluator */
458 goto fail;
459 if (!is_simple(value)) {
460 nasm_error(ERR_NONFATAL,
461 "non-constant argument supplied to TIMES");
462 result->times = 1L;
463 } else {
464 result->times = value->value;
465 if (value->value < 0 && pass0 == 2) {
466 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
467 value->value);
468 result->times = 0;
471 } else {
472 int slot = prefix_slot(tokval.t_integer);
473 if (result->prefixes[slot]) {
474 if (result->prefixes[slot] == tokval.t_integer)
475 nasm_error(ERR_WARNING | ERR_PASS1,
476 "instruction has redundant prefixes");
477 else
478 nasm_error(ERR_NONFATAL,
479 "instruction has conflicting prefixes");
481 result->prefixes[slot] = tokval.t_integer;
482 i = stdscan(NULL, &tokval);
486 if (i != TOKEN_INSN) {
487 int j;
488 enum prefixes pfx;
490 for (j = 0; j < MAXPREFIX; j++) {
491 if ((pfx = result->prefixes[j]) != P_none)
492 break;
495 if (i == 0 && pfx != P_none) {
497 * Instruction prefixes are present, but no actual
498 * instruction. This is allowed: at this point we
499 * invent a notional instruction of RESB 0.
501 result->opcode = I_RESB;
502 result->operands = 1;
503 result->oprs[0].type = IMMEDIATE;
504 result->oprs[0].offset = 0L;
505 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
506 return result;
507 } else {
508 nasm_error(ERR_NONFATAL, "parser: instruction expected");
509 goto fail;
513 result->opcode = tokval.t_integer;
514 result->condition = tokval.t_inttwo;
517 * INCBIN cannot be satisfied with incorrectly
518 * evaluated operands, since the correct values _must_ be known
519 * on the first pass. Hence, even in pass one, we set the
520 * `critical' flag on calling evaluate(), so that it will bomb
521 * out on undefined symbols.
523 if (result->opcode == I_INCBIN) {
524 critical = (pass0 < 2 ? 1 : 2);
526 } else
527 critical = (pass == 2 ? 2 : 0);
529 if (result->opcode == I_DB || result->opcode == I_DW ||
530 result->opcode == I_DD || result->opcode == I_DQ ||
531 result->opcode == I_DT || result->opcode == I_DO ||
532 result->opcode == I_DY || result->opcode == I_INCBIN) {
533 extop *eop, **tail = &result->eops, **fixptr;
534 int oper_num = 0;
535 int32_t sign;
537 result->eops_float = false;
540 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
542 while (1) {
543 i = stdscan(NULL, &tokval);
544 if (i == TOKEN_EOS)
545 break;
546 else if (first && i == ':') {
547 insn_is_label = true;
548 goto restart_parse;
550 first = false;
551 fixptr = tail;
552 eop = *tail = nasm_malloc(sizeof(extop));
553 tail = &eop->next;
554 eop->next = NULL;
555 eop->type = EOT_NOTHING;
556 oper_num++;
557 sign = +1;
560 * is_comma_next() here is to distinguish this from
561 * a string used as part of an expression...
563 if (i == TOKEN_STR && is_comma_next()) {
564 eop->type = EOT_DB_STRING;
565 eop->stringval = tokval.t_charptr;
566 eop->stringlen = tokval.t_inttwo;
567 i = stdscan(NULL, &tokval); /* eat the comma */
568 } else if (i == TOKEN_STRFUNC) {
569 bool parens = false;
570 const char *funcname = tokval.t_charptr;
571 enum strfunc func = tokval.t_integer;
572 i = stdscan(NULL, &tokval);
573 if (i == '(') {
574 parens = true;
575 i = stdscan(NULL, &tokval);
577 if (i != TOKEN_STR) {
578 nasm_error(ERR_NONFATAL,
579 "%s must be followed by a string constant",
580 funcname);
581 eop->type = EOT_NOTHING;
582 } else {
583 eop->type = EOT_DB_STRING_FREE;
584 eop->stringlen =
585 string_transform(tokval.t_charptr, tokval.t_inttwo,
586 &eop->stringval, func);
587 if (eop->stringlen == (size_t)-1) {
588 nasm_error(ERR_NONFATAL, "invalid string for transform");
589 eop->type = EOT_NOTHING;
592 if (parens && i && i != ')') {
593 i = stdscan(NULL, &tokval);
594 if (i != ')') {
595 nasm_error(ERR_NONFATAL, "unterminated %s function",
596 funcname);
599 if (i && i != ',')
600 i = stdscan(NULL, &tokval);
601 } else if (i == '-' || i == '+') {
602 char *save = stdscan_get();
603 int token = i;
604 sign = (i == '-') ? -1 : 1;
605 i = stdscan(NULL, &tokval);
606 if (i != TOKEN_FLOAT) {
607 stdscan_set(save);
608 i = tokval.t_type = token;
609 goto is_expression;
610 } else {
611 goto is_float;
613 } else if (i == TOKEN_FLOAT) {
614 is_float:
615 eop->type = EOT_DB_STRING;
616 result->eops_float = true;
618 eop->stringlen = idata_bytes(result->opcode);
619 if (eop->stringlen > 16) {
620 nasm_error(ERR_NONFATAL, "floating-point constant"
621 " encountered in DY instruction");
622 eop->stringlen = 0;
623 } else if (eop->stringlen < 1) {
624 nasm_error(ERR_NONFATAL, "floating-point constant"
625 " encountered in unknown instruction");
627 * fix suggested by Pedro Gimeno... original line was:
628 * eop->type = EOT_NOTHING;
630 eop->stringlen = 0;
633 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
634 tail = &eop->next;
635 *fixptr = eop;
636 eop->stringval = (char *)eop + sizeof(extop);
637 if (!eop->stringlen ||
638 !float_const(tokval.t_charptr, sign,
639 (uint8_t *)eop->stringval,
640 eop->stringlen, nasm_error))
641 eop->type = EOT_NOTHING;
642 i = stdscan(NULL, &tokval); /* eat the comma */
643 } else {
644 /* anything else, assume it is an expression */
645 expr *value;
647 is_expression:
648 value = evaluate(stdscan, NULL, &tokval, NULL,
649 critical, nasm_error, NULL);
650 i = tokval.t_type;
651 if (!value) /* Error in evaluator */
652 goto fail;
653 if (is_unknown(value)) {
654 eop->type = EOT_DB_NUMBER;
655 eop->offset = 0; /* doesn't matter what we put */
656 eop->segment = eop->wrt = NO_SEG; /* likewise */
657 } else if (is_reloc(value)) {
658 eop->type = EOT_DB_NUMBER;
659 eop->offset = reloc_value(value);
660 eop->segment = reloc_seg(value);
661 eop->wrt = reloc_wrt(value);
662 } else {
663 nasm_error(ERR_NONFATAL,
664 "operand %d: expression is not simple"
665 " or relocatable", oper_num);
670 * We're about to call stdscan(), which will eat the
671 * comma that we're currently sitting on between
672 * arguments. However, we'd better check first that it
673 * _is_ a comma.
675 if (i == TOKEN_EOS) /* also could be EOL */
676 break;
677 if (i != ',') {
678 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
679 oper_num);
680 goto fail;
684 if (result->opcode == I_INCBIN) {
686 * Correct syntax for INCBIN is that there should be
687 * one string operand, followed by one or two numeric
688 * operands.
690 if (!result->eops || result->eops->type != EOT_DB_STRING)
691 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
692 else if (result->eops->next &&
693 result->eops->next->type != EOT_DB_NUMBER)
694 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
695 " non-numeric");
696 else if (result->eops->next && result->eops->next->next &&
697 result->eops->next->next->type != EOT_DB_NUMBER)
698 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
699 " non-numeric");
700 else if (result->eops->next && result->eops->next->next &&
701 result->eops->next->next->next)
702 nasm_error(ERR_NONFATAL,
703 "`incbin': more than three parameters");
704 else
705 return result;
707 * If we reach here, one of the above errors happened.
708 * Throw the instruction away.
710 goto fail;
711 } else /* DB ... */ if (oper_num == 0)
712 nasm_error(ERR_WARNING | ERR_PASS1,
713 "no operand for data declaration");
714 else
715 result->operands = oper_num;
717 return result;
721 * Now we begin to parse the operands. There may be up to four
722 * of these, separated by commas, and terminated by a zero token.
725 for (opnum = 0; opnum < MAX_OPERANDS; opnum++) {
726 operand *op = &result->oprs[opnum];
727 expr *value; /* used most of the time */
728 int mref; /* is this going to be a memory ref? */
729 int bracket; /* is it a [] mref, or a & mref? */
730 int setsize = 0;
731 decoflags_t brace_flags = 0; /* flags for decorators in braces */
733 op->disp_size = 0; /* have to zero this whatever */
734 op->eaflags = 0; /* and this */
735 op->opflags = 0;
736 op->decoflags = 0;
738 i = stdscan(NULL, &tokval);
739 if (i == TOKEN_EOS)
740 break; /* end of operands: get out of here */
741 else if (first && i == ':') {
742 insn_is_label = true;
743 goto restart_parse;
745 first = false;
746 op->type = 0; /* so far, no override */
747 while (i == TOKEN_SPECIAL) { /* size specifiers */
748 switch ((int)tokval.t_integer) {
749 case S_BYTE:
750 if (!setsize) /* we want to use only the first */
751 op->type |= BITS8;
752 setsize = 1;
753 break;
754 case S_WORD:
755 if (!setsize)
756 op->type |= BITS16;
757 setsize = 1;
758 break;
759 case S_DWORD:
760 case S_LONG:
761 if (!setsize)
762 op->type |= BITS32;
763 setsize = 1;
764 break;
765 case S_QWORD:
766 if (!setsize)
767 op->type |= BITS64;
768 setsize = 1;
769 break;
770 case S_TWORD:
771 if (!setsize)
772 op->type |= BITS80;
773 setsize = 1;
774 break;
775 case S_OWORD:
776 if (!setsize)
777 op->type |= BITS128;
778 setsize = 1;
779 break;
780 case S_YWORD:
781 if (!setsize)
782 op->type |= BITS256;
783 setsize = 1;
784 break;
785 case S_ZWORD:
786 if (!setsize)
787 op->type |= BITS512;
788 setsize = 1;
789 break;
790 case S_TO:
791 op->type |= TO;
792 break;
793 case S_STRICT:
794 op->type |= STRICT;
795 break;
796 case S_FAR:
797 op->type |= FAR;
798 break;
799 case S_NEAR:
800 op->type |= NEAR;
801 break;
802 case S_SHORT:
803 op->type |= SHORT;
804 break;
805 default:
806 nasm_error(ERR_NONFATAL, "invalid operand size specification");
808 i = stdscan(NULL, &tokval);
811 if (i == '[' || i == '&') { /* memory reference */
812 mref = true;
813 bracket = (i == '[');
814 i = stdscan(NULL, &tokval); /* then skip the colon */
815 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
816 process_size_override(result, op);
817 i = stdscan(NULL, &tokval);
819 } else { /* immediate operand, or register */
820 mref = false;
821 bracket = false; /* placate optimisers */
824 if ((op->type & FAR) && !mref &&
825 result->opcode != I_JMP && result->opcode != I_CALL) {
826 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
829 value = evaluate(stdscan, NULL, &tokval,
830 &op->opflags,
831 critical, nasm_error, &hints);
832 i = tokval.t_type;
833 if (op->opflags & OPFLAG_FORWARD) {
834 result->forw_ref = true;
836 if (!value) /* Error in evaluator */
837 goto fail;
838 if (i == ':' && mref) { /* it was seg:offset */
840 * Process the segment override.
842 if (value[1].type != 0 ||
843 value->value != 1 ||
844 !IS_SREG(value->type))
845 nasm_error(ERR_NONFATAL, "invalid segment override");
846 else if (result->prefixes[PPS_SEG])
847 nasm_error(ERR_NONFATAL,
848 "instruction has conflicting segment overrides");
849 else {
850 result->prefixes[PPS_SEG] = value->type;
851 if (IS_FSGS(value->type))
852 op->eaflags |= EAF_FSGS;
855 i = stdscan(NULL, &tokval); /* then skip the colon */
856 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
857 process_size_override(result, op);
858 i = stdscan(NULL, &tokval);
860 value = evaluate(stdscan, NULL, &tokval,
861 &op->opflags,
862 critical, nasm_error, &hints);
863 i = tokval.t_type;
864 if (op->opflags & OPFLAG_FORWARD) {
865 result->forw_ref = true;
867 /* and get the offset */
868 if (!value) /* Error in evaluator */
869 goto fail;
872 recover = false;
873 if (mref && bracket) { /* find ] at the end */
874 if (i != ']') {
875 nasm_error(ERR_NONFATAL, "parser: expecting ]");
876 recover = true;
877 } else { /* we got the required ] */
878 i = stdscan(NULL, &tokval);
879 if ((i == TOKEN_DECORATOR) || (i == TOKEN_OPMASK)) {
881 * according to AVX512 spec, broacast or opmask decorator
882 * is expected for memory reference operands
884 if (tokval.t_flag & TFLAG_BRDCAST) {
885 brace_flags |= GEN_BRDCAST(0);
886 i = stdscan(NULL, &tokval);
887 } else if (i == TOKEN_OPMASK) {
888 brace_flags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
889 i = stdscan(NULL, &tokval);
890 } else {
891 nasm_error(ERR_NONFATAL, "broadcast or opmask "
892 "decorator expected inside braces");
893 recover = true;
897 if (i != 0 && i != ',') {
898 nasm_error(ERR_NONFATAL, "comma or end of line expected");
899 recover = true;
902 } else { /* immediate operand */
903 if (i != 0 && i != ',' && i != ':' &&
904 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
905 nasm_error(ERR_NONFATAL, "comma, colon, decorator or end of "
906 "line expected after operand");
907 recover = true;
908 } else if (i == ':') {
909 op->type |= COLON;
910 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
911 /* parse opmask (and zeroing) after an operand */
912 recover = parse_braces(&brace_flags);
915 if (recover) {
916 do { /* error recovery */
917 i = stdscan(NULL, &tokval);
918 } while (i != 0 && i != ',');
922 * now convert the exprs returned from evaluate()
923 * into operand descriptions...
926 if (mref) { /* it's a memory reference */
927 if (parse_mref(op, value, &hints, brace_flags))
928 goto fail;
929 } else { /* it's not a memory reference */
930 if (is_just_unknown(value)) { /* it's immediate but unknown */
931 op->type |= IMMEDIATE;
932 op->opflags |= OPFLAG_UNKNOWN;
933 op->offset = 0; /* don't care */
934 op->segment = NO_SEG; /* don't care again */
935 op->wrt = NO_SEG; /* still don't care */
937 if(optimizing >= 0 && !(op->type & STRICT)) {
938 /* Be optimistic */
939 op->type |=
940 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
942 } else if (is_reloc(value)) { /* it's immediate */
943 op->type |= IMMEDIATE;
944 op->offset = reloc_value(value);
945 op->segment = reloc_seg(value);
946 op->wrt = reloc_wrt(value);
948 if (is_simple(value)) {
949 uint64_t n = reloc_value(value);
950 if (n == 1)
951 op->type |= UNITY;
952 if (optimizing >= 0 &&
953 !(op->type & STRICT)) {
954 if ((uint32_t) (n + 128) <= 255)
955 op->type |= SBYTEDWORD;
956 if ((uint16_t) (n + 128) <= 255)
957 op->type |= SBYTEWORD;
958 if (n <= 0xFFFFFFFF)
959 op->type |= UDWORD;
960 if (n + 0x80000000 <= 0xFFFFFFFF)
961 op->type |= SDWORD;
964 } else if(value->type == EXPR_RDSAE) {
966 * it's not an operand but a rounding or SAE decorator.
967 * put the decorator information in the (opflag_t) type field
968 * of previous operand.
970 opnum--; op--;
971 switch (value->value) {
972 case BRC_RN:
973 case BRC_RU:
974 case BRC_RD:
975 case BRC_RZ:
976 case BRC_SAE:
977 op->decoflags |= (value->value == BRC_SAE ? SAE : ER);
978 result->evex_rm = value->value;
979 break;
980 default:
981 nasm_error(ERR_NONFATAL, "invalid decorator");
982 break;
984 } else { /* it's a register */
985 opflags_t rs;
987 if (value->type >= EXPR_SIMPLE || value->value != 1) {
988 nasm_error(ERR_NONFATAL, "invalid operand type");
989 goto fail;
993 * check that its only 1 register, not an expression...
995 for (i = 1; value[i].type; i++)
996 if (value[i].value) {
997 nasm_error(ERR_NONFATAL, "invalid operand type");
998 goto fail;
1001 /* clear overrides, except TO which applies to FPU regs */
1002 if (op->type & ~TO) {
1004 * we want to produce a warning iff the specified size
1005 * is different from the register size
1007 rs = op->type & SIZE_MASK;
1008 } else
1009 rs = 0;
1011 op->type &= TO;
1012 op->type |= REGISTER;
1013 op->type |= nasm_reg_flags[value->type];
1014 op->decoflags |= brace_flags;
1015 op->basereg = value->type;
1017 if (rs && (op->type & SIZE_MASK) != rs)
1018 nasm_error(ERR_WARNING | ERR_PASS1,
1019 "register size specification ignored");
1023 /* remember the position of operand having broadcasting/ER mode */
1024 if (op->decoflags & (BRDCAST_MASK | ER | SAE))
1025 result->evex_brerop = opnum;
1028 result->operands = opnum; /* set operand count */
1030 /* clear remaining operands */
1031 while (opnum < MAX_OPERANDS)
1032 result->oprs[opnum++].type = 0;
1035 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
1037 switch (result->opcode) {
1038 case I_RESW:
1039 result->opcode = I_RESB;
1040 result->oprs[0].offset *= 2;
1041 break;
1042 case I_RESD:
1043 result->opcode = I_RESB;
1044 result->oprs[0].offset *= 4;
1045 break;
1046 case I_RESQ:
1047 result->opcode = I_RESB;
1048 result->oprs[0].offset *= 8;
1049 break;
1050 case I_REST:
1051 result->opcode = I_RESB;
1052 result->oprs[0].offset *= 10;
1053 break;
1054 case I_RESO:
1055 result->opcode = I_RESB;
1056 result->oprs[0].offset *= 16;
1057 break;
1058 case I_RESY:
1059 result->opcode = I_RESB;
1060 result->oprs[0].offset *= 32;
1061 break;
1062 default:
1063 break;
1066 return result;
1068 fail:
1069 result->opcode = I_none;
1070 return result;
1073 static int is_comma_next(void)
1075 struct tokenval tv;
1076 char *p;
1077 int i;
1079 p = stdscan_get();
1080 i = stdscan(NULL, &tv);
1081 stdscan_set(p);
1083 return (i == ',' || i == ';' || !i);
1086 void cleanup_insn(insn * i)
1088 extop *e;
1090 while ((e = i->eops)) {
1091 i->eops = e->next;
1092 if (e->type == EOT_DB_STRING_FREE)
1093 nasm_free(e->stringval);
1094 nasm_free(e);