NASM 2.06rc10
[nasm/avx512.git] / parser.c
blob0f64043094fbf0c66eaf95c86c4c962265d16d77
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 P_WAIT:
50 return PPS_WAIT;
51 case R_CS:
52 case R_DS:
53 case R_SS:
54 case R_ES:
55 case R_FS:
56 case R_GS:
57 return PPS_SEG;
58 case P_LOCK:
59 case P_REP:
60 case P_REPE:
61 case P_REPZ:
62 case P_REPNE:
63 case P_REPNZ:
64 return PPS_LREP;
65 case P_O16:
66 case P_O32:
67 case P_O64:
68 case P_OSP:
69 return PPS_OSIZE;
70 case P_A16:
71 case P_A32:
72 case P_A64:
73 case P_ASP:
74 return PPS_ASIZE;
75 default:
76 error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
77 return -1;
81 static void process_size_override(insn * result, int operand)
83 if (tasm_compatible_mode) {
84 switch ((int)tokval.t_integer) {
85 /* For TASM compatibility a size override inside the
86 * brackets changes the size of the operand, not the
87 * address type of the operand as it does in standard
88 * NASM syntax. Hence:
90 * mov eax,[DWORD val]
92 * is valid syntax in TASM compatibility mode. Note that
93 * you lose the ability to override the default address
94 * type for the instruction, but we never use anything
95 * but 32-bit flat model addressing in our code.
97 case S_BYTE:
98 result->oprs[operand].type |= BITS8;
99 break;
100 case S_WORD:
101 result->oprs[operand].type |= BITS16;
102 break;
103 case S_DWORD:
104 case S_LONG:
105 result->oprs[operand].type |= BITS32;
106 break;
107 case S_QWORD:
108 result->oprs[operand].type |= BITS64;
109 break;
110 case S_TWORD:
111 result->oprs[operand].type |= BITS80;
112 break;
113 case S_OWORD:
114 result->oprs[operand].type |= BITS128;
115 break;
116 default:
117 error(ERR_NONFATAL,
118 "invalid operand size specification");
119 break;
121 } else {
122 /* Standard NASM compatible syntax */
123 switch ((int)tokval.t_integer) {
124 case S_NOSPLIT:
125 result->oprs[operand].eaflags |= EAF_TIMESTWO;
126 break;
127 case S_REL:
128 result->oprs[operand].eaflags |= EAF_REL;
129 break;
130 case S_ABS:
131 result->oprs[operand].eaflags |= EAF_ABS;
132 break;
133 case S_BYTE:
134 result->oprs[operand].disp_size = 8;
135 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
136 break;
137 case P_A16:
138 case P_A32:
139 case P_A64:
140 if (result->prefixes[PPS_ASIZE] &&
141 result->prefixes[PPS_ASIZE] != tokval.t_integer)
142 error(ERR_NONFATAL,
143 "conflicting address size specifications");
144 else
145 result->prefixes[PPS_ASIZE] = tokval.t_integer;
146 break;
147 case S_WORD:
148 result->oprs[operand].disp_size = 16;
149 result->oprs[operand].eaflags |= EAF_WORDOFFS;
150 break;
151 case S_DWORD:
152 case S_LONG:
153 result->oprs[operand].disp_size = 32;
154 result->oprs[operand].eaflags |= EAF_WORDOFFS;
155 break;
156 case S_QWORD:
157 result->oprs[operand].disp_size = 64;
158 result->oprs[operand].eaflags |= EAF_WORDOFFS;
159 break;
160 default:
161 error(ERR_NONFATAL, "invalid size specification in"
162 " effective address");
163 break;
168 insn *parse_line(int pass, char *buffer, insn * result,
169 efunc errfunc, evalfunc evaluate, ldfunc ldef)
171 int operand;
172 int critical;
173 struct eval_hints hints;
174 int j;
175 bool first;
176 bool insn_is_label = false;
178 restart_parse:
179 first = true;
180 result->forw_ref = false;
181 error = errfunc;
183 stdscan_reset();
184 stdscan_bufptr = buffer;
185 i = stdscan(NULL, &tokval);
187 result->label = NULL; /* Assume no label */
188 result->eops = NULL; /* must do this, whatever happens */
189 result->operands = 0; /* must initialize this */
191 if (i == 0) { /* blank line - ignore */
192 result->opcode = -1; /* and no instruction either */
193 return result;
195 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
196 (i != TOKEN_REG || (REG_SREG & ~nasm_reg_flags[tokval.t_integer]))) {
197 error(ERR_NONFATAL, "label or instruction expected"
198 " at start of line");
199 result->opcode = -1;
200 return result;
203 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
204 /* there's a label here */
205 first = false;
206 result->label = tokval.t_charptr;
207 i = stdscan(NULL, &tokval);
208 if (i == ':') { /* skip over the optional colon */
209 i = stdscan(NULL, &tokval);
210 } else if (i == 0) {
211 error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
212 "label alone on a line without a colon might be in error");
214 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
216 * FIXME: location->segment could be NO_SEG, in which case
217 * it is possible we should be passing 'abs_seg'. Look into this.
218 * Work out whether that is *really* what we should be doing.
219 * Generally fix things. I think this is right as it is, but
220 * am still not certain.
222 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
223 location->offset, NULL, true, false, outfmt, errfunc);
227 if (i == 0) {
228 result->opcode = -1; /* this line contains just a label */
229 return result;
232 for (j = 0; j < MAXPREFIX; j++)
233 result->prefixes[j] = P_none;
234 result->times = 1L;
236 while (i == TOKEN_PREFIX ||
237 (i == TOKEN_REG && !(REG_SREG & ~nasm_reg_flags[tokval.t_integer])))
239 first = false;
242 * Handle special case: the TIMES prefix.
244 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
245 expr *value;
247 i = stdscan(NULL, &tokval);
248 value =
249 evaluate(stdscan, NULL, &tokval, NULL, pass0, error, NULL);
250 i = tokval.t_type;
251 if (!value) { /* but, error in evaluator */
252 result->opcode = -1; /* unrecoverable parse error: */
253 return result; /* ignore this instruction */
255 if (!is_simple(value)) {
256 error(ERR_NONFATAL,
257 "non-constant argument supplied to TIMES");
258 result->times = 1L;
259 } else {
260 result->times = value->value;
261 if (value->value < 0 && pass0 == 2) {
262 error(ERR_NONFATAL, "TIMES value %d is negative",
263 value->value);
264 result->times = 0;
267 } else {
268 int slot = prefix_slot(tokval.t_integer);
269 if (result->prefixes[slot]) {
270 if (result->prefixes[slot] == tokval.t_integer)
271 error(ERR_WARNING,
272 "instruction has redundant prefixes");
273 else
274 error(ERR_NONFATAL,
275 "instruction has conflicting prefixes");
277 result->prefixes[slot] = tokval.t_integer;
278 i = stdscan(NULL, &tokval);
282 if (i != TOKEN_INSN) {
283 int j;
284 enum prefixes pfx;
286 for (j = 0; j < MAXPREFIX; j++)
287 if ((pfx = result->prefixes[j]) != P_none)
288 break;
290 if (i == 0 && pfx != P_none) {
292 * Instruction prefixes are present, but no actual
293 * instruction. This is allowed: at this point we
294 * invent a notional instruction of RESB 0.
296 result->opcode = I_RESB;
297 result->operands = 1;
298 result->oprs[0].type = IMMEDIATE;
299 result->oprs[0].offset = 0L;
300 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
301 return result;
302 } else {
303 error(ERR_NONFATAL, "parser: instruction expected");
304 result->opcode = -1;
305 return result;
309 result->opcode = tokval.t_integer;
310 result->condition = tokval.t_inttwo;
313 * INCBIN cannot be satisfied with incorrectly
314 * evaluated operands, since the correct values _must_ be known
315 * on the first pass. Hence, even in pass one, we set the
316 * `critical' flag on calling evaluate(), so that it will bomb
317 * out on undefined symbols.
319 if (result->opcode == I_INCBIN) {
320 critical = (pass0 < 2 ? 1 : 2);
322 } else
323 critical = (pass == 2 ? 2 : 0);
325 if (result->opcode == I_DB || result->opcode == I_DW ||
326 result->opcode == I_DD || result->opcode == I_DQ ||
327 result->opcode == I_DT || result->opcode == I_DO ||
328 result->opcode == I_DY || result->opcode == I_INCBIN) {
329 extop *eop, **tail = &result->eops, **fixptr;
330 int oper_num = 0;
331 int32_t sign;
333 result->eops_float = false;
336 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
338 while (1) {
339 i = stdscan(NULL, &tokval);
340 if (i == 0)
341 break;
342 else if (first && i == ':') {
343 insn_is_label = true;
344 goto restart_parse;
346 first = false;
347 fixptr = tail;
348 eop = *tail = nasm_malloc(sizeof(extop));
349 tail = &eop->next;
350 eop->next = NULL;
351 eop->type = EOT_NOTHING;
352 oper_num++;
353 sign = +1;
355 /* is_comma_next() here is to distinguish this from
356 a string used as part of an expression... */
357 if (i == TOKEN_STR && is_comma_next()) {
358 eop->type = EOT_DB_STRING;
359 eop->stringval = tokval.t_charptr;
360 eop->stringlen = tokval.t_inttwo;
361 i = stdscan(NULL, &tokval); /* eat the comma */
362 } else if (i == TOKEN_STRFUNC) {
363 bool parens = false;
364 const char *funcname = tokval.t_charptr;
365 enum strfunc func = tokval.t_integer;
366 i = stdscan(NULL, &tokval);
367 if (i == '(') {
368 parens = true;
369 i = stdscan(NULL, &tokval);
371 if (i != TOKEN_STR) {
372 error(ERR_NONFATAL,
373 "%s must be followed by a string constant",
374 funcname);
375 eop->type = EOT_NOTHING;
376 } else {
377 eop->type = EOT_DB_STRING_FREE;
378 eop->stringlen =
379 string_transform(tokval.t_charptr, tokval.t_inttwo,
380 &eop->stringval, func);
381 if (eop->stringlen == (size_t)-1) {
382 error(ERR_NONFATAL, "invalid string for transform");
383 eop->type = EOT_NOTHING;
386 if (parens && i && i != ')') {
387 i = stdscan(NULL, &tokval);
388 if (i != ')') {
389 error(ERR_NONFATAL, "unterminated %s function",
390 funcname);
393 if (i && i != ',')
394 i = stdscan(NULL, &tokval);
395 } else if (i == '-' || i == '+') {
396 char *save = stdscan_bufptr;
397 int token = i;
398 sign = (i == '-') ? -1 : 1;
399 i = stdscan(NULL, &tokval);
400 if (i != TOKEN_FLOAT) {
401 stdscan_bufptr = save;
402 i = tokval.t_type = token;
403 goto is_expression;
404 } else {
405 goto is_float;
407 } else if (i == TOKEN_FLOAT) {
408 is_float:
409 eop->type = EOT_DB_STRING;
410 result->eops_float = true;
411 switch (result->opcode) {
412 case I_DB:
413 eop->stringlen = 1;
414 break;
415 case I_DW:
416 eop->stringlen = 2;
417 break;
418 case I_DD:
419 eop->stringlen = 4;
420 break;
421 case I_DQ:
422 eop->stringlen = 8;
423 break;
424 case I_DT:
425 eop->stringlen = 10;
426 break;
427 case I_DO:
428 eop->stringlen = 16;
429 break;
430 case I_DY:
431 error(ERR_NONFATAL, "floating-point constant"
432 " encountered in DY instruction");
433 eop->stringlen = 0;
434 break;
435 default:
436 error(ERR_NONFATAL, "floating-point constant"
437 " encountered in unknown instruction");
439 * fix suggested by Pedro Gimeno... original line
440 * was:
441 * eop->type = EOT_NOTHING;
443 eop->stringlen = 0;
444 break;
446 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
447 tail = &eop->next;
448 *fixptr = eop;
449 eop->stringval = (char *)eop + sizeof(extop);
450 if (!eop->stringlen ||
451 !float_const(tokval.t_charptr, sign,
452 (uint8_t *)eop->stringval,
453 eop->stringlen, error))
454 eop->type = EOT_NOTHING;
455 i = stdscan(NULL, &tokval); /* eat the comma */
456 } else {
457 /* anything else, assume it is an expression */
458 expr *value;
460 is_expression:
461 value = evaluate(stdscan, NULL, &tokval, NULL,
462 critical, error, NULL);
463 i = tokval.t_type;
464 if (!value) { /* error in evaluator */
465 result->opcode = -1; /* unrecoverable parse error: */
466 return result; /* ignore this instruction */
468 if (is_unknown(value)) {
469 eop->type = EOT_DB_NUMBER;
470 eop->offset = 0; /* doesn't matter what we put */
471 eop->segment = eop->wrt = NO_SEG; /* likewise */
472 } else if (is_reloc(value)) {
473 eop->type = EOT_DB_NUMBER;
474 eop->offset = reloc_value(value);
475 eop->segment = reloc_seg(value);
476 eop->wrt = reloc_wrt(value);
477 } else {
478 error(ERR_NONFATAL,
479 "operand %d: expression is not simple"
480 " or relocatable", oper_num);
485 * We're about to call stdscan(), which will eat the
486 * comma that we're currently sitting on between
487 * arguments. However, we'd better check first that it
488 * _is_ a comma.
490 if (i == 0) /* also could be EOL */
491 break;
492 if (i != ',') {
493 error(ERR_NONFATAL, "comma expected after operand %d",
494 oper_num);
495 result->opcode = -1; /* unrecoverable parse error: */
496 return result; /* ignore this instruction */
500 if (result->opcode == I_INCBIN) {
502 * Correct syntax for INCBIN is that there should be
503 * one string operand, followed by one or two numeric
504 * operands.
506 if (!result->eops || result->eops->type != EOT_DB_STRING)
507 error(ERR_NONFATAL, "`incbin' expects a file name");
508 else if (result->eops->next &&
509 result->eops->next->type != EOT_DB_NUMBER)
510 error(ERR_NONFATAL, "`incbin': second parameter is",
511 " non-numeric");
512 else if (result->eops->next && result->eops->next->next &&
513 result->eops->next->next->type != EOT_DB_NUMBER)
514 error(ERR_NONFATAL, "`incbin': third parameter is",
515 " non-numeric");
516 else if (result->eops->next && result->eops->next->next &&
517 result->eops->next->next->next)
518 error(ERR_NONFATAL,
519 "`incbin': more than three parameters");
520 else
521 return result;
523 * If we reach here, one of the above errors happened.
524 * Throw the instruction away.
526 result->opcode = -1;
527 return result;
528 } else /* DB ... */ if (oper_num == 0)
529 error(ERR_WARNING | ERR_PASS1,
530 "no operand for data declaration");
531 else
532 result->operands = oper_num;
534 return result;
537 /* right. Now we begin to parse the operands. There may be up to four
538 * of these, separated by commas, and terminated by a zero token. */
540 for (operand = 0; operand < MAX_OPERANDS; operand++) {
541 expr *value; /* used most of the time */
542 int mref; /* is this going to be a memory ref? */
543 int bracket; /* is it a [] mref, or a & mref? */
544 int setsize = 0;
546 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
547 result->oprs[operand].eaflags = 0; /* and this */
548 result->oprs[operand].opflags = 0;
550 i = stdscan(NULL, &tokval);
551 if (i == 0)
552 break; /* end of operands: get out of here */
553 else if (first && i == ':') {
554 insn_is_label = true;
555 goto restart_parse;
557 first = false;
558 result->oprs[operand].type = 0; /* so far, no override */
559 while (i == TOKEN_SPECIAL) { /* size specifiers */
560 switch ((int)tokval.t_integer) {
561 case S_BYTE:
562 if (!setsize) /* we want to use only the first */
563 result->oprs[operand].type |= BITS8;
564 setsize = 1;
565 break;
566 case S_WORD:
567 if (!setsize)
568 result->oprs[operand].type |= BITS16;
569 setsize = 1;
570 break;
571 case S_DWORD:
572 case S_LONG:
573 if (!setsize)
574 result->oprs[operand].type |= BITS32;
575 setsize = 1;
576 break;
577 case S_QWORD:
578 if (!setsize)
579 result->oprs[operand].type |= BITS64;
580 setsize = 1;
581 break;
582 case S_TWORD:
583 if (!setsize)
584 result->oprs[operand].type |= BITS80;
585 setsize = 1;
586 break;
587 case S_OWORD:
588 if (!setsize)
589 result->oprs[operand].type |= BITS128;
590 setsize = 1;
591 break;
592 case S_YWORD:
593 if (!setsize)
594 result->oprs[operand].type |= BITS256;
595 setsize = 1;
596 break;
597 case S_TO:
598 result->oprs[operand].type |= TO;
599 break;
600 case S_STRICT:
601 result->oprs[operand].type |= STRICT;
602 break;
603 case S_FAR:
604 result->oprs[operand].type |= FAR;
605 break;
606 case S_NEAR:
607 result->oprs[operand].type |= NEAR;
608 break;
609 case S_SHORT:
610 result->oprs[operand].type |= SHORT;
611 break;
612 default:
613 error(ERR_NONFATAL, "invalid operand size specification");
615 i = stdscan(NULL, &tokval);
618 if (i == '[' || i == '&') { /* memory reference */
619 mref = true;
620 bracket = (i == '[');
621 i = stdscan(NULL, &tokval); /* then skip the colon */
622 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
623 process_size_override(result, operand);
624 i = stdscan(NULL, &tokval);
626 } else { /* immediate operand, or register */
627 mref = false;
628 bracket = false; /* placate optimisers */
631 if ((result->oprs[operand].type & FAR) && !mref &&
632 result->opcode != I_JMP && result->opcode != I_CALL) {
633 error(ERR_NONFATAL, "invalid use of FAR operand specifier");
636 value = evaluate(stdscan, NULL, &tokval,
637 &result->oprs[operand].opflags,
638 critical, error, &hints);
639 i = tokval.t_type;
640 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
641 result->forw_ref = true;
643 if (!value) { /* error in evaluator */
644 result->opcode = -1; /* unrecoverable parse error: */
645 return result; /* ignore this instruction */
647 if (i == ':' && mref) { /* it was seg:offset */
649 * Process the segment override.
651 if (value[1].type != 0 || value->value != 1 ||
652 REG_SREG & ~nasm_reg_flags[value->type])
653 error(ERR_NONFATAL, "invalid segment override");
654 else if (result->prefixes[PPS_SEG])
655 error(ERR_NONFATAL,
656 "instruction has conflicting segment overrides");
657 else {
658 result->prefixes[PPS_SEG] = value->type;
659 if (!(REG_FSGS & ~nasm_reg_flags[value->type]))
660 result->oprs[operand].eaflags |= EAF_FSGS;
663 i = stdscan(NULL, &tokval); /* then skip the colon */
664 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
665 process_size_override(result, operand);
666 i = stdscan(NULL, &tokval);
668 value = evaluate(stdscan, NULL, &tokval,
669 &result->oprs[operand].opflags,
670 critical, error, &hints);
671 i = tokval.t_type;
672 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
673 result->forw_ref = true;
675 /* and get the offset */
676 if (!value) { /* but, error in evaluator */
677 result->opcode = -1; /* unrecoverable parse error: */
678 return result; /* ignore this instruction */
681 if (mref && bracket) { /* find ] at the end */
682 if (i != ']') {
683 error(ERR_NONFATAL, "parser: expecting ]");
684 do { /* error recovery again */
685 i = stdscan(NULL, &tokval);
686 } while (i != 0 && i != ',');
687 } else /* we got the required ] */
688 i = stdscan(NULL, &tokval);
689 } else { /* immediate operand */
690 if (i != 0 && i != ',' && i != ':') {
691 error(ERR_NONFATAL, "comma or end of line expected");
692 do { /* error recovery */
693 i = stdscan(NULL, &tokval);
694 } while (i != 0 && i != ',');
695 } else if (i == ':') {
696 result->oprs[operand].type |= COLON;
700 /* now convert the exprs returned from evaluate() into operand
701 * descriptions... */
703 if (mref) { /* it's a memory reference */
704 expr *e = value;
705 int b, i, s; /* basereg, indexreg, scale */
706 int64_t o; /* offset */
708 b = i = -1, o = s = 0;
709 result->oprs[operand].hintbase = hints.base;
710 result->oprs[operand].hinttype = hints.type;
712 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
713 if (e->value == 1) /* in fact it can be basereg */
714 b = e->type;
715 else /* no, it has to be indexreg */
716 i = e->type, s = e->value;
717 e++;
719 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
720 if (b != -1) /* If the first was the base, ... */
721 i = e->type, s = e->value; /* second has to be indexreg */
723 else if (e->value != 1) { /* If both want to be index */
724 error(ERR_NONFATAL,
725 "beroset-p-592-invalid effective address");
726 result->opcode = -1;
727 return result;
728 } else
729 b = e->type;
730 e++;
732 if (e->type != 0) { /* is there an offset? */
733 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
734 error(ERR_NONFATAL,
735 "beroset-p-603-invalid effective address");
736 result->opcode = -1;
737 return result;
738 } else {
739 if (e->type == EXPR_UNKNOWN) {
740 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
741 o = 0; /* doesn't matter what */
742 result->oprs[operand].wrt = NO_SEG; /* nor this */
743 result->oprs[operand].segment = NO_SEG; /* or this */
744 while (e->type)
745 e++; /* go to the end of the line */
746 } else {
747 if (e->type == EXPR_SIMPLE) {
748 o = e->value;
749 e++;
751 if (e->type == EXPR_WRT) {
752 result->oprs[operand].wrt = e->value;
753 e++;
754 } else
755 result->oprs[operand].wrt = NO_SEG;
757 * Look for a segment base type.
759 if (e->type && e->type < EXPR_SEGBASE) {
760 error(ERR_NONFATAL,
761 "beroset-p-630-invalid effective address");
762 result->opcode = -1;
763 return result;
765 while (e->type && e->value == 0)
766 e++;
767 if (e->type && e->value != 1) {
768 error(ERR_NONFATAL,
769 "beroset-p-637-invalid effective address");
770 result->opcode = -1;
771 return result;
773 if (e->type) {
774 result->oprs[operand].segment =
775 e->type - EXPR_SEGBASE;
776 e++;
777 } else
778 result->oprs[operand].segment = NO_SEG;
779 while (e->type && e->value == 0)
780 e++;
781 if (e->type) {
782 error(ERR_NONFATAL,
783 "beroset-p-650-invalid effective address");
784 result->opcode = -1;
785 return result;
789 } else {
790 o = 0;
791 result->oprs[operand].wrt = NO_SEG;
792 result->oprs[operand].segment = NO_SEG;
795 if (e->type != 0) { /* there'd better be nothing left! */
796 error(ERR_NONFATAL,
797 "beroset-p-663-invalid effective address");
798 result->opcode = -1;
799 return result;
802 /* It is memory, but it can match any r/m operand */
803 result->oprs[operand].type |= MEMORY_ANY;
805 if (b == -1 && (i == -1 || s == 0)) {
806 int is_rel = globalbits == 64 &&
807 !(result->oprs[operand].eaflags & EAF_ABS) &&
808 ((globalrel &&
809 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
810 (result->oprs[operand].eaflags & EAF_REL));
812 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
814 result->oprs[operand].basereg = b;
815 result->oprs[operand].indexreg = i;
816 result->oprs[operand].scale = s;
817 result->oprs[operand].offset = o;
818 } else { /* it's not a memory reference */
820 if (is_just_unknown(value)) { /* it's immediate but unknown */
821 result->oprs[operand].type |= IMMEDIATE;
822 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
823 result->oprs[operand].offset = 0; /* don't care */
824 result->oprs[operand].segment = NO_SEG; /* don't care again */
825 result->oprs[operand].wrt = NO_SEG; /* still don't care */
827 if(optimizing >= 0 && !(result->oprs[operand].type & STRICT))
829 /* Be optimistic */
830 result->oprs[operand].type |= SBYTE16 | SBYTE32 | SBYTE64;
832 } else if (is_reloc(value)) { /* it's immediate */
833 result->oprs[operand].type |= IMMEDIATE;
834 result->oprs[operand].offset = reloc_value(value);
835 result->oprs[operand].segment = reloc_seg(value);
836 result->oprs[operand].wrt = reloc_wrt(value);
837 if (is_simple(value)) {
838 if (reloc_value(value) == 1)
839 result->oprs[operand].type |= UNITY;
840 if (optimizing >= 0 &&
841 !(result->oprs[operand].type & STRICT)) {
842 int64_t v64 = reloc_value(value);
843 int32_t v32 = (int32_t)v64;
844 int16_t v16 = (int16_t)v32;
846 if (v64 >= -128 && v64 <= 127)
847 result->oprs[operand].type |= SBYTE64;
848 if (v32 >= -128 && v32 <= 127)
849 result->oprs[operand].type |= SBYTE32;
850 if (v16 >= -128 && v16 <= 127)
851 result->oprs[operand].type |= SBYTE16;
854 } else { /* it's a register */
855 unsigned int rs;
857 if (value->type >= EXPR_SIMPLE || value->value != 1) {
858 error(ERR_NONFATAL, "invalid operand type");
859 result->opcode = -1;
860 return result;
864 * check that its only 1 register, not an expression...
866 for (i = 1; value[i].type; i++)
867 if (value[i].value) {
868 error(ERR_NONFATAL, "invalid operand type");
869 result->opcode = -1;
870 return result;
873 /* clear overrides, except TO which applies to FPU regs */
874 if (result->oprs[operand].type & ~TO) {
876 * we want to produce a warning iff the specified size
877 * is different from the register size
879 rs = result->oprs[operand].type & SIZE_MASK;
880 } else
881 rs = 0;
883 result->oprs[operand].type &= TO;
884 result->oprs[operand].type |= REGISTER;
885 result->oprs[operand].type |= nasm_reg_flags[value->type];
886 result->oprs[operand].basereg = value->type;
888 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
889 error(ERR_WARNING | ERR_PASS1,
890 "register size specification ignored");
895 result->operands = operand; /* set operand count */
897 /* clear remaining operands */
898 while (operand < MAX_OPERANDS)
899 result->oprs[operand++].type = 0;
902 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
904 switch (result->opcode) {
905 case I_RESW:
906 result->opcode = I_RESB;
907 result->oprs[0].offset *= 2;
908 break;
909 case I_RESD:
910 result->opcode = I_RESB;
911 result->oprs[0].offset *= 4;
912 break;
913 case I_RESQ:
914 result->opcode = I_RESB;
915 result->oprs[0].offset *= 8;
916 break;
917 case I_REST:
918 result->opcode = I_RESB;
919 result->oprs[0].offset *= 10;
920 break;
921 case I_RESO:
922 result->opcode = I_RESB;
923 result->oprs[0].offset *= 16;
924 break;
925 case I_RESY:
926 result->opcode = I_RESB;
927 result->oprs[0].offset *= 32;
928 break;
929 default:
930 break;
933 return result;
936 static int is_comma_next(void)
938 char *p;
939 int i;
940 struct tokenval tv;
942 p = stdscan_bufptr;
943 i = stdscan(NULL, &tv);
944 stdscan_bufptr = p;
945 return (i == ',' || i == ';' || !i);
948 void cleanup_insn(insn * i)
950 extop *e;
952 while ((e = i->eops)) {
953 i->eops = e->next;
954 if (e->type == EOT_DB_STRING_FREE)
955 nasm_free(e->stringval);
956 nasm_free(e);