1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2019 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
54 static int end_expression_next(void);
56 static struct tokenval tokval
;
58 static int prefix_slot(int prefix
)
97 nasm_panic("Invalid value %d passed to prefix_slot()", prefix
);
102 static void process_size_override(insn
*result
, operand
*op
)
104 if (tasm_compatible_mode
) {
105 switch (tokval
.t_integer
) {
106 /* For TASM compatibility a size override inside the
107 * brackets changes the size of the operand, not the
108 * address type of the operand as it does in standard
109 * NASM syntax. Hence:
111 * mov eax,[DWORD val]
113 * is valid syntax in TASM compatibility mode. Note that
114 * you lose the ability to override the default address
115 * type for the instruction, but we never use anything
116 * but 32-bit flat model addressing in our code.
138 nasm_nonfatal("invalid operand size specification");
142 /* Standard NASM compatible syntax */
143 switch (tokval
.t_integer
) {
145 op
->eaflags
|= EAF_TIMESTWO
;
148 op
->eaflags
|= EAF_REL
;
151 op
->eaflags
|= EAF_ABS
;
155 op
->eaflags
|= EAF_BYTEOFFS
;
160 if (result
->prefixes
[PPS_ASIZE
] &&
161 result
->prefixes
[PPS_ASIZE
] != tokval
.t_integer
)
162 nasm_nonfatal("conflicting address size specifications");
164 result
->prefixes
[PPS_ASIZE
] = tokval
.t_integer
;
168 op
->eaflags
|= EAF_WORDOFFS
;
173 op
->eaflags
|= EAF_WORDOFFS
;
177 op
->eaflags
|= EAF_WORDOFFS
;
180 nasm_nonfatal("invalid size specification in"
181 " effective address");
188 * Brace decorators are are parsed here. opmask and zeroing
189 * decorators can be placed in any order. e.g. zmm1 {k2}{z} or zmm2
190 * {z}{k3} decorator(s) are placed at the end of an operand.
192 static bool parse_braces(decoflags_t
*decoflags
)
201 if (*decoflags
& OPMASK_MASK
) {
202 nasm_nonfatal("opmask k%"PRIu64
" is already set",
203 *decoflags
& OPMASK_MASK
);
204 *decoflags
&= ~OPMASK_MASK
;
206 *decoflags
|= VAL_OPMASK(nasm_regvals
[tokval
.t_integer
]);
208 case TOKEN_DECORATOR
:
209 j
= tokval
.t_integer
;
212 *decoflags
|= Z_MASK
;
218 *decoflags
|= BRDCAST_MASK
| VAL_BRNUM(j
- BRC_1TO2
);
221 nasm_nonfatal("{%s} is not an expected decorator",
230 nasm_nonfatal("only a series of valid decorators expected");
233 i
= stdscan(NULL
, &tokval
);
237 static inline const expr
*next_expr(const expr
*e
, const expr
**next_list
)
251 static inline void init_operand(operand
*op
)
253 memset(op
, 0, sizeof *op
);
257 op
->segment
= NO_SEG
;
261 static int parse_mref(operand
*op
, const expr
*e
)
263 int b
, i
, s
; /* basereg, indexreg, scale */
264 int64_t o
; /* offset */
271 for (; e
->type
; e
++) {
272 if (e
->type
<= EXPR_REG_END
) {
273 bool is_gpr
= is_class(REG_GPR
,nasm_reg_flags
[e
->type
]);
275 if (is_gpr
&& e
->value
== 1 && b
== -1) {
276 /* It can be basereg */
278 } else if (i
== -1) {
279 /* Must be index register */
284 nasm_nonfatal("invalid effective address: two index registers");
286 nasm_nonfatal("invalid effective address: impossible register");
288 nasm_nonfatal("invalid effective address: too many registers");
291 } else if (e
->type
== EXPR_UNKNOWN
) {
292 op
->opflags
|= OPFLAG_UNKNOWN
;
293 } else if (e
->type
== EXPR_SIMPLE
) {
295 } else if (e
->type
== EXPR_WRT
) {
297 } else if (e
->type
>= EXPR_SEGBASE
) {
299 if (op
->segment
!= NO_SEG
) {
300 nasm_nonfatal("invalid effective address: multiple base segments");
303 op
->segment
= e
->type
- EXPR_SEGBASE
;
304 } else if (e
->value
== -1 &&
305 e
->type
== location
.segment
+ EXPR_SEGBASE
&&
306 !(op
->opflags
& OPFLAG_RELATIVE
)) {
307 op
->opflags
|= OPFLAG_RELATIVE
;
309 nasm_nonfatal("invalid effective address: impossible segment base multiplier");
313 nasm_nonfatal("invalid effective address: bad subexpression type");
325 static void mref_set_optype(operand
*op
)
328 int i
= op
->indexreg
;
331 /* It is memory, but it can match any r/m operand */
332 op
->type
|= MEMORY_ANY
;
334 if (b
== -1 && (i
== -1 || s
== 0)) {
335 int is_rel
= globalbits
== 64 &&
336 !(op
->eaflags
& EAF_ABS
) &&
338 !(op
->eaflags
& EAF_FSGS
)) ||
339 (op
->eaflags
& EAF_REL
));
341 op
->type
|= is_rel
? IP_REL
: MEM_OFFS
;
345 opflags_t iclass
= nasm_reg_flags
[i
];
347 if (is_class(XMMREG
,iclass
))
349 else if (is_class(YMMREG
,iclass
))
351 else if (is_class(ZMMREG
,iclass
))
357 * Convert an expression vector returned from evaluate() into an
358 * extop structure. Return zero on success. Note that the eop
359 * already has dup and elem set, so we can't clear it here.
361 static int value_to_extop(expr
*vect
, extop
*eop
, int32_t myseg
)
363 eop
->type
= EOT_DB_NUMBER
;
364 eop
->val
.num
.offset
= 0;
365 eop
->val
.num
.segment
= eop
->val
.num
.wrt
= NO_SEG
;
366 eop
->val
.num
.relative
= false;
368 for (; vect
->type
; vect
++) {
369 if (!vect
->value
) /* zero term, safe to ignore */
372 if (vect
->type
<= EXPR_REG_END
) /* false if a register is present */
375 if (vect
->type
== EXPR_UNKNOWN
) /* something we can't resolve yet */
378 if (vect
->type
== EXPR_SIMPLE
) {
379 /* Simple number expression */
380 eop
->val
.num
.offset
+= vect
->value
;
383 if (eop
->val
.num
.wrt
== NO_SEG
&& !eop
->val
.num
.relative
&&
384 vect
->type
== EXPR_WRT
) {
386 eop
->val
.num
.wrt
= vect
->value
;
390 if (!eop
->val
.num
.relative
&&
391 vect
->type
== EXPR_SEGBASE
+ myseg
&& vect
->value
== -1) {
392 /* Expression of the form: foo - $ */
393 eop
->val
.num
.relative
= true;
397 if (eop
->val
.num
.segment
== NO_SEG
&&
398 vect
->type
>= EXPR_SEGBASE
&& vect
->value
== 1) {
399 eop
->val
.num
.segment
= vect
->type
- EXPR_SEGBASE
;
403 /* Otherwise, badness */
407 /* We got to the end and it was all okay */
412 * Parse an extended expression, used by db et al. "elem" is the element
413 * size; initially comes from the specific opcode (e.g. db == 1) but
416 static int parse_eops(extop
**result
, bool critical
, int elem
)
418 extop
*eop
= NULL
, *prev
= NULL
;
419 extop
**tail
= result
;
421 int i
= tokval
.t_type
;
423 bool do_subexpr
= false;
427 /* End of string is obvious; ) ends a sub-expression list e.g. DUP */
428 for (i
= tokval
.t_type
; i
!= TOKEN_EOS
; i
= stdscan(NULL
, &tokval
)) {
429 char endparen
= ')'; /* Is a right paren the end of list? */
443 * end_expression_next() here is to distinguish this from
444 * a string used as part of an expression...
446 if (i
== TOKEN_QMARK
) {
447 eop
->type
= EOT_DB_RESERVE
;
448 } else if (do_subexpr
&& i
== '(') {
451 stdscan(NULL
, &tokval
); /* Skip paren */
452 if (parse_eops(&eop
->val
.subexpr
, critical
, eop
->elem
) < 0)
455 subexpr
= eop
->val
.subexpr
;
457 /* Subexpression is empty */
458 eop
->type
= EOT_NOTHING
;
459 } else if (!subexpr
->next
) {
460 /* Subexpression is a single element, flatten */
461 eop
->val
= subexpr
->val
;
462 eop
->type
= subexpr
->type
;
463 eop
->dup
*= subexpr
->dup
;
466 eop
->type
= EOT_EXTOP
;
469 /* We should have ended on a closing paren */
470 if (tokval
.t_type
!= ')') {
471 nasm_nonfatal("expected `)' after subexpression, got `%s'",
473 "end of line" : tokval
.t_charptr
);
476 endparen
= 0; /* This time the paren is not the end */
477 } else if (i
== '%') {
478 /* %(expression_list) */
481 } else if (i
== TOKEN_SIZE
) {
482 /* Element size override */
483 eop
->elem
= tokval
.t_inttwo
;
486 } else if (i
== TOKEN_STR
&& end_expression_next()) {
487 eop
->type
= EOT_DB_STRING
;
488 eop
->val
.string
.data
= tokval
.t_charptr
;
489 eop
->val
.string
.len
= tokval
.t_inttwo
;
490 } else if (i
== TOKEN_STRFUNC
) {
492 const char *funcname
= tokval
.t_charptr
;
493 enum strfunc func
= tokval
.t_integer
;
495 i
= stdscan(NULL
, &tokval
);
499 i
= stdscan(NULL
, &tokval
);
501 if (i
!= TOKEN_STR
) {
502 nasm_nonfatal("%s must be followed by a string constant",
504 eop
->type
= EOT_NOTHING
;
506 eop
->type
= EOT_DB_STRING_FREE
;
507 eop
->val
.string
.len
=
508 string_transform(tokval
.t_charptr
, tokval
.t_inttwo
,
509 &eop
->val
.string
.data
, func
);
510 if (eop
->val
.string
.len
== (size_t)-1) {
511 nasm_nonfatal("invalid input string to %s", funcname
);
512 eop
->type
= EOT_NOTHING
;
515 if (parens
&& i
&& i
!= ')') {
516 i
= stdscan(NULL
, &tokval
);
518 nasm_nonfatal("unterminated %s function", funcname
);
520 } else if (i
== '-' || i
== '+') {
521 char *save
= stdscan_get();
522 struct tokenval tmptok
;
524 sign
= (i
== '-') ? -1 : 1;
525 if (stdscan(NULL
, &tmptok
) != TOKEN_FLOAT
) {
532 } else if (i
== TOKEN_FLOAT
) {
534 eop
->type
= EOT_DB_FLOAT
;
536 if (eop
->elem
> 16) {
537 nasm_nonfatal("no %d-bit floating-point format supported",
539 eop
->val
.string
.len
= 0;
540 } else if (eop
->elem
< 1) {
541 nasm_nonfatal("floating-point constant"
542 " encountered in unknown instruction");
544 * fix suggested by Pedro Gimeno... original line was:
545 * eop->type = EOT_NOTHING;
547 eop
->val
.string
.len
= 0;
549 eop
->val
.string
.len
= eop
->elem
;
551 eop
= nasm_realloc(eop
, sizeof(extop
) + eop
->val
.string
.len
);
552 eop
->val
.string
.data
= (char *)eop
+ sizeof(extop
);
553 if (!float_const(tokval
.t_charptr
, sign
,
554 (uint8_t *)eop
->val
.string
.data
,
555 eop
->val
.string
.len
))
556 eop
->val
.string
.len
= 0;
558 if (!eop
->val
.string
.len
)
559 eop
->type
= EOT_NOTHING
;
561 /* anything else, assume it is an expression */
565 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
,
568 if (!value
) /* Error in evaluator */
570 if (tokval
.t_flag
& TFLAG_DUP
) {
571 /* Expression followed by DUP */
572 if (!is_simple(value
)) {
573 nasm_nonfatal("non-constant argument supplied to DUP");
575 } else if (value
->value
< 0) {
576 nasm_nonfatal("negative argument supplied to DUP");
579 eop
->dup
*= (size_t)value
->value
;
583 if (value_to_extop(value
, eop
, location
.segment
)) {
584 nasm_nonfatal("expression is not simple or relocatable");
588 if (eop
->dup
== 0 || eop
->type
== EOT_NOTHING
) {
590 } else if (eop
->type
== EOT_DB_RESERVE
&&
591 prev
&& prev
->type
== EOT_DB_RESERVE
&&
592 prev
->elem
== eop
->elem
) {
593 /* Coalesce multiple EOT_DB_RESERVE */
594 prev
->dup
+= eop
->dup
;
597 /* Add this eop to the end of the chain */
604 eop
= NULL
; /* Done with this operand */
607 * We're about to call stdscan(), which will eat the
608 * comma that we're currently sitting on between
609 * arguments. However, we'd better check first that it
612 if (i
== TOKEN_EOS
|| i
== endparen
) /* Already at end? */
615 i
= stdscan(NULL
, &tokval
); /* eat the comma or final paren */
616 if (i
== TOKEN_EOS
|| i
== ')') /* got end of expression */
619 nasm_nonfatal("comma expected after operand");
633 insn
*parse_line(char *buffer
, insn
*result
)
635 bool insn_is_label
= false;
636 struct eval_hints hints
;
644 nasm_static_assert(P_none
== 0);
648 result
->forw_ref
= false;
652 i
= stdscan(NULL
, &tokval
);
654 memset(result
->prefixes
, P_none
, sizeof(result
->prefixes
));
655 result
->times
= 1; /* No TIMES either yet */
656 result
->label
= NULL
; /* Assume no label */
657 result
->eops
= NULL
; /* must do this, whatever happens */
658 result
->operands
= 0; /* must initialize this */
659 result
->evex_rm
= 0; /* Ensure EVEX rounding mode is reset */
660 result
->evex_brerop
= -1; /* Reset EVEX broadcasting/ER op position */
662 /* Ignore blank lines */
669 (i
!= TOKEN_REG
|| !IS_SREG(tokval
.t_integer
))) {
670 nasm_nonfatal("label or instruction expected at start of line");
674 if (i
== TOKEN_ID
|| (insn_is_label
&& i
== TOKEN_INSN
)) {
675 /* there's a label here */
677 result
->label
= tokval
.t_charptr
;
678 i
= stdscan(NULL
, &tokval
);
679 if (i
== ':') { /* skip over the optional colon */
680 i
= stdscan(NULL
, &tokval
);
683 *!label-orphan [on] labels alone on lines without trailing `:'
685 *! warns about source lines which contain no instruction but define
686 *! a label without a trailing colon. This is most likely indicative
687 *! of a typo, but is technically correct NASM syntax (see \k{syntax}.)
689 nasm_warn(WARN_LABEL_ORPHAN
,
690 "label alone on a line without a colon might be in error");
692 if (i
!= TOKEN_INSN
|| tokval
.t_integer
!= I_EQU
) {
694 * FIXME: location.segment could be NO_SEG, in which case
695 * it is possible we should be passing 'absolute.segment'. Look into this.
696 * Work out whether that is *really* what we should be doing.
697 * Generally fix things. I think this is right as it is, but
698 * am still not certain.
700 define_label(result
->label
,
701 in_absolute
? absolute
.segment
: location
.segment
,
702 location
.offset
, true);
706 /* Just a label here */
710 while (i
== TOKEN_PREFIX
||
711 (i
== TOKEN_REG
&& IS_SREG(tokval
.t_integer
))) {
715 * Handle special case: the TIMES prefix.
717 if (i
== TOKEN_PREFIX
&& tokval
.t_integer
== P_TIMES
) {
720 i
= stdscan(NULL
, &tokval
);
721 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass_stable(), NULL
);
723 if (!value
) /* Error in evaluator */
725 if (!is_simple(value
)) {
726 nasm_nonfatal("non-constant argument supplied to TIMES");
729 result
->times
= value
->value
;
730 if (value
->value
< 0) {
731 nasm_nonfatalf(ERR_PASS2
, "TIMES value %"PRId64
" is negative", value
->value
);
736 int slot
= prefix_slot(tokval
.t_integer
);
737 if (result
->prefixes
[slot
]) {
738 if (result
->prefixes
[slot
] == tokval
.t_integer
)
739 nasm_warn(WARN_OTHER
, "instruction has redundant prefixes");
741 nasm_nonfatal("instruction has conflicting prefixes");
743 result
->prefixes
[slot
] = tokval
.t_integer
;
744 i
= stdscan(NULL
, &tokval
);
748 if (i
!= TOKEN_INSN
) {
752 for (j
= 0; j
< MAXPREFIX
; j
++) {
753 if ((pfx
= result
->prefixes
[j
]) != P_none
)
757 if (i
== 0 && pfx
!= P_none
) {
759 * Instruction prefixes are present, but no actual
760 * instruction. This is allowed: at this point we
761 * invent a notional instruction of RESB 0.
763 result
->opcode
= I_RESB
;
764 result
->operands
= 1;
765 nasm_zero(result
->oprs
);
766 result
->oprs
[0].type
= IMMEDIATE
;
767 result
->oprs
[0].offset
= 0L;
768 result
->oprs
[0].segment
= result
->oprs
[0].wrt
= NO_SEG
;
771 nasm_nonfatal("parser: instruction expected");
776 result
->opcode
= tokval
.t_integer
;
777 result
->condition
= tokval
.t_inttwo
;
780 * INCBIN cannot be satisfied with incorrectly
781 * evaluated operands, since the correct values _must_ be known
782 * on the first pass. Hence, even in pass one, we set the
783 * `critical' flag on calling evaluate(), so that it will bomb
784 * out on undefined symbols.
786 critical
= pass_final() || (result
->opcode
== I_INCBIN
);
788 if (opcode_is_db(result
->opcode
) || result
->opcode
== I_INCBIN
) {
791 i
= stdscan(NULL
, &tokval
);
793 if (first
&& i
== ':') {
795 insn_is_label
= true;
799 oper_num
= parse_eops(&result
->eops
, critical
, db_bytes(result
->opcode
));
803 if (result
->opcode
== I_INCBIN
) {
805 * Correct syntax for INCBIN is that there should be
806 * one string operand, followed by one or two numeric
809 if (!result
->eops
|| result
->eops
->type
!= EOT_DB_STRING
)
810 nasm_nonfatal("`incbin' expects a file name");
811 else if (result
->eops
->next
&&
812 result
->eops
->next
->type
!= EOT_DB_NUMBER
)
813 nasm_nonfatal("`incbin': second parameter is"
815 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
816 result
->eops
->next
->next
->type
!= EOT_DB_NUMBER
)
817 nasm_nonfatal("`incbin': third parameter is"
819 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
820 result
->eops
->next
->next
->next
)
821 nasm_nonfatal("`incbin': more than three parameters");
825 * If we reach here, one of the above errors happened.
826 * Throw the instruction away.
831 result
->operands
= oper_num
;
833 nasm_warn(WARN_OTHER
, "no operand for data declaration");
839 * Now we begin to parse the operands. There may be up to four
840 * of these, separated by commas, and terminated by a zero token.
842 far_jmp_ok
= result
->opcode
== I_JMP
|| result
->opcode
== I_CALL
;
844 for (opnum
= 0; opnum
< MAX_OPERANDS
; opnum
++) {
845 operand
*op
= &result
->oprs
[opnum
];
846 expr
*value
; /* used most of the time */
847 bool mref
= false; /* is this going to be a memory ref? */
848 int bracket
= 0; /* is it a [] mref, or a "naked" mref? */
849 bool mib
; /* compound (mib) mref? */
851 decoflags_t brace_flags
= 0; /* flags for decorators in braces */
855 i
= stdscan(NULL
, &tokval
);
857 break; /* end of operands: get out of here */
858 else if (first
&& i
== ':') {
859 insn_is_label
= true;
863 op
->type
= 0; /* so far, no override */
864 /* size specifiers */
865 while (i
== TOKEN_SPECIAL
|| i
== TOKEN_SIZE
) {
866 switch (tokval
.t_integer
) {
868 if (!setsize
) /* we want to use only the first */
924 nasm_nonfatal("invalid operand size specification");
926 i
= stdscan(NULL
, &tokval
);
929 if (i
== '[' || i
== TOKEN_MASM_PTR
|| i
== '&') {
930 /* memory reference */
932 bracket
+= (i
== '[');
933 i
= stdscan(NULL
, &tokval
);
946 process_size_override(result
, op
);
954 tokval
.t_type
= TOKEN_NUM
;
955 tokval
.t_integer
= 0;
956 stdscan_set(stdscan_get() - 1); /* rewind the comma */
960 case TOKEN_MASM_FLAT
:
961 i
= stdscan(NULL
, &tokval
);
963 nasm_nonfatal("unknown use of FLAT in MASM emulation");
975 i
= stdscan(NULL
, &tokval
);
979 value
= evaluate(stdscan
, NULL
, &tokval
,
980 &op
->opflags
, critical
, &hints
);
982 if (op
->opflags
& OPFLAG_FORWARD
) {
983 result
->forw_ref
= true;
985 if (!value
) /* Error in evaluator */
988 if (i
== '[' && !bracket
) {
989 /* displacement[regs] syntax */
991 parse_mref(op
, value
); /* Process what we have so far */
995 if (i
== ':' && (mref
|| !far_jmp_ok
)) {
996 /* segment override? */
1000 * Process the segment override.
1002 if (value
[1].type
!= 0 ||
1003 value
->value
!= 1 ||
1004 !IS_SREG(value
->type
))
1005 nasm_nonfatal("invalid segment override");
1006 else if (result
->prefixes
[PPS_SEG
])
1007 nasm_nonfatal("instruction has conflicting segment overrides");
1009 result
->prefixes
[PPS_SEG
] = value
->type
;
1010 if (IS_FSGS(value
->type
))
1011 op
->eaflags
|= EAF_FSGS
;
1014 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
1019 if (mref
&& bracket
&& i
== ',') {
1020 /* [seg:base+offset,index*scale] syntax (mib) */
1021 operand o2
; /* Index operand */
1023 if (parse_mref(op
, value
))
1026 i
= stdscan(NULL
, &tokval
); /* Eat comma */
1027 value
= evaluate(stdscan
, NULL
, &tokval
, &op
->opflags
,
1034 if (parse_mref(&o2
, value
))
1037 if (o2
.basereg
!= -1 && o2
.indexreg
== -1) {
1038 o2
.indexreg
= o2
.basereg
;
1043 if (op
->indexreg
!= -1 || o2
.basereg
!= -1 || o2
.offset
!= 0 ||
1044 o2
.segment
!= NO_SEG
|| o2
.wrt
!= NO_SEG
) {
1045 nasm_nonfatal("invalid mib expression");
1049 op
->indexreg
= o2
.indexreg
;
1050 op
->scale
= o2
.scale
;
1052 if (op
->basereg
!= -1) {
1053 op
->hintbase
= op
->basereg
;
1054 op
->hinttype
= EAH_MAKEBASE
;
1055 } else if (op
->indexreg
!= -1) {
1056 op
->hintbase
= op
->indexreg
;
1057 op
->hinttype
= EAH_NOTBASE
;
1060 op
->hinttype
= EAH_NOHINT
;
1071 i
= stdscan(NULL
, &tokval
);
1073 nasm_nonfatal("expecting ] at end of memory operand");
1076 } else if (bracket
== 0) {
1078 } else if (bracket
> 0) {
1079 nasm_nonfatal("excess brackets in memory operand");
1081 } else if (bracket
< 0) {
1082 nasm_nonfatal("unmatched ] in memory operand");
1086 if (i
== TOKEN_DECORATOR
|| i
== TOKEN_OPMASK
) {
1087 /* parse opmask (and zeroing) after an operand */
1088 recover
= parse_braces(&brace_flags
);
1091 if (!recover
&& i
!= 0 && i
!= ',') {
1092 nasm_nonfatal("comma, decorator or end of line expected, got %d", i
);
1095 } else { /* immediate operand */
1096 if (i
!= 0 && i
!= ',' && i
!= ':' &&
1097 i
!= TOKEN_DECORATOR
&& i
!= TOKEN_OPMASK
) {
1098 nasm_nonfatal("comma, colon, decorator or end of "
1099 "line expected after operand");
1101 } else if (i
== ':') {
1103 } else if (i
== TOKEN_DECORATOR
|| i
== TOKEN_OPMASK
) {
1104 /* parse opmask (and zeroing) after an operand */
1105 recover
= parse_braces(&brace_flags
);
1109 do { /* error recovery */
1110 i
= stdscan(NULL
, &tokval
);
1111 } while (i
!= 0 && i
!= ',');
1115 * now convert the exprs returned from evaluate()
1116 * into operand descriptions...
1118 op
->decoflags
|= brace_flags
;
1120 if (mref
) { /* it's a memory reference */
1121 /* A mib reference was fully parsed already */
1123 if (parse_mref(op
, value
))
1125 op
->hintbase
= hints
.base
;
1126 op
->hinttype
= hints
.type
;
1128 mref_set_optype(op
);
1129 } else if ((op
->type
& FAR
) && !far_jmp_ok
) {
1130 nasm_nonfatal("invalid use of FAR operand specifier");
1132 } else { /* it's not a memory reference */
1133 if (is_just_unknown(value
)) { /* it's immediate but unknown */
1134 op
->type
|= IMMEDIATE
;
1135 op
->opflags
|= OPFLAG_UNKNOWN
;
1136 op
->offset
= 0; /* don't care */
1137 op
->segment
= NO_SEG
; /* don't care again */
1138 op
->wrt
= NO_SEG
; /* still don't care */
1140 if(optimizing
.level
>= 0 && !(op
->type
& STRICT
)) {
1143 UNITY
| SBYTEWORD
| SBYTEDWORD
| UDWORD
| SDWORD
;
1145 } else if (is_reloc(value
)) { /* it's immediate */
1146 uint64_t n
= reloc_value(value
);
1148 op
->type
|= IMMEDIATE
;
1150 op
->segment
= reloc_seg(value
);
1151 op
->wrt
= reloc_wrt(value
);
1152 op
->opflags
|= is_self_relative(value
) ? OPFLAG_RELATIVE
: 0;
1154 if (is_simple(value
)) {
1157 if (optimizing
.level
>= 0 && !(op
->type
& STRICT
)) {
1158 if ((uint32_t) (n
+ 128) <= 255)
1159 op
->type
|= SBYTEDWORD
;
1160 if ((uint16_t) (n
+ 128) <= 255)
1161 op
->type
|= SBYTEWORD
;
1162 if (n
<= UINT64_C(0xFFFFFFFF))
1164 if (n
+ UINT64_C(0x80000000) <= UINT64_C(0xFFFFFFFF))
1168 } else if (value
->type
== EXPR_RDSAE
) {
1170 * it's not an operand but a rounding or SAE decorator.
1171 * put the decorator information in the (opflag_t) type field
1172 * of previous operand.
1175 switch (value
->value
) {
1181 op
->decoflags
|= (value
->value
== BRC_SAE
? SAE
: ER
);
1182 result
->evex_rm
= value
->value
;
1185 nasm_nonfatal("invalid decorator");
1188 } else { /* it's a register */
1190 uint64_t regset_size
= 0;
1192 if (value
->type
>= EXPR_SIMPLE
|| value
->value
!= 1) {
1193 nasm_nonfatal("invalid operand type");
1198 * We do not allow any kind of expression, except for
1199 * reg+value in which case it is a register set.
1201 for (i
= 1; value
[i
].type
; i
++) {
1202 if (!value
[i
].value
)
1205 switch (value
[i
].type
) {
1208 regset_size
= value
[i
].value
+ 1;
1213 nasm_nonfatal("invalid operand type");
1218 if ((regset_size
& (regset_size
- 1)) ||
1219 regset_size
>= (UINT64_C(1) << REGSET_BITS
)) {
1220 nasm_nonfatalf(ERR_PASS2
, "invalid register set size");
1224 /* clear overrides, except TO which applies to FPU regs */
1225 if (op
->type
& ~TO
) {
1227 * we want to produce a warning iff the specified size
1228 * is different from the register size
1230 rs
= op
->type
& SIZE_MASK
;
1236 * Make sure we're not out of nasm_reg_flags, still
1237 * probably this should be fixed when we're defining
1240 * An easy trigger is
1242 * e equ 0x80000000:0
1246 if (value
->type
< EXPR_REG_START
||
1247 value
->type
> EXPR_REG_END
) {
1248 nasm_nonfatal("invalid operand type");
1253 op
->type
|= REGISTER
;
1254 op
->type
|= nasm_reg_flags
[value
->type
];
1255 op
->type
|= (regset_size
>> 1) << REGSET_SHIFT
;
1256 op
->decoflags
|= brace_flags
;
1257 op
->basereg
= value
->type
;
1260 opflags_t opsize
= nasm_reg_flags
[value
->type
] & SIZE_MASK
;
1262 op
->type
|= rs
; /* For non-size-specific registers, permit size override */
1263 } else if (opsize
!= rs
) {
1265 *!regsize [on] register size specification ignored
1267 *! warns about a register with implicit size (such as \c{EAX}, which is always 32 bits)
1268 *! been given an explicit size specification which is inconsistent with the size
1269 *! of the named register, e.g. \c{WORD EAX}. \c{DWORD EAX} or \c{WORD AX} are
1270 *! permitted, and do not trigger this warning. Some registers which \e{do not} imply
1271 *! a specific size, such as \c{K0}, may need this specification unless the instruction
1272 *! itself implies the instruction size:
1274 *! \c KMOVW K0,[foo] ; Permitted, KMOVW implies 16 bits
1275 *! \c KMOV WORD K0,[foo] ; Permitted, WORD K0 specifies instruction size
1276 *! \c KMOV K0,WORD [foo] ; Permitted, WORD [foo] specifies instruction size
1277 *! \c KMOV K0,[foo] ; Not permitted, instruction size ambiguous
1279 nasm_warn(WARN_REGSIZE
, "invalid register size specification ignored");
1285 /* remember the position of operand having broadcasting/ER mode */
1286 if (op
->decoflags
& (BRDCAST_MASK
| ER
| SAE
))
1287 result
->evex_brerop
= opnum
;
1290 result
->operands
= opnum
; /* set operand count */
1292 /* clear remaining operands */
1293 while (opnum
< MAX_OPERANDS
)
1294 result
->oprs
[opnum
++].type
= 0;
1299 result
->opcode
= I_none
;
1303 static int end_expression_next(void)
1310 i
= stdscan(NULL
, &tv
);
1313 return (i
== ',' || i
== ';' || i
== ')' || !i
);
1316 static void free_eops(extop
*e
)
1324 free_eops(e
->val
.subexpr
);
1327 case EOT_DB_STRING_FREE
:
1328 nasm_free(e
->val
.string
.data
);
1340 void cleanup_insn(insn
* i
)