1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * 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"
187 #include "assemble.h"
195 * Matching errors. These should be sorted so that more specific
196 * errors come later in the sequence.
212 * Matching success; the conditional ones first
214 MOK_JUMP
, /* Matching OK but needs jmp_match() */
215 MOK_GOOD
/* Matching unconditionally OK */
219 enum ea_type type
; /* what kind of EA is this? */
220 int sib_present
; /* is a SIB byte necessary? */
221 int bytes
; /* # of bytes of offset needed */
222 int size
; /* lazy - this is sib+bytes+1 */
223 uint8_t modrm
, sib
, rex
, rip
; /* the bytes themselves */
224 int8_t disp8
; /* compressed displacement for EVEX */
227 #define GEN_SIB(scale, index, base) \
228 (((scale) << 6) | ((index) << 3) | ((base)))
230 #define GEN_MODRM(mod, reg, rm) \
231 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
233 static int64_t calcsize(int32_t, int64_t, int, insn
*,
234 const struct itemplate
*);
235 static int emit_prefix(struct out_data
*data
, const int bits
, insn
*ins
);
236 static void gencode(struct out_data
*data
, insn
*ins
);
237 static enum match_result
find_match(const struct itemplate
**tempp
,
239 int32_t segment
, int64_t offset
, int bits
);
240 static enum match_result
matches(const struct itemplate
*, insn
*, int bits
);
241 static opflags_t
regflag(const operand
*);
242 static int32_t regval(const operand
*);
243 static int rexflags(int, opflags_t
, int);
244 static int op_rexflags(const operand
*, int);
245 static int op_evexflags(const operand
*, int, uint8_t);
246 static void add_asp(insn
*, int);
248 static enum ea_type
process_ea(operand
*, ea
*, int, int,
249 opflags_t
, insn
*, const char **);
251 static inline bool absolute_op(const struct operand
*o
)
253 return o
->segment
== NO_SEG
&& o
->wrt
== NO_SEG
&&
254 !(o
->opflags
& OPFLAG_RELATIVE
);
257 static int has_prefix(insn
* ins
, enum prefix_pos pos
, int prefix
)
259 return ins
->prefixes
[pos
] == prefix
;
262 static void assert_no_prefix(insn
* ins
, enum prefix_pos pos
)
264 if (ins
->prefixes
[pos
])
265 nasm_error(ERR_NONFATAL
, "invalid %s prefix",
266 prefix_name(ins
->prefixes
[pos
]));
269 static const char *size_name(int size
)
293 static void warn_overflow(int size
)
295 nasm_error(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
296 "%s data exceeds bounds", size_name(size
));
299 static void warn_overflow_const(int64_t data
, int size
)
301 if (overflow_general(data
, size
))
305 static void warn_overflow_opd(const struct operand
*o
, int size
)
307 if (absolute_op(o
)) {
308 if (overflow_general(o
->offset
, size
))
313 static void warn_overflow_out(int64_t data
, int size
, enum out_sign sign
)
319 err
= overflow_general(data
, size
);
322 err
= overflow_signed(data
, size
);
325 err
= overflow_unsigned(data
, size
);
337 * This routine wrappers the real output format's output routine,
338 * in order to pass a copy of the data off to the listing file
339 * generator at the same time, flatten unnecessary relocations,
340 * and verify backend compatibility.
342 static void out(struct out_data
*data
)
344 static int32_t lineno
= 0; /* static!!! */
345 static const char *lnfname
= NULL
;
347 const int amax
= ofmt
->maxbits
>> 3; /* Maximum address size in bytes */
352 uint64_t size
= data
->size
;
354 int32_t fixseg
; /* Segment for which to produce fixed data */
357 return; /* Nothing to do */
360 * Convert addresses to RAWDATA if possible
361 * XXX: not all backends want this for global symbols!!!!
363 switch (data
->type
) {
365 addrval
= data
->toffset
;
366 fixseg
= NO_SEG
; /* Absolute address is fixed data */
370 addrval
= data
->toffset
- data
->relbase
;
371 fixseg
= data
->segment
; /* Our own segment is fixed data */
376 nasm_assert(asize
<= 8);
377 if (data
->tsegment
== fixseg
&& data
->twrt
== NO_SEG
) {
378 uint8_t *q
= xdata
.b
;
380 warn_overflow_out(addrval
, asize
, data
->sign
);
382 WRITEADDR(q
, addrval
, asize
);
383 data
->data
= xdata
.b
;
384 data
->type
= OUT_RAWDATA
;
385 asize
= 0; /* No longer an address */
390 asize
= 0; /* Not an address */
397 * this call to src_get determines when we call the
398 * debug-format-specific "linenum" function
399 * it updates lineno and lnfname to the current values
400 * returning 0 if "same as last time", -2 if lnfname
401 * changed, and the amount by which lineno changed,
402 * if it did. thus, these variables must be static
405 if (src_get(&lineno
, &lnfname
))
406 dfmt
->linenum(lnfname
, lineno
, data
->segment
);
408 if (asize
&& asize
> amax
) {
409 if (data
->type
!= OUT_ADDRESS
|| data
->sign
== OUT_SIGNED
) {
410 nasm_error(ERR_NONFATAL
,
411 "%d-bit signed relocation unsupported by output format %s\n",
412 asize
<< 3, ofmt
->shortname
);
414 nasm_error(ERR_WARNING
| ERR_WARN_ZEXTRELOC
,
415 "%d-bit unsigned relocation zero-extended from %d bits\n",
416 asize
<< 3, ofmt
->maxbits
);
419 data
->insoffs
+= amax
;
420 data
->offset
+= amax
;
421 data
->size
= size
= asize
- amax
;
423 data
->data
= zero_buffer
;
424 data
->type
= OUT_RAWDATA
;
428 data
->offset
+= size
;
429 data
->insoffs
+= size
;
432 static inline void out_rawdata(struct out_data
*data
, const void *rawdata
,
435 data
->type
= OUT_RAWDATA
;
436 data
->data
= rawdata
;
441 static void out_rawbyte(struct out_data
*data
, uint8_t byte
)
443 data
->type
= OUT_RAWDATA
;
449 static inline void out_reserve(struct out_data
*data
, uint64_t size
)
451 data
->type
= OUT_RESERVE
;
456 static inline void out_imm(struct out_data
*data
, const struct operand
*opx
,
457 int size
, enum out_sign sign
)
460 (opx
->opflags
& OPFLAG_RELATIVE
) ? OUT_RELADDR
: OUT_ADDRESS
;
463 data
->toffset
= opx
->offset
;
464 data
->tsegment
= opx
->segment
;
465 data
->twrt
= opx
->wrt
;
467 * XXX: improve this if at some point in the future we can
468 * distinguish the subtrahend in expressions like [foo - bar]
469 * where bar is a symbol in the current segment. However, at the
470 * current point, if OPFLAG_RELATIVE is set that subtraction has
477 static void out_reladdr(struct out_data
*data
, const struct operand
*opx
,
480 if (opx
->opflags
& OPFLAG_RELATIVE
)
481 nasm_error(ERR_NONFATAL
, "invalid use of self-relative expression");
483 data
->type
= OUT_RELADDR
;
484 data
->sign
= OUT_SIGNED
;
486 data
->toffset
= opx
->offset
;
487 data
->tsegment
= opx
->segment
;
488 data
->twrt
= opx
->wrt
;
489 data
->relbase
= data
->offset
+ (data
->inslen
- data
->insoffs
);
493 static inline void out_segment(struct out_data
*data
,
494 const struct operand
*opx
)
496 data
->type
= OUT_SEGMENT
;
497 data
->sign
= OUT_UNSIGNED
;
499 data
->toffset
= opx
->offset
; /* Is this really needed/wanted? */
500 data
->tsegment
= ofmt
->segbase(opx
->segment
+ 1);
501 data
->twrt
= opx
->wrt
;
505 static bool jmp_match(int32_t segment
, int64_t offset
, int bits
,
506 insn
* ins
, const struct itemplate
*temp
)
509 const uint8_t *code
= temp
->code
;
513 if (((c
& ~1) != 0370) || (ins
->oprs
[0].type
& STRICT
))
517 if (optimizing
< 0 && c
== 0371)
520 isize
= calcsize(segment
, offset
, bits
, ins
, temp
);
522 if (ins
->oprs
[0].opflags
& OPFLAG_UNKNOWN
)
523 /* Be optimistic in pass 1 */
526 if (ins
->oprs
[0].segment
!= segment
)
529 isize
= ins
->oprs
[0].offset
- offset
- isize
; /* isize is delta */
530 is_byte
= (isize
>= -128 && isize
<= 127); /* is it byte size? */
532 if (is_byte
&& c
== 0371 && ins
->prefixes
[PPS_REP
] == P_BND
) {
533 /* jmp short (opcode eb) cannot be used with bnd prefix. */
534 ins
->prefixes
[PPS_REP
] = P_none
;
535 nasm_error(ERR_WARNING
| ERR_WARN_BND
| ERR_PASS2
,
536 "jmp short does not init bnd regs - bnd prefix dropped.");
542 /* This is totally just a wild guess what is reasonable... */
543 #define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
545 int64_t assemble(int32_t segment
, int64_t start
, int bits
, insn
*instruction
)
547 struct out_data data
;
548 const struct itemplate
*temp
;
550 int64_t wsize
; /* size for DB etc. */
554 data
.segment
= segment
;
556 data
.sign
= OUT_WRAP
;
559 wsize
= idata_bytes(instruction
->opcode
);
566 list_for_each(e
, instruction
->eops
) {
567 if (e
->type
== EOT_DB_NUMBER
) {
569 nasm_error(ERR_NONFATAL
,
570 "integer supplied to a DT, DO, DY or DZ"
574 data
.type
= e
->relative
? OUT_RELADDR
: OUT_ADDRESS
;
575 data
.inslen
= data
.size
= wsize
;
576 data
.toffset
= e
->offset
;
577 data
.tsegment
= e
->segment
;
582 } else if (e
->type
== EOT_DB_STRING
||
583 e
->type
== EOT_DB_STRING_FREE
) {
584 int align
= e
->stringlen
% wsize
;
586 align
= wsize
- align
;
589 data
.inslen
= e
->stringlen
+ align
;
591 out_rawdata(&data
, e
->stringval
, e
->stringlen
);
592 out_rawdata(&data
, zero_buffer
, align
);
595 } else if (instruction
->opcode
== I_INCBIN
) {
596 const char *fname
= instruction
->eops
->stringval
;
598 size_t t
= instruction
->times
; /* INCBIN handles TIMES by itself */
601 const void *map
= NULL
;
603 size_t blk
= 0; /* Buffered I/O block size */
604 size_t m
= 0; /* Bytes last read */
606 fp
= nasm_open_read(fname
, NF_BINARY
|NF_FORMAP
);
608 nasm_error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
613 len
= nasm_file_size(fp
);
615 if (len
== (off_t
)-1) {
616 nasm_error(ERR_NONFATAL
, "`incbin': unable to get length of file `%s'",
621 if (instruction
->eops
->next
) {
622 base
= instruction
->eops
->next
->offset
;
627 if (instruction
->eops
->next
->next
&&
628 len
> (off_t
)instruction
->eops
->next
->next
->offset
)
629 len
= (off_t
)instruction
->eops
->next
->next
->offset
;
633 lfmt
->set_offset(data
.offset
);
634 lfmt
->uplevel(LIST_INCBIN
);
639 /* Try to map file data */
640 map
= nasm_map_file(fp
, base
, len
);
642 blk
= len
< (off_t
)INCBIN_MAX_BUF
? (size_t)len
: INCBIN_MAX_BUF
;
643 buf
= nasm_malloc(blk
);
648 * Consider these irrelevant for INCBIN, since it is fully
649 * possible that these might be (way) bigger than an int
650 * can hold; there is, however, no reason to widen these
651 * types just for INCBIN. data.inslen == 0 signals to the
652 * backend that these fields are meaningless, if at all
659 out_rawdata(&data
, map
, len
);
660 } else if ((off_t
)m
== len
) {
661 out_rawdata(&data
, buf
, len
);
665 if (fseeko(fp
, base
, SEEK_SET
) < 0 || ferror(fp
)) {
666 nasm_error(ERR_NONFATAL
,
667 "`incbin': unable to seek on file `%s'",
672 m
= fread(buf
, 1, l
< (off_t
)blk
? (size_t)l
: blk
, fp
);
673 if (!m
|| feof(fp
)) {
675 * This shouldn't happen unless the file
676 * actually changes while we are reading
679 nasm_error(ERR_NONFATAL
,
680 "`incbin': unexpected EOF while"
681 " reading file `%s'", fname
);
684 out_rawdata(&data
, buf
, m
);
690 lfmt
->downlevel(LIST_INCBIN
);
691 if (instruction
->times
> 1) {
692 lfmt
->set_offset(start
);
693 lfmt
->uplevel(LIST_TIMES
);
694 lfmt
->downlevel(LIST_TIMES
);
697 nasm_error(ERR_NONFATAL
,
698 "`incbin': error while"
699 " reading file `%s'", fname
);
705 nasm_unmap_file(map
, len
);
708 instruction
->times
= 1; /* Tell the upper layer not to iterate */
711 /* "Real" instruction */
713 /* Check to see if we need an address-size prefix */
714 add_asp(instruction
, bits
);
716 m
= find_match(&temp
, instruction
, data
.segment
, data
.offset
, bits
);
720 int64_t insn_size
= calcsize(data
.segment
, data
.offset
,
721 bits
, instruction
, temp
);
722 nasm_assert(insn_size
>= 0);
727 data
.inslen
= insn_size
;
729 gencode(&data
, instruction
);
730 nasm_assert(data
.insoffs
== insn_size
);
734 case MERR_OPSIZEMISSING
:
735 nasm_error(ERR_NONFATAL
, "operation size not specified");
737 case MERR_OPSIZEMISMATCH
:
738 nasm_error(ERR_NONFATAL
, "mismatch in operand sizes");
741 nasm_error(ERR_NONFATAL
,
742 "broadcast not permitted on this operand");
744 case MERR_BRNUMMISMATCH
:
745 nasm_error(ERR_NONFATAL
,
746 "mismatch in the number of broadcasting elements");
748 case MERR_MASKNOTHERE
:
749 nasm_error(ERR_NONFATAL
,
750 "mask not permitted on this operand");
752 case MERR_DECONOTHERE
:
753 nasm_error(ERR_NONFATAL
, "unsupported mode decorator for instruction");
756 nasm_error(ERR_NONFATAL
, "no instruction for this cpu level");
759 nasm_error(ERR_NONFATAL
, "instruction not supported in %d-bit mode",
762 case MERR_ENCMISMATCH
:
763 nasm_error(ERR_NONFATAL
, "specific encoding scheme not available");
766 nasm_error(ERR_NONFATAL
, "bnd prefix is not allowed");
769 nasm_error(ERR_NONFATAL
, "%s prefix is not allowed",
770 (has_prefix(instruction
, PPS_REP
, P_REPNE
) ?
774 nasm_error(ERR_NONFATAL
,
775 "invalid combination of opcode and operands");
779 instruction
->times
= 1; /* Avoid repeated error messages */
782 return data
.offset
- start
;
785 int64_t insn_size(int32_t segment
, int64_t offset
, int bits
, insn
*instruction
)
787 const struct itemplate
*temp
;
790 if (instruction
->opcode
== I_none
)
793 if (opcode_is_db(instruction
->opcode
)) {
795 int32_t isize
, osize
, wsize
;
798 wsize
= idata_bytes(instruction
->opcode
);
799 nasm_assert(wsize
> 0);
801 list_for_each(e
, instruction
->eops
) {
805 if (e
->type
== EOT_DB_NUMBER
) {
807 warn_overflow_const(e
->offset
, wsize
);
808 } else if (e
->type
== EOT_DB_STRING
||
809 e
->type
== EOT_DB_STRING_FREE
)
810 osize
= e
->stringlen
;
812 align
= (-osize
) % wsize
;
815 isize
+= osize
+ align
;
820 if (instruction
->opcode
== I_INCBIN
) {
821 const char *fname
= instruction
->eops
->stringval
;
824 len
= nasm_file_size_by_path(fname
);
825 if (len
== (off_t
)-1) {
826 nasm_error(ERR_NONFATAL
, "`incbin': unable to get length of file `%s'",
831 if (instruction
->eops
->next
) {
832 if (len
<= (off_t
)instruction
->eops
->next
->offset
) {
835 len
-= instruction
->eops
->next
->offset
;
836 if (instruction
->eops
->next
->next
&&
837 len
> (off_t
)instruction
->eops
->next
->next
->offset
) {
838 len
= (off_t
)instruction
->eops
->next
->next
->offset
;
843 len
*= instruction
->times
;
844 instruction
->times
= 1; /* Tell the upper layer to not iterate */
849 /* Check to see if we need an address-size prefix */
850 add_asp(instruction
, bits
);
852 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
854 /* we've matched an instruction. */
855 return calcsize(segment
, offset
, bits
, instruction
, temp
);
857 return -1; /* didn't match any instruction */
861 static void bad_hle_warn(const insn
* ins
, uint8_t hleok
)
863 enum prefixes rep_pfx
= ins
->prefixes
[PPS_REP
];
864 enum whatwarn
{ w_none
, w_lock
, w_inval
} ww
;
865 static const enum whatwarn warn
[2][4] =
867 { w_inval
, w_inval
, w_none
, w_lock
}, /* XACQUIRE */
868 { w_inval
, w_none
, w_none
, w_lock
}, /* XRELEASE */
872 n
= (unsigned int)rep_pfx
- P_XACQUIRE
;
874 return; /* Not XACQUIRE/XRELEASE */
877 if (!is_class(MEMORY
, ins
->oprs
[0].type
))
878 ww
= w_inval
; /* HLE requires operand 0 to be memory */
885 if (ins
->prefixes
[PPS_LOCK
] != P_LOCK
) {
886 nasm_error(ERR_WARNING
| ERR_WARN_HLE
| ERR_PASS2
,
887 "%s with this instruction requires lock",
888 prefix_name(rep_pfx
));
893 nasm_error(ERR_WARNING
| ERR_WARN_HLE
| ERR_PASS2
,
894 "%s invalid with this instruction",
895 prefix_name(rep_pfx
));
900 /* Common construct */
901 #define case3(x) case (x): case (x)+1: case (x)+2
902 #define case4(x) case3(x): case (x)+3
904 static int64_t calcsize(int32_t segment
, int64_t offset
, int bits
,
905 insn
* ins
, const struct itemplate
*temp
)
907 const uint8_t *codes
= temp
->code
;
916 bool lockcheck
= true;
917 enum reg_enum mib_index
= R_none
; /* For a separate index MIB reg form */
920 ins
->rex
= 0; /* Ensure REX is reset */
921 eat
= EA_SCALAR
; /* Expect a scalar EA */
922 memset(ins
->evex_p
, 0, 3); /* Ensure EVEX is reset */
924 if (ins
->prefixes
[PPS_OSIZE
] == P_O64
)
927 (void)segment
; /* Don't warn that this parameter is unused */
928 (void)offset
; /* Don't warn that this parameter is unused */
932 op1
= (c
& 3) + ((opex
& 1) << 2);
933 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
934 opx
= &ins
->oprs
[op1
];
935 opex
= 0; /* For the next iteration */
939 codes
+= c
, length
+= c
;
948 op_rexflags(opx
, REX_B
|REX_H
|REX_P
|REX_W
);
953 /* this is an index reg of MIB operand */
954 mib_index
= opx
->basereg
;
967 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
968 length
+= (opx
->type
& BITS16
) ? 2 : 4;
970 length
+= (bits
== 16) ? 2 : 4;
978 length
+= ins
->addr_size
>> 3;
986 length
+= 8; /* MOV reg64/imm */
994 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
995 length
+= (opx
->type
& BITS16
) ? 2 : 4;
997 length
+= (bits
== 16) ? 2 : 4;
1020 ins
->vexreg
= regval(opx
);
1021 ins
->evex_p
[2] |= op_evexflags(opx
, EVEX_P2VP
, 2); /* High-16 NDS */
1022 ins
->vex_cm
= *codes
++;
1023 ins
->vex_wlp
= *codes
++;
1024 ins
->evex_tuple
= (*codes
++ - 0300);
1030 ins
->vex_cm
= *codes
++;
1031 ins
->vex_wlp
= *codes
++;
1032 ins
->evex_tuple
= (*codes
++ - 0300);
1041 ins
->vexreg
= regval(opx
);
1042 ins
->vex_cm
= *codes
++;
1043 ins
->vex_wlp
= *codes
++;
1049 ins
->vex_cm
= *codes
++;
1050 ins
->vex_wlp
= *codes
++;
1067 length
+= (bits
!= 16) && !has_prefix(ins
, PPS_ASIZE
, P_A16
);
1071 length
+= (bits
!= 32) && !has_prefix(ins
, PPS_ASIZE
, P_A32
);
1078 if (bits
!= 64 || has_prefix(ins
, PPS_ASIZE
, P_A16
) ||
1079 has_prefix(ins
, PPS_ASIZE
, P_A32
))
1088 enum prefixes pfx
= ins
->prefixes
[PPS_OSIZE
];
1092 nasm_error(ERR_WARNING
| ERR_PASS2
, "invalid operand size prefix");
1094 ins
->prefixes
[PPS_OSIZE
] = P_O16
;
1100 enum prefixes pfx
= ins
->prefixes
[PPS_OSIZE
];
1104 nasm_error(ERR_WARNING
| ERR_PASS2
, "invalid operand size prefix");
1106 ins
->prefixes
[PPS_OSIZE
] = P_O32
;
1148 if (!ins
->prefixes
[PPS_REP
])
1149 ins
->prefixes
[PPS_REP
] = P_REP
;
1153 if (!ins
->prefixes
[PPS_REP
])
1154 ins
->prefixes
[PPS_REP
] = P_REPNE
;
1158 if (!absolute_op(&ins
->oprs
[0]))
1159 nasm_error(ERR_NONFATAL
, "attempt to reserve non-constant"
1160 " quantity of BSS space");
1161 else if (ins
->oprs
[0].opflags
& OPFLAG_FORWARD
)
1162 nasm_error(ERR_WARNING
| ERR_PASS1
,
1163 "forward reference in RESx can have unpredictable results");
1165 length
+= ins
->oprs
[0].offset
;
1169 if (!ins
->prefixes
[PPS_WAIT
])
1170 ins
->prefixes
[PPS_WAIT
] = P_WAIT
;
1225 struct operand
*opy
= &ins
->oprs
[op2
];
1226 struct operand
*op_er_sae
;
1228 ea_data
.rex
= 0; /* Ensure ea.REX is initially 0 */
1231 /* pick rfield from operand b (opx) */
1232 rflags
= regflag(opx
);
1233 rfield
= nasm_regvals
[opx
->basereg
];
1239 /* EVEX.b1 : evex_brerop contains the operand position */
1240 op_er_sae
= (ins
->evex_brerop
>= 0 ?
1241 &ins
->oprs
[ins
->evex_brerop
] : NULL
);
1243 if (op_er_sae
&& (op_er_sae
->decoflags
& (ER
| SAE
))) {
1245 ins
->evex_p
[2] |= EVEX_P2B
;
1246 if (op_er_sae
->decoflags
& ER
) {
1247 /* set EVEX.RC (rounding control) */
1248 ins
->evex_p
[2] |= ((ins
->evex_rm
- BRC_RN
) << 5)
1252 /* set EVEX.L'L (vector length) */
1253 ins
->evex_p
[2] |= ((ins
->vex_wlp
<< (5 - 2)) & EVEX_P2LL
);
1254 ins
->evex_p
[1] |= ((ins
->vex_wlp
<< (7 - 4)) & EVEX_P1W
);
1255 if (opy
->decoflags
& BRDCAST_MASK
) {
1257 ins
->evex_p
[2] |= EVEX_P2B
;
1261 if (itemp_has(temp
, IF_MIB
)) {
1262 opy
->eaflags
|= EAF_MIB
;
1264 * if a separate form of MIB (ICC style) is used,
1265 * the index reg info is merged into mem operand
1267 if (mib_index
!= R_none
) {
1268 opy
->indexreg
= mib_index
;
1270 opy
->hintbase
= mib_index
;
1271 opy
->hinttype
= EAH_NOTBASE
;
1275 if (process_ea(opy
, &ea_data
, bits
,
1276 rfield
, rflags
, ins
, &errmsg
) != eat
) {
1277 nasm_error(ERR_NONFATAL
, "%s", errmsg
);
1280 ins
->rex
|= ea_data
.rex
;
1281 length
+= ea_data
.size
;
1287 nasm_panic(0, "internal instruction table corrupt"
1288 ": instruction code \\%o (0x%02X) given", c
, c
);
1293 ins
->rex
&= rex_mask
;
1295 if (ins
->rex
& REX_NH
) {
1296 if (ins
->rex
& REX_H
) {
1297 nasm_error(ERR_NONFATAL
, "instruction cannot use high registers");
1300 ins
->rex
&= ~REX_P
; /* Don't force REX prefix due to high reg */
1303 switch (ins
->prefixes
[PPS_VEX
]) {
1305 if (!(ins
->rex
& REX_EV
))
1310 if (!(ins
->rex
& REX_V
))
1317 if (ins
->rex
& (REX_V
| REX_EV
)) {
1318 int bad32
= REX_R
|REX_W
|REX_X
|REX_B
;
1320 if (ins
->rex
& REX_H
) {
1321 nasm_error(ERR_NONFATAL
, "cannot use high register in AVX instruction");
1324 switch (ins
->vex_wlp
& 060) {
1338 if (bits
!= 64 && ((ins
->rex
& bad32
) || ins
->vexreg
> 7)) {
1339 nasm_error(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1341 } else if (!(ins
->rex
& REX_EV
) &&
1342 ((ins
->vexreg
> 15) || (ins
->evex_p
[0] & 0xf0))) {
1343 nasm_error(ERR_NONFATAL
, "invalid high-16 register in non-AVX-512");
1346 if (ins
->rex
& REX_EV
)
1348 else if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
)) ||
1349 ins
->prefixes
[PPS_VEX
] == P_VEX3
)
1353 } else if (ins
->rex
& REX_MASK
) {
1354 if (ins
->rex
& REX_H
) {
1355 nasm_error(ERR_NONFATAL
, "cannot use high register in rex instruction");
1357 } else if (bits
== 64) {
1359 } else if ((ins
->rex
& REX_L
) &&
1360 !(ins
->rex
& (REX_P
|REX_W
|REX_X
|REX_B
)) &&
1361 iflag_ffs(&cpu
) >= IF_X86_64
) {
1363 assert_no_prefix(ins
, PPS_LOCK
);
1364 lockcheck
= false; /* Already errored, no need for warning */
1367 nasm_error(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1372 if (has_prefix(ins
, PPS_LOCK
, P_LOCK
) && lockcheck
&&
1373 (!itemp_has(temp
,IF_LOCK
) || !is_class(MEMORY
, ins
->oprs
[0].type
))) {
1374 nasm_error(ERR_WARNING
| ERR_WARN_LOCK
| ERR_PASS2
,
1375 "instruction is not lockable");
1378 bad_hle_warn(ins
, hleok
);
1381 * when BND prefix is set by DEFAULT directive,
1382 * BND prefix is added to every appropriate instruction line
1383 * unless it is overridden by NOBND prefix.
1386 (itemp_has(temp
, IF_BND
) && !has_prefix(ins
, PPS_REP
, P_NOBND
)))
1387 ins
->prefixes
[PPS_REP
] = P_BND
;
1390 * Add length of legacy prefixes
1392 length
+= emit_prefix(NULL
, bits
, ins
);
1397 static inline void emit_rex(struct out_data
*data
, insn
*ins
)
1399 if (data
->bits
== 64) {
1400 if ((ins
->rex
& REX_MASK
) &&
1401 !(ins
->rex
& (REX_V
| REX_EV
)) &&
1403 uint8_t rex
= (ins
->rex
& REX_MASK
) | REX_P
;
1404 out_rawbyte(data
, rex
);
1405 ins
->rex_done
= true;
1410 static int emit_prefix(struct out_data
*data
, const int bits
, insn
*ins
)
1415 for (j
= 0; j
< MAXPREFIX
; j
++) {
1417 switch (ins
->prefixes
[j
]) {
1438 nasm_error(ERR_WARNING
| ERR_PASS2
,
1439 "cs segment base generated, but will be ignored in 64-bit mode");
1445 nasm_error(ERR_WARNING
| ERR_PASS2
,
1446 "ds segment base generated, but will be ignored in 64-bit mode");
1452 nasm_error(ERR_WARNING
| ERR_PASS2
,
1453 "es segment base generated, but will be ignored in 64-bit mode");
1465 nasm_error(ERR_WARNING
| ERR_PASS2
,
1466 "ss segment base generated, but will be ignored in 64-bit mode");
1472 nasm_error(ERR_NONFATAL
,
1473 "segr6 and segr7 cannot be used as prefixes");
1477 nasm_error(ERR_NONFATAL
,
1478 "16-bit addressing is not supported "
1480 } else if (bits
!= 16)
1489 nasm_error(ERR_NONFATAL
,
1490 "64-bit addressing is only supported "
1518 nasm_panic(0, "invalid instruction prefix");
1522 out_rawbyte(data
, c
);
1529 static void gencode(struct out_data
*data
, insn
*ins
)
1535 struct operand
*opx
;
1536 const uint8_t *codes
= data
->itemp
->code
;
1538 enum ea_type eat
= EA_SCALAR
;
1540 const int bits
= data
->bits
;
1543 ins
->rex_done
= false;
1545 emit_prefix(data
, bits
, ins
);
1549 op1
= (c
& 3) + ((opex
& 1) << 2);
1550 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
1551 opx
= &ins
->oprs
[op1
];
1552 opex
= 0; /* For the next iteration */
1560 emit_rex(data
, ins
);
1561 out_rawdata(data
, codes
, c
);
1572 emit_rex(data
, ins
);
1573 out_rawbyte(data
, *codes
++ + (regval(opx
) & 7));
1580 if (opx
->offset
< -256 || opx
->offset
> 255)
1581 nasm_error(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1582 "byte value exceeds bounds");
1583 out_imm(data
, opx
, 1, OUT_WRAP
);
1587 if (opx
->offset
< 0 || opx
->offset
> 255)
1588 nasm_error(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1589 "unsigned byte value exceeds bounds");
1590 out_imm(data
, opx
, 1, OUT_UNSIGNED
);
1594 warn_overflow_opd(opx
, 2);
1595 out_imm(data
, opx
, 2, OUT_WRAP
);
1599 if (opx
->type
& (BITS16
| BITS32
))
1600 size
= (opx
->type
& BITS16
) ? 2 : 4;
1602 size
= (bits
== 16) ? 2 : 4;
1603 warn_overflow_opd(opx
, size
);
1604 out_imm(data
, opx
, size
, OUT_WRAP
);
1608 warn_overflow_opd(opx
, 4);
1609 out_imm(data
, opx
, 4, OUT_WRAP
);
1613 size
= ins
->addr_size
>> 3;
1614 warn_overflow_opd(opx
, size
);
1615 out_imm(data
, opx
, size
, OUT_WRAP
);
1619 if (opx
->segment
== data
->segment
) {
1620 int64_t delta
= opx
->offset
- data
->offset
1621 - (data
->inslen
- data
->insoffs
);
1622 if (delta
> 127 || delta
< -128)
1623 nasm_error(ERR_NONFATAL
, "short jump is out of range");
1625 out_reladdr(data
, opx
, 1);
1629 out_imm(data
, opx
, 8, OUT_WRAP
);
1633 out_reladdr(data
, opx
, 2);
1637 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1638 size
= (opx
->type
& BITS16
) ? 2 : 4;
1640 size
= (bits
== 16) ? 2 : 4;
1642 out_reladdr(data
, opx
, size
);
1646 out_reladdr(data
, opx
, 4);
1650 if (opx
->segment
== NO_SEG
)
1651 nasm_error(ERR_NONFATAL
, "value referenced by FAR is not"
1653 out_segment(data
, opx
);
1658 int mask
= ins
->prefixes
[PPS_VEX
] == P_EVEX
? 7 : 15;
1659 const struct operand
*opy
;
1662 opx
= &ins
->oprs
[c
>> 3];
1663 opy
= &ins
->oprs
[c
& 7];
1664 if (!absolute_op(opy
)) {
1665 nasm_error(ERR_NONFATAL
,
1666 "non-absolute expression not permitted as argument %d",
1668 } else if (opy
->offset
& ~mask
) {
1669 nasm_error(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1670 "is4 argument exceeds bounds");
1672 c
= opy
->offset
& mask
;
1678 opx
= &ins
->oprs
[c
>> 4];
1685 r
= nasm_regvals
[opx
->basereg
];
1686 out_rawbyte(data
, (r
<< 4) | ((r
& 0x10) >> 1) | c
);
1690 if (absolute_op(opx
) &&
1691 (int32_t)opx
->offset
!= (int64_t)opx
->offset
) {
1692 nasm_error(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1693 "signed dword immediate exceeds bounds");
1695 out_imm(data
, opx
, 4, OUT_SIGNED
);
1701 ins
->evex_p
[2] |= op_evexflags(&ins
->oprs
[0],
1702 EVEX_P2Z
| EVEX_P2AAA
, 2);
1703 ins
->evex_p
[2] ^= EVEX_P2VP
; /* 1's complement */
1705 /* EVEX.X can be set by either REX or EVEX for different reasons */
1706 bytes
[1] = ((((ins
->rex
& 7) << 5) |
1707 (ins
->evex_p
[0] & (EVEX_P0X
| EVEX_P0RP
))) ^ 0xf0) |
1708 (ins
->vex_cm
& EVEX_P0MM
);
1709 bytes
[2] = ((ins
->rex
& REX_W
) << (7 - 3)) |
1710 ((~ins
->vexreg
& 15) << 3) |
1711 (1 << 2) | (ins
->vex_wlp
& 3);
1712 bytes
[3] = ins
->evex_p
[2];
1713 out_rawdata(data
, bytes
, 4);
1719 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
)) ||
1720 ins
->prefixes
[PPS_VEX
] == P_VEX3
) {
1721 bytes
[0] = (ins
->vex_cm
>> 6) ? 0x8f : 0xc4;
1722 bytes
[1] = (ins
->vex_cm
& 31) | ((~ins
->rex
& 7) << 5);
1723 bytes
[2] = ((ins
->rex
& REX_W
) << (7-3)) |
1724 ((~ins
->vexreg
& 15)<< 3) | (ins
->vex_wlp
& 07);
1725 out_rawdata(data
, bytes
, 3);
1728 bytes
[1] = ((~ins
->rex
& REX_R
) << (7-2)) |
1729 ((~ins
->vexreg
& 15) << 3) | (ins
->vex_wlp
& 07);
1730 out_rawdata(data
, bytes
, 2);
1744 if (absolute_op(opx
)) {
1745 if (ins
->rex
& REX_W
)
1747 else if (ins
->prefixes
[PPS_OSIZE
] == P_O16
)
1749 else if (ins
->prefixes
[PPS_OSIZE
] == P_O32
)
1754 um
= (uint64_t)2 << (s
-1);
1757 if (uv
> 127 && uv
< (uint64_t)-128 &&
1758 (uv
< um
-128 || uv
> um
-1)) {
1759 /* If this wasn't explicitly byte-sized, warn as though we
1760 * had fallen through to the imm16/32/64 case.
1762 nasm_error(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1763 "%s value exceeds bounds",
1764 (opx
->type
& BITS8
) ? "signed byte" :
1770 /* Output as a raw byte to avoid byte overflow check */
1771 out_rawbyte(data
, (uint8_t)uv
);
1773 out_imm(data
, opx
, 1, OUT_WRAP
); /* XXX: OUT_SIGNED? */
1782 if (bits
== 32 && !has_prefix(ins
, PPS_ASIZE
, P_A16
))
1783 out_rawbyte(data
, 0x67);
1787 if (bits
!= 32 && !has_prefix(ins
, PPS_ASIZE
, P_A32
))
1788 out_rawbyte(data
, 0x67);
1820 out_rawbyte(data
, *codes
++ ^ get_cond_opcode(ins
->condition
));
1828 out_rawbyte(data
, c
- 0332 + 0xF2);
1832 if (ins
->rex
& REX_R
)
1833 out_rawbyte(data
, 0xF0);
1834 ins
->rex
&= ~(REX_L
|REX_R
);
1845 if (ins
->oprs
[0].segment
!= NO_SEG
)
1846 nasm_panic(0, "non-constant BSS size in pass two");
1848 out_reserve(data
, ins
->oprs
[0].offset
);
1858 out_rawbyte(data
, 0x66);
1867 out_rawbyte(data
, c
- 0366 + 0x66);
1874 out_rawbyte(data
, bits
== 16 ? 3 : 5);
1906 struct operand
*opy
= &ins
->oprs
[op2
];
1909 /* pick rfield from operand b (opx) */
1910 rflags
= regflag(opx
);
1911 rfield
= nasm_regvals
[opx
->basereg
];
1913 /* rfield is constant */
1918 if (process_ea(opy
, &ea_data
, bits
,
1919 rfield
, rflags
, ins
, &errmsg
) != eat
)
1920 nasm_error(ERR_NONFATAL
, "%s", errmsg
);
1923 *p
++ = ea_data
.modrm
;
1924 if (ea_data
.sib_present
)
1926 out_rawdata(data
, bytes
, p
- bytes
);
1929 * Make sure the address gets the right offset in case
1930 * the line breaks in the .lst file (BR 1197827)
1933 if (ea_data
.bytes
) {
1934 /* use compressed displacement, if available */
1935 if (ea_data
.disp8
) {
1936 out_rawbyte(data
, ea_data
.disp8
);
1937 } else if (ea_data
.rip
) {
1938 out_reladdr(data
, opy
, ea_data
.bytes
);
1940 int asize
= ins
->addr_size
>> 3;
1942 if (overflow_general(opy
->offset
, asize
) ||
1943 signed_bits(opy
->offset
, ins
->addr_size
) !=
1944 signed_bits(opy
->offset
, ea_data
.bytes
<< 3))
1945 warn_overflow(ea_data
.bytes
);
1947 out_imm(data
, opy
, ea_data
.bytes
,
1948 (asize
> ea_data
.bytes
)
1949 ? OUT_SIGNED
: OUT_WRAP
);
1956 nasm_panic(0, "internal instruction table corrupt"
1957 ": instruction code \\%o (0x%02X) given", c
, c
);
1963 static opflags_t
regflag(const operand
* o
)
1965 if (!is_register(o
->basereg
))
1966 nasm_panic(0, "invalid operand passed to regflag()");
1967 return nasm_reg_flags
[o
->basereg
];
1970 static int32_t regval(const operand
* o
)
1972 if (!is_register(o
->basereg
))
1973 nasm_panic(0, "invalid operand passed to regval()");
1974 return nasm_regvals
[o
->basereg
];
1977 static int op_rexflags(const operand
* o
, int mask
)
1982 if (!is_register(o
->basereg
))
1983 nasm_panic(0, "invalid operand passed to op_rexflags()");
1985 flags
= nasm_reg_flags
[o
->basereg
];
1986 val
= nasm_regvals
[o
->basereg
];
1988 return rexflags(val
, flags
, mask
);
1991 static int rexflags(int val
, opflags_t flags
, int mask
)
1995 if (val
>= 0 && (val
& 8))
1996 rex
|= REX_B
|REX_X
|REX_R
;
1999 if (!(REG_HIGH
& ~flags
)) /* AH, CH, DH, BH */
2001 else if (!(REG8
& ~flags
) && val
>= 4) /* SPL, BPL, SIL, DIL */
2007 static int evexflags(int val
, decoflags_t deco
,
2008 int mask
, uint8_t byte
)
2014 if (val
>= 0 && (val
& 16))
2015 evex
|= (EVEX_P0RP
| EVEX_P0X
);
2018 if (val
>= 0 && (val
& 16))
2022 if (deco
& OPMASK_MASK
)
2023 evex
|= deco
& EVEX_P2AAA
;
2029 static int op_evexflags(const operand
* o
, int mask
, uint8_t byte
)
2033 val
= nasm_regvals
[o
->basereg
];
2035 return evexflags(val
, o
->decoflags
, mask
, byte
);
2038 static enum match_result
find_match(const struct itemplate
**tempp
,
2040 int32_t segment
, int64_t offset
, int bits
)
2042 const struct itemplate
*temp
;
2043 enum match_result m
, merr
;
2044 opflags_t xsizeflags
[MAX_OPERANDS
];
2045 bool opsizemissing
= false;
2046 int8_t broadcast
= instruction
->evex_brerop
;
2049 /* broadcasting uses a different data element size */
2050 for (i
= 0; i
< instruction
->operands
; i
++)
2052 xsizeflags
[i
] = instruction
->oprs
[i
].decoflags
& BRSIZE_MASK
;
2054 xsizeflags
[i
] = instruction
->oprs
[i
].type
& SIZE_MASK
;
2056 merr
= MERR_INVALOP
;
2058 for (temp
= nasm_instructions
[instruction
->opcode
];
2059 temp
->opcode
!= I_none
; temp
++) {
2060 m
= matches(temp
, instruction
, bits
);
2061 if (m
== MOK_JUMP
) {
2062 if (jmp_match(segment
, offset
, bits
, instruction
, temp
))
2066 } else if (m
== MERR_OPSIZEMISSING
&& !itemp_has(temp
, IF_SX
)) {
2068 * Missing operand size and a candidate for fuzzy matching...
2070 for (i
= 0; i
< temp
->operands
; i
++)
2072 xsizeflags
[i
] |= temp
->deco
[i
] & BRSIZE_MASK
;
2074 xsizeflags
[i
] |= temp
->opd
[i
] & SIZE_MASK
;
2075 opsizemissing
= true;
2079 if (merr
== MOK_GOOD
)
2083 /* No match, but see if we can get a fuzzy operand size match... */
2087 for (i
= 0; i
< instruction
->operands
; i
++) {
2089 * We ignore extrinsic operand sizes on registers, so we should
2090 * never try to fuzzy-match on them. This also resolves the case
2091 * when we have e.g. "xmmrm128" in two different positions.
2093 if (is_class(REGISTER
, instruction
->oprs
[i
].type
))
2096 /* This tests if xsizeflags[i] has more than one bit set */
2097 if ((xsizeflags
[i
] & (xsizeflags
[i
]-1)))
2098 goto done
; /* No luck */
2100 if (i
== broadcast
) {
2101 instruction
->oprs
[i
].decoflags
|= xsizeflags
[i
];
2102 instruction
->oprs
[i
].type
|= (xsizeflags
[i
] == BR_BITS32
?
2105 instruction
->oprs
[i
].type
|= xsizeflags
[i
]; /* Set the size */
2109 /* Try matching again... */
2110 for (temp
= nasm_instructions
[instruction
->opcode
];
2111 temp
->opcode
!= I_none
; temp
++) {
2112 m
= matches(temp
, instruction
, bits
);
2113 if (m
== MOK_JUMP
) {
2114 if (jmp_match(segment
, offset
, bits
, instruction
, temp
))
2121 if (merr
== MOK_GOOD
)
2130 static uint8_t get_broadcast_num(opflags_t opflags
, opflags_t brsize
)
2132 unsigned int opsize
= (opflags
& SIZE_MASK
) >> SIZE_SHIFT
;
2135 if (brsize
> BITS64
)
2136 nasm_error(ERR_FATAL
,
2137 "size of broadcasting element is greater than 64 bits");
2140 * The shift term is to take care of the extra BITS80 inserted
2141 * between BITS64 and BITS128.
2143 brcast_num
= ((opsize
/ (BITS64
>> SIZE_SHIFT
)) * (BITS64
/ brsize
))
2144 >> (opsize
> (BITS64
>> SIZE_SHIFT
));
2149 static enum match_result
matches(const struct itemplate
*itemp
,
2150 insn
*instruction
, int bits
)
2152 opflags_t size
[MAX_OPERANDS
], asize
;
2153 bool opsizemissing
= false;
2159 if (itemp
->opcode
!= instruction
->opcode
)
2160 return MERR_INVALOP
;
2163 * Count the operands
2165 if (itemp
->operands
!= instruction
->operands
)
2166 return MERR_INVALOP
;
2171 if (!(optimizing
> 0) && itemp_has(itemp
, IF_OPT
))
2172 return MERR_INVALOP
;
2177 switch (instruction
->prefixes
[PPS_VEX
]) {
2179 if (!itemp_has(itemp
, IF_EVEX
))
2180 return MERR_ENCMISMATCH
;
2184 if (!itemp_has(itemp
, IF_VEX
))
2185 return MERR_ENCMISMATCH
;
2192 * Check that no spurious colons or TOs are present
2194 for (i
= 0; i
< itemp
->operands
; i
++)
2195 if (instruction
->oprs
[i
].type
& ~itemp
->opd
[i
] & (COLON
| TO
))
2196 return MERR_INVALOP
;
2199 * Process size flags
2201 switch (itemp_smask(itemp
)) {
2202 case IF_GENBIT(IF_SB
):
2205 case IF_GENBIT(IF_SW
):
2208 case IF_GENBIT(IF_SD
):
2211 case IF_GENBIT(IF_SQ
):
2214 case IF_GENBIT(IF_SO
):
2217 case IF_GENBIT(IF_SY
):
2220 case IF_GENBIT(IF_SZ
):
2223 case IF_GENBIT(IF_SIZE
):
2244 if (itemp_armask(itemp
)) {
2245 /* S- flags only apply to a specific operand */
2246 i
= itemp_arg(itemp
);
2247 memset(size
, 0, sizeof size
);
2250 /* S- flags apply to all operands */
2251 for (i
= 0; i
< MAX_OPERANDS
; i
++)
2256 * Check that the operand flags all match up,
2257 * it's a bit tricky so lets be verbose:
2259 * 1) Find out the size of operand. If instruction
2260 * doesn't have one specified -- we're trying to
2261 * guess it either from template (IF_S* flag) or
2264 * 2) If template operand do not match the instruction OR
2265 * template has an operand size specified AND this size differ
2266 * from which instruction has (perhaps we got it from code bits)
2268 * a) Check that only size of instruction and operand is differ
2269 * other characteristics do match
2270 * b) Perhaps it's a register specified in instruction so
2271 * for such a case we just mark that operand as "size
2272 * missing" and this will turn on fuzzy operand size
2273 * logic facility (handled by a caller)
2275 for (i
= 0; i
< itemp
->operands
; i
++) {
2276 opflags_t type
= instruction
->oprs
[i
].type
;
2277 decoflags_t deco
= instruction
->oprs
[i
].decoflags
;
2278 decoflags_t ideco
= itemp
->deco
[i
];
2279 bool is_broadcast
= deco
& BRDCAST_MASK
;
2280 uint8_t brcast_num
= 0;
2281 opflags_t template_opsize
, insn_opsize
;
2283 if (!(type
& SIZE_MASK
))
2286 insn_opsize
= type
& SIZE_MASK
;
2287 if (!is_broadcast
) {
2288 template_opsize
= itemp
->opd
[i
] & SIZE_MASK
;
2290 decoflags_t deco_brsize
= ideco
& BRSIZE_MASK
;
2292 if (~ideco
& BRDCAST_MASK
)
2293 return MERR_BRNOTHERE
;
2296 * when broadcasting, the element size depends on
2297 * the instruction type. decorator flag should match.
2300 template_opsize
= (deco_brsize
== BR_BITS32
? BITS32
: BITS64
);
2301 /* calculate the proper number : {1to<brcast_num>} */
2302 brcast_num
= get_broadcast_num(itemp
->opd
[i
], template_opsize
);
2304 template_opsize
= 0;
2308 if (~ideco
& deco
& OPMASK_MASK
)
2309 return MERR_MASKNOTHERE
;
2311 if (~ideco
& deco
& (Z_MASK
|STATICRND_MASK
|SAE_MASK
))
2312 return MERR_DECONOTHERE
;
2314 if (itemp
->opd
[i
] & ~type
& ~SIZE_MASK
) {
2315 return MERR_INVALOP
;
2316 } else if (template_opsize
) {
2317 if (template_opsize
!= insn_opsize
) {
2319 return MERR_INVALOP
;
2320 } else if (!is_class(REGISTER
, type
)) {
2322 * Note: we don't honor extrinsic operand sizes for registers,
2323 * so "missing operand size" for a register should be
2324 * considered a wildcard match rather than an error.
2326 opsizemissing
= true;
2328 } else if (is_broadcast
&&
2330 (2U << ((deco
& BRNUM_MASK
) >> BRNUM_SHIFT
)))) {
2332 * broadcasting opsize matches but the number of repeated memory
2333 * element does not match.
2334 * if 64b double precision float is broadcasted to ymm (256b),
2335 * broadcasting decorator must be {1to4}.
2337 return MERR_BRNUMMISMATCH
;
2343 return MERR_OPSIZEMISSING
;
2346 * Check operand sizes
2348 if (itemp_has(itemp
, IF_SM
) || itemp_has(itemp
, IF_SM2
)) {
2349 oprs
= (itemp_has(itemp
, IF_SM2
) ? 2 : itemp
->operands
);
2350 for (i
= 0; i
< oprs
; i
++) {
2351 asize
= itemp
->opd
[i
] & SIZE_MASK
;
2353 for (i
= 0; i
< oprs
; i
++)
2359 oprs
= itemp
->operands
;
2362 for (i
= 0; i
< itemp
->operands
; i
++) {
2363 if (!(itemp
->opd
[i
] & SIZE_MASK
) &&
2364 (instruction
->oprs
[i
].type
& SIZE_MASK
& ~size
[i
]))
2365 return MERR_OPSIZEMISMATCH
;
2369 * Check template is okay at the set cpu level
2371 if (iflag_cmp_cpu_level(&insns_flags
[itemp
->iflag_idx
], &cpu
) > 0)
2375 * Verify the appropriate long mode flag.
2377 if (itemp_has(itemp
, (bits
== 64 ? IF_NOLONG
: IF_LONG
)))
2378 return MERR_BADMODE
;
2381 * If we have a HLE prefix, look for the NOHLE flag
2383 if (itemp_has(itemp
, IF_NOHLE
) &&
2384 (has_prefix(instruction
, PPS_REP
, P_XACQUIRE
) ||
2385 has_prefix(instruction
, PPS_REP
, P_XRELEASE
)))
2389 * Check if special handling needed for Jumps
2391 if ((itemp
->code
[0] & ~1) == 0370)
2395 * Check if BND prefix is allowed.
2396 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
2398 if (!itemp_has(itemp
, IF_BND
) &&
2399 (has_prefix(instruction
, PPS_REP
, P_BND
) ||
2400 has_prefix(instruction
, PPS_REP
, P_NOBND
)))
2402 else if (itemp_has(itemp
, IF_BND
) &&
2403 (has_prefix(instruction
, PPS_REP
, P_REPNE
) ||
2404 has_prefix(instruction
, PPS_REP
, P_REPNZ
)))
2405 return MERR_BADREPNE
;
2411 * Check if ModR/M.mod should/can be 01.
2412 * - EAF_BYTEOFFS is set
2413 * - offset can fit in a byte when EVEX is not used
2414 * - offset can be compressed when EVEX is used
2416 #define IS_MOD_01() (!(input->eaflags & EAF_WORDOFFS) && \
2417 (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
2418 is_disp8n(input, ins, &output->disp8) : \
2419 input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
2420 o <= 127 && seg == NO_SEG && !forw_ref)))
2422 static enum ea_type
process_ea(operand
*input
, ea
*output
, int bits
,
2423 int rfield
, opflags_t rflags
, insn
*ins
,
2424 const char **errmsg
)
2426 bool forw_ref
= !!(input
->opflags
& OPFLAG_UNKNOWN
);
2427 int addrbits
= ins
->addr_size
;
2428 int eaflags
= input
->eaflags
;
2430 *errmsg
= "invalid effective address"; /* Default error message */
2432 output
->type
= EA_SCALAR
;
2433 output
->rip
= false;
2436 /* REX flags for the rfield operand */
2437 output
->rex
|= rexflags(rfield
, rflags
, REX_R
| REX_P
| REX_W
| REX_H
);
2438 /* EVEX.R' flag for the REG operand */
2439 ins
->evex_p
[0] |= evexflags(rfield
, 0, EVEX_P0RP
, 0);
2441 if (is_class(REGISTER
, input
->type
)) {
2443 * It's a direct register.
2445 if (!is_register(input
->basereg
))
2448 if (!is_reg_class(REG_EA
, input
->basereg
))
2451 /* broadcasting is not available with a direct register operand. */
2452 if (input
->decoflags
& BRDCAST_MASK
) {
2453 *errmsg
= "broadcast not allowed with register operand";
2457 output
->rex
|= op_rexflags(input
, REX_B
| REX_P
| REX_W
| REX_H
);
2458 ins
->evex_p
[0] |= op_evexflags(input
, EVEX_P0X
, 0);
2459 output
->sib_present
= false; /* no SIB necessary */
2460 output
->bytes
= 0; /* no offset necessary either */
2461 output
->modrm
= GEN_MODRM(3, rfield
, nasm_regvals
[input
->basereg
]);
2464 * It's a memory reference.
2467 /* Embedded rounding or SAE is not available with a mem ref operand. */
2468 if (input
->decoflags
& (ER
| SAE
)) {
2469 *errmsg
= "embedded rounding is available only with "
2470 "register-register operations";
2474 if (input
->basereg
== -1 &&
2475 (input
->indexreg
== -1 || input
->scale
== 0)) {
2477 * It's a pure offset.
2479 if (bits
== 64 && ((input
->type
& IP_REL
) == IP_REL
)) {
2480 if (input
->segment
== NO_SEG
||
2481 (input
->opflags
& OPFLAG_RELATIVE
)) {
2482 nasm_error(ERR_WARNING
| ERR_PASS2
,
2483 "absolute address can not be RIP-relative");
2484 input
->type
&= ~IP_REL
;
2485 input
->type
|= MEMORY
;
2490 !(IP_REL
& ~input
->type
) && (eaflags
& EAF_MIB
)) {
2491 *errmsg
= "RIP-relative addressing is prohibited for MIB";
2495 if (eaflags
& EAF_BYTEOFFS
||
2496 (eaflags
& EAF_WORDOFFS
&&
2497 input
->disp_size
!= (addrbits
!= 16 ? 32 : 16))) {
2498 nasm_error(ERR_WARNING
| ERR_PASS1
,
2499 "displacement size ignored on absolute address");
2502 if (bits
== 64 && (~input
->type
& IP_REL
)) {
2503 output
->sib_present
= true;
2504 output
->sib
= GEN_SIB(0, 4, 5);
2506 output
->modrm
= GEN_MODRM(0, rfield
, 4);
2507 output
->rip
= false;
2509 output
->sib_present
= false;
2510 output
->bytes
= (addrbits
!= 16 ? 4 : 2);
2511 output
->modrm
= GEN_MODRM(0, rfield
,
2512 (addrbits
!= 16 ? 5 : 6));
2513 output
->rip
= bits
== 64;
2517 * It's an indirection.
2519 int i
= input
->indexreg
, b
= input
->basereg
, s
= input
->scale
;
2520 int32_t seg
= input
->segment
;
2521 int hb
= input
->hintbase
, ht
= input
->hinttype
;
2522 int t
, it
, bt
; /* register numbers */
2523 opflags_t x
, ix
, bx
; /* register flags */
2526 i
= -1; /* make this easy, at least */
2528 if (is_register(i
)) {
2529 it
= nasm_regvals
[i
];
2530 ix
= nasm_reg_flags
[i
];
2536 if (is_register(b
)) {
2537 bt
= nasm_regvals
[b
];
2538 bx
= nasm_reg_flags
[b
];
2544 /* if either one are a vector register... */
2545 if ((ix
|bx
) & (XMMREG
|YMMREG
|ZMMREG
) & ~REG_EA
) {
2546 opflags_t sok
= BITS32
| BITS64
;
2547 int32_t o
= input
->offset
;
2548 int mod
, scale
, index
, base
;
2551 * For a vector SIB, one has to be a vector and the other,
2552 * if present, a GPR. The vector must be the index operand.
2554 if (it
== -1 || (bx
& (XMMREG
|YMMREG
|ZMMREG
) & ~REG_EA
)) {
2560 t
= bt
, bt
= it
, it
= t
;
2561 x
= bx
, bx
= ix
, ix
= x
;
2567 if (!(REG64
& ~bx
) || !(REG32
& ~bx
))
2574 * While we're here, ensure the user didn't specify
2577 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2580 if (addrbits
== 16 ||
2581 (addrbits
== 32 && !(sok
& BITS32
)) ||
2582 (addrbits
== 64 && !(sok
& BITS64
)))
2585 output
->type
= ((ix
& ZMMREG
& ~REG_EA
) ? EA_ZMMVSIB
2586 : ((ix
& YMMREG
& ~REG_EA
)
2587 ? EA_YMMVSIB
: EA_XMMVSIB
));
2589 output
->rex
|= rexflags(it
, ix
, REX_X
);
2590 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2591 ins
->evex_p
[2] |= evexflags(it
, 0, EVEX_P2VP
, 2);
2593 index
= it
& 7; /* it is known to be != -1 */
2608 default: /* then what the smeg is it? */
2609 goto err
; /* panic */
2617 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2618 seg
== NO_SEG
&& !forw_ref
&&
2619 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2621 else if (IS_MOD_01())
2627 output
->sib_present
= true;
2628 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2629 output
->modrm
= GEN_MODRM(mod
, rfield
, 4);
2630 output
->sib
= GEN_SIB(scale
, index
, base
);
2631 } else if ((ix
|bx
) & (BITS32
|BITS64
)) {
2633 * it must be a 32/64-bit memory reference. Firstly we have
2634 * to check that all registers involved are type E/Rxx.
2636 opflags_t sok
= BITS32
| BITS64
;
2637 int32_t o
= input
->offset
;
2640 if (!(REG64
& ~ix
) || !(REG32
& ~ix
))
2648 goto err
; /* Invalid register */
2649 if (~sok
& bx
& SIZE_MASK
)
2650 goto err
; /* Invalid size */
2655 * While we're here, ensure the user didn't specify
2658 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2661 if (addrbits
== 16 ||
2662 (addrbits
== 32 && !(sok
& BITS32
)) ||
2663 (addrbits
== 64 && !(sok
& BITS64
)))
2666 /* now reorganize base/index */
2667 if (s
== 1 && bt
!= it
&& bt
!= -1 && it
!= -1 &&
2668 ((hb
== b
&& ht
== EAH_NOTBASE
) ||
2669 (hb
== i
&& ht
== EAH_MAKEBASE
))) {
2670 /* swap if hints say so */
2671 t
= bt
, bt
= it
, it
= t
;
2672 x
= bx
, bx
= ix
, ix
= x
;
2675 if (bt
== -1 && s
== 1 && !(hb
== i
&& ht
== EAH_NOTBASE
)) {
2676 /* make single reg base, unless hint */
2677 bt
= it
, bx
= ix
, it
= -1, ix
= 0;
2679 if (eaflags
& EAF_MIB
) {
2680 /* only for mib operands */
2681 if (it
== -1 && (hb
== b
&& ht
== EAH_NOTBASE
)) {
2683 * make a single reg index [reg*1].
2684 * gas uses this form for an explicit index register.
2686 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2688 if ((ht
== EAH_SUMMED
) && bt
== -1) {
2689 /* separate once summed index into [base, index] */
2690 bt
= it
, bx
= ix
, s
--;
2693 if (((s
== 2 && it
!= REG_NUM_ESP
&&
2694 (!(eaflags
& EAF_TIMESTWO
) || (ht
== EAH_SUMMED
))) ||
2695 s
== 3 || s
== 5 || s
== 9) && bt
== -1) {
2696 /* convert 3*EAX to EAX+2*EAX */
2697 bt
= it
, bx
= ix
, s
--;
2699 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
&&
2700 (eaflags
& EAF_TIMESTWO
) &&
2701 (hb
== b
&& ht
== EAH_NOTBASE
)) {
2703 * convert [NOSPLIT EAX*1]
2704 * to sib format with 0x0 displacement - [EAX*1+0].
2706 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2709 if (s
== 1 && it
== REG_NUM_ESP
) {
2710 /* swap ESP into base if scale is 1 */
2711 t
= it
, it
= bt
, bt
= t
;
2712 x
= ix
, ix
= bx
, bx
= x
;
2714 if (it
== REG_NUM_ESP
||
2715 (s
!= 1 && s
!= 2 && s
!= 4 && s
!= 8 && it
!= -1))
2716 goto err
; /* wrong, for various reasons */
2718 output
->rex
|= rexflags(it
, ix
, REX_X
);
2719 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2721 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
) {
2730 if (rm
!= REG_NUM_EBP
&& o
== 0 &&
2731 seg
== NO_SEG
&& !forw_ref
&&
2732 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2734 else if (IS_MOD_01())
2740 output
->sib_present
= false;
2741 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2742 output
->modrm
= GEN_MODRM(mod
, rfield
, rm
);
2745 int mod
, scale
, index
, base
;
2765 default: /* then what the smeg is it? */
2766 goto err
; /* panic */
2774 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2775 seg
== NO_SEG
&& !forw_ref
&&
2776 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2778 else if (IS_MOD_01())
2784 output
->sib_present
= true;
2785 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2786 output
->modrm
= GEN_MODRM(mod
, rfield
, 4);
2787 output
->sib
= GEN_SIB(scale
, index
, base
);
2789 } else { /* it's 16-bit */
2791 int16_t o
= input
->offset
;
2793 /* check for 64-bit long mode */
2797 /* check all registers are BX, BP, SI or DI */
2798 if ((b
!= -1 && b
!= R_BP
&& b
!= R_BX
&& b
!= R_SI
&& b
!= R_DI
) ||
2799 (i
!= -1 && i
!= R_BP
&& i
!= R_BX
&& i
!= R_SI
&& i
!= R_DI
))
2802 /* ensure the user didn't specify DWORD/QWORD */
2803 if (input
->disp_size
== 32 || input
->disp_size
== 64)
2806 if (s
!= 1 && i
!= -1)
2807 goto err
; /* no can do, in 16-bit EA */
2808 if (b
== -1 && i
!= -1) {
2813 if ((b
== R_SI
|| b
== R_DI
) && i
!= -1) {
2818 /* have BX/BP as base, SI/DI index */
2820 goto err
; /* shouldn't ever happen, in theory */
2821 if (i
!= -1 && b
!= -1 &&
2822 (i
== R_BP
|| i
== R_BX
|| b
== R_SI
|| b
== R_DI
))
2823 goto err
; /* invalid combinations */
2824 if (b
== -1) /* pure offset: handled above */
2825 goto err
; /* so if it gets to here, panic! */
2829 switch (i
* 256 + b
) {
2830 case R_SI
* 256 + R_BX
:
2833 case R_DI
* 256 + R_BX
:
2836 case R_SI
* 256 + R_BP
:
2839 case R_DI
* 256 + R_BP
:
2857 if (rm
== -1) /* can't happen, in theory */
2858 goto err
; /* so panic if it does */
2860 if (o
== 0 && seg
== NO_SEG
&& !forw_ref
&& rm
!= 6 &&
2861 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2863 else if (IS_MOD_01())
2868 output
->sib_present
= false; /* no SIB - it's 16-bit */
2869 output
->bytes
= mod
; /* bytes of offset needed */
2870 output
->modrm
= GEN_MODRM(mod
, rfield
, rm
);
2875 output
->size
= 1 + output
->sib_present
+ output
->bytes
;
2876 return output
->type
;
2879 return output
->type
= EA_INVALID
;
2882 static void add_asp(insn
*ins
, int addrbits
)
2887 valid
= (addrbits
== 64) ? 64|32 : 32|16;
2889 switch (ins
->prefixes
[PPS_ASIZE
]) {
2900 valid
&= (addrbits
== 32) ? 16 : 32;
2906 for (j
= 0; j
< ins
->operands
; j
++) {
2907 if (is_class(MEMORY
, ins
->oprs
[j
].type
)) {
2910 /* Verify as Register */
2911 if (!is_register(ins
->oprs
[j
].indexreg
))
2914 i
= nasm_reg_flags
[ins
->oprs
[j
].indexreg
];
2916 /* Verify as Register */
2917 if (!is_register(ins
->oprs
[j
].basereg
))
2920 b
= nasm_reg_flags
[ins
->oprs
[j
].basereg
];
2922 if (ins
->oprs
[j
].scale
== 0)
2926 int ds
= ins
->oprs
[j
].disp_size
;
2927 if ((addrbits
!= 64 && ds
> 8) ||
2928 (addrbits
== 64 && ds
== 16))
2948 if (valid
& addrbits
) {
2949 ins
->addr_size
= addrbits
;
2950 } else if (valid
& ((addrbits
== 32) ? 16 : 32)) {
2951 /* Add an address size prefix */
2952 ins
->prefixes
[PPS_ASIZE
] = (addrbits
== 32) ? P_A16
: P_A32
;;
2953 ins
->addr_size
= (addrbits
== 32) ? 16 : 32;
2956 nasm_error(ERR_NONFATAL
, "impossible combination of address sizes");
2957 ins
->addr_size
= addrbits
; /* Error recovery */
2960 defdisp
= ins
->addr_size
== 16 ? 16 : 32;
2962 for (j
= 0; j
< ins
->operands
; j
++) {
2963 if (!(MEM_OFFS
& ~ins
->oprs
[j
].type
) &&
2964 (ins
->oprs
[j
].disp_size
? ins
->oprs
[j
].disp_size
: defdisp
) != ins
->addr_size
) {
2966 * mem_offs sizes must match the address size; if not,
2967 * strip the MEM_OFFS bit and match only EA instructions
2969 ins
->oprs
[j
].type
&= ~(MEM_OFFS
& ~MEMORY
);