Missing % in %ifmacro
[nasm/avx512.git] / parser.c
blob72cb120712eff5dccbb0825b759be42683058770
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"
26 #include "tables.h"
28 extern int in_abs_seg; /* ABSOLUTE segment flag */
29 extern int32_t abs_seg; /* ABSOLUTE segment */
30 extern int32_t abs_offset; /* ABSOLUTE segment offset */
32 static int is_comma_next(void);
34 static int i;
35 static struct tokenval tokval;
36 static efunc error;
37 static struct ofmt *outfmt; /* Structure of addresses of output routines */
38 static struct location *location; /* Pointer to current line's segment,offset */
40 void parser_global_info(struct ofmt *output, struct location * locp)
42 outfmt = output;
43 location = locp;
46 static int prefix_slot(enum prefixes prefix)
48 switch (prefix) {
49 case R_CS:
50 case R_DS:
51 case R_SS:
52 case R_ES:
53 case R_FS:
54 case R_GS:
55 return PPS_SEG;
56 case P_LOCK:
57 case P_REP:
58 case P_REPE:
59 case P_REPZ:
60 case P_REPNE:
61 case P_REPNZ:
62 return PPS_LREP;
63 case P_O16:
64 case P_O32:
65 case P_O64:
66 case P_OSP:
67 return PPS_OSIZE;
68 case P_A16:
69 case P_A32:
70 case P_A64:
71 case P_ASP:
72 return PPS_ASIZE;
73 default:
74 error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
75 return -1;
79 static void process_size_override(insn * result, int operand)
81 if (tasm_compatible_mode) {
82 switch ((int)tokval.t_integer) {
83 /* For TASM compatibility a size override inside the
84 * brackets changes the size of the operand, not the
85 * address type of the operand as it does in standard
86 * NASM syntax. Hence:
88 * mov eax,[DWORD val]
90 * is valid syntax in TASM compatibility mode. Note that
91 * you lose the ability to override the default address
92 * type for the instruction, but we never use anything
93 * but 32-bit flat model addressing in our code.
95 case S_BYTE:
96 result->oprs[operand].type |= BITS8;
97 break;
98 case S_WORD:
99 result->oprs[operand].type |= BITS16;
100 break;
101 case S_DWORD:
102 case S_LONG:
103 result->oprs[operand].type |= BITS32;
104 break;
105 case S_QWORD:
106 result->oprs[operand].type |= BITS64;
107 break;
108 case S_TWORD:
109 result->oprs[operand].type |= BITS80;
110 break;
111 case S_OWORD:
112 result->oprs[operand].type |= BITS128;
113 break;
114 default:
115 error(ERR_NONFATAL,
116 "invalid operand size specification");
117 break;
119 } else {
120 /* Standard NASM compatible syntax */
121 switch ((int)tokval.t_integer) {
122 case S_NOSPLIT:
123 result->oprs[operand].eaflags |= EAF_TIMESTWO;
124 break;
125 case S_REL:
126 result->oprs[operand].eaflags |= EAF_REL;
127 break;
128 case S_ABS:
129 result->oprs[operand].eaflags |= EAF_ABS;
130 break;
131 case S_BYTE:
132 result->oprs[operand].disp_size = 8;
133 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
134 break;
135 case P_A16:
136 case P_A32:
137 case P_A64:
138 if (result->prefixes[PPS_ASIZE] &&
139 result->prefixes[PPS_ASIZE] != tokval.t_integer)
140 error(ERR_NONFATAL,
141 "conflicting address size specifications");
142 else
143 result->prefixes[PPS_ASIZE] = tokval.t_integer;
144 break;
145 case S_WORD:
146 result->oprs[operand].disp_size = 16;
147 result->oprs[operand].eaflags |= EAF_WORDOFFS;
148 break;
149 case S_DWORD:
150 case S_LONG:
151 result->oprs[operand].disp_size = 32;
152 result->oprs[operand].eaflags |= EAF_WORDOFFS;
153 break;
154 case S_QWORD:
155 result->oprs[operand].disp_size = 64;
156 result->oprs[operand].eaflags |= EAF_WORDOFFS;
157 break;
158 default:
159 error(ERR_NONFATAL, "invalid size specification in"
160 " effective address");
161 break;
166 insn *parse_line(int pass, char *buffer, insn * result,
167 efunc errfunc, evalfunc evaluate, ldfunc ldef)
169 int operand;
170 int critical;
171 struct eval_hints hints;
172 int j;
173 bool first;
174 bool insn_is_label = false;
176 restart_parse:
177 first = true;
178 result->forw_ref = false;
179 error = errfunc;
181 stdscan_reset();
182 stdscan_bufptr = buffer;
183 i = stdscan(NULL, &tokval);
185 result->label = NULL; /* Assume no label */
186 result->eops = NULL; /* must do this, whatever happens */
187 result->operands = 0; /* must initialize this */
189 if (i == 0) { /* blank line - ignore */
190 result->opcode = -1; /* and no instruction either */
191 return result;
193 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
194 (i != TOKEN_REG || (REG_SREG & ~nasm_reg_flags[tokval.t_integer]))) {
195 error(ERR_NONFATAL, "label or instruction expected"
196 " at start of line");
197 result->opcode = -1;
198 return result;
201 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
202 /* there's a label here */
203 first = false;
204 result->label = tokval.t_charptr;
205 i = stdscan(NULL, &tokval);
206 if (i == ':') { /* skip over the optional colon */
207 i = stdscan(NULL, &tokval);
208 } else if (i == 0) {
209 error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
210 "label alone on a line without a colon might be in error");
212 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
214 * FIXME: location->segment could be NO_SEG, in which case
215 * it is possible we should be passing 'abs_seg'. Look into this.
216 * Work out whether that is *really* what we should be doing.
217 * Generally fix things. I think this is right as it is, but
218 * am still not certain.
220 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
221 location->offset, NULL, true, false, outfmt, errfunc);
225 if (i == 0) {
226 result->opcode = -1; /* this line contains just a label */
227 return result;
230 for (j = 0; j < MAXPREFIX; j++)
231 result->prefixes[j] = P_none;
232 result->times = 1L;
234 while (i == TOKEN_PREFIX ||
235 (i == TOKEN_REG && !(REG_SREG & ~nasm_reg_flags[tokval.t_integer])))
237 first = false;
240 * Handle special case: the TIMES prefix.
242 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
243 expr *value;
245 i = stdscan(NULL, &tokval);
246 value =
247 evaluate(stdscan, NULL, &tokval, NULL, pass0, error, NULL);
248 i = tokval.t_type;
249 if (!value) { /* but, error in evaluator */
250 result->opcode = -1; /* unrecoverable parse error: */
251 return result; /* ignore this instruction */
253 if (!is_simple(value)) {
254 error(ERR_NONFATAL,
255 "non-constant argument supplied to TIMES");
256 result->times = 1L;
257 } else {
258 result->times = value->value;
259 if (value->value < 0) {
260 error(ERR_NONFATAL, "TIMES value %d is negative",
261 value->value);
262 result->times = 0;
265 } else {
266 int slot = prefix_slot(tokval.t_integer);
267 if (result->prefixes[slot]) {
268 if (result->prefixes[slot] == tokval.t_integer)
269 error(ERR_WARNING,
270 "instruction has redundant prefixes");
271 else
272 error(ERR_NONFATAL,
273 "instruction has conflicting prefixes");
275 result->prefixes[slot] = tokval.t_integer;
276 i = stdscan(NULL, &tokval);
280 if (i != TOKEN_INSN) {
281 int j;
282 enum prefixes pfx;
284 for (j = 0; j < MAXPREFIX; j++)
285 if ((pfx = result->prefixes[j]) != P_none)
286 break;
288 if (i == 0 && pfx != P_none) {
290 * Instruction prefixes are present, but no actual
291 * instruction. This is allowed: at this point we
292 * invent a notional instruction of RESB 0.
294 result->opcode = I_RESB;
295 result->operands = 1;
296 result->oprs[0].type = IMMEDIATE;
297 result->oprs[0].offset = 0L;
298 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
299 return result;
300 } else {
301 error(ERR_NONFATAL, "parser: instruction expected");
302 result->opcode = -1;
303 return result;
307 result->opcode = tokval.t_integer;
308 result->condition = tokval.t_inttwo;
311 * RESB, RESW and RESD cannot be satisfied with incorrectly
312 * evaluated operands, since the correct values _must_ be known
313 * on the first pass. Hence, even in pass one, we set the
314 * `critical' flag on calling evaluate(), so that it will bomb
315 * out on undefined symbols. Nasty, but there's nothing we can
316 * do about it.
318 * For the moment, EQU has the same difficulty, so we'll
319 * include that.
321 if (result->opcode == I_RESB || result->opcode == I_RESW ||
322 result->opcode == I_RESD || result->opcode == I_RESQ ||
323 result->opcode == I_REST || result->opcode == I_RESO ||
324 result->opcode == I_RESY ||
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_DY || 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 case I_DY:
405 error(ERR_NONFATAL, "floating-point constant"
406 " encountered in DY instruction");
407 eop->stringlen = 0;
408 break;
409 default:
410 error(ERR_NONFATAL, "floating-point constant"
411 " encountered in unknown instruction");
413 * fix suggested by Pedro Gimeno... original line
414 * was:
415 * eop->type = EOT_NOTHING;
417 eop->stringlen = 0;
418 break;
420 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
421 tail = &eop->next;
422 *fixptr = eop;
423 eop->stringval = (char *)eop + sizeof(extop);
424 if (!eop->stringlen ||
425 !float_const(tokval.t_charptr, sign,
426 (uint8_t *)eop->stringval,
427 eop->stringlen, error))
428 eop->type = EOT_NOTHING;
429 i = stdscan(NULL, &tokval); /* eat the comma */
430 continue;
434 /* anything else */
436 expr *value;
437 value = evaluate(stdscan, NULL, &tokval, NULL,
438 critical, error, NULL);
439 i = tokval.t_type;
440 if (!value) { /* error in evaluator */
441 result->opcode = -1; /* unrecoverable parse error: */
442 return result; /* ignore this instruction */
444 if (is_unknown(value)) {
445 eop->type = EOT_DB_NUMBER;
446 eop->offset = 0; /* doesn't matter what we put */
447 eop->segment = eop->wrt = NO_SEG; /* likewise */
448 } else if (is_reloc(value)) {
449 eop->type = EOT_DB_NUMBER;
450 eop->offset = reloc_value(value);
451 eop->segment = reloc_seg(value);
452 eop->wrt = reloc_wrt(value);
453 } else {
454 error(ERR_NONFATAL,
455 "operand %d: expression is not simple"
456 " or relocatable", oper_num);
461 * We're about to call stdscan(), which will eat the
462 * comma that we're currently sitting on between
463 * arguments. However, we'd better check first that it
464 * _is_ a comma.
466 if (i == 0) /* also could be EOL */
467 break;
468 if (i != ',') {
469 error(ERR_NONFATAL, "comma expected after operand %d",
470 oper_num);
471 result->opcode = -1; /* unrecoverable parse error: */
472 return result; /* ignore this instruction */
476 if (result->opcode == I_INCBIN) {
478 * Correct syntax for INCBIN is that there should be
479 * one string operand, followed by one or two numeric
480 * operands.
482 if (!result->eops || result->eops->type != EOT_DB_STRING)
483 error(ERR_NONFATAL, "`incbin' expects a file name");
484 else if (result->eops->next &&
485 result->eops->next->type != EOT_DB_NUMBER)
486 error(ERR_NONFATAL, "`incbin': second parameter is",
487 " non-numeric");
488 else if (result->eops->next && result->eops->next->next &&
489 result->eops->next->next->type != EOT_DB_NUMBER)
490 error(ERR_NONFATAL, "`incbin': third parameter is",
491 " non-numeric");
492 else if (result->eops->next && result->eops->next->next &&
493 result->eops->next->next->next)
494 error(ERR_NONFATAL,
495 "`incbin': more than three parameters");
496 else
497 return result;
499 * If we reach here, one of the above errors happened.
500 * Throw the instruction away.
502 result->opcode = -1;
503 return result;
504 } else /* DB ... */ if (oper_num == 0)
505 error(ERR_WARNING | ERR_PASS1,
506 "no operand for data declaration");
507 else
508 result->operands = oper_num;
510 return result;
513 /* right. Now we begin to parse the operands. There may be up to four
514 * of these, separated by commas, and terminated by a zero token. */
516 for (operand = 0; operand < MAX_OPERANDS; operand++) {
517 expr *value; /* used most of the time */
518 int mref; /* is this going to be a memory ref? */
519 int bracket; /* is it a [] mref, or a & mref? */
520 int setsize = 0;
522 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
523 result->oprs[operand].eaflags = 0; /* and this */
524 result->oprs[operand].opflags = 0;
526 i = stdscan(NULL, &tokval);
527 if (i == 0)
528 break; /* end of operands: get out of here */
529 else if (first && i == ':') {
530 insn_is_label = true;
531 goto restart_parse;
533 first = false;
534 result->oprs[operand].type = 0; /* so far, no override */
535 while (i == TOKEN_SPECIAL) { /* size specifiers */
536 switch ((int)tokval.t_integer) {
537 case S_BYTE:
538 if (!setsize) /* we want to use only the first */
539 result->oprs[operand].type |= BITS8;
540 setsize = 1;
541 break;
542 case S_WORD:
543 if (!setsize)
544 result->oprs[operand].type |= BITS16;
545 setsize = 1;
546 break;
547 case S_DWORD:
548 case S_LONG:
549 if (!setsize)
550 result->oprs[operand].type |= BITS32;
551 setsize = 1;
552 break;
553 case S_QWORD:
554 if (!setsize)
555 result->oprs[operand].type |= BITS64;
556 setsize = 1;
557 break;
558 case S_TWORD:
559 if (!setsize)
560 result->oprs[operand].type |= BITS80;
561 setsize = 1;
562 break;
563 case S_OWORD:
564 if (!setsize)
565 result->oprs[operand].type |= BITS128;
566 setsize = 1;
567 break;
568 case S_YWORD:
569 if (!setsize)
570 result->oprs[operand].type |= BITS256;
571 setsize = 1;
572 break;
573 case S_TO:
574 result->oprs[operand].type |= TO;
575 break;
576 case S_STRICT:
577 result->oprs[operand].type |= STRICT;
578 break;
579 case S_FAR:
580 result->oprs[operand].type |= FAR;
581 break;
582 case S_NEAR:
583 result->oprs[operand].type |= NEAR;
584 break;
585 case S_SHORT:
586 result->oprs[operand].type |= SHORT;
587 break;
588 default:
589 error(ERR_NONFATAL, "invalid operand size specification");
591 i = stdscan(NULL, &tokval);
594 if (i == '[' || i == '&') { /* memory reference */
595 mref = true;
596 bracket = (i == '[');
597 i = stdscan(NULL, &tokval); /* then skip the colon */
598 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
599 process_size_override(result, operand);
600 i = stdscan(NULL, &tokval);
602 } else { /* immediate operand, or register */
603 mref = false;
604 bracket = false; /* placate optimisers */
607 if ((result->oprs[operand].type & FAR) && !mref &&
608 result->opcode != I_JMP && result->opcode != I_CALL) {
609 error(ERR_NONFATAL, "invalid use of FAR operand specifier");
612 value = evaluate(stdscan, NULL, &tokval,
613 &result->oprs[operand].opflags,
614 critical, error, &hints);
615 i = tokval.t_type;
616 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
617 result->forw_ref = true;
619 if (!value) { /* error in evaluator */
620 result->opcode = -1; /* unrecoverable parse error: */
621 return result; /* ignore this instruction */
623 if (i == ':' && mref) { /* it was seg:offset */
625 * Process the segment override.
627 if (value[1].type != 0 || value->value != 1 ||
628 REG_SREG & ~nasm_reg_flags[value->type])
629 error(ERR_NONFATAL, "invalid segment override");
630 else if (result->prefixes[PPS_SEG])
631 error(ERR_NONFATAL,
632 "instruction has conflicting segment overrides");
633 else {
634 result->prefixes[PPS_SEG] = value->type;
635 if (!(REG_FSGS & ~nasm_reg_flags[value->type]))
636 result->oprs[operand].eaflags |= EAF_FSGS;
639 i = stdscan(NULL, &tokval); /* then skip the colon */
640 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
641 process_size_override(result, operand);
642 i = stdscan(NULL, &tokval);
644 value = evaluate(stdscan, NULL, &tokval,
645 &result->oprs[operand].opflags,
646 critical, error, &hints);
647 i = tokval.t_type;
648 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
649 result->forw_ref = true;
651 /* and get the offset */
652 if (!value) { /* but, error in evaluator */
653 result->opcode = -1; /* unrecoverable parse error: */
654 return result; /* ignore this instruction */
657 if (mref && bracket) { /* find ] at the end */
658 if (i != ']') {
659 error(ERR_NONFATAL, "parser: expecting ]");
660 do { /* error recovery again */
661 i = stdscan(NULL, &tokval);
662 } while (i != 0 && i != ',');
663 } else /* we got the required ] */
664 i = stdscan(NULL, &tokval);
665 } else { /* immediate operand */
666 if (i != 0 && i != ',' && i != ':') {
667 error(ERR_NONFATAL, "comma or end of line expected");
668 do { /* error recovery */
669 i = stdscan(NULL, &tokval);
670 } while (i != 0 && i != ',');
671 } else if (i == ':') {
672 result->oprs[operand].type |= COLON;
676 /* now convert the exprs returned from evaluate() into operand
677 * descriptions... */
679 if (mref) { /* it's a memory reference */
680 expr *e = value;
681 int b, i, s; /* basereg, indexreg, scale */
682 int64_t o; /* offset */
684 b = i = -1, o = s = 0;
685 result->oprs[operand].hintbase = hints.base;
686 result->oprs[operand].hinttype = hints.type;
688 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
689 if (e->value == 1) /* in fact it can be basereg */
690 b = e->type;
691 else /* no, it has to be indexreg */
692 i = e->type, s = e->value;
693 e++;
695 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
696 if (b != -1) /* If the first was the base, ... */
697 i = e->type, s = e->value; /* second has to be indexreg */
699 else if (e->value != 1) { /* If both want to be index */
700 error(ERR_NONFATAL,
701 "beroset-p-592-invalid effective address");
702 result->opcode = -1;
703 return result;
704 } else
705 b = e->type;
706 e++;
708 if (e->type != 0) { /* is there an offset? */
709 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
710 error(ERR_NONFATAL,
711 "beroset-p-603-invalid effective address");
712 result->opcode = -1;
713 return result;
714 } else {
715 if (e->type == EXPR_UNKNOWN) {
716 o = 0; /* doesn't matter what */
717 result->oprs[operand].wrt = NO_SEG; /* nor this */
718 result->oprs[operand].segment = NO_SEG; /* or this */
719 while (e->type)
720 e++; /* go to the end of the line */
721 } else {
722 if (e->type == EXPR_SIMPLE) {
723 o = e->value;
724 e++;
726 if (e->type == EXPR_WRT) {
727 result->oprs[operand].wrt = e->value;
728 e++;
729 } else
730 result->oprs[operand].wrt = NO_SEG;
732 * Look for a segment base type.
734 if (e->type && e->type < EXPR_SEGBASE) {
735 error(ERR_NONFATAL,
736 "beroset-p-630-invalid effective address");
737 result->opcode = -1;
738 return result;
740 while (e->type && e->value == 0)
741 e++;
742 if (e->type && e->value != 1) {
743 error(ERR_NONFATAL,
744 "beroset-p-637-invalid effective address");
745 result->opcode = -1;
746 return result;
748 if (e->type) {
749 result->oprs[operand].segment =
750 e->type - EXPR_SEGBASE;
751 e++;
752 } else
753 result->oprs[operand].segment = NO_SEG;
754 while (e->type && e->value == 0)
755 e++;
756 if (e->type) {
757 error(ERR_NONFATAL,
758 "beroset-p-650-invalid effective address");
759 result->opcode = -1;
760 return result;
764 } else {
765 o = 0;
766 result->oprs[operand].wrt = NO_SEG;
767 result->oprs[operand].segment = NO_SEG;
770 if (e->type != 0) { /* there'd better be nothing left! */
771 error(ERR_NONFATAL,
772 "beroset-p-663-invalid effective address");
773 result->opcode = -1;
774 return result;
777 /* It is memory, but it can match any r/m operand */
778 result->oprs[operand].type |= MEMORY_ANY;
780 if (b == -1 && (i == -1 || s == 0)) {
781 int is_rel = globalbits == 64 &&
782 !(result->oprs[operand].eaflags & EAF_ABS) &&
783 ((globalrel &&
784 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
785 (result->oprs[operand].eaflags & EAF_REL));
787 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
789 result->oprs[operand].basereg = b;
790 result->oprs[operand].indexreg = i;
791 result->oprs[operand].scale = s;
792 result->oprs[operand].offset = o;
793 } else { /* it's not a memory reference */
795 if (is_just_unknown(value)) { /* it's immediate but unknown */
796 result->oprs[operand].type |= IMMEDIATE;
797 result->oprs[operand].offset = 0; /* don't care */
798 result->oprs[operand].segment = NO_SEG; /* don't care again */
799 result->oprs[operand].wrt = NO_SEG; /* still don't care */
800 } else if (is_reloc(value)) { /* it's immediate */
801 result->oprs[operand].type |= IMMEDIATE;
802 result->oprs[operand].offset = reloc_value(value);
803 result->oprs[operand].segment = reloc_seg(value);
804 result->oprs[operand].wrt = reloc_wrt(value);
805 if (is_simple(value)) {
806 if (reloc_value(value) == 1)
807 result->oprs[operand].type |= UNITY;
808 if (optimizing >= 0 &&
809 !(result->oprs[operand].type & STRICT)) {
810 int64_t v64 = reloc_value(value);
811 int32_t v32 = (int32_t)v64;
812 int16_t v16 = (int16_t)v32;
814 if (v64 >= -128 && v64 <= 127)
815 result->oprs[operand].type |= SBYTE64;
816 if (v32 >= -128 && v32 <= 127)
817 result->oprs[operand].type |= SBYTE32;
818 if (v16 >= -128 && v16 <= 127)
819 result->oprs[operand].type |= SBYTE16;
822 } else { /* it's a register */
823 unsigned int rs;
825 if (value->type >= EXPR_SIMPLE || value->value != 1) {
826 error(ERR_NONFATAL, "invalid operand type");
827 result->opcode = -1;
828 return result;
832 * check that its only 1 register, not an expression...
834 for (i = 1; value[i].type; i++)
835 if (value[i].value) {
836 error(ERR_NONFATAL, "invalid operand type");
837 result->opcode = -1;
838 return result;
841 /* clear overrides, except TO which applies to FPU regs */
842 if (result->oprs[operand].type & ~TO) {
844 * we want to produce a warning iff the specified size
845 * is different from the register size
847 rs = result->oprs[operand].type & SIZE_MASK;
848 } else
849 rs = 0;
851 result->oprs[operand].type &= TO;
852 result->oprs[operand].type |= REGISTER;
853 result->oprs[operand].type |= nasm_reg_flags[value->type];
854 result->oprs[operand].basereg = value->type;
856 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
857 error(ERR_WARNING | ERR_PASS1,
858 "register size specification ignored");
863 result->operands = operand; /* set operand count */
865 /* clear remaining operands */
866 while (operand < MAX_OPERANDS)
867 result->oprs[operand++].type = 0;
870 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
872 switch (result->opcode) {
873 case I_RESW:
874 result->opcode = I_RESB;
875 result->oprs[0].offset *= 2;
876 break;
877 case I_RESD:
878 result->opcode = I_RESB;
879 result->oprs[0].offset *= 4;
880 break;
881 case I_RESQ:
882 result->opcode = I_RESB;
883 result->oprs[0].offset *= 8;
884 break;
885 case I_REST:
886 result->opcode = I_RESB;
887 result->oprs[0].offset *= 10;
888 break;
889 case I_RESO:
890 result->opcode = I_RESB;
891 result->oprs[0].offset *= 16;
892 break;
893 case I_RESY:
894 result->opcode = I_RESB;
895 result->oprs[0].offset *= 32;
896 break;
897 default:
898 break;
901 return result;
904 static int is_comma_next(void)
906 char *p;
907 int i;
908 struct tokenval tv;
910 p = stdscan_bufptr;
911 i = stdscan(NULL, &tv);
912 stdscan_bufptr = p;
913 return (i == ',' || i == ';' || !i);
916 void cleanup_insn(insn * i)
918 extop *e;
920 while (i->eops) {
921 e = i->eops;
922 i->eops = i->eops->next;
923 nasm_free(e);