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
55 extern int in_abs_seg
; /* ABSOLUTE segment flag */
56 extern int32_t abs_seg
; /* ABSOLUTE segment */
57 extern int32_t abs_offset
; /* ABSOLUTE segment offset */
59 static int is_comma_next(void);
62 static struct tokenval tokval
;
64 static int prefix_slot(int prefix
)
103 nasm_panic(0, "Invalid value %d passed to prefix_slot()", prefix
);
108 static void process_size_override(insn
*result
, operand
*op
)
110 if (tasm_compatible_mode
) {
111 switch ((int)tokval
.t_integer
) {
112 /* For TASM compatibility a size override inside the
113 * brackets changes the size of the operand, not the
114 * address type of the operand as it does in standard
115 * NASM syntax. Hence:
117 * mov eax,[DWORD val]
119 * is valid syntax in TASM compatibility mode. Note that
120 * you lose the ability to override the default address
121 * type for the instruction, but we never use anything
122 * but 32-bit flat model addressing in our code.
144 nasm_error(ERR_NONFATAL
,
145 "invalid operand size specification");
149 /* Standard NASM compatible syntax */
150 switch ((int)tokval
.t_integer
) {
152 op
->eaflags
|= EAF_TIMESTWO
;
155 op
->eaflags
|= EAF_REL
;
158 op
->eaflags
|= EAF_ABS
;
162 op
->eaflags
|= EAF_BYTEOFFS
;
167 if (result
->prefixes
[PPS_ASIZE
] &&
168 result
->prefixes
[PPS_ASIZE
] != tokval
.t_integer
)
169 nasm_error(ERR_NONFATAL
,
170 "conflicting address size specifications");
172 result
->prefixes
[PPS_ASIZE
] = tokval
.t_integer
;
176 op
->eaflags
|= EAF_WORDOFFS
;
181 op
->eaflags
|= EAF_WORDOFFS
;
185 op
->eaflags
|= EAF_WORDOFFS
;
188 nasm_error(ERR_NONFATAL
, "invalid size specification in"
189 " effective address");
196 * when two or more decorators follow a register operand,
197 * consecutive decorators are parsed here.
198 * opmask and zeroing decorators can be placed in any order.
199 * e.g. zmm1 {k2}{z} or zmm2 {z}{k3}
200 * decorator(s) are placed at the end of an operand.
202 static bool parse_braces(decoflags_t
*decoflags
)
205 bool recover
= false;
209 if (i
== TOKEN_OPMASK
) {
210 if (*decoflags
& OPMASK_MASK
) {
211 nasm_error(ERR_NONFATAL
, "opmask k%"PRIu64
" is already set",
212 *decoflags
& OPMASK_MASK
);
213 *decoflags
&= ~OPMASK_MASK
;
215 *decoflags
|= VAL_OPMASK(nasm_regvals
[tokval
.t_integer
]);
216 } else if (i
== TOKEN_DECORATOR
) {
217 switch (tokval
.t_integer
) {
220 * according to AVX512 spec, only zeroing/merging decorator
221 * is supported with opmask
223 *decoflags
|= GEN_Z(0);
226 nasm_error(ERR_NONFATAL
, "{%s} is not an expected decorator",
230 } else if (i
== ',' || i
== TOKEN_EOS
){
233 nasm_error(ERR_NONFATAL
, "only a series of valid decorators"
238 i
= stdscan(NULL
, &tokval
);
244 static int parse_mref(operand
*op
, const expr
*e
)
246 int b
, i
, s
; /* basereg, indexreg, scale */
247 int64_t o
; /* offset */
252 if (e
->type
&& e
->type
<= EXPR_REG_END
) { /* this bit's a register */
253 bool is_gpr
= is_class(REG_GPR
,nasm_reg_flags
[e
->type
]);
255 if (is_gpr
&& e
->value
== 1)
256 b
= e
->type
; /* It can be basereg */
257 else /* No, it has to be indexreg */
258 i
= e
->type
, s
= e
->value
;
261 if (e
->type
&& e
->type
<= EXPR_REG_END
) { /* it's a 2nd register */
262 bool is_gpr
= is_class(REG_GPR
,nasm_reg_flags
[e
->type
]);
264 if (b
!= -1) /* If the first was the base, ... */
265 i
= e
->type
, s
= e
->value
; /* second has to be indexreg */
267 else if (!is_gpr
|| e
->value
!= 1) {
268 /* If both want to be index */
269 nasm_error(ERR_NONFATAL
,
270 "invalid effective address: two index registers");
276 if (e
->type
!= 0) { /* is there an offset? */
277 if (e
->type
<= EXPR_REG_END
) { /* in fact, is there an error? */
278 nasm_error(ERR_NONFATAL
,
279 "beroset-p-603-invalid effective address");
282 if (e
->type
== EXPR_UNKNOWN
) {
283 op
->opflags
|= OPFLAG_UNKNOWN
;
284 o
= 0; /* doesn't matter what */
285 op
->wrt
= NO_SEG
; /* nor this */
286 op
->segment
= NO_SEG
; /* or this */
288 e
++; /* go to the end of the line */
290 if (e
->type
== EXPR_SIMPLE
) {
294 if (e
->type
== EXPR_WRT
) {
300 * Look for a segment base type.
302 if (e
->type
&& e
->type
< EXPR_SEGBASE
) {
303 nasm_error(ERR_NONFATAL
,
304 "beroset-p-630-invalid effective address");
307 while (e
->type
&& e
->value
== 0)
309 if (e
->type
&& e
->value
!= 1) {
310 nasm_error(ERR_NONFATAL
,
311 "beroset-p-637-invalid effective address");
315 op
->segment
= e
->type
- EXPR_SEGBASE
;
318 op
->segment
= NO_SEG
;
319 while (e
->type
&& e
->value
== 0)
322 nasm_error(ERR_NONFATAL
,
323 "beroset-p-650-invalid effective address");
331 op
->segment
= NO_SEG
;
334 if (e
->type
!= 0) { /* there'd better be nothing left! */
335 nasm_error(ERR_NONFATAL
,
336 "beroset-p-663-invalid effective address");
347 static void mref_set_optype(operand
*op
)
350 int i
= op
->indexreg
;
353 /* It is memory, but it can match any r/m operand */
354 op
->type
|= MEMORY_ANY
;
356 if (b
== -1 && (i
== -1 || s
== 0)) {
357 int is_rel
= globalbits
== 64 &&
358 !(op
->eaflags
& EAF_ABS
) &&
360 !(op
->eaflags
& EAF_FSGS
)) ||
361 (op
->eaflags
& EAF_REL
));
363 op
->type
|= is_rel
? IP_REL
: MEM_OFFS
;
367 opflags_t iclass
= nasm_reg_flags
[i
];
369 if (is_class(XMMREG
,iclass
))
371 else if (is_class(YMMREG
,iclass
))
373 else if (is_class(ZMMREG
,iclass
))
378 insn
*parse_line(int pass
, char *buffer
, insn
*result
, ldfunc ldef
)
380 bool insn_is_label
= false;
381 struct eval_hints hints
;
389 result
->forw_ref
= false;
393 i
= stdscan(NULL
, &tokval
);
395 result
->label
= NULL
; /* Assume no label */
396 result
->eops
= NULL
; /* must do this, whatever happens */
397 result
->operands
= 0; /* must initialize this */
398 result
->evex_rm
= 0; /* Ensure EVEX rounding mode is reset */
399 result
->evex_brerop
= -1; /* Reset EVEX broadcasting/ER op position */
401 /* Ignore blank lines */
408 (i
!= TOKEN_REG
|| !IS_SREG(tokval
.t_integer
))) {
409 nasm_error(ERR_NONFATAL
,
410 "label or instruction expected at start of line");
414 if (i
== TOKEN_ID
|| (insn_is_label
&& i
== TOKEN_INSN
)) {
415 /* there's a label here */
417 result
->label
= tokval
.t_charptr
;
418 i
= stdscan(NULL
, &tokval
);
419 if (i
== ':') { /* skip over the optional colon */
420 i
= stdscan(NULL
, &tokval
);
422 nasm_error(ERR_WARNING
| ERR_WARN_OL
| ERR_PASS1
,
423 "label alone on a line without a colon might be in error");
425 if (i
!= TOKEN_INSN
|| tokval
.t_integer
!= I_EQU
) {
427 * FIXME: location.segment could be NO_SEG, in which case
428 * it is possible we should be passing 'abs_seg'. Look into this.
429 * Work out whether that is *really* what we should be doing.
430 * Generally fix things. I think this is right as it is, but
431 * am still not certain.
433 ldef(result
->label
, in_abs_seg
? abs_seg
: location
.segment
,
434 location
.offset
, NULL
, true, false);
438 /* Just a label here */
442 nasm_build_assert(P_none
!= 0);
443 memset(result
->prefixes
, P_none
, sizeof(result
->prefixes
));
446 while (i
== TOKEN_PREFIX
||
447 (i
== TOKEN_REG
&& IS_SREG(tokval
.t_integer
))) {
451 * Handle special case: the TIMES prefix.
453 if (i
== TOKEN_PREFIX
&& tokval
.t_integer
== P_TIMES
) {
456 i
= stdscan(NULL
, &tokval
);
457 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass0
, NULL
);
459 if (!value
) /* Error in evaluator */
461 if (!is_simple(value
)) {
462 nasm_error(ERR_NONFATAL
,
463 "non-constant argument supplied to TIMES");
466 result
->times
= value
->value
;
467 if (value
->value
< 0 && pass0
== 2) {
468 nasm_error(ERR_NONFATAL
, "TIMES value %"PRId64
" is negative",
474 int slot
= prefix_slot(tokval
.t_integer
);
475 if (result
->prefixes
[slot
]) {
476 if (result
->prefixes
[slot
] == tokval
.t_integer
)
477 nasm_error(ERR_WARNING
| ERR_PASS1
,
478 "instruction has redundant prefixes");
480 nasm_error(ERR_NONFATAL
,
481 "instruction has conflicting prefixes");
483 result
->prefixes
[slot
] = tokval
.t_integer
;
484 i
= stdscan(NULL
, &tokval
);
488 if (i
!= TOKEN_INSN
) {
492 for (j
= 0; j
< MAXPREFIX
; j
++) {
493 if ((pfx
= result
->prefixes
[j
]) != P_none
)
497 if (i
== 0 && pfx
!= P_none
) {
499 * Instruction prefixes are present, but no actual
500 * instruction. This is allowed: at this point we
501 * invent a notional instruction of RESB 0.
503 result
->opcode
= I_RESB
;
504 result
->operands
= 1;
505 result
->oprs
[0].type
= IMMEDIATE
;
506 result
->oprs
[0].offset
= 0L;
507 result
->oprs
[0].segment
= result
->oprs
[0].wrt
= NO_SEG
;
510 nasm_error(ERR_NONFATAL
, "parser: instruction expected");
515 result
->opcode
= tokval
.t_integer
;
516 result
->condition
= tokval
.t_inttwo
;
519 * INCBIN cannot be satisfied with incorrectly
520 * evaluated operands, since the correct values _must_ be known
521 * on the first pass. Hence, even in pass one, we set the
522 * `critical' flag on calling evaluate(), so that it will bomb
523 * out on undefined symbols.
525 if (result
->opcode
== I_INCBIN
) {
526 critical
= (pass0
< 2 ? 1 : 2);
529 critical
= (pass
== 2 ? 2 : 0);
531 if (result
->opcode
== I_DB
|| result
->opcode
== I_DW
||
532 result
->opcode
== I_DD
|| result
->opcode
== I_DQ
||
533 result
->opcode
== I_DT
|| result
->opcode
== I_DO
||
534 result
->opcode
== I_DY
|| result
->opcode
== I_DZ
||
535 result
->opcode
== I_INCBIN
) {
536 extop
*eop
, **tail
= &result
->eops
, **fixptr
;
540 result
->eops_float
= false;
543 * Begin to read the DB/DW/DD/DQ/DT/DO/DY/DZ/INCBIN operands.
546 i
= stdscan(NULL
, &tokval
);
549 else if (first
&& i
== ':') {
550 insn_is_label
= true;
555 eop
= *tail
= nasm_malloc(sizeof(extop
));
558 eop
->type
= EOT_NOTHING
;
563 * is_comma_next() here is to distinguish this from
564 * a string used as part of an expression...
566 if (i
== TOKEN_STR
&& is_comma_next()) {
567 eop
->type
= EOT_DB_STRING
;
568 eop
->stringval
= tokval
.t_charptr
;
569 eop
->stringlen
= tokval
.t_inttwo
;
570 i
= stdscan(NULL
, &tokval
); /* eat the comma */
571 } else if (i
== TOKEN_STRFUNC
) {
573 const char *funcname
= tokval
.t_charptr
;
574 enum strfunc func
= tokval
.t_integer
;
575 i
= stdscan(NULL
, &tokval
);
578 i
= stdscan(NULL
, &tokval
);
580 if (i
!= TOKEN_STR
) {
581 nasm_error(ERR_NONFATAL
,
582 "%s must be followed by a string constant",
584 eop
->type
= EOT_NOTHING
;
586 eop
->type
= EOT_DB_STRING_FREE
;
588 string_transform(tokval
.t_charptr
, tokval
.t_inttwo
,
589 &eop
->stringval
, func
);
590 if (eop
->stringlen
== (size_t)-1) {
591 nasm_error(ERR_NONFATAL
, "invalid string for transform");
592 eop
->type
= EOT_NOTHING
;
595 if (parens
&& i
&& i
!= ')') {
596 i
= stdscan(NULL
, &tokval
);
598 nasm_error(ERR_NONFATAL
, "unterminated %s function",
603 i
= stdscan(NULL
, &tokval
);
604 } else if (i
== '-' || i
== '+') {
605 char *save
= stdscan_get();
607 sign
= (i
== '-') ? -1 : 1;
608 i
= stdscan(NULL
, &tokval
);
609 if (i
!= TOKEN_FLOAT
) {
611 i
= tokval
.t_type
= token
;
616 } else if (i
== TOKEN_FLOAT
) {
618 eop
->type
= EOT_DB_STRING
;
619 result
->eops_float
= true;
621 eop
->stringlen
= idata_bytes(result
->opcode
);
622 if (eop
->stringlen
> 16) {
623 nasm_error(ERR_NONFATAL
, "floating-point constant"
624 " encountered in DY or DZ instruction");
626 } else if (eop
->stringlen
< 1) {
627 nasm_error(ERR_NONFATAL
, "floating-point constant"
628 " encountered in unknown instruction");
630 * fix suggested by Pedro Gimeno... original line was:
631 * eop->type = EOT_NOTHING;
636 eop
= nasm_realloc(eop
, sizeof(extop
) + eop
->stringlen
);
639 eop
->stringval
= (char *)eop
+ sizeof(extop
);
640 if (!eop
->stringlen
||
641 !float_const(tokval
.t_charptr
, sign
,
642 (uint8_t *)eop
->stringval
, eop
->stringlen
))
643 eop
->type
= EOT_NOTHING
;
644 i
= stdscan(NULL
, &tokval
); /* eat the comma */
646 /* anything else, assume it is an expression */
650 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
,
653 if (!value
) /* Error in evaluator */
655 if (is_unknown(value
)) {
656 eop
->type
= EOT_DB_NUMBER
;
657 eop
->offset
= 0; /* doesn't matter what we put */
658 eop
->segment
= eop
->wrt
= NO_SEG
; /* likewise */
659 } else if (is_reloc(value
)) {
660 eop
->type
= EOT_DB_NUMBER
;
661 eop
->offset
= reloc_value(value
);
662 eop
->segment
= reloc_seg(value
);
663 eop
->wrt
= reloc_wrt(value
);
665 nasm_error(ERR_NONFATAL
,
666 "operand %d: expression is not simple"
667 " or relocatable", oper_num
);
672 * We're about to call stdscan(), which will eat the
673 * comma that we're currently sitting on between
674 * arguments. However, we'd better check first that it
677 if (i
== TOKEN_EOS
) /* also could be EOL */
680 nasm_error(ERR_NONFATAL
, "comma expected after operand %d",
686 if (result
->opcode
== I_INCBIN
) {
688 * Correct syntax for INCBIN is that there should be
689 * one string operand, followed by one or two numeric
692 if (!result
->eops
|| result
->eops
->type
!= EOT_DB_STRING
)
693 nasm_error(ERR_NONFATAL
, "`incbin' expects a file name");
694 else if (result
->eops
->next
&&
695 result
->eops
->next
->type
!= EOT_DB_NUMBER
)
696 nasm_error(ERR_NONFATAL
, "`incbin': second parameter is"
698 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
699 result
->eops
->next
->next
->type
!= EOT_DB_NUMBER
)
700 nasm_error(ERR_NONFATAL
, "`incbin': third parameter is"
702 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
703 result
->eops
->next
->next
->next
)
704 nasm_error(ERR_NONFATAL
,
705 "`incbin': more than three parameters");
709 * If we reach here, one of the above errors happened.
710 * Throw the instruction away.
713 } else /* DB ... */ if (oper_num
== 0)
714 nasm_error(ERR_WARNING
| ERR_PASS1
,
715 "no operand for data declaration");
717 result
->operands
= oper_num
;
723 * Now we begin to parse the operands. There may be up to four
724 * of these, separated by commas, and terminated by a zero token.
727 for (opnum
= 0; opnum
< MAX_OPERANDS
; opnum
++) {
728 operand
*op
= &result
->oprs
[opnum
];
729 expr
*value
; /* used most of the time */
730 bool mref
; /* is this going to be a memory ref? */
731 bool bracket
; /* is it a [] mref, or a & mref? */
732 bool mib
; /* compound (mib) mref? */
734 decoflags_t brace_flags
= 0; /* flags for decorators in braces */
736 op
->disp_size
= 0; /* have to zero this whatever */
737 op
->eaflags
= 0; /* and this */
741 i
= stdscan(NULL
, &tokval
);
743 break; /* end of operands: get out of here */
744 else if (first
&& i
== ':') {
745 insn_is_label
= true;
749 op
->type
= 0; /* so far, no override */
750 while (i
== TOKEN_SPECIAL
) { /* size specifiers */
751 switch ((int)tokval
.t_integer
) {
753 if (!setsize
) /* we want to use only the first */
809 nasm_error(ERR_NONFATAL
, "invalid operand size specification");
811 i
= stdscan(NULL
, &tokval
);
814 if (i
== '[' || i
== '&') { /* memory reference */
816 bracket
= (i
== '[');
817 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
818 while (i
== TOKEN_SPECIAL
|| i
== TOKEN_PREFIX
) {
819 process_size_override(result
, op
);
820 i
= stdscan(NULL
, &tokval
);
822 /* when a comma follows an opening bracket - [ , eax*4] */
824 /* treat as if there is a zero displacement virtually */
825 tokval
.t_type
= TOKEN_NUM
;
826 tokval
.t_integer
= 0;
827 stdscan_set(stdscan_get() - 1); /* rewind the comma */
829 } else { /* immediate operand, or register */
831 bracket
= false; /* placate optimisers */
834 if ((op
->type
& FAR
) && !mref
&&
835 result
->opcode
!= I_JMP
&& result
->opcode
!= I_CALL
) {
836 nasm_error(ERR_NONFATAL
, "invalid use of FAR operand specifier");
839 value
= evaluate(stdscan
, NULL
, &tokval
,
840 &op
->opflags
, critical
, &hints
);
842 if (op
->opflags
& OPFLAG_FORWARD
) {
843 result
->forw_ref
= true;
845 if (!value
) /* Error in evaluator */
847 if (i
== ':' && mref
) { /* it was seg:offset */
849 * Process the segment override.
851 if (value
[1].type
!= 0 ||
853 !IS_SREG(value
->type
))
854 nasm_error(ERR_NONFATAL
, "invalid segment override");
855 else if (result
->prefixes
[PPS_SEG
])
856 nasm_error(ERR_NONFATAL
,
857 "instruction has conflicting segment overrides");
859 result
->prefixes
[PPS_SEG
] = value
->type
;
860 if (IS_FSGS(value
->type
))
861 op
->eaflags
|= EAF_FSGS
;
864 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
865 while (i
== TOKEN_SPECIAL
|| i
== TOKEN_PREFIX
) {
866 process_size_override(result
, op
);
867 i
= stdscan(NULL
, &tokval
);
869 value
= evaluate(stdscan
, NULL
, &tokval
,
870 &op
->opflags
, critical
, &hints
);
872 if (op
->opflags
& OPFLAG_FORWARD
) {
873 result
->forw_ref
= true;
875 /* and get the offset */
876 if (!value
) /* Error in evaluator */
881 if (mref
&& bracket
&& i
== ',') {
882 /* [seg:base+offset,index*scale] syntax (mib) */
884 operand o1
, o2
; /* Partial operands */
886 if (parse_mref(&o1
, value
))
889 i
= stdscan(NULL
, &tokval
); /* Eat comma */
890 value
= evaluate(stdscan
, NULL
, &tokval
, &op
->opflags
,
896 if (parse_mref(&o2
, value
))
899 if (o2
.basereg
!= -1 && o2
.indexreg
== -1) {
900 o2
.indexreg
= o2
.basereg
;
905 if (o1
.indexreg
!= -1 || o2
.basereg
!= -1 || o2
.offset
!= 0 ||
906 o2
.segment
!= NO_SEG
|| o2
.wrt
!= NO_SEG
) {
907 nasm_error(ERR_NONFATAL
, "invalid mib expression");
911 op
->basereg
= o1
.basereg
;
912 op
->indexreg
= o2
.indexreg
;
913 op
->scale
= o2
.scale
;
914 op
->offset
= o1
.offset
;
915 op
->segment
= o1
.segment
;
918 if (op
->basereg
!= -1) {
919 op
->hintbase
= op
->basereg
;
920 op
->hinttype
= EAH_MAKEBASE
;
921 } else if (op
->indexreg
!= -1) {
922 op
->hintbase
= op
->indexreg
;
923 op
->hinttype
= EAH_NOTBASE
;
926 op
->hinttype
= EAH_NOHINT
;
933 if (mref
&& bracket
) { /* find ] at the end */
935 nasm_error(ERR_NONFATAL
, "parser: expecting ]");
937 } else { /* we got the required ] */
938 i
= stdscan(NULL
, &tokval
);
939 if ((i
== TOKEN_DECORATOR
) || (i
== TOKEN_OPMASK
)) {
941 * according to AVX512 spec, broacast or opmask decorator
942 * is expected for memory reference operands
944 if (tokval
.t_flag
& TFLAG_BRDCAST
) {
945 brace_flags
|= GEN_BRDCAST(0) |
946 VAL_BRNUM(tokval
.t_integer
- BRC_1TO2
);
947 i
= stdscan(NULL
, &tokval
);
948 } else if (i
== TOKEN_OPMASK
) {
949 brace_flags
|= VAL_OPMASK(nasm_regvals
[tokval
.t_integer
]);
950 i
= stdscan(NULL
, &tokval
);
952 nasm_error(ERR_NONFATAL
, "broadcast or opmask "
953 "decorator expected inside braces");
958 if (i
!= 0 && i
!= ',') {
959 nasm_error(ERR_NONFATAL
, "comma or end of line expected");
963 } else { /* immediate operand */
964 if (i
!= 0 && i
!= ',' && i
!= ':' &&
965 i
!= TOKEN_DECORATOR
&& i
!= TOKEN_OPMASK
) {
966 nasm_error(ERR_NONFATAL
, "comma, colon, decorator or end of "
967 "line expected after operand");
969 } else if (i
== ':') {
971 } else if (i
== TOKEN_DECORATOR
|| i
== TOKEN_OPMASK
) {
972 /* parse opmask (and zeroing) after an operand */
973 recover
= parse_braces(&brace_flags
);
977 do { /* error recovery */
978 i
= stdscan(NULL
, &tokval
);
979 } while (i
!= 0 && i
!= ',');
983 * now convert the exprs returned from evaluate()
984 * into operand descriptions...
986 op
->decoflags
|= brace_flags
;
988 if (mref
) { /* it's a memory reference */
989 /* A mib reference was fully parsed already */
991 if (parse_mref(op
, value
))
993 op
->hintbase
= hints
.base
;
994 op
->hinttype
= hints
.type
;
997 } else { /* it's not a memory reference */
998 if (is_just_unknown(value
)) { /* it's immediate but unknown */
999 op
->type
|= IMMEDIATE
;
1000 op
->opflags
|= OPFLAG_UNKNOWN
;
1001 op
->offset
= 0; /* don't care */
1002 op
->segment
= NO_SEG
; /* don't care again */
1003 op
->wrt
= NO_SEG
; /* still don't care */
1005 if(optimizing
>= 0 && !(op
->type
& STRICT
)) {
1008 UNITY
| SBYTEWORD
| SBYTEDWORD
| UDWORD
| SDWORD
;
1010 } else if (is_reloc(value
)) { /* it's immediate */
1011 op
->type
|= IMMEDIATE
;
1012 op
->offset
= reloc_value(value
);
1013 op
->segment
= reloc_seg(value
);
1014 op
->wrt
= reloc_wrt(value
);
1016 if (is_simple(value
)) {
1017 uint64_t n
= reloc_value(value
);
1020 if (optimizing
>= 0 &&
1021 !(op
->type
& STRICT
)) {
1022 if ((uint32_t) (n
+ 128) <= 255)
1023 op
->type
|= SBYTEDWORD
;
1024 if ((uint16_t) (n
+ 128) <= 255)
1025 op
->type
|= SBYTEWORD
;
1026 if (n
<= 0xFFFFFFFF)
1028 if (n
+ 0x80000000 <= 0xFFFFFFFF)
1032 } else if(value
->type
== EXPR_RDSAE
) {
1034 * it's not an operand but a rounding or SAE decorator.
1035 * put the decorator information in the (opflag_t) type field
1036 * of previous operand.
1039 switch (value
->value
) {
1045 op
->decoflags
|= (value
->value
== BRC_SAE
? SAE
: ER
);
1046 result
->evex_rm
= value
->value
;
1049 nasm_error(ERR_NONFATAL
, "invalid decorator");
1052 } else { /* it's a register */
1055 if (value
->type
>= EXPR_SIMPLE
|| value
->value
!= 1) {
1056 nasm_error(ERR_NONFATAL
, "invalid operand type");
1061 * check that its only 1 register, not an expression...
1063 for (i
= 1; value
[i
].type
; i
++)
1064 if (value
[i
].value
) {
1065 nasm_error(ERR_NONFATAL
, "invalid operand type");
1069 /* clear overrides, except TO which applies to FPU regs */
1070 if (op
->type
& ~TO
) {
1072 * we want to produce a warning iff the specified size
1073 * is different from the register size
1075 rs
= op
->type
& SIZE_MASK
;
1080 op
->type
|= REGISTER
;
1081 op
->type
|= nasm_reg_flags
[value
->type
];
1082 op
->decoflags
|= brace_flags
;
1083 op
->basereg
= value
->type
;
1085 if (rs
&& (op
->type
& SIZE_MASK
) != rs
)
1086 nasm_error(ERR_WARNING
| ERR_PASS1
,
1087 "register size specification ignored");
1091 /* remember the position of operand having broadcasting/ER mode */
1092 if (op
->decoflags
& (BRDCAST_MASK
| ER
| SAE
))
1093 result
->evex_brerop
= opnum
;
1096 result
->operands
= opnum
; /* set operand count */
1098 /* clear remaining operands */
1099 while (opnum
< MAX_OPERANDS
)
1100 result
->oprs
[opnum
++].type
= 0;
1103 * Transform RESW, RESD, RESQ, REST, RESO, RESY, RESZ into RESB.
1105 switch (result
->opcode
) {
1107 result
->opcode
= I_RESB
;
1108 result
->oprs
[0].offset
*= 2;
1111 result
->opcode
= I_RESB
;
1112 result
->oprs
[0].offset
*= 4;
1115 result
->opcode
= I_RESB
;
1116 result
->oprs
[0].offset
*= 8;
1119 result
->opcode
= I_RESB
;
1120 result
->oprs
[0].offset
*= 10;
1123 result
->opcode
= I_RESB
;
1124 result
->oprs
[0].offset
*= 16;
1127 result
->opcode
= I_RESB
;
1128 result
->oprs
[0].offset
*= 32;
1131 result
->opcode
= I_RESB
;
1132 result
->oprs
[0].offset
*= 64;
1141 result
->opcode
= I_none
;
1145 static int is_comma_next(void)
1152 i
= stdscan(NULL
, &tv
);
1155 return (i
== ',' || i
== ';' || !i
);
1158 void cleanup_insn(insn
* i
)
1162 while ((e
= i
->eops
)) {
1164 if (e
->type
== EOT_DB_STRING_FREE
)
1165 nasm_free(e
->stringval
);