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