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
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
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);
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
)
71 static int prefix_slot(int prefix
)
104 nasm_error(ERR_PANIC
, "Invalid value %d passed to prefix_slot()", prefix
);
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.
126 result
->oprs
[operand
].type
|= BITS8
;
129 result
->oprs
[operand
].type
|= BITS16
;
133 result
->oprs
[operand
].type
|= BITS32
;
136 result
->oprs
[operand
].type
|= BITS64
;
139 result
->oprs
[operand
].type
|= BITS80
;
142 result
->oprs
[operand
].type
|= BITS128
;
145 nasm_error(ERR_NONFATAL
,
146 "invalid operand size specification");
150 /* Standard NASM compatible syntax */
151 switch ((int)tokval
.t_integer
) {
153 result
->oprs
[operand
].eaflags
|= EAF_TIMESTWO
;
156 result
->oprs
[operand
].eaflags
|= EAF_REL
;
159 result
->oprs
[operand
].eaflags
|= EAF_ABS
;
162 result
->oprs
[operand
].disp_size
= 8;
163 result
->oprs
[operand
].eaflags
|= EAF_BYTEOFFS
;
168 if (result
->prefixes
[PPS_ASIZE
] &&
169 result
->prefixes
[PPS_ASIZE
] != tokval
.t_integer
)
170 nasm_error(ERR_NONFATAL
,
171 "conflicting address size specifications");
173 result
->prefixes
[PPS_ASIZE
] = tokval
.t_integer
;
176 result
->oprs
[operand
].disp_size
= 16;
177 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
181 result
->oprs
[operand
].disp_size
= 32;
182 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
185 result
->oprs
[operand
].disp_size
= 64;
186 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
189 nasm_error(ERR_NONFATAL
, "invalid size specification in"
190 " effective address");
197 * when two or more decorators follow a register operand,
198 * consecutive decorators are parsed here.
199 * opmask and zeroing decorators can be placed in any order.
200 * e.g. zmm1 {k2}{z} or zmm2 {z,k3}
201 * decorator(s) are placed at the end of an operand.
203 static bool parse_braces(decoflags_t
*decoflags
)
206 bool recover
= false;
210 if (i
== TOKEN_OPMASK
) {
211 if (*decoflags
& OPMASK_MASK
) {
212 nasm_error(ERR_NONFATAL
, "opmask k%lu is already set",
213 *decoflags
& OPMASK_MASK
);
214 *decoflags
&= ~OPMASK_MASK
;
216 *decoflags
|= VAL_OPMASK(nasm_regvals
[tokval
.t_integer
]);
217 } else if (i
== TOKEN_DECORATOR
) {
218 switch (tokval
.t_integer
) {
221 * according to AVX512 spec, only zeroing/merging decorator
222 * is supported with opmask
224 *decoflags
|= GEN_Z(0);
227 nasm_error(ERR_NONFATAL
, "{%s} is not an expected decorator",
231 } else if (i
== ',' || i
== TOKEN_EOS
){
234 nasm_error(ERR_NONFATAL
, "only a series of valid decorators"
239 i
= stdscan(NULL
, &tokval
);
245 insn
*parse_line(int pass
, char *buffer
, insn
*result
, ldfunc ldef
)
247 bool insn_is_label
= false;
248 struct eval_hints hints
;
256 result
->forw_ref
= false;
260 i
= stdscan(NULL
, &tokval
);
262 result
->label
= NULL
; /* Assume no label */
263 result
->eops
= NULL
; /* must do this, whatever happens */
264 result
->operands
= 0; /* must initialize this */
266 /* Ignore blank lines */
267 if (i
== TOKEN_EOS
) {
268 result
->opcode
= I_none
;
275 (i
!= TOKEN_REG
|| !IS_SREG(tokval
.t_integer
))) {
276 nasm_error(ERR_NONFATAL
,
277 "label or instruction expected at start of line");
278 result
->opcode
= I_none
;
282 if (i
== TOKEN_ID
|| (insn_is_label
&& i
== TOKEN_INSN
)) {
283 /* there's a label here */
285 result
->label
= tokval
.t_charptr
;
286 i
= stdscan(NULL
, &tokval
);
287 if (i
== ':') { /* skip over the optional colon */
288 i
= stdscan(NULL
, &tokval
);
290 nasm_error(ERR_WARNING
| ERR_WARN_OL
| ERR_PASS1
,
291 "label alone on a line without a colon might be in error");
293 if (i
!= TOKEN_INSN
|| tokval
.t_integer
!= I_EQU
) {
295 * FIXME: location->segment could be NO_SEG, in which case
296 * it is possible we should be passing 'abs_seg'. Look into this.
297 * Work out whether that is *really* what we should be doing.
298 * Generally fix things. I think this is right as it is, but
299 * am still not certain.
301 ldef(result
->label
, in_abs_seg
? abs_seg
: location
->segment
,
302 location
->offset
, NULL
, true, false);
306 /* Just a label here */
307 if (i
== TOKEN_EOS
) {
308 result
->opcode
= I_none
;
312 nasm_build_assert(P_none
!= 0);
313 memset(result
->prefixes
, P_none
, sizeof(result
->prefixes
));
316 while (i
== TOKEN_PREFIX
||
317 (i
== TOKEN_REG
&& IS_SREG(tokval
.t_integer
))) {
321 * Handle special case: the TIMES prefix.
323 if (i
== TOKEN_PREFIX
&& tokval
.t_integer
== P_TIMES
) {
326 i
= stdscan(NULL
, &tokval
);
327 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass0
, nasm_error
, NULL
);
329 if (!value
) { /* but, error in evaluator */
330 result
->opcode
= I_none
; /* unrecoverable parse error: */
331 return result
; /* ignore this instruction */
333 if (!is_simple(value
)) {
334 nasm_error(ERR_NONFATAL
,
335 "non-constant argument supplied to TIMES");
338 result
->times
= value
->value
;
339 if (value
->value
< 0 && pass0
== 2) {
340 nasm_error(ERR_NONFATAL
, "TIMES value %"PRId64
" is negative",
346 int slot
= prefix_slot(tokval
.t_integer
);
347 if (result
->prefixes
[slot
]) {
348 if (result
->prefixes
[slot
] == tokval
.t_integer
)
349 nasm_error(ERR_WARNING
| ERR_PASS1
,
350 "instruction has redundant prefixes");
352 nasm_error(ERR_NONFATAL
,
353 "instruction has conflicting prefixes");
355 result
->prefixes
[slot
] = tokval
.t_integer
;
356 i
= stdscan(NULL
, &tokval
);
360 if (i
!= TOKEN_INSN
) {
364 for (j
= 0; j
< MAXPREFIX
; j
++) {
365 if ((pfx
= result
->prefixes
[j
]) != P_none
)
369 if (i
== 0 && pfx
!= P_none
) {
371 * Instruction prefixes are present, but no actual
372 * instruction. This is allowed: at this point we
373 * invent a notional instruction of RESB 0.
375 result
->opcode
= I_RESB
;
376 result
->operands
= 1;
377 result
->oprs
[0].type
= IMMEDIATE
;
378 result
->oprs
[0].offset
= 0L;
379 result
->oprs
[0].segment
= result
->oprs
[0].wrt
= NO_SEG
;
382 nasm_error(ERR_NONFATAL
, "parser: instruction expected");
383 result
->opcode
= I_none
;
388 result
->opcode
= tokval
.t_integer
;
389 result
->condition
= tokval
.t_inttwo
;
392 * INCBIN cannot be satisfied with incorrectly
393 * evaluated operands, since the correct values _must_ be known
394 * on the first pass. Hence, even in pass one, we set the
395 * `critical' flag on calling evaluate(), so that it will bomb
396 * out on undefined symbols.
398 if (result
->opcode
== I_INCBIN
) {
399 critical
= (pass0
< 2 ? 1 : 2);
402 critical
= (pass
== 2 ? 2 : 0);
404 if (result
->opcode
== I_DB
|| result
->opcode
== I_DW
||
405 result
->opcode
== I_DD
|| result
->opcode
== I_DQ
||
406 result
->opcode
== I_DT
|| result
->opcode
== I_DO
||
407 result
->opcode
== I_DY
|| result
->opcode
== I_INCBIN
) {
408 extop
*eop
, **tail
= &result
->eops
, **fixptr
;
412 result
->eops_float
= false;
415 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
418 i
= stdscan(NULL
, &tokval
);
421 else if (first
&& i
== ':') {
422 insn_is_label
= true;
427 eop
= *tail
= nasm_malloc(sizeof(extop
));
430 eop
->type
= EOT_NOTHING
;
435 * is_comma_next() here is to distinguish this from
436 * a string used as part of an expression...
438 if (i
== TOKEN_STR
&& is_comma_next()) {
439 eop
->type
= EOT_DB_STRING
;
440 eop
->stringval
= tokval
.t_charptr
;
441 eop
->stringlen
= tokval
.t_inttwo
;
442 i
= stdscan(NULL
, &tokval
); /* eat the comma */
443 } else if (i
== TOKEN_STRFUNC
) {
445 const char *funcname
= tokval
.t_charptr
;
446 enum strfunc func
= tokval
.t_integer
;
447 i
= stdscan(NULL
, &tokval
);
450 i
= stdscan(NULL
, &tokval
);
452 if (i
!= TOKEN_STR
) {
453 nasm_error(ERR_NONFATAL
,
454 "%s must be followed by a string constant",
456 eop
->type
= EOT_NOTHING
;
458 eop
->type
= EOT_DB_STRING_FREE
;
460 string_transform(tokval
.t_charptr
, tokval
.t_inttwo
,
461 &eop
->stringval
, func
);
462 if (eop
->stringlen
== (size_t)-1) {
463 nasm_error(ERR_NONFATAL
, "invalid string for transform");
464 eop
->type
= EOT_NOTHING
;
467 if (parens
&& i
&& i
!= ')') {
468 i
= stdscan(NULL
, &tokval
);
470 nasm_error(ERR_NONFATAL
, "unterminated %s function",
475 i
= stdscan(NULL
, &tokval
);
476 } else if (i
== '-' || i
== '+') {
477 char *save
= stdscan_get();
479 sign
= (i
== '-') ? -1 : 1;
480 i
= stdscan(NULL
, &tokval
);
481 if (i
!= TOKEN_FLOAT
) {
483 i
= tokval
.t_type
= token
;
488 } else if (i
== TOKEN_FLOAT
) {
490 eop
->type
= EOT_DB_STRING
;
491 result
->eops_float
= true;
493 eop
->stringlen
= idata_bytes(result
->opcode
);
494 if (eop
->stringlen
> 16) {
495 nasm_error(ERR_NONFATAL
, "floating-point constant"
496 " encountered in DY instruction");
498 } else if (eop
->stringlen
< 1) {
499 nasm_error(ERR_NONFATAL
, "floating-point constant"
500 " encountered in unknown instruction");
502 * fix suggested by Pedro Gimeno... original line was:
503 * eop->type = EOT_NOTHING;
508 eop
= nasm_realloc(eop
, sizeof(extop
) + eop
->stringlen
);
511 eop
->stringval
= (char *)eop
+ sizeof(extop
);
512 if (!eop
->stringlen
||
513 !float_const(tokval
.t_charptr
, sign
,
514 (uint8_t *)eop
->stringval
,
515 eop
->stringlen
, nasm_error
))
516 eop
->type
= EOT_NOTHING
;
517 i
= stdscan(NULL
, &tokval
); /* eat the comma */
519 /* anything else, assume it is an expression */
523 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
,
524 critical
, nasm_error
, NULL
);
526 if (!value
) { /* error in evaluator */
527 result
->opcode
= I_none
; /* unrecoverable parse error: */
528 return result
; /* ignore this instruction */
530 if (is_unknown(value
)) {
531 eop
->type
= EOT_DB_NUMBER
;
532 eop
->offset
= 0; /* doesn't matter what we put */
533 eop
->segment
= eop
->wrt
= NO_SEG
; /* likewise */
534 } else if (is_reloc(value
)) {
535 eop
->type
= EOT_DB_NUMBER
;
536 eop
->offset
= reloc_value(value
);
537 eop
->segment
= reloc_seg(value
);
538 eop
->wrt
= reloc_wrt(value
);
540 nasm_error(ERR_NONFATAL
,
541 "operand %d: expression is not simple"
542 " or relocatable", oper_num
);
547 * We're about to call stdscan(), which will eat the
548 * comma that we're currently sitting on between
549 * arguments. However, we'd better check first that it
552 if (i
== TOKEN_EOS
) /* also could be EOL */
555 nasm_error(ERR_NONFATAL
, "comma expected after operand %d",
557 result
->opcode
= I_none
;/* unrecoverable parse error: */
558 return result
; /* ignore this instruction */
562 if (result
->opcode
== I_INCBIN
) {
564 * Correct syntax for INCBIN is that there should be
565 * one string operand, followed by one or two numeric
568 if (!result
->eops
|| result
->eops
->type
!= EOT_DB_STRING
)
569 nasm_error(ERR_NONFATAL
, "`incbin' expects a file name");
570 else if (result
->eops
->next
&&
571 result
->eops
->next
->type
!= EOT_DB_NUMBER
)
572 nasm_error(ERR_NONFATAL
, "`incbin': second parameter is"
574 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
575 result
->eops
->next
->next
->type
!= EOT_DB_NUMBER
)
576 nasm_error(ERR_NONFATAL
, "`incbin': third parameter is"
578 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
579 result
->eops
->next
->next
->next
)
580 nasm_error(ERR_NONFATAL
,
581 "`incbin': more than three parameters");
585 * If we reach here, one of the above errors happened.
586 * Throw the instruction away.
588 result
->opcode
= I_none
;
590 } else /* DB ... */ if (oper_num
== 0)
591 nasm_error(ERR_WARNING
| ERR_PASS1
,
592 "no operand for data declaration");
594 result
->operands
= oper_num
;
600 * Now we begin to parse the operands. There may be up to four
601 * of these, separated by commas, and terminated by a zero token.
604 for (operand
= 0; operand
< MAX_OPERANDS
; operand
++) {
605 expr
*value
; /* used most of the time */
606 int mref
; /* is this going to be a memory ref? */
607 int bracket
; /* is it a [] mref, or a & mref? */
609 decoflags_t brace_flags
= 0; /* flags for decorators in braces */
611 result
->oprs
[operand
].disp_size
= 0; /* have to zero this whatever */
612 result
->oprs
[operand
].eaflags
= 0; /* and this */
613 result
->oprs
[operand
].opflags
= 0;
614 result
->oprs
[operand
].decoflags
= 0;
616 i
= stdscan(NULL
, &tokval
);
618 break; /* end of operands: get out of here */
619 else if (first
&& i
== ':') {
620 insn_is_label
= true;
624 result
->oprs
[operand
].type
= 0; /* so far, no override */
625 while (i
== TOKEN_SPECIAL
) { /* size specifiers */
626 switch ((int)tokval
.t_integer
) {
628 if (!setsize
) /* we want to use only the first */
629 result
->oprs
[operand
].type
|= BITS8
;
634 result
->oprs
[operand
].type
|= BITS16
;
640 result
->oprs
[operand
].type
|= BITS32
;
645 result
->oprs
[operand
].type
|= BITS64
;
650 result
->oprs
[operand
].type
|= BITS80
;
655 result
->oprs
[operand
].type
|= BITS128
;
660 result
->oprs
[operand
].type
|= BITS256
;
665 result
->oprs
[operand
].type
|= BITS512
;
669 result
->oprs
[operand
].type
|= TO
;
672 result
->oprs
[operand
].type
|= STRICT
;
675 result
->oprs
[operand
].type
|= FAR
;
678 result
->oprs
[operand
].type
|= NEAR
;
681 result
->oprs
[operand
].type
|= SHORT
;
684 nasm_error(ERR_NONFATAL
, "invalid operand size specification");
686 i
= stdscan(NULL
, &tokval
);
689 if (i
== '[' || i
== '&') { /* memory reference */
691 bracket
= (i
== '[');
692 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
693 while (i
== TOKEN_SPECIAL
|| i
== TOKEN_PREFIX
) {
694 process_size_override(result
, operand
);
695 i
= stdscan(NULL
, &tokval
);
697 } else { /* immediate operand, or register */
699 bracket
= false; /* placate optimisers */
702 if ((result
->oprs
[operand
].type
& FAR
) && !mref
&&
703 result
->opcode
!= I_JMP
&& result
->opcode
!= I_CALL
) {
704 nasm_error(ERR_NONFATAL
, "invalid use of FAR operand specifier");
707 value
= evaluate(stdscan
, NULL
, &tokval
,
708 &result
->oprs
[operand
].opflags
,
709 critical
, nasm_error
, &hints
);
711 if (result
->oprs
[operand
].opflags
& OPFLAG_FORWARD
) {
712 result
->forw_ref
= true;
714 if (!value
) { /* nasm_error in evaluator */
715 result
->opcode
= I_none
; /* unrecoverable parse error: */
716 return result
; /* ignore this instruction */
718 if (i
== ':' && mref
) { /* it was seg:offset */
720 * Process the segment override.
722 if (value
[1].type
!= 0 ||
724 !IS_SREG(value
->type
))
725 nasm_error(ERR_NONFATAL
, "invalid segment override");
726 else if (result
->prefixes
[PPS_SEG
])
727 nasm_error(ERR_NONFATAL
,
728 "instruction has conflicting segment overrides");
730 result
->prefixes
[PPS_SEG
] = value
->type
;
731 if (IS_FSGS(value
->type
))
732 result
->oprs
[operand
].eaflags
|= EAF_FSGS
;
735 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
736 while (i
== TOKEN_SPECIAL
|| i
== TOKEN_PREFIX
) {
737 process_size_override(result
, operand
);
738 i
= stdscan(NULL
, &tokval
);
740 value
= evaluate(stdscan
, NULL
, &tokval
,
741 &result
->oprs
[operand
].opflags
,
742 critical
, nasm_error
, &hints
);
744 if (result
->oprs
[operand
].opflags
& OPFLAG_FORWARD
) {
745 result
->forw_ref
= true;
747 /* and get the offset */
748 if (!value
) { /* but, error in evaluator */
749 result
->opcode
= I_none
; /* unrecoverable parse error: */
750 return result
; /* ignore this instruction */
755 if (mref
&& bracket
) { /* find ] at the end */
757 nasm_error(ERR_NONFATAL
, "parser: expecting ]");
759 } else { /* we got the required ] */
760 i
= stdscan(NULL
, &tokval
);
761 if ((i
== TOKEN_DECORATOR
) || (i
== TOKEN_OPMASK
)) {
763 * according to AVX512 spec, broacast or opmask decorator
764 * is expected for memory reference operands
766 if (tokval
.t_flag
& TFLAG_BRDCAST
) {
767 brace_flags
|= GEN_BRDCAST(0);
768 i
= stdscan(NULL
, &tokval
);
769 } else if (i
== TOKEN_OPMASK
) {
770 brace_flags
|= VAL_OPMASK(nasm_regvals
[tokval
.t_integer
]);
771 i
= stdscan(NULL
, &tokval
);
773 nasm_error(ERR_NONFATAL
, "broadcast or opmask "
774 "decorator expected inside braces");
779 if (i
!= 0 && i
!= ',') {
780 nasm_error(ERR_NONFATAL
, "comma or end of line expected");
784 } else { /* immediate operand */
785 if (i
!= 0 && i
!= ',' && i
!= ':' &&
786 i
!= TOKEN_DECORATOR
&& i
!= TOKEN_OPMASK
) {
787 nasm_error(ERR_NONFATAL
, "comma, colon, decorator or end of "
788 "line expected after operand");
790 } else if (i
== ':') {
791 result
->oprs
[operand
].type
|= COLON
;
792 } else if (i
== TOKEN_DECORATOR
|| i
== TOKEN_OPMASK
) {
793 /* parse opmask (and zeroing) after an operand */
794 recover
= parse_braces(&brace_flags
);
798 do { /* error recovery */
799 i
= stdscan(NULL
, &tokval
);
800 } while (i
!= 0 && i
!= ',');
804 * now convert the exprs returned from evaluate()
805 * into operand descriptions...
808 if (mref
) { /* it's a memory reference */
810 int b
, i
, s
; /* basereg, indexreg, scale */
811 int64_t o
; /* offset */
813 b
= i
= -1, o
= s
= 0;
814 result
->oprs
[operand
].hintbase
= hints
.base
;
815 result
->oprs
[operand
].hinttype
= hints
.type
;
817 if (e
->type
&& e
->type
<= EXPR_REG_END
) { /* this bit's a register */
818 bool is_gpr
= is_class(REG_GPR
,nasm_reg_flags
[e
->type
]);
820 if (is_gpr
&& e
->value
== 1)
821 b
= e
->type
; /* It can be basereg */
822 else /* No, it has to be indexreg */
823 i
= e
->type
, s
= e
->value
;
826 if (e
->type
&& e
->type
<= EXPR_REG_END
) { /* it's a 2nd register */
827 bool is_gpr
= is_class(REG_GPR
,nasm_reg_flags
[e
->type
]);
829 if (b
!= -1) /* If the first was the base, ... */
830 i
= e
->type
, s
= e
->value
; /* second has to be indexreg */
832 else if (!is_gpr
|| e
->value
!= 1) {
833 /* If both want to be index */
834 nasm_error(ERR_NONFATAL
,
835 "invalid effective address: two index registers");
836 result
->opcode
= I_none
;
842 if (e
->type
!= 0) { /* is there an offset? */
843 if (e
->type
<= EXPR_REG_END
) { /* in fact, is there an error? */
844 nasm_error(ERR_NONFATAL
,
845 "beroset-p-603-invalid effective address");
846 result
->opcode
= I_none
;
849 if (e
->type
== EXPR_UNKNOWN
) {
850 result
->oprs
[operand
].opflags
|= OPFLAG_UNKNOWN
;
851 o
= 0; /* doesn't matter what */
852 result
->oprs
[operand
].wrt
= NO_SEG
; /* nor this */
853 result
->oprs
[operand
].segment
= NO_SEG
; /* or this */
855 e
++; /* go to the end of the line */
857 if (e
->type
== EXPR_SIMPLE
) {
861 if (e
->type
== EXPR_WRT
) {
862 result
->oprs
[operand
].wrt
= e
->value
;
865 result
->oprs
[operand
].wrt
= NO_SEG
;
867 * Look for a segment base type.
869 if (e
->type
&& e
->type
< EXPR_SEGBASE
) {
870 nasm_error(ERR_NONFATAL
,
871 "beroset-p-630-invalid effective address");
872 result
->opcode
= I_none
;
875 while (e
->type
&& e
->value
== 0)
877 if (e
->type
&& e
->value
!= 1) {
878 nasm_error(ERR_NONFATAL
,
879 "beroset-p-637-invalid effective address");
880 result
->opcode
= I_none
;
884 result
->oprs
[operand
].segment
=
885 e
->type
- EXPR_SEGBASE
;
888 result
->oprs
[operand
].segment
= NO_SEG
;
889 while (e
->type
&& e
->value
== 0)
892 nasm_error(ERR_NONFATAL
,
893 "beroset-p-650-invalid effective address");
894 result
->opcode
= I_none
;
901 result
->oprs
[operand
].wrt
= NO_SEG
;
902 result
->oprs
[operand
].segment
= NO_SEG
;
905 if (e
->type
!= 0) { /* there'd better be nothing left! */
906 nasm_error(ERR_NONFATAL
,
907 "beroset-p-663-invalid effective address");
908 result
->opcode
= I_none
;
912 /* It is memory, but it can match any r/m operand */
913 result
->oprs
[operand
].type
|= MEMORY_ANY
;
915 if (b
== -1 && (i
== -1 || s
== 0)) {
916 int is_rel
= globalbits
== 64 &&
917 !(result
->oprs
[operand
].eaflags
& EAF_ABS
) &&
919 !(result
->oprs
[operand
].eaflags
& EAF_FSGS
)) ||
920 (result
->oprs
[operand
].eaflags
& EAF_REL
));
922 result
->oprs
[operand
].type
|= is_rel
? IP_REL
: MEM_OFFS
;
926 opflags_t iclass
= nasm_reg_flags
[i
];
928 if (is_class(XMMREG
,iclass
))
929 result
->oprs
[operand
].type
|= XMEM
;
930 else if (is_class(YMMREG
,iclass
))
931 result
->oprs
[operand
].type
|= YMEM
;
932 else if (is_class(ZMMREG
,iclass
))
933 result
->oprs
[operand
].type
|= ZMEM
;
936 result
->oprs
[operand
].basereg
= b
;
937 result
->oprs
[operand
].indexreg
= i
;
938 result
->oprs
[operand
].scale
= s
;
939 result
->oprs
[operand
].offset
= o
;
940 result
->oprs
[operand
].decoflags
|= brace_flags
;
941 } else { /* it's not a memory reference */
942 if (is_just_unknown(value
)) { /* it's immediate but unknown */
943 result
->oprs
[operand
].type
|= IMMEDIATE
;
944 result
->oprs
[operand
].opflags
|= OPFLAG_UNKNOWN
;
945 result
->oprs
[operand
].offset
= 0; /* don't care */
946 result
->oprs
[operand
].segment
= NO_SEG
; /* don't care again */
947 result
->oprs
[operand
].wrt
= NO_SEG
; /* still don't care */
949 if(optimizing
>= 0 && !(result
->oprs
[operand
].type
& STRICT
)) {
951 result
->oprs
[operand
].type
|=
952 UNITY
| SBYTEWORD
| SBYTEDWORD
| UDWORD
| SDWORD
;
954 } else if (is_reloc(value
)) { /* it's immediate */
955 result
->oprs
[operand
].type
|= IMMEDIATE
;
956 result
->oprs
[operand
].offset
= reloc_value(value
);
957 result
->oprs
[operand
].segment
= reloc_seg(value
);
958 result
->oprs
[operand
].wrt
= reloc_wrt(value
);
960 if (is_simple(value
)) {
961 uint64_t n
= reloc_value(value
);
963 result
->oprs
[operand
].type
|= UNITY
;
964 if (optimizing
>= 0 &&
965 !(result
->oprs
[operand
].type
& STRICT
)) {
966 if ((uint32_t) (n
+ 128) <= 255)
967 result
->oprs
[operand
].type
|= SBYTEDWORD
;
968 if ((uint16_t) (n
+ 128) <= 255)
969 result
->oprs
[operand
].type
|= SBYTEWORD
;
971 result
->oprs
[operand
].type
|= UDWORD
;
972 if (n
+ 0x80000000 <= 0xFFFFFFFF)
973 result
->oprs
[operand
].type
|= SDWORD
;
976 } else if(value
->type
== EXPR_RDSAE
) {
978 * it's not an operand but a rounding or SAE decorator.
979 * put the decorator information in the (opflag_t) type field
980 * of previous operand.
983 switch (value
->value
) {
989 result
->oprs
[operand
].decoflags
|=
990 (value
->value
== BRC_SAE
? SAE
: ER
);
991 result
->evex_rm
= value
->value
;
994 nasm_error(ERR_NONFATAL
, "invalid decorator");
997 } else { /* it's a register */
1000 if (value
->type
>= EXPR_SIMPLE
|| value
->value
!= 1) {
1001 nasm_error(ERR_NONFATAL
, "invalid operand type");
1002 result
->opcode
= I_none
;
1007 * check that its only 1 register, not an expression...
1009 for (i
= 1; value
[i
].type
; i
++)
1010 if (value
[i
].value
) {
1011 nasm_error(ERR_NONFATAL
, "invalid operand type");
1012 result
->opcode
= I_none
;
1016 /* clear overrides, except TO which applies to FPU regs */
1017 if (result
->oprs
[operand
].type
& ~TO
) {
1019 * we want to produce a warning iff the specified size
1020 * is different from the register size
1022 rs
= result
->oprs
[operand
].type
& SIZE_MASK
;
1026 result
->oprs
[operand
].type
&= TO
;
1027 result
->oprs
[operand
].type
|= REGISTER
;
1028 result
->oprs
[operand
].type
|= nasm_reg_flags
[value
->type
];
1029 result
->oprs
[operand
].decoflags
|= brace_flags
;
1030 result
->oprs
[operand
].basereg
= value
->type
;
1032 if (rs
&& (result
->oprs
[operand
].type
& SIZE_MASK
) != rs
)
1033 nasm_error(ERR_WARNING
| ERR_PASS1
,
1034 "register size specification ignored");
1039 result
->operands
= operand
; /* set operand count */
1041 /* clear remaining operands */
1042 while (operand
< MAX_OPERANDS
)
1043 result
->oprs
[operand
++].type
= 0;
1046 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
1048 switch (result
->opcode
) {
1050 result
->opcode
= I_RESB
;
1051 result
->oprs
[0].offset
*= 2;
1054 result
->opcode
= I_RESB
;
1055 result
->oprs
[0].offset
*= 4;
1058 result
->opcode
= I_RESB
;
1059 result
->oprs
[0].offset
*= 8;
1062 result
->opcode
= I_RESB
;
1063 result
->oprs
[0].offset
*= 10;
1066 result
->opcode
= I_RESB
;
1067 result
->oprs
[0].offset
*= 16;
1070 result
->opcode
= I_RESB
;
1071 result
->oprs
[0].offset
*= 32;
1080 static int is_comma_next(void)
1087 i
= stdscan(NULL
, &tv
);
1090 return (i
== ',' || i
== ';' || !i
);
1093 void cleanup_insn(insn
* i
)
1097 while ((e
= i
->eops
)) {
1099 if (e
->type
== EOT_DB_STRING_FREE
)
1100 nasm_free(e
->stringval
);