64-bit addressing and prefix handling changes
[nasm.git] / parser.c
blobfad755e88f39e861ea615010b8ec0364aaa65a80
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 error(ERR_NONFATAL,
262 "instruction has conflicting prefixes");
264 result->prefixes[slot] = tokval.t_integer;
265 i = stdscan(NULL, &tokval);
269 if (i != TOKEN_INSN) {
270 int j;
271 enum prefixes pfx;
273 for (j = 0; j < MAXPREFIX; j++)
274 if ((pfx = result->prefixes[j]) != P_none)
275 break;
277 if (i == 0 && pfx != P_none) {
279 * Instruction prefixes are present, but no actual
280 * instruction. This is allowed: at this point we
281 * invent a notional instruction of RESB 0.
283 result->opcode = I_RESB;
284 result->operands = 1;
285 result->oprs[0].type = IMMEDIATE;
286 result->oprs[0].offset = 0L;
287 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
288 return result;
289 } else {
290 error(ERR_NONFATAL, "parser: instruction expected");
291 result->opcode = -1;
292 return result;
296 result->opcode = tokval.t_integer;
297 result->condition = tokval.t_inttwo;
300 * RESB, RESW and RESD cannot be satisfied with incorrectly
301 * evaluated operands, since the correct values _must_ be known
302 * on the first pass. Hence, even in pass one, we set the
303 * `critical' flag on calling evaluate(), so that it will bomb
304 * out on undefined symbols. Nasty, but there's nothing we can
305 * do about it.
307 * For the moment, EQU has the same difficulty, so we'll
308 * include that.
310 if (result->opcode == I_RESB || result->opcode == I_RESW ||
311 result->opcode == I_RESD || result->opcode == I_RESQ ||
312 result->opcode == I_REST || result->opcode == I_RESO ||
313 result->opcode == I_EQU || result->opcode == I_INCBIN) {
314 critical = pass0;
315 } else
316 critical = (pass == 2 ? 2 : 0);
318 if (result->opcode == I_DB || result->opcode == I_DW ||
319 result->opcode == I_DD || result->opcode == I_DQ ||
320 result->opcode == I_DT || result->opcode == I_DO ||
321 result->opcode == I_INCBIN) {
322 extop *eop, **tail = &result->eops, **fixptr;
323 int oper_num = 0;
325 result->eops_float = false;
328 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
330 while (1) {
331 i = stdscan(NULL, &tokval);
332 if (i == 0)
333 break;
334 fixptr = tail;
335 eop = *tail = nasm_malloc(sizeof(extop));
336 tail = &eop->next;
337 eop->next = NULL;
338 eop->type = EOT_NOTHING;
339 oper_num++;
341 if (i == TOKEN_NUM && tokval.t_charptr && is_comma_next()) {
342 eop->type = EOT_DB_STRING;
343 eop->stringval = tokval.t_charptr;
344 eop->stringlen = tokval.t_inttwo;
345 i = stdscan(NULL, &tokval); /* eat the comma */
346 continue;
349 if ((i == TOKEN_FLOAT && is_comma_next())
350 || i == '-' || i == '+') {
351 int32_t sign = +1;
353 if (i == '+' || i == '-') {
354 char *save = stdscan_bufptr;
355 int token = i;
356 sign = (i == '-') ? -1 : 1;
357 i = stdscan(NULL, &tokval);
358 if (i != TOKEN_FLOAT || !is_comma_next()) {
359 stdscan_bufptr = save;
360 i = tokval.t_type = token;
364 if (i == TOKEN_FLOAT) {
365 eop->type = EOT_DB_STRING;
366 result->eops_float = true;
367 switch (result->opcode) {
368 case I_DW:
369 eop->stringlen = 2;
370 break;
371 case I_DD:
372 eop->stringlen = 4;
373 break;
374 case I_DQ:
375 eop->stringlen = 8;
376 break;
377 case I_DT:
378 eop->stringlen = 10;
379 break;
380 case I_DO:
381 eop->stringlen = 16;
382 break;
383 default:
384 error(ERR_NONFATAL, "floating-point constant"
385 " encountered in `db' instruction");
387 * fix suggested by Pedro Gimeno... original line
388 * was:
389 * eop->type = EOT_NOTHING;
391 eop->stringlen = 0;
392 break;
394 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
395 tail = &eop->next;
396 *fixptr = eop;
397 eop->stringval = (char *)eop + sizeof(extop);
398 if (!eop->stringlen ||
399 !float_const(tokval.t_charptr, sign,
400 (uint8_t *)eop->stringval,
401 eop->stringlen, error))
402 eop->type = EOT_NOTHING;
403 i = stdscan(NULL, &tokval); /* eat the comma */
404 continue;
408 /* anything else */
410 expr *value;
411 value = evaluate(stdscan, NULL, &tokval, NULL,
412 critical, error, NULL);
413 i = tokval.t_type;
414 if (!value) { /* error in evaluator */
415 result->opcode = -1; /* unrecoverable parse error: */
416 return result; /* ignore this instruction */
418 if (is_unknown(value)) {
419 eop->type = EOT_DB_NUMBER;
420 eop->offset = 0; /* doesn't matter what we put */
421 eop->segment = eop->wrt = NO_SEG; /* likewise */
422 } else if (is_reloc(value)) {
423 eop->type = EOT_DB_NUMBER;
424 eop->offset = reloc_value(value);
425 eop->segment = reloc_seg(value);
426 eop->wrt = reloc_wrt(value);
427 } else {
428 error(ERR_NONFATAL,
429 "operand %d: expression is not simple"
430 " or relocatable", oper_num);
435 * We're about to call stdscan(), which will eat the
436 * comma that we're currently sitting on between
437 * arguments. However, we'd better check first that it
438 * _is_ a comma.
440 if (i == 0) /* also could be EOL */
441 break;
442 if (i != ',') {
443 error(ERR_NONFATAL, "comma expected after operand %d",
444 oper_num);
445 result->opcode = -1; /* unrecoverable parse error: */
446 return result; /* ignore this instruction */
450 if (result->opcode == I_INCBIN) {
452 * Correct syntax for INCBIN is that there should be
453 * one string operand, followed by one or two numeric
454 * operands.
456 if (!result->eops || result->eops->type != EOT_DB_STRING)
457 error(ERR_NONFATAL, "`incbin' expects a file name");
458 else if (result->eops->next &&
459 result->eops->next->type != EOT_DB_NUMBER)
460 error(ERR_NONFATAL, "`incbin': second parameter is",
461 " non-numeric");
462 else if (result->eops->next && result->eops->next->next &&
463 result->eops->next->next->type != EOT_DB_NUMBER)
464 error(ERR_NONFATAL, "`incbin': third parameter is",
465 " non-numeric");
466 else if (result->eops->next && result->eops->next->next &&
467 result->eops->next->next->next)
468 error(ERR_NONFATAL,
469 "`incbin': more than three parameters");
470 else
471 return result;
473 * If we reach here, one of the above errors happened.
474 * Throw the instruction away.
476 result->opcode = -1;
477 return result;
478 } else /* DB ... */ if (oper_num == 0)
479 error(ERR_WARNING | ERR_PASS1,
480 "no operand for data declaration");
481 else
482 result->operands = oper_num;
484 return result;
487 /* right. Now we begin to parse the operands. There may be up to four
488 * of these, separated by commas, and terminated by a zero token. */
490 for (operand = 0; operand < MAX_OPERANDS; operand++) {
491 expr *value; /* used most of the time */
492 int mref; /* is this going to be a memory ref? */
493 int bracket; /* is it a [] mref, or a & mref? */
494 int setsize = 0;
496 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
497 result->oprs[operand].eaflags = 0; /* and this */
498 result->oprs[operand].opflags = 0;
500 i = stdscan(NULL, &tokval);
501 if (i == 0)
502 break; /* end of operands: get out of here */
503 result->oprs[operand].type = 0; /* so far, no override */
504 while (i == TOKEN_SPECIAL) { /* size specifiers */
505 switch ((int)tokval.t_integer) {
506 case S_BYTE:
507 if (!setsize) /* we want to use only the first */
508 result->oprs[operand].type |= BITS8;
509 setsize = 1;
510 break;
511 case S_WORD:
512 if (!setsize)
513 result->oprs[operand].type |= BITS16;
514 setsize = 1;
515 break;
516 case S_DWORD:
517 case S_LONG:
518 if (!setsize)
519 result->oprs[operand].type |= BITS32;
520 setsize = 1;
521 break;
522 case S_QWORD:
523 if (!setsize)
524 result->oprs[operand].type |= BITS64;
525 setsize = 1;
526 break;
527 case S_TWORD:
528 if (!setsize)
529 result->oprs[operand].type |= BITS80;
530 setsize = 1;
531 break;
532 case S_OWORD:
533 if (!setsize)
534 result->oprs[operand].type |= BITS128;
535 setsize = 1;
536 break;
537 case S_TO:
538 result->oprs[operand].type |= TO;
539 break;
540 case S_STRICT:
541 result->oprs[operand].type |= STRICT;
542 break;
543 case S_FAR:
544 result->oprs[operand].type |= FAR;
545 break;
546 case S_NEAR:
547 result->oprs[operand].type |= NEAR;
548 break;
549 case S_SHORT:
550 result->oprs[operand].type |= SHORT;
551 break;
552 default:
553 error(ERR_NONFATAL, "invalid operand size specification");
555 i = stdscan(NULL, &tokval);
558 if (i == '[' || i == '&') { /* memory reference */
559 mref = true;
560 bracket = (i == '[');
561 i = stdscan(NULL, &tokval); /* then skip the colon */
562 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
563 process_size_override(result, operand);
564 i = stdscan(NULL, &tokval);
566 } else { /* immediate operand, or register */
567 mref = false;
568 bracket = false; /* placate optimisers */
571 if ((result->oprs[operand].type & FAR) && !mref &&
572 result->opcode != I_JMP && result->opcode != I_CALL) {
573 error(ERR_NONFATAL, "invalid use of FAR operand specifier");
576 value = evaluate(stdscan, NULL, &tokval,
577 &result->oprs[operand].opflags,
578 critical, error, &hints);
579 i = tokval.t_type;
580 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
581 result->forw_ref = true;
583 if (!value) { /* error in evaluator */
584 result->opcode = -1; /* unrecoverable parse error: */
585 return result; /* ignore this instruction */
587 if (i == ':' && mref) { /* it was seg:offset */
589 * Process the segment override.
591 if (value[1].type != 0 || value->value != 1 ||
592 REG_SREG & ~reg_flags[value->type])
593 error(ERR_NONFATAL, "invalid segment override");
594 else if (result->prefixes[PPS_SEG])
595 error(ERR_NONFATAL,
596 "instruction has conflicting segment overrides");
597 else {
598 result->prefixes[PPS_SEG] = value->type;
599 if (!(REG_FSGS & ~reg_flags[value->type]))
600 result->oprs[operand].eaflags |= EAF_FSGS;
603 i = stdscan(NULL, &tokval); /* then skip the colon */
604 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
605 process_size_override(result, operand);
606 i = stdscan(NULL, &tokval);
608 value = evaluate(stdscan, NULL, &tokval,
609 &result->oprs[operand].opflags,
610 critical, error, &hints);
611 i = tokval.t_type;
612 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
613 result->forw_ref = true;
615 /* and get the offset */
616 if (!value) { /* but, error in evaluator */
617 result->opcode = -1; /* unrecoverable parse error: */
618 return result; /* ignore this instruction */
621 if (mref && bracket) { /* find ] at the end */
622 if (i != ']') {
623 error(ERR_NONFATAL, "parser: expecting ]");
624 do { /* error recovery again */
625 i = stdscan(NULL, &tokval);
626 } while (i != 0 && i != ',');
627 } else /* we got the required ] */
628 i = stdscan(NULL, &tokval);
629 } else { /* immediate operand */
630 if (i != 0 && i != ',' && i != ':') {
631 error(ERR_NONFATAL, "comma or end of line expected");
632 do { /* error recovery */
633 i = stdscan(NULL, &tokval);
634 } while (i != 0 && i != ',');
635 } else if (i == ':') {
636 result->oprs[operand].type |= COLON;
640 /* now convert the exprs returned from evaluate() into operand
641 * descriptions... */
643 if (mref) { /* it's a memory reference */
644 expr *e = value;
645 int b, i, s; /* basereg, indexreg, scale */
646 int64_t o; /* offset */
648 b = i = -1, o = s = 0;
649 result->oprs[operand].hintbase = hints.base;
650 result->oprs[operand].hinttype = hints.type;
652 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
653 if (e->value == 1) /* in fact it can be basereg */
654 b = e->type;
655 else /* no, it has to be indexreg */
656 i = e->type, s = e->value;
657 e++;
659 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
660 if (b != -1) /* If the first was the base, ... */
661 i = e->type, s = e->value; /* second has to be indexreg */
663 else if (e->value != 1) { /* If both want to be index */
664 error(ERR_NONFATAL,
665 "beroset-p-592-invalid effective address");
666 result->opcode = -1;
667 return result;
668 } else
669 b = e->type;
670 e++;
672 if (e->type != 0) { /* is there an offset? */
673 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
674 error(ERR_NONFATAL,
675 "beroset-p-603-invalid effective address");
676 result->opcode = -1;
677 return result;
678 } else {
679 if (e->type == EXPR_UNKNOWN) {
680 o = 0; /* doesn't matter what */
681 result->oprs[operand].wrt = NO_SEG; /* nor this */
682 result->oprs[operand].segment = NO_SEG; /* or this */
683 while (e->type)
684 e++; /* go to the end of the line */
685 } else {
686 if (e->type == EXPR_SIMPLE) {
687 o = e->value;
688 e++;
690 if (e->type == EXPR_WRT) {
691 result->oprs[operand].wrt = e->value;
692 e++;
693 } else
694 result->oprs[operand].wrt = NO_SEG;
696 * Look for a segment base type.
698 if (e->type && e->type < EXPR_SEGBASE) {
699 error(ERR_NONFATAL,
700 "beroset-p-630-invalid effective address");
701 result->opcode = -1;
702 return result;
704 while (e->type && e->value == 0)
705 e++;
706 if (e->type && e->value != 1) {
707 error(ERR_NONFATAL,
708 "beroset-p-637-invalid effective address");
709 result->opcode = -1;
710 return result;
712 if (e->type) {
713 result->oprs[operand].segment =
714 e->type - EXPR_SEGBASE;
715 e++;
716 } else
717 result->oprs[operand].segment = NO_SEG;
718 while (e->type && e->value == 0)
719 e++;
720 if (e->type) {
721 error(ERR_NONFATAL,
722 "beroset-p-650-invalid effective address");
723 result->opcode = -1;
724 return result;
728 } else {
729 o = 0;
730 result->oprs[operand].wrt = NO_SEG;
731 result->oprs[operand].segment = NO_SEG;
734 if (e->type != 0) { /* there'd better be nothing left! */
735 error(ERR_NONFATAL,
736 "beroset-p-663-invalid effective address");
737 result->opcode = -1;
738 return result;
741 /* It is memory, but it can match any r/m operand */
742 result->oprs[operand].type |= MEMORY_ANY;
744 if (b == -1 && (i == -1 || s == 0)) {
745 int is_rel = globalbits == 64 &&
746 !(result->oprs[operand].eaflags & EAF_ABS) &&
747 ((globalrel &&
748 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
749 (result->oprs[operand].eaflags & EAF_REL));
751 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
753 result->oprs[operand].basereg = b;
754 result->oprs[operand].indexreg = i;
755 result->oprs[operand].scale = s;
756 result->oprs[operand].offset = o;
757 } else { /* it's not a memory reference */
759 if (is_just_unknown(value)) { /* it's immediate but unknown */
760 result->oprs[operand].type |= IMMEDIATE;
761 result->oprs[operand].offset = 0; /* don't care */
762 result->oprs[operand].segment = NO_SEG; /* don't care again */
763 result->oprs[operand].wrt = NO_SEG; /* still don't care */
764 } else if (is_reloc(value)) { /* it's immediate */
765 result->oprs[operand].type |= IMMEDIATE;
766 result->oprs[operand].offset = reloc_value(value);
767 result->oprs[operand].segment = reloc_seg(value);
768 result->oprs[operand].wrt = reloc_wrt(value);
769 if (is_simple(value)) {
770 if (reloc_value(value) == 1)
771 result->oprs[operand].type |= UNITY;
772 if (optimizing >= 0 &&
773 !(result->oprs[operand].type & STRICT)) {
774 if (reloc_value(value) >= -128 &&
775 reloc_value(value) <= 127)
776 result->oprs[operand].type |= SBYTE;
779 } else { /* it's a register */
781 if (value->type >= EXPR_SIMPLE || value->value != 1) {
782 error(ERR_NONFATAL, "invalid operand type");
783 result->opcode = -1;
784 return result;
788 * check that its only 1 register, not an expression...
790 for (i = 1; value[i].type; i++)
791 if (value[i].value) {
792 error(ERR_NONFATAL, "invalid operand type");
793 result->opcode = -1;
794 return result;
797 /* clear overrides, except TO which applies to FPU regs */
798 if (result->oprs[operand].type & ~TO) {
800 * we want to produce a warning iff the specified size
801 * is different from the register size
803 i = result->oprs[operand].type & SIZE_MASK;
804 } else
805 i = 0;
807 result->oprs[operand].type &= TO;
808 result->oprs[operand].type |= REGISTER;
809 result->oprs[operand].type |= reg_flags[value->type];
810 result->oprs[operand].basereg = value->type;
812 if (i && (result->oprs[operand].type & SIZE_MASK) != i)
813 error(ERR_WARNING | ERR_PASS1,
814 "register size specification ignored");
819 result->operands = operand; /* set operand count */
821 /* clear remaining operands */
822 while (operand < MAX_OPERANDS)
823 result->oprs[operand++].type = 0;
826 * Transform RESW, RESD, RESQ, REST, RESO into RESB.
828 switch (result->opcode) {
829 case I_RESW:
830 result->opcode = I_RESB;
831 result->oprs[0].offset *= 2;
832 break;
833 case I_RESD:
834 result->opcode = I_RESB;
835 result->oprs[0].offset *= 4;
836 break;
837 case I_RESQ:
838 result->opcode = I_RESB;
839 result->oprs[0].offset *= 8;
840 break;
841 case I_REST:
842 result->opcode = I_RESB;
843 result->oprs[0].offset *= 10;
844 break;
845 case I_RESO:
846 result->opcode = I_RESB;
847 result->oprs[0].offset *= 16;
848 break;
849 default:
850 break;
853 return result;
856 static int is_comma_next(void)
858 char *p;
859 int i;
860 struct tokenval tv;
862 p = stdscan_bufptr;
863 i = stdscan(NULL, &tv);
864 stdscan_bufptr = p;
865 return (i == ',' || i == ';' || !i);
868 void cleanup_insn(insn * i)
870 extop *e;
872 while (i->eops) {
873 e = i->eops;
874 i->eops = i->eops->next;
875 nasm_free(e);