1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2020 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 unused_func
238 const expr
*next_expr(const expr
*e
, const expr
**next_list
)
252 static inline void init_operand(operand
*op
)
254 memset(op
, 0, sizeof *op
);
258 op
->segment
= NO_SEG
;
262 static int parse_mref(operand
*op
, const expr
*e
)
264 int b
, i
, s
; /* basereg, indexreg, scale */
265 int64_t o
; /* offset */
272 for (; e
->type
; e
++) {
273 if (e
->type
<= EXPR_REG_END
) {
274 bool is_gpr
= is_class(REG_GPR
,nasm_reg_flags
[e
->type
]);
276 if (is_gpr
&& e
->value
== 1 && b
== -1) {
277 /* It can be basereg */
279 } else if (i
== -1) {
280 /* Must be index register */
285 nasm_nonfatal("invalid effective address: two index registers");
287 nasm_nonfatal("invalid effective address: impossible register");
289 nasm_nonfatal("invalid effective address: too many registers");
292 } else if (e
->type
== EXPR_UNKNOWN
) {
293 op
->opflags
|= OPFLAG_UNKNOWN
;
294 } else if (e
->type
== EXPR_SIMPLE
) {
296 } else if (e
->type
== EXPR_WRT
) {
298 } else if (e
->type
>= EXPR_SEGBASE
) {
300 if (op
->segment
!= NO_SEG
) {
301 nasm_nonfatal("invalid effective address: multiple base segments");
304 op
->segment
= e
->type
- EXPR_SEGBASE
;
305 } else if (e
->value
== -1 &&
306 e
->type
== location
.segment
+ EXPR_SEGBASE
&&
307 !(op
->opflags
& OPFLAG_RELATIVE
)) {
308 op
->opflags
|= OPFLAG_RELATIVE
;
310 nasm_nonfatal("invalid effective address: impossible segment base multiplier");
314 nasm_nonfatal("invalid effective address: bad subexpression type");
326 static void mref_set_optype(operand
*op
)
329 int i
= op
->indexreg
;
332 /* It is memory, but it can match any r/m operand */
333 op
->type
|= MEMORY_ANY
;
335 if (b
== -1 && (i
== -1 || s
== 0)) {
336 int is_rel
= globalbits
== 64 &&
337 !(op
->eaflags
& EAF_ABS
) &&
339 !(op
->eaflags
& EAF_FSGS
)) ||
340 (op
->eaflags
& EAF_REL
));
342 op
->type
|= is_rel
? IP_REL
: MEM_OFFS
;
346 opflags_t iclass
= nasm_reg_flags
[i
];
348 if (is_class(XMMREG
,iclass
))
350 else if (is_class(YMMREG
,iclass
))
352 else if (is_class(ZMMREG
,iclass
))
358 * Convert an expression vector returned from evaluate() into an
359 * extop structure. Return zero on success. Note that the eop
360 * already has dup and elem set, so we can't clear it here.
362 static int value_to_extop(expr
*vect
, extop
*eop
, int32_t myseg
)
364 eop
->type
= EOT_DB_NUMBER
;
365 eop
->val
.num
.offset
= 0;
366 eop
->val
.num
.segment
= eop
->val
.num
.wrt
= NO_SEG
;
367 eop
->val
.num
.relative
= false;
369 for (; vect
->type
; vect
++) {
370 if (!vect
->value
) /* zero term, safe to ignore */
373 if (vect
->type
<= EXPR_REG_END
) /* false if a register is present */
376 if (vect
->type
== EXPR_UNKNOWN
) /* something we can't resolve yet */
379 if (vect
->type
== EXPR_SIMPLE
) {
380 /* Simple number expression */
381 eop
->val
.num
.offset
+= vect
->value
;
384 if (eop
->val
.num
.wrt
== NO_SEG
&& !eop
->val
.num
.relative
&&
385 vect
->type
== EXPR_WRT
) {
387 eop
->val
.num
.wrt
= vect
->value
;
391 if (!eop
->val
.num
.relative
&&
392 vect
->type
== EXPR_SEGBASE
+ myseg
&& vect
->value
== -1) {
393 /* Expression of the form: foo - $ */
394 eop
->val
.num
.relative
= true;
398 if (eop
->val
.num
.segment
== NO_SEG
&&
399 vect
->type
>= EXPR_SEGBASE
&& vect
->value
== 1) {
400 eop
->val
.num
.segment
= vect
->type
- EXPR_SEGBASE
;
404 /* Otherwise, badness */
408 /* We got to the end and it was all okay */
413 * Parse an extended expression, used by db et al. "elem" is the element
414 * size; initially comes from the specific opcode (e.g. db == 1) but
417 static int parse_eops(extop
**result
, bool critical
, int elem
)
419 extop
*eop
= NULL
, *prev
= NULL
;
420 extop
**tail
= result
;
422 int i
= tokval
.t_type
;
424 bool do_subexpr
= false;
428 /* End of string is obvious; ) ends a sub-expression list e.g. DUP */
429 for (i
= tokval
.t_type
; i
!= TOKEN_EOS
; i
= stdscan(NULL
, &tokval
)) {
430 char endparen
= ')'; /* Is a right paren the end of list? */
444 * end_expression_next() here is to distinguish this from
445 * a string used as part of an expression...
447 if (i
== TOKEN_QMARK
) {
448 eop
->type
= EOT_DB_RESERVE
;
449 } else if (do_subexpr
&& i
== '(') {
452 stdscan(NULL
, &tokval
); /* Skip paren */
453 if (parse_eops(&eop
->val
.subexpr
, critical
, eop
->elem
) < 0)
456 subexpr
= eop
->val
.subexpr
;
458 /* Subexpression is empty */
459 eop
->type
= EOT_NOTHING
;
460 } else if (!subexpr
->next
) {
461 /* Subexpression is a single element, flatten */
462 eop
->val
= subexpr
->val
;
463 eop
->type
= subexpr
->type
;
464 eop
->dup
*= subexpr
->dup
;
467 eop
->type
= EOT_EXTOP
;
470 /* We should have ended on a closing paren */
471 if (tokval
.t_type
!= ')') {
472 nasm_nonfatal("expected `)' after subexpression, got `%s'",
474 "end of line" : tokval
.t_charptr
);
477 endparen
= 0; /* This time the paren is not the end */
478 } else if (i
== '%') {
479 /* %(expression_list) */
482 } else if (i
== TOKEN_SIZE
) {
483 /* Element size override */
484 eop
->elem
= tokval
.t_inttwo
;
487 } else if (i
== TOKEN_STR
&& end_expression_next()) {
488 eop
->type
= EOT_DB_STRING
;
489 eop
->val
.string
.data
= tokval
.t_charptr
;
490 eop
->val
.string
.len
= tokval
.t_inttwo
;
491 } else if (i
== TOKEN_STRFUNC
) {
493 const char *funcname
= tokval
.t_charptr
;
494 enum strfunc func
= tokval
.t_integer
;
496 i
= stdscan(NULL
, &tokval
);
500 i
= stdscan(NULL
, &tokval
);
502 if (i
!= TOKEN_STR
) {
503 nasm_nonfatal("%s must be followed by a string constant",
505 eop
->type
= EOT_NOTHING
;
507 eop
->type
= EOT_DB_STRING_FREE
;
508 eop
->val
.string
.len
=
509 string_transform(tokval
.t_charptr
, tokval
.t_inttwo
,
510 &eop
->val
.string
.data
, func
);
511 if (eop
->val
.string
.len
== (size_t)-1) {
512 nasm_nonfatal("invalid input string to %s", funcname
);
513 eop
->type
= EOT_NOTHING
;
516 if (parens
&& i
&& i
!= ')') {
517 i
= stdscan(NULL
, &tokval
);
519 nasm_nonfatal("unterminated %s function", funcname
);
521 } else if (i
== '-' || i
== '+') {
522 char *save
= stdscan_get();
523 struct tokenval tmptok
;
525 sign
= (i
== '-') ? -1 : 1;
526 if (stdscan(NULL
, &tmptok
) != TOKEN_FLOAT
) {
533 } else if (i
== TOKEN_FLOAT
) {
535 eop
->type
= EOT_DB_FLOAT
;
537 if (eop
->elem
> 16) {
538 nasm_nonfatal("no %d-bit floating-point format supported",
540 eop
->val
.string
.len
= 0;
541 } else if (eop
->elem
< 1) {
542 nasm_nonfatal("floating-point constant"
543 " encountered in unknown instruction");
545 * fix suggested by Pedro Gimeno... original line was:
546 * eop->type = EOT_NOTHING;
548 eop
->val
.string
.len
= 0;
550 eop
->val
.string
.len
= eop
->elem
;
552 eop
= nasm_realloc(eop
, sizeof(extop
) + eop
->val
.string
.len
);
553 eop
->val
.string
.data
= (char *)eop
+ sizeof(extop
);
554 if (!float_const(tokval
.t_charptr
, sign
,
555 (uint8_t *)eop
->val
.string
.data
,
556 eop
->val
.string
.len
))
557 eop
->val
.string
.len
= 0;
559 if (!eop
->val
.string
.len
)
560 eop
->type
= EOT_NOTHING
;
562 /* anything else, assume it is an expression */
566 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
,
569 if (!value
) /* Error in evaluator */
571 if (tokval
.t_flag
& TFLAG_DUP
) {
572 /* Expression followed by DUP */
573 if (!is_simple(value
)) {
574 nasm_nonfatal("non-constant argument supplied to DUP");
576 } else if (value
->value
< 0) {
577 nasm_nonfatal("negative argument supplied to DUP");
580 eop
->dup
*= (size_t)value
->value
;
584 if (value_to_extop(value
, eop
, location
.segment
)) {
585 nasm_nonfatal("expression is not simple or relocatable");
589 if (eop
->dup
== 0 || eop
->type
== EOT_NOTHING
) {
591 } else if (eop
->type
== EOT_DB_RESERVE
&&
592 prev
&& prev
->type
== EOT_DB_RESERVE
&&
593 prev
->elem
== eop
->elem
) {
594 /* Coalesce multiple EOT_DB_RESERVE */
595 prev
->dup
+= eop
->dup
;
598 /* Add this eop to the end of the chain */
605 eop
= NULL
; /* Done with this operand */
608 * We're about to call stdscan(), which will eat the
609 * comma that we're currently sitting on between
610 * arguments. However, we'd better check first that it
613 if (i
== TOKEN_EOS
|| i
== endparen
) /* Already at end? */
616 i
= stdscan(NULL
, &tokval
); /* eat the comma or final paren */
617 if (i
== TOKEN_EOS
|| i
== ')') /* got end of expression */
620 nasm_nonfatal("comma expected after operand");
634 insn
*parse_line(char *buffer
, insn
*result
)
636 bool insn_is_label
= false;
637 struct eval_hints hints
;
645 nasm_static_assert(P_none
== 0);
649 result
->forw_ref
= false;
653 i
= stdscan(NULL
, &tokval
);
655 memset(result
->prefixes
, P_none
, sizeof(result
->prefixes
));
656 result
->times
= 1; /* No TIMES either yet */
657 result
->label
= NULL
; /* Assume no label */
658 result
->eops
= NULL
; /* must do this, whatever happens */
659 result
->operands
= 0; /* must initialize this */
660 result
->evex_rm
= 0; /* Ensure EVEX rounding mode is reset */
661 result
->evex_brerop
= -1; /* Reset EVEX broadcasting/ER op position */
663 /* Ignore blank lines */
670 (i
!= TOKEN_REG
|| !IS_SREG(tokval
.t_integer
))) {
671 nasm_nonfatal("label or instruction expected at start of line");
675 if (i
== TOKEN_ID
|| (insn_is_label
&& i
== TOKEN_INSN
)) {
676 /* there's a label here */
678 result
->label
= tokval
.t_charptr
;
679 i
= stdscan(NULL
, &tokval
);
680 if (i
== ':') { /* skip over the optional colon */
681 i
= stdscan(NULL
, &tokval
);
684 *!label-orphan [on] labels alone on lines without trailing `:'
686 *! warns about source lines which contain no instruction but define
687 *! a label without a trailing colon. This is most likely indicative
688 *! of a typo, but is technically correct NASM syntax (see \k{syntax}.)
690 nasm_warn(WARN_LABEL_ORPHAN
,
691 "label alone on a line without a colon might be in error");
693 if (i
!= TOKEN_INSN
|| tokval
.t_integer
!= I_EQU
) {
695 * FIXME: location.segment could be NO_SEG, in which case
696 * it is possible we should be passing 'absolute.segment'. Look into this.
697 * Work out whether that is *really* what we should be doing.
698 * Generally fix things. I think this is right as it is, but
699 * am still not certain.
701 define_label(result
->label
,
702 in_absolute
? absolute
.segment
: location
.segment
,
703 location
.offset
, true);
707 /* Just a label here */
711 while (i
== TOKEN_PREFIX
||
712 (i
== TOKEN_REG
&& IS_SREG(tokval
.t_integer
))) {
716 * Handle special case: the TIMES prefix.
718 if (i
== TOKEN_PREFIX
&& tokval
.t_integer
== P_TIMES
) {
721 i
= stdscan(NULL
, &tokval
);
722 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass_stable(), NULL
);
724 if (!value
) /* Error in evaluator */
726 if (!is_simple(value
)) {
727 nasm_nonfatal("non-constant argument supplied to TIMES");
730 result
->times
= value
->value
;
731 if (value
->value
< 0) {
732 nasm_nonfatalf(ERR_PASS2
, "TIMES value %"PRId64
" is negative", value
->value
);
737 int slot
= prefix_slot(tokval
.t_integer
);
738 if (result
->prefixes
[slot
]) {
739 if (result
->prefixes
[slot
] == tokval
.t_integer
)
740 nasm_warn(WARN_OTHER
, "instruction has redundant prefixes");
742 nasm_nonfatal("instruction has conflicting prefixes");
744 result
->prefixes
[slot
] = tokval
.t_integer
;
745 i
= stdscan(NULL
, &tokval
);
749 if (i
!= TOKEN_INSN
) {
753 for (j
= 0; j
< MAXPREFIX
; j
++) {
754 if ((pfx
= result
->prefixes
[j
]) != P_none
)
758 if (i
== 0 && pfx
!= P_none
) {
760 * Instruction prefixes are present, but no actual
761 * instruction. This is allowed: at this point we
762 * invent a notional instruction of RESB 0.
764 result
->opcode
= I_RESB
;
765 result
->operands
= 1;
766 nasm_zero(result
->oprs
);
767 result
->oprs
[0].type
= IMMEDIATE
;
768 result
->oprs
[0].offset
= 0L;
769 result
->oprs
[0].segment
= result
->oprs
[0].wrt
= NO_SEG
;
772 nasm_nonfatal("parser: instruction expected");
777 result
->opcode
= tokval
.t_integer
;
778 result
->condition
= tokval
.t_inttwo
;
781 * INCBIN cannot be satisfied with incorrectly
782 * evaluated operands, since the correct values _must_ be known
783 * on the first pass. Hence, even in pass one, we set the
784 * `critical' flag on calling evaluate(), so that it will bomb
785 * out on undefined symbols.
787 critical
= pass_final() || (result
->opcode
== I_INCBIN
);
789 if (opcode_is_db(result
->opcode
) || result
->opcode
== I_INCBIN
) {
792 i
= stdscan(NULL
, &tokval
);
794 if (first
&& i
== ':') {
796 insn_is_label
= true;
800 oper_num
= parse_eops(&result
->eops
, critical
, db_bytes(result
->opcode
));
804 if (result
->opcode
== I_INCBIN
) {
806 * Correct syntax for INCBIN is that there should be
807 * one string operand, followed by one or two numeric
810 if (!result
->eops
|| result
->eops
->type
!= EOT_DB_STRING
)
811 nasm_nonfatal("`incbin' expects a file name");
812 else if (result
->eops
->next
&&
813 result
->eops
->next
->type
!= EOT_DB_NUMBER
)
814 nasm_nonfatal("`incbin': second parameter is"
816 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
817 result
->eops
->next
->next
->type
!= EOT_DB_NUMBER
)
818 nasm_nonfatal("`incbin': third parameter is"
820 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
821 result
->eops
->next
->next
->next
)
822 nasm_nonfatal("`incbin': more than three parameters");
826 * If we reach here, one of the above errors happened.
827 * Throw the instruction away.
832 result
->operands
= oper_num
;
835 *!db-empty [on] no operand for data declaration
836 *! warns about a \c{DB}, \c{DW}, etc declaration
837 *! with no operands, producing no output.
838 *! This is permitted, but often indicative of an error.
841 nasm_warn(WARN_DB_EMPTY
, "no operand for data declaration");
847 * Now we begin to parse the operands. There may be up to four
848 * of these, separated by commas, and terminated by a zero token.
850 far_jmp_ok
= result
->opcode
== I_JMP
|| result
->opcode
== I_CALL
;
852 for (opnum
= 0; opnum
< MAX_OPERANDS
; opnum
++) {
853 operand
*op
= &result
->oprs
[opnum
];
854 expr
*value
; /* used most of the time */
855 bool mref
= false; /* is this going to be a memory ref? */
856 int bracket
= 0; /* is it a [] mref, or a "naked" mref? */
857 bool mib
; /* compound (mib) mref? */
859 decoflags_t brace_flags
= 0; /* flags for decorators in braces */
863 i
= stdscan(NULL
, &tokval
);
865 break; /* end of operands: get out of here */
866 else if (first
&& i
== ':') {
867 insn_is_label
= true;
871 op
->type
= 0; /* so far, no override */
872 /* size specifiers */
873 while (i
== TOKEN_SPECIAL
|| i
== TOKEN_SIZE
) {
874 switch (tokval
.t_integer
) {
876 if (!setsize
) /* we want to use only the first */
932 nasm_nonfatal("invalid operand size specification");
934 i
= stdscan(NULL
, &tokval
);
937 if (i
== '[' || i
== TOKEN_MASM_PTR
|| i
== '&') {
938 /* memory reference */
940 bracket
+= (i
== '[');
941 i
= stdscan(NULL
, &tokval
);
954 process_size_override(result
, op
);
962 tokval
.t_type
= TOKEN_NUM
;
963 tokval
.t_integer
= 0;
964 stdscan_set(stdscan_get() - 1); /* rewind the comma */
968 case TOKEN_MASM_FLAT
:
969 i
= stdscan(NULL
, &tokval
);
971 nasm_nonfatal("unknown use of FLAT in MASM emulation");
983 i
= stdscan(NULL
, &tokval
);
987 value
= evaluate(stdscan
, NULL
, &tokval
,
988 &op
->opflags
, critical
, &hints
);
990 if (op
->opflags
& OPFLAG_FORWARD
) {
991 result
->forw_ref
= true;
993 if (!value
) /* Error in evaluator */
996 if (i
== '[' && !bracket
) {
997 /* displacement[regs] syntax */
999 parse_mref(op
, value
); /* Process what we have so far */
1003 if (i
== ':' && (mref
|| !far_jmp_ok
)) {
1004 /* segment override? */
1008 * Process the segment override.
1010 if (value
[1].type
!= 0 ||
1011 value
->value
!= 1 ||
1012 !IS_SREG(value
->type
))
1013 nasm_nonfatal("invalid segment override");
1014 else if (result
->prefixes
[PPS_SEG
])
1015 nasm_nonfatal("instruction has conflicting segment overrides");
1017 result
->prefixes
[PPS_SEG
] = value
->type
;
1018 if (IS_FSGS(value
->type
))
1019 op
->eaflags
|= EAF_FSGS
;
1022 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
1027 if (mref
&& bracket
&& i
== ',') {
1028 /* [seg:base+offset,index*scale] syntax (mib) */
1029 operand o2
; /* Index operand */
1031 if (parse_mref(op
, value
))
1034 i
= stdscan(NULL
, &tokval
); /* Eat comma */
1035 value
= evaluate(stdscan
, NULL
, &tokval
, &op
->opflags
,
1042 if (parse_mref(&o2
, value
))
1045 if (o2
.basereg
!= -1 && o2
.indexreg
== -1) {
1046 o2
.indexreg
= o2
.basereg
;
1051 if (op
->indexreg
!= -1 || o2
.basereg
!= -1 || o2
.offset
!= 0 ||
1052 o2
.segment
!= NO_SEG
|| o2
.wrt
!= NO_SEG
) {
1053 nasm_nonfatal("invalid mib expression");
1057 op
->indexreg
= o2
.indexreg
;
1058 op
->scale
= o2
.scale
;
1060 if (op
->basereg
!= -1) {
1061 op
->hintbase
= op
->basereg
;
1062 op
->hinttype
= EAH_MAKEBASE
;
1063 } else if (op
->indexreg
!= -1) {
1064 op
->hintbase
= op
->indexreg
;
1065 op
->hinttype
= EAH_NOTBASE
;
1068 op
->hinttype
= EAH_NOHINT
;
1079 i
= stdscan(NULL
, &tokval
);
1081 nasm_nonfatal("expecting ] at end of memory operand");
1084 } else if (bracket
== 0) {
1086 } else if (bracket
> 0) {
1087 nasm_nonfatal("excess brackets in memory operand");
1089 } else if (bracket
< 0) {
1090 nasm_nonfatal("unmatched ] in memory operand");
1094 if (i
== TOKEN_DECORATOR
|| i
== TOKEN_OPMASK
) {
1095 /* parse opmask (and zeroing) after an operand */
1096 recover
= parse_braces(&brace_flags
);
1099 if (!recover
&& i
!= 0 && i
!= ',') {
1100 nasm_nonfatal("comma, decorator or end of line expected, got %d", i
);
1103 } else { /* immediate operand */
1104 if (i
!= 0 && i
!= ',' && i
!= ':' &&
1105 i
!= TOKEN_DECORATOR
&& i
!= TOKEN_OPMASK
) {
1106 nasm_nonfatal("comma, colon, decorator or end of "
1107 "line expected after operand");
1109 } else if (i
== ':') {
1111 } else if (i
== TOKEN_DECORATOR
|| i
== TOKEN_OPMASK
) {
1112 /* parse opmask (and zeroing) after an operand */
1113 recover
= parse_braces(&brace_flags
);
1117 do { /* error recovery */
1118 i
= stdscan(NULL
, &tokval
);
1119 } while (i
!= 0 && i
!= ',');
1123 * now convert the exprs returned from evaluate()
1124 * into operand descriptions...
1126 op
->decoflags
|= brace_flags
;
1128 if (mref
) { /* it's a memory reference */
1129 /* A mib reference was fully parsed already */
1131 if (parse_mref(op
, value
))
1133 op
->hintbase
= hints
.base
;
1134 op
->hinttype
= hints
.type
;
1136 mref_set_optype(op
);
1137 } else if ((op
->type
& FAR
) && !far_jmp_ok
) {
1138 nasm_nonfatal("invalid use of FAR operand specifier");
1140 } else { /* it's not a memory reference */
1141 if (is_just_unknown(value
)) { /* it's immediate but unknown */
1142 op
->type
|= IMMEDIATE
;
1143 op
->opflags
|= OPFLAG_UNKNOWN
;
1144 op
->offset
= 0; /* don't care */
1145 op
->segment
= NO_SEG
; /* don't care again */
1146 op
->wrt
= NO_SEG
; /* still don't care */
1148 if(optimizing
.level
>= 0 && !(op
->type
& STRICT
)) {
1151 UNITY
| SBYTEWORD
| SBYTEDWORD
| UDWORD
| SDWORD
;
1153 } else if (is_reloc(value
)) { /* it's immediate */
1154 uint64_t n
= reloc_value(value
);
1156 op
->type
|= IMMEDIATE
;
1158 op
->segment
= reloc_seg(value
);
1159 op
->wrt
= reloc_wrt(value
);
1160 op
->opflags
|= is_self_relative(value
) ? OPFLAG_RELATIVE
: 0;
1162 if (is_simple(value
)) {
1165 if (optimizing
.level
>= 0 && !(op
->type
& STRICT
)) {
1166 if ((uint32_t) (n
+ 128) <= 255)
1167 op
->type
|= SBYTEDWORD
;
1168 if ((uint16_t) (n
+ 128) <= 255)
1169 op
->type
|= SBYTEWORD
;
1170 if (n
<= UINT64_C(0xFFFFFFFF))
1172 if (n
+ UINT64_C(0x80000000) <= UINT64_C(0xFFFFFFFF))
1176 } else if (value
->type
== EXPR_RDSAE
) {
1178 * it's not an operand but a rounding or SAE decorator.
1179 * put the decorator information in the (opflag_t) type field
1180 * of previous operand.
1183 switch (value
->value
) {
1189 op
->decoflags
|= (value
->value
== BRC_SAE
? SAE
: ER
);
1190 result
->evex_rm
= value
->value
;
1193 nasm_nonfatal("invalid decorator");
1196 } else { /* it's a register */
1198 uint64_t regset_size
= 0;
1200 if (value
->type
>= EXPR_SIMPLE
|| value
->value
!= 1) {
1201 nasm_nonfatal("invalid operand type");
1206 * We do not allow any kind of expression, except for
1207 * reg+value in which case it is a register set.
1209 for (i
= 1; value
[i
].type
; i
++) {
1210 if (!value
[i
].value
)
1213 switch (value
[i
].type
) {
1216 regset_size
= value
[i
].value
+ 1;
1221 nasm_nonfatal("invalid operand type");
1226 if ((regset_size
& (regset_size
- 1)) ||
1227 regset_size
>= (UINT64_C(1) << REGSET_BITS
)) {
1228 nasm_nonfatalf(ERR_PASS2
, "invalid register set size");
1232 /* clear overrides, except TO which applies to FPU regs */
1233 if (op
->type
& ~TO
) {
1235 * we want to produce a warning iff the specified size
1236 * is different from the register size
1238 rs
= op
->type
& SIZE_MASK
;
1244 * Make sure we're not out of nasm_reg_flags, still
1245 * probably this should be fixed when we're defining
1248 * An easy trigger is
1250 * e equ 0x80000000:0
1254 if (value
->type
< EXPR_REG_START
||
1255 value
->type
> EXPR_REG_END
) {
1256 nasm_nonfatal("invalid operand type");
1261 op
->type
|= REGISTER
;
1262 op
->type
|= nasm_reg_flags
[value
->type
];
1263 op
->type
|= (regset_size
>> 1) << REGSET_SHIFT
;
1264 op
->decoflags
|= brace_flags
;
1265 op
->basereg
= value
->type
;
1268 opflags_t opsize
= nasm_reg_flags
[value
->type
] & SIZE_MASK
;
1270 op
->type
|= rs
; /* For non-size-specific registers, permit size override */
1271 } else if (opsize
!= rs
) {
1273 *!regsize [on] register size specification ignored
1275 *! warns about a register with implicit size (such as \c{EAX}, which is always 32 bits)
1276 *! been given an explicit size specification which is inconsistent with the size
1277 *! of the named register, e.g. \c{WORD EAX}. \c{DWORD EAX} or \c{WORD AX} are
1278 *! permitted, and do not trigger this warning. Some registers which \e{do not} imply
1279 *! a specific size, such as \c{K0}, may need this specification unless the instruction
1280 *! itself implies the instruction size:
1282 *! \c KMOVW K0,[foo] ; Permitted, KMOVW implies 16 bits
1283 *! \c KMOV WORD K0,[foo] ; Permitted, WORD K0 specifies instruction size
1284 *! \c KMOV K0,WORD [foo] ; Permitted, WORD [foo] specifies instruction size
1285 *! \c KMOV K0,[foo] ; Not permitted, instruction size ambiguous
1287 nasm_warn(WARN_REGSIZE
, "invalid register size specification ignored");
1293 /* remember the position of operand having broadcasting/ER mode */
1294 if (op
->decoflags
& (BRDCAST_MASK
| ER
| SAE
))
1295 result
->evex_brerop
= opnum
;
1298 result
->operands
= opnum
; /* set operand count */
1300 /* clear remaining operands */
1301 while (opnum
< MAX_OPERANDS
)
1302 result
->oprs
[opnum
++].type
= 0;
1307 result
->opcode
= I_none
;
1311 static int end_expression_next(void)
1318 i
= stdscan(NULL
, &tv
);
1321 return (i
== ',' || i
== ';' || i
== ')' || !i
);
1324 static void free_eops(extop
*e
)
1332 free_eops(e
->val
.subexpr
);
1335 case EOT_DB_STRING_FREE
:
1336 nasm_free(e
->val
.string
.data
);
1348 void cleanup_insn(insn
* i
)