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