AVX-512: Add OPMASK instructions
[nasm/avx512.git] / parser.c
blob1b0865778c0d2d4776da901f99058f1f32d390dc
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, int operand)
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 result->oprs[operand].type |= BITS8;
127 break;
128 case S_WORD:
129 result->oprs[operand].type |= BITS16;
130 break;
131 case S_DWORD:
132 case S_LONG:
133 result->oprs[operand].type |= BITS32;
134 break;
135 case S_QWORD:
136 result->oprs[operand].type |= BITS64;
137 break;
138 case S_TWORD:
139 result->oprs[operand].type |= BITS80;
140 break;
141 case S_OWORD:
142 result->oprs[operand].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 result->oprs[operand].eaflags |= EAF_TIMESTWO;
154 break;
155 case S_REL:
156 result->oprs[operand].eaflags |= EAF_REL;
157 break;
158 case S_ABS:
159 result->oprs[operand].eaflags |= EAF_ABS;
160 break;
161 case S_BYTE:
162 result->oprs[operand].disp_size = 8;
163 result->oprs[operand].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 result->oprs[operand].disp_size = 16;
177 result->oprs[operand].eaflags |= EAF_WORDOFFS;
178 break;
179 case S_DWORD:
180 case S_LONG:
181 result->oprs[operand].disp_size = 32;
182 result->oprs[operand].eaflags |= EAF_WORDOFFS;
183 break;
184 case S_QWORD:
185 result->oprs[operand].disp_size = 64;
186 result->oprs[operand].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 insn *parse_line(int pass, char *buffer, insn *result, ldfunc ldef)
247 bool insn_is_label = false;
248 struct eval_hints hints;
249 int operand;
250 int critical;
251 bool first;
252 bool recover;
254 restart_parse:
255 first = true;
256 result->forw_ref = false;
258 stdscan_reset();
259 stdscan_set(buffer);
260 i = stdscan(NULL, &tokval);
262 result->label = NULL; /* Assume no label */
263 result->eops = NULL; /* must do this, whatever happens */
264 result->operands = 0; /* must initialize this */
265 result->evex_rm = 0; /* Ensure EVEX rounding mode is reset */
266 result->evex_brerop = -1; /* Reset EVEX broadcasting/ER op position */
268 /* Ignore blank lines */
269 if (i == TOKEN_EOS) {
270 result->opcode = I_none;
271 return result;
274 if (i != TOKEN_ID &&
275 i != TOKEN_INSN &&
276 i != TOKEN_PREFIX &&
277 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
278 nasm_error(ERR_NONFATAL,
279 "label or instruction expected at start of line");
280 result->opcode = I_none;
281 return result;
284 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
285 /* there's a label here */
286 first = false;
287 result->label = tokval.t_charptr;
288 i = stdscan(NULL, &tokval);
289 if (i == ':') { /* skip over the optional colon */
290 i = stdscan(NULL, &tokval);
291 } else if (i == 0) {
292 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
293 "label alone on a line without a colon might be in error");
295 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
297 * FIXME: location->segment could be NO_SEG, in which case
298 * it is possible we should be passing 'abs_seg'. Look into this.
299 * Work out whether that is *really* what we should be doing.
300 * Generally fix things. I think this is right as it is, but
301 * am still not certain.
303 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
304 location->offset, NULL, true, false);
308 /* Just a label here */
309 if (i == TOKEN_EOS) {
310 result->opcode = I_none;
311 return result;
314 nasm_build_assert(P_none != 0);
315 memset(result->prefixes, P_none, sizeof(result->prefixes));
316 result->times = 1L;
318 while (i == TOKEN_PREFIX ||
319 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
320 first = false;
323 * Handle special case: the TIMES prefix.
325 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
326 expr *value;
328 i = stdscan(NULL, &tokval);
329 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
330 i = tokval.t_type;
331 if (!value) { /* but, error in evaluator */
332 result->opcode = I_none; /* unrecoverable parse error: */
333 return result; /* ignore this instruction */
335 if (!is_simple(value)) {
336 nasm_error(ERR_NONFATAL,
337 "non-constant argument supplied to TIMES");
338 result->times = 1L;
339 } else {
340 result->times = value->value;
341 if (value->value < 0 && pass0 == 2) {
342 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
343 value->value);
344 result->times = 0;
347 } else {
348 int slot = prefix_slot(tokval.t_integer);
349 if (result->prefixes[slot]) {
350 if (result->prefixes[slot] == tokval.t_integer)
351 nasm_error(ERR_WARNING | ERR_PASS1,
352 "instruction has redundant prefixes");
353 else
354 nasm_error(ERR_NONFATAL,
355 "instruction has conflicting prefixes");
357 result->prefixes[slot] = tokval.t_integer;
358 i = stdscan(NULL, &tokval);
362 if (i != TOKEN_INSN) {
363 int j;
364 enum prefixes pfx;
366 for (j = 0; j < MAXPREFIX; j++) {
367 if ((pfx = result->prefixes[j]) != P_none)
368 break;
371 if (i == 0 && pfx != P_none) {
373 * Instruction prefixes are present, but no actual
374 * instruction. This is allowed: at this point we
375 * invent a notional instruction of RESB 0.
377 result->opcode = I_RESB;
378 result->operands = 1;
379 result->oprs[0].type = IMMEDIATE;
380 result->oprs[0].offset = 0L;
381 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
382 return result;
383 } else {
384 nasm_error(ERR_NONFATAL, "parser: instruction expected");
385 result->opcode = I_none;
386 return result;
390 result->opcode = tokval.t_integer;
391 result->condition = tokval.t_inttwo;
394 * INCBIN cannot be satisfied with incorrectly
395 * evaluated operands, since the correct values _must_ be known
396 * on the first pass. Hence, even in pass one, we set the
397 * `critical' flag on calling evaluate(), so that it will bomb
398 * out on undefined symbols.
400 if (result->opcode == I_INCBIN) {
401 critical = (pass0 < 2 ? 1 : 2);
403 } else
404 critical = (pass == 2 ? 2 : 0);
406 if (result->opcode == I_DB || result->opcode == I_DW ||
407 result->opcode == I_DD || result->opcode == I_DQ ||
408 result->opcode == I_DT || result->opcode == I_DO ||
409 result->opcode == I_DY || result->opcode == I_INCBIN) {
410 extop *eop, **tail = &result->eops, **fixptr;
411 int oper_num = 0;
412 int32_t sign;
414 result->eops_float = false;
417 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
419 while (1) {
420 i = stdscan(NULL, &tokval);
421 if (i == TOKEN_EOS)
422 break;
423 else if (first && i == ':') {
424 insn_is_label = true;
425 goto restart_parse;
427 first = false;
428 fixptr = tail;
429 eop = *tail = nasm_malloc(sizeof(extop));
430 tail = &eop->next;
431 eop->next = NULL;
432 eop->type = EOT_NOTHING;
433 oper_num++;
434 sign = +1;
437 * is_comma_next() here is to distinguish this from
438 * a string used as part of an expression...
440 if (i == TOKEN_STR && is_comma_next()) {
441 eop->type = EOT_DB_STRING;
442 eop->stringval = tokval.t_charptr;
443 eop->stringlen = tokval.t_inttwo;
444 i = stdscan(NULL, &tokval); /* eat the comma */
445 } else if (i == TOKEN_STRFUNC) {
446 bool parens = false;
447 const char *funcname = tokval.t_charptr;
448 enum strfunc func = tokval.t_integer;
449 i = stdscan(NULL, &tokval);
450 if (i == '(') {
451 parens = true;
452 i = stdscan(NULL, &tokval);
454 if (i != TOKEN_STR) {
455 nasm_error(ERR_NONFATAL,
456 "%s must be followed by a string constant",
457 funcname);
458 eop->type = EOT_NOTHING;
459 } else {
460 eop->type = EOT_DB_STRING_FREE;
461 eop->stringlen =
462 string_transform(tokval.t_charptr, tokval.t_inttwo,
463 &eop->stringval, func);
464 if (eop->stringlen == (size_t)-1) {
465 nasm_error(ERR_NONFATAL, "invalid string for transform");
466 eop->type = EOT_NOTHING;
469 if (parens && i && i != ')') {
470 i = stdscan(NULL, &tokval);
471 if (i != ')') {
472 nasm_error(ERR_NONFATAL, "unterminated %s function",
473 funcname);
476 if (i && i != ',')
477 i = stdscan(NULL, &tokval);
478 } else if (i == '-' || i == '+') {
479 char *save = stdscan_get();
480 int token = i;
481 sign = (i == '-') ? -1 : 1;
482 i = stdscan(NULL, &tokval);
483 if (i != TOKEN_FLOAT) {
484 stdscan_set(save);
485 i = tokval.t_type = token;
486 goto is_expression;
487 } else {
488 goto is_float;
490 } else if (i == TOKEN_FLOAT) {
491 is_float:
492 eop->type = EOT_DB_STRING;
493 result->eops_float = true;
495 eop->stringlen = idata_bytes(result->opcode);
496 if (eop->stringlen > 16) {
497 nasm_error(ERR_NONFATAL, "floating-point constant"
498 " encountered in DY instruction");
499 eop->stringlen = 0;
500 } else if (eop->stringlen < 1) {
501 nasm_error(ERR_NONFATAL, "floating-point constant"
502 " encountered in unknown instruction");
504 * fix suggested by Pedro Gimeno... original line was:
505 * eop->type = EOT_NOTHING;
507 eop->stringlen = 0;
510 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
511 tail = &eop->next;
512 *fixptr = eop;
513 eop->stringval = (char *)eop + sizeof(extop);
514 if (!eop->stringlen ||
515 !float_const(tokval.t_charptr, sign,
516 (uint8_t *)eop->stringval,
517 eop->stringlen, nasm_error))
518 eop->type = EOT_NOTHING;
519 i = stdscan(NULL, &tokval); /* eat the comma */
520 } else {
521 /* anything else, assume it is an expression */
522 expr *value;
524 is_expression:
525 value = evaluate(stdscan, NULL, &tokval, NULL,
526 critical, nasm_error, NULL);
527 i = tokval.t_type;
528 if (!value) { /* error in evaluator */
529 result->opcode = I_none; /* unrecoverable parse error: */
530 return result; /* ignore this instruction */
532 if (is_unknown(value)) {
533 eop->type = EOT_DB_NUMBER;
534 eop->offset = 0; /* doesn't matter what we put */
535 eop->segment = eop->wrt = NO_SEG; /* likewise */
536 } else if (is_reloc(value)) {
537 eop->type = EOT_DB_NUMBER;
538 eop->offset = reloc_value(value);
539 eop->segment = reloc_seg(value);
540 eop->wrt = reloc_wrt(value);
541 } else {
542 nasm_error(ERR_NONFATAL,
543 "operand %d: expression is not simple"
544 " or relocatable", oper_num);
549 * We're about to call stdscan(), which will eat the
550 * comma that we're currently sitting on between
551 * arguments. However, we'd better check first that it
552 * _is_ a comma.
554 if (i == TOKEN_EOS) /* also could be EOL */
555 break;
556 if (i != ',') {
557 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
558 oper_num);
559 result->opcode = I_none;/* unrecoverable parse error: */
560 return result; /* ignore this instruction */
564 if (result->opcode == I_INCBIN) {
566 * Correct syntax for INCBIN is that there should be
567 * one string operand, followed by one or two numeric
568 * operands.
570 if (!result->eops || result->eops->type != EOT_DB_STRING)
571 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
572 else if (result->eops->next &&
573 result->eops->next->type != EOT_DB_NUMBER)
574 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
575 " non-numeric");
576 else if (result->eops->next && result->eops->next->next &&
577 result->eops->next->next->type != EOT_DB_NUMBER)
578 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
579 " non-numeric");
580 else if (result->eops->next && result->eops->next->next &&
581 result->eops->next->next->next)
582 nasm_error(ERR_NONFATAL,
583 "`incbin': more than three parameters");
584 else
585 return result;
587 * If we reach here, one of the above errors happened.
588 * Throw the instruction away.
590 result->opcode = I_none;
591 return result;
592 } else /* DB ... */ if (oper_num == 0)
593 nasm_error(ERR_WARNING | ERR_PASS1,
594 "no operand for data declaration");
595 else
596 result->operands = oper_num;
598 return result;
602 * Now we begin to parse the operands. There may be up to four
603 * of these, separated by commas, and terminated by a zero token.
606 for (operand = 0; operand < MAX_OPERANDS; operand++) {
607 expr *value; /* used most of the time */
608 int mref; /* is this going to be a memory ref? */
609 int bracket; /* is it a [] mref, or a & mref? */
610 int setsize = 0;
611 decoflags_t brace_flags = 0; /* flags for decorators in braces */
613 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
614 result->oprs[operand].eaflags = 0; /* and this */
615 result->oprs[operand].opflags = 0;
616 result->oprs[operand].decoflags = 0;
618 i = stdscan(NULL, &tokval);
619 if (i == TOKEN_EOS)
620 break; /* end of operands: get out of here */
621 else if (first && i == ':') {
622 insn_is_label = true;
623 goto restart_parse;
625 first = false;
626 result->oprs[operand].type = 0; /* so far, no override */
627 while (i == TOKEN_SPECIAL) { /* size specifiers */
628 switch ((int)tokval.t_integer) {
629 case S_BYTE:
630 if (!setsize) /* we want to use only the first */
631 result->oprs[operand].type |= BITS8;
632 setsize = 1;
633 break;
634 case S_WORD:
635 if (!setsize)
636 result->oprs[operand].type |= BITS16;
637 setsize = 1;
638 break;
639 case S_DWORD:
640 case S_LONG:
641 if (!setsize)
642 result->oprs[operand].type |= BITS32;
643 setsize = 1;
644 break;
645 case S_QWORD:
646 if (!setsize)
647 result->oprs[operand].type |= BITS64;
648 setsize = 1;
649 break;
650 case S_TWORD:
651 if (!setsize)
652 result->oprs[operand].type |= BITS80;
653 setsize = 1;
654 break;
655 case S_OWORD:
656 if (!setsize)
657 result->oprs[operand].type |= BITS128;
658 setsize = 1;
659 break;
660 case S_YWORD:
661 if (!setsize)
662 result->oprs[operand].type |= BITS256;
663 setsize = 1;
664 break;
665 case S_ZWORD:
666 if (!setsize)
667 result->oprs[operand].type |= BITS512;
668 setsize = 1;
669 break;
670 case S_TO:
671 result->oprs[operand].type |= TO;
672 break;
673 case S_STRICT:
674 result->oprs[operand].type |= STRICT;
675 break;
676 case S_FAR:
677 result->oprs[operand].type |= FAR;
678 break;
679 case S_NEAR:
680 result->oprs[operand].type |= NEAR;
681 break;
682 case S_SHORT:
683 result->oprs[operand].type |= SHORT;
684 break;
685 default:
686 nasm_error(ERR_NONFATAL, "invalid operand size specification");
688 i = stdscan(NULL, &tokval);
691 if (i == '[' || i == '&') { /* memory reference */
692 mref = true;
693 bracket = (i == '[');
694 i = stdscan(NULL, &tokval); /* then skip the colon */
695 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
696 process_size_override(result, operand);
697 i = stdscan(NULL, &tokval);
699 } else { /* immediate operand, or register */
700 mref = false;
701 bracket = false; /* placate optimisers */
704 if ((result->oprs[operand].type & FAR) && !mref &&
705 result->opcode != I_JMP && result->opcode != I_CALL) {
706 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
709 value = evaluate(stdscan, NULL, &tokval,
710 &result->oprs[operand].opflags,
711 critical, nasm_error, &hints);
712 i = tokval.t_type;
713 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
714 result->forw_ref = true;
716 if (!value) { /* nasm_error in evaluator */
717 result->opcode = I_none; /* unrecoverable parse error: */
718 return result; /* ignore this instruction */
720 if (i == ':' && mref) { /* it was seg:offset */
722 * Process the segment override.
724 if (value[1].type != 0 ||
725 value->value != 1 ||
726 !IS_SREG(value->type))
727 nasm_error(ERR_NONFATAL, "invalid segment override");
728 else if (result->prefixes[PPS_SEG])
729 nasm_error(ERR_NONFATAL,
730 "instruction has conflicting segment overrides");
731 else {
732 result->prefixes[PPS_SEG] = value->type;
733 if (IS_FSGS(value->type))
734 result->oprs[operand].eaflags |= EAF_FSGS;
737 i = stdscan(NULL, &tokval); /* then skip the colon */
738 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
739 process_size_override(result, operand);
740 i = stdscan(NULL, &tokval);
742 value = evaluate(stdscan, NULL, &tokval,
743 &result->oprs[operand].opflags,
744 critical, nasm_error, &hints);
745 i = tokval.t_type;
746 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
747 result->forw_ref = true;
749 /* and get the offset */
750 if (!value) { /* but, error in evaluator */
751 result->opcode = I_none; /* unrecoverable parse error: */
752 return result; /* ignore this instruction */
756 recover = false;
757 if (mref && bracket) { /* find ] at the end */
758 if (i != ']') {
759 nasm_error(ERR_NONFATAL, "parser: expecting ]");
760 recover = true;
761 } else { /* we got the required ] */
762 i = stdscan(NULL, &tokval);
763 if ((i == TOKEN_DECORATOR) || (i == TOKEN_OPMASK)) {
765 * according to AVX512 spec, broacast or opmask decorator
766 * is expected for memory reference operands
768 if (tokval.t_flag & TFLAG_BRDCAST) {
769 brace_flags |= GEN_BRDCAST(0);
770 i = stdscan(NULL, &tokval);
771 } else if (i == TOKEN_OPMASK) {
772 brace_flags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
773 i = stdscan(NULL, &tokval);
774 } else {
775 nasm_error(ERR_NONFATAL, "broadcast or opmask "
776 "decorator expected inside braces");
777 recover = true;
781 if (i != 0 && i != ',') {
782 nasm_error(ERR_NONFATAL, "comma or end of line expected");
783 recover = true;
786 } else { /* immediate operand */
787 if (i != 0 && i != ',' && i != ':' &&
788 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
789 nasm_error(ERR_NONFATAL, "comma, colon, decorator or end of "
790 "line expected after operand");
791 recover = true;
792 } else if (i == ':') {
793 result->oprs[operand].type |= COLON;
794 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
795 /* parse opmask (and zeroing) after an operand */
796 recover = parse_braces(&brace_flags);
799 if (recover) {
800 do { /* error recovery */
801 i = stdscan(NULL, &tokval);
802 } while (i != 0 && i != ',');
806 * now convert the exprs returned from evaluate()
807 * into operand descriptions...
810 if (mref) { /* it's a memory reference */
811 expr *e = value;
812 int b, i, s; /* basereg, indexreg, scale */
813 int64_t o; /* offset */
815 b = i = -1, o = s = 0;
816 result->oprs[operand].hintbase = hints.base;
817 result->oprs[operand].hinttype = hints.type;
819 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
820 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
822 if (is_gpr && e->value == 1)
823 b = e->type; /* It can be basereg */
824 else /* No, it has to be indexreg */
825 i = e->type, s = e->value;
826 e++;
828 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
829 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
831 if (b != -1) /* If the first was the base, ... */
832 i = e->type, s = e->value; /* second has to be indexreg */
834 else if (!is_gpr || e->value != 1) {
835 /* If both want to be index */
836 nasm_error(ERR_NONFATAL,
837 "invalid effective address: two index registers");
838 result->opcode = I_none;
839 return result;
840 } else
841 b = e->type;
842 e++;
844 if (e->type != 0) { /* is there an offset? */
845 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
846 nasm_error(ERR_NONFATAL,
847 "beroset-p-603-invalid effective address");
848 result->opcode = I_none;
849 return result;
850 } else {
851 if (e->type == EXPR_UNKNOWN) {
852 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
853 o = 0; /* doesn't matter what */
854 result->oprs[operand].wrt = NO_SEG; /* nor this */
855 result->oprs[operand].segment = NO_SEG; /* or this */
856 while (e->type)
857 e++; /* go to the end of the line */
858 } else {
859 if (e->type == EXPR_SIMPLE) {
860 o = e->value;
861 e++;
863 if (e->type == EXPR_WRT) {
864 result->oprs[operand].wrt = e->value;
865 e++;
866 } else
867 result->oprs[operand].wrt = NO_SEG;
869 * Look for a segment base type.
871 if (e->type && e->type < EXPR_SEGBASE) {
872 nasm_error(ERR_NONFATAL,
873 "beroset-p-630-invalid effective address");
874 result->opcode = I_none;
875 return result;
877 while (e->type && e->value == 0)
878 e++;
879 if (e->type && e->value != 1) {
880 nasm_error(ERR_NONFATAL,
881 "beroset-p-637-invalid effective address");
882 result->opcode = I_none;
883 return result;
885 if (e->type) {
886 result->oprs[operand].segment =
887 e->type - EXPR_SEGBASE;
888 e++;
889 } else
890 result->oprs[operand].segment = NO_SEG;
891 while (e->type && e->value == 0)
892 e++;
893 if (e->type) {
894 nasm_error(ERR_NONFATAL,
895 "beroset-p-650-invalid effective address");
896 result->opcode = I_none;
897 return result;
901 } else {
902 o = 0;
903 result->oprs[operand].wrt = NO_SEG;
904 result->oprs[operand].segment = NO_SEG;
907 if (e->type != 0) { /* there'd better be nothing left! */
908 nasm_error(ERR_NONFATAL,
909 "beroset-p-663-invalid effective address");
910 result->opcode = I_none;
911 return result;
914 /* It is memory, but it can match any r/m operand */
915 result->oprs[operand].type |= MEMORY_ANY;
917 if (b == -1 && (i == -1 || s == 0)) {
918 int is_rel = globalbits == 64 &&
919 !(result->oprs[operand].eaflags & EAF_ABS) &&
920 ((globalrel &&
921 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
922 (result->oprs[operand].eaflags & EAF_REL));
924 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
927 if (i != -1) {
928 opflags_t iclass = nasm_reg_flags[i];
930 if (is_class(XMMREG,iclass))
931 result->oprs[operand].type |= XMEM;
932 else if (is_class(YMMREG,iclass))
933 result->oprs[operand].type |= YMEM;
934 else if (is_class(ZMMREG,iclass))
935 result->oprs[operand].type |= ZMEM;
938 result->oprs[operand].basereg = b;
939 result->oprs[operand].indexreg = i;
940 result->oprs[operand].scale = s;
941 result->oprs[operand].offset = o;
942 result->oprs[operand].decoflags |= brace_flags;
943 } else { /* it's not a memory reference */
944 if (is_just_unknown(value)) { /* it's immediate but unknown */
945 result->oprs[operand].type |= IMMEDIATE;
946 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
947 result->oprs[operand].offset = 0; /* don't care */
948 result->oprs[operand].segment = NO_SEG; /* don't care again */
949 result->oprs[operand].wrt = NO_SEG; /* still don't care */
951 if(optimizing >= 0 && !(result->oprs[operand].type & STRICT)) {
952 /* Be optimistic */
953 result->oprs[operand].type |=
954 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
956 } else if (is_reloc(value)) { /* it's immediate */
957 result->oprs[operand].type |= IMMEDIATE;
958 result->oprs[operand].offset = reloc_value(value);
959 result->oprs[operand].segment = reloc_seg(value);
960 result->oprs[operand].wrt = reloc_wrt(value);
962 if (is_simple(value)) {
963 uint64_t n = reloc_value(value);
964 if (n == 1)
965 result->oprs[operand].type |= UNITY;
966 if (optimizing >= 0 &&
967 !(result->oprs[operand].type & STRICT)) {
968 if ((uint32_t) (n + 128) <= 255)
969 result->oprs[operand].type |= SBYTEDWORD;
970 if ((uint16_t) (n + 128) <= 255)
971 result->oprs[operand].type |= SBYTEWORD;
972 if (n <= 0xFFFFFFFF)
973 result->oprs[operand].type |= UDWORD;
974 if (n + 0x80000000 <= 0xFFFFFFFF)
975 result->oprs[operand].type |= SDWORD;
978 } else if(value->type == EXPR_RDSAE) {
980 * it's not an operand but a rounding or SAE decorator.
981 * put the decorator information in the (opflag_t) type field
982 * of previous operand.
984 operand --;
985 switch (value->value) {
986 case BRC_RN:
987 case BRC_RU:
988 case BRC_RD:
989 case BRC_RZ:
990 case BRC_SAE:
991 result->oprs[operand].decoflags |=
992 (value->value == BRC_SAE ? SAE : ER);
993 result->evex_rm = value->value;
994 break;
995 default:
996 nasm_error(ERR_NONFATAL, "invalid decorator");
997 break;
999 } else { /* it's a register */
1000 opflags_t rs;
1002 if (value->type >= EXPR_SIMPLE || value->value != 1) {
1003 nasm_error(ERR_NONFATAL, "invalid operand type");
1004 result->opcode = I_none;
1005 return result;
1009 * check that its only 1 register, not an expression...
1011 for (i = 1; value[i].type; i++)
1012 if (value[i].value) {
1013 nasm_error(ERR_NONFATAL, "invalid operand type");
1014 result->opcode = I_none;
1015 return result;
1018 /* clear overrides, except TO which applies to FPU regs */
1019 if (result->oprs[operand].type & ~TO) {
1021 * we want to produce a warning iff the specified size
1022 * is different from the register size
1024 rs = result->oprs[operand].type & SIZE_MASK;
1025 } else
1026 rs = 0;
1028 result->oprs[operand].type &= TO;
1029 result->oprs[operand].type |= REGISTER;
1030 result->oprs[operand].type |= nasm_reg_flags[value->type];
1031 result->oprs[operand].decoflags |= brace_flags;
1032 result->oprs[operand].basereg = value->type;
1034 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
1035 nasm_error(ERR_WARNING | ERR_PASS1,
1036 "register size specification ignored");
1040 /* remember the position of operand having broadcasting/ER mode */
1041 if (result->oprs[operand].decoflags & (BRDCAST_MASK | ER | SAE))
1042 result->evex_brerop = operand;
1045 result->operands = operand; /* set operand count */
1047 /* clear remaining operands */
1048 while (operand < MAX_OPERANDS)
1049 result->oprs[operand++].type = 0;
1052 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
1054 switch (result->opcode) {
1055 case I_RESW:
1056 result->opcode = I_RESB;
1057 result->oprs[0].offset *= 2;
1058 break;
1059 case I_RESD:
1060 result->opcode = I_RESB;
1061 result->oprs[0].offset *= 4;
1062 break;
1063 case I_RESQ:
1064 result->opcode = I_RESB;
1065 result->oprs[0].offset *= 8;
1066 break;
1067 case I_REST:
1068 result->opcode = I_RESB;
1069 result->oprs[0].offset *= 10;
1070 break;
1071 case I_RESO:
1072 result->opcode = I_RESB;
1073 result->oprs[0].offset *= 16;
1074 break;
1075 case I_RESY:
1076 result->opcode = I_RESB;
1077 result->oprs[0].offset *= 32;
1078 break;
1079 default:
1080 break;
1083 return result;
1086 static int is_comma_next(void)
1088 struct tokenval tv;
1089 char *p;
1090 int i;
1092 p = stdscan_get();
1093 i = stdscan(NULL, &tv);
1094 stdscan_set(p);
1096 return (i == ',' || i == ';' || !i);
1099 void cleanup_insn(insn * i)
1101 extop *e;
1103 while ((e = i->eops)) {
1104 i->eops = e->next;
1105 if (e->type == EOT_DB_STRING_FREE)
1106 nasm_free(e->stringval);
1107 nasm_free(e);