coff: Better handling of section redefinition
[nasm.git] / parser.c
blobafc422a9b2ba353fb68ac78dbf7d477305a7c15b
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2013 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;
205 restart_parse:
206 first = true;
207 result->forw_ref = false;
209 stdscan_reset();
210 stdscan_set(buffer);
211 i = stdscan(NULL, &tokval);
213 result->label = NULL; /* Assume no label */
214 result->eops = NULL; /* must do this, whatever happens */
215 result->operands = 0; /* must initialize this */
217 /* Ignore blank lines */
218 if (i == TOKEN_EOS) {
219 result->opcode = I_none;
220 return result;
223 if (i != TOKEN_ID &&
224 i != TOKEN_INSN &&
225 i != TOKEN_PREFIX &&
226 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
227 nasm_error(ERR_NONFATAL,
228 "label or instruction expected at start of line");
229 result->opcode = I_none;
230 return result;
233 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
234 /* there's a label here */
235 first = false;
236 result->label = tokval.t_charptr;
237 i = stdscan(NULL, &tokval);
238 if (i == ':') { /* skip over the optional colon */
239 i = stdscan(NULL, &tokval);
240 } else if (i == 0) {
241 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
242 "label alone on a line without a colon might be in error");
244 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
246 * FIXME: location->segment could be NO_SEG, in which case
247 * it is possible we should be passing 'abs_seg'. Look into this.
248 * Work out whether that is *really* what we should be doing.
249 * Generally fix things. I think this is right as it is, but
250 * am still not certain.
252 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
253 location->offset, NULL, true, false);
257 /* Just a label here */
258 if (i == TOKEN_EOS) {
259 result->opcode = I_none;
260 return result;
263 nasm_build_assert(P_none != 0);
264 memset(result->prefixes, P_none, sizeof(result->prefixes));
265 result->times = 1L;
267 while (i == TOKEN_PREFIX ||
268 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
269 first = false;
272 * Handle special case: the TIMES prefix.
274 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
275 expr *value;
277 i = stdscan(NULL, &tokval);
278 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
279 i = tokval.t_type;
280 if (!value) { /* but, error in evaluator */
281 result->opcode = I_none; /* unrecoverable parse error: */
282 return result; /* ignore this instruction */
284 if (!is_simple(value)) {
285 nasm_error(ERR_NONFATAL,
286 "non-constant argument supplied to TIMES");
287 result->times = 1L;
288 } else {
289 result->times = value->value;
290 if (value->value < 0 && pass0 == 2) {
291 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
292 value->value);
293 result->times = 0;
296 } else {
297 int slot = prefix_slot(tokval.t_integer);
298 if (result->prefixes[slot]) {
299 if (result->prefixes[slot] == tokval.t_integer)
300 nasm_error(ERR_WARNING | ERR_PASS1,
301 "instruction has redundant prefixes");
302 else
303 nasm_error(ERR_NONFATAL,
304 "instruction has conflicting prefixes");
306 result->prefixes[slot] = tokval.t_integer;
307 i = stdscan(NULL, &tokval);
311 if (i != TOKEN_INSN) {
312 int j;
313 enum prefixes pfx;
315 for (j = 0; j < MAXPREFIX; j++) {
316 if ((pfx = result->prefixes[j]) != P_none)
317 break;
320 if (i == 0 && pfx != P_none) {
322 * Instruction prefixes are present, but no actual
323 * instruction. This is allowed: at this point we
324 * invent a notional instruction of RESB 0.
326 result->opcode = I_RESB;
327 result->operands = 1;
328 result->oprs[0].type = IMMEDIATE;
329 result->oprs[0].offset = 0L;
330 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
331 return result;
332 } else {
333 nasm_error(ERR_NONFATAL, "parser: instruction expected");
334 result->opcode = I_none;
335 return result;
339 result->opcode = tokval.t_integer;
340 result->condition = tokval.t_inttwo;
343 * INCBIN cannot be satisfied with incorrectly
344 * evaluated operands, since the correct values _must_ be known
345 * on the first pass. Hence, even in pass one, we set the
346 * `critical' flag on calling evaluate(), so that it will bomb
347 * out on undefined symbols.
349 if (result->opcode == I_INCBIN) {
350 critical = (pass0 < 2 ? 1 : 2);
352 } else
353 critical = (pass == 2 ? 2 : 0);
355 if (result->opcode == I_DB || result->opcode == I_DW ||
356 result->opcode == I_DD || result->opcode == I_DQ ||
357 result->opcode == I_DT || result->opcode == I_DO ||
358 result->opcode == I_DY || result->opcode == I_INCBIN) {
359 extop *eop, **tail = &result->eops, **fixptr;
360 int oper_num = 0;
361 int32_t sign;
363 result->eops_float = false;
366 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
368 while (1) {
369 i = stdscan(NULL, &tokval);
370 if (i == TOKEN_EOS)
371 break;
372 else if (first && i == ':') {
373 insn_is_label = true;
374 goto restart_parse;
376 first = false;
377 fixptr = tail;
378 eop = *tail = nasm_malloc(sizeof(extop));
379 tail = &eop->next;
380 eop->next = NULL;
381 eop->type = EOT_NOTHING;
382 oper_num++;
383 sign = +1;
386 * is_comma_next() here is to distinguish this from
387 * a string used as part of an expression...
389 if (i == TOKEN_STR && is_comma_next()) {
390 eop->type = EOT_DB_STRING;
391 eop->stringval = tokval.t_charptr;
392 eop->stringlen = tokval.t_inttwo;
393 i = stdscan(NULL, &tokval); /* eat the comma */
394 } else if (i == TOKEN_STRFUNC) {
395 bool parens = false;
396 const char *funcname = tokval.t_charptr;
397 enum strfunc func = tokval.t_integer;
398 i = stdscan(NULL, &tokval);
399 if (i == '(') {
400 parens = true;
401 i = stdscan(NULL, &tokval);
403 if (i != TOKEN_STR) {
404 nasm_error(ERR_NONFATAL,
405 "%s must be followed by a string constant",
406 funcname);
407 eop->type = EOT_NOTHING;
408 } else {
409 eop->type = EOT_DB_STRING_FREE;
410 eop->stringlen =
411 string_transform(tokval.t_charptr, tokval.t_inttwo,
412 &eop->stringval, func);
413 if (eop->stringlen == (size_t)-1) {
414 nasm_error(ERR_NONFATAL, "invalid string for transform");
415 eop->type = EOT_NOTHING;
418 if (parens && i && i != ')') {
419 i = stdscan(NULL, &tokval);
420 if (i != ')') {
421 nasm_error(ERR_NONFATAL, "unterminated %s function",
422 funcname);
425 if (i && i != ',')
426 i = stdscan(NULL, &tokval);
427 } else if (i == '-' || i == '+') {
428 char *save = stdscan_get();
429 int token = i;
430 sign = (i == '-') ? -1 : 1;
431 i = stdscan(NULL, &tokval);
432 if (i != TOKEN_FLOAT) {
433 stdscan_set(save);
434 i = tokval.t_type = token;
435 goto is_expression;
436 } else {
437 goto is_float;
439 } else if (i == TOKEN_FLOAT) {
440 is_float:
441 eop->type = EOT_DB_STRING;
442 result->eops_float = true;
444 eop->stringlen = idata_bytes(result->opcode);
445 if (eop->stringlen > 16) {
446 nasm_error(ERR_NONFATAL, "floating-point constant"
447 " encountered in DY instruction");
448 eop->stringlen = 0;
449 } else if (eop->stringlen < 1) {
450 nasm_error(ERR_NONFATAL, "floating-point constant"
451 " encountered in unknown instruction");
453 * fix suggested by Pedro Gimeno... original line was:
454 * eop->type = EOT_NOTHING;
456 eop->stringlen = 0;
459 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
460 tail = &eop->next;
461 *fixptr = eop;
462 eop->stringval = (char *)eop + sizeof(extop);
463 if (!eop->stringlen ||
464 !float_const(tokval.t_charptr, sign,
465 (uint8_t *)eop->stringval,
466 eop->stringlen, nasm_error))
467 eop->type = EOT_NOTHING;
468 i = stdscan(NULL, &tokval); /* eat the comma */
469 } else {
470 /* anything else, assume it is an expression */
471 expr *value;
473 is_expression:
474 value = evaluate(stdscan, NULL, &tokval, NULL,
475 critical, nasm_error, NULL);
476 i = tokval.t_type;
477 if (!value) { /* error in evaluator */
478 result->opcode = I_none; /* unrecoverable parse error: */
479 return result; /* ignore this instruction */
481 if (is_unknown(value)) {
482 eop->type = EOT_DB_NUMBER;
483 eop->offset = 0; /* doesn't matter what we put */
484 eop->segment = eop->wrt = NO_SEG; /* likewise */
485 } else if (is_reloc(value)) {
486 eop->type = EOT_DB_NUMBER;
487 eop->offset = reloc_value(value);
488 eop->segment = reloc_seg(value);
489 eop->wrt = reloc_wrt(value);
490 } else {
491 nasm_error(ERR_NONFATAL,
492 "operand %d: expression is not simple"
493 " or relocatable", oper_num);
498 * We're about to call stdscan(), which will eat the
499 * comma that we're currently sitting on between
500 * arguments. However, we'd better check first that it
501 * _is_ a comma.
503 if (i == TOKEN_EOS) /* also could be EOL */
504 break;
505 if (i != ',') {
506 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
507 oper_num);
508 result->opcode = I_none;/* unrecoverable parse error: */
509 return result; /* ignore this instruction */
513 if (result->opcode == I_INCBIN) {
515 * Correct syntax for INCBIN is that there should be
516 * one string operand, followed by one or two numeric
517 * operands.
519 if (!result->eops || result->eops->type != EOT_DB_STRING)
520 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
521 else if (result->eops->next &&
522 result->eops->next->type != EOT_DB_NUMBER)
523 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
524 " non-numeric");
525 else if (result->eops->next && result->eops->next->next &&
526 result->eops->next->next->type != EOT_DB_NUMBER)
527 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
528 " non-numeric");
529 else if (result->eops->next && result->eops->next->next &&
530 result->eops->next->next->next)
531 nasm_error(ERR_NONFATAL,
532 "`incbin': more than three parameters");
533 else
534 return result;
536 * If we reach here, one of the above errors happened.
537 * Throw the instruction away.
539 result->opcode = I_none;
540 return result;
541 } else /* DB ... */ if (oper_num == 0)
542 nasm_error(ERR_WARNING | ERR_PASS1,
543 "no operand for data declaration");
544 else
545 result->operands = oper_num;
547 return result;
551 * Now we begin to parse the operands. There may be up to four
552 * of these, separated by commas, and terminated by a zero token.
555 for (operand = 0; operand < MAX_OPERANDS; operand++) {
556 expr *value; /* used most of the time */
557 int mref; /* is this going to be a memory ref? */
558 int bracket; /* is it a [] mref, or a & mref? */
559 int setsize = 0;
561 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
562 result->oprs[operand].eaflags = 0; /* and this */
563 result->oprs[operand].opflags = 0;
565 i = stdscan(NULL, &tokval);
566 if (i == TOKEN_EOS)
567 break; /* end of operands: get out of here */
568 else if (first && i == ':') {
569 insn_is_label = true;
570 goto restart_parse;
572 first = false;
573 result->oprs[operand].type = 0; /* so far, no override */
574 while (i == TOKEN_SPECIAL) { /* size specifiers */
575 switch ((int)tokval.t_integer) {
576 case S_BYTE:
577 if (!setsize) /* we want to use only the first */
578 result->oprs[operand].type |= BITS8;
579 setsize = 1;
580 break;
581 case S_WORD:
582 if (!setsize)
583 result->oprs[operand].type |= BITS16;
584 setsize = 1;
585 break;
586 case S_DWORD:
587 case S_LONG:
588 if (!setsize)
589 result->oprs[operand].type |= BITS32;
590 setsize = 1;
591 break;
592 case S_QWORD:
593 if (!setsize)
594 result->oprs[operand].type |= BITS64;
595 setsize = 1;
596 break;
597 case S_TWORD:
598 if (!setsize)
599 result->oprs[operand].type |= BITS80;
600 setsize = 1;
601 break;
602 case S_OWORD:
603 if (!setsize)
604 result->oprs[operand].type |= BITS128;
605 setsize = 1;
606 break;
607 case S_YWORD:
608 if (!setsize)
609 result->oprs[operand].type |= BITS256;
610 setsize = 1;
611 break;
612 case S_TO:
613 result->oprs[operand].type |= TO;
614 break;
615 case S_STRICT:
616 result->oprs[operand].type |= STRICT;
617 break;
618 case S_FAR:
619 result->oprs[operand].type |= FAR;
620 break;
621 case S_NEAR:
622 result->oprs[operand].type |= NEAR;
623 break;
624 case S_SHORT:
625 result->oprs[operand].type |= SHORT;
626 break;
627 default:
628 nasm_error(ERR_NONFATAL, "invalid operand size specification");
630 i = stdscan(NULL, &tokval);
633 if (i == '[' || i == '&') { /* memory reference */
634 mref = true;
635 bracket = (i == '[');
636 i = stdscan(NULL, &tokval); /* then skip the colon */
637 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
638 process_size_override(result, operand);
639 i = stdscan(NULL, &tokval);
641 } else { /* immediate operand, or register */
642 mref = false;
643 bracket = false; /* placate optimisers */
646 if ((result->oprs[operand].type & FAR) && !mref &&
647 result->opcode != I_JMP && result->opcode != I_CALL) {
648 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
651 value = evaluate(stdscan, NULL, &tokval,
652 &result->oprs[operand].opflags,
653 critical, nasm_error, &hints);
654 i = tokval.t_type;
655 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
656 result->forw_ref = true;
658 if (!value) { /* nasm_error in evaluator */
659 result->opcode = I_none; /* unrecoverable parse error: */
660 return result; /* ignore this instruction */
662 if (i == ':' && mref) { /* it was seg:offset */
664 * Process the segment override.
666 if (value[1].type != 0 ||
667 value->value != 1 ||
668 !IS_SREG(value->type))
669 nasm_error(ERR_NONFATAL, "invalid segment override");
670 else if (result->prefixes[PPS_SEG])
671 nasm_error(ERR_NONFATAL,
672 "instruction has conflicting segment overrides");
673 else {
674 result->prefixes[PPS_SEG] = value->type;
675 if (IS_FSGS(value->type))
676 result->oprs[operand].eaflags |= EAF_FSGS;
679 i = stdscan(NULL, &tokval); /* then skip the colon */
680 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
681 process_size_override(result, operand);
682 i = stdscan(NULL, &tokval);
684 value = evaluate(stdscan, NULL, &tokval,
685 &result->oprs[operand].opflags,
686 critical, nasm_error, &hints);
687 i = tokval.t_type;
688 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
689 result->forw_ref = true;
691 /* and get the offset */
692 if (!value) { /* but, error in evaluator */
693 result->opcode = I_none; /* unrecoverable parse error: */
694 return result; /* ignore this instruction */
698 recover = false;
699 if (mref && bracket) { /* find ] at the end */
700 if (i != ']') {
701 nasm_error(ERR_NONFATAL, "parser: expecting ]");
702 recover = true;
703 } else { /* we got the required ] */
704 i = stdscan(NULL, &tokval);
705 if (i != 0 && i != ',') {
706 nasm_error(ERR_NONFATAL, "comma or end of line expected");
707 recover = true;
710 } else { /* immediate operand */
711 if (i != 0 && i != ',' && i != ':') {
712 nasm_error(ERR_NONFATAL, "comma, colon or end of line expected");
713 recover = true;
714 } else if (i == ':') {
715 result->oprs[operand].type |= COLON;
718 if (recover) {
719 do { /* error recovery */
720 i = stdscan(NULL, &tokval);
721 } while (i != 0 && i != ',');
725 * now convert the exprs returned from evaluate()
726 * into operand descriptions...
729 if (mref) { /* it's a memory reference */
730 expr *e = value;
731 int b, i, s; /* basereg, indexreg, scale */
732 int64_t o; /* offset */
734 b = i = -1, o = s = 0;
735 result->oprs[operand].hintbase = hints.base;
736 result->oprs[operand].hinttype = hints.type;
738 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
739 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
741 if (is_gpr && e->value == 1)
742 b = e->type; /* It can be basereg */
743 else /* No, it has to be indexreg */
744 i = e->type, s = e->value;
745 e++;
747 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
748 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
750 if (b != -1) /* If the first was the base, ... */
751 i = e->type, s = e->value; /* second has to be indexreg */
753 else if (!is_gpr || e->value != 1) {
754 /* If both want to be index */
755 nasm_error(ERR_NONFATAL,
756 "invalid effective address: two index registers");
757 result->opcode = I_none;
758 return result;
759 } else
760 b = e->type;
761 e++;
763 if (e->type != 0) { /* is there an offset? */
764 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
765 nasm_error(ERR_NONFATAL,
766 "beroset-p-603-invalid effective address");
767 result->opcode = I_none;
768 return result;
769 } else {
770 if (e->type == EXPR_UNKNOWN) {
771 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
772 o = 0; /* doesn't matter what */
773 result->oprs[operand].wrt = NO_SEG; /* nor this */
774 result->oprs[operand].segment = NO_SEG; /* or this */
775 while (e->type)
776 e++; /* go to the end of the line */
777 } else {
778 if (e->type == EXPR_SIMPLE) {
779 o = e->value;
780 e++;
782 if (e->type == EXPR_WRT) {
783 result->oprs[operand].wrt = e->value;
784 e++;
785 } else
786 result->oprs[operand].wrt = NO_SEG;
788 * Look for a segment base type.
790 if (e->type && e->type < EXPR_SEGBASE) {
791 nasm_error(ERR_NONFATAL,
792 "beroset-p-630-invalid effective address");
793 result->opcode = I_none;
794 return result;
796 while (e->type && e->value == 0)
797 e++;
798 if (e->type && e->value != 1) {
799 nasm_error(ERR_NONFATAL,
800 "beroset-p-637-invalid effective address");
801 result->opcode = I_none;
802 return result;
804 if (e->type) {
805 result->oprs[operand].segment =
806 e->type - EXPR_SEGBASE;
807 e++;
808 } else
809 result->oprs[operand].segment = NO_SEG;
810 while (e->type && e->value == 0)
811 e++;
812 if (e->type) {
813 nasm_error(ERR_NONFATAL,
814 "beroset-p-650-invalid effective address");
815 result->opcode = I_none;
816 return result;
820 } else {
821 o = 0;
822 result->oprs[operand].wrt = NO_SEG;
823 result->oprs[operand].segment = NO_SEG;
826 if (e->type != 0) { /* there'd better be nothing left! */
827 nasm_error(ERR_NONFATAL,
828 "beroset-p-663-invalid effective address");
829 result->opcode = I_none;
830 return result;
833 /* It is memory, but it can match any r/m operand */
834 result->oprs[operand].type |= MEMORY_ANY;
836 if (b == -1 && (i == -1 || s == 0)) {
837 int is_rel = globalbits == 64 &&
838 !(result->oprs[operand].eaflags & EAF_ABS) &&
839 ((globalrel &&
840 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
841 (result->oprs[operand].eaflags & EAF_REL));
843 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
846 if (i != -1) {
847 opflags_t iclass = nasm_reg_flags[i];
849 if (is_class(XMMREG,iclass))
850 result->oprs[operand].type |= XMEM;
851 else if (is_class(YMMREG,iclass))
852 result->oprs[operand].type |= YMEM;
855 result->oprs[operand].basereg = b;
856 result->oprs[operand].indexreg = i;
857 result->oprs[operand].scale = s;
858 result->oprs[operand].offset = o;
859 } else { /* it's not a memory reference */
860 if (is_just_unknown(value)) { /* it's immediate but unknown */
861 result->oprs[operand].type |= IMMEDIATE;
862 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
863 result->oprs[operand].offset = 0; /* don't care */
864 result->oprs[operand].segment = NO_SEG; /* don't care again */
865 result->oprs[operand].wrt = NO_SEG; /* still don't care */
867 if(optimizing >= 0 && !(result->oprs[operand].type & STRICT)) {
868 /* Be optimistic */
869 result->oprs[operand].type |=
870 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
872 } else if (is_reloc(value)) { /* it's immediate */
873 result->oprs[operand].type |= IMMEDIATE;
874 result->oprs[operand].offset = reloc_value(value);
875 result->oprs[operand].segment = reloc_seg(value);
876 result->oprs[operand].wrt = reloc_wrt(value);
878 if (is_simple(value)) {
879 uint64_t n = reloc_value(value);
880 if (n == 1)
881 result->oprs[operand].type |= UNITY;
882 if (optimizing >= 0 &&
883 !(result->oprs[operand].type & STRICT)) {
884 if ((uint32_t) (n + 128) <= 255)
885 result->oprs[operand].type |= SBYTEDWORD;
886 if ((uint16_t) (n + 128) <= 255)
887 result->oprs[operand].type |= SBYTEWORD;
888 if (n <= 0xFFFFFFFF)
889 result->oprs[operand].type |= UDWORD;
890 if (n + 0x80000000 <= 0xFFFFFFFF)
891 result->oprs[operand].type |= SDWORD;
894 } else { /* it's a register */
895 opflags_t rs;
897 if (value->type >= EXPR_SIMPLE || value->value != 1) {
898 nasm_error(ERR_NONFATAL, "invalid operand type");
899 result->opcode = I_none;
900 return result;
904 * check that its only 1 register, not an expression...
906 for (i = 1; value[i].type; i++)
907 if (value[i].value) {
908 nasm_error(ERR_NONFATAL, "invalid operand type");
909 result->opcode = I_none;
910 return result;
913 /* clear overrides, except TO which applies to FPU regs */
914 if (result->oprs[operand].type & ~TO) {
916 * we want to produce a warning iff the specified size
917 * is different from the register size
919 rs = result->oprs[operand].type & SIZE_MASK;
920 } else
921 rs = 0;
923 result->oprs[operand].type &= TO;
924 result->oprs[operand].type |= REGISTER;
925 result->oprs[operand].type |= nasm_reg_flags[value->type];
926 result->oprs[operand].basereg = value->type;
928 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
929 nasm_error(ERR_WARNING | ERR_PASS1,
930 "register size specification ignored");
935 result->operands = operand; /* set operand count */
937 /* clear remaining operands */
938 while (operand < MAX_OPERANDS)
939 result->oprs[operand++].type = 0;
942 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
944 switch (result->opcode) {
945 case I_RESW:
946 result->opcode = I_RESB;
947 result->oprs[0].offset *= 2;
948 break;
949 case I_RESD:
950 result->opcode = I_RESB;
951 result->oprs[0].offset *= 4;
952 break;
953 case I_RESQ:
954 result->opcode = I_RESB;
955 result->oprs[0].offset *= 8;
956 break;
957 case I_REST:
958 result->opcode = I_RESB;
959 result->oprs[0].offset *= 10;
960 break;
961 case I_RESO:
962 result->opcode = I_RESB;
963 result->oprs[0].offset *= 16;
964 break;
965 case I_RESY:
966 result->opcode = I_RESB;
967 result->oprs[0].offset *= 32;
968 break;
969 default:
970 break;
973 return result;
976 static int is_comma_next(void)
978 struct tokenval tv;
979 char *p;
980 int i;
982 p = stdscan_get();
983 i = stdscan(NULL, &tv);
984 stdscan_set(p);
986 return (i == ',' || i == ';' || !i);
989 void cleanup_insn(insn * i)
991 extop *e;
993 while ((e = i->eops)) {
994 i->eops = e->next;
995 if (e->type == EOT_DB_STRING_FREE)
996 nasm_free(e->stringval);
997 nasm_free(e);