Clean up elf symbol table section
[platform/upstream/nasm.git] / parser.c
1 /* parser.c   source line parser for the Netwide Assembler
2  *
3  * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4  * Julian Hall. All rights reserved. The software is
5  * redistributable under the licence given in the file "Licence"
6  * distributed in the NASM archive.
7  *
8  * initial version 27/iii/95 by Simon Tatham
9  */
10
11 #include "compiler.h"
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stddef.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <inttypes.h>
19
20 #include "nasm.h"
21 #include "insns.h"
22 #include "nasmlib.h"
23 #include "stdscan.h"
24 #include "parser.h"
25 #include "float.h"
26
27 extern int in_abs_seg;          /* ABSOLUTE segment flag */
28 extern int32_t abs_seg;            /* ABSOLUTE segment */
29 extern int32_t abs_offset;         /* ABSOLUTE segment offset */
30
31 #include "regflags.c"           /* List of register flags */
32
33 static int is_comma_next(void);
34
35 static int i;
36 static struct tokenval tokval;
37 static efunc error;
38 static struct ofmt *outfmt;     /* Structure of addresses of output routines */
39 static struct location *location;         /* Pointer to current line's segment,offset */
40
41 void parser_global_info(struct ofmt *output, struct location * locp)
42 {
43     outfmt = output;
44     location = locp;
45 }
46
47 insn *parse_line(int pass, char *buffer, insn * result,
48                  efunc errfunc, evalfunc evaluate, ldfunc ldef)
49 {
50     int operand;
51     int critical;
52     struct eval_hints hints;
53
54     result->forw_ref = false;
55     error = errfunc;
56
57     stdscan_reset();
58     stdscan_bufptr = buffer;
59     i = stdscan(NULL, &tokval);
60
61     result->label = NULL;       /* Assume no label */
62     result->eops = NULL;        /* must do this, whatever happens */
63     result->operands = 0;       /* must initialize this */
64
65     if (i == 0) {               /* blank line - ignore */
66         result->opcode = -1;    /* and no instruction either */
67         return result;
68     }
69     if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
70         (i != TOKEN_REG || (REG_SREG & ~reg_flags[tokval.t_integer]))) {
71         error(ERR_NONFATAL, "label or instruction expected"
72               " at start of line");
73         result->opcode = -1;
74         return result;
75     }
76
77     if (i == TOKEN_ID) {        /* there's a label here */
78         result->label = tokval.t_charptr;
79         i = stdscan(NULL, &tokval);
80         if (i == ':') {         /* skip over the optional colon */
81             i = stdscan(NULL, &tokval);
82         } else if (i == 0) {
83             error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
84                   "label alone on a line without a colon might be in error");
85         }
86         if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
87             /*
88              * FIXME: location->segment could be NO_SEG, in which case
89              * it is possible we should be passing 'abs_seg'. Look into this.
90              * Work out whether that is *really* what we should be doing.
91              * Generally fix things. I think this is right as it is, but
92              * am still not certain.
93              */
94             ldef(result->label, in_abs_seg ? abs_seg : location->segment,
95                  location->offset, NULL, true, false, outfmt, errfunc);
96         }
97     }
98
99     if (i == 0) {
100         result->opcode = -1;    /* this line contains just a label */
101         return result;
102     }
103
104     result->nprefix = 0;
105     result->times = 1L;
106
107     while (i == TOKEN_PREFIX ||
108            (i == TOKEN_REG && !(REG_SREG & ~reg_flags[tokval.t_integer])))
109     {
110         /*
111          * Handle special case: the TIMES prefix.
112          */
113         if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
114             expr *value;
115
116             i = stdscan(NULL, &tokval);
117             value =
118                 evaluate(stdscan, NULL, &tokval, NULL, pass0, error, NULL);
119             i = tokval.t_type;
120             if (!value) {       /* but, error in evaluator */
121                 result->opcode = -1;    /* unrecoverable parse error: */
122                 return result;  /* ignore this instruction */
123             }
124             if (!is_simple(value)) {
125                 error(ERR_NONFATAL,
126                       "non-constant argument supplied to TIMES");
127                 result->times = 1L;
128             } else {
129                 result->times = value->value;
130                 if (value->value < 0) {
131                     error(ERR_NONFATAL, "TIMES value %d is negative",
132                           value->value);
133                     result->times = 0;
134                 }
135             }
136         } else {
137             if (result->nprefix == MAXPREFIX)
138                 error(ERR_NONFATAL,
139                       "instruction has more than %d prefixes", MAXPREFIX);
140             else
141                 result->prefixes[result->nprefix++] = tokval.t_integer;
142             i = stdscan(NULL, &tokval);
143         }
144     }
145
146     if (i != TOKEN_INSN) {
147         if (result->nprefix > 0 && i == 0) {
148             /*
149              * Instruction prefixes are present, but no actual
150              * instruction. This is allowed: at this point we
151              * invent a notional instruction of RESB 0.
152              */
153             result->opcode = I_RESB;
154             result->operands = 1;
155             result->oprs[0].type = IMMEDIATE;
156             result->oprs[0].offset = 0L;
157             result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
158             return result;
159         } else {
160             error(ERR_NONFATAL, "parser: instruction expected");
161             result->opcode = -1;
162             return result;
163         }
164     }
165
166     result->opcode = tokval.t_integer;
167     result->condition = tokval.t_inttwo;
168
169     /*
170      * RESB, RESW and RESD cannot be satisfied with incorrectly
171      * evaluated operands, since the correct values _must_ be known
172      * on the first pass. Hence, even in pass one, we set the
173      * `critical' flag on calling evaluate(), so that it will bomb
174      * out on undefined symbols. Nasty, but there's nothing we can
175      * do about it.
176      *
177      * For the moment, EQU has the same difficulty, so we'll
178      * include that.
179      */
180     if (result->opcode == I_RESB || result->opcode == I_RESW ||
181         result->opcode == I_RESD || result->opcode == I_RESQ ||
182         result->opcode == I_REST || result->opcode == I_RESO ||
183         result->opcode == I_EQU || result->opcode == I_INCBIN) {
184         critical = pass0;
185     } else
186         critical = (pass == 2 ? 2 : 0);
187
188     if (result->opcode == I_DB || result->opcode == I_DW ||
189         result->opcode == I_DD || result->opcode == I_DQ ||
190         result->opcode == I_DT || result->opcode == I_DO ||
191         result->opcode == I_INCBIN) {
192         extop *eop, **tail = &result->eops, **fixptr;
193         int oper_num = 0;
194
195         result->eops_float = false;
196
197         /*
198          * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
199          */
200         while (1) {
201             i = stdscan(NULL, &tokval);
202             if (i == 0)
203                 break;
204             fixptr = tail;
205             eop = *tail = nasm_malloc(sizeof(extop));
206             tail = &eop->next;
207             eop->next = NULL;
208             eop->type = EOT_NOTHING;
209             oper_num++;
210
211             if (i == TOKEN_NUM && tokval.t_charptr && is_comma_next()) {
212                 eop->type = EOT_DB_STRING;
213                 eop->stringval = tokval.t_charptr;
214                 eop->stringlen = tokval.t_inttwo;
215                 i = stdscan(NULL, &tokval);     /* eat the comma */
216                 continue;
217             }
218
219             if ((i == TOKEN_FLOAT && is_comma_next())
220                 || i == '-' || i == '+') {
221                 int32_t sign = +1;
222
223                 if (i == '+' || i == '-') {
224                     char *save = stdscan_bufptr;
225                     int token = i;
226                     sign = (i == '-') ? -1 : 1;
227                     i = stdscan(NULL, &tokval);
228                     if (i != TOKEN_FLOAT || !is_comma_next()) {
229                         stdscan_bufptr = save;
230                         i = tokval.t_type = token;
231                     }
232                 }
233
234                 if (i == TOKEN_FLOAT) {
235                     eop->type = EOT_DB_STRING;
236                     result->eops_float = true;
237                     switch (result->opcode) {
238                     case I_DW:
239                         eop->stringlen = 2;
240                         break;
241                     case I_DD:
242                         eop->stringlen = 4;
243                         break;
244                     case I_DQ:
245                         eop->stringlen = 8;
246                         break;
247                     case I_DT:
248                         eop->stringlen = 10;
249                         break;
250                     case I_DO:
251                         eop->stringlen = 16;
252                         break;
253                     default:
254                         error(ERR_NONFATAL, "floating-point constant"
255                               " encountered in `db' instruction");
256                         /*
257                          * fix suggested by Pedro Gimeno... original line
258                          * was:
259                          * eop->type = EOT_NOTHING;
260                          */
261                         eop->stringlen = 0;
262                         break;
263                     }
264                     eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
265                     tail = &eop->next;
266                     *fixptr = eop;
267                     eop->stringval = (char *)eop + sizeof(extop);
268                     if (!eop->stringlen ||
269                         !float_const(tokval.t_charptr, sign,
270                                      (uint8_t *)eop->stringval,
271                                      eop->stringlen, error))
272                         eop->type = EOT_NOTHING;
273                     i = stdscan(NULL, &tokval); /* eat the comma */
274                     continue;
275                 }
276             }
277
278             /* anything else */
279             {
280                 expr *value;
281                 value = evaluate(stdscan, NULL, &tokval, NULL,
282                                  critical, error, NULL);
283                 i = tokval.t_type;
284                 if (!value) {   /* error in evaluator */
285                     result->opcode = -1;        /* unrecoverable parse error: */
286                     return result;      /* ignore this instruction */
287                 }
288                 if (is_unknown(value)) {
289                     eop->type = EOT_DB_NUMBER;
290                     eop->offset = 0;    /* doesn't matter what we put */
291                     eop->segment = eop->wrt = NO_SEG;   /* likewise */
292                 } else if (is_reloc(value)) {
293                     eop->type = EOT_DB_NUMBER;
294                     eop->offset = reloc_value(value);
295                     eop->segment = reloc_seg(value);
296                     eop->wrt = reloc_wrt(value);
297                 } else {
298                     error(ERR_NONFATAL,
299                           "operand %d: expression is not simple"
300                           " or relocatable", oper_num);
301                 }
302             }
303
304             /*
305              * We're about to call stdscan(), which will eat the
306              * comma that we're currently sitting on between
307              * arguments. However, we'd better check first that it
308              * _is_ a comma.
309              */
310             if (i == 0)         /* also could be EOL */
311                 break;
312             if (i != ',') {
313                 error(ERR_NONFATAL, "comma expected after operand %d",
314                       oper_num);
315                 result->opcode = -1;    /* unrecoverable parse error: */
316                 return result;  /* ignore this instruction */
317             }
318         }
319
320         if (result->opcode == I_INCBIN) {
321             /*
322              * Correct syntax for INCBIN is that there should be
323              * one string operand, followed by one or two numeric
324              * operands.
325              */
326             if (!result->eops || result->eops->type != EOT_DB_STRING)
327                 error(ERR_NONFATAL, "`incbin' expects a file name");
328             else if (result->eops->next &&
329                      result->eops->next->type != EOT_DB_NUMBER)
330                 error(ERR_NONFATAL, "`incbin': second parameter is",
331                       " non-numeric");
332             else if (result->eops->next && result->eops->next->next &&
333                      result->eops->next->next->type != EOT_DB_NUMBER)
334                 error(ERR_NONFATAL, "`incbin': third parameter is",
335                       " non-numeric");
336             else if (result->eops->next && result->eops->next->next &&
337                      result->eops->next->next->next)
338                 error(ERR_NONFATAL,
339                       "`incbin': more than three parameters");
340             else
341                 return result;
342             /*
343              * If we reach here, one of the above errors happened.
344              * Throw the instruction away.
345              */
346             result->opcode = -1;
347             return result;
348         } else /* DB ... */ if (oper_num == 0)
349             error(ERR_WARNING | ERR_PASS1,
350                   "no operand for data declaration");
351         else
352             result->operands = oper_num;
353
354         return result;
355     }
356
357     /* right. Now we begin to parse the operands. There may be up to four
358      * of these, separated by commas, and terminated by a zero token. */
359
360     for (operand = 0; operand < MAX_OPERANDS; operand++) {
361         expr *value;            /* used most of the time */
362         int mref;               /* is this going to be a memory ref? */
363         int bracket;            /* is it a [] mref, or a & mref? */
364         int setsize = 0;
365
366         result->oprs[operand].addr_size = 0;    /* have to zero this whatever */
367         result->oprs[operand].eaflags = 0;      /* and this */
368         result->oprs[operand].opflags = 0;
369
370         i = stdscan(NULL, &tokval);
371         if (i == 0)
372             break;              /* end of operands: get out of here */
373         result->oprs[operand].type = 0; /* so far, no override */
374         while (i == TOKEN_SPECIAL) {    /* size specifiers */
375             switch ((int)tokval.t_integer) {
376             case S_BYTE:
377                 if (!setsize)   /* we want to use only the first */
378                     result->oprs[operand].type |= BITS8;
379                 setsize = 1;
380                 break;
381             case S_WORD:
382                 if (!setsize)
383                     result->oprs[operand].type |= BITS16;
384                 setsize = 1;
385                 break;
386             case S_DWORD:
387             case S_LONG:
388                 if (!setsize)
389                     result->oprs[operand].type |= BITS32;
390                 setsize = 1;
391                 break;
392             case S_QWORD:
393                 if (!setsize)
394                     result->oprs[operand].type |= BITS64;
395                 setsize = 1;
396                 break;
397             case S_TWORD:
398                 if (!setsize)
399                     result->oprs[operand].type |= BITS80;
400                 setsize = 1;
401                 break;
402             case S_OWORD:
403                 if (!setsize)
404                     result->oprs[operand].type |= BITS128;
405                 setsize = 1;
406                 break;
407             case S_TO:
408                 result->oprs[operand].type |= TO;
409                 break;
410             case S_STRICT:
411                 result->oprs[operand].type |= STRICT;
412                 break;
413             case S_FAR:
414                 result->oprs[operand].type |= FAR;
415                 break;
416             case S_NEAR:
417                 result->oprs[operand].type |= NEAR;
418                 break;
419             case S_SHORT:
420                 result->oprs[operand].type |= SHORT;
421                 break;
422             default:
423                 error(ERR_NONFATAL, "invalid operand size specification");
424             }
425             i = stdscan(NULL, &tokval);
426         }
427
428         if (i == '[' || i == '&') {     /* memory reference */
429             mref = true;
430             bracket = (i == '[');
431             while ((i = stdscan(NULL, &tokval)) == TOKEN_SPECIAL) {
432                 /* check for address directives */
433                 if (tasm_compatible_mode) {
434                     switch ((int)tokval.t_integer) {
435                         /* For TASM compatibility a size override inside the
436                          * brackets changes the size of the operand, not the
437                          * address type of the operand as it does in standard
438                          * NASM syntax. Hence:
439                          *
440                          *  mov     eax,[DWORD val]
441                          *
442                          * is valid syntax in TASM compatibility mode. Note that
443                          * you lose the ability to override the default address
444                          * type for the instruction, but we never use anything
445                          * but 32-bit flat model addressing in our code.
446                          */
447                     case S_BYTE:
448                         result->oprs[operand].type |= BITS8;
449                         break;
450                     case S_WORD:
451                         result->oprs[operand].type |= BITS16;
452                         break;
453                     case S_DWORD:
454                     case S_LONG:
455                         result->oprs[operand].type |= BITS32;
456                         break;
457                     case S_QWORD:
458                         result->oprs[operand].type |= BITS64;
459                         break;
460                     case S_TWORD:
461                         result->oprs[operand].type |= BITS80;
462                         break;
463                     case S_OWORD:
464                         result->oprs[operand].type |= BITS128;
465                         break;
466                     default:
467                         error(ERR_NONFATAL,
468                               "invalid operand size specification");
469                     }
470                 } else {
471                     /* Standard NASM compatible syntax */
472                     switch ((int)tokval.t_integer) {
473                     case S_NOSPLIT:
474                         result->oprs[operand].eaflags |= EAF_TIMESTWO;
475                         break;
476                     case S_REL:
477                         result->oprs[operand].eaflags |= EAF_REL;
478                         break;
479                     case S_ABS:
480                         result->oprs[operand].eaflags |= EAF_ABS;
481                         break;
482                     case S_BYTE:
483                         result->oprs[operand].eaflags |= EAF_BYTEOFFS;
484                         break;
485                     case S_WORD:
486                         result->oprs[operand].addr_size = 16;
487                         result->oprs[operand].eaflags |= EAF_WORDOFFS;
488                         break;
489                     case S_DWORD:
490                     case S_LONG:
491                         result->oprs[operand].addr_size = 32;
492                         result->oprs[operand].eaflags |= EAF_WORDOFFS;
493                         break;
494                     case S_QWORD:
495                         result->oprs[operand].addr_size = 64;
496                         result->oprs[operand].eaflags |= EAF_WORDOFFS;
497                         break;
498                     default:
499                         error(ERR_NONFATAL, "invalid size specification in"
500                               " effective address");
501                     }
502                 }
503             }
504         } else {                /* immediate operand, or register */
505             mref = false;
506             bracket = false;    /* placate optimisers */
507         }
508
509         if ((result->oprs[operand].type & FAR) && !mref &&
510             result->opcode != I_JMP && result->opcode != I_CALL) {
511             error(ERR_NONFATAL, "invalid use of FAR operand specifier");
512         }
513
514         value = evaluate(stdscan, NULL, &tokval,
515                          &result->oprs[operand].opflags,
516                          critical, error, &hints);
517         i = tokval.t_type;
518         if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
519             result->forw_ref = true;
520         }
521         if (!value) {           /* error in evaluator */
522             result->opcode = -1;        /* unrecoverable parse error: */
523             return result;      /* ignore this instruction */
524         }
525         if (i == ':' && mref) { /* it was seg:offset */
526             /*
527              * Process the segment override.
528              */
529             if (value[1].type != 0 || value->value != 1 ||
530                 REG_SREG & ~reg_flags[value->type])
531                 error(ERR_NONFATAL, "invalid segment override");
532             else if (result->nprefix == MAXPREFIX)
533                 error(ERR_NONFATAL,
534                       "instruction has more than %d prefixes", MAXPREFIX);
535             else {
536                 result->prefixes[result->nprefix++] = value->type;
537                 if (!(REG_FSGS & ~reg_flags[value->type]))
538                     result->oprs[operand].eaflags |= EAF_FSGS;
539             }
540
541             i = stdscan(NULL, &tokval); /* then skip the colon */
542             if (i == TOKEN_SPECIAL) {   /* another check for size override */
543                 switch ((int)tokval.t_integer) {
544                 case S_WORD:
545                     result->oprs[operand].addr_size = 16;
546                     break;
547                 case S_DWORD:
548                 case S_LONG:
549                     result->oprs[operand].addr_size = 32;
550                     break;
551                 case S_QWORD:
552                     result->oprs[operand].addr_size = 64;
553                     break;
554                 default:
555                     error(ERR_NONFATAL, "invalid size specification in"
556                           " effective address");
557                 }
558                 i = stdscan(NULL, &tokval);
559             }
560             value = evaluate(stdscan, NULL, &tokval,
561                              &result->oprs[operand].opflags,
562                              critical, error, &hints);
563             i = tokval.t_type;
564             if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
565                 result->forw_ref = true;
566             }
567             /* and get the offset */
568             if (!value) {       /* but, error in evaluator */
569                 result->opcode = -1;    /* unrecoverable parse error: */
570                 return result;  /* ignore this instruction */
571             }
572         }
573         if (mref && bracket) {  /* find ] at the end */
574             if (i != ']') {
575                 error(ERR_NONFATAL, "parser: expecting ]");
576                 do {            /* error recovery again */
577                     i = stdscan(NULL, &tokval);
578                 } while (i != 0 && i != ',');
579             } else              /* we got the required ] */
580                 i = stdscan(NULL, &tokval);
581         } else {                /* immediate operand */
582             if (i != 0 && i != ',' && i != ':') {
583                 error(ERR_NONFATAL, "comma or end of line expected");
584                 do {            /* error recovery */
585                     i = stdscan(NULL, &tokval);
586                 } while (i != 0 && i != ',');
587             } else if (i == ':') {
588                 result->oprs[operand].type |= COLON;
589             }
590         }
591
592         /* now convert the exprs returned from evaluate() into operand
593          * descriptions... */
594
595         if (mref) {             /* it's a memory reference */
596             expr *e = value;
597             int b, i, s;        /* basereg, indexreg, scale */
598             int64_t o;             /* offset */
599
600             b = i = -1, o = s = 0;
601             result->oprs[operand].hintbase = hints.base;
602             result->oprs[operand].hinttype = hints.type;
603
604             if (e->type && e->type <= EXPR_REG_END) {   /* this bit's a register */
605                 if (e->value == 1)      /* in fact it can be basereg */
606                     b = e->type;
607                 else            /* no, it has to be indexreg */
608                     i = e->type, s = e->value;
609                 e++;
610             }
611             if (e->type && e->type <= EXPR_REG_END) {   /* it's a 2nd register */
612                 if (b != -1)    /* If the first was the base, ... */
613                     i = e->type, s = e->value;  /* second has to be indexreg */
614
615                 else if (e->value != 1) {       /* If both want to be index */
616                     error(ERR_NONFATAL,
617                           "beroset-p-592-invalid effective address");
618                     result->opcode = -1;
619                     return result;
620                 } else
621                     b = e->type;
622                 e++;
623             }
624             if (e->type != 0) { /* is there an offset? */
625                 if (e->type <= EXPR_REG_END) {  /* in fact, is there an error? */
626                     error(ERR_NONFATAL,
627                           "beroset-p-603-invalid effective address");
628                     result->opcode = -1;
629                     return result;
630                 } else {
631                     if (e->type == EXPR_UNKNOWN) {
632                         o = 0;  /* doesn't matter what */
633                         result->oprs[operand].wrt = NO_SEG;     /* nor this */
634                         result->oprs[operand].segment = NO_SEG; /* or this */
635                         while (e->type)
636                             e++;        /* go to the end of the line */
637                     } else {
638                         if (e->type == EXPR_SIMPLE) {
639                             o = e->value;
640                             e++;
641                         }
642                         if (e->type == EXPR_WRT) {
643                             result->oprs[operand].wrt = e->value;
644                             e++;
645                         } else
646                             result->oprs[operand].wrt = NO_SEG;
647                         /*
648                          * Look for a segment base type.
649                          */
650                         if (e->type && e->type < EXPR_SEGBASE) {
651                             error(ERR_NONFATAL,
652                                   "beroset-p-630-invalid effective address");
653                             result->opcode = -1;
654                             return result;
655                         }
656                         while (e->type && e->value == 0)
657                             e++;
658                         if (e->type && e->value != 1) {
659                             error(ERR_NONFATAL,
660                                   "beroset-p-637-invalid effective address");
661                             result->opcode = -1;
662                             return result;
663                         }
664                         if (e->type) {
665                             result->oprs[operand].segment =
666                                 e->type - EXPR_SEGBASE;
667                             e++;
668                         } else
669                             result->oprs[operand].segment = NO_SEG;
670                         while (e->type && e->value == 0)
671                             e++;
672                         if (e->type) {
673                             error(ERR_NONFATAL,
674                                   "beroset-p-650-invalid effective address");
675                             result->opcode = -1;
676                             return result;
677                         }
678                     }
679                 }
680             } else {
681                 o = 0;
682                 result->oprs[operand].wrt = NO_SEG;
683                 result->oprs[operand].segment = NO_SEG;
684             }
685
686             if (e->type != 0) { /* there'd better be nothing left! */
687                 error(ERR_NONFATAL,
688                       "beroset-p-663-invalid effective address");
689                 result->opcode = -1;
690                 return result;
691             }
692
693             /* It is memory, but it can match any r/m operand */
694             result->oprs[operand].type |= MEMORY_ANY;
695
696             if (b == -1 && (i == -1 || s == 0)) {
697                 int is_rel = globalbits == 64 &&
698                     !(result->oprs[operand].eaflags & EAF_ABS) &&
699                     ((globalrel &&
700                       !(result->oprs[operand].eaflags & EAF_FSGS)) ||
701                      (result->oprs[operand].eaflags & EAF_REL));
702
703                 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
704             }
705             result->oprs[operand].basereg = b;
706             result->oprs[operand].indexreg = i;
707             result->oprs[operand].scale = s;
708             result->oprs[operand].offset = o;
709         } else {                /* it's not a memory reference */
710
711             if (is_just_unknown(value)) {       /* it's immediate but unknown */
712                 result->oprs[operand].type |= IMMEDIATE;
713                 result->oprs[operand].offset = 0;       /* don't care */
714                 result->oprs[operand].segment = NO_SEG; /* don't care again */
715                 result->oprs[operand].wrt = NO_SEG;     /* still don't care */
716             } else if (is_reloc(value)) {       /* it's immediate */
717                 result->oprs[operand].type |= IMMEDIATE;
718                 result->oprs[operand].offset = reloc_value(value);
719                 result->oprs[operand].segment = reloc_seg(value);
720                 result->oprs[operand].wrt = reloc_wrt(value);
721                 if (is_simple(value)) {
722                     if (reloc_value(value) == 1)
723                         result->oprs[operand].type |= UNITY;
724                     if (optimizing >= 0 &&
725                         !(result->oprs[operand].type & STRICT)) {
726                         if (reloc_value(value) >= -128 &&
727                             reloc_value(value) <= 127)
728                             result->oprs[operand].type |= SBYTE;
729                     }
730                 }
731             } else {            /* it's a register */
732
733                 if (value->type >= EXPR_SIMPLE || value->value != 1) {
734                     error(ERR_NONFATAL, "invalid operand type");
735                     result->opcode = -1;
736                     return result;
737                 }
738
739                 /*
740                  * check that its only 1 register, not an expression...
741                  */
742                 for (i = 1; value[i].type; i++)
743                     if (value[i].value) {
744                         error(ERR_NONFATAL, "invalid operand type");
745                         result->opcode = -1;
746                         return result;
747                     }
748
749                 /* clear overrides, except TO which applies to FPU regs */
750                 if (result->oprs[operand].type & ~TO) {
751                     /*
752                      * we want to produce a warning iff the specified size
753                      * is different from the register size
754                      */
755                     i = result->oprs[operand].type & SIZE_MASK;
756                 } else
757                     i = 0;
758
759                 result->oprs[operand].type &= TO;
760                 result->oprs[operand].type |= REGISTER;
761                 result->oprs[operand].type |= reg_flags[value->type];
762                 result->oprs[operand].basereg = value->type;
763
764                 if (i && (result->oprs[operand].type & SIZE_MASK) != i)
765                     error(ERR_WARNING | ERR_PASS1,
766                           "register size specification ignored");
767             }
768         }
769     }
770
771     result->operands = operand; /* set operand count */
772
773     while (operand < 3)         /* clear remaining operands */
774         result->oprs[operand++].type = 0;
775
776     /*
777      * Transform RESW, RESD, RESQ, REST, RESO into RESB.
778      */
779     switch (result->opcode) {
780     case I_RESW:
781         result->opcode = I_RESB;
782         result->oprs[0].offset *= 2;
783         break;
784     case I_RESD:
785         result->opcode = I_RESB;
786         result->oprs[0].offset *= 4;
787         break;
788     case I_RESQ:
789         result->opcode = I_RESB;
790         result->oprs[0].offset *= 8;
791         break;
792     case I_REST:
793         result->opcode = I_RESB;
794         result->oprs[0].offset *= 10;
795         break;
796     case I_RESO:
797         result->opcode = I_RESB;
798         result->oprs[0].offset *= 16;
799         break;
800     default:
801         break;
802     }
803
804     return result;
805 }
806
807 static int is_comma_next(void)
808 {
809     char *p;
810     int i;
811     struct tokenval tv;
812
813     p = stdscan_bufptr;
814     i = stdscan(NULL, &tv);
815     stdscan_bufptr = p;
816     return (i == ',' || i == ';' || !i);
817 }
818
819 void cleanup_insn(insn * i)
820 {
821     extop *e;
822
823     while (i->eops) {
824         e = i->eops;
825         i->eops = i->eops->next;
826         nasm_free(e);
827     }
828 }