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