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