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
, operand
*op
)
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.
145 nasm_error(ERR_NONFATAL
,
146 "invalid operand size specification");
150 /* Standard NASM compatible syntax */
151 switch ((int)tokval
.t_integer
) {
153 op
->eaflags
|= EAF_TIMESTWO
;
156 op
->eaflags
|= EAF_REL
;
159 op
->eaflags
|= EAF_ABS
;
163 op
->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
;
177 op
->eaflags
|= EAF_WORDOFFS
;
182 op
->eaflags
|= EAF_WORDOFFS
;
186 op
->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 static int parse_mref(operand
*op
, const expr
*e
)
247 int b
, i
, s
; /* basereg, indexreg, scale */
248 int64_t o
; /* offset */
253 if (e
->type
&& e
->type
<= EXPR_REG_END
) { /* this bit's a register */
254 bool is_gpr
= is_class(REG_GPR
,nasm_reg_flags
[e
->type
]);
256 if (is_gpr
&& e
->value
== 1)
257 b
= e
->type
; /* It can be basereg */
258 else /* No, it has to be indexreg */
259 i
= e
->type
, s
= e
->value
;
262 if (e
->type
&& e
->type
<= EXPR_REG_END
) { /* it's a 2nd register */
263 bool is_gpr
= is_class(REG_GPR
,nasm_reg_flags
[e
->type
]);
265 if (b
!= -1) /* If the first was the base, ... */
266 i
= e
->type
, s
= e
->value
; /* second has to be indexreg */
268 else if (!is_gpr
|| e
->value
!= 1) {
269 /* If both want to be index */
270 nasm_error(ERR_NONFATAL
,
271 "invalid effective address: two index registers");
277 if (e
->type
!= 0) { /* is there an offset? */
278 if (e
->type
<= EXPR_REG_END
) { /* in fact, is there an error? */
279 nasm_error(ERR_NONFATAL
,
280 "beroset-p-603-invalid effective address");
283 if (e
->type
== EXPR_UNKNOWN
) {
284 op
->opflags
|= OPFLAG_UNKNOWN
;
285 o
= 0; /* doesn't matter what */
286 op
->wrt
= NO_SEG
; /* nor this */
287 op
->segment
= NO_SEG
; /* or this */
289 e
++; /* go to the end of the line */
291 if (e
->type
== EXPR_SIMPLE
) {
295 if (e
->type
== EXPR_WRT
) {
301 * Look for a segment base type.
303 if (e
->type
&& e
->type
< EXPR_SEGBASE
) {
304 nasm_error(ERR_NONFATAL
,
305 "beroset-p-630-invalid effective address");
308 while (e
->type
&& e
->value
== 0)
310 if (e
->type
&& e
->value
!= 1) {
311 nasm_error(ERR_NONFATAL
,
312 "beroset-p-637-invalid effective address");
316 op
->segment
= e
->type
- EXPR_SEGBASE
;
319 op
->segment
= NO_SEG
;
320 while (e
->type
&& e
->value
== 0)
323 nasm_error(ERR_NONFATAL
,
324 "beroset-p-650-invalid effective address");
332 op
->segment
= NO_SEG
;
335 if (e
->type
!= 0) { /* there'd better be nothing left! */
336 nasm_error(ERR_NONFATAL
,
337 "beroset-p-663-invalid effective address");
348 static void mref_set_optype(operand
*op
)
351 int i
= op
->indexreg
;
354 /* It is memory, but it can match any r/m operand */
355 op
->type
|= MEMORY_ANY
;
357 if (b
== -1 && (i
== -1 || s
== 0)) {
358 int is_rel
= globalbits
== 64 &&
359 !(op
->eaflags
& EAF_ABS
) &&
361 !(op
->eaflags
& EAF_FSGS
)) ||
362 (op
->eaflags
& EAF_REL
));
364 op
->type
|= is_rel
? IP_REL
: MEM_OFFS
;
368 opflags_t iclass
= nasm_reg_flags
[i
];
370 if (is_class(XMMREG
,iclass
))
372 else if (is_class(YMMREG
,iclass
))
374 else if (is_class(ZMMREG
,iclass
))
379 insn
*parse_line(int pass
, char *buffer
, insn
*result
, ldfunc ldef
)
381 bool insn_is_label
= false;
382 struct eval_hints hints
;
390 result
->forw_ref
= false;
394 i
= stdscan(NULL
, &tokval
);
396 result
->label
= NULL
; /* Assume no label */
397 result
->eops
= NULL
; /* must do this, whatever happens */
398 result
->operands
= 0; /* must initialize this */
399 result
->evex_rm
= 0; /* Ensure EVEX rounding mode is reset */
400 result
->evex_brerop
= -1; /* Reset EVEX broadcasting/ER op position */
402 /* Ignore blank lines */
409 (i
!= TOKEN_REG
|| !IS_SREG(tokval
.t_integer
))) {
410 nasm_error(ERR_NONFATAL
,
411 "label or instruction expected at start of line");
415 if (i
== TOKEN_ID
|| (insn_is_label
&& i
== TOKEN_INSN
)) {
416 /* there's a label here */
418 result
->label
= tokval
.t_charptr
;
419 i
= stdscan(NULL
, &tokval
);
420 if (i
== ':') { /* skip over the optional colon */
421 i
= stdscan(NULL
, &tokval
);
423 nasm_error(ERR_WARNING
| ERR_WARN_OL
| ERR_PASS1
,
424 "label alone on a line without a colon might be in error");
426 if (i
!= TOKEN_INSN
|| tokval
.t_integer
!= I_EQU
) {
428 * FIXME: location->segment could be NO_SEG, in which case
429 * it is possible we should be passing 'abs_seg'. Look into this.
430 * Work out whether that is *really* what we should be doing.
431 * Generally fix things. I think this is right as it is, but
432 * am still not certain.
434 ldef(result
->label
, in_abs_seg
? abs_seg
: location
->segment
,
435 location
->offset
, NULL
, true, false);
439 /* Just a label here */
443 nasm_build_assert(P_none
!= 0);
444 memset(result
->prefixes
, P_none
, sizeof(result
->prefixes
));
447 while (i
== TOKEN_PREFIX
||
448 (i
== TOKEN_REG
&& IS_SREG(tokval
.t_integer
))) {
452 * Handle special case: the TIMES prefix.
454 if (i
== TOKEN_PREFIX
&& tokval
.t_integer
== P_TIMES
) {
457 i
= stdscan(NULL
, &tokval
);
458 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass0
, nasm_error
, NULL
);
460 if (!value
) /* Error in evaluator */
462 if (!is_simple(value
)) {
463 nasm_error(ERR_NONFATAL
,
464 "non-constant argument supplied to TIMES");
467 result
->times
= value
->value
;
468 if (value
->value
< 0 && pass0
== 2) {
469 nasm_error(ERR_NONFATAL
, "TIMES value %"PRId64
" is negative",
475 int slot
= prefix_slot(tokval
.t_integer
);
476 if (result
->prefixes
[slot
]) {
477 if (result
->prefixes
[slot
] == tokval
.t_integer
)
478 nasm_error(ERR_WARNING
| ERR_PASS1
,
479 "instruction has redundant prefixes");
481 nasm_error(ERR_NONFATAL
,
482 "instruction has conflicting prefixes");
484 result
->prefixes
[slot
] = tokval
.t_integer
;
485 i
= stdscan(NULL
, &tokval
);
489 if (i
!= TOKEN_INSN
) {
493 for (j
= 0; j
< MAXPREFIX
; j
++) {
494 if ((pfx
= result
->prefixes
[j
]) != P_none
)
498 if (i
== 0 && pfx
!= P_none
) {
500 * Instruction prefixes are present, but no actual
501 * instruction. This is allowed: at this point we
502 * invent a notional instruction of RESB 0.
504 result
->opcode
= I_RESB
;
505 result
->operands
= 1;
506 result
->oprs
[0].type
= IMMEDIATE
;
507 result
->oprs
[0].offset
= 0L;
508 result
->oprs
[0].segment
= result
->oprs
[0].wrt
= NO_SEG
;
511 nasm_error(ERR_NONFATAL
, "parser: instruction expected");
516 result
->opcode
= tokval
.t_integer
;
517 result
->condition
= tokval
.t_inttwo
;
520 * INCBIN cannot be satisfied with incorrectly
521 * evaluated operands, since the correct values _must_ be known
522 * on the first pass. Hence, even in pass one, we set the
523 * `critical' flag on calling evaluate(), so that it will bomb
524 * out on undefined symbols.
526 if (result
->opcode
== I_INCBIN
) {
527 critical
= (pass0
< 2 ? 1 : 2);
530 critical
= (pass
== 2 ? 2 : 0);
532 if (result
->opcode
== I_DB
|| result
->opcode
== I_DW
||
533 result
->opcode
== I_DD
|| result
->opcode
== I_DQ
||
534 result
->opcode
== I_DT
|| result
->opcode
== I_DO
||
535 result
->opcode
== I_DY
|| 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/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 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
,
643 eop
->stringlen
, nasm_error
))
644 eop
->type
= EOT_NOTHING
;
645 i
= stdscan(NULL
, &tokval
); /* eat the comma */
647 /* anything else, assume it is an expression */
651 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
,
652 critical
, nasm_error
, NULL
);
654 if (!value
) /* Error in evaluator */
656 if (is_unknown(value
)) {
657 eop
->type
= EOT_DB_NUMBER
;
658 eop
->offset
= 0; /* doesn't matter what we put */
659 eop
->segment
= eop
->wrt
= NO_SEG
; /* likewise */
660 } else if (is_reloc(value
)) {
661 eop
->type
= EOT_DB_NUMBER
;
662 eop
->offset
= reloc_value(value
);
663 eop
->segment
= reloc_seg(value
);
664 eop
->wrt
= reloc_wrt(value
);
666 nasm_error(ERR_NONFATAL
,
667 "operand %d: expression is not simple"
668 " or relocatable", oper_num
);
673 * We're about to call stdscan(), which will eat the
674 * comma that we're currently sitting on between
675 * arguments. However, we'd better check first that it
678 if (i
== TOKEN_EOS
) /* also could be EOL */
681 nasm_error(ERR_NONFATAL
, "comma expected after operand %d",
687 if (result
->opcode
== I_INCBIN
) {
689 * Correct syntax for INCBIN is that there should be
690 * one string operand, followed by one or two numeric
693 if (!result
->eops
|| result
->eops
->type
!= EOT_DB_STRING
)
694 nasm_error(ERR_NONFATAL
, "`incbin' expects a file name");
695 else if (result
->eops
->next
&&
696 result
->eops
->next
->type
!= EOT_DB_NUMBER
)
697 nasm_error(ERR_NONFATAL
, "`incbin': second parameter is"
699 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
700 result
->eops
->next
->next
->type
!= EOT_DB_NUMBER
)
701 nasm_error(ERR_NONFATAL
, "`incbin': third parameter is"
703 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
704 result
->eops
->next
->next
->next
)
705 nasm_error(ERR_NONFATAL
,
706 "`incbin': more than three parameters");
710 * If we reach here, one of the above errors happened.
711 * Throw the instruction away.
714 } else /* DB ... */ if (oper_num
== 0)
715 nasm_error(ERR_WARNING
| ERR_PASS1
,
716 "no operand for data declaration");
718 result
->operands
= oper_num
;
724 * Now we begin to parse the operands. There may be up to four
725 * of these, separated by commas, and terminated by a zero token.
728 for (opnum
= 0; opnum
< MAX_OPERANDS
; opnum
++) {
729 operand
*op
= &result
->oprs
[opnum
];
730 expr
*value
; /* used most of the time */
731 bool mref
; /* is this going to be a memory ref? */
732 bool bracket
; /* is it a [] mref, or a & mref? */
733 bool mib
; /* compound (mib) mref? */
735 decoflags_t brace_flags
= 0; /* flags for decorators in braces */
737 op
->disp_size
= 0; /* have to zero this whatever */
738 op
->eaflags
= 0; /* and this */
742 i
= stdscan(NULL
, &tokval
);
744 break; /* end of operands: get out of here */
745 else if (first
&& i
== ':') {
746 insn_is_label
= true;
750 op
->type
= 0; /* so far, no override */
751 while (i
== TOKEN_SPECIAL
) { /* size specifiers */
752 switch ((int)tokval
.t_integer
) {
754 if (!setsize
) /* we want to use only the first */
810 nasm_error(ERR_NONFATAL
, "invalid operand size specification");
812 i
= stdscan(NULL
, &tokval
);
815 if (i
== '[' || i
== '&') { /* memory reference */
817 bracket
= (i
== '[');
818 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
819 while (i
== TOKEN_SPECIAL
|| i
== TOKEN_PREFIX
) {
820 process_size_override(result
, op
);
821 i
= stdscan(NULL
, &tokval
);
823 } else { /* immediate operand, or register */
825 bracket
= false; /* placate optimisers */
828 if ((op
->type
& FAR
) && !mref
&&
829 result
->opcode
!= I_JMP
&& result
->opcode
!= I_CALL
) {
830 nasm_error(ERR_NONFATAL
, "invalid use of FAR operand specifier");
833 value
= evaluate(stdscan
, NULL
, &tokval
,
835 critical
, nasm_error
, &hints
);
837 if (op
->opflags
& OPFLAG_FORWARD
) {
838 result
->forw_ref
= true;
840 if (!value
) /* Error in evaluator */
842 if (i
== ':' && mref
) { /* it was seg:offset */
844 * Process the segment override.
846 if (value
[1].type
!= 0 ||
848 !IS_SREG(value
->type
))
849 nasm_error(ERR_NONFATAL
, "invalid segment override");
850 else if (result
->prefixes
[PPS_SEG
])
851 nasm_error(ERR_NONFATAL
,
852 "instruction has conflicting segment overrides");
854 result
->prefixes
[PPS_SEG
] = value
->type
;
855 if (IS_FSGS(value
->type
))
856 op
->eaflags
|= EAF_FSGS
;
859 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
860 while (i
== TOKEN_SPECIAL
|| i
== TOKEN_PREFIX
) {
861 process_size_override(result
, op
);
862 i
= stdscan(NULL
, &tokval
);
864 value
= evaluate(stdscan
, NULL
, &tokval
,
866 critical
, nasm_error
, &hints
);
868 if (op
->opflags
& OPFLAG_FORWARD
) {
869 result
->forw_ref
= true;
871 /* and get the offset */
872 if (!value
) /* Error in evaluator */
877 if (mref
&& bracket
&& i
== ',') {
878 /* [seg:base+offset,index*scale] syntax (mib) */
880 operand o1
, o2
; /* Partial operands */
882 if (parse_mref(&o1
, value
))
885 i
= stdscan(NULL
, &tokval
); /* Eat comma */
886 value
= evaluate(stdscan
, NULL
, &tokval
, &op
->opflags
,
887 critical
, nasm_error
, &hints
);
890 if (parse_mref(&o2
, value
))
893 if (o2
.basereg
!= -1 && o2
.indexreg
== -1) {
894 o2
.indexreg
= o2
.basereg
;
899 if (o1
.indexreg
!= -1 || o2
.basereg
!= -1 || o2
.offset
!= 0 ||
900 o2
.segment
!= NO_SEG
|| o2
.wrt
!= NO_SEG
) {
901 nasm_error(ERR_NONFATAL
, "invalid mib expression");
905 op
->basereg
= o1
.basereg
;
906 op
->indexreg
= o2
.indexreg
;
907 op
->scale
= o2
.scale
;
908 op
->offset
= o1
.offset
;
909 op
->segment
= o1
.segment
;
912 if (op
->basereg
!= -1) {
913 op
->hintbase
= op
->basereg
;
914 op
->hinttype
= EAH_MAKEBASE
;
915 } else if (op
->indexreg
!= -1) {
916 op
->hintbase
= op
->indexreg
;
917 op
->hinttype
= EAH_NOTBASE
;
920 op
->hinttype
= EAH_NOHINT
;
927 if (mref
&& bracket
) { /* find ] at the end */
929 nasm_error(ERR_NONFATAL
, "parser: expecting ]");
931 } else { /* we got the required ] */
932 i
= stdscan(NULL
, &tokval
);
933 if ((i
== TOKEN_DECORATOR
) || (i
== TOKEN_OPMASK
)) {
935 * according to AVX512 spec, broacast or opmask decorator
936 * is expected for memory reference operands
938 if (tokval
.t_flag
& TFLAG_BRDCAST
) {
939 brace_flags
|= GEN_BRDCAST(0);
940 i
= stdscan(NULL
, &tokval
);
941 } else if (i
== TOKEN_OPMASK
) {
942 brace_flags
|= VAL_OPMASK(nasm_regvals
[tokval
.t_integer
]);
943 i
= stdscan(NULL
, &tokval
);
945 nasm_error(ERR_NONFATAL
, "broadcast or opmask "
946 "decorator expected inside braces");
951 if (i
!= 0 && i
!= ',') {
952 nasm_error(ERR_NONFATAL
, "comma or end of line expected");
956 } else { /* immediate operand */
957 if (i
!= 0 && i
!= ',' && i
!= ':' &&
958 i
!= TOKEN_DECORATOR
&& i
!= TOKEN_OPMASK
) {
959 nasm_error(ERR_NONFATAL
, "comma, colon, decorator or end of "
960 "line expected after operand");
962 } else if (i
== ':') {
964 } else if (i
== TOKEN_DECORATOR
|| i
== TOKEN_OPMASK
) {
965 /* parse opmask (and zeroing) after an operand */
966 recover
= parse_braces(&brace_flags
);
970 do { /* error recovery */
971 i
= stdscan(NULL
, &tokval
);
972 } while (i
!= 0 && i
!= ',');
976 * now convert the exprs returned from evaluate()
977 * into operand descriptions...
979 op
->decoflags
|= brace_flags
;
981 if (mref
) { /* it's a memory reference */
982 /* A mib reference was fully parsed already */
984 if (parse_mref(op
, value
))
986 op
->hintbase
= hints
.base
;
987 op
->hinttype
= hints
.type
;
990 } else { /* it's not a memory reference */
991 if (is_just_unknown(value
)) { /* it's immediate but unknown */
992 op
->type
|= IMMEDIATE
;
993 op
->opflags
|= OPFLAG_UNKNOWN
;
994 op
->offset
= 0; /* don't care */
995 op
->segment
= NO_SEG
; /* don't care again */
996 op
->wrt
= NO_SEG
; /* still don't care */
998 if(optimizing
>= 0 && !(op
->type
& STRICT
)) {
1001 UNITY
| SBYTEWORD
| SBYTEDWORD
| UDWORD
| SDWORD
;
1003 } else if (is_reloc(value
)) { /* it's immediate */
1004 op
->type
|= IMMEDIATE
;
1005 op
->offset
= reloc_value(value
);
1006 op
->segment
= reloc_seg(value
);
1007 op
->wrt
= reloc_wrt(value
);
1009 if (is_simple(value
)) {
1010 uint64_t n
= reloc_value(value
);
1013 if (optimizing
>= 0 &&
1014 !(op
->type
& STRICT
)) {
1015 if ((uint32_t) (n
+ 128) <= 255)
1016 op
->type
|= SBYTEDWORD
;
1017 if ((uint16_t) (n
+ 128) <= 255)
1018 op
->type
|= SBYTEWORD
;
1019 if (n
<= 0xFFFFFFFF)
1021 if (n
+ 0x80000000 <= 0xFFFFFFFF)
1025 } else if(value
->type
== EXPR_RDSAE
) {
1027 * it's not an operand but a rounding or SAE decorator.
1028 * put the decorator information in the (opflag_t) type field
1029 * of previous operand.
1032 switch (value
->value
) {
1038 op
->decoflags
|= (value
->value
== BRC_SAE
? SAE
: ER
);
1039 result
->evex_rm
= value
->value
;
1042 nasm_error(ERR_NONFATAL
, "invalid decorator");
1045 } else { /* it's a register */
1048 if (value
->type
>= EXPR_SIMPLE
|| value
->value
!= 1) {
1049 nasm_error(ERR_NONFATAL
, "invalid operand type");
1054 * check that its only 1 register, not an expression...
1056 for (i
= 1; value
[i
].type
; i
++)
1057 if (value
[i
].value
) {
1058 nasm_error(ERR_NONFATAL
, "invalid operand type");
1062 /* clear overrides, except TO which applies to FPU regs */
1063 if (op
->type
& ~TO
) {
1065 * we want to produce a warning iff the specified size
1066 * is different from the register size
1068 rs
= op
->type
& SIZE_MASK
;
1073 op
->type
|= REGISTER
;
1074 op
->type
|= nasm_reg_flags
[value
->type
];
1075 op
->decoflags
|= brace_flags
;
1076 op
->basereg
= value
->type
;
1078 if (rs
&& (op
->type
& SIZE_MASK
) != rs
)
1079 nasm_error(ERR_WARNING
| ERR_PASS1
,
1080 "register size specification ignored");
1084 /* remember the position of operand having broadcasting/ER mode */
1085 if (op
->decoflags
& (BRDCAST_MASK
| ER
| SAE
))
1086 result
->evex_brerop
= opnum
;
1089 result
->operands
= opnum
; /* set operand count */
1091 /* clear remaining operands */
1092 while (opnum
< MAX_OPERANDS
)
1093 result
->oprs
[opnum
++].type
= 0;
1096 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
1098 switch (result
->opcode
) {
1100 result
->opcode
= I_RESB
;
1101 result
->oprs
[0].offset
*= 2;
1104 result
->opcode
= I_RESB
;
1105 result
->oprs
[0].offset
*= 4;
1108 result
->opcode
= I_RESB
;
1109 result
->oprs
[0].offset
*= 8;
1112 result
->opcode
= I_RESB
;
1113 result
->oprs
[0].offset
*= 10;
1116 result
->opcode
= I_RESB
;
1117 result
->oprs
[0].offset
*= 16;
1120 result
->opcode
= I_RESB
;
1121 result
->oprs
[0].offset
*= 32;
1130 result
->opcode
= I_none
;
1134 static int is_comma_next(void)
1141 i
= stdscan(NULL
, &tv
);
1144 return (i
== ',' || i
== ';' || !i
);
1147 void cleanup_insn(insn
* i
)
1151 while ((e
= i
->eops
)) {
1153 if (e
->type
== EOT_DB_STRING_FREE
)
1154 nasm_free(e
->stringval
);