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