1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 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);
61 static struct tokenval tokval
;
63 static int prefix_slot(int prefix
)
102 nasm_panic(0, "Invalid value %d passed to prefix_slot()", prefix
);
107 static void process_size_override(insn
*result
, operand
*op
)
109 if (tasm_compatible_mode
) {
110 switch ((int)tokval
.t_integer
) {
111 /* For TASM compatibility a size override inside the
112 * brackets changes the size of the operand, not the
113 * address type of the operand as it does in standard
114 * NASM syntax. Hence:
116 * mov eax,[DWORD val]
118 * is valid syntax in TASM compatibility mode. Note that
119 * you lose the ability to override the default address
120 * type for the instruction, but we never use anything
121 * but 32-bit flat model addressing in our code.
143 nasm_error(ERR_NONFATAL
,
144 "invalid operand size specification");
148 /* Standard NASM compatible syntax */
149 switch ((int)tokval
.t_integer
) {
151 op
->eaflags
|= EAF_TIMESTWO
;
154 op
->eaflags
|= EAF_REL
;
157 op
->eaflags
|= EAF_ABS
;
161 op
->eaflags
|= EAF_BYTEOFFS
;
166 if (result
->prefixes
[PPS_ASIZE
] &&
167 result
->prefixes
[PPS_ASIZE
] != tokval
.t_integer
)
168 nasm_error(ERR_NONFATAL
,
169 "conflicting address size specifications");
171 result
->prefixes
[PPS_ASIZE
] = tokval
.t_integer
;
175 op
->eaflags
|= EAF_WORDOFFS
;
180 op
->eaflags
|= EAF_WORDOFFS
;
184 op
->eaflags
|= EAF_WORDOFFS
;
187 nasm_error(ERR_NONFATAL
, "invalid size specification in"
188 " effective address");
195 * when two or more decorators follow a register operand,
196 * consecutive decorators are parsed here.
197 * opmask and zeroing decorators can be placed in any order.
198 * e.g. zmm1 {k2}{z} or zmm2 {z}{k3}
199 * decorator(s) are placed at the end of an operand.
201 static bool parse_braces(decoflags_t
*decoflags
)
204 bool recover
= false;
208 if (i
== TOKEN_OPMASK
) {
209 if (*decoflags
& OPMASK_MASK
) {
210 nasm_error(ERR_NONFATAL
, "opmask k%"PRIu64
" is already set",
211 *decoflags
& OPMASK_MASK
);
212 *decoflags
&= ~OPMASK_MASK
;
214 *decoflags
|= VAL_OPMASK(nasm_regvals
[tokval
.t_integer
]);
215 } else if (i
== TOKEN_DECORATOR
) {
216 switch (tokval
.t_integer
) {
219 * according to AVX512 spec, only zeroing/merging decorator
220 * is supported with opmask
222 *decoflags
|= GEN_Z(0);
225 nasm_error(ERR_NONFATAL
, "{%s} is not an expected decorator",
229 } else if (i
== ',' || i
== TOKEN_EOS
){
232 nasm_error(ERR_NONFATAL
, "only a series of valid decorators"
237 i
= stdscan(NULL
, &tokval
);
243 static int parse_mref(operand
*op
, const expr
*e
)
245 int b
, i
, s
; /* basereg, indexreg, scale */
246 int64_t o
; /* offset */
250 op
->segment
= op
->wrt
= NO_SEG
;
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");
277 if (e
->type
) { /* is there an offset? */
278 if (e
->type
<= EXPR_REG_END
) { /* in fact, is there an error? */
279 nasm_error(ERR_NONFATAL
,
280 "invalid effective address: impossible register");
283 if (e
->type
== EXPR_UNKNOWN
) {
284 op
->opflags
|= OPFLAG_UNKNOWN
;
285 o
= 0; /* doesn't matter what */
287 e
++; /* go to the end of the line */
289 if (e
->type
== EXPR_SIMPLE
) {
293 if (e
->type
== EXPR_WRT
) {
298 * Look for a segment base type.
300 for (; e
->type
; e
++) {
304 if (e
->type
<= EXPR_REG_END
) {
305 nasm_error(ERR_NONFATAL
,
306 "invalid effective address: too many registers");
308 } else if (e
->type
< EXPR_SEGBASE
) {
309 nasm_error(ERR_NONFATAL
,
310 "invalid effective address: bad subexpression type");
312 } else if (e
->value
== 1) {
313 if (op
->segment
!= NO_SEG
) {
314 nasm_error(ERR_NONFATAL
,
315 "invalid effective address: multiple base segments");
318 op
->segment
= e
->type
- EXPR_SEGBASE
;
319 } else if (e
->value
== -1 &&
320 e
->type
== location
.segment
+ EXPR_SEGBASE
&&
321 !(op
->opflags
& OPFLAG_RELATIVE
)) {
322 op
->opflags
|= OPFLAG_RELATIVE
;
324 nasm_error(ERR_NONFATAL
,
325 "invalid effective address: impossible segment base multiplier");
333 nasm_assert(!e
->type
); /* We should be at the end */
342 static void mref_set_optype(operand
*op
)
345 int i
= op
->indexreg
;
348 /* It is memory, but it can match any r/m operand */
349 op
->type
|= MEMORY_ANY
;
351 if (b
== -1 && (i
== -1 || s
== 0)) {
352 int is_rel
= globalbits
== 64 &&
353 !(op
->eaflags
& EAF_ABS
) &&
355 !(op
->eaflags
& EAF_FSGS
)) ||
356 (op
->eaflags
& EAF_REL
));
358 op
->type
|= is_rel
? IP_REL
: MEM_OFFS
;
362 opflags_t iclass
= nasm_reg_flags
[i
];
364 if (is_class(XMMREG
,iclass
))
366 else if (is_class(YMMREG
,iclass
))
368 else if (is_class(ZMMREG
,iclass
))
374 * Convert an expression vector returned from evaluate() into an
375 * extop structure. Return zero on success.
377 static int value_to_extop(expr
* vect
, extop
*eop
, int32_t myseg
)
379 eop
->type
= EOT_DB_NUMBER
;
381 eop
->segment
= eop
->wrt
= NO_SEG
;
382 eop
->relative
= false;
384 for (; vect
->type
; vect
++) {
385 if (!vect
->value
) /* zero term, safe to ignore */
388 if (vect
->type
<= EXPR_REG_END
) /* false if a register is present */
391 if (vect
->type
== EXPR_UNKNOWN
) /* something we can't resolve yet */
394 if (vect
->type
== EXPR_SIMPLE
) {
395 /* Simple number expression */
396 eop
->offset
+= vect
->value
;
399 if (eop
->wrt
== NO_SEG
&& !eop
->relative
&& vect
->type
== EXPR_WRT
) {
401 eop
->wrt
= vect
->value
;
405 if (!eop
->relative
&&
406 vect
->type
== EXPR_SEGBASE
+ myseg
&& vect
->value
== -1) {
407 /* Expression of the form: foo - $ */
408 eop
->relative
= true;
412 if (eop
->segment
== NO_SEG
&& vect
->type
>= EXPR_SEGBASE
&&
414 eop
->segment
= vect
->type
- EXPR_SEGBASE
;
418 /* Otherwise, badness */
422 /* We got to the end and it was all okay */
426 insn
*parse_line(int pass
, char *buffer
, insn
*result
, ldfunc ldef
)
428 bool insn_is_label
= false;
429 struct eval_hints hints
;
438 result
->forw_ref
= false;
442 i
= stdscan(NULL
, &tokval
);
444 result
->label
= NULL
; /* Assume no label */
445 result
->eops
= NULL
; /* must do this, whatever happens */
446 result
->operands
= 0; /* must initialize this */
447 result
->evex_rm
= 0; /* Ensure EVEX rounding mode is reset */
448 result
->evex_brerop
= -1; /* Reset EVEX broadcasting/ER op position */
450 /* Ignore blank lines */
457 (i
!= TOKEN_REG
|| !IS_SREG(tokval
.t_integer
))) {
458 nasm_error(ERR_NONFATAL
,
459 "label or instruction expected at start of line");
463 if (i
== TOKEN_ID
|| (insn_is_label
&& i
== TOKEN_INSN
)) {
464 /* there's a label here */
466 result
->label
= tokval
.t_charptr
;
467 i
= stdscan(NULL
, &tokval
);
468 if (i
== ':') { /* skip over the optional colon */
469 i
= stdscan(NULL
, &tokval
);
471 nasm_error(ERR_WARNING
| ERR_WARN_OL
| ERR_PASS1
,
472 "label alone on a line without a colon might be in error");
474 if (i
!= TOKEN_INSN
|| tokval
.t_integer
!= I_EQU
) {
476 * FIXME: location.segment could be NO_SEG, in which case
477 * it is possible we should be passing 'abs_seg'. Look into this.
478 * Work out whether that is *really* what we should be doing.
479 * Generally fix things. I think this is right as it is, but
480 * am still not certain.
482 ldef(result
->label
, in_abs_seg
? abs_seg
: location
.segment
,
483 location
.offset
, NULL
, true, false);
487 /* Just a label here */
491 nasm_static_assert(P_none
== 0);
492 memset(result
->prefixes
, P_none
, sizeof(result
->prefixes
));
495 while (i
== TOKEN_PREFIX
||
496 (i
== TOKEN_REG
&& IS_SREG(tokval
.t_integer
))) {
500 * Handle special case: the TIMES prefix.
502 if (i
== TOKEN_PREFIX
&& tokval
.t_integer
== P_TIMES
) {
505 i
= stdscan(NULL
, &tokval
);
506 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass0
, NULL
);
508 if (!value
) /* Error in evaluator */
510 if (!is_simple(value
)) {
511 nasm_error(ERR_NONFATAL
,
512 "non-constant argument supplied to TIMES");
515 result
->times
= value
->value
;
516 if (value
->value
< 0 && pass0
== 2) {
517 nasm_error(ERR_NONFATAL
, "TIMES value %"PRId64
" is negative",
523 int slot
= prefix_slot(tokval
.t_integer
);
524 if (result
->prefixes
[slot
]) {
525 if (result
->prefixes
[slot
] == tokval
.t_integer
)
526 nasm_error(ERR_WARNING
| ERR_PASS1
,
527 "instruction has redundant prefixes");
529 nasm_error(ERR_NONFATAL
,
530 "instruction has conflicting prefixes");
532 result
->prefixes
[slot
] = tokval
.t_integer
;
533 i
= stdscan(NULL
, &tokval
);
537 if (i
!= TOKEN_INSN
) {
541 for (j
= 0; j
< MAXPREFIX
; j
++) {
542 if ((pfx
= result
->prefixes
[j
]) != P_none
)
546 if (i
== 0 && pfx
!= P_none
) {
548 * Instruction prefixes are present, but no actual
549 * instruction. This is allowed: at this point we
550 * invent a notional instruction of RESB 0.
552 result
->opcode
= I_RESB
;
553 result
->operands
= 1;
554 result
->oprs
[0].type
= IMMEDIATE
;
555 result
->oprs
[0].offset
= 0L;
556 result
->oprs
[0].segment
= result
->oprs
[0].wrt
= NO_SEG
;
559 nasm_error(ERR_NONFATAL
, "parser: instruction expected");
564 result
->opcode
= tokval
.t_integer
;
565 result
->condition
= tokval
.t_inttwo
;
568 * INCBIN cannot be satisfied with incorrectly
569 * evaluated operands, since the correct values _must_ be known
570 * on the first pass. Hence, even in pass one, we set the
571 * `critical' flag on calling evaluate(), so that it will bomb
572 * out on undefined symbols.
574 if (result
->opcode
== I_INCBIN
) {
575 critical
= (pass0
< 2 ? 1 : 2);
578 critical
= (pass
== 2 ? 2 : 0);
580 if (result
->opcode
== I_DB
|| result
->opcode
== I_DW
||
581 result
->opcode
== I_DD
|| result
->opcode
== I_DQ
||
582 result
->opcode
== I_DT
|| result
->opcode
== I_DO
||
583 result
->opcode
== I_DY
|| result
->opcode
== I_DZ
||
584 result
->opcode
== I_INCBIN
) {
585 extop
*eop
, **tail
= &result
->eops
, **fixptr
;
589 result
->eops_float
= false;
592 * Begin to read the DB/DW/DD/DQ/DT/DO/DY/DZ/INCBIN operands.
595 i
= stdscan(NULL
, &tokval
);
598 else if (first
&& i
== ':') {
599 insn_is_label
= true;
604 eop
= *tail
= nasm_malloc(sizeof(extop
));
607 eop
->type
= EOT_NOTHING
;
612 * is_comma_next() here is to distinguish this from
613 * a string used as part of an expression...
615 if (i
== TOKEN_STR
&& is_comma_next()) {
616 eop
->type
= EOT_DB_STRING
;
617 eop
->stringval
= tokval
.t_charptr
;
618 eop
->stringlen
= tokval
.t_inttwo
;
619 i
= stdscan(NULL
, &tokval
); /* eat the comma */
620 } else if (i
== TOKEN_STRFUNC
) {
622 const char *funcname
= tokval
.t_charptr
;
623 enum strfunc func
= tokval
.t_integer
;
624 i
= stdscan(NULL
, &tokval
);
627 i
= stdscan(NULL
, &tokval
);
629 if (i
!= TOKEN_STR
) {
630 nasm_error(ERR_NONFATAL
,
631 "%s must be followed by a string constant",
633 eop
->type
= EOT_NOTHING
;
635 eop
->type
= EOT_DB_STRING_FREE
;
637 string_transform(tokval
.t_charptr
, tokval
.t_inttwo
,
638 &eop
->stringval
, func
);
639 if (eop
->stringlen
== (size_t)-1) {
640 nasm_error(ERR_NONFATAL
, "invalid string for transform");
641 eop
->type
= EOT_NOTHING
;
644 if (parens
&& i
&& i
!= ')') {
645 i
= stdscan(NULL
, &tokval
);
647 nasm_error(ERR_NONFATAL
, "unterminated %s function",
652 i
= stdscan(NULL
, &tokval
);
653 } else if (i
== '-' || i
== '+') {
654 char *save
= stdscan_get();
656 sign
= (i
== '-') ? -1 : 1;
657 i
= stdscan(NULL
, &tokval
);
658 if (i
!= TOKEN_FLOAT
) {
660 i
= tokval
.t_type
= token
;
665 } else if (i
== TOKEN_FLOAT
) {
667 eop
->type
= EOT_DB_STRING
;
668 result
->eops_float
= true;
670 eop
->stringlen
= idata_bytes(result
->opcode
);
671 if (eop
->stringlen
> 16) {
672 nasm_error(ERR_NONFATAL
, "floating-point constant"
673 " encountered in DY or DZ instruction");
675 } else if (eop
->stringlen
< 1) {
676 nasm_error(ERR_NONFATAL
, "floating-point constant"
677 " encountered in unknown instruction");
679 * fix suggested by Pedro Gimeno... original line was:
680 * eop->type = EOT_NOTHING;
685 eop
= nasm_realloc(eop
, sizeof(extop
) + eop
->stringlen
);
688 eop
->stringval
= (char *)eop
+ sizeof(extop
);
689 if (!eop
->stringlen
||
690 !float_const(tokval
.t_charptr
, sign
,
691 (uint8_t *)eop
->stringval
, eop
->stringlen
))
692 eop
->type
= EOT_NOTHING
;
693 i
= stdscan(NULL
, &tokval
); /* eat the comma */
695 /* anything else, assume it is an expression */
699 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
,
702 if (!value
) /* Error in evaluator */
704 if (value_to_extop(value
, eop
, location
.segment
)) {
705 nasm_error(ERR_NONFATAL
,
706 "operand %d: expression is not simple or relocatable",
712 * We're about to call stdscan(), which will eat the
713 * comma that we're currently sitting on between
714 * arguments. However, we'd better check first that it
717 if (i
== TOKEN_EOS
) /* also could be EOL */
720 nasm_error(ERR_NONFATAL
, "comma expected after operand %d",
726 if (result
->opcode
== I_INCBIN
) {
728 * Correct syntax for INCBIN is that there should be
729 * one string operand, followed by one or two numeric
732 if (!result
->eops
|| result
->eops
->type
!= EOT_DB_STRING
)
733 nasm_error(ERR_NONFATAL
, "`incbin' expects a file name");
734 else if (result
->eops
->next
&&
735 result
->eops
->next
->type
!= EOT_DB_NUMBER
)
736 nasm_error(ERR_NONFATAL
, "`incbin': second parameter is"
738 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
739 result
->eops
->next
->next
->type
!= EOT_DB_NUMBER
)
740 nasm_error(ERR_NONFATAL
, "`incbin': third parameter is"
742 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
743 result
->eops
->next
->next
->next
)
744 nasm_error(ERR_NONFATAL
,
745 "`incbin': more than three parameters");
749 * If we reach here, one of the above errors happened.
750 * Throw the instruction away.
753 } else /* DB ... */ if (oper_num
== 0)
754 nasm_error(ERR_WARNING
| ERR_PASS1
,
755 "no operand for data declaration");
757 result
->operands
= oper_num
;
763 * Now we begin to parse the operands. There may be up to four
764 * of these, separated by commas, and terminated by a zero token.
767 for (opnum
= 0; opnum
< MAX_OPERANDS
; opnum
++) {
768 operand
*op
= &result
->oprs
[opnum
];
769 expr
*value
; /* used most of the time */
770 bool mref
; /* is this going to be a memory ref? */
771 bool bracket
; /* is it a [] mref, or a & mref? */
772 bool mib
; /* compound (mib) mref? */
774 decoflags_t brace_flags
= 0; /* flags for decorators in braces */
776 op
->disp_size
= 0; /* have to zero this whatever */
777 op
->eaflags
= 0; /* and this */
781 i
= stdscan(NULL
, &tokval
);
783 break; /* end of operands: get out of here */
784 else if (first
&& i
== ':') {
785 insn_is_label
= true;
789 op
->type
= 0; /* so far, no override */
790 while (i
== TOKEN_SPECIAL
) { /* size specifiers */
791 switch ((int)tokval
.t_integer
) {
793 if (!setsize
) /* we want to use only the first */
849 nasm_error(ERR_NONFATAL
, "invalid operand size specification");
851 i
= stdscan(NULL
, &tokval
);
854 if (i
== '[' || i
== '&') { /* memory reference */
856 bracket
= (i
== '[');
857 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
858 while (i
== TOKEN_SPECIAL
|| i
== TOKEN_PREFIX
) {
859 process_size_override(result
, op
);
860 i
= stdscan(NULL
, &tokval
);
862 /* when a comma follows an opening bracket - [ , eax*4] */
864 /* treat as if there is a zero displacement virtually */
865 tokval
.t_type
= TOKEN_NUM
;
866 tokval
.t_integer
= 0;
867 stdscan_set(stdscan_get() - 1); /* rewind the comma */
869 } else { /* immediate operand, or register */
871 bracket
= false; /* placate optimisers */
874 if ((op
->type
& FAR
) && !mref
&&
875 result
->opcode
!= I_JMP
&& result
->opcode
!= I_CALL
) {
876 nasm_error(ERR_NONFATAL
, "invalid use of FAR operand specifier");
879 value
= evaluate(stdscan
, NULL
, &tokval
,
880 &op
->opflags
, critical
, &hints
);
882 if (op
->opflags
& OPFLAG_FORWARD
) {
883 result
->forw_ref
= true;
885 if (!value
) /* Error in evaluator */
887 if (i
== ':' && mref
) { /* it was seg:offset */
889 * Process the segment override.
891 if (value
[1].type
!= 0 ||
893 !IS_SREG(value
->type
))
894 nasm_error(ERR_NONFATAL
, "invalid segment override");
895 else if (result
->prefixes
[PPS_SEG
])
896 nasm_error(ERR_NONFATAL
,
897 "instruction has conflicting segment overrides");
899 result
->prefixes
[PPS_SEG
] = value
->type
;
900 if (IS_FSGS(value
->type
))
901 op
->eaflags
|= EAF_FSGS
;
904 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
905 while (i
== TOKEN_SPECIAL
|| i
== TOKEN_PREFIX
) {
906 process_size_override(result
, op
);
907 i
= stdscan(NULL
, &tokval
);
909 value
= evaluate(stdscan
, NULL
, &tokval
,
910 &op
->opflags
, critical
, &hints
);
912 if (op
->opflags
& OPFLAG_FORWARD
) {
913 result
->forw_ref
= true;
915 /* and get the offset */
916 if (!value
) /* Error in evaluator */
921 if (mref
&& bracket
&& i
== ',') {
922 /* [seg:base+offset,index*scale] syntax (mib) */
924 operand o1
, o2
; /* Partial operands */
926 if (parse_mref(&o1
, value
))
929 i
= stdscan(NULL
, &tokval
); /* Eat comma */
930 value
= evaluate(stdscan
, NULL
, &tokval
, &op
->opflags
,
936 if (parse_mref(&o2
, value
))
939 if (o2
.basereg
!= -1 && o2
.indexreg
== -1) {
940 o2
.indexreg
= o2
.basereg
;
945 if (o1
.indexreg
!= -1 || o2
.basereg
!= -1 || o2
.offset
!= 0 ||
946 o2
.segment
!= NO_SEG
|| o2
.wrt
!= NO_SEG
) {
947 nasm_error(ERR_NONFATAL
, "invalid mib expression");
951 op
->basereg
= o1
.basereg
;
952 op
->indexreg
= o2
.indexreg
;
953 op
->scale
= o2
.scale
;
954 op
->offset
= o1
.offset
;
955 op
->segment
= o1
.segment
;
958 if (op
->basereg
!= -1) {
959 op
->hintbase
= op
->basereg
;
960 op
->hinttype
= EAH_MAKEBASE
;
961 } else if (op
->indexreg
!= -1) {
962 op
->hintbase
= op
->indexreg
;
963 op
->hinttype
= EAH_NOTBASE
;
966 op
->hinttype
= EAH_NOHINT
;
973 if (mref
&& bracket
) { /* find ] at the end */
975 nasm_error(ERR_NONFATAL
, "parser: expecting ]");
977 } else { /* we got the required ] */
978 i
= stdscan(NULL
, &tokval
);
979 if ((i
== TOKEN_DECORATOR
) || (i
== TOKEN_OPMASK
)) {
981 * according to AVX512 spec, broacast or opmask decorator
982 * is expected for memory reference operands
984 if (tokval
.t_flag
& TFLAG_BRDCAST
) {
985 brace_flags
|= GEN_BRDCAST(0) |
986 VAL_BRNUM(tokval
.t_integer
- BRC_1TO2
);
987 i
= stdscan(NULL
, &tokval
);
988 } else if (i
== TOKEN_OPMASK
) {
989 brace_flags
|= VAL_OPMASK(nasm_regvals
[tokval
.t_integer
]);
990 i
= stdscan(NULL
, &tokval
);
992 nasm_error(ERR_NONFATAL
, "broadcast or opmask "
993 "decorator expected inside braces");
998 if (i
!= 0 && i
!= ',') {
999 nasm_error(ERR_NONFATAL
, "comma or end of line expected");
1003 } else { /* immediate operand */
1004 if (i
!= 0 && i
!= ',' && i
!= ':' &&
1005 i
!= TOKEN_DECORATOR
&& i
!= TOKEN_OPMASK
) {
1006 nasm_error(ERR_NONFATAL
, "comma, colon, decorator or end of "
1007 "line expected after operand");
1009 } else if (i
== ':') {
1011 } else if (i
== TOKEN_DECORATOR
|| i
== TOKEN_OPMASK
) {
1012 /* parse opmask (and zeroing) after an operand */
1013 recover
= parse_braces(&brace_flags
);
1017 do { /* error recovery */
1018 i
= stdscan(NULL
, &tokval
);
1019 } while (i
!= 0 && i
!= ',');
1023 * now convert the exprs returned from evaluate()
1024 * into operand descriptions...
1026 op
->decoflags
|= brace_flags
;
1028 if (mref
) { /* it's a memory reference */
1029 /* A mib reference was fully parsed already */
1031 if (parse_mref(op
, value
))
1033 op
->hintbase
= hints
.base
;
1034 op
->hinttype
= hints
.type
;
1036 mref_set_optype(op
);
1037 } else { /* it's not a memory reference */
1038 if (is_just_unknown(value
)) { /* it's immediate but unknown */
1039 op
->type
|= IMMEDIATE
;
1040 op
->opflags
|= OPFLAG_UNKNOWN
;
1041 op
->offset
= 0; /* don't care */
1042 op
->segment
= NO_SEG
; /* don't care again */
1043 op
->wrt
= NO_SEG
; /* still don't care */
1045 if(optimizing
>= 0 && !(op
->type
& STRICT
)) {
1048 UNITY
| SBYTEWORD
| SBYTEDWORD
| UDWORD
| SDWORD
;
1050 } else if (is_reloc(value
)) { /* it's immediate */
1051 op
->type
|= IMMEDIATE
;
1052 op
->offset
= reloc_value(value
);
1053 op
->segment
= reloc_seg(value
);
1054 op
->wrt
= reloc_wrt(value
);
1055 op
->opflags
|= is_self_relative(value
) ? OPFLAG_RELATIVE
: 0;
1057 if (is_simple(value
)) {
1058 uint64_t n
= reloc_value(value
);
1061 if (optimizing
>= 0 &&
1062 !(op
->type
& STRICT
)) {
1063 if ((uint32_t) (n
+ 128) <= 255)
1064 op
->type
|= SBYTEDWORD
;
1065 if ((uint16_t) (n
+ 128) <= 255)
1066 op
->type
|= SBYTEWORD
;
1067 if (n
<= 0xFFFFFFFF)
1069 if (n
+ 0x80000000 <= 0xFFFFFFFF)
1073 } else if (value
->type
== EXPR_RDSAE
) {
1075 * it's not an operand but a rounding or SAE decorator.
1076 * put the decorator information in the (opflag_t) type field
1077 * of previous operand.
1080 switch (value
->value
) {
1086 op
->decoflags
|= (value
->value
== BRC_SAE
? SAE
: ER
);
1087 result
->evex_rm
= value
->value
;
1090 nasm_error(ERR_NONFATAL
, "invalid decorator");
1093 } else { /* it's a register */
1096 if (value
->type
>= EXPR_SIMPLE
|| value
->value
!= 1) {
1097 nasm_error(ERR_NONFATAL
, "invalid operand type");
1102 * check that its only 1 register, not an expression...
1104 for (i
= 1; value
[i
].type
; i
++)
1105 if (value
[i
].value
) {
1106 nasm_error(ERR_NONFATAL
, "invalid operand type");
1110 /* clear overrides, except TO which applies to FPU regs */
1111 if (op
->type
& ~TO
) {
1113 * we want to produce a warning iff the specified size
1114 * is different from the register size
1116 rs
= op
->type
& SIZE_MASK
;
1121 op
->type
|= REGISTER
;
1122 op
->type
|= nasm_reg_flags
[value
->type
];
1123 op
->decoflags
|= brace_flags
;
1124 op
->basereg
= value
->type
;
1126 if (rs
&& (op
->type
& SIZE_MASK
) != rs
)
1127 nasm_error(ERR_WARNING
| ERR_PASS1
,
1128 "register size specification ignored");
1132 /* remember the position of operand having broadcasting/ER mode */
1133 if (op
->decoflags
& (BRDCAST_MASK
| ER
| SAE
))
1134 result
->evex_brerop
= opnum
;
1137 result
->operands
= opnum
; /* set operand count */
1139 /* clear remaining operands */
1140 while (opnum
< MAX_OPERANDS
)
1141 result
->oprs
[opnum
++].type
= 0;
1144 * Transform RESW, RESD, RESQ, REST, RESO, RESY, RESZ into RESB.
1146 switch (result
->opcode
) {
1148 result
->opcode
= I_RESB
;
1149 result
->oprs
[0].offset
*= 2;
1152 result
->opcode
= I_RESB
;
1153 result
->oprs
[0].offset
*= 4;
1156 result
->opcode
= I_RESB
;
1157 result
->oprs
[0].offset
*= 8;
1160 result
->opcode
= I_RESB
;
1161 result
->oprs
[0].offset
*= 10;
1164 result
->opcode
= I_RESB
;
1165 result
->oprs
[0].offset
*= 16;
1168 result
->opcode
= I_RESB
;
1169 result
->oprs
[0].offset
*= 32;
1172 result
->opcode
= I_RESB
;
1173 result
->oprs
[0].offset
*= 64;
1182 result
->opcode
= I_none
;
1186 static int is_comma_next(void)
1193 i
= stdscan(NULL
, &tv
);
1196 return (i
== ',' || i
== ';' || !i
);
1199 void cleanup_insn(insn
* i
)
1203 while ((e
= i
->eops
)) {
1205 if (e
->type
== EOT_DB_STRING_FREE
)
1206 nasm_free(e
->stringval
);