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