Add np and similar prefixes to instructions that should have them
[nasm.git] / parser.c
blob889adf39e7c40a5e61f39bd70e0abede225fdc97
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * parser.c source line parser for the Netwide Assembler
38 #include "compiler.h"
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <stddef.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <inttypes.h>
47 #include "nasm.h"
48 #include "insns.h"
49 #include "nasmlib.h"
50 #include "stdscan.h"
51 #include "eval.h"
52 #include "parser.h"
53 #include "float.h"
54 #include "tables.h"
56 extern int in_abs_seg; /* ABSOLUTE segment flag */
57 extern int32_t abs_seg; /* ABSOLUTE segment */
58 extern int32_t abs_offset; /* ABSOLUTE segment offset */
60 static int is_comma_next(void);
62 static int i;
63 static struct tokenval tokval;
64 static struct location *location; /* Pointer to current line's segment,offset */
66 void parser_global_info(struct location * locp)
68 location = locp;
71 static int prefix_slot(int prefix)
73 switch (prefix) {
74 case P_WAIT:
75 return PPS_WAIT;
76 case R_CS:
77 case R_DS:
78 case R_SS:
79 case R_ES:
80 case R_FS:
81 case R_GS:
82 return PPS_SEG;
83 case P_LOCK:
84 return PPS_LOCK;
85 case P_REP:
86 case P_REPE:
87 case P_REPZ:
88 case P_REPNE:
89 case P_REPNZ:
90 case P_XACQUIRE:
91 case P_XRELEASE:
92 return PPS_REP;
93 case P_O16:
94 case P_O32:
95 case P_O64:
96 case P_OSP:
97 return PPS_OSIZE;
98 case P_A16:
99 case P_A32:
100 case P_A64:
101 case P_ASP:
102 return PPS_ASIZE;
103 default:
104 nasm_error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
105 return -1;
109 static void process_size_override(insn *result, int operand)
111 if (tasm_compatible_mode) {
112 switch ((int)tokval.t_integer) {
113 /* For TASM compatibility a size override inside the
114 * brackets changes the size of the operand, not the
115 * address type of the operand as it does in standard
116 * NASM syntax. Hence:
118 * mov eax,[DWORD val]
120 * is valid syntax in TASM compatibility mode. Note that
121 * you lose the ability to override the default address
122 * type for the instruction, but we never use anything
123 * but 32-bit flat model addressing in our code.
125 case S_BYTE:
126 result->oprs[operand].type |= BITS8;
127 break;
128 case S_WORD:
129 result->oprs[operand].type |= BITS16;
130 break;
131 case S_DWORD:
132 case S_LONG:
133 result->oprs[operand].type |= BITS32;
134 break;
135 case S_QWORD:
136 result->oprs[operand].type |= BITS64;
137 break;
138 case S_TWORD:
139 result->oprs[operand].type |= BITS80;
140 break;
141 case S_OWORD:
142 result->oprs[operand].type |= BITS128;
143 break;
144 default:
145 nasm_error(ERR_NONFATAL,
146 "invalid operand size specification");
147 break;
149 } else {
150 /* Standard NASM compatible syntax */
151 switch ((int)tokval.t_integer) {
152 case S_NOSPLIT:
153 result->oprs[operand].eaflags |= EAF_TIMESTWO;
154 break;
155 case S_REL:
156 result->oprs[operand].eaflags |= EAF_REL;
157 break;
158 case S_ABS:
159 result->oprs[operand].eaflags |= EAF_ABS;
160 break;
161 case S_BYTE:
162 result->oprs[operand].disp_size = 8;
163 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
164 break;
165 case P_A16:
166 case P_A32:
167 case P_A64:
168 if (result->prefixes[PPS_ASIZE] &&
169 result->prefixes[PPS_ASIZE] != tokval.t_integer)
170 nasm_error(ERR_NONFATAL,
171 "conflicting address size specifications");
172 else
173 result->prefixes[PPS_ASIZE] = tokval.t_integer;
174 break;
175 case S_WORD:
176 result->oprs[operand].disp_size = 16;
177 result->oprs[operand].eaflags |= EAF_WORDOFFS;
178 break;
179 case S_DWORD:
180 case S_LONG:
181 result->oprs[operand].disp_size = 32;
182 result->oprs[operand].eaflags |= EAF_WORDOFFS;
183 break;
184 case S_QWORD:
185 result->oprs[operand].disp_size = 64;
186 result->oprs[operand].eaflags |= EAF_WORDOFFS;
187 break;
188 default:
189 nasm_error(ERR_NONFATAL, "invalid size specification in"
190 " effective address");
191 break;
196 insn *parse_line(int pass, char *buffer, insn *result, ldfunc ldef)
198 bool insn_is_label = false;
199 struct eval_hints hints;
200 int operand;
201 int critical;
202 bool first;
203 bool recover;
204 int j;
206 restart_parse:
207 first = true;
208 result->forw_ref = false;
210 stdscan_reset();
211 stdscan_set(buffer);
212 i = stdscan(NULL, &tokval);
214 result->label = NULL; /* Assume no label */
215 result->eops = NULL; /* must do this, whatever happens */
216 result->operands = 0; /* must initialize this */
218 /* Ignore blank lines */
219 if (i == TOKEN_EOS) {
220 result->opcode = I_none;
221 return result;
224 if (i != TOKEN_ID &&
225 i != TOKEN_INSN &&
226 i != TOKEN_PREFIX &&
227 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
228 nasm_error(ERR_NONFATAL,
229 "label or instruction expected at start of line");
230 result->opcode = I_none;
231 return result;
234 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
235 /* there's a label here */
236 first = false;
237 result->label = tokval.t_charptr;
238 i = stdscan(NULL, &tokval);
239 if (i == ':') { /* skip over the optional colon */
240 i = stdscan(NULL, &tokval);
241 } else if (i == 0) {
242 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
243 "label alone on a line without a colon might be in error");
245 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
247 * FIXME: location->segment could be NO_SEG, in which case
248 * it is possible we should be passing 'abs_seg'. Look into this.
249 * Work out whether that is *really* what we should be doing.
250 * Generally fix things. I think this is right as it is, but
251 * am still not certain.
253 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
254 location->offset, NULL, true, false);
258 /* Just a label here */
259 if (i == TOKEN_EOS) {
260 result->opcode = I_none;
261 return result;
264 for (j = 0; j < MAXPREFIX; j++)
265 result->prefixes[j] = P_none;
266 result->times = 1L;
268 while (i == TOKEN_PREFIX ||
269 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
270 first = false;
273 * Handle special case: the TIMES prefix.
275 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
276 expr *value;
278 i = stdscan(NULL, &tokval);
279 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
280 i = tokval.t_type;
281 if (!value) { /* but, error in evaluator */
282 result->opcode = I_none; /* unrecoverable parse error: */
283 return result; /* ignore this instruction */
285 if (!is_simple(value)) {
286 nasm_error(ERR_NONFATAL,
287 "non-constant argument supplied to TIMES");
288 result->times = 1L;
289 } else {
290 result->times = value->value;
291 if (value->value < 0 && pass0 == 2) {
292 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
293 value->value);
294 result->times = 0;
297 } else {
298 int slot = prefix_slot(tokval.t_integer);
299 if (result->prefixes[slot]) {
300 if (result->prefixes[slot] == tokval.t_integer)
301 nasm_error(ERR_WARNING | ERR_PASS1,
302 "instruction has redundant prefixes");
303 else
304 nasm_error(ERR_NONFATAL,
305 "instruction has conflicting prefixes");
307 result->prefixes[slot] = tokval.t_integer;
308 i = stdscan(NULL, &tokval);
312 if (i != TOKEN_INSN) {
313 int j;
314 enum prefixes pfx;
316 for (j = 0; j < MAXPREFIX; j++) {
317 if ((pfx = result->prefixes[j]) != P_none)
318 break;
321 if (i == 0 && pfx != P_none) {
323 * Instruction prefixes are present, but no actual
324 * instruction. This is allowed: at this point we
325 * invent a notional instruction of RESB 0.
327 result->opcode = I_RESB;
328 result->operands = 1;
329 result->oprs[0].type = IMMEDIATE;
330 result->oprs[0].offset = 0L;
331 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
332 return result;
333 } else {
334 nasm_error(ERR_NONFATAL, "parser: instruction expected");
335 result->opcode = I_none;
336 return result;
340 result->opcode = tokval.t_integer;
341 result->condition = tokval.t_inttwo;
344 * INCBIN cannot be satisfied with incorrectly
345 * evaluated operands, since the correct values _must_ be known
346 * on the first pass. Hence, even in pass one, we set the
347 * `critical' flag on calling evaluate(), so that it will bomb
348 * out on undefined symbols.
350 if (result->opcode == I_INCBIN) {
351 critical = (pass0 < 2 ? 1 : 2);
353 } else
354 critical = (pass == 2 ? 2 : 0);
356 if (result->opcode == I_DB || result->opcode == I_DW ||
357 result->opcode == I_DD || result->opcode == I_DQ ||
358 result->opcode == I_DT || result->opcode == I_DO ||
359 result->opcode == I_DY || result->opcode == I_INCBIN) {
360 extop *eop, **tail = &result->eops, **fixptr;
361 int oper_num = 0;
362 int32_t sign;
364 result->eops_float = false;
367 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
369 while (1) {
370 i = stdscan(NULL, &tokval);
371 if (i == TOKEN_EOS)
372 break;
373 else if (first && i == ':') {
374 insn_is_label = true;
375 goto restart_parse;
377 first = false;
378 fixptr = tail;
379 eop = *tail = nasm_malloc(sizeof(extop));
380 tail = &eop->next;
381 eop->next = NULL;
382 eop->type = EOT_NOTHING;
383 oper_num++;
384 sign = +1;
387 * is_comma_next() here is to distinguish this from
388 * a string used as part of an expression...
390 if (i == TOKEN_STR && is_comma_next()) {
391 eop->type = EOT_DB_STRING;
392 eop->stringval = tokval.t_charptr;
393 eop->stringlen = tokval.t_inttwo;
394 i = stdscan(NULL, &tokval); /* eat the comma */
395 } else if (i == TOKEN_STRFUNC) {
396 bool parens = false;
397 const char *funcname = tokval.t_charptr;
398 enum strfunc func = tokval.t_integer;
399 i = stdscan(NULL, &tokval);
400 if (i == '(') {
401 parens = true;
402 i = stdscan(NULL, &tokval);
404 if (i != TOKEN_STR) {
405 nasm_error(ERR_NONFATAL,
406 "%s must be followed by a string constant",
407 funcname);
408 eop->type = EOT_NOTHING;
409 } else {
410 eop->type = EOT_DB_STRING_FREE;
411 eop->stringlen =
412 string_transform(tokval.t_charptr, tokval.t_inttwo,
413 &eop->stringval, func);
414 if (eop->stringlen == (size_t)-1) {
415 nasm_error(ERR_NONFATAL, "invalid string for transform");
416 eop->type = EOT_NOTHING;
419 if (parens && i && i != ')') {
420 i = stdscan(NULL, &tokval);
421 if (i != ')') {
422 nasm_error(ERR_NONFATAL, "unterminated %s function",
423 funcname);
426 if (i && i != ',')
427 i = stdscan(NULL, &tokval);
428 } else if (i == '-' || i == '+') {
429 char *save = stdscan_get();
430 int token = i;
431 sign = (i == '-') ? -1 : 1;
432 i = stdscan(NULL, &tokval);
433 if (i != TOKEN_FLOAT) {
434 stdscan_set(save);
435 i = tokval.t_type = token;
436 goto is_expression;
437 } else {
438 goto is_float;
440 } else if (i == TOKEN_FLOAT) {
441 is_float:
442 eop->type = EOT_DB_STRING;
443 result->eops_float = true;
445 eop->stringlen = idata_bytes(result->opcode);
446 if (eop->stringlen > 16) {
447 nasm_error(ERR_NONFATAL, "floating-point constant"
448 " encountered in DY instruction");
449 eop->stringlen = 0;
450 } else if (eop->stringlen < 1) {
451 nasm_error(ERR_NONFATAL, "floating-point constant"
452 " encountered in unknown instruction");
454 * fix suggested by Pedro Gimeno... original line was:
455 * eop->type = EOT_NOTHING;
457 eop->stringlen = 0;
460 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
461 tail = &eop->next;
462 *fixptr = eop;
463 eop->stringval = (char *)eop + sizeof(extop);
464 if (!eop->stringlen ||
465 !float_const(tokval.t_charptr, sign,
466 (uint8_t *)eop->stringval,
467 eop->stringlen, nasm_error))
468 eop->type = EOT_NOTHING;
469 i = stdscan(NULL, &tokval); /* eat the comma */
470 } else {
471 /* anything else, assume it is an expression */
472 expr *value;
474 is_expression:
475 value = evaluate(stdscan, NULL, &tokval, NULL,
476 critical, nasm_error, NULL);
477 i = tokval.t_type;
478 if (!value) { /* error in evaluator */
479 result->opcode = I_none; /* unrecoverable parse error: */
480 return result; /* ignore this instruction */
482 if (is_unknown(value)) {
483 eop->type = EOT_DB_NUMBER;
484 eop->offset = 0; /* doesn't matter what we put */
485 eop->segment = eop->wrt = NO_SEG; /* likewise */
486 } else if (is_reloc(value)) {
487 eop->type = EOT_DB_NUMBER;
488 eop->offset = reloc_value(value);
489 eop->segment = reloc_seg(value);
490 eop->wrt = reloc_wrt(value);
491 } else {
492 nasm_error(ERR_NONFATAL,
493 "operand %d: expression is not simple"
494 " or relocatable", oper_num);
499 * We're about to call stdscan(), which will eat the
500 * comma that we're currently sitting on between
501 * arguments. However, we'd better check first that it
502 * _is_ a comma.
504 if (i == TOKEN_EOS) /* also could be EOL */
505 break;
506 if (i != ',') {
507 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
508 oper_num);
509 result->opcode = I_none;/* unrecoverable parse error: */
510 return result; /* ignore this instruction */
514 if (result->opcode == I_INCBIN) {
516 * Correct syntax for INCBIN is that there should be
517 * one string operand, followed by one or two numeric
518 * operands.
520 if (!result->eops || result->eops->type != EOT_DB_STRING)
521 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
522 else if (result->eops->next &&
523 result->eops->next->type != EOT_DB_NUMBER)
524 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
525 " non-numeric");
526 else if (result->eops->next && result->eops->next->next &&
527 result->eops->next->next->type != EOT_DB_NUMBER)
528 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
529 " non-numeric");
530 else if (result->eops->next && result->eops->next->next &&
531 result->eops->next->next->next)
532 nasm_error(ERR_NONFATAL,
533 "`incbin': more than three parameters");
534 else
535 return result;
537 * If we reach here, one of the above errors happened.
538 * Throw the instruction away.
540 result->opcode = I_none;
541 return result;
542 } else /* DB ... */ if (oper_num == 0)
543 nasm_error(ERR_WARNING | ERR_PASS1,
544 "no operand for data declaration");
545 else
546 result->operands = oper_num;
548 return result;
552 * Now we begin to parse the operands. There may be up to four
553 * of these, separated by commas, and terminated by a zero token.
556 for (operand = 0; operand < MAX_OPERANDS; operand++) {
557 expr *value; /* used most of the time */
558 int mref; /* is this going to be a memory ref? */
559 int bracket; /* is it a [] mref, or a & mref? */
560 int setsize = 0;
562 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
563 result->oprs[operand].eaflags = 0; /* and this */
564 result->oprs[operand].opflags = 0;
566 i = stdscan(NULL, &tokval);
567 if (i == TOKEN_EOS)
568 break; /* end of operands: get out of here */
569 else if (first && i == ':') {
570 insn_is_label = true;
571 goto restart_parse;
573 first = false;
574 result->oprs[operand].type = 0; /* so far, no override */
575 while (i == TOKEN_SPECIAL) { /* size specifiers */
576 switch ((int)tokval.t_integer) {
577 case S_BYTE:
578 if (!setsize) /* we want to use only the first */
579 result->oprs[operand].type |= BITS8;
580 setsize = 1;
581 break;
582 case S_WORD:
583 if (!setsize)
584 result->oprs[operand].type |= BITS16;
585 setsize = 1;
586 break;
587 case S_DWORD:
588 case S_LONG:
589 if (!setsize)
590 result->oprs[operand].type |= BITS32;
591 setsize = 1;
592 break;
593 case S_QWORD:
594 if (!setsize)
595 result->oprs[operand].type |= BITS64;
596 setsize = 1;
597 break;
598 case S_TWORD:
599 if (!setsize)
600 result->oprs[operand].type |= BITS80;
601 setsize = 1;
602 break;
603 case S_OWORD:
604 if (!setsize)
605 result->oprs[operand].type |= BITS128;
606 setsize = 1;
607 break;
608 case S_YWORD:
609 if (!setsize)
610 result->oprs[operand].type |= BITS256;
611 setsize = 1;
612 break;
613 case S_TO:
614 result->oprs[operand].type |= TO;
615 break;
616 case S_STRICT:
617 result->oprs[operand].type |= STRICT;
618 break;
619 case S_FAR:
620 result->oprs[operand].type |= FAR;
621 break;
622 case S_NEAR:
623 result->oprs[operand].type |= NEAR;
624 break;
625 case S_SHORT:
626 result->oprs[operand].type |= SHORT;
627 break;
628 default:
629 nasm_error(ERR_NONFATAL, "invalid operand size specification");
631 i = stdscan(NULL, &tokval);
634 if (i == '[' || i == '&') { /* memory reference */
635 mref = true;
636 bracket = (i == '[');
637 i = stdscan(NULL, &tokval); /* then skip the colon */
638 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
639 process_size_override(result, operand);
640 i = stdscan(NULL, &tokval);
642 } else { /* immediate operand, or register */
643 mref = false;
644 bracket = false; /* placate optimisers */
647 if ((result->oprs[operand].type & FAR) && !mref &&
648 result->opcode != I_JMP && result->opcode != I_CALL) {
649 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
652 value = evaluate(stdscan, NULL, &tokval,
653 &result->oprs[operand].opflags,
654 critical, nasm_error, &hints);
655 i = tokval.t_type;
656 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
657 result->forw_ref = true;
659 if (!value) { /* nasm_error in evaluator */
660 result->opcode = I_none; /* unrecoverable parse error: */
661 return result; /* ignore this instruction */
663 if (i == ':' && mref) { /* it was seg:offset */
665 * Process the segment override.
667 if (value[1].type != 0 ||
668 value->value != 1 ||
669 !IS_SREG(value->type))
670 nasm_error(ERR_NONFATAL, "invalid segment override");
671 else if (result->prefixes[PPS_SEG])
672 nasm_error(ERR_NONFATAL,
673 "instruction has conflicting segment overrides");
674 else {
675 result->prefixes[PPS_SEG] = value->type;
676 if (IS_FSGS(value->type))
677 result->oprs[operand].eaflags |= EAF_FSGS;
680 i = stdscan(NULL, &tokval); /* then skip the colon */
681 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
682 process_size_override(result, operand);
683 i = stdscan(NULL, &tokval);
685 value = evaluate(stdscan, NULL, &tokval,
686 &result->oprs[operand].opflags,
687 critical, nasm_error, &hints);
688 i = tokval.t_type;
689 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
690 result->forw_ref = true;
692 /* and get the offset */
693 if (!value) { /* but, error in evaluator */
694 result->opcode = I_none; /* unrecoverable parse error: */
695 return result; /* ignore this instruction */
699 recover = false;
700 if (mref && bracket) { /* find ] at the end */
701 if (i != ']') {
702 nasm_error(ERR_NONFATAL, "parser: expecting ]");
703 recover = true;
704 } else { /* we got the required ] */
705 i = stdscan(NULL, &tokval);
706 if (i != 0 && i != ',') {
707 nasm_error(ERR_NONFATAL, "comma or end of line expected");
708 recover = true;
711 } else { /* immediate operand */
712 if (i != 0 && i != ',' && i != ':') {
713 nasm_error(ERR_NONFATAL, "comma, colon or end of line expected");
714 recover = true;
715 } else if (i == ':') {
716 result->oprs[operand].type |= COLON;
719 if (recover) {
720 do { /* error recovery */
721 i = stdscan(NULL, &tokval);
722 } while (i != 0 && i != ',');
726 * now convert the exprs returned from evaluate()
727 * into operand descriptions...
730 if (mref) { /* it's a memory reference */
731 expr *e = value;
732 int b, i, s; /* basereg, indexreg, scale */
733 int64_t o; /* offset */
735 b = i = -1, o = s = 0;
736 result->oprs[operand].hintbase = hints.base;
737 result->oprs[operand].hinttype = hints.type;
739 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
740 if (e->value == 1) /* in fact it can be basereg */
741 b = e->type;
742 else /* no, it has to be indexreg */
743 i = e->type, s = e->value;
744 e++;
746 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
747 if (b != -1) /* If the first was the base, ... */
748 i = e->type, s = e->value; /* second has to be indexreg */
750 else if (e->value != 1) { /* If both want to be index */
751 nasm_error(ERR_NONFATAL,
752 "beroset-p-592-invalid effective address");
753 result->opcode = I_none;
754 return result;
755 } else
756 b = e->type;
757 e++;
759 if (e->type != 0) { /* is there an offset? */
760 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
761 nasm_error(ERR_NONFATAL,
762 "beroset-p-603-invalid effective address");
763 result->opcode = I_none;
764 return result;
765 } else {
766 if (e->type == EXPR_UNKNOWN) {
767 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
768 o = 0; /* doesn't matter what */
769 result->oprs[operand].wrt = NO_SEG; /* nor this */
770 result->oprs[operand].segment = NO_SEG; /* or this */
771 while (e->type)
772 e++; /* go to the end of the line */
773 } else {
774 if (e->type == EXPR_SIMPLE) {
775 o = e->value;
776 e++;
778 if (e->type == EXPR_WRT) {
779 result->oprs[operand].wrt = e->value;
780 e++;
781 } else
782 result->oprs[operand].wrt = NO_SEG;
784 * Look for a segment base type.
786 if (e->type && e->type < EXPR_SEGBASE) {
787 nasm_error(ERR_NONFATAL,
788 "beroset-p-630-invalid effective address");
789 result->opcode = I_none;
790 return result;
792 while (e->type && e->value == 0)
793 e++;
794 if (e->type && e->value != 1) {
795 nasm_error(ERR_NONFATAL,
796 "beroset-p-637-invalid effective address");
797 result->opcode = I_none;
798 return result;
800 if (e->type) {
801 result->oprs[operand].segment =
802 e->type - EXPR_SEGBASE;
803 e++;
804 } else
805 result->oprs[operand].segment = NO_SEG;
806 while (e->type && e->value == 0)
807 e++;
808 if (e->type) {
809 nasm_error(ERR_NONFATAL,
810 "beroset-p-650-invalid effective address");
811 result->opcode = I_none;
812 return result;
816 } else {
817 o = 0;
818 result->oprs[operand].wrt = NO_SEG;
819 result->oprs[operand].segment = NO_SEG;
822 if (e->type != 0) { /* there'd better be nothing left! */
823 nasm_error(ERR_NONFATAL,
824 "beroset-p-663-invalid effective address");
825 result->opcode = I_none;
826 return result;
829 /* It is memory, but it can match any r/m operand */
830 result->oprs[operand].type |= MEMORY_ANY;
832 if (b == -1 && (i == -1 || s == 0)) {
833 int is_rel = globalbits == 64 &&
834 !(result->oprs[operand].eaflags & EAF_ABS) &&
835 ((globalrel &&
836 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
837 (result->oprs[operand].eaflags & EAF_REL));
839 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
841 result->oprs[operand].basereg = b;
842 result->oprs[operand].indexreg = i;
843 result->oprs[operand].scale = s;
844 result->oprs[operand].offset = o;
845 } else { /* it's not a memory reference */
846 if (is_just_unknown(value)) { /* it's immediate but unknown */
847 result->oprs[operand].type |= IMMEDIATE;
848 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
849 result->oprs[operand].offset = 0; /* don't care */
850 result->oprs[operand].segment = NO_SEG; /* don't care again */
851 result->oprs[operand].wrt = NO_SEG; /* still don't care */
853 if(optimizing >= 0 && !(result->oprs[operand].type & STRICT)) {
854 /* Be optimistic */
855 result->oprs[operand].type |=
856 SBYTE16 | SBYTE32 | SBYTE64 | UDWORD64 | SDWORD64;
858 } else if (is_reloc(value)) { /* it's immediate */
859 result->oprs[operand].type |= IMMEDIATE;
860 result->oprs[operand].offset = reloc_value(value);
861 result->oprs[operand].segment = reloc_seg(value);
862 result->oprs[operand].wrt = reloc_wrt(value);
864 if (is_simple(value)) {
865 if (reloc_value(value) == 1)
866 result->oprs[operand].type |= UNITY;
867 if (optimizing >= 0 &&
868 !(result->oprs[operand].type & STRICT)) {
869 int64_t v64 = reloc_value(value);
870 int32_t v32 = (int32_t)v64;
871 int16_t v16 = (int16_t)v32;
873 if (v64 >= -128 && v64 <= 127)
874 result->oprs[operand].type |= SBYTE64;
875 if (v32 >= -128 && v32 <= 127)
876 result->oprs[operand].type |= SBYTE32;
877 if (v16 >= -128 && v16 <= 127)
878 result->oprs[operand].type |= SBYTE16;
879 if ((uint64_t)v64 <= UINT64_C(0xffffffff))
880 result->oprs[operand].type |= UDWORD64;
881 if (v64 >= -INT64_C(0x80000000) &&
882 v64 <= INT64_C(0x7fffffff))
883 result->oprs[operand].type |= SDWORD64;
886 } else { /* it's a register */
887 opflags_t rs;
889 if (value->type >= EXPR_SIMPLE || value->value != 1) {
890 nasm_error(ERR_NONFATAL, "invalid operand type");
891 result->opcode = I_none;
892 return result;
896 * check that its only 1 register, not an expression...
898 for (i = 1; value[i].type; i++)
899 if (value[i].value) {
900 nasm_error(ERR_NONFATAL, "invalid operand type");
901 result->opcode = I_none;
902 return result;
905 /* clear overrides, except TO which applies to FPU regs */
906 if (result->oprs[operand].type & ~TO) {
908 * we want to produce a warning iff the specified size
909 * is different from the register size
911 rs = result->oprs[operand].type & SIZE_MASK;
912 } else
913 rs = 0;
915 result->oprs[operand].type &= TO;
916 result->oprs[operand].type |= REGISTER;
917 result->oprs[operand].type |= nasm_reg_flags[value->type];
918 result->oprs[operand].basereg = value->type;
920 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
921 nasm_error(ERR_WARNING | ERR_PASS1,
922 "register size specification ignored");
927 result->operands = operand; /* set operand count */
929 /* clear remaining operands */
930 while (operand < MAX_OPERANDS)
931 result->oprs[operand++].type = 0;
934 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
936 switch (result->opcode) {
937 case I_RESW:
938 result->opcode = I_RESB;
939 result->oprs[0].offset *= 2;
940 break;
941 case I_RESD:
942 result->opcode = I_RESB;
943 result->oprs[0].offset *= 4;
944 break;
945 case I_RESQ:
946 result->opcode = I_RESB;
947 result->oprs[0].offset *= 8;
948 break;
949 case I_REST:
950 result->opcode = I_RESB;
951 result->oprs[0].offset *= 10;
952 break;
953 case I_RESO:
954 result->opcode = I_RESB;
955 result->oprs[0].offset *= 16;
956 break;
957 case I_RESY:
958 result->opcode = I_RESB;
959 result->oprs[0].offset *= 32;
960 break;
961 default:
962 break;
965 return result;
968 static int is_comma_next(void)
970 struct tokenval tv;
971 char *p;
972 int i;
974 p = stdscan_get();
975 i = stdscan(NULL, &tv);
976 stdscan_set(p);
978 return (i == ',' || i == ';' || !i);
981 void cleanup_insn(insn * i)
983 extop *e;
985 while ((e = i->eops)) {
986 i->eops = e->next;
987 if (e->type == EOT_DB_STRING_FREE)
988 nasm_free(e->stringval);
989 nasm_free(e);