A few style fixups in parser.c
[nasm.git] / parser.c
blob8cbb49c5929688d5cd4731cbbf79d2b71c0ab5a7
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(enum prefixes 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 case P_REP:
85 case P_REPE:
86 case P_REPZ:
87 case P_REPNE:
88 case P_REPNZ:
89 return PPS_LREP;
90 case P_O16:
91 case P_O32:
92 case P_O64:
93 case P_OSP:
94 return PPS_OSIZE;
95 case P_A16:
96 case P_A32:
97 case P_A64:
98 case P_ASP:
99 return PPS_ASIZE;
100 default:
101 nasm_error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
102 return -1;
106 static void process_size_override(insn *result, int operand)
108 if (tasm_compatible_mode) {
109 switch ((int)tokval.t_integer) {
110 /* For TASM compatibility a size override inside the
111 * brackets changes the size of the operand, not the
112 * address type of the operand as it does in standard
113 * NASM syntax. Hence:
115 * mov eax,[DWORD val]
117 * is valid syntax in TASM compatibility mode. Note that
118 * you lose the ability to override the default address
119 * type for the instruction, but we never use anything
120 * but 32-bit flat model addressing in our code.
122 case S_BYTE:
123 result->oprs[operand].type |= BITS8;
124 break;
125 case S_WORD:
126 result->oprs[operand].type |= BITS16;
127 break;
128 case S_DWORD:
129 case S_LONG:
130 result->oprs[operand].type |= BITS32;
131 break;
132 case S_QWORD:
133 result->oprs[operand].type |= BITS64;
134 break;
135 case S_TWORD:
136 result->oprs[operand].type |= BITS80;
137 break;
138 case S_OWORD:
139 result->oprs[operand].type |= BITS128;
140 break;
141 default:
142 nasm_error(ERR_NONFATAL,
143 "invalid operand size specification");
144 break;
146 } else {
147 /* Standard NASM compatible syntax */
148 switch ((int)tokval.t_integer) {
149 case S_NOSPLIT:
150 result->oprs[operand].eaflags |= EAF_TIMESTWO;
151 break;
152 case S_REL:
153 result->oprs[operand].eaflags |= EAF_REL;
154 break;
155 case S_ABS:
156 result->oprs[operand].eaflags |= EAF_ABS;
157 break;
158 case S_BYTE:
159 result->oprs[operand].disp_size = 8;
160 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
161 break;
162 case P_A16:
163 case P_A32:
164 case P_A64:
165 if (result->prefixes[PPS_ASIZE] &&
166 result->prefixes[PPS_ASIZE] != tokval.t_integer)
167 nasm_error(ERR_NONFATAL,
168 "conflicting address size specifications");
169 else
170 result->prefixes[PPS_ASIZE] = tokval.t_integer;
171 break;
172 case S_WORD:
173 result->oprs[operand].disp_size = 16;
174 result->oprs[operand].eaflags |= EAF_WORDOFFS;
175 break;
176 case S_DWORD:
177 case S_LONG:
178 result->oprs[operand].disp_size = 32;
179 result->oprs[operand].eaflags |= EAF_WORDOFFS;
180 break;
181 case S_QWORD:
182 result->oprs[operand].disp_size = 64;
183 result->oprs[operand].eaflags |= EAF_WORDOFFS;
184 break;
185 default:
186 nasm_error(ERR_NONFATAL, "invalid size specification in"
187 " effective address");
188 break;
193 insn *parse_line(int pass, char *buffer, insn *result, ldfunc ldef)
195 bool insn_is_label = false;
196 struct eval_hints hints;
197 int operand;
198 int critical;
199 bool first;
200 bool recover;
201 int j;
203 restart_parse:
204 first = true;
205 result->forw_ref = false;
207 stdscan_reset();
208 stdscan_set(buffer);
209 i = stdscan(NULL, &tokval);
211 result->label = NULL; /* Assume no label */
212 result->eops = NULL; /* must do this, whatever happens */
213 result->operands = 0; /* must initialize this */
215 /* Ignore blank lines */
216 if (i == TOKEN_EOS) {
217 result->opcode = I_none;
218 return result;
221 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
222 (i != TOKEN_REG || (REG_SREG & ~nasm_reg_flags[tokval.t_integer]))) {
223 nasm_error(ERR_NONFATAL, "label or instruction expected"
224 " at start of line");
225 result->opcode = I_none;
226 return result;
229 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
230 /* there's a label here */
231 first = false;
232 result->label = tokval.t_charptr;
233 i = stdscan(NULL, &tokval);
234 if (i == ':') { /* skip over the optional colon */
235 i = stdscan(NULL, &tokval);
236 } else if (i == 0) {
237 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
238 "label alone on a line without a colon might be in error");
240 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
242 * FIXME: location->segment could be NO_SEG, in which case
243 * it is possible we should be passing 'abs_seg'. Look into this.
244 * Work out whether that is *really* what we should be doing.
245 * Generally fix things. I think this is right as it is, but
246 * am still not certain.
248 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
249 location->offset, NULL, true, false);
253 /* Just a label here */
254 if (i == TOKEN_EOS) {
255 result->opcode = I_none;
256 return result;
259 for (j = 0; j < MAXPREFIX; j++)
260 result->prefixes[j] = P_none;
261 result->times = 1L;
263 while (i == TOKEN_PREFIX ||
264 (i == TOKEN_REG && !(REG_SREG & ~nasm_reg_flags[tokval.t_integer])))
266 first = false;
269 * Handle special case: the TIMES prefix.
271 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
272 expr *value;
274 i = stdscan(NULL, &tokval);
275 value =
276 evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
277 i = tokval.t_type;
278 if (!value) { /* but, error in evaluator */
279 result->opcode = I_none; /* unrecoverable parse error: */
280 return result; /* ignore this instruction */
282 if (!is_simple(value)) {
283 nasm_error(ERR_NONFATAL,
284 "non-constant argument supplied to TIMES");
285 result->times = 1L;
286 } else {
287 result->times = value->value;
288 if (value->value < 0 && pass0 == 2) {
289 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
290 value->value);
291 result->times = 0;
294 } else {
295 int slot = prefix_slot(tokval.t_integer);
296 if (result->prefixes[slot]) {
297 if (result->prefixes[slot] == tokval.t_integer)
298 nasm_error(ERR_WARNING | ERR_PASS1,
299 "instruction has redundant prefixes");
300 else
301 nasm_error(ERR_NONFATAL,
302 "instruction has conflicting prefixes");
304 result->prefixes[slot] = tokval.t_integer;
305 i = stdscan(NULL, &tokval);
309 if (i != TOKEN_INSN) {
310 int j;
311 enum prefixes pfx;
313 for (j = 0; j < MAXPREFIX; j++) {
314 if ((pfx = result->prefixes[j]) != P_none)
315 break;
318 if (i == 0 && pfx != P_none) {
320 * Instruction prefixes are present, but no actual
321 * instruction. This is allowed: at this point we
322 * invent a notional instruction of RESB 0.
324 result->opcode = I_RESB;
325 result->operands = 1;
326 result->oprs[0].type = IMMEDIATE;
327 result->oprs[0].offset = 0L;
328 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
329 return result;
330 } else {
331 nasm_error(ERR_NONFATAL, "parser: instruction expected");
332 result->opcode = I_none;
333 return result;
337 result->opcode = tokval.t_integer;
338 result->condition = tokval.t_inttwo;
341 * INCBIN cannot be satisfied with incorrectly
342 * evaluated operands, since the correct values _must_ be known
343 * on the first pass. Hence, even in pass one, we set the
344 * `critical' flag on calling evaluate(), so that it will bomb
345 * out on undefined symbols.
347 if (result->opcode == I_INCBIN) {
348 critical = (pass0 < 2 ? 1 : 2);
350 } else
351 critical = (pass == 2 ? 2 : 0);
353 if (result->opcode == I_DB || result->opcode == I_DW ||
354 result->opcode == I_DD || result->opcode == I_DQ ||
355 result->opcode == I_DT || result->opcode == I_DO ||
356 result->opcode == I_DY || result->opcode == I_INCBIN) {
357 extop *eop, **tail = &result->eops, **fixptr;
358 int oper_num = 0;
359 int32_t sign;
361 result->eops_float = false;
364 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
366 while (1) {
367 i = stdscan(NULL, &tokval);
368 if (i == TOKEN_EOS)
369 break;
370 else if (first && i == ':') {
371 insn_is_label = true;
372 goto restart_parse;
374 first = false;
375 fixptr = tail;
376 eop = *tail = nasm_malloc(sizeof(extop));
377 tail = &eop->next;
378 eop->next = NULL;
379 eop->type = EOT_NOTHING;
380 oper_num++;
381 sign = +1;
384 * is_comma_next() here is to distinguish this from
385 * a string used as part of an expression...
387 if (i == TOKEN_STR && is_comma_next()) {
388 eop->type = EOT_DB_STRING;
389 eop->stringval = tokval.t_charptr;
390 eop->stringlen = tokval.t_inttwo;
391 i = stdscan(NULL, &tokval); /* eat the comma */
392 } else if (i == TOKEN_STRFUNC) {
393 bool parens = false;
394 const char *funcname = tokval.t_charptr;
395 enum strfunc func = tokval.t_integer;
396 i = stdscan(NULL, &tokval);
397 if (i == '(') {
398 parens = true;
399 i = stdscan(NULL, &tokval);
401 if (i != TOKEN_STR) {
402 nasm_error(ERR_NONFATAL,
403 "%s must be followed by a string constant",
404 funcname);
405 eop->type = EOT_NOTHING;
406 } else {
407 eop->type = EOT_DB_STRING_FREE;
408 eop->stringlen =
409 string_transform(tokval.t_charptr, tokval.t_inttwo,
410 &eop->stringval, func);
411 if (eop->stringlen == (size_t)-1) {
412 nasm_error(ERR_NONFATAL, "invalid string for transform");
413 eop->type = EOT_NOTHING;
416 if (parens && i && i != ')') {
417 i = stdscan(NULL, &tokval);
418 if (i != ')') {
419 nasm_error(ERR_NONFATAL, "unterminated %s function",
420 funcname);
423 if (i && i != ',')
424 i = stdscan(NULL, &tokval);
425 } else if (i == '-' || i == '+') {
426 char *save = stdscan_get();
427 int token = i;
428 sign = (i == '-') ? -1 : 1;
429 i = stdscan(NULL, &tokval);
430 if (i != TOKEN_FLOAT) {
431 stdscan_set(save);
432 i = tokval.t_type = token;
433 goto is_expression;
434 } else {
435 goto is_float;
437 } else if (i == TOKEN_FLOAT) {
438 is_float:
439 eop->type = EOT_DB_STRING;
440 result->eops_float = true;
442 eop->stringlen = idata_bytes(result->opcode);
443 if (eop->stringlen > 16) {
444 nasm_error(ERR_NONFATAL, "floating-point constant"
445 " encountered in DY instruction");
446 eop->stringlen = 0;
447 } else if (eop->stringlen < 1) {
448 nasm_error(ERR_NONFATAL, "floating-point constant"
449 " encountered in unknown instruction");
451 * fix suggested by Pedro Gimeno... original line was:
452 * eop->type = EOT_NOTHING;
454 eop->stringlen = 0;
457 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
458 tail = &eop->next;
459 *fixptr = eop;
460 eop->stringval = (char *)eop + sizeof(extop);
461 if (!eop->stringlen ||
462 !float_const(tokval.t_charptr, sign,
463 (uint8_t *)eop->stringval,
464 eop->stringlen, nasm_error))
465 eop->type = EOT_NOTHING;
466 i = stdscan(NULL, &tokval); /* eat the comma */
467 } else {
468 /* anything else, assume it is an expression */
469 expr *value;
471 is_expression:
472 value = evaluate(stdscan, NULL, &tokval, NULL,
473 critical, nasm_error, NULL);
474 i = tokval.t_type;
475 if (!value) { /* error in evaluator */
476 result->opcode = I_none; /* unrecoverable parse error: */
477 return result; /* ignore this instruction */
479 if (is_unknown(value)) {
480 eop->type = EOT_DB_NUMBER;
481 eop->offset = 0; /* doesn't matter what we put */
482 eop->segment = eop->wrt = NO_SEG; /* likewise */
483 } else if (is_reloc(value)) {
484 eop->type = EOT_DB_NUMBER;
485 eop->offset = reloc_value(value);
486 eop->segment = reloc_seg(value);
487 eop->wrt = reloc_wrt(value);
488 } else {
489 nasm_error(ERR_NONFATAL,
490 "operand %d: expression is not simple"
491 " or relocatable", oper_num);
496 * We're about to call stdscan(), which will eat the
497 * comma that we're currently sitting on between
498 * arguments. However, we'd better check first that it
499 * _is_ a comma.
501 if (i == TOKEN_EOS) /* also could be EOL */
502 break;
503 if (i != ',') {
504 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
505 oper_num);
506 result->opcode = I_none;/* unrecoverable parse error: */
507 return result; /* ignore this instruction */
511 if (result->opcode == I_INCBIN) {
513 * Correct syntax for INCBIN is that there should be
514 * one string operand, followed by one or two numeric
515 * operands.
517 if (!result->eops || result->eops->type != EOT_DB_STRING)
518 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
519 else if (result->eops->next &&
520 result->eops->next->type != EOT_DB_NUMBER)
521 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
522 " non-numeric");
523 else if (result->eops->next && result->eops->next->next &&
524 result->eops->next->next->type != EOT_DB_NUMBER)
525 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
526 " non-numeric");
527 else if (result->eops->next && result->eops->next->next &&
528 result->eops->next->next->next)
529 nasm_error(ERR_NONFATAL,
530 "`incbin': more than three parameters");
531 else
532 return result;
534 * If we reach here, one of the above errors happened.
535 * Throw the instruction away.
537 result->opcode = I_none;
538 return result;
539 } else /* DB ... */ if (oper_num == 0)
540 nasm_error(ERR_WARNING | ERR_PASS1,
541 "no operand for data declaration");
542 else
543 result->operands = oper_num;
545 return result;
549 * Now we begin to parse the operands. There may be up to four
550 * of these, separated by commas, and terminated by a zero token.
553 for (operand = 0; operand < MAX_OPERANDS; operand++) {
554 expr *value; /* used most of the time */
555 int mref; /* is this going to be a memory ref? */
556 int bracket; /* is it a [] mref, or a & mref? */
557 int setsize = 0;
559 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
560 result->oprs[operand].eaflags = 0; /* and this */
561 result->oprs[operand].opflags = 0;
563 i = stdscan(NULL, &tokval);
564 if (i == TOKEN_EOS)
565 break; /* end of operands: get out of here */
566 else if (first && i == ':') {
567 insn_is_label = true;
568 goto restart_parse;
570 first = false;
571 result->oprs[operand].type = 0; /* so far, no override */
572 while (i == TOKEN_SPECIAL) { /* size specifiers */
573 switch ((int)tokval.t_integer) {
574 case S_BYTE:
575 if (!setsize) /* we want to use only the first */
576 result->oprs[operand].type |= BITS8;
577 setsize = 1;
578 break;
579 case S_WORD:
580 if (!setsize)
581 result->oprs[operand].type |= BITS16;
582 setsize = 1;
583 break;
584 case S_DWORD:
585 case S_LONG:
586 if (!setsize)
587 result->oprs[operand].type |= BITS32;
588 setsize = 1;
589 break;
590 case S_QWORD:
591 if (!setsize)
592 result->oprs[operand].type |= BITS64;
593 setsize = 1;
594 break;
595 case S_TWORD:
596 if (!setsize)
597 result->oprs[operand].type |= BITS80;
598 setsize = 1;
599 break;
600 case S_OWORD:
601 if (!setsize)
602 result->oprs[operand].type |= BITS128;
603 setsize = 1;
604 break;
605 case S_YWORD:
606 if (!setsize)
607 result->oprs[operand].type |= BITS256;
608 setsize = 1;
609 break;
610 case S_TO:
611 result->oprs[operand].type |= TO;
612 break;
613 case S_STRICT:
614 result->oprs[operand].type |= STRICT;
615 break;
616 case S_FAR:
617 result->oprs[operand].type |= FAR;
618 break;
619 case S_NEAR:
620 result->oprs[operand].type |= NEAR;
621 break;
622 case S_SHORT:
623 result->oprs[operand].type |= SHORT;
624 break;
625 default:
626 nasm_error(ERR_NONFATAL, "invalid operand size specification");
628 i = stdscan(NULL, &tokval);
631 if (i == '[' || i == '&') { /* memory reference */
632 mref = true;
633 bracket = (i == '[');
634 i = stdscan(NULL, &tokval); /* then skip the colon */
635 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
636 process_size_override(result, operand);
637 i = stdscan(NULL, &tokval);
639 } else { /* immediate operand, or register */
640 mref = false;
641 bracket = false; /* placate optimisers */
644 if ((result->oprs[operand].type & FAR) && !mref &&
645 result->opcode != I_JMP && result->opcode != I_CALL) {
646 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
649 value = evaluate(stdscan, NULL, &tokval,
650 &result->oprs[operand].opflags,
651 critical, nasm_error, &hints);
652 i = tokval.t_type;
653 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
654 result->forw_ref = true;
656 if (!value) { /* nasm_error in evaluator */
657 result->opcode = I_none; /* unrecoverable parse error: */
658 return result; /* ignore this instruction */
660 if (i == ':' && mref) { /* it was seg:offset */
662 * Process the segment override.
664 if (value[1].type != 0 || value->value != 1 ||
665 REG_SREG & ~nasm_reg_flags[value->type])
666 nasm_error(ERR_NONFATAL, "invalid segment override");
667 else if (result->prefixes[PPS_SEG])
668 nasm_error(ERR_NONFATAL,
669 "instruction has conflicting segment overrides");
670 else {
671 result->prefixes[PPS_SEG] = value->type;
672 if (!(REG_FSGS & ~nasm_reg_flags[value->type]))
673 result->oprs[operand].eaflags |= EAF_FSGS;
676 i = stdscan(NULL, &tokval); /* then skip the colon */
677 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
678 process_size_override(result, operand);
679 i = stdscan(NULL, &tokval);
681 value = evaluate(stdscan, NULL, &tokval,
682 &result->oprs[operand].opflags,
683 critical, nasm_error, &hints);
684 i = tokval.t_type;
685 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
686 result->forw_ref = true;
688 /* and get the offset */
689 if (!value) { /* but, error in evaluator */
690 result->opcode = I_none; /* unrecoverable parse error: */
691 return result; /* ignore this instruction */
695 recover = false;
696 if (mref && bracket) { /* find ] at the end */
697 if (i != ']') {
698 nasm_error(ERR_NONFATAL, "parser: expecting ]");
699 recover = true;
700 } else { /* we got the required ] */
701 i = stdscan(NULL, &tokval);
702 if (i != 0 && i != ',') {
703 nasm_error(ERR_NONFATAL, "comma or end of line expected");
704 recover = true;
707 } else { /* immediate operand */
708 if (i != 0 && i != ',' && i != ':') {
709 nasm_error(ERR_NONFATAL, "comma, colon or end of line expected");
710 recover = true;
711 } else if (i == ':') {
712 result->oprs[operand].type |= COLON;
715 if (recover) {
716 do { /* error recovery */
717 i = stdscan(NULL, &tokval);
718 } while (i != 0 && i != ',');
722 * now convert the exprs returned from evaluate()
723 * into operand descriptions...
726 if (mref) { /* it's a memory reference */
727 expr *e = value;
728 int b, i, s; /* basereg, indexreg, scale */
729 int64_t o; /* offset */
731 b = i = -1, o = s = 0;
732 result->oprs[operand].hintbase = hints.base;
733 result->oprs[operand].hinttype = hints.type;
735 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
736 if (e->value == 1) /* in fact it can be basereg */
737 b = e->type;
738 else /* no, it has to be indexreg */
739 i = e->type, s = e->value;
740 e++;
742 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
743 if (b != -1) /* If the first was the base, ... */
744 i = e->type, s = e->value; /* second has to be indexreg */
746 else if (e->value != 1) { /* If both want to be index */
747 nasm_error(ERR_NONFATAL,
748 "beroset-p-592-invalid effective address");
749 result->opcode = I_none;
750 return result;
751 } else
752 b = e->type;
753 e++;
755 if (e->type != 0) { /* is there an offset? */
756 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
757 nasm_error(ERR_NONFATAL,
758 "beroset-p-603-invalid effective address");
759 result->opcode = I_none;
760 return result;
761 } else {
762 if (e->type == EXPR_UNKNOWN) {
763 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
764 o = 0; /* doesn't matter what */
765 result->oprs[operand].wrt = NO_SEG; /* nor this */
766 result->oprs[operand].segment = NO_SEG; /* or this */
767 while (e->type)
768 e++; /* go to the end of the line */
769 } else {
770 if (e->type == EXPR_SIMPLE) {
771 o = e->value;
772 e++;
774 if (e->type == EXPR_WRT) {
775 result->oprs[operand].wrt = e->value;
776 e++;
777 } else
778 result->oprs[operand].wrt = NO_SEG;
780 * Look for a segment base type.
782 if (e->type && e->type < EXPR_SEGBASE) {
783 nasm_error(ERR_NONFATAL,
784 "beroset-p-630-invalid effective address");
785 result->opcode = I_none;
786 return result;
788 while (e->type && e->value == 0)
789 e++;
790 if (e->type && e->value != 1) {
791 nasm_error(ERR_NONFATAL,
792 "beroset-p-637-invalid effective address");
793 result->opcode = I_none;
794 return result;
796 if (e->type) {
797 result->oprs[operand].segment =
798 e->type - EXPR_SEGBASE;
799 e++;
800 } else
801 result->oprs[operand].segment = NO_SEG;
802 while (e->type && e->value == 0)
803 e++;
804 if (e->type) {
805 nasm_error(ERR_NONFATAL,
806 "beroset-p-650-invalid effective address");
807 result->opcode = I_none;
808 return result;
812 } else {
813 o = 0;
814 result->oprs[operand].wrt = NO_SEG;
815 result->oprs[operand].segment = NO_SEG;
818 if (e->type != 0) { /* there'd better be nothing left! */
819 nasm_error(ERR_NONFATAL,
820 "beroset-p-663-invalid effective address");
821 result->opcode = I_none;
822 return result;
825 /* It is memory, but it can match any r/m operand */
826 result->oprs[operand].type |= MEMORY_ANY;
828 if (b == -1 && (i == -1 || s == 0)) {
829 int is_rel = globalbits == 64 &&
830 !(result->oprs[operand].eaflags & EAF_ABS) &&
831 ((globalrel &&
832 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
833 (result->oprs[operand].eaflags & EAF_REL));
835 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
837 result->oprs[operand].basereg = b;
838 result->oprs[operand].indexreg = i;
839 result->oprs[operand].scale = s;
840 result->oprs[operand].offset = o;
841 } else { /* it's not a memory reference */
842 if (is_just_unknown(value)) { /* it's immediate but unknown */
843 result->oprs[operand].type |= IMMEDIATE;
844 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
845 result->oprs[operand].offset = 0; /* don't care */
846 result->oprs[operand].segment = NO_SEG; /* don't care again */
847 result->oprs[operand].wrt = NO_SEG; /* still don't care */
849 if(optimizing >= 0 && !(result->oprs[operand].type & STRICT)) {
850 /* Be optimistic */
851 result->oprs[operand].type |=
852 SBYTE16 | SBYTE32 | SBYTE64 | UDWORD64 | SDWORD64;
854 } else if (is_reloc(value)) { /* it's immediate */
855 result->oprs[operand].type |= IMMEDIATE;
856 result->oprs[operand].offset = reloc_value(value);
857 result->oprs[operand].segment = reloc_seg(value);
858 result->oprs[operand].wrt = reloc_wrt(value);
860 if (is_simple(value)) {
861 if (reloc_value(value) == 1)
862 result->oprs[operand].type |= UNITY;
863 if (optimizing >= 0 &&
864 !(result->oprs[operand].type & STRICT)) {
865 int64_t v64 = reloc_value(value);
866 int32_t v32 = (int32_t)v64;
867 int16_t v16 = (int16_t)v32;
869 if (v64 >= -128 && v64 <= 127)
870 result->oprs[operand].type |= SBYTE64;
871 if (v32 >= -128 && v32 <= 127)
872 result->oprs[operand].type |= SBYTE32;
873 if (v16 >= -128 && v16 <= 127)
874 result->oprs[operand].type |= SBYTE16;
875 if ((uint64_t)v64 <= UINT64_C(0xffffffff))
876 result->oprs[operand].type |= UDWORD64;
877 if (v64 >= -INT64_C(0x80000000) &&
878 v64 <= INT64_C(0x7fffffff))
879 result->oprs[operand].type |= SDWORD64;
882 } else { /* it's a register */
883 unsigned int rs;
885 if (value->type >= EXPR_SIMPLE || value->value != 1) {
886 nasm_error(ERR_NONFATAL, "invalid operand type");
887 result->opcode = I_none;
888 return result;
892 * check that its only 1 register, not an expression...
894 for (i = 1; value[i].type; i++)
895 if (value[i].value) {
896 nasm_error(ERR_NONFATAL, "invalid operand type");
897 result->opcode = I_none;
898 return result;
901 /* clear overrides, except TO which applies to FPU regs */
902 if (result->oprs[operand].type & ~TO) {
904 * we want to produce a warning iff the specified size
905 * is different from the register size
907 rs = result->oprs[operand].type & SIZE_MASK;
908 } else
909 rs = 0;
911 result->oprs[operand].type &= TO;
912 result->oprs[operand].type |= REGISTER;
913 result->oprs[operand].type |= nasm_reg_flags[value->type];
914 result->oprs[operand].basereg = value->type;
916 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
917 nasm_error(ERR_WARNING | ERR_PASS1,
918 "register size specification ignored");
923 result->operands = operand; /* set operand count */
925 /* clear remaining operands */
926 while (operand < MAX_OPERANDS)
927 result->oprs[operand++].type = 0;
930 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
932 switch (result->opcode) {
933 case I_RESW:
934 result->opcode = I_RESB;
935 result->oprs[0].offset *= 2;
936 break;
937 case I_RESD:
938 result->opcode = I_RESB;
939 result->oprs[0].offset *= 4;
940 break;
941 case I_RESQ:
942 result->opcode = I_RESB;
943 result->oprs[0].offset *= 8;
944 break;
945 case I_REST:
946 result->opcode = I_RESB;
947 result->oprs[0].offset *= 10;
948 break;
949 case I_RESO:
950 result->opcode = I_RESB;
951 result->oprs[0].offset *= 16;
952 break;
953 case I_RESY:
954 result->opcode = I_RESB;
955 result->oprs[0].offset *= 32;
956 break;
957 default:
958 break;
961 return result;
964 static int is_comma_next(void)
966 struct tokenval tv;
967 char *p;
968 int i;
970 p = stdscan_get();
971 i = stdscan(NULL, &tv);
972 stdscan_set(p);
974 return (i == ',' || i == ';' || !i);
977 void cleanup_insn(insn * i)
979 extop *e;
981 while ((e = i->eops)) {
982 i->eops = e->next;
983 if (e->type == EOT_DB_STRING_FREE)
984 nasm_free(e->stringval);
985 nasm_free(e);