The AVX R,X,B, and V fields (but not W) are inverted.
[nasm.git] / parser.c
blob9172fb1cd3145bef6cd31f9ac00d5ef5cdc9d2f7
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 license given in the file "LICENSE"
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;
174 bool first;
175 bool insn_is_label = false;
177 restart_parse:
178 first = true;
179 result->forw_ref = false;
180 error = errfunc;
182 stdscan_reset();
183 stdscan_bufptr = buffer;
184 i = stdscan(NULL, &tokval);
186 result->label = NULL; /* Assume no label */
187 result->eops = NULL; /* must do this, whatever happens */
188 result->operands = 0; /* must initialize this */
190 if (i == 0) { /* blank line - ignore */
191 result->opcode = -1; /* and no instruction either */
192 return result;
194 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
195 (i != TOKEN_REG || (REG_SREG & ~reg_flags[tokval.t_integer]))) {
196 error(ERR_NONFATAL, "label or instruction expected"
197 " at start of line");
198 result->opcode = -1;
199 return result;
202 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
203 /* there's a label here */
204 first = false;
205 result->label = tokval.t_charptr;
206 i = stdscan(NULL, &tokval);
207 if (i == ':') { /* skip over the optional colon */
208 i = stdscan(NULL, &tokval);
209 } else if (i == 0) {
210 error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
211 "label alone on a line without a colon might be in error");
213 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
215 * FIXME: location->segment could be NO_SEG, in which case
216 * it is possible we should be passing 'abs_seg'. Look into this.
217 * Work out whether that is *really* what we should be doing.
218 * Generally fix things. I think this is right as it is, but
219 * am still not certain.
221 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
222 location->offset, NULL, true, false, outfmt, errfunc);
226 if (i == 0) {
227 result->opcode = -1; /* this line contains just a label */
228 return result;
231 for (j = 0; j < MAXPREFIX; j++)
232 result->prefixes[j] = P_none;
233 result->times = 1L;
235 while (i == TOKEN_PREFIX ||
236 (i == TOKEN_REG && !(REG_SREG & ~reg_flags[tokval.t_integer])))
238 first = false;
241 * Handle special case: the TIMES prefix.
243 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
244 expr *value;
246 i = stdscan(NULL, &tokval);
247 value =
248 evaluate(stdscan, NULL, &tokval, NULL, pass0, error, NULL);
249 i = tokval.t_type;
250 if (!value) { /* but, error in evaluator */
251 result->opcode = -1; /* unrecoverable parse error: */
252 return result; /* ignore this instruction */
254 if (!is_simple(value)) {
255 error(ERR_NONFATAL,
256 "non-constant argument supplied to TIMES");
257 result->times = 1L;
258 } else {
259 result->times = value->value;
260 if (value->value < 0) {
261 error(ERR_NONFATAL, "TIMES value %d is negative",
262 value->value);
263 result->times = 0;
266 } else {
267 int slot = prefix_slot(tokval.t_integer);
268 if (result->prefixes[slot]) {
269 if (result->prefixes[slot] == tokval.t_integer)
270 error(ERR_WARNING,
271 "instruction has redundant prefixes");
272 else
273 error(ERR_NONFATAL,
274 "instruction has conflicting prefixes");
276 result->prefixes[slot] = tokval.t_integer;
277 i = stdscan(NULL, &tokval);
281 if (i != TOKEN_INSN) {
282 int j;
283 enum prefixes pfx;
285 for (j = 0; j < MAXPREFIX; j++)
286 if ((pfx = result->prefixes[j]) != P_none)
287 break;
289 if (i == 0 && pfx != P_none) {
291 * Instruction prefixes are present, but no actual
292 * instruction. This is allowed: at this point we
293 * invent a notional instruction of RESB 0.
295 result->opcode = I_RESB;
296 result->operands = 1;
297 result->oprs[0].type = IMMEDIATE;
298 result->oprs[0].offset = 0L;
299 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
300 return result;
301 } else {
302 error(ERR_NONFATAL, "parser: instruction expected");
303 result->opcode = -1;
304 return result;
308 result->opcode = tokval.t_integer;
309 result->condition = tokval.t_inttwo;
312 * RESB, RESW and RESD cannot be satisfied with incorrectly
313 * evaluated operands, since the correct values _must_ be known
314 * on the first pass. Hence, even in pass one, we set the
315 * `critical' flag on calling evaluate(), so that it will bomb
316 * out on undefined symbols. Nasty, but there's nothing we can
317 * do about it.
319 * For the moment, EQU has the same difficulty, so we'll
320 * include that.
322 if (result->opcode == I_RESB || result->opcode == I_RESW ||
323 result->opcode == I_RESD || result->opcode == I_RESQ ||
324 result->opcode == I_REST || result->opcode == I_RESO ||
325 result->opcode == I_EQU || result->opcode == I_INCBIN) {
326 critical = (pass0 < 2 ? 1 : 2);
328 } else
329 critical = (pass == 2 ? 2 : 0);
331 if (result->opcode == I_DB || result->opcode == I_DW ||
332 result->opcode == I_DD || result->opcode == I_DQ ||
333 result->opcode == I_DT || result->opcode == I_DO ||
334 result->opcode == I_INCBIN) {
335 extop *eop, **tail = &result->eops, **fixptr;
336 int oper_num = 0;
338 result->eops_float = false;
341 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
343 while (1) {
344 i = stdscan(NULL, &tokval);
345 if (i == 0)
346 break;
347 else if (first && i == ':') {
348 insn_is_label = true;
349 goto restart_parse;
351 first = false;
352 fixptr = tail;
353 eop = *tail = nasm_malloc(sizeof(extop));
354 tail = &eop->next;
355 eop->next = NULL;
356 eop->type = EOT_NOTHING;
357 oper_num++;
359 if (i == TOKEN_NUM && tokval.t_charptr && is_comma_next()) {
360 eop->type = EOT_DB_STRING;
361 eop->stringval = tokval.t_charptr;
362 eop->stringlen = tokval.t_inttwo;
363 i = stdscan(NULL, &tokval); /* eat the comma */
364 continue;
367 if ((i == TOKEN_FLOAT && is_comma_next())
368 || i == '-' || i == '+') {
369 int32_t sign = +1;
371 if (i == '+' || i == '-') {
372 char *save = stdscan_bufptr;
373 int token = i;
374 sign = (i == '-') ? -1 : 1;
375 i = stdscan(NULL, &tokval);
376 if (i != TOKEN_FLOAT || !is_comma_next()) {
377 stdscan_bufptr = save;
378 i = tokval.t_type = token;
382 if (i == TOKEN_FLOAT) {
383 eop->type = EOT_DB_STRING;
384 result->eops_float = true;
385 switch (result->opcode) {
386 case I_DB:
387 eop->stringlen = 1;
388 break;
389 case I_DW:
390 eop->stringlen = 2;
391 break;
392 case I_DD:
393 eop->stringlen = 4;
394 break;
395 case I_DQ:
396 eop->stringlen = 8;
397 break;
398 case I_DT:
399 eop->stringlen = 10;
400 break;
401 case I_DO:
402 eop->stringlen = 16;
403 break;
404 default:
405 error(ERR_NONFATAL, "floating-point constant"
406 " encountered in unknown instruction");
408 * fix suggested by Pedro Gimeno... original line
409 * was:
410 * eop->type = EOT_NOTHING;
412 eop->stringlen = 0;
413 break;
415 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
416 tail = &eop->next;
417 *fixptr = eop;
418 eop->stringval = (char *)eop + sizeof(extop);
419 if (!eop->stringlen ||
420 !float_const(tokval.t_charptr, sign,
421 (uint8_t *)eop->stringval,
422 eop->stringlen, error))
423 eop->type = EOT_NOTHING;
424 i = stdscan(NULL, &tokval); /* eat the comma */
425 continue;
429 /* anything else */
431 expr *value;
432 value = evaluate(stdscan, NULL, &tokval, NULL,
433 critical, error, NULL);
434 i = tokval.t_type;
435 if (!value) { /* error in evaluator */
436 result->opcode = -1; /* unrecoverable parse error: */
437 return result; /* ignore this instruction */
439 if (is_unknown(value)) {
440 eop->type = EOT_DB_NUMBER;
441 eop->offset = 0; /* doesn't matter what we put */
442 eop->segment = eop->wrt = NO_SEG; /* likewise */
443 } else if (is_reloc(value)) {
444 eop->type = EOT_DB_NUMBER;
445 eop->offset = reloc_value(value);
446 eop->segment = reloc_seg(value);
447 eop->wrt = reloc_wrt(value);
448 } else {
449 error(ERR_NONFATAL,
450 "operand %d: expression is not simple"
451 " or relocatable", oper_num);
456 * We're about to call stdscan(), which will eat the
457 * comma that we're currently sitting on between
458 * arguments. However, we'd better check first that it
459 * _is_ a comma.
461 if (i == 0) /* also could be EOL */
462 break;
463 if (i != ',') {
464 error(ERR_NONFATAL, "comma expected after operand %d",
465 oper_num);
466 result->opcode = -1; /* unrecoverable parse error: */
467 return result; /* ignore this instruction */
471 if (result->opcode == I_INCBIN) {
473 * Correct syntax for INCBIN is that there should be
474 * one string operand, followed by one or two numeric
475 * operands.
477 if (!result->eops || result->eops->type != EOT_DB_STRING)
478 error(ERR_NONFATAL, "`incbin' expects a file name");
479 else if (result->eops->next &&
480 result->eops->next->type != EOT_DB_NUMBER)
481 error(ERR_NONFATAL, "`incbin': second parameter is",
482 " non-numeric");
483 else if (result->eops->next && result->eops->next->next &&
484 result->eops->next->next->type != EOT_DB_NUMBER)
485 error(ERR_NONFATAL, "`incbin': third parameter is",
486 " non-numeric");
487 else if (result->eops->next && result->eops->next->next &&
488 result->eops->next->next->next)
489 error(ERR_NONFATAL,
490 "`incbin': more than three parameters");
491 else
492 return result;
494 * If we reach here, one of the above errors happened.
495 * Throw the instruction away.
497 result->opcode = -1;
498 return result;
499 } else /* DB ... */ if (oper_num == 0)
500 error(ERR_WARNING | ERR_PASS1,
501 "no operand for data declaration");
502 else
503 result->operands = oper_num;
505 return result;
508 /* right. Now we begin to parse the operands. There may be up to four
509 * of these, separated by commas, and terminated by a zero token. */
511 for (operand = 0; operand < MAX_OPERANDS; operand++) {
512 expr *value; /* used most of the time */
513 int mref; /* is this going to be a memory ref? */
514 int bracket; /* is it a [] mref, or a & mref? */
515 int setsize = 0;
517 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
518 result->oprs[operand].eaflags = 0; /* and this */
519 result->oprs[operand].opflags = 0;
521 i = stdscan(NULL, &tokval);
522 if (i == 0)
523 break; /* end of operands: get out of here */
524 else if (first && i == ':') {
525 insn_is_label = true;
526 goto restart_parse;
528 first = false;
529 result->oprs[operand].type = 0; /* so far, no override */
530 while (i == TOKEN_SPECIAL) { /* size specifiers */
531 switch ((int)tokval.t_integer) {
532 case S_BYTE:
533 if (!setsize) /* we want to use only the first */
534 result->oprs[operand].type |= BITS8;
535 setsize = 1;
536 break;
537 case S_WORD:
538 if (!setsize)
539 result->oprs[operand].type |= BITS16;
540 setsize = 1;
541 break;
542 case S_DWORD:
543 case S_LONG:
544 if (!setsize)
545 result->oprs[operand].type |= BITS32;
546 setsize = 1;
547 break;
548 case S_QWORD:
549 if (!setsize)
550 result->oprs[operand].type |= BITS64;
551 setsize = 1;
552 break;
553 case S_TWORD:
554 if (!setsize)
555 result->oprs[operand].type |= BITS80;
556 setsize = 1;
557 break;
558 case S_OWORD:
559 if (!setsize)
560 result->oprs[operand].type |= BITS128;
561 setsize = 1;
562 break;
563 case S_TO:
564 result->oprs[operand].type |= TO;
565 break;
566 case S_STRICT:
567 result->oprs[operand].type |= STRICT;
568 break;
569 case S_FAR:
570 result->oprs[operand].type |= FAR;
571 break;
572 case S_NEAR:
573 result->oprs[operand].type |= NEAR;
574 break;
575 case S_SHORT:
576 result->oprs[operand].type |= SHORT;
577 break;
578 default:
579 error(ERR_NONFATAL, "invalid operand size specification");
581 i = stdscan(NULL, &tokval);
584 if (i == '[' || i == '&') { /* memory reference */
585 mref = true;
586 bracket = (i == '[');
587 i = stdscan(NULL, &tokval); /* then skip the colon */
588 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
589 process_size_override(result, operand);
590 i = stdscan(NULL, &tokval);
592 } else { /* immediate operand, or register */
593 mref = false;
594 bracket = false; /* placate optimisers */
597 if ((result->oprs[operand].type & FAR) && !mref &&
598 result->opcode != I_JMP && result->opcode != I_CALL) {
599 error(ERR_NONFATAL, "invalid use of FAR operand specifier");
602 value = evaluate(stdscan, NULL, &tokval,
603 &result->oprs[operand].opflags,
604 critical, error, &hints);
605 i = tokval.t_type;
606 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
607 result->forw_ref = true;
609 if (!value) { /* error in evaluator */
610 result->opcode = -1; /* unrecoverable parse error: */
611 return result; /* ignore this instruction */
613 if (i == ':' && mref) { /* it was seg:offset */
615 * Process the segment override.
617 if (value[1].type != 0 || value->value != 1 ||
618 REG_SREG & ~reg_flags[value->type])
619 error(ERR_NONFATAL, "invalid segment override");
620 else if (result->prefixes[PPS_SEG])
621 error(ERR_NONFATAL,
622 "instruction has conflicting segment overrides");
623 else {
624 result->prefixes[PPS_SEG] = value->type;
625 if (!(REG_FSGS & ~reg_flags[value->type]))
626 result->oprs[operand].eaflags |= EAF_FSGS;
629 i = stdscan(NULL, &tokval); /* then skip the colon */
630 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
631 process_size_override(result, operand);
632 i = stdscan(NULL, &tokval);
634 value = evaluate(stdscan, NULL, &tokval,
635 &result->oprs[operand].opflags,
636 critical, error, &hints);
637 i = tokval.t_type;
638 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
639 result->forw_ref = true;
641 /* and get the offset */
642 if (!value) { /* but, error in evaluator */
643 result->opcode = -1; /* unrecoverable parse error: */
644 return result; /* ignore this instruction */
647 if (mref && bracket) { /* find ] at the end */
648 if (i != ']') {
649 error(ERR_NONFATAL, "parser: expecting ]");
650 do { /* error recovery again */
651 i = stdscan(NULL, &tokval);
652 } while (i != 0 && i != ',');
653 } else /* we got the required ] */
654 i = stdscan(NULL, &tokval);
655 } else { /* immediate operand */
656 if (i != 0 && i != ',' && i != ':') {
657 error(ERR_NONFATAL, "comma or end of line expected");
658 do { /* error recovery */
659 i = stdscan(NULL, &tokval);
660 } while (i != 0 && i != ',');
661 } else if (i == ':') {
662 result->oprs[operand].type |= COLON;
666 /* now convert the exprs returned from evaluate() into operand
667 * descriptions... */
669 if (mref) { /* it's a memory reference */
670 expr *e = value;
671 int b, i, s; /* basereg, indexreg, scale */
672 int64_t o; /* offset */
674 b = i = -1, o = s = 0;
675 result->oprs[operand].hintbase = hints.base;
676 result->oprs[operand].hinttype = hints.type;
678 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
679 if (e->value == 1) /* in fact it can be basereg */
680 b = e->type;
681 else /* no, it has to be indexreg */
682 i = e->type, s = e->value;
683 e++;
685 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
686 if (b != -1) /* If the first was the base, ... */
687 i = e->type, s = e->value; /* second has to be indexreg */
689 else if (e->value != 1) { /* If both want to be index */
690 error(ERR_NONFATAL,
691 "beroset-p-592-invalid effective address");
692 result->opcode = -1;
693 return result;
694 } else
695 b = e->type;
696 e++;
698 if (e->type != 0) { /* is there an offset? */
699 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
700 error(ERR_NONFATAL,
701 "beroset-p-603-invalid effective address");
702 result->opcode = -1;
703 return result;
704 } else {
705 if (e->type == EXPR_UNKNOWN) {
706 o = 0; /* doesn't matter what */
707 result->oprs[operand].wrt = NO_SEG; /* nor this */
708 result->oprs[operand].segment = NO_SEG; /* or this */
709 while (e->type)
710 e++; /* go to the end of the line */
711 } else {
712 if (e->type == EXPR_SIMPLE) {
713 o = e->value;
714 e++;
716 if (e->type == EXPR_WRT) {
717 result->oprs[operand].wrt = e->value;
718 e++;
719 } else
720 result->oprs[operand].wrt = NO_SEG;
722 * Look for a segment base type.
724 if (e->type && e->type < EXPR_SEGBASE) {
725 error(ERR_NONFATAL,
726 "beroset-p-630-invalid effective address");
727 result->opcode = -1;
728 return result;
730 while (e->type && e->value == 0)
731 e++;
732 if (e->type && e->value != 1) {
733 error(ERR_NONFATAL,
734 "beroset-p-637-invalid effective address");
735 result->opcode = -1;
736 return result;
738 if (e->type) {
739 result->oprs[operand].segment =
740 e->type - EXPR_SEGBASE;
741 e++;
742 } else
743 result->oprs[operand].segment = NO_SEG;
744 while (e->type && e->value == 0)
745 e++;
746 if (e->type) {
747 error(ERR_NONFATAL,
748 "beroset-p-650-invalid effective address");
749 result->opcode = -1;
750 return result;
754 } else {
755 o = 0;
756 result->oprs[operand].wrt = NO_SEG;
757 result->oprs[operand].segment = NO_SEG;
760 if (e->type != 0) { /* there'd better be nothing left! */
761 error(ERR_NONFATAL,
762 "beroset-p-663-invalid effective address");
763 result->opcode = -1;
764 return result;
767 /* It is memory, but it can match any r/m operand */
768 result->oprs[operand].type |= MEMORY_ANY;
770 if (b == -1 && (i == -1 || s == 0)) {
771 int is_rel = globalbits == 64 &&
772 !(result->oprs[operand].eaflags & EAF_ABS) &&
773 ((globalrel &&
774 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
775 (result->oprs[operand].eaflags & EAF_REL));
777 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
779 result->oprs[operand].basereg = b;
780 result->oprs[operand].indexreg = i;
781 result->oprs[operand].scale = s;
782 result->oprs[operand].offset = o;
783 } else { /* it's not a memory reference */
785 if (is_just_unknown(value)) { /* it's immediate but unknown */
786 result->oprs[operand].type |= IMMEDIATE;
787 result->oprs[operand].offset = 0; /* don't care */
788 result->oprs[operand].segment = NO_SEG; /* don't care again */
789 result->oprs[operand].wrt = NO_SEG; /* still don't care */
790 } else if (is_reloc(value)) { /* it's immediate */
791 result->oprs[operand].type |= IMMEDIATE;
792 result->oprs[operand].offset = reloc_value(value);
793 result->oprs[operand].segment = reloc_seg(value);
794 result->oprs[operand].wrt = reloc_wrt(value);
795 if (is_simple(value)) {
796 if (reloc_value(value) == 1)
797 result->oprs[operand].type |= UNITY;
798 if (optimizing >= 0 &&
799 !(result->oprs[operand].type & STRICT)) {
800 int64_t v64 = reloc_value(value);
801 int32_t v32 = (int32_t)v64;
802 int16_t v16 = (int16_t)v32;
804 if (v64 >= -128 && v64 <= 127)
805 result->oprs[operand].type |= SBYTE64;
806 if (v32 >= -128 && v32 <= 127)
807 result->oprs[operand].type |= SBYTE32;
808 if (v16 >= -128 && v16 <= 127)
809 result->oprs[operand].type |= SBYTE16;
812 } else { /* it's a register */
813 unsigned int rs;
815 if (value->type >= EXPR_SIMPLE || value->value != 1) {
816 error(ERR_NONFATAL, "invalid operand type");
817 result->opcode = -1;
818 return result;
822 * check that its only 1 register, not an expression...
824 for (i = 1; value[i].type; i++)
825 if (value[i].value) {
826 error(ERR_NONFATAL, "invalid operand type");
827 result->opcode = -1;
828 return result;
831 /* clear overrides, except TO which applies to FPU regs */
832 if (result->oprs[operand].type & ~TO) {
834 * we want to produce a warning iff the specified size
835 * is different from the register size
837 rs = result->oprs[operand].type & SIZE_MASK;
838 } else
839 rs = 0;
841 result->oprs[operand].type &= TO;
842 result->oprs[operand].type |= REGISTER;
843 result->oprs[operand].type |= reg_flags[value->type];
844 result->oprs[operand].basereg = value->type;
846 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
847 error(ERR_WARNING | ERR_PASS1,
848 "register size specification ignored");
853 result->operands = operand; /* set operand count */
855 /* clear remaining operands */
856 while (operand < MAX_OPERANDS)
857 result->oprs[operand++].type = 0;
860 * Transform RESW, RESD, RESQ, REST, RESO into RESB.
862 switch (result->opcode) {
863 case I_RESW:
864 result->opcode = I_RESB;
865 result->oprs[0].offset *= 2;
866 break;
867 case I_RESD:
868 result->opcode = I_RESB;
869 result->oprs[0].offset *= 4;
870 break;
871 case I_RESQ:
872 result->opcode = I_RESB;
873 result->oprs[0].offset *= 8;
874 break;
875 case I_REST:
876 result->opcode = I_RESB;
877 result->oprs[0].offset *= 10;
878 break;
879 case I_RESO:
880 result->opcode = I_RESB;
881 result->oprs[0].offset *= 16;
882 break;
883 default:
884 break;
887 return result;
890 static int is_comma_next(void)
892 char *p;
893 int i;
894 struct tokenval tv;
896 p = stdscan_bufptr;
897 i = stdscan(NULL, &tv);
898 stdscan_bufptr = p;
899 return (i == ',' || i == ';' || !i);
902 void cleanup_insn(insn * i)
904 extop *e;
906 while (i->eops) {
907 e = i->eops;
908 i->eops = i->eops->next;
909 nasm_free(e);