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