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
)
105 nasm_error(ERR_PANIC
, "Invalid value %d passed to prefix_slot()", prefix
);
110 static void process_size_override(insn
*result
, operand
*op
)
112 if (tasm_compatible_mode
) {
113 switch ((int)tokval
.t_integer
) {
114 /* For TASM compatibility a size override inside the
115 * brackets changes the size of the operand, not the
116 * address type of the operand as it does in standard
117 * NASM syntax. Hence:
119 * mov eax,[DWORD val]
121 * is valid syntax in TASM compatibility mode. Note that
122 * you lose the ability to override the default address
123 * type for the instruction, but we never use anything
124 * but 32-bit flat model addressing in our code.
146 nasm_error(ERR_NONFATAL
,
147 "invalid operand size specification");
151 /* Standard NASM compatible syntax */
152 switch ((int)tokval
.t_integer
) {
154 op
->eaflags
|= EAF_TIMESTWO
;
157 op
->eaflags
|= EAF_REL
;
160 op
->eaflags
|= EAF_ABS
;
164 op
->eaflags
|= EAF_BYTEOFFS
;
169 if (result
->prefixes
[PPS_ASIZE
] &&
170 result
->prefixes
[PPS_ASIZE
] != tokval
.t_integer
)
171 nasm_error(ERR_NONFATAL
,
172 "conflicting address size specifications");
174 result
->prefixes
[PPS_ASIZE
] = tokval
.t_integer
;
178 op
->eaflags
|= EAF_WORDOFFS
;
183 op
->eaflags
|= EAF_WORDOFFS
;
187 op
->eaflags
|= EAF_WORDOFFS
;
190 nasm_error(ERR_NONFATAL
, "invalid size specification in"
191 " effective address");
198 * when two or more decorators follow a register operand,
199 * consecutive decorators are parsed here.
200 * opmask and zeroing decorators can be placed in any order.
201 * e.g. zmm1 {k2}{z} or zmm2 {z,k3}
202 * decorator(s) are placed at the end of an operand.
204 static bool parse_braces(decoflags_t
*decoflags
)
207 bool recover
= false;
211 if (i
== TOKEN_OPMASK
) {
212 if (*decoflags
& OPMASK_MASK
) {
213 nasm_error(ERR_NONFATAL
, "opmask k%lu is already set",
214 *decoflags
& OPMASK_MASK
);
215 *decoflags
&= ~OPMASK_MASK
;
217 *decoflags
|= VAL_OPMASK(nasm_regvals
[tokval
.t_integer
]);
218 } else if (i
== TOKEN_DECORATOR
) {
219 switch (tokval
.t_integer
) {
222 * according to AVX512 spec, only zeroing/merging decorator
223 * is supported with opmask
225 *decoflags
|= GEN_Z(0);
228 nasm_error(ERR_NONFATAL
, "{%s} is not an expected decorator",
232 } else if (i
== ',' || i
== TOKEN_EOS
){
235 nasm_error(ERR_NONFATAL
, "only a series of valid decorators"
240 i
= stdscan(NULL
, &tokval
);
246 static int parse_mref(operand
*op
, const expr
*e
)
248 int b
, i
, s
; /* basereg, indexreg, scale */
249 int64_t o
; /* offset */
254 if (e
->type
&& e
->type
<= EXPR_REG_END
) { /* this bit's a register */
255 bool is_gpr
= is_class(REG_GPR
,nasm_reg_flags
[e
->type
]);
257 if (is_gpr
&& e
->value
== 1)
258 b
= e
->type
; /* It can be basereg */
259 else /* No, it has to be indexreg */
260 i
= e
->type
, s
= e
->value
;
263 if (e
->type
&& e
->type
<= EXPR_REG_END
) { /* it's a 2nd register */
264 bool is_gpr
= is_class(REG_GPR
,nasm_reg_flags
[e
->type
]);
266 if (b
!= -1) /* If the first was the base, ... */
267 i
= e
->type
, s
= e
->value
; /* second has to be indexreg */
269 else if (!is_gpr
|| e
->value
!= 1) {
270 /* If both want to be index */
271 nasm_error(ERR_NONFATAL
,
272 "invalid effective address: two index registers");
278 if (e
->type
!= 0) { /* is there an offset? */
279 if (e
->type
<= EXPR_REG_END
) { /* in fact, is there an error? */
280 nasm_error(ERR_NONFATAL
,
281 "beroset-p-603-invalid effective address");
284 if (e
->type
== EXPR_UNKNOWN
) {
285 op
->opflags
|= OPFLAG_UNKNOWN
;
286 o
= 0; /* doesn't matter what */
287 op
->wrt
= NO_SEG
; /* nor this */
288 op
->segment
= NO_SEG
; /* or this */
290 e
++; /* go to the end of the line */
292 if (e
->type
== EXPR_SIMPLE
) {
296 if (e
->type
== EXPR_WRT
) {
302 * Look for a segment base type.
304 if (e
->type
&& e
->type
< EXPR_SEGBASE
) {
305 nasm_error(ERR_NONFATAL
,
306 "beroset-p-630-invalid effective address");
309 while (e
->type
&& e
->value
== 0)
311 if (e
->type
&& e
->value
!= 1) {
312 nasm_error(ERR_NONFATAL
,
313 "beroset-p-637-invalid effective address");
317 op
->segment
= e
->type
- EXPR_SEGBASE
;
320 op
->segment
= NO_SEG
;
321 while (e
->type
&& e
->value
== 0)
324 nasm_error(ERR_NONFATAL
,
325 "beroset-p-650-invalid effective address");
333 op
->segment
= NO_SEG
;
336 if (e
->type
!= 0) { /* there'd better be nothing left! */
337 nasm_error(ERR_NONFATAL
,
338 "beroset-p-663-invalid effective address");
349 static void mref_set_optype(operand
*op
)
352 int i
= op
->indexreg
;
355 /* It is memory, but it can match any r/m operand */
356 op
->type
|= MEMORY_ANY
;
358 if (b
== -1 && (i
== -1 || s
== 0)) {
359 int is_rel
= globalbits
== 64 &&
360 !(op
->eaflags
& EAF_ABS
) &&
362 !(op
->eaflags
& EAF_FSGS
)) ||
363 (op
->eaflags
& EAF_REL
));
365 op
->type
|= is_rel
? IP_REL
: MEM_OFFS
;
369 opflags_t iclass
= nasm_reg_flags
[i
];
371 if (is_class(XMMREG
,iclass
))
373 else if (is_class(YMMREG
,iclass
))
375 else if (is_class(ZMMREG
,iclass
))
380 insn
*parse_line(int pass
, char *buffer
, insn
*result
, ldfunc ldef
)
382 bool insn_is_label
= false;
383 struct eval_hints hints
;
391 result
->forw_ref
= false;
395 i
= stdscan(NULL
, &tokval
);
397 result
->label
= NULL
; /* Assume no label */
398 result
->eops
= NULL
; /* must do this, whatever happens */
399 result
->operands
= 0; /* must initialize this */
400 result
->evex_rm
= 0; /* Ensure EVEX rounding mode is reset */
401 result
->evex_brerop
= -1; /* Reset EVEX broadcasting/ER op position */
403 /* Ignore blank lines */
410 (i
!= TOKEN_REG
|| !IS_SREG(tokval
.t_integer
))) {
411 nasm_error(ERR_NONFATAL
,
412 "label or instruction expected at start of line");
416 if (i
== TOKEN_ID
|| (insn_is_label
&& i
== TOKEN_INSN
)) {
417 /* there's a label here */
419 result
->label
= tokval
.t_charptr
;
420 i
= stdscan(NULL
, &tokval
);
421 if (i
== ':') { /* skip over the optional colon */
422 i
= stdscan(NULL
, &tokval
);
424 nasm_error(ERR_WARNING
| ERR_WARN_OL
| ERR_PASS1
,
425 "label alone on a line without a colon might be in error");
427 if (i
!= TOKEN_INSN
|| tokval
.t_integer
!= I_EQU
) {
429 * FIXME: location->segment could be NO_SEG, in which case
430 * it is possible we should be passing 'abs_seg'. Look into this.
431 * Work out whether that is *really* what we should be doing.
432 * Generally fix things. I think this is right as it is, but
433 * am still not certain.
435 ldef(result
->label
, in_abs_seg
? abs_seg
: location
->segment
,
436 location
->offset
, NULL
, true, false);
440 /* Just a label here */
444 nasm_build_assert(P_none
!= 0);
445 memset(result
->prefixes
, P_none
, sizeof(result
->prefixes
));
448 while (i
== TOKEN_PREFIX
||
449 (i
== TOKEN_REG
&& IS_SREG(tokval
.t_integer
))) {
453 * Handle special case: the TIMES prefix.
455 if (i
== TOKEN_PREFIX
&& tokval
.t_integer
== P_TIMES
) {
458 i
= stdscan(NULL
, &tokval
);
459 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass0
, nasm_error
, NULL
);
461 if (!value
) /* Error in evaluator */
463 if (!is_simple(value
)) {
464 nasm_error(ERR_NONFATAL
,
465 "non-constant argument supplied to TIMES");
468 result
->times
= value
->value
;
469 if (value
->value
< 0 && pass0
== 2) {
470 nasm_error(ERR_NONFATAL
, "TIMES value %"PRId64
" is negative",
476 int slot
= prefix_slot(tokval
.t_integer
);
477 if (result
->prefixes
[slot
]) {
478 if (result
->prefixes
[slot
] == tokval
.t_integer
)
479 nasm_error(ERR_WARNING
| ERR_PASS1
,
480 "instruction has redundant prefixes");
482 nasm_error(ERR_NONFATAL
,
483 "instruction has conflicting prefixes");
485 result
->prefixes
[slot
] = tokval
.t_integer
;
486 i
= stdscan(NULL
, &tokval
);
490 if (i
!= TOKEN_INSN
) {
494 for (j
= 0; j
< MAXPREFIX
; j
++) {
495 if ((pfx
= result
->prefixes
[j
]) != P_none
)
499 if (i
== 0 && pfx
!= P_none
) {
501 * Instruction prefixes are present, but no actual
502 * instruction. This is allowed: at this point we
503 * invent a notional instruction of RESB 0.
505 result
->opcode
= I_RESB
;
506 result
->operands
= 1;
507 result
->oprs
[0].type
= IMMEDIATE
;
508 result
->oprs
[0].offset
= 0L;
509 result
->oprs
[0].segment
= result
->oprs
[0].wrt
= NO_SEG
;
512 nasm_error(ERR_NONFATAL
, "parser: instruction expected");
517 result
->opcode
= tokval
.t_integer
;
518 result
->condition
= tokval
.t_inttwo
;
521 * INCBIN cannot be satisfied with incorrectly
522 * evaluated operands, since the correct values _must_ be known
523 * on the first pass. Hence, even in pass one, we set the
524 * `critical' flag on calling evaluate(), so that it will bomb
525 * out on undefined symbols.
527 if (result
->opcode
== I_INCBIN
) {
528 critical
= (pass0
< 2 ? 1 : 2);
531 critical
= (pass
== 2 ? 2 : 0);
533 if (result
->opcode
== I_DB
|| result
->opcode
== I_DW
||
534 result
->opcode
== I_DD
|| result
->opcode
== I_DQ
||
535 result
->opcode
== I_DT
|| result
->opcode
== I_DO
||
536 result
->opcode
== I_DY
|| result
->opcode
== I_INCBIN
) {
537 extop
*eop
, **tail
= &result
->eops
, **fixptr
;
541 result
->eops_float
= false;
544 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
547 i
= stdscan(NULL
, &tokval
);
550 else if (first
&& i
== ':') {
551 insn_is_label
= true;
556 eop
= *tail
= nasm_malloc(sizeof(extop
));
559 eop
->type
= EOT_NOTHING
;
564 * is_comma_next() here is to distinguish this from
565 * a string used as part of an expression...
567 if (i
== TOKEN_STR
&& is_comma_next()) {
568 eop
->type
= EOT_DB_STRING
;
569 eop
->stringval
= tokval
.t_charptr
;
570 eop
->stringlen
= tokval
.t_inttwo
;
571 i
= stdscan(NULL
, &tokval
); /* eat the comma */
572 } else if (i
== TOKEN_STRFUNC
) {
574 const char *funcname
= tokval
.t_charptr
;
575 enum strfunc func
= tokval
.t_integer
;
576 i
= stdscan(NULL
, &tokval
);
579 i
= stdscan(NULL
, &tokval
);
581 if (i
!= TOKEN_STR
) {
582 nasm_error(ERR_NONFATAL
,
583 "%s must be followed by a string constant",
585 eop
->type
= EOT_NOTHING
;
587 eop
->type
= EOT_DB_STRING_FREE
;
589 string_transform(tokval
.t_charptr
, tokval
.t_inttwo
,
590 &eop
->stringval
, func
);
591 if (eop
->stringlen
== (size_t)-1) {
592 nasm_error(ERR_NONFATAL
, "invalid string for transform");
593 eop
->type
= EOT_NOTHING
;
596 if (parens
&& i
&& i
!= ')') {
597 i
= stdscan(NULL
, &tokval
);
599 nasm_error(ERR_NONFATAL
, "unterminated %s function",
604 i
= stdscan(NULL
, &tokval
);
605 } else if (i
== '-' || i
== '+') {
606 char *save
= stdscan_get();
608 sign
= (i
== '-') ? -1 : 1;
609 i
= stdscan(NULL
, &tokval
);
610 if (i
!= TOKEN_FLOAT
) {
612 i
= tokval
.t_type
= token
;
617 } else if (i
== TOKEN_FLOAT
) {
619 eop
->type
= EOT_DB_STRING
;
620 result
->eops_float
= true;
622 eop
->stringlen
= idata_bytes(result
->opcode
);
623 if (eop
->stringlen
> 16) {
624 nasm_error(ERR_NONFATAL
, "floating-point constant"
625 " encountered in DY instruction");
627 } else if (eop
->stringlen
< 1) {
628 nasm_error(ERR_NONFATAL
, "floating-point constant"
629 " encountered in unknown instruction");
631 * fix suggested by Pedro Gimeno... original line was:
632 * eop->type = EOT_NOTHING;
637 eop
= nasm_realloc(eop
, sizeof(extop
) + eop
->stringlen
);
640 eop
->stringval
= (char *)eop
+ sizeof(extop
);
641 if (!eop
->stringlen
||
642 !float_const(tokval
.t_charptr
, sign
,
643 (uint8_t *)eop
->stringval
,
644 eop
->stringlen
, nasm_error
))
645 eop
->type
= EOT_NOTHING
;
646 i
= stdscan(NULL
, &tokval
); /* eat the comma */
648 /* anything else, assume it is an expression */
652 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
,
653 critical
, nasm_error
, NULL
);
655 if (!value
) /* Error in evaluator */
657 if (is_unknown(value
)) {
658 eop
->type
= EOT_DB_NUMBER
;
659 eop
->offset
= 0; /* doesn't matter what we put */
660 eop
->segment
= eop
->wrt
= NO_SEG
; /* likewise */
661 } else if (is_reloc(value
)) {
662 eop
->type
= EOT_DB_NUMBER
;
663 eop
->offset
= reloc_value(value
);
664 eop
->segment
= reloc_seg(value
);
665 eop
->wrt
= reloc_wrt(value
);
667 nasm_error(ERR_NONFATAL
,
668 "operand %d: expression is not simple"
669 " or relocatable", oper_num
);
674 * We're about to call stdscan(), which will eat the
675 * comma that we're currently sitting on between
676 * arguments. However, we'd better check first that it
679 if (i
== TOKEN_EOS
) /* also could be EOL */
682 nasm_error(ERR_NONFATAL
, "comma expected after operand %d",
688 if (result
->opcode
== I_INCBIN
) {
690 * Correct syntax for INCBIN is that there should be
691 * one string operand, followed by one or two numeric
694 if (!result
->eops
|| result
->eops
->type
!= EOT_DB_STRING
)
695 nasm_error(ERR_NONFATAL
, "`incbin' expects a file name");
696 else if (result
->eops
->next
&&
697 result
->eops
->next
->type
!= EOT_DB_NUMBER
)
698 nasm_error(ERR_NONFATAL
, "`incbin': second parameter is"
700 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
701 result
->eops
->next
->next
->type
!= EOT_DB_NUMBER
)
702 nasm_error(ERR_NONFATAL
, "`incbin': third parameter is"
704 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
705 result
->eops
->next
->next
->next
)
706 nasm_error(ERR_NONFATAL
,
707 "`incbin': more than three parameters");
711 * If we reach here, one of the above errors happened.
712 * Throw the instruction away.
715 } else /* DB ... */ if (oper_num
== 0)
716 nasm_error(ERR_WARNING
| ERR_PASS1
,
717 "no operand for data declaration");
719 result
->operands
= oper_num
;
725 * Now we begin to parse the operands. There may be up to four
726 * of these, separated by commas, and terminated by a zero token.
729 for (opnum
= 0; opnum
< MAX_OPERANDS
; opnum
++) {
730 operand
*op
= &result
->oprs
[opnum
];
731 expr
*value
; /* used most of the time */
732 bool mref
; /* is this going to be a memory ref? */
733 bool bracket
; /* is it a [] mref, or a & mref? */
734 bool mib
; /* compound (mib) mref? */
736 decoflags_t brace_flags
= 0; /* flags for decorators in braces */
738 op
->disp_size
= 0; /* have to zero this whatever */
739 op
->eaflags
= 0; /* and this */
743 i
= stdscan(NULL
, &tokval
);
745 break; /* end of operands: get out of here */
746 else if (first
&& i
== ':') {
747 insn_is_label
= true;
751 op
->type
= 0; /* so far, no override */
752 while (i
== TOKEN_SPECIAL
) { /* size specifiers */
753 switch ((int)tokval
.t_integer
) {
755 if (!setsize
) /* we want to use only the first */
811 nasm_error(ERR_NONFATAL
, "invalid operand size specification");
813 i
= stdscan(NULL
, &tokval
);
816 if (i
== '[' || i
== '&') { /* memory reference */
818 bracket
= (i
== '[');
819 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
820 while (i
== TOKEN_SPECIAL
|| i
== TOKEN_PREFIX
) {
821 process_size_override(result
, op
);
822 i
= stdscan(NULL
, &tokval
);
824 /* when a comma follows an opening bracket - [ , eax*4] */
826 /* treat as if there is a zero displacement virtually */
827 tokval
.t_type
= TOKEN_NUM
;
828 tokval
.t_integer
= 0;
829 stdscan_set(stdscan_get() - 1); /* rewind the comma */
831 } else { /* immediate operand, or register */
833 bracket
= false; /* placate optimisers */
836 if ((op
->type
& FAR
) && !mref
&&
837 result
->opcode
!= I_JMP
&& result
->opcode
!= I_CALL
) {
838 nasm_error(ERR_NONFATAL
, "invalid use of FAR operand specifier");
841 value
= evaluate(stdscan
, NULL
, &tokval
,
843 critical
, nasm_error
, &hints
);
845 if (op
->opflags
& OPFLAG_FORWARD
) {
846 result
->forw_ref
= true;
848 if (!value
) /* Error in evaluator */
850 if (i
== ':' && mref
) { /* it was seg:offset */
852 * Process the segment override.
854 if (value
[1].type
!= 0 ||
856 !IS_SREG(value
->type
))
857 nasm_error(ERR_NONFATAL
, "invalid segment override");
858 else if (result
->prefixes
[PPS_SEG
])
859 nasm_error(ERR_NONFATAL
,
860 "instruction has conflicting segment overrides");
862 result
->prefixes
[PPS_SEG
] = value
->type
;
863 if (IS_FSGS(value
->type
))
864 op
->eaflags
|= EAF_FSGS
;
867 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
868 while (i
== TOKEN_SPECIAL
|| i
== TOKEN_PREFIX
) {
869 process_size_override(result
, op
);
870 i
= stdscan(NULL
, &tokval
);
872 value
= evaluate(stdscan
, NULL
, &tokval
,
874 critical
, nasm_error
, &hints
);
876 if (op
->opflags
& OPFLAG_FORWARD
) {
877 result
->forw_ref
= true;
879 /* and get the offset */
880 if (!value
) /* Error in evaluator */
885 if (mref
&& bracket
&& i
== ',') {
886 /* [seg:base+offset,index*scale] syntax (mib) */
888 operand o1
, o2
; /* Partial operands */
890 if (parse_mref(&o1
, value
))
893 i
= stdscan(NULL
, &tokval
); /* Eat comma */
894 value
= evaluate(stdscan
, NULL
, &tokval
, &op
->opflags
,
895 critical
, nasm_error
, &hints
);
898 if (parse_mref(&o2
, value
))
901 if (o2
.basereg
!= -1 && o2
.indexreg
== -1) {
902 o2
.indexreg
= o2
.basereg
;
907 if (o1
.indexreg
!= -1 || o2
.basereg
!= -1 || o2
.offset
!= 0 ||
908 o2
.segment
!= NO_SEG
|| o2
.wrt
!= NO_SEG
) {
909 nasm_error(ERR_NONFATAL
, "invalid mib expression");
913 op
->basereg
= o1
.basereg
;
914 op
->indexreg
= o2
.indexreg
;
915 op
->scale
= o2
.scale
;
916 op
->offset
= o1
.offset
;
917 op
->segment
= o1
.segment
;
920 if (op
->basereg
!= -1) {
921 op
->hintbase
= op
->basereg
;
922 op
->hinttype
= EAH_MAKEBASE
;
923 } else if (op
->indexreg
!= -1) {
924 op
->hintbase
= op
->indexreg
;
925 op
->hinttype
= EAH_NOTBASE
;
928 op
->hinttype
= EAH_NOHINT
;
935 if (mref
&& bracket
) { /* find ] at the end */
937 nasm_error(ERR_NONFATAL
, "parser: expecting ]");
939 } else { /* we got the required ] */
940 i
= stdscan(NULL
, &tokval
);
941 if ((i
== TOKEN_DECORATOR
) || (i
== TOKEN_OPMASK
)) {
943 * according to AVX512 spec, broacast or opmask decorator
944 * is expected for memory reference operands
946 if (tokval
.t_flag
& TFLAG_BRDCAST
) {
947 brace_flags
|= GEN_BRDCAST(0);
948 i
= stdscan(NULL
, &tokval
);
949 } else if (i
== TOKEN_OPMASK
) {
950 brace_flags
|= VAL_OPMASK(nasm_regvals
[tokval
.t_integer
]);
951 i
= stdscan(NULL
, &tokval
);
953 nasm_error(ERR_NONFATAL
, "broadcast or opmask "
954 "decorator expected inside braces");
959 if (i
!= 0 && i
!= ',') {
960 nasm_error(ERR_NONFATAL
, "comma or end of line expected");
964 } else { /* immediate operand */
965 if (i
!= 0 && i
!= ',' && i
!= ':' &&
966 i
!= TOKEN_DECORATOR
&& i
!= TOKEN_OPMASK
) {
967 nasm_error(ERR_NONFATAL
, "comma, colon, decorator or end of "
968 "line expected after operand");
970 } else if (i
== ':') {
972 } else if (i
== TOKEN_DECORATOR
|| i
== TOKEN_OPMASK
) {
973 /* parse opmask (and zeroing) after an operand */
974 recover
= parse_braces(&brace_flags
);
978 do { /* error recovery */
979 i
= stdscan(NULL
, &tokval
);
980 } while (i
!= 0 && i
!= ',');
984 * now convert the exprs returned from evaluate()
985 * into operand descriptions...
987 op
->decoflags
|= brace_flags
;
989 if (mref
) { /* it's a memory reference */
990 /* A mib reference was fully parsed already */
992 if (parse_mref(op
, value
))
994 op
->hintbase
= hints
.base
;
995 op
->hinttype
= hints
.type
;
998 } else { /* it's not a memory reference */
999 if (is_just_unknown(value
)) { /* it's immediate but unknown */
1000 op
->type
|= IMMEDIATE
;
1001 op
->opflags
|= OPFLAG_UNKNOWN
;
1002 op
->offset
= 0; /* don't care */
1003 op
->segment
= NO_SEG
; /* don't care again */
1004 op
->wrt
= NO_SEG
; /* still don't care */
1006 if(optimizing
>= 0 && !(op
->type
& STRICT
)) {
1009 UNITY
| SBYTEWORD
| SBYTEDWORD
| UDWORD
| SDWORD
;
1011 } else if (is_reloc(value
)) { /* it's immediate */
1012 op
->type
|= IMMEDIATE
;
1013 op
->offset
= reloc_value(value
);
1014 op
->segment
= reloc_seg(value
);
1015 op
->wrt
= reloc_wrt(value
);
1017 if (is_simple(value
)) {
1018 uint64_t n
= reloc_value(value
);
1021 if (optimizing
>= 0 &&
1022 !(op
->type
& STRICT
)) {
1023 if ((uint32_t) (n
+ 128) <= 255)
1024 op
->type
|= SBYTEDWORD
;
1025 if ((uint16_t) (n
+ 128) <= 255)
1026 op
->type
|= SBYTEWORD
;
1027 if (n
<= 0xFFFFFFFF)
1029 if (n
+ 0x80000000 <= 0xFFFFFFFF)
1033 } else if(value
->type
== EXPR_RDSAE
) {
1035 * it's not an operand but a rounding or SAE decorator.
1036 * put the decorator information in the (opflag_t) type field
1037 * of previous operand.
1040 switch (value
->value
) {
1046 op
->decoflags
|= (value
->value
== BRC_SAE
? SAE
: ER
);
1047 result
->evex_rm
= value
->value
;
1050 nasm_error(ERR_NONFATAL
, "invalid decorator");
1053 } else { /* it's a register */
1056 if (value
->type
>= EXPR_SIMPLE
|| value
->value
!= 1) {
1057 nasm_error(ERR_NONFATAL
, "invalid operand type");
1062 * check that its only 1 register, not an expression...
1064 for (i
= 1; value
[i
].type
; i
++)
1065 if (value
[i
].value
) {
1066 nasm_error(ERR_NONFATAL
, "invalid operand type");
1070 /* clear overrides, except TO which applies to FPU regs */
1071 if (op
->type
& ~TO
) {
1073 * we want to produce a warning iff the specified size
1074 * is different from the register size
1076 rs
= op
->type
& SIZE_MASK
;
1081 op
->type
|= REGISTER
;
1082 op
->type
|= nasm_reg_flags
[value
->type
];
1083 op
->decoflags
|= brace_flags
;
1084 op
->basereg
= value
->type
;
1086 if (rs
&& (op
->type
& SIZE_MASK
) != rs
)
1087 nasm_error(ERR_WARNING
| ERR_PASS1
,
1088 "register size specification ignored");
1092 /* remember the position of operand having broadcasting/ER mode */
1093 if (op
->decoflags
& (BRDCAST_MASK
| ER
| SAE
))
1094 result
->evex_brerop
= opnum
;
1097 result
->operands
= opnum
; /* set operand count */
1099 /* clear remaining operands */
1100 while (opnum
< MAX_OPERANDS
)
1101 result
->oprs
[opnum
++].type
= 0;
1104 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
1106 switch (result
->opcode
) {
1108 result
->opcode
= I_RESB
;
1109 result
->oprs
[0].offset
*= 2;
1112 result
->opcode
= I_RESB
;
1113 result
->oprs
[0].offset
*= 4;
1116 result
->opcode
= I_RESB
;
1117 result
->oprs
[0].offset
*= 8;
1120 result
->opcode
= I_RESB
;
1121 result
->oprs
[0].offset
*= 10;
1124 result
->opcode
= I_RESB
;
1125 result
->oprs
[0].offset
*= 16;
1128 result
->opcode
= I_RESB
;
1129 result
->oprs
[0].offset
*= 32;
1138 result
->opcode
= I_none
;
1142 static int is_comma_next(void)
1149 i
= stdscan(NULL
, &tv
);
1152 return (i
== ',' || i
== ';' || !i
);
1155 void cleanup_insn(insn
* i
)
1159 while ((e
= i
->eops
)) {
1161 if (e
->type
== EOT_DB_STRING_FREE
)
1162 nasm_free(e
->stringval
);