AVX-512: Fix match function to check the range of registers
[nasm/avx512.git] / parser.c
blob585abe207da311740d541098afd5e5ea726bee3a
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 */
266 /* Ignore blank lines */
267 if (i == TOKEN_EOS) {
268 result->opcode = I_none;
269 return result;
272 if (i != TOKEN_ID &&
273 i != TOKEN_INSN &&
274 i != TOKEN_PREFIX &&
275 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
276 nasm_error(ERR_NONFATAL,
277 "label or instruction expected at start of line");
278 result->opcode = I_none;
279 return result;
282 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
283 /* there's a label here */
284 first = false;
285 result->label = tokval.t_charptr;
286 i = stdscan(NULL, &tokval);
287 if (i == ':') { /* skip over the optional colon */
288 i = stdscan(NULL, &tokval);
289 } else if (i == 0) {
290 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
291 "label alone on a line without a colon might be in error");
293 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
295 * FIXME: location->segment could be NO_SEG, in which case
296 * it is possible we should be passing 'abs_seg'. Look into this.
297 * Work out whether that is *really* what we should be doing.
298 * Generally fix things. I think this is right as it is, but
299 * am still not certain.
301 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
302 location->offset, NULL, true, false);
306 /* Just a label here */
307 if (i == TOKEN_EOS) {
308 result->opcode = I_none;
309 return result;
312 nasm_build_assert(P_none != 0);
313 memset(result->prefixes, P_none, sizeof(result->prefixes));
314 result->times = 1L;
316 while (i == TOKEN_PREFIX ||
317 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
318 first = false;
321 * Handle special case: the TIMES prefix.
323 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
324 expr *value;
326 i = stdscan(NULL, &tokval);
327 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
328 i = tokval.t_type;
329 if (!value) { /* but, error in evaluator */
330 result->opcode = I_none; /* unrecoverable parse error: */
331 return result; /* ignore this instruction */
333 if (!is_simple(value)) {
334 nasm_error(ERR_NONFATAL,
335 "non-constant argument supplied to TIMES");
336 result->times = 1L;
337 } else {
338 result->times = value->value;
339 if (value->value < 0 && pass0 == 2) {
340 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
341 value->value);
342 result->times = 0;
345 } else {
346 int slot = prefix_slot(tokval.t_integer);
347 if (result->prefixes[slot]) {
348 if (result->prefixes[slot] == tokval.t_integer)
349 nasm_error(ERR_WARNING | ERR_PASS1,
350 "instruction has redundant prefixes");
351 else
352 nasm_error(ERR_NONFATAL,
353 "instruction has conflicting prefixes");
355 result->prefixes[slot] = tokval.t_integer;
356 i = stdscan(NULL, &tokval);
360 if (i != TOKEN_INSN) {
361 int j;
362 enum prefixes pfx;
364 for (j = 0; j < MAXPREFIX; j++) {
365 if ((pfx = result->prefixes[j]) != P_none)
366 break;
369 if (i == 0 && pfx != P_none) {
371 * Instruction prefixes are present, but no actual
372 * instruction. This is allowed: at this point we
373 * invent a notional instruction of RESB 0.
375 result->opcode = I_RESB;
376 result->operands = 1;
377 result->oprs[0].type = IMMEDIATE;
378 result->oprs[0].offset = 0L;
379 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
380 return result;
381 } else {
382 nasm_error(ERR_NONFATAL, "parser: instruction expected");
383 result->opcode = I_none;
384 return result;
388 result->opcode = tokval.t_integer;
389 result->condition = tokval.t_inttwo;
392 * INCBIN cannot be satisfied with incorrectly
393 * evaluated operands, since the correct values _must_ be known
394 * on the first pass. Hence, even in pass one, we set the
395 * `critical' flag on calling evaluate(), so that it will bomb
396 * out on undefined symbols.
398 if (result->opcode == I_INCBIN) {
399 critical = (pass0 < 2 ? 1 : 2);
401 } else
402 critical = (pass == 2 ? 2 : 0);
404 if (result->opcode == I_DB || result->opcode == I_DW ||
405 result->opcode == I_DD || result->opcode == I_DQ ||
406 result->opcode == I_DT || result->opcode == I_DO ||
407 result->opcode == I_DY || result->opcode == I_INCBIN) {
408 extop *eop, **tail = &result->eops, **fixptr;
409 int oper_num = 0;
410 int32_t sign;
412 result->eops_float = false;
415 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
417 while (1) {
418 i = stdscan(NULL, &tokval);
419 if (i == TOKEN_EOS)
420 break;
421 else if (first && i == ':') {
422 insn_is_label = true;
423 goto restart_parse;
425 first = false;
426 fixptr = tail;
427 eop = *tail = nasm_malloc(sizeof(extop));
428 tail = &eop->next;
429 eop->next = NULL;
430 eop->type = EOT_NOTHING;
431 oper_num++;
432 sign = +1;
435 * is_comma_next() here is to distinguish this from
436 * a string used as part of an expression...
438 if (i == TOKEN_STR && is_comma_next()) {
439 eop->type = EOT_DB_STRING;
440 eop->stringval = tokval.t_charptr;
441 eop->stringlen = tokval.t_inttwo;
442 i = stdscan(NULL, &tokval); /* eat the comma */
443 } else if (i == TOKEN_STRFUNC) {
444 bool parens = false;
445 const char *funcname = tokval.t_charptr;
446 enum strfunc func = tokval.t_integer;
447 i = stdscan(NULL, &tokval);
448 if (i == '(') {
449 parens = true;
450 i = stdscan(NULL, &tokval);
452 if (i != TOKEN_STR) {
453 nasm_error(ERR_NONFATAL,
454 "%s must be followed by a string constant",
455 funcname);
456 eop->type = EOT_NOTHING;
457 } else {
458 eop->type = EOT_DB_STRING_FREE;
459 eop->stringlen =
460 string_transform(tokval.t_charptr, tokval.t_inttwo,
461 &eop->stringval, func);
462 if (eop->stringlen == (size_t)-1) {
463 nasm_error(ERR_NONFATAL, "invalid string for transform");
464 eop->type = EOT_NOTHING;
467 if (parens && i && i != ')') {
468 i = stdscan(NULL, &tokval);
469 if (i != ')') {
470 nasm_error(ERR_NONFATAL, "unterminated %s function",
471 funcname);
474 if (i && i != ',')
475 i = stdscan(NULL, &tokval);
476 } else if (i == '-' || i == '+') {
477 char *save = stdscan_get();
478 int token = i;
479 sign = (i == '-') ? -1 : 1;
480 i = stdscan(NULL, &tokval);
481 if (i != TOKEN_FLOAT) {
482 stdscan_set(save);
483 i = tokval.t_type = token;
484 goto is_expression;
485 } else {
486 goto is_float;
488 } else if (i == TOKEN_FLOAT) {
489 is_float:
490 eop->type = EOT_DB_STRING;
491 result->eops_float = true;
493 eop->stringlen = idata_bytes(result->opcode);
494 if (eop->stringlen > 16) {
495 nasm_error(ERR_NONFATAL, "floating-point constant"
496 " encountered in DY instruction");
497 eop->stringlen = 0;
498 } else if (eop->stringlen < 1) {
499 nasm_error(ERR_NONFATAL, "floating-point constant"
500 " encountered in unknown instruction");
502 * fix suggested by Pedro Gimeno... original line was:
503 * eop->type = EOT_NOTHING;
505 eop->stringlen = 0;
508 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
509 tail = &eop->next;
510 *fixptr = eop;
511 eop->stringval = (char *)eop + sizeof(extop);
512 if (!eop->stringlen ||
513 !float_const(tokval.t_charptr, sign,
514 (uint8_t *)eop->stringval,
515 eop->stringlen, nasm_error))
516 eop->type = EOT_NOTHING;
517 i = stdscan(NULL, &tokval); /* eat the comma */
518 } else {
519 /* anything else, assume it is an expression */
520 expr *value;
522 is_expression:
523 value = evaluate(stdscan, NULL, &tokval, NULL,
524 critical, nasm_error, NULL);
525 i = tokval.t_type;
526 if (!value) { /* error in evaluator */
527 result->opcode = I_none; /* unrecoverable parse error: */
528 return result; /* ignore this instruction */
530 if (is_unknown(value)) {
531 eop->type = EOT_DB_NUMBER;
532 eop->offset = 0; /* doesn't matter what we put */
533 eop->segment = eop->wrt = NO_SEG; /* likewise */
534 } else if (is_reloc(value)) {
535 eop->type = EOT_DB_NUMBER;
536 eop->offset = reloc_value(value);
537 eop->segment = reloc_seg(value);
538 eop->wrt = reloc_wrt(value);
539 } else {
540 nasm_error(ERR_NONFATAL,
541 "operand %d: expression is not simple"
542 " or relocatable", oper_num);
547 * We're about to call stdscan(), which will eat the
548 * comma that we're currently sitting on between
549 * arguments. However, we'd better check first that it
550 * _is_ a comma.
552 if (i == TOKEN_EOS) /* also could be EOL */
553 break;
554 if (i != ',') {
555 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
556 oper_num);
557 result->opcode = I_none;/* unrecoverable parse error: */
558 return result; /* ignore this instruction */
562 if (result->opcode == I_INCBIN) {
564 * Correct syntax for INCBIN is that there should be
565 * one string operand, followed by one or two numeric
566 * operands.
568 if (!result->eops || result->eops->type != EOT_DB_STRING)
569 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
570 else if (result->eops->next &&
571 result->eops->next->type != EOT_DB_NUMBER)
572 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
573 " non-numeric");
574 else if (result->eops->next && result->eops->next->next &&
575 result->eops->next->next->type != EOT_DB_NUMBER)
576 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
577 " non-numeric");
578 else if (result->eops->next && result->eops->next->next &&
579 result->eops->next->next->next)
580 nasm_error(ERR_NONFATAL,
581 "`incbin': more than three parameters");
582 else
583 return result;
585 * If we reach here, one of the above errors happened.
586 * Throw the instruction away.
588 result->opcode = I_none;
589 return result;
590 } else /* DB ... */ if (oper_num == 0)
591 nasm_error(ERR_WARNING | ERR_PASS1,
592 "no operand for data declaration");
593 else
594 result->operands = oper_num;
596 return result;
600 * Now we begin to parse the operands. There may be up to four
601 * of these, separated by commas, and terminated by a zero token.
604 for (operand = 0; operand < MAX_OPERANDS; operand++) {
605 expr *value; /* used most of the time */
606 int mref; /* is this going to be a memory ref? */
607 int bracket; /* is it a [] mref, or a & mref? */
608 int setsize = 0;
609 decoflags_t brace_flags = 0; /* flags for decorators in braces */
611 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
612 result->oprs[operand].eaflags = 0; /* and this */
613 result->oprs[operand].opflags = 0;
614 result->oprs[operand].decoflags = 0;
616 i = stdscan(NULL, &tokval);
617 if (i == TOKEN_EOS)
618 break; /* end of operands: get out of here */
619 else if (first && i == ':') {
620 insn_is_label = true;
621 goto restart_parse;
623 first = false;
624 result->oprs[operand].type = 0; /* so far, no override */
625 while (i == TOKEN_SPECIAL) { /* size specifiers */
626 switch ((int)tokval.t_integer) {
627 case S_BYTE:
628 if (!setsize) /* we want to use only the first */
629 result->oprs[operand].type |= BITS8;
630 setsize = 1;
631 break;
632 case S_WORD:
633 if (!setsize)
634 result->oprs[operand].type |= BITS16;
635 setsize = 1;
636 break;
637 case S_DWORD:
638 case S_LONG:
639 if (!setsize)
640 result->oprs[operand].type |= BITS32;
641 setsize = 1;
642 break;
643 case S_QWORD:
644 if (!setsize)
645 result->oprs[operand].type |= BITS64;
646 setsize = 1;
647 break;
648 case S_TWORD:
649 if (!setsize)
650 result->oprs[operand].type |= BITS80;
651 setsize = 1;
652 break;
653 case S_OWORD:
654 if (!setsize)
655 result->oprs[operand].type |= BITS128;
656 setsize = 1;
657 break;
658 case S_YWORD:
659 if (!setsize)
660 result->oprs[operand].type |= BITS256;
661 setsize = 1;
662 break;
663 case S_ZWORD:
664 if (!setsize)
665 result->oprs[operand].type |= BITS512;
666 setsize = 1;
667 break;
668 case S_TO:
669 result->oprs[operand].type |= TO;
670 break;
671 case S_STRICT:
672 result->oprs[operand].type |= STRICT;
673 break;
674 case S_FAR:
675 result->oprs[operand].type |= FAR;
676 break;
677 case S_NEAR:
678 result->oprs[operand].type |= NEAR;
679 break;
680 case S_SHORT:
681 result->oprs[operand].type |= SHORT;
682 break;
683 default:
684 nasm_error(ERR_NONFATAL, "invalid operand size specification");
686 i = stdscan(NULL, &tokval);
689 if (i == '[' || i == '&') { /* memory reference */
690 mref = true;
691 bracket = (i == '[');
692 i = stdscan(NULL, &tokval); /* then skip the colon */
693 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
694 process_size_override(result, operand);
695 i = stdscan(NULL, &tokval);
697 } else { /* immediate operand, or register */
698 mref = false;
699 bracket = false; /* placate optimisers */
702 if ((result->oprs[operand].type & FAR) && !mref &&
703 result->opcode != I_JMP && result->opcode != I_CALL) {
704 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
707 value = evaluate(stdscan, NULL, &tokval,
708 &result->oprs[operand].opflags,
709 critical, nasm_error, &hints);
710 i = tokval.t_type;
711 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
712 result->forw_ref = true;
714 if (!value) { /* nasm_error in evaluator */
715 result->opcode = I_none; /* unrecoverable parse error: */
716 return result; /* ignore this instruction */
718 if (i == ':' && mref) { /* it was seg:offset */
720 * Process the segment override.
722 if (value[1].type != 0 ||
723 value->value != 1 ||
724 !IS_SREG(value->type))
725 nasm_error(ERR_NONFATAL, "invalid segment override");
726 else if (result->prefixes[PPS_SEG])
727 nasm_error(ERR_NONFATAL,
728 "instruction has conflicting segment overrides");
729 else {
730 result->prefixes[PPS_SEG] = value->type;
731 if (IS_FSGS(value->type))
732 result->oprs[operand].eaflags |= EAF_FSGS;
735 i = stdscan(NULL, &tokval); /* then skip the colon */
736 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
737 process_size_override(result, operand);
738 i = stdscan(NULL, &tokval);
740 value = evaluate(stdscan, NULL, &tokval,
741 &result->oprs[operand].opflags,
742 critical, nasm_error, &hints);
743 i = tokval.t_type;
744 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
745 result->forw_ref = true;
747 /* and get the offset */
748 if (!value) { /* but, error in evaluator */
749 result->opcode = I_none; /* unrecoverable parse error: */
750 return result; /* ignore this instruction */
754 recover = false;
755 if (mref && bracket) { /* find ] at the end */
756 if (i != ']') {
757 nasm_error(ERR_NONFATAL, "parser: expecting ]");
758 recover = true;
759 } else { /* we got the required ] */
760 i = stdscan(NULL, &tokval);
761 if ((i == TOKEN_DECORATOR) || (i == TOKEN_OPMASK)) {
763 * according to AVX512 spec, broacast or opmask decorator
764 * is expected for memory reference operands
766 if (tokval.t_flag & TFLAG_BRDCAST) {
767 brace_flags |= GEN_BRDCAST(0);
768 i = stdscan(NULL, &tokval);
769 } else if (i == TOKEN_OPMASK) {
770 brace_flags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
771 i = stdscan(NULL, &tokval);
772 } else {
773 nasm_error(ERR_NONFATAL, "broadcast or opmask "
774 "decorator expected inside braces");
775 recover = true;
779 if (i != 0 && i != ',') {
780 nasm_error(ERR_NONFATAL, "comma or end of line expected");
781 recover = true;
784 } else { /* immediate operand */
785 if (i != 0 && i != ',' && i != ':' &&
786 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
787 nasm_error(ERR_NONFATAL, "comma, colon, decorator or end of "
788 "line expected after operand");
789 recover = true;
790 } else if (i == ':') {
791 result->oprs[operand].type |= COLON;
792 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
793 /* parse opmask (and zeroing) after an operand */
794 recover = parse_braces(&brace_flags);
797 if (recover) {
798 do { /* error recovery */
799 i = stdscan(NULL, &tokval);
800 } while (i != 0 && i != ',');
804 * now convert the exprs returned from evaluate()
805 * into operand descriptions...
808 if (mref) { /* it's a memory reference */
809 expr *e = value;
810 int b, i, s; /* basereg, indexreg, scale */
811 int64_t o; /* offset */
813 b = i = -1, o = s = 0;
814 result->oprs[operand].hintbase = hints.base;
815 result->oprs[operand].hinttype = hints.type;
817 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
818 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
820 if (is_gpr && e->value == 1)
821 b = e->type; /* It can be basereg */
822 else /* No, it has to be indexreg */
823 i = e->type, s = e->value;
824 e++;
826 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
827 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
829 if (b != -1) /* If the first was the base, ... */
830 i = e->type, s = e->value; /* second has to be indexreg */
832 else if (!is_gpr || e->value != 1) {
833 /* If both want to be index */
834 nasm_error(ERR_NONFATAL,
835 "invalid effective address: two index registers");
836 result->opcode = I_none;
837 return result;
838 } else
839 b = e->type;
840 e++;
842 if (e->type != 0) { /* is there an offset? */
843 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
844 nasm_error(ERR_NONFATAL,
845 "beroset-p-603-invalid effective address");
846 result->opcode = I_none;
847 return result;
848 } else {
849 if (e->type == EXPR_UNKNOWN) {
850 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
851 o = 0; /* doesn't matter what */
852 result->oprs[operand].wrt = NO_SEG; /* nor this */
853 result->oprs[operand].segment = NO_SEG; /* or this */
854 while (e->type)
855 e++; /* go to the end of the line */
856 } else {
857 if (e->type == EXPR_SIMPLE) {
858 o = e->value;
859 e++;
861 if (e->type == EXPR_WRT) {
862 result->oprs[operand].wrt = e->value;
863 e++;
864 } else
865 result->oprs[operand].wrt = NO_SEG;
867 * Look for a segment base type.
869 if (e->type && e->type < EXPR_SEGBASE) {
870 nasm_error(ERR_NONFATAL,
871 "beroset-p-630-invalid effective address");
872 result->opcode = I_none;
873 return result;
875 while (e->type && e->value == 0)
876 e++;
877 if (e->type && e->value != 1) {
878 nasm_error(ERR_NONFATAL,
879 "beroset-p-637-invalid effective address");
880 result->opcode = I_none;
881 return result;
883 if (e->type) {
884 result->oprs[operand].segment =
885 e->type - EXPR_SEGBASE;
886 e++;
887 } else
888 result->oprs[operand].segment = NO_SEG;
889 while (e->type && e->value == 0)
890 e++;
891 if (e->type) {
892 nasm_error(ERR_NONFATAL,
893 "beroset-p-650-invalid effective address");
894 result->opcode = I_none;
895 return result;
899 } else {
900 o = 0;
901 result->oprs[operand].wrt = NO_SEG;
902 result->oprs[operand].segment = NO_SEG;
905 if (e->type != 0) { /* there'd better be nothing left! */
906 nasm_error(ERR_NONFATAL,
907 "beroset-p-663-invalid effective address");
908 result->opcode = I_none;
909 return result;
912 /* It is memory, but it can match any r/m operand */
913 result->oprs[operand].type |= MEMORY_ANY;
915 if (b == -1 && (i == -1 || s == 0)) {
916 int is_rel = globalbits == 64 &&
917 !(result->oprs[operand].eaflags & EAF_ABS) &&
918 ((globalrel &&
919 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
920 (result->oprs[operand].eaflags & EAF_REL));
922 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
925 if (i != -1) {
926 opflags_t iclass = nasm_reg_flags[i];
928 if (is_class(XMMREG,iclass))
929 result->oprs[operand].type |= XMEM;
930 else if (is_class(YMMREG,iclass))
931 result->oprs[operand].type |= YMEM;
932 else if (is_class(ZMMREG,iclass))
933 result->oprs[operand].type |= ZMEM;
936 result->oprs[operand].basereg = b;
937 result->oprs[operand].indexreg = i;
938 result->oprs[operand].scale = s;
939 result->oprs[operand].offset = o;
940 result->oprs[operand].decoflags |= brace_flags;
941 } else { /* it's not a memory reference */
942 if (is_just_unknown(value)) { /* it's immediate but unknown */
943 result->oprs[operand].type |= IMMEDIATE;
944 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
945 result->oprs[operand].offset = 0; /* don't care */
946 result->oprs[operand].segment = NO_SEG; /* don't care again */
947 result->oprs[operand].wrt = NO_SEG; /* still don't care */
949 if(optimizing >= 0 && !(result->oprs[operand].type & STRICT)) {
950 /* Be optimistic */
951 result->oprs[operand].type |=
952 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
954 } else if (is_reloc(value)) { /* it's immediate */
955 result->oprs[operand].type |= IMMEDIATE;
956 result->oprs[operand].offset = reloc_value(value);
957 result->oprs[operand].segment = reloc_seg(value);
958 result->oprs[operand].wrt = reloc_wrt(value);
960 if (is_simple(value)) {
961 uint64_t n = reloc_value(value);
962 if (n == 1)
963 result->oprs[operand].type |= UNITY;
964 if (optimizing >= 0 &&
965 !(result->oprs[operand].type & STRICT)) {
966 if ((uint32_t) (n + 128) <= 255)
967 result->oprs[operand].type |= SBYTEDWORD;
968 if ((uint16_t) (n + 128) <= 255)
969 result->oprs[operand].type |= SBYTEWORD;
970 if (n <= 0xFFFFFFFF)
971 result->oprs[operand].type |= UDWORD;
972 if (n + 0x80000000 <= 0xFFFFFFFF)
973 result->oprs[operand].type |= SDWORD;
976 } else if(value->type == EXPR_RDSAE) {
978 * it's not an operand but a rounding or SAE decorator.
979 * put the decorator information in the (opflag_t) type field
980 * of previous operand.
982 operand --;
983 switch (value->value) {
984 case BRC_RN:
985 case BRC_RU:
986 case BRC_RD:
987 case BRC_RZ:
988 case BRC_SAE:
989 result->oprs[operand].decoflags |=
990 (value->value == BRC_SAE ? SAE : ER);
991 result->evex_rm = value->value;
992 break;
993 default:
994 nasm_error(ERR_NONFATAL, "invalid decorator");
995 break;
997 } else { /* it's a register */
998 opflags_t rs;
1000 if (value->type >= EXPR_SIMPLE || value->value != 1) {
1001 nasm_error(ERR_NONFATAL, "invalid operand type");
1002 result->opcode = I_none;
1003 return result;
1007 * check that its only 1 register, not an expression...
1009 for (i = 1; value[i].type; i++)
1010 if (value[i].value) {
1011 nasm_error(ERR_NONFATAL, "invalid operand type");
1012 result->opcode = I_none;
1013 return result;
1016 /* clear overrides, except TO which applies to FPU regs */
1017 if (result->oprs[operand].type & ~TO) {
1019 * we want to produce a warning iff the specified size
1020 * is different from the register size
1022 rs = result->oprs[operand].type & SIZE_MASK;
1023 } else
1024 rs = 0;
1026 result->oprs[operand].type &= TO;
1027 result->oprs[operand].type |= REGISTER;
1028 result->oprs[operand].type |= nasm_reg_flags[value->type];
1029 result->oprs[operand].decoflags |= brace_flags;
1030 result->oprs[operand].basereg = value->type;
1032 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
1033 nasm_error(ERR_WARNING | ERR_PASS1,
1034 "register size specification ignored");
1039 result->operands = operand; /* set operand count */
1041 /* clear remaining operands */
1042 while (operand < MAX_OPERANDS)
1043 result->oprs[operand++].type = 0;
1046 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
1048 switch (result->opcode) {
1049 case I_RESW:
1050 result->opcode = I_RESB;
1051 result->oprs[0].offset *= 2;
1052 break;
1053 case I_RESD:
1054 result->opcode = I_RESB;
1055 result->oprs[0].offset *= 4;
1056 break;
1057 case I_RESQ:
1058 result->opcode = I_RESB;
1059 result->oprs[0].offset *= 8;
1060 break;
1061 case I_REST:
1062 result->opcode = I_RESB;
1063 result->oprs[0].offset *= 10;
1064 break;
1065 case I_RESO:
1066 result->opcode = I_RESB;
1067 result->oprs[0].offset *= 16;
1068 break;
1069 case I_RESY:
1070 result->opcode = I_RESB;
1071 result->oprs[0].offset *= 32;
1072 break;
1073 default:
1074 break;
1077 return result;
1080 static int is_comma_next(void)
1082 struct tokenval tv;
1083 char *p;
1084 int i;
1086 p = stdscan_get();
1087 i = stdscan(NULL, &tv);
1088 stdscan_set(p);
1090 return (i == ',' || i == ';' || !i);
1093 void cleanup_insn(insn * i)
1095 extop *e;
1097 while ((e = i->eops)) {
1098 i->eops = e->next;
1099 if (e->type == EOT_DB_STRING_FREE)
1100 nasm_free(e->stringval);
1101 nasm_free(e);