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 * assemble.c code generation for the Netwide Assembler
37 * Bytecode specification
38 * ----------------------
41 * Codes Mnemonic Explanation
43 * \0 terminates the code. (Unless it's a literal of course.)
44 * \1..\4 that many literal bytes follow in the code stream
45 * \5 add 4 to the primary operand number (b, low octdigit)
46 * \6 add 4 to the secondary operand number (a, middle octdigit)
47 * \7 add 4 to both the primary and the secondary operand number
48 * \10..\13 a literal byte follows in the code stream, to be added
49 * to the register value of operand 0..3
50 * \14..\17 the position of index register operand in MIB (BND insns)
51 * \20..\23 ib a byte immediate operand, from operand 0..3
52 * \24..\27 ib,u a zero-extended byte immediate operand, from operand 0..3
53 * \30..\33 iw a word immediate operand, from operand 0..3
54 * \34..\37 iwd select between \3[0-3] and \4[0-3] depending on 16/32 bit
55 * assembly mode or the operand-size override on the operand
56 * \40..\43 id a long immediate operand, from operand 0..3
57 * \44..\47 iwdq select between \3[0-3], \4[0-3] and \5[4-7]
58 * depending on the address size of the instruction.
59 * \50..\53 rel8 a byte relative operand, from operand 0..3
60 * \54..\57 iq a qword immediate operand, from operand 0..3
61 * \60..\63 rel16 a word relative operand, from operand 0..3
62 * \64..\67 rel select between \6[0-3] and \7[0-3] depending on 16/32 bit
63 * assembly mode or the operand-size override on the operand
64 * \70..\73 rel32 a long relative operand, from operand 0..3
65 * \74..\77 seg a word constant, from the _segment_ part of operand 0..3
66 * \1ab a ModRM, calculated on EA in operand a, with the spare
67 * field the register value of operand b.
68 * \172\ab the register number from operand a in bits 7..4, with
69 * the 4-bit immediate from operand b in bits 3..0.
70 * \173\xab the register number from operand a in bits 7..4, with
71 * the value b in bits 3..0.
72 * \174..\177 the register number from operand 0..3 in bits 7..4, and
73 * an arbitrary value in bits 3..0 (assembled as zero.)
74 * \2ab a ModRM, calculated on EA in operand a, with the spare
75 * field equal to digit b.
77 * \240..\243 this instruction uses EVEX rather than REX or VEX/XOP, with the
78 * V field taken from operand 0..3.
79 * \250 this instruction uses EVEX rather than REX or VEX/XOP, with the
80 * V field set to 1111b.
82 * EVEX prefixes are followed by the sequence:
83 * \cm\wlp\tup where cm is:
85 * c = 2 for EVEX and mmmm is the M field (EVEX.P0[3:0])
88 * [l0] ll = 0 (.128, .lz)
91 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
93 * [w0] ww = 0 for W = 0
94 * [w1] ww = 1 for W = 1
95 * [wig] ww = 2 for W don't care (always assembled as 0)
96 * [ww] ww = 3 for W used as REX.W
98 * [p0] pp = 0 for no prefix
99 * [60] pp = 1 for legacy prefix 60
103 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
104 * (compressed displacement encoding)
106 * \254..\257 id,s a signed 32-bit operand to be extended to 64 bits.
107 * \260..\263 this instruction uses VEX/XOP rather than REX, with the
108 * V field taken from operand 0..3.
109 * \270 this instruction uses VEX/XOP rather than REX, with the
110 * V field set to 1111b.
112 * VEX/XOP prefixes are followed by the sequence:
113 * \tmm\wlp where mm is the M field; and wlp is:
115 * [l0] ll = 0 for L = 0 (.128, .lz)
116 * [l1] ll = 1 for L = 1 (.256)
117 * [lig] ll = 2 for L don't care (always assembled as 0)
119 * [w0] ww = 0 for W = 0
120 * [w1 ] ww = 1 for W = 1
121 * [wig] ww = 2 for W don't care (always assembled as 0)
122 * [ww] ww = 3 for W used as REX.W
124 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
126 * \271 hlexr instruction takes XRELEASE (F3) with or without lock
127 * \272 hlenl instruction takes XACQUIRE/XRELEASE with or without lock
128 * \273 hle instruction takes XACQUIRE/XRELEASE with lock only
129 * \274..\277 ib,s a byte immediate operand, from operand 0..3, sign-extended
130 * to the operand size (if o16/o32/o64 present) or the bit size
131 * \310 a16 indicates fixed 16-bit address size, i.e. optional 0x67.
132 * \311 a32 indicates fixed 32-bit address size, i.e. optional 0x67.
133 * \312 adf (disassembler only) invalid with non-default address size.
134 * \313 a64 indicates fixed 64-bit address size, 0x67 invalid.
135 * \314 norexb (disassembler only) invalid with REX.B
136 * \315 norexx (disassembler only) invalid with REX.X
137 * \316 norexr (disassembler only) invalid with REX.R
138 * \317 norexw (disassembler only) invalid with REX.W
139 * \320 o16 indicates fixed 16-bit operand size, i.e. optional 0x66.
140 * \321 o32 indicates fixed 32-bit operand size, i.e. optional 0x66.
141 * \322 odf indicates that this instruction is only valid when the
142 * operand size is the default (instruction to disassembler,
143 * generates no code in the assembler)
144 * \323 o64nw indicates fixed 64-bit operand size, REX on extensions only.
145 * \324 o64 indicates 64-bit operand size requiring REX prefix.
146 * \325 nohi instruction which always uses spl/bpl/sil/dil
147 * \326 nof3 instruction not valid with 0xF3 REP prefix. Hint for
148 disassembler only; for SSE instructions.
149 * \330 a literal byte follows in the code stream, to be added
150 * to the condition code value of the instruction.
151 * \331 norep instruction not valid with REP prefix. Hint for
152 * disassembler only; for SSE instructions.
153 * \332 f2i REP prefix (0xF2 byte) used as opcode extension.
154 * \333 f3i REP prefix (0xF3 byte) used as opcode extension.
155 * \334 rex.l LOCK prefix used as REX.R (used in non-64-bit mode)
156 * \335 repe disassemble a rep (0xF3 byte) prefix as repe not rep.
157 * \336 mustrep force a REP(E) prefix (0xF3) even if not specified.
158 * \337 mustrepne force a REPNE prefix (0xF2) even if not specified.
159 * \336-\337 are still listed as prefixes in the disassembler.
160 * \340 resb reserve <operand 0> bytes of uninitialized storage.
161 * Operand 0 had better be a segmentless constant.
162 * \341 wait this instruction needs a WAIT "prefix"
163 * \360 np no SSE prefix (== \364\331)
164 * \361 66 SSE prefix (== \366\331)
165 * \364 !osp operand-size prefix (0x66) not permitted
166 * \365 !asp address-size prefix (0x67) not permitted
167 * \366 operand-size prefix (0x66) used as opcode extension
168 * \367 address-size prefix (0x67) used as opcode extension
169 * \370,\371 jcc8 match only if operand 0 meets byte jump criteria.
170 * jmp8 370 is used for Jcc, 371 is used for JMP.
171 * \373 jlen assemble 0x03 if bits==16, 0x05 if bits==32;
172 * used for conditional jump over longer jump
173 * \374 vsibx|vm32x|vm64x this instruction takes an XMM VSIB memory EA
174 * \375 vsiby|vm32y|vm64y this instruction takes an YMM VSIB memory EA
175 * \376 vsibz|vm32z|vm64z this instruction takes an ZMM VSIB memory EA
178 #include "compiler.h"
184 #include "assemble.h"
192 * Matching errors. These should be sorted so that more specific
193 * errors come later in the sequence.
211 * Matching success; the conditional ones first
213 MOK_JUMP
, /* Matching OK but needs jmp_match() */
214 MOK_GOOD
/* Matching unconditionally OK */
218 enum ea_type type
; /* what kind of EA is this? */
219 int sib_present
; /* is a SIB byte necessary? */
220 int bytes
; /* # of bytes of offset needed */
221 int size
; /* lazy - this is sib+bytes+1 */
222 uint8_t modrm
, sib
, rex
, rip
; /* the bytes themselves */
223 int8_t disp8
; /* compressed displacement for EVEX */
226 #define GEN_SIB(scale, index, base) \
227 (((scale) << 6) | ((index) << 3) | ((base)))
229 #define GEN_MODRM(mod, reg, rm) \
230 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
232 static int64_t calcsize(int32_t, int64_t, int, insn
*,
233 const struct itemplate
*);
234 static int emit_prefix(struct out_data
*data
, const int bits
, insn
*ins
);
235 static void gencode(struct out_data
*data
, insn
*ins
);
236 static enum match_result
find_match(const struct itemplate
**tempp
,
238 int32_t segment
, int64_t offset
, int bits
);
239 static enum match_result
matches(const struct itemplate
*, insn
*, int bits
);
240 static opflags_t
regflag(const operand
*);
241 static int32_t regval(const operand
*);
242 static int rexflags(int, opflags_t
, int);
243 static int op_rexflags(const operand
*, int);
244 static int op_evexflags(const operand
*, int, uint8_t);
245 static void add_asp(insn
*, int);
247 static enum ea_type
process_ea(operand
*, ea
*, int, int,
248 opflags_t
, insn
*, const char **);
250 static inline bool absolute_op(const struct operand
*o
)
252 return o
->segment
== NO_SEG
&& o
->wrt
== NO_SEG
&&
253 !(o
->opflags
& OPFLAG_RELATIVE
);
256 static int has_prefix(insn
* ins
, enum prefix_pos pos
, int prefix
)
258 return ins
->prefixes
[pos
] == prefix
;
261 static void assert_no_prefix(insn
* ins
, enum prefix_pos pos
)
263 if (ins
->prefixes
[pos
])
264 nasm_nonfatal("invalid %s prefix", prefix_name(ins
->prefixes
[pos
]));
267 static const char *size_name(int size
)
291 static void warn_overflow(int size
)
293 nasm_warn(ERR_PASS2
| WARN_NUMBER_OVERFLOW
, "%s data exceeds bounds",
297 static void warn_overflow_const(int64_t data
, int size
)
299 if (overflow_general(data
, size
))
303 static void warn_overflow_out(int64_t data
, int size
, enum out_sign sign
)
309 err
= overflow_general(data
, size
);
312 err
= overflow_signed(data
, size
);
315 err
= overflow_unsigned(data
, size
);
327 * This routine wrappers the real output format's output routine,
328 * in order to pass a copy of the data off to the listing file
329 * generator at the same time, flatten unnecessary relocations,
330 * and verify backend compatibility.
333 * This warning is currently issued by backends, but in the future
334 * this code should be centralized.
336 *!zeroing [on] RESx in initialized section becomes zero
337 *! a \c{RESx} directive was used in a section which contains
338 *! initialized data, and the output format does not support
339 *! this. Instead, this will be replaced with explicit zero
340 *! content, which may produce a large output file.
342 static void out(struct out_data
*data
)
344 static int32_t lineno
= 0; /* static!!! */
345 static const char *lnfname
= NULL
;
351 uint64_t zeropad
= 0;
353 int32_t fixseg
; /* Segment for which to produce fixed data */
356 return; /* Nothing to do */
359 * Convert addresses to RAWDATA if possible
360 * XXX: not all backends want this for global symbols!!!!
362 switch (data
->type
) {
364 addrval
= data
->toffset
;
365 fixseg
= NO_SEG
; /* Absolute address is fixed data */
369 addrval
= data
->toffset
- data
->relbase
;
370 fixseg
= data
->segment
; /* Our own segment is fixed data */
374 nasm_assert(data
->size
<= 8);
376 amax
= ofmt
->maxbits
>> 3; /* Maximum address size in bytes */
377 if ((ofmt
->flags
& OFMT_KEEP_ADDR
) == 0 && data
->tsegment
== fixseg
&&
378 data
->twrt
== NO_SEG
) {
379 if (asize
>= (size_t)(data
->bits
>> 3))
380 data
->sign
= OUT_WRAP
; /* Support address space wrapping for low-bit modes */
381 warn_overflow_out(addrval
, asize
, data
->sign
);
382 xdata
.q
= cpu_to_le64(addrval
);
383 data
->data
= xdata
.b
;
384 data
->type
= OUT_RAWDATA
;
385 asize
= amax
= 0; /* No longer an address */
390 nasm_assert(data
->size
<= 8);
396 asize
= amax
= 0; /* Not an address */
401 * this call to src_get determines when we call the
402 * debug-format-specific "linenum" function
403 * it updates lineno and lnfname to the current values
404 * returning 0 if "same as last time", -2 if lnfname
405 * changed, and the amount by which lineno changed,
406 * if it did. thus, these variables must be static
409 if (src_get(&lineno
, &lnfname
))
410 dfmt
->linenum(lnfname
, lineno
, data
->segment
);
413 if (data
->type
== OUT_RELADDR
|| data
->sign
== OUT_SIGNED
) {
414 nasm_nonfatal("%u-bit signed relocation unsupported by output format %s",
415 (unsigned int)(asize
<< 3), ofmt
->shortname
);
418 *!zext-reloc [on] relocation zero-extended to match output format
419 *! warns that a relocation has been zero-extended due
420 *! to limitations in the output format.
422 nasm_warn(WARN_ZEXT_RELOC
,
423 "%u-bit %s relocation zero-extended from %u bits",
424 (unsigned int)(asize
<< 3),
425 data
->type
== OUT_SEGMENT
? "segment" : "unsigned",
426 (unsigned int)(amax
<< 3));
428 zeropad
= data
->size
- amax
;
433 if (likely(data
->segment
!= NO_SEG
)) {
436 /* Outputting to ABSOLUTE section - only reserve is permitted */
437 if (data
->type
!= OUT_RESERVE
)
438 nasm_nonfatal("attempt to assemble code in [ABSOLUTE] space");
439 /* No need to push to the backend */
442 data
->offset
+= data
->size
;
443 data
->insoffs
+= data
->size
;
446 data
->type
= OUT_ZERODATA
;
447 data
->size
= zeropad
;
450 data
->offset
+= zeropad
;
451 data
->insoffs
+= zeropad
;
452 data
->size
+= zeropad
; /* Restore original size value */
456 static inline void out_rawdata(struct out_data
*data
, const void *rawdata
,
459 data
->type
= OUT_RAWDATA
;
460 data
->data
= rawdata
;
465 static void out_rawbyte(struct out_data
*data
, uint8_t byte
)
467 data
->type
= OUT_RAWDATA
;
473 static inline void out_reserve(struct out_data
*data
, uint64_t size
)
475 data
->type
= OUT_RESERVE
;
480 static void out_segment(struct out_data
*data
, const struct operand
*opx
)
482 if (opx
->opflags
& OPFLAG_RELATIVE
)
483 nasm_nonfatal("segment references cannot be relative");
485 data
->type
= OUT_SEGMENT
;
486 data
->sign
= OUT_UNSIGNED
;
488 data
->toffset
= opx
->offset
;
489 data
->tsegment
= ofmt
->segbase(opx
->segment
| 1);
490 data
->twrt
= opx
->wrt
;
494 static void out_imm(struct out_data
*data
, const struct operand
*opx
,
495 int size
, enum out_sign sign
)
497 if (opx
->segment
!= NO_SEG
&& (opx
->segment
& 1)) {
499 * This is actually a segment reference, but eval() has
500 * already called ofmt->segbase() for us. Sigh.
503 nasm_nonfatal("segment reference must be 16 bits");
505 data
->type
= OUT_SEGMENT
;
507 data
->type
= (opx
->opflags
& OPFLAG_RELATIVE
)
508 ? OUT_RELADDR
: OUT_ADDRESS
;
511 data
->toffset
= opx
->offset
;
512 data
->tsegment
= opx
->segment
;
513 data
->twrt
= opx
->wrt
;
515 * XXX: improve this if at some point in the future we can
516 * distinguish the subtrahend in expressions like [foo - bar]
517 * where bar is a symbol in the current segment. However, at the
518 * current point, if OPFLAG_RELATIVE is set that subtraction has
526 static void out_reladdr(struct out_data
*data
, const struct operand
*opx
,
529 if (opx
->opflags
& OPFLAG_RELATIVE
)
530 nasm_nonfatal("invalid use of self-relative expression");
532 data
->type
= OUT_RELADDR
;
533 data
->sign
= OUT_SIGNED
;
535 data
->toffset
= opx
->offset
;
536 data
->tsegment
= opx
->segment
;
537 data
->twrt
= opx
->wrt
;
538 data
->relbase
= data
->offset
+ (data
->inslen
- data
->insoffs
);
542 static bool jmp_match(int32_t segment
, int64_t offset
, int bits
,
543 insn
* ins
, const struct itemplate
*temp
)
546 const uint8_t *code
= temp
->code
;
550 if (((c
& ~1) != 0370) || (ins
->oprs
[0].type
& STRICT
))
552 if (!optimizing
.level
|| (optimizing
.flag
& OPTIM_DISABLE_JMP_MATCH
))
554 if (optimizing
.level
< 0 && c
== 0371)
557 isize
= calcsize(segment
, offset
, bits
, ins
, temp
);
559 if (ins
->oprs
[0].opflags
& OPFLAG_UNKNOWN
)
560 /* Be optimistic in pass 1 */
563 if (ins
->oprs
[0].segment
!= segment
)
566 isize
= ins
->oprs
[0].offset
- offset
- isize
; /* isize is delta */
567 is_byte
= (isize
>= -128 && isize
<= 127); /* is it byte size? */
569 if (is_byte
&& c
== 0371 && ins
->prefixes
[PPS_REP
] == P_BND
) {
570 /* jmp short (opcode eb) cannot be used with bnd prefix. */
571 ins
->prefixes
[PPS_REP
] = P_none
;
573 *!bnd [on] invalid BND prefixes
574 *! warns about ineffective use of the \c{BND} prefix when the
575 *! \c{JMP} instruction is converted to the \c{SHORT} form.
576 *! This should be extremely rare since the short \c{JMP} only
577 *! is applicable to jumps inside the same module, but if
578 *! it is legitimate, it may be necessary to use
579 *! \c{bnd jmp dword}.
581 nasm_warn(WARN_BND
| ERR_PASS2
,
582 "jmp short does not init bnd regs - bnd prefix dropped");
588 static inline int64_t merge_resb(insn
*ins
, int64_t isize
)
590 int nbytes
= resb_bytes(ins
->opcode
);
595 if (isize
!= nbytes
* ins
->oprs
[0].offset
)
596 return isize
; /* Has prefixes of some sort */
598 ins
->oprs
[0].offset
*= ins
->times
;
604 /* This must be handle non-power-of-2 alignment values */
605 static inline size_t pad_bytes(size_t len
, size_t align
)
607 size_t partial
= len
% align
;
608 return partial
? align
- partial
: 0;
611 static void out_eops(struct out_data
*data
, const extop
*e
)
622 out_eops(data
, e
->val
.subexpr
);
627 nasm_nonfatal("integer supplied as %d-bit data",
632 data
->inslen
= data
->size
= e
->elem
;
633 data
->tsegment
= e
->val
.num
.segment
;
634 data
->toffset
= e
->val
.num
.offset
;
635 data
->twrt
= e
->val
.num
.wrt
;
637 if (e
->val
.num
.segment
!= NO_SEG
&&
638 (e
->val
.num
.segment
& 1)) {
639 data
->type
= OUT_SEGMENT
;
640 data
->sign
= OUT_UNSIGNED
;
642 data
->type
= e
->val
.num
.relative
643 ? OUT_RELADDR
: OUT_ADDRESS
;
644 data
->sign
= OUT_WRAP
;
653 case EOT_DB_STRING_FREE
:
657 pad
= pad_bytes(e
->val
.string
.len
, e
->elem
);
658 len
= e
->val
.string
.len
+ pad
;
663 out_rawdata(data
, e
->val
.string
.data
, e
->val
.string
.len
);
665 out_rawdata(data
, zero_buffer
, pad
);
672 data
->inslen
= dup
* e
->elem
;
673 out_reserve(data
, data
->inslen
);
681 /* This is totally just a wild guess what is reasonable... */
682 #define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
684 int64_t assemble(int32_t segment
, int64_t start
, int bits
, insn
*instruction
)
686 struct out_data data
;
687 const struct itemplate
*temp
;
690 if (instruction
->opcode
== I_none
)
695 data
.segment
= segment
;
699 if (opcode_is_db(instruction
->opcode
)) {
700 out_eops(&data
, instruction
->eops
);
701 } else if (instruction
->opcode
== I_INCBIN
) {
702 const char *fname
= instruction
->eops
->val
.string
.data
;
704 size_t t
= instruction
->times
; /* INCBIN handles TIMES by itself */
707 const void *map
= NULL
;
709 size_t blk
= 0; /* Buffered I/O block size */
710 size_t m
= 0; /* Bytes last read */
715 fp
= nasm_open_read(fname
, NF_BINARY
|NF_FORMAP
);
717 nasm_nonfatal("`incbin': unable to open file `%s'",
722 len
= nasm_file_size(fp
);
724 if (len
== (off_t
)-1) {
725 nasm_nonfatal("`incbin': unable to get length of file `%s'",
730 if (instruction
->eops
->next
) {
731 base
= instruction
->eops
->next
->val
.num
.offset
;
736 if (instruction
->eops
->next
->next
&&
737 len
> (off_t
)instruction
->eops
->next
->next
->val
.num
.offset
)
738 len
= (off_t
)instruction
->eops
->next
->next
->val
.num
.offset
;
742 lfmt
->set_offset(data
.offset
);
743 lfmt
->uplevel(LIST_INCBIN
, len
);
748 /* Try to map file data */
749 map
= nasm_map_file(fp
, base
, len
);
751 blk
= len
< (off_t
)INCBIN_MAX_BUF
? (size_t)len
: INCBIN_MAX_BUF
;
752 buf
= nasm_malloc(blk
);
757 * Consider these irrelevant for INCBIN, since it is fully
758 * possible that these might be (way) bigger than an int
759 * can hold; there is, however, no reason to widen these
760 * types just for INCBIN. data.inslen == 0 signals to the
761 * backend that these fields are meaningless, if at all
768 out_rawdata(&data
, map
, len
);
769 } else if ((off_t
)m
== len
) {
770 out_rawdata(&data
, buf
, len
);
774 if (fseeko(fp
, base
, SEEK_SET
) < 0 || ferror(fp
)) {
775 nasm_nonfatal("`incbin': unable to seek on file `%s'",
780 m
= fread(buf
, 1, l
< (off_t
)blk
? (size_t)l
: blk
, fp
);
781 if (!m
|| feof(fp
)) {
783 * This shouldn't happen unless the file
784 * actually changes while we are reading
787 nasm_nonfatal("`incbin': unexpected EOF while"
788 " reading file `%s'", fname
);
791 out_rawdata(&data
, buf
, m
);
797 lfmt
->downlevel(LIST_INCBIN
);
798 if (instruction
->times
> 1) {
799 lfmt
->uplevel(LIST_TIMES
, instruction
->times
);
800 lfmt
->downlevel(LIST_TIMES
);
803 nasm_nonfatal("`incbin': error while"
804 " reading file `%s'", fname
);
810 nasm_unmap_file(map
, len
);
813 instruction
->times
= 1; /* Tell the upper layer not to iterate */
816 /* "Real" instruction */
818 /* Check to see if we need an address-size prefix */
819 add_asp(instruction
, bits
);
821 m
= find_match(&temp
, instruction
, data
.segment
, data
.offset
, bits
);
825 if (unlikely(itemp_has(temp
, IF_OBSOLETE
))) {
827 const char *whathappened
;
828 const char *validity
;
829 bool never
= itemp_has(temp
, IF_NEVER
);
832 * If IF_OBSOLETE is set, warn the user. Different
833 * warning classes for "obsolete but valid for this
834 * specific CPU" and "obsolete and gone."
836 *!obsolete-removed [on] instruction obsolete and removed on the target CPU
837 *! warns for an instruction which has been removed
838 *! from the architecture, and is no longer included
839 *! in the CPU definition given in the \c{[CPU]}
840 *! directive, for example \c{POP CS}, the opcode for
841 *! which, \c{0Fh}, instead is an opcode prefix on
842 *! CPUs newer than the first generation 8086.
844 *!obsolete-nop [on] instruction obsolete and is a noop on the target CPU
845 *! warns for an instruction which has been removed
846 *! from the architecture, but has been architecturally
847 *! defined to be a noop for future CPUs.
849 *!obsolete-valid [on] instruction obsolete but valid on the target CPU
850 *! warns for an instruction which has been removed
851 *! from the architecture, but is still valid on the
852 *! specific CPU given in the \c{CPU} directive. Code
853 *! using these instructions is most likely not
854 *! forward compatible.
857 whathappened
= never
? "never implemented" : "obsolete";
859 if (!never
&& !iflag_cmp_cpu_level(&insns_flags
[temp
->iflag_idx
], &cpu
)) {
860 warning
= WARN_OBSOLETE_VALID
;
861 validity
= "but valid on";
862 } else if (itemp_has(temp
, IF_NOP
)) {
863 warning
= WARN_OBSOLETE_NOP
;
864 validity
= "and is a noop on";
866 warning
= WARN_OBSOLETE_REMOVED
;
867 validity
= never
? "and invalid on" : "and removed from";
870 nasm_warn(warning
, "instruction %s %s the target CPU",
871 whathappened
, validity
);
878 data
.inslen
= calcsize(data
.segment
, data
.offset
,
879 bits
, instruction
, temp
);
880 nasm_assert(data
.inslen
>= 0);
881 data
.inslen
= merge_resb(instruction
, data
.inslen
);
883 gencode(&data
, instruction
);
884 nasm_assert(data
.insoffs
== data
.inslen
);
888 case MERR_OPSIZEMISSING
:
889 nasm_nonfatal("operation size not specified");
891 case MERR_OPSIZEMISMATCH
:
892 nasm_nonfatal("mismatch in operand sizes");
895 nasm_nonfatal("broadcast not permitted on this operand");
897 case MERR_BRNUMMISMATCH
:
898 nasm_nonfatal("mismatch in the number of broadcasting elements");
900 case MERR_MASKNOTHERE
:
901 nasm_nonfatal("mask not permitted on this operand");
903 case MERR_DECONOTHERE
:
904 nasm_nonfatal("unsupported mode decorator for instruction");
907 nasm_nonfatal("no instruction for this cpu level");
910 nasm_nonfatal("instruction not supported in %d-bit mode", bits
);
912 case MERR_ENCMISMATCH
:
913 nasm_nonfatal("specific encoding scheme not available");
916 nasm_nonfatal("bnd prefix is not allowed");
919 nasm_nonfatal("%s prefix is not allowed",
920 (has_prefix(instruction
, PPS_REP
, P_REPNE
) ?
923 case MERR_REGSETSIZE
:
924 nasm_nonfatal("invalid register set size");
927 nasm_nonfatal("register set not valid for operand");
930 nasm_nonfatal("invalid combination of opcode and operands");
934 instruction
->times
= 1; /* Avoid repeated error messages */
937 return data
.offset
- start
;
940 static int32_t eops_typeinfo(const extop
*e
)
942 int32_t typeinfo
= 0;
950 typeinfo
|= eops_typeinfo(e
->val
.subexpr
);
955 case 1: typeinfo
|= TY_BYTE
; break;
956 case 2: typeinfo
|= TY_WORD
; break;
957 case 4: typeinfo
|= TY_FLOAT
; break;
958 case 8: typeinfo
|= TY_QWORD
; break; /* double? */
959 case 10: typeinfo
|= TY_TBYTE
; break; /* long double? */
960 case 16: typeinfo
|= TY_YWORD
; break;
961 case 32: typeinfo
|= TY_ZWORD
; break;
968 case 1: typeinfo
|= TY_BYTE
; break;
969 case 2: typeinfo
|= TY_WORD
; break;
970 case 4: typeinfo
|= TY_DWORD
; break;
971 case 8: typeinfo
|= TY_QWORD
; break;
972 case 10: typeinfo
|= TY_TBYTE
; break;
973 case 16: typeinfo
|= TY_YWORD
; break;
974 case 32: typeinfo
|= TY_ZWORD
; break;
985 static inline void debug_set_db_type(insn
*instruction
)
988 int32_t typeinfo
= TYS_ELEMENTS(instruction
->operands
);
990 typeinfo
|= eops_typeinfo(instruction
->eops
);
991 dfmt
->debug_typevalue(typeinfo
);
994 static void debug_set_type(insn
*instruction
)
998 if (opcode_is_resb(instruction
->opcode
)) {
999 typeinfo
= TYS_ELEMENTS(instruction
->oprs
[0].offset
);
1001 switch (instruction
->opcode
) {
1003 typeinfo
|= TY_BYTE
;
1006 typeinfo
|= TY_WORD
;
1009 typeinfo
|= TY_DWORD
;
1012 typeinfo
|= TY_QWORD
;
1015 typeinfo
|= TY_TBYTE
;
1018 typeinfo
|= TY_OWORD
;
1021 typeinfo
|= TY_YWORD
;
1024 typeinfo
|= TY_ZWORD
;
1030 typeinfo
= TY_LABEL
;
1033 dfmt
->debug_typevalue(typeinfo
);
1037 /* Proecess an EQU directive */
1038 static void define_equ(insn
* instruction
)
1040 if (!instruction
->label
) {
1041 nasm_nonfatal("EQU not preceded by label");
1042 } else if (instruction
->operands
== 1 &&
1043 (instruction
->oprs
[0].type
& IMMEDIATE
) &&
1044 instruction
->oprs
[0].wrt
== NO_SEG
) {
1045 define_label(instruction
->label
,
1046 instruction
->oprs
[0].segment
,
1047 instruction
->oprs
[0].offset
, false);
1048 } else if (instruction
->operands
== 2
1049 && (instruction
->oprs
[0].type
& IMMEDIATE
)
1050 && (instruction
->oprs
[0].type
& COLON
)
1051 && instruction
->oprs
[0].segment
== NO_SEG
1052 && instruction
->oprs
[0].wrt
== NO_SEG
1053 && (instruction
->oprs
[1].type
& IMMEDIATE
)
1054 && instruction
->oprs
[1].segment
== NO_SEG
1055 && instruction
->oprs
[1].wrt
== NO_SEG
) {
1056 define_label(instruction
->label
,
1057 instruction
->oprs
[0].offset
| SEG_ABS
,
1058 instruction
->oprs
[1].offset
, false);
1060 nasm_nonfatal("bad syntax for EQU");
1064 static int64_t len_extops(const extop
*e
)
1075 isize
+= e
->dup
* len_extops(e
->val
.subexpr
);
1079 case EOT_DB_STRING_FREE
:
1081 pad
= pad_bytes(e
->val
.string
.len
, e
->elem
);
1082 isize
+= e
->dup
* (e
->val
.string
.len
+ pad
);
1086 warn_overflow_const(e
->val
.num
.offset
, e
->elem
);
1087 isize
+= e
->dup
* e
->elem
;
1090 case EOT_DB_RESERVE
:
1101 int64_t insn_size(int32_t segment
, int64_t offset
, int bits
, insn
*instruction
)
1103 const struct itemplate
*temp
;
1104 enum match_result m
;
1107 if (instruction
->opcode
== I_none
) {
1109 } else if (instruction
->opcode
== I_EQU
) {
1110 define_equ(instruction
);
1112 } else if (opcode_is_db(instruction
->opcode
)) {
1113 isize
= len_extops(instruction
->eops
);
1114 debug_set_db_type(instruction
);
1116 } else if (instruction
->opcode
== I_INCBIN
) {
1117 const extop
*e
= instruction
->eops
;
1118 const char *fname
= e
->val
.string
.data
;
1121 len
= nasm_file_size_by_path(fname
);
1122 if (len
== (off_t
)-1) {
1123 nasm_nonfatal("`incbin': unable to get length of file `%s'",
1130 if (len
<= (off_t
)e
->val
.num
.offset
) {
1133 len
-= e
->val
.num
.offset
;
1135 if (e
&& len
> (off_t
)e
->val
.num
.offset
) {
1136 len
= (off_t
)e
->val
.num
.offset
;
1141 len
*= instruction
->times
;
1142 instruction
->times
= 1; /* Tell the upper layer to not iterate */
1146 /* Normal instruction, or RESx */
1148 /* Check to see if we need an address-size prefix */
1149 add_asp(instruction
, bits
);
1151 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
1153 return -1; /* No match */
1155 isize
= calcsize(segment
, offset
, bits
, instruction
, temp
);
1156 debug_set_type(instruction
);
1157 isize
= merge_resb(instruction
, isize
);
1163 static void bad_hle_warn(const insn
* ins
, uint8_t hleok
)
1165 enum prefixes rep_pfx
= ins
->prefixes
[PPS_REP
];
1166 enum whatwarn
{ w_none
, w_lock
, w_inval
} ww
;
1167 static const enum whatwarn warn
[2][4] =
1169 { w_inval
, w_inval
, w_none
, w_lock
}, /* XACQUIRE */
1170 { w_inval
, w_none
, w_none
, w_lock
}, /* XRELEASE */
1174 n
= (unsigned int)rep_pfx
- P_XACQUIRE
;
1176 return; /* Not XACQUIRE/XRELEASE */
1178 ww
= warn
[n
][hleok
];
1179 if (!is_class(MEMORY
, ins
->oprs
[0].type
))
1180 ww
= w_inval
; /* HLE requires operand 0 to be memory */
1183 *!hle [on] invalid HLE prefixes
1184 *! warns about invalid use of the HLE \c{XACQUIRE} or \c{XRELEASE}
1192 if (ins
->prefixes
[PPS_LOCK
] != P_LOCK
) {
1193 nasm_warn(WARN_HLE
| ERR_PASS2
,
1194 "%s with this instruction requires lock",
1195 prefix_name(rep_pfx
));
1200 nasm_warn(WARN_HLE
| ERR_PASS2
,
1201 "%s invalid with this instruction",
1202 prefix_name(rep_pfx
));
1207 /* Common construct */
1208 #define case3(x) case (x): case (x)+1: case (x)+2
1209 #define case4(x) case3(x): case (x)+3
1211 static int64_t calcsize(int32_t segment
, int64_t offset
, int bits
,
1212 insn
* ins
, const struct itemplate
*temp
)
1214 const uint8_t *codes
= temp
->code
;
1219 struct operand
*opx
;
1223 bool lockcheck
= true;
1224 enum reg_enum mib_index
= R_none
; /* For a separate index MIB reg form */
1227 ins
->rex
= 0; /* Ensure REX is reset */
1228 eat
= EA_SCALAR
; /* Expect a scalar EA */
1229 memset(ins
->evex_p
, 0, 3); /* Ensure EVEX is reset */
1231 if (ins
->prefixes
[PPS_OSIZE
] == P_O64
)
1234 (void)segment
; /* Don't warn that this parameter is unused */
1235 (void)offset
; /* Don't warn that this parameter is unused */
1239 op1
= (c
& 3) + ((opex
& 1) << 2);
1240 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
1241 opx
= &ins
->oprs
[op1
];
1242 opex
= 0; /* For the next iteration */
1246 codes
+= c
, length
+= c
;
1255 op_rexflags(opx
, REX_B
|REX_H
|REX_P
|REX_W
);
1260 /* this is an index reg of MIB operand */
1261 mib_index
= opx
->basereg
;
1274 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1275 length
+= (opx
->type
& BITS16
) ? 2 : 4;
1277 length
+= (bits
== 16) ? 2 : 4;
1285 length
+= ins
->addr_size
>> 3;
1293 length
+= 8; /* MOV reg64/imm */
1301 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1302 length
+= (opx
->type
& BITS16
) ? 2 : 4;
1304 length
+= (bits
== 16) ? 2 : 4;
1327 ins
->vexreg
= regval(opx
);
1328 ins
->evex_p
[2] |= op_evexflags(opx
, EVEX_P2VP
, 2); /* High-16 NDS */
1329 ins
->vex_cm
= *codes
++;
1330 ins
->vex_wlp
= *codes
++;
1331 ins
->evex_tuple
= (*codes
++ - 0300);
1337 ins
->vex_cm
= *codes
++;
1338 ins
->vex_wlp
= *codes
++;
1339 ins
->evex_tuple
= (*codes
++ - 0300);
1348 ins
->vexreg
= regval(opx
);
1349 ins
->vex_cm
= *codes
++;
1350 ins
->vex_wlp
= *codes
++;
1356 ins
->vex_cm
= *codes
++;
1357 ins
->vex_wlp
= *codes
++;
1374 length
+= (bits
!= 16) && !has_prefix(ins
, PPS_ASIZE
, P_A16
);
1378 length
+= (bits
!= 32) && !has_prefix(ins
, PPS_ASIZE
, P_A32
);
1385 if (bits
!= 64 || has_prefix(ins
, PPS_ASIZE
, P_A16
) ||
1386 has_prefix(ins
, PPS_ASIZE
, P_A32
))
1395 enum prefixes pfx
= ins
->prefixes
[PPS_OSIZE
];
1399 nasm_warn(WARN_OTHER
|ERR_PASS2
, "invalid operand size prefix");
1401 ins
->prefixes
[PPS_OSIZE
] = P_O16
;
1407 enum prefixes pfx
= ins
->prefixes
[PPS_OSIZE
];
1411 nasm_warn(WARN_OTHER
|ERR_PASS2
, "invalid operand size prefix");
1413 ins
->prefixes
[PPS_OSIZE
] = P_O32
;
1455 if (!ins
->prefixes
[PPS_REP
])
1456 ins
->prefixes
[PPS_REP
] = P_REP
;
1460 if (!ins
->prefixes
[PPS_REP
])
1461 ins
->prefixes
[PPS_REP
] = P_REPNE
;
1465 if (!absolute_op(&ins
->oprs
[0]))
1466 nasm_nonfatal("attempt to reserve non-constant"
1467 " quantity of BSS space");
1468 else if (ins
->oprs
[0].opflags
& OPFLAG_FORWARD
)
1469 nasm_warn(WARN_OTHER
, "forward reference in RESx "
1470 "can have unpredictable results");
1472 length
+= ins
->oprs
[0].offset
* resb_bytes(ins
->opcode
);
1476 if (!ins
->prefixes
[PPS_WAIT
])
1477 ins
->prefixes
[PPS_WAIT
] = P_WAIT
;
1532 struct operand
*opy
= &ins
->oprs
[op2
];
1533 struct operand
*op_er_sae
;
1535 ea_data
.rex
= 0; /* Ensure ea.REX is initially 0 */
1538 /* pick rfield from operand b (opx) */
1539 rflags
= regflag(opx
);
1540 rfield
= nasm_regvals
[opx
->basereg
];
1546 /* EVEX.b1 : evex_brerop contains the operand position */
1547 op_er_sae
= (ins
->evex_brerop
>= 0 ?
1548 &ins
->oprs
[ins
->evex_brerop
] : NULL
);
1550 if (op_er_sae
&& (op_er_sae
->decoflags
& (ER
| SAE
))) {
1552 ins
->evex_p
[2] |= EVEX_P2B
;
1553 if (op_er_sae
->decoflags
& ER
) {
1554 /* set EVEX.RC (rounding control) */
1555 ins
->evex_p
[2] |= ((ins
->evex_rm
- BRC_RN
) << 5)
1559 /* set EVEX.L'L (vector length) */
1560 ins
->evex_p
[2] |= ((ins
->vex_wlp
<< (5 - 2)) & EVEX_P2LL
);
1561 ins
->evex_p
[1] |= ((ins
->vex_wlp
<< (7 - 4)) & EVEX_P1W
);
1562 if (opy
->decoflags
& BRDCAST_MASK
) {
1564 ins
->evex_p
[2] |= EVEX_P2B
;
1568 if (itemp_has(temp
, IF_MIB
)) {
1569 opy
->eaflags
|= EAF_MIB
;
1571 * if a separate form of MIB (ICC style) is used,
1572 * the index reg info is merged into mem operand
1574 if (mib_index
!= R_none
) {
1575 opy
->indexreg
= mib_index
;
1577 opy
->hintbase
= mib_index
;
1578 opy
->hinttype
= EAH_NOTBASE
;
1582 if (process_ea(opy
, &ea_data
, bits
,
1583 rfield
, rflags
, ins
, &errmsg
) != eat
) {
1584 nasm_nonfatal("%s", errmsg
);
1587 ins
->rex
|= ea_data
.rex
;
1588 length
+= ea_data
.size
;
1594 nasm_panic("internal instruction table corrupt"
1595 ": instruction code \\%o (0x%02X) given", c
, c
);
1600 ins
->rex
&= rex_mask
;
1602 if (ins
->rex
& REX_NH
) {
1603 if (ins
->rex
& REX_H
) {
1604 nasm_nonfatal("instruction cannot use high registers");
1607 ins
->rex
&= ~REX_P
; /* Don't force REX prefix due to high reg */
1610 switch (ins
->prefixes
[PPS_VEX
]) {
1612 if (!(ins
->rex
& REX_EV
))
1617 if (!(ins
->rex
& REX_V
))
1624 if (ins
->rex
& (REX_V
| REX_EV
)) {
1625 int bad32
= REX_R
|REX_W
|REX_X
|REX_B
;
1627 if (ins
->rex
& REX_H
) {
1628 nasm_nonfatal("cannot use high register in AVX instruction");
1631 switch (ins
->vex_wlp
& 060) {
1645 if (bits
!= 64 && ((ins
->rex
& bad32
) || ins
->vexreg
> 7)) {
1646 nasm_nonfatal("invalid operands in non-64-bit mode");
1648 } else if (!(ins
->rex
& REX_EV
) &&
1649 ((ins
->vexreg
> 15) || (ins
->evex_p
[0] & 0xf0))) {
1650 nasm_nonfatal("invalid high-16 register in non-AVX-512");
1653 if (ins
->rex
& REX_EV
)
1655 else if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
)) ||
1656 ins
->prefixes
[PPS_VEX
] == P_VEX3
)
1660 } else if (ins
->rex
& REX_MASK
) {
1661 if (ins
->rex
& REX_H
) {
1662 nasm_nonfatal("cannot use high register in rex instruction");
1664 } else if (bits
== 64) {
1666 } else if ((ins
->rex
& REX_L
) &&
1667 !(ins
->rex
& (REX_P
|REX_W
|REX_X
|REX_B
)) &&
1668 iflag_cpu_level_ok(&cpu
, IF_X86_64
)) {
1670 assert_no_prefix(ins
, PPS_LOCK
);
1671 lockcheck
= false; /* Already errored, no need for warning */
1674 nasm_nonfatal("invalid operands in non-64-bit mode");
1679 if (has_prefix(ins
, PPS_LOCK
, P_LOCK
) && lockcheck
&&
1680 (!itemp_has(temp
,IF_LOCK
) || !is_class(MEMORY
, ins
->oprs
[0].type
))) {
1682 *!lock [on] LOCK prefix on unlockable instructions
1683 *! warns about \c{LOCK} prefixes on unlockable instructions.
1685 nasm_warn(WARN_LOCK
| ERR_PASS2
, "instruction is not lockable");
1688 bad_hle_warn(ins
, hleok
);
1691 * when BND prefix is set by DEFAULT directive,
1692 * BND prefix is added to every appropriate instruction line
1693 * unless it is overridden by NOBND prefix.
1696 (itemp_has(temp
, IF_BND
) && !has_prefix(ins
, PPS_REP
, P_NOBND
)))
1697 ins
->prefixes
[PPS_REP
] = P_BND
;
1700 * Add length of legacy prefixes
1702 length
+= emit_prefix(NULL
, bits
, ins
);
1707 static inline void emit_rex(struct out_data
*data
, insn
*ins
)
1709 if (data
->bits
== 64) {
1710 if ((ins
->rex
& REX_MASK
) &&
1711 !(ins
->rex
& (REX_V
| REX_EV
)) &&
1713 uint8_t rex
= (ins
->rex
& REX_MASK
) | REX_P
;
1714 out_rawbyte(data
, rex
);
1715 ins
->rex_done
= true;
1720 static int emit_prefix(struct out_data
*data
, const int bits
, insn
*ins
)
1725 for (j
= 0; j
< MAXPREFIX
; j
++) {
1727 switch (ins
->prefixes
[j
]) {
1748 nasm_warn(WARN_OTHER
|ERR_PASS2
, "cs segment base generated, "
1749 "but will be ignored in 64-bit mode");
1754 nasm_warn(WARN_OTHER
|ERR_PASS2
, "ds segment base generated, "
1755 "but will be ignored in 64-bit mode");
1760 nasm_warn(WARN_OTHER
|ERR_PASS2
, "es segment base generated, "
1761 "but will be ignored in 64-bit mode");
1772 nasm_warn(WARN_OTHER
|ERR_PASS2
, "ss segment base generated, "
1773 "but will be ignored in 64-bit mode");
1779 nasm_nonfatal("segr6 and segr7 cannot be used as prefixes");
1783 nasm_nonfatal("16-bit addressing is not supported "
1785 } else if (bits
!= 16)
1794 nasm_nonfatal("64-bit addressing is only supported "
1822 nasm_panic("invalid instruction prefix");
1826 out_rawbyte(data
, c
);
1833 static void gencode(struct out_data
*data
, insn
*ins
)
1839 struct operand
*opx
;
1840 const uint8_t *codes
= data
->itemp
->code
;
1842 enum ea_type eat
= EA_SCALAR
;
1844 const int bits
= data
->bits
;
1847 ins
->rex_done
= false;
1849 emit_prefix(data
, bits
, ins
);
1853 op1
= (c
& 3) + ((opex
& 1) << 2);
1854 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
1855 opx
= &ins
->oprs
[op1
];
1856 opex
= 0; /* For the next iteration */
1864 emit_rex(data
, ins
);
1865 out_rawdata(data
, codes
, c
);
1876 emit_rex(data
, ins
);
1877 out_rawbyte(data
, *codes
++ + (regval(opx
) & 7));
1884 out_imm(data
, opx
, 1, OUT_WRAP
);
1888 out_imm(data
, opx
, 1, OUT_UNSIGNED
);
1892 out_imm(data
, opx
, 2, OUT_WRAP
);
1896 if (opx
->type
& (BITS16
| BITS32
))
1897 size
= (opx
->type
& BITS16
) ? 2 : 4;
1899 size
= (bits
== 16) ? 2 : 4;
1900 out_imm(data
, opx
, size
, OUT_WRAP
);
1904 out_imm(data
, opx
, 4, OUT_WRAP
);
1908 size
= ins
->addr_size
>> 3;
1909 out_imm(data
, opx
, size
, OUT_WRAP
);
1913 if (opx
->segment
== data
->segment
) {
1914 int64_t delta
= opx
->offset
- data
->offset
1915 - (data
->inslen
- data
->insoffs
);
1916 if (delta
> 127 || delta
< -128)
1917 nasm_nonfatal("short jump is out of range");
1919 out_reladdr(data
, opx
, 1);
1923 out_imm(data
, opx
, 8, OUT_WRAP
);
1927 out_reladdr(data
, opx
, 2);
1931 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1932 size
= (opx
->type
& BITS16
) ? 2 : 4;
1934 size
= (bits
== 16) ? 2 : 4;
1936 out_reladdr(data
, opx
, size
);
1940 out_reladdr(data
, opx
, 4);
1944 if (opx
->segment
== NO_SEG
)
1945 nasm_nonfatal("value referenced by FAR is not relocatable");
1946 out_segment(data
, opx
);
1951 int mask
= ins
->prefixes
[PPS_VEX
] == P_EVEX
? 7 : 15;
1952 const struct operand
*opy
;
1955 opx
= &ins
->oprs
[c
>> 3];
1956 opy
= &ins
->oprs
[c
& 7];
1957 if (!absolute_op(opy
))
1958 nasm_nonfatal("non-absolute expression not permitted "
1959 "as argument %d", c
& 7);
1960 else if (opy
->offset
& ~mask
)
1961 nasm_warn(ERR_PASS2
| WARN_NUMBER_OVERFLOW
,
1962 "is4 argument exceeds bounds");
1963 c
= opy
->offset
& mask
;
1969 opx
= &ins
->oprs
[c
>> 4];
1976 r
= nasm_regvals
[opx
->basereg
];
1977 out_rawbyte(data
, (r
<< 4) | ((r
& 0x10) >> 1) | c
);
1981 if (absolute_op(opx
) &&
1982 (int32_t)opx
->offset
!= (int64_t)opx
->offset
) {
1983 nasm_warn(ERR_PASS2
| WARN_NUMBER_OVERFLOW
,
1984 "signed dword immediate exceeds bounds");
1986 out_imm(data
, opx
, 4, OUT_SIGNED
);
1992 ins
->evex_p
[2] |= op_evexflags(&ins
->oprs
[0],
1993 EVEX_P2Z
| EVEX_P2AAA
, 2);
1994 ins
->evex_p
[2] ^= EVEX_P2VP
; /* 1's complement */
1996 /* EVEX.X can be set by either REX or EVEX for different reasons */
1997 bytes
[1] = ((((ins
->rex
& 7) << 5) |
1998 (ins
->evex_p
[0] & (EVEX_P0X
| EVEX_P0RP
))) ^ 0xf0) |
1999 (ins
->vex_cm
& EVEX_P0MM
);
2000 bytes
[2] = ((ins
->rex
& REX_W
) << (7 - 3)) |
2001 ((~ins
->vexreg
& 15) << 3) |
2002 (1 << 2) | (ins
->vex_wlp
& 3);
2003 bytes
[3] = ins
->evex_p
[2];
2004 out_rawdata(data
, bytes
, 4);
2010 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
)) ||
2011 ins
->prefixes
[PPS_VEX
] == P_VEX3
) {
2012 bytes
[0] = (ins
->vex_cm
>> 6) ? 0x8f : 0xc4;
2013 bytes
[1] = (ins
->vex_cm
& 31) | ((~ins
->rex
& 7) << 5);
2014 bytes
[2] = ((ins
->rex
& REX_W
) << (7-3)) |
2015 ((~ins
->vexreg
& 15)<< 3) | (ins
->vex_wlp
& 07);
2016 out_rawdata(data
, bytes
, 3);
2019 bytes
[1] = ((~ins
->rex
& REX_R
) << (7-2)) |
2020 ((~ins
->vexreg
& 15) << 3) | (ins
->vex_wlp
& 07);
2021 out_rawdata(data
, bytes
, 2);
2035 if (absolute_op(opx
)) {
2036 if (ins
->rex
& REX_W
)
2038 else if (ins
->prefixes
[PPS_OSIZE
] == P_O16
)
2040 else if (ins
->prefixes
[PPS_OSIZE
] == P_O32
)
2045 um
= (uint64_t)2 << (s
-1);
2048 if (uv
> 127 && uv
< (uint64_t)-128 &&
2049 (uv
< um
-128 || uv
> um
-1)) {
2050 /* If this wasn't explicitly byte-sized, warn as though we
2051 * had fallen through to the imm16/32/64 case.
2053 nasm_warn(ERR_PASS2
| WARN_NUMBER_OVERFLOW
,
2054 "%s value exceeds bounds",
2055 (opx
->type
& BITS8
) ? "signed byte" :
2061 /* Output as a raw byte to avoid byte overflow check */
2062 out_rawbyte(data
, (uint8_t)uv
);
2064 out_imm(data
, opx
, 1, OUT_WRAP
); /* XXX: OUT_SIGNED? */
2073 if (bits
== 32 && !has_prefix(ins
, PPS_ASIZE
, P_A16
))
2074 out_rawbyte(data
, 0x67);
2078 if (bits
!= 32 && !has_prefix(ins
, PPS_ASIZE
, P_A32
))
2079 out_rawbyte(data
, 0x67);
2110 out_rawbyte(data
, *codes
++ ^ get_cond_opcode(ins
->condition
));
2118 out_rawbyte(data
, c
- 0332 + 0xF2);
2122 if (ins
->rex
& REX_R
)
2123 out_rawbyte(data
, 0xF0);
2124 ins
->rex
&= ~(REX_L
|REX_R
);
2135 if (ins
->oprs
[0].segment
!= NO_SEG
)
2136 nasm_panic("non-constant BSS size in pass two");
2138 out_reserve(data
, ins
->oprs
[0].offset
* resb_bytes(ins
->opcode
));
2148 out_rawbyte(data
, 0x66);
2157 out_rawbyte(data
, c
- 0366 + 0x66);
2164 out_rawbyte(data
, bits
== 16 ? 3 : 5);
2196 struct operand
*opy
= &ins
->oprs
[op2
];
2199 /* pick rfield from operand b (opx) */
2200 rflags
= regflag(opx
);
2201 rfield
= nasm_regvals
[opx
->basereg
];
2203 /* rfield is constant */
2208 if (process_ea(opy
, &ea_data
, bits
,
2209 rfield
, rflags
, ins
, &errmsg
) != eat
)
2210 nasm_nonfatal("%s", errmsg
);
2213 *p
++ = ea_data
.modrm
;
2214 if (ea_data
.sib_present
)
2216 out_rawdata(data
, bytes
, p
- bytes
);
2219 * Make sure the address gets the right offset in case
2220 * the line breaks in the .lst file (BR 1197827)
2223 if (ea_data
.bytes
) {
2224 /* use compressed displacement, if available */
2225 if (ea_data
.disp8
) {
2226 out_rawbyte(data
, ea_data
.disp8
);
2227 } else if (ea_data
.rip
) {
2228 out_reladdr(data
, opy
, ea_data
.bytes
);
2230 int asize
= ins
->addr_size
>> 3;
2232 if (overflow_general(opy
->offset
, asize
) ||
2233 signed_bits(opy
->offset
, ins
->addr_size
) !=
2234 signed_bits(opy
->offset
, ea_data
.bytes
<< 3))
2235 warn_overflow(ea_data
.bytes
);
2237 out_imm(data
, opy
, ea_data
.bytes
,
2238 (asize
> ea_data
.bytes
)
2239 ? OUT_SIGNED
: OUT_WRAP
);
2246 nasm_panic("internal instruction table corrupt"
2247 ": instruction code \\%o (0x%02X) given", c
, c
);
2253 static opflags_t
regflag(const operand
* o
)
2255 if (!is_register(o
->basereg
))
2256 nasm_panic("invalid operand passed to regflag()");
2257 return nasm_reg_flags
[o
->basereg
];
2260 static int32_t regval(const operand
* o
)
2262 if (!is_register(o
->basereg
))
2263 nasm_panic("invalid operand passed to regval()");
2264 return nasm_regvals
[o
->basereg
];
2267 static int op_rexflags(const operand
* o
, int mask
)
2272 if (!is_register(o
->basereg
))
2273 nasm_panic("invalid operand passed to op_rexflags()");
2275 flags
= nasm_reg_flags
[o
->basereg
];
2276 val
= nasm_regvals
[o
->basereg
];
2278 return rexflags(val
, flags
, mask
);
2281 static int rexflags(int val
, opflags_t flags
, int mask
)
2285 if (val
>= 0 && (val
& 8))
2286 rex
|= REX_B
|REX_X
|REX_R
;
2289 if (!(REG_HIGH
& ~flags
)) /* AH, CH, DH, BH */
2291 else if (!(REG8
& ~flags
) && val
>= 4) /* SPL, BPL, SIL, DIL */
2297 static int evexflags(int val
, decoflags_t deco
,
2298 int mask
, uint8_t byte
)
2304 if (val
>= 0 && (val
& 16))
2305 evex
|= (EVEX_P0RP
| EVEX_P0X
);
2308 if (val
>= 0 && (val
& 16))
2312 if (deco
& OPMASK_MASK
)
2313 evex
|= deco
& EVEX_P2AAA
;
2319 static int op_evexflags(const operand
* o
, int mask
, uint8_t byte
)
2323 val
= nasm_regvals
[o
->basereg
];
2325 return evexflags(val
, o
->decoflags
, mask
, byte
);
2328 static enum match_result
find_match(const struct itemplate
**tempp
,
2330 int32_t segment
, int64_t offset
, int bits
)
2332 const struct itemplate
*temp
;
2333 enum match_result m
, merr
;
2334 opflags_t xsizeflags
[MAX_OPERANDS
];
2335 bool opsizemissing
= false;
2336 int8_t broadcast
= instruction
->evex_brerop
;
2339 /* broadcasting uses a different data element size */
2340 for (i
= 0; i
< instruction
->operands
; i
++)
2342 xsizeflags
[i
] = instruction
->oprs
[i
].decoflags
& BRSIZE_MASK
;
2344 xsizeflags
[i
] = instruction
->oprs
[i
].type
& SIZE_MASK
;
2346 merr
= MERR_INVALOP
;
2348 for (temp
= nasm_instructions
[instruction
->opcode
];
2349 temp
->opcode
!= I_none
; temp
++) {
2350 m
= matches(temp
, instruction
, bits
);
2351 if (m
== MOK_JUMP
) {
2352 if (jmp_match(segment
, offset
, bits
, instruction
, temp
))
2356 } else if (m
== MERR_OPSIZEMISSING
&& !itemp_has(temp
, IF_SX
)) {
2358 * Missing operand size and a candidate for fuzzy matching...
2360 for (i
= 0; i
< temp
->operands
; i
++)
2362 xsizeflags
[i
] |= temp
->deco
[i
] & BRSIZE_MASK
;
2364 xsizeflags
[i
] |= temp
->opd
[i
] & SIZE_MASK
;
2365 opsizemissing
= true;
2369 if (merr
== MOK_GOOD
)
2373 /* No match, but see if we can get a fuzzy operand size match... */
2377 for (i
= 0; i
< instruction
->operands
; i
++) {
2379 * We ignore extrinsic operand sizes on registers, so we should
2380 * never try to fuzzy-match on them. This also resolves the case
2381 * when we have e.g. "xmmrm128" in two different positions.
2383 if (is_class(REGISTER
, instruction
->oprs
[i
].type
))
2386 /* This tests if xsizeflags[i] has more than one bit set */
2387 if ((xsizeflags
[i
] & (xsizeflags
[i
]-1)))
2388 goto done
; /* No luck */
2390 if (i
== broadcast
) {
2391 instruction
->oprs
[i
].decoflags
|= xsizeflags
[i
];
2392 instruction
->oprs
[i
].type
|= (xsizeflags
[i
] == BR_BITS32
?
2395 instruction
->oprs
[i
].type
|= xsizeflags
[i
]; /* Set the size */
2399 /* Try matching again... */
2400 for (temp
= nasm_instructions
[instruction
->opcode
];
2401 temp
->opcode
!= I_none
; temp
++) {
2402 m
= matches(temp
, instruction
, bits
);
2403 if (m
== MOK_JUMP
) {
2404 if (jmp_match(segment
, offset
, bits
, instruction
, temp
))
2411 if (merr
== MOK_GOOD
)
2420 static uint8_t get_broadcast_num(opflags_t opflags
, opflags_t brsize
)
2422 unsigned int opsize
= (opflags
& SIZE_MASK
) >> SIZE_SHIFT
;
2425 if (brsize
> BITS64
)
2426 nasm_fatal("size of broadcasting element is greater than 64 bits");
2429 * The shift term is to take care of the extra BITS80 inserted
2430 * between BITS64 and BITS128.
2432 brcast_num
= ((opsize
/ (BITS64
>> SIZE_SHIFT
)) * (BITS64
/ brsize
))
2433 >> (opsize
> (BITS64
>> SIZE_SHIFT
));
2438 static enum match_result
matches(const struct itemplate
*itemp
,
2439 insn
*instruction
, int bits
)
2441 opflags_t size
[MAX_OPERANDS
], asize
;
2442 bool opsizemissing
= false;
2448 if (itemp
->opcode
!= instruction
->opcode
)
2449 return MERR_INVALOP
;
2452 * Count the operands
2454 if (itemp
->operands
!= instruction
->operands
)
2455 return MERR_INVALOP
;
2460 if (!(optimizing
.level
> 0) && itemp_has(itemp
, IF_OPT
))
2461 return MERR_INVALOP
;
2466 switch (instruction
->prefixes
[PPS_VEX
]) {
2468 if (!itemp_has(itemp
, IF_EVEX
))
2469 return MERR_ENCMISMATCH
;
2473 if (!itemp_has(itemp
, IF_VEX
))
2474 return MERR_ENCMISMATCH
;
2481 * Check that no spurious colons or TOs are present
2483 for (i
= 0; i
< itemp
->operands
; i
++)
2484 if (instruction
->oprs
[i
].type
& ~itemp
->opd
[i
] & (COLON
| TO
))
2485 return MERR_INVALOP
;
2488 * Process size flags
2490 switch (itemp_smask(itemp
)) {
2491 case IF_GENBIT(IF_SB
):
2494 case IF_GENBIT(IF_SW
):
2497 case IF_GENBIT(IF_SD
):
2500 case IF_GENBIT(IF_SQ
):
2503 case IF_GENBIT(IF_SO
):
2506 case IF_GENBIT(IF_SY
):
2509 case IF_GENBIT(IF_SZ
):
2512 case IF_GENBIT(IF_ANYSIZE
):
2515 case IF_GENBIT(IF_SIZE
):
2536 if (itemp_armask(itemp
)) {
2537 /* S- flags only apply to a specific operand */
2538 i
= itemp_arg(itemp
);
2539 memset(size
, 0, sizeof size
);
2542 /* S- flags apply to all operands */
2543 for (i
= 0; i
< MAX_OPERANDS
; i
++)
2548 * Check that the operand flags all match up,
2549 * it's a bit tricky so lets be verbose:
2551 * 1) Find out the size of operand. If instruction
2552 * doesn't have one specified -- we're trying to
2553 * guess it either from template (IF_S* flag) or
2556 * 2) If template operand do not match the instruction OR
2557 * template has an operand size specified AND this size differ
2558 * from which instruction has (perhaps we got it from code bits)
2560 * a) Check that only size of instruction and operand is differ
2561 * other characteristics do match
2562 * b) Perhaps it's a register specified in instruction so
2563 * for such a case we just mark that operand as "size
2564 * missing" and this will turn on fuzzy operand size
2565 * logic facility (handled by a caller)
2567 for (i
= 0; i
< itemp
->operands
; i
++) {
2568 opflags_t type
= instruction
->oprs
[i
].type
;
2569 decoflags_t deco
= instruction
->oprs
[i
].decoflags
;
2570 decoflags_t ideco
= itemp
->deco
[i
];
2571 bool is_broadcast
= deco
& BRDCAST_MASK
;
2572 uint8_t brcast_num
= 0;
2573 opflags_t template_opsize
, insn_opsize
;
2575 if (!(type
& SIZE_MASK
))
2578 insn_opsize
= type
& SIZE_MASK
;
2579 if (!is_broadcast
) {
2580 template_opsize
= itemp
->opd
[i
] & SIZE_MASK
;
2582 decoflags_t deco_brsize
= ideco
& BRSIZE_MASK
;
2584 if (~ideco
& BRDCAST_MASK
)
2585 return MERR_BRNOTHERE
;
2588 * when broadcasting, the element size depends on
2589 * the instruction type. decorator flag should match.
2592 template_opsize
= (deco_brsize
== BR_BITS32
? BITS32
: BITS64
);
2593 /* calculate the proper number : {1to<brcast_num>} */
2594 brcast_num
= get_broadcast_num(itemp
->opd
[i
], template_opsize
);
2596 template_opsize
= 0;
2600 if (~ideco
& deco
& OPMASK_MASK
)
2601 return MERR_MASKNOTHERE
;
2603 if (~ideco
& deco
& (Z_MASK
|STATICRND_MASK
|SAE_MASK
))
2604 return MERR_DECONOTHERE
;
2606 if (itemp
->opd
[i
] & ~type
& ~(SIZE_MASK
|REGSET_MASK
))
2607 return MERR_INVALOP
;
2609 if (~itemp
->opd
[i
] & type
& REGSET_MASK
)
2610 return (itemp
->opd
[i
] & REGSET_MASK
)
2611 ? MERR_REGSETSIZE
: MERR_REGSET
;
2613 if (template_opsize
) {
2614 if (template_opsize
!= insn_opsize
) {
2616 return MERR_INVALOP
;
2617 } else if (!is_class(REGISTER
, type
)) {
2619 * Note: we don't honor extrinsic operand sizes for registers,
2620 * so "missing operand size" for a register should be
2621 * considered a wildcard match rather than an error.
2623 opsizemissing
= true;
2625 } else if (is_broadcast
&&
2627 (2U << ((deco
& BRNUM_MASK
) >> BRNUM_SHIFT
)))) {
2629 * broadcasting opsize matches but the number of repeated memory
2630 * element does not match.
2631 * if 64b double precision float is broadcasted to ymm (256b),
2632 * broadcasting decorator must be {1to4}.
2634 return MERR_BRNUMMISMATCH
;
2640 return MERR_OPSIZEMISSING
;
2643 * Check operand sizes
2645 if (itemp_has(itemp
, IF_SM
) || itemp_has(itemp
, IF_SM2
)) {
2646 oprs
= (itemp_has(itemp
, IF_SM2
) ? 2 : itemp
->operands
);
2647 for (i
= 0; i
< oprs
; i
++) {
2648 asize
= itemp
->opd
[i
] & SIZE_MASK
;
2650 for (i
= 0; i
< oprs
; i
++)
2656 oprs
= itemp
->operands
;
2659 for (i
= 0; i
< itemp
->operands
; i
++) {
2660 if (!(itemp
->opd
[i
] & SIZE_MASK
) &&
2661 (instruction
->oprs
[i
].type
& SIZE_MASK
& ~size
[i
]))
2662 return MERR_OPSIZEMISMATCH
;
2666 * Check template is okay at the set cpu level
2668 if (iflag_cmp_cpu_level(&insns_flags
[itemp
->iflag_idx
], &cpu
) > 0)
2672 * Verify the appropriate long mode flag.
2674 if (itemp_has(itemp
, (bits
== 64 ? IF_NOLONG
: IF_LONG
)))
2675 return MERR_BADMODE
;
2678 * If we have a HLE prefix, look for the NOHLE flag
2680 if (itemp_has(itemp
, IF_NOHLE
) &&
2681 (has_prefix(instruction
, PPS_REP
, P_XACQUIRE
) ||
2682 has_prefix(instruction
, PPS_REP
, P_XRELEASE
)))
2686 * Check if special handling needed for Jumps
2688 if ((itemp
->code
[0] & ~1) == 0370)
2692 * Check if BND prefix is allowed.
2693 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
2695 if (!itemp_has(itemp
, IF_BND
) &&
2696 (has_prefix(instruction
, PPS_REP
, P_BND
) ||
2697 has_prefix(instruction
, PPS_REP
, P_NOBND
)))
2699 else if (itemp_has(itemp
, IF_BND
) &&
2700 (has_prefix(instruction
, PPS_REP
, P_REPNE
) ||
2701 has_prefix(instruction
, PPS_REP
, P_REPNZ
)))
2702 return MERR_BADREPNE
;
2708 * Check if ModR/M.mod should/can be 01.
2709 * - EAF_BYTEOFFS is set
2710 * - offset can fit in a byte when EVEX is not used
2711 * - offset can be compressed when EVEX is used
2713 #define IS_MOD_01() (!(input->eaflags & EAF_WORDOFFS) && \
2714 (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
2715 is_disp8n(input, ins, &output->disp8) : \
2716 input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
2717 o <= 127 && seg == NO_SEG && !forw_ref)))
2719 static enum ea_type
process_ea(operand
*input
, ea
*output
, int bits
,
2720 int rfield
, opflags_t rflags
, insn
*ins
,
2721 const char **errmsg
)
2723 bool forw_ref
= !!(input
->opflags
& OPFLAG_UNKNOWN
);
2724 int addrbits
= ins
->addr_size
;
2725 int eaflags
= input
->eaflags
;
2727 *errmsg
= "invalid effective address"; /* Default error message */
2729 output
->type
= EA_SCALAR
;
2730 output
->rip
= false;
2733 /* REX flags for the rfield operand */
2734 output
->rex
|= rexflags(rfield
, rflags
, REX_R
| REX_P
| REX_W
| REX_H
);
2735 /* EVEX.R' flag for the REG operand */
2736 ins
->evex_p
[0] |= evexflags(rfield
, 0, EVEX_P0RP
, 0);
2738 if (is_class(REGISTER
, input
->type
)) {
2740 * It's a direct register.
2742 if (!is_register(input
->basereg
))
2745 if (!is_reg_class(REG_EA
, input
->basereg
))
2748 /* broadcasting is not available with a direct register operand. */
2749 if (input
->decoflags
& BRDCAST_MASK
) {
2750 *errmsg
= "broadcast not allowed with register operand";
2754 output
->rex
|= op_rexflags(input
, REX_B
| REX_P
| REX_W
| REX_H
);
2755 ins
->evex_p
[0] |= op_evexflags(input
, EVEX_P0X
, 0);
2756 output
->sib_present
= false; /* no SIB necessary */
2757 output
->bytes
= 0; /* no offset necessary either */
2758 output
->modrm
= GEN_MODRM(3, rfield
, nasm_regvals
[input
->basereg
]);
2761 * It's a memory reference.
2764 /* Embedded rounding or SAE is not available with a mem ref operand. */
2765 if (input
->decoflags
& (ER
| SAE
)) {
2766 *errmsg
= "embedded rounding is available only with "
2767 "register-register operations";
2771 if (input
->basereg
== -1 &&
2772 (input
->indexreg
== -1 || input
->scale
== 0)) {
2774 * It's a pure offset.
2776 if (bits
== 64 && ((input
->type
& IP_REL
) == IP_REL
)) {
2777 if (input
->segment
== NO_SEG
||
2778 (input
->opflags
& OPFLAG_RELATIVE
)) {
2779 nasm_warn(WARN_OTHER
|ERR_PASS2
, "absolute address can not be RIP-relative");
2780 input
->type
&= ~IP_REL
;
2781 input
->type
|= MEMORY
;
2786 !(IP_REL
& ~input
->type
) && (eaflags
& EAF_MIB
)) {
2787 *errmsg
= "RIP-relative addressing is prohibited for MIB";
2791 if (eaflags
& EAF_BYTEOFFS
||
2792 (eaflags
& EAF_WORDOFFS
&&
2793 input
->disp_size
!= (addrbits
!= 16 ? 32 : 16)))
2794 nasm_warn(WARN_OTHER
, "displacement size ignored on absolute address");
2796 if (bits
== 64 && (~input
->type
& IP_REL
)) {
2797 output
->sib_present
= true;
2798 output
->sib
= GEN_SIB(0, 4, 5);
2800 output
->modrm
= GEN_MODRM(0, rfield
, 4);
2801 output
->rip
= false;
2803 output
->sib_present
= false;
2804 output
->bytes
= (addrbits
!= 16 ? 4 : 2);
2805 output
->modrm
= GEN_MODRM(0, rfield
,
2806 (addrbits
!= 16 ? 5 : 6));
2807 output
->rip
= bits
== 64;
2811 * It's an indirection.
2813 int i
= input
->indexreg
, b
= input
->basereg
, s
= input
->scale
;
2814 int32_t seg
= input
->segment
;
2815 int hb
= input
->hintbase
, ht
= input
->hinttype
;
2816 int t
, it
, bt
; /* register numbers */
2817 opflags_t x
, ix
, bx
; /* register flags */
2820 i
= -1; /* make this easy, at least */
2822 if (is_register(i
)) {
2823 it
= nasm_regvals
[i
];
2824 ix
= nasm_reg_flags
[i
];
2830 if (is_register(b
)) {
2831 bt
= nasm_regvals
[b
];
2832 bx
= nasm_reg_flags
[b
];
2838 /* if either one are a vector register... */
2839 if ((ix
|bx
) & (XMMREG
|YMMREG
|ZMMREG
) & ~REG_EA
) {
2840 opflags_t sok
= BITS32
| BITS64
;
2841 int32_t o
= input
->offset
;
2842 int mod
, scale
, index
, base
;
2845 * For a vector SIB, one has to be a vector and the other,
2846 * if present, a GPR. The vector must be the index operand.
2848 if (it
== -1 || (bx
& (XMMREG
|YMMREG
|ZMMREG
) & ~REG_EA
)) {
2854 t
= bt
, bt
= it
, it
= t
;
2855 x
= bx
, bx
= ix
, ix
= x
;
2861 if (!(REG64
& ~bx
) || !(REG32
& ~bx
))
2868 * While we're here, ensure the user didn't specify
2871 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2874 if (addrbits
== 16 ||
2875 (addrbits
== 32 && !(sok
& BITS32
)) ||
2876 (addrbits
== 64 && !(sok
& BITS64
)))
2879 output
->type
= ((ix
& ZMMREG
& ~REG_EA
) ? EA_ZMMVSIB
2880 : ((ix
& YMMREG
& ~REG_EA
)
2881 ? EA_YMMVSIB
: EA_XMMVSIB
));
2883 output
->rex
|= rexflags(it
, ix
, REX_X
);
2884 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2885 ins
->evex_p
[2] |= evexflags(it
, 0, EVEX_P2VP
, 2);
2887 index
= it
& 7; /* it is known to be != -1 */
2902 default: /* then what the smeg is it? */
2903 goto err
; /* panic */
2911 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2912 seg
== NO_SEG
&& !forw_ref
&&
2913 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2915 else if (IS_MOD_01())
2921 output
->sib_present
= true;
2922 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2923 output
->modrm
= GEN_MODRM(mod
, rfield
, 4);
2924 output
->sib
= GEN_SIB(scale
, index
, base
);
2925 } else if ((ix
|bx
) & (BITS32
|BITS64
)) {
2927 * it must be a 32/64-bit memory reference. Firstly we have
2928 * to check that all registers involved are type E/Rxx.
2930 opflags_t sok
= BITS32
| BITS64
;
2931 int32_t o
= input
->offset
;
2934 if (!(REG64
& ~ix
) || !(REG32
& ~ix
))
2942 goto err
; /* Invalid register */
2943 if (~sok
& bx
& SIZE_MASK
)
2944 goto err
; /* Invalid size */
2949 * While we're here, ensure the user didn't specify
2952 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2955 if (addrbits
== 16 ||
2956 (addrbits
== 32 && !(sok
& BITS32
)) ||
2957 (addrbits
== 64 && !(sok
& BITS64
)))
2960 /* now reorganize base/index */
2961 if (s
== 1 && bt
!= it
&& bt
!= -1 && it
!= -1 &&
2962 ((hb
== b
&& ht
== EAH_NOTBASE
) ||
2963 (hb
== i
&& ht
== EAH_MAKEBASE
))) {
2964 /* swap if hints say so */
2965 t
= bt
, bt
= it
, it
= t
;
2966 x
= bx
, bx
= ix
, ix
= x
;
2969 if (bt
== -1 && s
== 1 && !(hb
== i
&& ht
== EAH_NOTBASE
)) {
2970 /* make single reg base, unless hint */
2971 bt
= it
, bx
= ix
, it
= -1, ix
= 0;
2973 if (eaflags
& EAF_MIB
) {
2974 /* only for mib operands */
2975 if (it
== -1 && (hb
== b
&& ht
== EAH_NOTBASE
)) {
2977 * make a single reg index [reg*1].
2978 * gas uses this form for an explicit index register.
2980 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2982 if ((ht
== EAH_SUMMED
) && bt
== -1) {
2983 /* separate once summed index into [base, index] */
2984 bt
= it
, bx
= ix
, s
--;
2987 if (((s
== 2 && it
!= REG_NUM_ESP
&&
2988 (!(eaflags
& EAF_TIMESTWO
) || (ht
== EAH_SUMMED
))) ||
2989 s
== 3 || s
== 5 || s
== 9) && bt
== -1) {
2990 /* convert 3*EAX to EAX+2*EAX */
2991 bt
= it
, bx
= ix
, s
--;
2993 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
&&
2994 (eaflags
& EAF_TIMESTWO
) &&
2995 (hb
== b
&& ht
== EAH_NOTBASE
)) {
2997 * convert [NOSPLIT EAX*1]
2998 * to sib format with 0x0 displacement - [EAX*1+0].
3000 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
3003 if (s
== 1 && it
== REG_NUM_ESP
) {
3004 /* swap ESP into base if scale is 1 */
3005 t
= it
, it
= bt
, bt
= t
;
3006 x
= ix
, ix
= bx
, bx
= x
;
3008 if (it
== REG_NUM_ESP
||
3009 (s
!= 1 && s
!= 2 && s
!= 4 && s
!= 8 && it
!= -1))
3010 goto err
; /* wrong, for various reasons */
3012 output
->rex
|= rexflags(it
, ix
, REX_X
);
3013 output
->rex
|= rexflags(bt
, bx
, REX_B
);
3015 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
) {
3024 if (rm
!= REG_NUM_EBP
&& o
== 0 &&
3025 seg
== NO_SEG
&& !forw_ref
&&
3026 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
3028 else if (IS_MOD_01())
3034 output
->sib_present
= false;
3035 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
3036 output
->modrm
= GEN_MODRM(mod
, rfield
, rm
);
3039 int mod
, scale
, index
, base
;
3059 default: /* then what the smeg is it? */
3060 goto err
; /* panic */
3068 if (base
!= REG_NUM_EBP
&& o
== 0 &&
3069 seg
== NO_SEG
&& !forw_ref
&&
3070 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
3072 else if (IS_MOD_01())
3078 output
->sib_present
= true;
3079 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
3080 output
->modrm
= GEN_MODRM(mod
, rfield
, 4);
3081 output
->sib
= GEN_SIB(scale
, index
, base
);
3083 } else { /* it's 16-bit */
3085 int16_t o
= input
->offset
;
3087 /* check for 64-bit long mode */
3091 /* check all registers are BX, BP, SI or DI */
3092 if ((b
!= -1 && b
!= R_BP
&& b
!= R_BX
&& b
!= R_SI
&& b
!= R_DI
) ||
3093 (i
!= -1 && i
!= R_BP
&& i
!= R_BX
&& i
!= R_SI
&& i
!= R_DI
))
3096 /* ensure the user didn't specify DWORD/QWORD */
3097 if (input
->disp_size
== 32 || input
->disp_size
== 64)
3100 if (s
!= 1 && i
!= -1)
3101 goto err
; /* no can do, in 16-bit EA */
3102 if (b
== -1 && i
!= -1) {
3107 if ((b
== R_SI
|| b
== R_DI
) && i
!= -1) {
3112 /* have BX/BP as base, SI/DI index */
3114 goto err
; /* shouldn't ever happen, in theory */
3115 if (i
!= -1 && b
!= -1 &&
3116 (i
== R_BP
|| i
== R_BX
|| b
== R_SI
|| b
== R_DI
))
3117 goto err
; /* invalid combinations */
3118 if (b
== -1) /* pure offset: handled above */
3119 goto err
; /* so if it gets to here, panic! */
3123 switch (i
* 256 + b
) {
3124 case R_SI
* 256 + R_BX
:
3127 case R_DI
* 256 + R_BX
:
3130 case R_SI
* 256 + R_BP
:
3133 case R_DI
* 256 + R_BP
:
3151 if (rm
== -1) /* can't happen, in theory */
3152 goto err
; /* so panic if it does */
3154 if (o
== 0 && seg
== NO_SEG
&& !forw_ref
&& rm
!= 6 &&
3155 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
3157 else if (IS_MOD_01())
3162 output
->sib_present
= false; /* no SIB - it's 16-bit */
3163 output
->bytes
= mod
; /* bytes of offset needed */
3164 output
->modrm
= GEN_MODRM(mod
, rfield
, rm
);
3169 output
->size
= 1 + output
->sib_present
+ output
->bytes
;
3170 return output
->type
;
3173 return output
->type
= EA_INVALID
;
3176 static void add_asp(insn
*ins
, int addrbits
)
3181 valid
= (addrbits
== 64) ? 64|32 : 32|16;
3183 switch (ins
->prefixes
[PPS_ASIZE
]) {
3194 valid
&= (addrbits
== 32) ? 16 : 32;
3200 for (j
= 0; j
< ins
->operands
; j
++) {
3201 if (is_class(MEMORY
, ins
->oprs
[j
].type
)) {
3204 /* Verify as Register */
3205 if (!is_register(ins
->oprs
[j
].indexreg
))
3208 i
= nasm_reg_flags
[ins
->oprs
[j
].indexreg
];
3210 /* Verify as Register */
3211 if (!is_register(ins
->oprs
[j
].basereg
))
3214 b
= nasm_reg_flags
[ins
->oprs
[j
].basereg
];
3216 if (ins
->oprs
[j
].scale
== 0)
3220 int ds
= ins
->oprs
[j
].disp_size
;
3221 if ((addrbits
!= 64 && ds
> 8) ||
3222 (addrbits
== 64 && ds
== 16))
3242 if (valid
& addrbits
) {
3243 ins
->addr_size
= addrbits
;
3244 } else if (valid
& ((addrbits
== 32) ? 16 : 32)) {
3245 /* Add an address size prefix */
3246 ins
->prefixes
[PPS_ASIZE
] = (addrbits
== 32) ? P_A16
: P_A32
;;
3247 ins
->addr_size
= (addrbits
== 32) ? 16 : 32;
3250 nasm_nonfatal("impossible combination of address sizes");
3251 ins
->addr_size
= addrbits
; /* Error recovery */
3254 defdisp
= ins
->addr_size
== 16 ? 16 : 32;
3256 for (j
= 0; j
< ins
->operands
; j
++) {
3257 if (!(MEM_OFFS
& ~ins
->oprs
[j
].type
) &&
3258 (ins
->oprs
[j
].disp_size
? ins
->oprs
[j
].disp_size
: defdisp
) != ins
->addr_size
) {
3260 * mem_offs sizes must match the address size; if not,
3261 * strip the MEM_OFFS bit and match only EA instructions
3263 ins
->oprs
[j
].type
&= ~(MEM_OFFS
& ~MEMORY
);