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