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