1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2016 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 m is the legacy escape (0f, 0f38, 0f3a)
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"
183 #include <inttypes.h>
187 #include "assemble.h"
194 * Matching errors. These should be sorted so that more specific
195 * errors come later in the sequence.
208 * Matching success; the conditional ones first
210 MOK_JUMP
, /* Matching OK but needs jmp_match() */
211 MOK_GOOD
/* Matching unconditionally OK */
215 enum ea_type type
; /* what kind of EA is this? */
216 int sib_present
; /* is a SIB byte necessary? */
217 int bytes
; /* # of bytes of offset needed */
218 int size
; /* lazy - this is sib+bytes+1 */
219 uint8_t modrm
, sib
, rex
, rip
; /* the bytes themselves */
220 int8_t disp8
; /* compressed displacement for EVEX */
223 #define GEN_SIB(scale, index, base) \
224 (((scale) << 6) | ((index) << 3) | ((base)))
226 #define GEN_MODRM(mod, reg, rm) \
227 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
229 static iflag_t cpu
; /* cpu level received from nasm.c */
231 static int64_t calcsize(int32_t, int64_t, int, insn
*,
232 const struct itemplate
*);
233 static void gencode(int32_t segment
, int64_t offset
, int bits
,
234 insn
* ins
, const struct itemplate
*temp
,
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, opflags_t
, insn
*);
249 static int has_prefix(insn
* ins
, enum prefix_pos pos
, int prefix
)
251 return ins
->prefixes
[pos
] == prefix
;
254 static void assert_no_prefix(insn
* ins
, enum prefix_pos pos
)
256 if (ins
->prefixes
[pos
])
257 nasm_error(ERR_NONFATAL
, "invalid %s prefix",
258 prefix_name(ins
->prefixes
[pos
]));
261 static const char *size_name(int size
)
285 static void warn_overflow(int pass
, int size
)
287 nasm_error(ERR_WARNING
| pass
| ERR_WARN_NOV
,
288 "%s data exceeds bounds", size_name(size
));
291 static void warn_overflow_const(int64_t data
, int size
)
293 if (overflow_general(data
, size
))
294 warn_overflow(ERR_PASS1
, size
);
297 static void warn_overflow_opd(const struct operand
*o
, int size
)
299 if (o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
) {
300 if (overflow_general(o
->offset
, size
))
301 warn_overflow(ERR_PASS2
, size
);
306 * Size of an address relocation, or zero if not an address
308 static int addrsize(enum out_type type
, uint64_t size
)
312 return abs((int)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.
332 static void out(int64_t offset
, int32_t segto
, const void *data
,
333 enum out_type type
, uint64_t size
,
334 int32_t segment
, int32_t wrt
)
336 static int32_t lineno
= 0; /* static!!! */
337 static char *lnfname
= NULL
;
339 int asize
= addrsize(type
, size
); /* Address size in bytes */
340 const int amax
= ofmt
->maxbits
>> 3; /* Maximum address size in bytes */
342 if (type
== OUT_ADDRESS
&& segment
== NO_SEG
&& wrt
== NO_SEG
) {
344 * This is a non-relocated address, and we're going to
345 * convert it into RAWDATA format.
350 nasm_panic(0, "OUT_ADDRESS with size > 8");
354 WRITEADDR(q
, *(int64_t *)data
, asize
);
358 asize
= 0; /* No longer an address */
361 nasmlist
->output(offset
, data
, type
, size
);
364 * this call to src_get determines when we call the
365 * debug-format-specific "linenum" function
366 * it updates lineno and lnfname to the current values
367 * returning 0 if "same as last time", -2 if lnfname
368 * changed, and the amount by which lineno changed,
369 * if it did. thus, these variables must be static
372 if (src_get(&lineno
, &lnfname
))
373 ofmt
->current_dfmt
->linenum(lnfname
, lineno
, segto
);
375 if (asize
&& asize
> amax
) {
376 if (type
!= OUT_ADDRESS
|| (int)size
< 0) {
377 nasm_error(ERR_NONFATAL
,
378 "%d-bit signed relocation unsupported by output format %s\n",
379 asize
<< 3, ofmt
->shortname
);
382 nasm_error(ERR_WARNING
| ERR_WARN_ZEXTRELOC
,
383 "%d-bit unsigned relocation zero-extended from %d bits\n",
384 asize
<< 3, ofmt
->maxbits
);
385 ofmt
->output(segto
, data
, type
, amax
, segment
, wrt
);
390 segment
= wrt
= NO_SEG
;
393 ofmt
->output(segto
, data
, type
, size
, segment
, wrt
);
396 static void out_imm8(int64_t offset
, int32_t segment
,
397 struct operand
*opx
, int asize
)
399 if (opx
->segment
!= NO_SEG
) {
400 uint64_t data
= opx
->offset
;
401 out(offset
, segment
, &data
, OUT_ADDRESS
, asize
, opx
->segment
, opx
->wrt
);
403 uint8_t byte
= opx
->offset
;
404 out(offset
, segment
, &byte
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
408 static bool jmp_match(int32_t segment
, int64_t offset
, int bits
,
409 insn
* ins
, const struct itemplate
*temp
)
412 const uint8_t *code
= temp
->code
;
416 if (((c
& ~1) != 0370) || (ins
->oprs
[0].type
& STRICT
))
420 if (optimizing
< 0 && c
== 0371)
423 isize
= calcsize(segment
, offset
, bits
, ins
, temp
);
425 if (ins
->oprs
[0].opflags
& OPFLAG_UNKNOWN
)
426 /* Be optimistic in pass 1 */
429 if (ins
->oprs
[0].segment
!= segment
)
432 isize
= ins
->oprs
[0].offset
- offset
- isize
; /* isize is delta */
433 is_byte
= (isize
>= -128 && isize
<= 127); /* is it byte size? */
435 if (is_byte
&& c
== 0371 && ins
->prefixes
[PPS_REP
] == P_BND
) {
436 /* jmp short (opcode eb) cannot be used with bnd prefix. */
437 ins
->prefixes
[PPS_REP
] = P_none
;
438 nasm_error(ERR_WARNING
| ERR_WARN_BND
| ERR_PASS2
,
439 "jmp short does not init bnd regs - bnd prefix dropped.");
445 int64_t assemble(int32_t segment
, int64_t offset
, int bits
, iflag_t cp
,
448 const struct itemplate
*temp
;
453 int64_t start
= offset
;
454 int64_t wsize
; /* size for DB etc. */
458 wsize
= idata_bytes(instruction
->opcode
);
464 int32_t t
= instruction
->times
;
466 nasm_panic(0, "instruction->times < 0 (%"PRId32
") in assemble()", t
);
468 while (t
--) { /* repeat TIMES times */
469 list_for_each(e
, instruction
->eops
) {
470 if (e
->type
== EOT_DB_NUMBER
) {
472 nasm_error(ERR_NONFATAL
,
473 "integer supplied to a DT, DO or DY"
476 out(offset
, segment
, &e
->offset
,
477 OUT_ADDRESS
, wsize
, e
->segment
, e
->wrt
);
480 } else if (e
->type
== EOT_DB_STRING
||
481 e
->type
== EOT_DB_STRING_FREE
) {
484 out(offset
, segment
, e
->stringval
,
485 OUT_RAWDATA
, e
->stringlen
, NO_SEG
, NO_SEG
);
486 align
= e
->stringlen
% wsize
;
489 align
= wsize
- align
;
490 out(offset
, segment
, zero_buffer
,
491 OUT_RAWDATA
, align
, NO_SEG
, NO_SEG
);
493 offset
+= e
->stringlen
+ align
;
496 if (t
> 0 && t
== instruction
->times
- 1) {
498 * Dummy call to nasmlist->output to give the offset to the
501 nasmlist
->output(offset
, NULL
, OUT_RAWDATA
, 0);
502 nasmlist
->uplevel(LIST_TIMES
);
505 if (instruction
->times
> 1)
506 nasmlist
->downlevel(LIST_TIMES
);
507 return offset
- start
;
510 if (instruction
->opcode
== I_INCBIN
) {
511 const char *fname
= instruction
->eops
->stringval
;
514 fp
= fopen(fname
, "rb");
516 nasm_error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
518 } else if (fseek(fp
, 0L, SEEK_END
) < 0) {
519 nasm_error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
523 static char buf
[4096];
524 size_t t
= instruction
->times
;
529 if (instruction
->eops
->next
) {
530 base
= instruction
->eops
->next
->offset
;
532 if (instruction
->eops
->next
->next
&&
533 len
> (size_t)instruction
->eops
->next
->next
->offset
)
534 len
= (size_t)instruction
->eops
->next
->next
->offset
;
537 * Dummy call to nasmlist->output to give the offset to the
540 nasmlist
->output(offset
, NULL
, OUT_RAWDATA
, 0);
541 nasmlist
->uplevel(LIST_INCBIN
);
545 fseek(fp
, base
, SEEK_SET
);
549 m
= fread(buf
, 1, l
> sizeof(buf
) ? sizeof(buf
) : l
, fp
);
552 * This shouldn't happen unless the file
553 * actually changes while we are reading
556 nasm_error(ERR_NONFATAL
,
557 "`incbin': unexpected EOF while"
558 " reading file `%s'", fname
);
559 t
= 0; /* Try to exit cleanly */
562 out(offset
, segment
, buf
, OUT_RAWDATA
, m
,
567 nasmlist
->downlevel(LIST_INCBIN
);
568 if (instruction
->times
> 1) {
570 * Dummy call to nasmlist->output to give the offset to the
573 nasmlist
->output(offset
, NULL
, OUT_RAWDATA
, 0);
574 nasmlist
->uplevel(LIST_TIMES
);
575 nasmlist
->downlevel(LIST_TIMES
);
578 return instruction
->times
* len
;
580 return 0; /* if we're here, there's an error */
583 /* Check to see if we need an address-size prefix */
584 add_asp(instruction
, bits
);
586 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
590 int64_t insn_size
= calcsize(segment
, offset
, bits
, instruction
, temp
);
591 itimes
= instruction
->times
;
592 if (insn_size
< 0) /* shouldn't be, on pass two */
593 nasm_panic(0, "errors made it through from pass one");
596 for (j
= 0; j
< MAXPREFIX
; j
++) {
598 switch (instruction
->prefixes
[j
]) {
619 nasm_error(ERR_WARNING
| ERR_PASS2
,
620 "cs segment base generated, but will be ignored in 64-bit mode");
626 nasm_error(ERR_WARNING
| ERR_PASS2
,
627 "ds segment base generated, but will be ignored in 64-bit mode");
633 nasm_error(ERR_WARNING
| ERR_PASS2
,
634 "es segment base generated, but will be ignored in 64-bit mode");
646 nasm_error(ERR_WARNING
| ERR_PASS2
,
647 "ss segment base generated, but will be ignored in 64-bit mode");
653 nasm_error(ERR_NONFATAL
,
654 "segr6 and segr7 cannot be used as prefixes");
658 nasm_error(ERR_NONFATAL
,
659 "16-bit addressing is not supported "
661 } else if (bits
!= 16)
670 nasm_error(ERR_NONFATAL
,
671 "64-bit addressing is only supported "
699 nasm_panic(0, "invalid instruction prefix");
702 out(offset
, segment
, &c
, OUT_RAWDATA
, 1,
707 insn_end
= offset
+ insn_size
;
708 gencode(segment
, offset
, bits
, instruction
,
711 if (itimes
> 0 && itimes
== instruction
->times
- 1) {
713 * Dummy call to nasmlist->output to give the offset to the
716 nasmlist
->output(offset
, NULL
, OUT_RAWDATA
, 0);
717 nasmlist
->uplevel(LIST_TIMES
);
720 if (instruction
->times
> 1)
721 nasmlist
->downlevel(LIST_TIMES
);
722 return offset
- start
;
726 case MERR_OPSIZEMISSING
:
727 nasm_error(ERR_NONFATAL
, "operation size not specified");
729 case MERR_OPSIZEMISMATCH
:
730 nasm_error(ERR_NONFATAL
, "mismatch in operand sizes");
732 case MERR_BRNUMMISMATCH
:
733 nasm_error(ERR_NONFATAL
,
734 "mismatch in the number of broadcasting elements");
737 nasm_error(ERR_NONFATAL
, "no instruction for this cpu level");
740 nasm_error(ERR_NONFATAL
, "instruction not supported in %d-bit mode",
743 case MERR_ENCMISMATCH
:
744 nasm_error(ERR_NONFATAL
, "specific encoding scheme not available");
747 nasm_error(ERR_NONFATAL
, "bnd prefix is not allowed");
750 nasm_error(ERR_NONFATAL
, "%s prefix is not allowed",
751 (has_prefix(instruction
, PPS_REP
, P_REPNE
) ?
755 nasm_error(ERR_NONFATAL
,
756 "invalid combination of opcode and operands");
763 int64_t insn_size(int32_t segment
, int64_t offset
, int bits
, iflag_t cp
,
766 const struct itemplate
*temp
;
771 if (instruction
->opcode
== I_none
)
774 if (instruction
->opcode
== I_DB
|| instruction
->opcode
== I_DW
||
775 instruction
->opcode
== I_DD
|| instruction
->opcode
== I_DQ
||
776 instruction
->opcode
== I_DT
|| instruction
->opcode
== I_DO
||
777 instruction
->opcode
== I_DY
) {
779 int32_t isize
, osize
, wsize
;
782 wsize
= idata_bytes(instruction
->opcode
);
784 list_for_each(e
, instruction
->eops
) {
788 if (e
->type
== EOT_DB_NUMBER
) {
790 warn_overflow_const(e
->offset
, wsize
);
791 } else if (e
->type
== EOT_DB_STRING
||
792 e
->type
== EOT_DB_STRING_FREE
)
793 osize
= e
->stringlen
;
795 align
= (-osize
) % wsize
;
798 isize
+= osize
+ align
;
800 return isize
* instruction
->times
;
803 if (instruction
->opcode
== I_INCBIN
) {
804 const char *fname
= instruction
->eops
->stringval
;
809 fp
= fopen(fname
, "rb");
811 nasm_error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
813 else if (fseek(fp
, 0L, SEEK_END
) < 0)
814 nasm_error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
818 if (instruction
->eops
->next
) {
819 len
-= instruction
->eops
->next
->offset
;
820 if (instruction
->eops
->next
->next
&&
821 len
> (size_t)instruction
->eops
->next
->next
->offset
) {
822 len
= (size_t)instruction
->eops
->next
->next
->offset
;
825 val
= instruction
->times
* len
;
832 /* Check to see if we need an address-size prefix */
833 add_asp(instruction
, bits
);
835 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
837 /* we've matched an instruction. */
841 isize
= calcsize(segment
, offset
, bits
, instruction
, temp
);
844 for (j
= 0; j
< MAXPREFIX
; j
++) {
845 switch (instruction
->prefixes
[j
]) {
875 return isize
* instruction
->times
;
877 return -1; /* didn't match any instruction */
881 static void bad_hle_warn(const insn
* ins
, uint8_t hleok
)
883 enum prefixes rep_pfx
= ins
->prefixes
[PPS_REP
];
884 enum whatwarn
{ w_none
, w_lock
, w_inval
} ww
;
885 static const enum whatwarn warn
[2][4] =
887 { w_inval
, w_inval
, w_none
, w_lock
}, /* XACQUIRE */
888 { w_inval
, w_none
, w_none
, w_lock
}, /* XRELEASE */
892 n
= (unsigned int)rep_pfx
- P_XACQUIRE
;
894 return; /* Not XACQUIRE/XRELEASE */
897 if (!is_class(MEMORY
, ins
->oprs
[0].type
))
898 ww
= w_inval
; /* HLE requires operand 0 to be memory */
905 if (ins
->prefixes
[PPS_LOCK
] != P_LOCK
) {
906 nasm_error(ERR_WARNING
| ERR_WARN_HLE
| ERR_PASS2
,
907 "%s with this instruction requires lock",
908 prefix_name(rep_pfx
));
913 nasm_error(ERR_WARNING
| ERR_WARN_HLE
| ERR_PASS2
,
914 "%s invalid with this instruction",
915 prefix_name(rep_pfx
));
920 /* Common construct */
921 #define case3(x) case (x): case (x)+1: case (x)+2
922 #define case4(x) case3(x): case (x)+3
924 static int64_t calcsize(int32_t segment
, int64_t offset
, int bits
,
925 insn
* ins
, const struct itemplate
*temp
)
927 const uint8_t *codes
= temp
->code
;
936 bool lockcheck
= true;
937 enum reg_enum mib_index
= R_none
; /* For a separate index MIB reg form */
939 ins
->rex
= 0; /* Ensure REX is reset */
940 eat
= EA_SCALAR
; /* Expect a scalar EA */
941 memset(ins
->evex_p
, 0, 3); /* Ensure EVEX is reset */
943 if (ins
->prefixes
[PPS_OSIZE
] == P_O64
)
946 (void)segment
; /* Don't warn that this parameter is unused */
947 (void)offset
; /* Don't warn that this parameter is unused */
951 op1
= (c
& 3) + ((opex
& 1) << 2);
952 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
953 opx
= &ins
->oprs
[op1
];
954 opex
= 0; /* For the next iteration */
958 codes
+= c
, length
+= c
;
967 op_rexflags(opx
, REX_B
|REX_H
|REX_P
|REX_W
);
972 /* this is an index reg of MIB operand */
973 mib_index
= opx
->basereg
;
986 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
987 length
+= (opx
->type
& BITS16
) ? 2 : 4;
989 length
+= (bits
== 16) ? 2 : 4;
997 length
+= ins
->addr_size
>> 3;
1005 length
+= 8; /* MOV reg64/imm */
1013 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1014 length
+= (opx
->type
& BITS16
) ? 2 : 4;
1016 length
+= (bits
== 16) ? 2 : 4;
1039 ins
->vexreg
= regval(opx
);
1040 ins
->evex_p
[2] |= op_evexflags(opx
, EVEX_P2VP
, 2); /* High-16 NDS */
1041 ins
->vex_cm
= *codes
++;
1042 ins
->vex_wlp
= *codes
++;
1043 ins
->evex_tuple
= (*codes
++ - 0300);
1049 ins
->vex_cm
= *codes
++;
1050 ins
->vex_wlp
= *codes
++;
1051 ins
->evex_tuple
= (*codes
++ - 0300);
1060 ins
->vexreg
= regval(opx
);
1061 ins
->vex_cm
= *codes
++;
1062 ins
->vex_wlp
= *codes
++;
1068 ins
->vex_cm
= *codes
++;
1069 ins
->vex_wlp
= *codes
++;
1086 length
+= (bits
!= 16) && !has_prefix(ins
, PPS_ASIZE
, P_A16
);
1090 length
+= (bits
!= 32) && !has_prefix(ins
, PPS_ASIZE
, P_A32
);
1097 if (bits
!= 64 || has_prefix(ins
, PPS_ASIZE
, P_A16
) ||
1098 has_prefix(ins
, PPS_ASIZE
, P_A32
))
1107 enum prefixes pfx
= ins
->prefixes
[PPS_OSIZE
];
1111 nasm_error(ERR_WARNING
| ERR_PASS2
, "invalid operand size prefix");
1113 ins
->prefixes
[PPS_OSIZE
] = P_O16
;
1119 enum prefixes pfx
= ins
->prefixes
[PPS_OSIZE
];
1123 nasm_error(ERR_WARNING
| ERR_PASS2
, "invalid operand size prefix");
1125 ins
->prefixes
[PPS_OSIZE
] = P_O32
;
1167 if (!ins
->prefixes
[PPS_REP
])
1168 ins
->prefixes
[PPS_REP
] = P_REP
;
1172 if (!ins
->prefixes
[PPS_REP
])
1173 ins
->prefixes
[PPS_REP
] = P_REPNE
;
1177 if (ins
->oprs
[0].segment
!= NO_SEG
)
1178 nasm_error(ERR_NONFATAL
, "attempt to reserve non-constant"
1179 " quantity of BSS space");
1181 length
+= ins
->oprs
[0].offset
;
1185 if (!ins
->prefixes
[PPS_WAIT
])
1186 ins
->prefixes
[PPS_WAIT
] = P_WAIT
;
1241 struct operand
*opy
= &ins
->oprs
[op2
];
1242 struct operand
*op_er_sae
;
1244 ea_data
.rex
= 0; /* Ensure ea.REX is initially 0 */
1247 /* pick rfield from operand b (opx) */
1248 rflags
= regflag(opx
);
1249 rfield
= nasm_regvals
[opx
->basereg
];
1255 /* EVEX.b1 : evex_brerop contains the operand position */
1256 op_er_sae
= (ins
->evex_brerop
>= 0 ?
1257 &ins
->oprs
[ins
->evex_brerop
] : NULL
);
1259 if (op_er_sae
&& (op_er_sae
->decoflags
& (ER
| SAE
))) {
1261 ins
->evex_p
[2] |= EVEX_P2B
;
1262 if (op_er_sae
->decoflags
& ER
) {
1263 /* set EVEX.RC (rounding control) */
1264 ins
->evex_p
[2] |= ((ins
->evex_rm
- BRC_RN
) << 5)
1268 /* set EVEX.L'L (vector length) */
1269 ins
->evex_p
[2] |= ((ins
->vex_wlp
<< (5 - 2)) & EVEX_P2LL
);
1270 ins
->evex_p
[1] |= ((ins
->vex_wlp
<< (7 - 4)) & EVEX_P1W
);
1271 if (opy
->decoflags
& BRDCAST_MASK
) {
1273 ins
->evex_p
[2] |= EVEX_P2B
;
1277 if (itemp_has(temp
, IF_MIB
)) {
1278 opy
->eaflags
|= EAF_MIB
;
1280 * if a separate form of MIB (ICC style) is used,
1281 * the index reg info is merged into mem operand
1283 if (mib_index
!= R_none
) {
1284 opy
->indexreg
= mib_index
;
1286 opy
->hintbase
= mib_index
;
1287 opy
->hinttype
= EAH_NOTBASE
;
1291 if (process_ea(opy
, &ea_data
, bits
,
1292 rfield
, rflags
, ins
) != eat
) {
1293 nasm_error(ERR_NONFATAL
, "invalid effective address");
1296 ins
->rex
|= ea_data
.rex
;
1297 length
+= ea_data
.size
;
1303 nasm_panic(0, "internal instruction table corrupt"
1304 ": instruction code \\%o (0x%02X) given", c
, c
);
1309 ins
->rex
&= rex_mask
;
1311 if (ins
->rex
& REX_NH
) {
1312 if (ins
->rex
& REX_H
) {
1313 nasm_error(ERR_NONFATAL
, "instruction cannot use high registers");
1316 ins
->rex
&= ~REX_P
; /* Don't force REX prefix due to high reg */
1319 switch (ins
->prefixes
[PPS_VEX
]) {
1321 if (!(ins
->rex
& REX_EV
))
1326 if (!(ins
->rex
& REX_V
))
1333 if (ins
->rex
& (REX_V
| REX_EV
)) {
1334 int bad32
= REX_R
|REX_W
|REX_X
|REX_B
;
1336 if (ins
->rex
& REX_H
) {
1337 nasm_error(ERR_NONFATAL
, "cannot use high register in AVX instruction");
1340 switch (ins
->vex_wlp
& 060) {
1354 if (bits
!= 64 && ((ins
->rex
& bad32
) || ins
->vexreg
> 7)) {
1355 nasm_error(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1357 } else if (!(ins
->rex
& REX_EV
) &&
1358 ((ins
->vexreg
> 15) || (ins
->evex_p
[0] & 0xf0))) {
1359 nasm_error(ERR_NONFATAL
, "invalid high-16 register in non-AVX-512");
1362 if (ins
->rex
& REX_EV
)
1364 else if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
)) ||
1365 ins
->prefixes
[PPS_VEX
] == P_VEX3
)
1369 } else if (ins
->rex
& REX_MASK
) {
1370 if (ins
->rex
& REX_H
) {
1371 nasm_error(ERR_NONFATAL
, "cannot use high register in rex instruction");
1373 } else if (bits
== 64) {
1375 } else if ((ins
->rex
& REX_L
) &&
1376 !(ins
->rex
& (REX_P
|REX_W
|REX_X
|REX_B
)) &&
1377 iflag_ffs(&cpu
) >= IF_X86_64
) {
1379 assert_no_prefix(ins
, PPS_LOCK
);
1380 lockcheck
= false; /* Already errored, no need for warning */
1383 nasm_error(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1388 if (has_prefix(ins
, PPS_LOCK
, P_LOCK
) && lockcheck
&&
1389 (!itemp_has(temp
,IF_LOCK
) || !is_class(MEMORY
, ins
->oprs
[0].type
))) {
1390 nasm_error(ERR_WARNING
| ERR_WARN_LOCK
| ERR_PASS2
,
1391 "instruction is not lockable");
1394 bad_hle_warn(ins
, hleok
);
1397 * when BND prefix is set by DEFAULT directive,
1398 * BND prefix is added to every appropriate instruction line
1399 * unless it is overridden by NOBND prefix.
1402 (itemp_has(temp
, IF_BND
) && !has_prefix(ins
, PPS_REP
, P_NOBND
)))
1403 ins
->prefixes
[PPS_REP
] = P_BND
;
1408 static inline unsigned int emit_rex(insn
*ins
, int32_t segment
, int64_t offset
, int bits
)
1411 if ((ins
->rex
& REX_MASK
) &&
1412 !(ins
->rex
& (REX_V
| REX_EV
)) &&
1414 int rex
= (ins
->rex
& REX_MASK
) | REX_P
;
1415 out(offset
, segment
, &rex
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1416 ins
->rex_done
= true;
1424 static void gencode(int32_t segment
, int64_t offset
, int bits
,
1425 insn
* ins
, const struct itemplate
*temp
,
1433 struct operand
*opx
;
1434 const uint8_t *codes
= temp
->code
;
1436 enum ea_type eat
= EA_SCALAR
;
1438 ins
->rex_done
= false;
1442 op1
= (c
& 3) + ((opex
& 1) << 2);
1443 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
1444 opx
= &ins
->oprs
[op1
];
1445 opex
= 0; /* For the next iteration */
1452 offset
+= emit_rex(ins
, segment
, offset
, bits
);
1453 out(offset
, segment
, codes
, OUT_RAWDATA
, c
, NO_SEG
, NO_SEG
);
1465 offset
+= emit_rex(ins
, segment
, offset
, bits
);
1466 bytes
[0] = *codes
++ + (regval(opx
) & 7);
1467 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1475 if (opx
->offset
< -256 || opx
->offset
> 255) {
1476 nasm_error(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1477 "byte value exceeds bounds");
1479 out_imm8(offset
, segment
, opx
, -1);
1484 if (opx
->offset
< 0 || opx
->offset
> 255)
1485 nasm_error(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1486 "unsigned byte value exceeds bounds");
1487 out_imm8(offset
, segment
, opx
, 1);
1492 warn_overflow_opd(opx
, 2);
1494 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1495 opx
->segment
, opx
->wrt
);
1500 if (opx
->type
& (BITS16
| BITS32
))
1501 size
= (opx
->type
& BITS16
) ? 2 : 4;
1503 size
= (bits
== 16) ? 2 : 4;
1504 warn_overflow_opd(opx
, size
);
1506 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1507 opx
->segment
, opx
->wrt
);
1512 warn_overflow_opd(opx
, 4);
1514 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1515 opx
->segment
, opx
->wrt
);
1521 size
= ins
->addr_size
>> 3;
1522 warn_overflow_opd(opx
, size
);
1523 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1524 opx
->segment
, opx
->wrt
);
1529 if (opx
->segment
!= segment
) {
1531 out(offset
, segment
, &data
,
1532 OUT_REL1ADR
, insn_end
- offset
,
1533 opx
->segment
, opx
->wrt
);
1535 data
= opx
->offset
- insn_end
;
1536 if (data
> 127 || data
< -128)
1537 nasm_error(ERR_NONFATAL
, "short jump is out of range");
1538 out(offset
, segment
, &data
,
1539 OUT_ADDRESS
, 1, NO_SEG
, NO_SEG
);
1545 data
= (int64_t)opx
->offset
;
1546 out(offset
, segment
, &data
, OUT_ADDRESS
, 8,
1547 opx
->segment
, opx
->wrt
);
1552 if (opx
->segment
!= segment
) {
1554 out(offset
, segment
, &data
,
1555 OUT_REL2ADR
, insn_end
- offset
,
1556 opx
->segment
, opx
->wrt
);
1558 data
= opx
->offset
- insn_end
;
1559 out(offset
, segment
, &data
,
1560 OUT_ADDRESS
, 2, NO_SEG
, NO_SEG
);
1566 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1567 size
= (opx
->type
& BITS16
) ? 2 : 4;
1569 size
= (bits
== 16) ? 2 : 4;
1570 if (opx
->segment
!= segment
) {
1572 out(offset
, segment
, &data
,
1573 size
== 2 ? OUT_REL2ADR
: OUT_REL4ADR
,
1574 insn_end
- offset
, opx
->segment
, opx
->wrt
);
1576 data
= opx
->offset
- insn_end
;
1577 out(offset
, segment
, &data
,
1578 OUT_ADDRESS
, size
, NO_SEG
, NO_SEG
);
1584 if (opx
->segment
!= segment
) {
1586 out(offset
, segment
, &data
,
1587 OUT_REL4ADR
, insn_end
- offset
,
1588 opx
->segment
, opx
->wrt
);
1590 data
= opx
->offset
- insn_end
;
1591 out(offset
, segment
, &data
,
1592 OUT_ADDRESS
, 4, NO_SEG
, NO_SEG
);
1598 if (opx
->segment
== NO_SEG
)
1599 nasm_error(ERR_NONFATAL
, "value referenced by FAR is not"
1602 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1603 ofmt
->segbase(1 + opx
->segment
),
1610 opx
= &ins
->oprs
[c
>> 3];
1611 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1612 opx
= &ins
->oprs
[c
& 7];
1613 if (opx
->segment
!= NO_SEG
|| opx
->wrt
!= NO_SEG
) {
1614 nasm_error(ERR_NONFATAL
,
1615 "non-absolute expression not permitted as argument %d",
1618 if (opx
->offset
& ~15) {
1619 nasm_error(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1620 "four-bit argument exceeds bounds");
1622 bytes
[0] |= opx
->offset
& 15;
1624 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1630 opx
= &ins
->oprs
[c
>> 4];
1631 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1633 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1638 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1639 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1645 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1646 (int32_t)data
!= (int64_t)data
) {
1647 nasm_error(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1648 "signed dword immediate exceeds bounds");
1650 out(offset
, segment
, &data
, OUT_ADDRESS
, -4,
1651 opx
->segment
, opx
->wrt
);
1658 ins
->evex_p
[2] |= op_evexflags(&ins
->oprs
[0],
1659 EVEX_P2Z
| EVEX_P2AAA
, 2);
1660 ins
->evex_p
[2] ^= EVEX_P2VP
; /* 1's complement */
1662 /* EVEX.X can be set by either REX or EVEX for different reasons */
1663 bytes
[1] = ((((ins
->rex
& 7) << 5) |
1664 (ins
->evex_p
[0] & (EVEX_P0X
| EVEX_P0RP
))) ^ 0xf0) |
1666 bytes
[2] = ((ins
->rex
& REX_W
) << (7 - 3)) |
1667 ((~ins
->vexreg
& 15) << 3) |
1668 (1 << 2) | (ins
->vex_wlp
& 3);
1669 bytes
[3] = ins
->evex_p
[2];
1670 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 4, NO_SEG
, NO_SEG
);
1677 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
)) ||
1678 ins
->prefixes
[PPS_VEX
] == P_VEX3
) {
1679 bytes
[0] = (ins
->vex_cm
>> 6) ? 0x8f : 0xc4;
1680 bytes
[1] = (ins
->vex_cm
& 31) | ((~ins
->rex
& 7) << 5);
1681 bytes
[2] = ((ins
->rex
& REX_W
) << (7-3)) |
1682 ((~ins
->vexreg
& 15)<< 3) | (ins
->vex_wlp
& 07);
1683 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 3, NO_SEG
, NO_SEG
);
1687 bytes
[1] = ((~ins
->rex
& REX_R
) << (7-2)) |
1688 ((~ins
->vexreg
& 15) << 3) | (ins
->vex_wlp
& 07);
1689 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 2, NO_SEG
, NO_SEG
);
1704 if (ins
->rex
& REX_W
)
1706 else if (ins
->prefixes
[PPS_OSIZE
] == P_O16
)
1708 else if (ins
->prefixes
[PPS_OSIZE
] == P_O32
)
1713 um
= (uint64_t)2 << (s
-1);
1716 if (uv
> 127 && uv
< (uint64_t)-128 &&
1717 (uv
< um
-128 || uv
> um
-1)) {
1718 /* If this wasn't explicitly byte-sized, warn as though we
1719 * had fallen through to the imm16/32/64 case.
1721 nasm_error(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1722 "%s value exceeds bounds",
1723 (opx
->type
& BITS8
) ? "signed byte" :
1728 if (opx
->segment
!= NO_SEG
) {
1730 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1731 opx
->segment
, opx
->wrt
);
1734 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1745 if (bits
== 32 && !has_prefix(ins
, PPS_ASIZE
, P_A16
)) {
1747 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1754 if (bits
!= 32 && !has_prefix(ins
, PPS_ASIZE
, P_A32
)) {
1756 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1791 *bytes
= *codes
++ ^ get_cond_opcode(ins
->condition
);
1792 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1801 *bytes
= c
- 0332 + 0xF2;
1802 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1807 if (ins
->rex
& REX_R
) {
1809 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1812 ins
->rex
&= ~(REX_L
|REX_R
);
1823 if (ins
->oprs
[0].segment
!= NO_SEG
)
1824 nasm_panic(0, "non-constant BSS size in pass two");
1826 int64_t size
= ins
->oprs
[0].offset
;
1828 out(offset
, segment
, NULL
,
1829 OUT_RESERVE
, size
, NO_SEG
, NO_SEG
);
1842 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1852 *bytes
= c
- 0366 + 0x66;
1853 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1861 *bytes
= bits
== 16 ? 3 : 5;
1862 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1896 struct operand
*opy
= &ins
->oprs
[op2
];
1899 /* pick rfield from operand b (opx) */
1900 rflags
= regflag(opx
);
1901 rfield
= nasm_regvals
[opx
->basereg
];
1903 /* rfield is constant */
1908 if (process_ea(opy
, &ea_data
, bits
,
1909 rfield
, rflags
, ins
) != eat
)
1910 nasm_error(ERR_NONFATAL
, "invalid effective address");
1913 *p
++ = ea_data
.modrm
;
1914 if (ea_data
.sib_present
)
1918 out(offset
, segment
, bytes
, OUT_RAWDATA
, s
, NO_SEG
, NO_SEG
);
1921 * Make sure the address gets the right offset in case
1922 * the line breaks in the .lst file (BR 1197827)
1927 if (ea_data
.bytes
) {
1928 /* use compressed displacement, if available */
1929 data
= ea_data
.disp8
? ea_data
.disp8
: opy
->offset
;
1932 if (opy
->segment
== segment
) {
1934 if (overflow_signed(data
, ea_data
.bytes
))
1935 warn_overflow(ERR_PASS2
, ea_data
.bytes
);
1936 out(offset
, segment
, &data
, OUT_ADDRESS
,
1937 ea_data
.bytes
, NO_SEG
, NO_SEG
);
1939 /* overflow check in output/linker? */
1940 out(offset
, segment
, &data
, OUT_REL4ADR
,
1941 insn_end
- offset
, opy
->segment
, opy
->wrt
);
1944 int asize
= ins
->addr_size
>> 3;
1945 int atype
= ea_data
.bytes
;
1947 if (overflow_general(data
, asize
) ||
1948 signed_bits(data
, ins
->addr_size
) !=
1949 signed_bits(data
, ea_data
.bytes
<< 3))
1950 warn_overflow(ERR_PASS2
, ea_data
.bytes
);
1952 if (asize
> ea_data
.bytes
) {
1954 * If the address isn't the full width of
1955 * the address size, treat is as signed...
1960 out(offset
, segment
, &data
, OUT_ADDRESS
,
1961 atype
, opy
->segment
, opy
->wrt
);
1969 nasm_panic(0, "internal instruction table corrupt"
1970 ": instruction code \\%o (0x%02X) given", c
, c
);
1976 static opflags_t
regflag(const operand
* o
)
1978 if (!is_register(o
->basereg
))
1979 nasm_panic(0, "invalid operand passed to regflag()");
1980 return nasm_reg_flags
[o
->basereg
];
1983 static int32_t regval(const operand
* o
)
1985 if (!is_register(o
->basereg
))
1986 nasm_panic(0, "invalid operand passed to regval()");
1987 return nasm_regvals
[o
->basereg
];
1990 static int op_rexflags(const operand
* o
, int mask
)
1995 if (!is_register(o
->basereg
))
1996 nasm_panic(0, "invalid operand passed to op_rexflags()");
1998 flags
= nasm_reg_flags
[o
->basereg
];
1999 val
= nasm_regvals
[o
->basereg
];
2001 return rexflags(val
, flags
, mask
);
2004 static int rexflags(int val
, opflags_t flags
, int mask
)
2008 if (val
>= 0 && (val
& 8))
2009 rex
|= REX_B
|REX_X
|REX_R
;
2012 if (!(REG_HIGH
& ~flags
)) /* AH, CH, DH, BH */
2014 else if (!(REG8
& ~flags
) && val
>= 4) /* SPL, BPL, SIL, DIL */
2020 static int evexflags(int val
, decoflags_t deco
,
2021 int mask
, uint8_t byte
)
2027 if (val
>= 0 && (val
& 16))
2028 evex
|= (EVEX_P0RP
| EVEX_P0X
);
2031 if (val
>= 0 && (val
& 16))
2035 if (deco
& OPMASK_MASK
)
2036 evex
|= deco
& EVEX_P2AAA
;
2042 static int op_evexflags(const operand
* o
, int mask
, uint8_t byte
)
2046 val
= nasm_regvals
[o
->basereg
];
2048 return evexflags(val
, o
->decoflags
, mask
, byte
);
2051 static enum match_result
find_match(const struct itemplate
**tempp
,
2053 int32_t segment
, int64_t offset
, int bits
)
2055 const struct itemplate
*temp
;
2056 enum match_result m
, merr
;
2057 opflags_t xsizeflags
[MAX_OPERANDS
];
2058 bool opsizemissing
= false;
2059 int8_t broadcast
= instruction
->evex_brerop
;
2062 /* broadcasting uses a different data element size */
2063 for (i
= 0; i
< instruction
->operands
; i
++)
2065 xsizeflags
[i
] = instruction
->oprs
[i
].decoflags
& BRSIZE_MASK
;
2067 xsizeflags
[i
] = instruction
->oprs
[i
].type
& SIZE_MASK
;
2069 merr
= MERR_INVALOP
;
2071 for (temp
= nasm_instructions
[instruction
->opcode
];
2072 temp
->opcode
!= I_none
; temp
++) {
2073 m
= matches(temp
, instruction
, bits
);
2074 if (m
== MOK_JUMP
) {
2075 if (jmp_match(segment
, offset
, bits
, instruction
, temp
))
2079 } else if (m
== MERR_OPSIZEMISSING
&& !itemp_has(temp
, IF_SX
)) {
2081 * Missing operand size and a candidate for fuzzy matching...
2083 for (i
= 0; i
< temp
->operands
; i
++)
2085 xsizeflags
[i
] |= temp
->deco
[i
] & BRSIZE_MASK
;
2087 xsizeflags
[i
] |= temp
->opd
[i
] & SIZE_MASK
;
2088 opsizemissing
= true;
2092 if (merr
== MOK_GOOD
)
2096 /* No match, but see if we can get a fuzzy operand size match... */
2100 for (i
= 0; i
< instruction
->operands
; i
++) {
2102 * We ignore extrinsic operand sizes on registers, so we should
2103 * never try to fuzzy-match on them. This also resolves the case
2104 * when we have e.g. "xmmrm128" in two different positions.
2106 if (is_class(REGISTER
, instruction
->oprs
[i
].type
))
2109 /* This tests if xsizeflags[i] has more than one bit set */
2110 if ((xsizeflags
[i
] & (xsizeflags
[i
]-1)))
2111 goto done
; /* No luck */
2113 if (i
== broadcast
) {
2114 instruction
->oprs
[i
].decoflags
|= xsizeflags
[i
];
2115 instruction
->oprs
[i
].type
|= (xsizeflags
[i
] == BR_BITS32
?
2118 instruction
->oprs
[i
].type
|= xsizeflags
[i
]; /* Set the size */
2122 /* Try matching again... */
2123 for (temp
= nasm_instructions
[instruction
->opcode
];
2124 temp
->opcode
!= I_none
; temp
++) {
2125 m
= matches(temp
, instruction
, bits
);
2126 if (m
== MOK_JUMP
) {
2127 if (jmp_match(segment
, offset
, bits
, instruction
, temp
))
2134 if (merr
== MOK_GOOD
)
2143 static uint8_t get_broadcast_num(opflags_t opflags
, opflags_t brsize
)
2145 opflags_t opsize
= opflags
& SIZE_MASK
;
2149 * Due to discontinuity between BITS64 and BITS128 (BITS80),
2150 * this cannot be a simple arithmetic calculation.
2152 if (brsize
> BITS64
)
2153 nasm_error(ERR_FATAL
,
2154 "size of broadcasting element is greater than 64 bits");
2158 brcast_num
= BITS64
/ brsize
;
2161 brcast_num
= (opsize
/ BITS128
) * (BITS64
/ brsize
) * 2;
2168 static enum match_result
matches(const struct itemplate
*itemp
,
2169 insn
*instruction
, int bits
)
2171 opflags_t size
[MAX_OPERANDS
], asize
;
2172 bool opsizemissing
= false;
2178 if (itemp
->opcode
!= instruction
->opcode
)
2179 return MERR_INVALOP
;
2182 * Count the operands
2184 if (itemp
->operands
!= instruction
->operands
)
2185 return MERR_INVALOP
;
2190 if (!(optimizing
> 0) && itemp_has(itemp
, IF_OPT
))
2191 return MERR_INVALOP
;
2196 switch (instruction
->prefixes
[PPS_VEX
]) {
2198 if (!itemp_has(itemp
, IF_EVEX
))
2199 return MERR_ENCMISMATCH
;
2203 if (!itemp_has(itemp
, IF_VEX
))
2204 return MERR_ENCMISMATCH
;
2211 * Check that no spurious colons or TOs are present
2213 for (i
= 0; i
< itemp
->operands
; i
++)
2214 if (instruction
->oprs
[i
].type
& ~itemp
->opd
[i
] & (COLON
| TO
))
2215 return MERR_INVALOP
;
2218 * Process size flags
2220 switch (itemp_smask(itemp
)) {
2221 case IF_GENBIT(IF_SB
):
2224 case IF_GENBIT(IF_SW
):
2227 case IF_GENBIT(IF_SD
):
2230 case IF_GENBIT(IF_SQ
):
2233 case IF_GENBIT(IF_SO
):
2236 case IF_GENBIT(IF_SY
):
2239 case IF_GENBIT(IF_SZ
):
2242 case IF_GENBIT(IF_SIZE
):
2263 if (itemp_armask(itemp
)) {
2264 /* S- flags only apply to a specific operand */
2265 i
= itemp_arg(itemp
);
2266 memset(size
, 0, sizeof size
);
2269 /* S- flags apply to all operands */
2270 for (i
= 0; i
< MAX_OPERANDS
; i
++)
2275 * Check that the operand flags all match up,
2276 * it's a bit tricky so lets be verbose:
2278 * 1) Find out the size of operand. If instruction
2279 * doesn't have one specified -- we're trying to
2280 * guess it either from template (IF_S* flag) or
2283 * 2) If template operand do not match the instruction OR
2284 * template has an operand size specified AND this size differ
2285 * from which instruction has (perhaps we got it from code bits)
2287 * a) Check that only size of instruction and operand is differ
2288 * other characteristics do match
2289 * b) Perhaps it's a register specified in instruction so
2290 * for such a case we just mark that operand as "size
2291 * missing" and this will turn on fuzzy operand size
2292 * logic facility (handled by a caller)
2294 for (i
= 0; i
< itemp
->operands
; i
++) {
2295 opflags_t type
= instruction
->oprs
[i
].type
;
2296 decoflags_t deco
= instruction
->oprs
[i
].decoflags
;
2297 bool is_broadcast
= deco
& BRDCAST_MASK
;
2298 uint8_t brcast_num
= 0;
2299 opflags_t template_opsize
, insn_opsize
;
2301 if (!(type
& SIZE_MASK
))
2304 insn_opsize
= type
& SIZE_MASK
;
2305 if (!is_broadcast
) {
2306 template_opsize
= itemp
->opd
[i
] & SIZE_MASK
;
2308 decoflags_t deco_brsize
= itemp
->deco
[i
] & BRSIZE_MASK
;
2310 * when broadcasting, the element size depends on
2311 * the instruction type. decorator flag should match.
2315 template_opsize
= (deco_brsize
== BR_BITS32
? BITS32
: BITS64
);
2316 /* calculate the proper number : {1to<brcast_num>} */
2317 brcast_num
= get_broadcast_num(itemp
->opd
[i
], template_opsize
);
2319 template_opsize
= 0;
2323 if ((itemp
->opd
[i
] & ~type
& ~SIZE_MASK
) ||
2324 (deco
& ~itemp
->deco
[i
] & ~BRNUM_MASK
)) {
2325 return MERR_INVALOP
;
2326 } else if (template_opsize
) {
2327 if (template_opsize
!= insn_opsize
) {
2329 return MERR_INVALOP
;
2330 } else if (!is_class(REGISTER
, type
)) {
2332 * Note: we don't honor extrinsic operand sizes for registers,
2333 * so "missing operand size" for a register should be
2334 * considered a wildcard match rather than an error.
2336 opsizemissing
= true;
2338 } else if (is_broadcast
&&
2340 (2U << ((deco
& BRNUM_MASK
) >> BRNUM_SHIFT
)))) {
2342 * broadcasting opsize matches but the number of repeated memory
2343 * element does not match.
2344 * if 64b double precision float is broadcasted to ymm (256b),
2345 * broadcasting decorator must be {1to4}.
2347 return MERR_BRNUMMISMATCH
;
2353 return MERR_OPSIZEMISSING
;
2356 * Check operand sizes
2358 if (itemp_has(itemp
, IF_SM
) || itemp_has(itemp
, IF_SM2
)) {
2359 oprs
= (itemp_has(itemp
, IF_SM2
) ? 2 : itemp
->operands
);
2360 for (i
= 0; i
< oprs
; i
++) {
2361 asize
= itemp
->opd
[i
] & SIZE_MASK
;
2363 for (i
= 0; i
< oprs
; i
++)
2369 oprs
= itemp
->operands
;
2372 for (i
= 0; i
< itemp
->operands
; i
++) {
2373 if (!(itemp
->opd
[i
] & SIZE_MASK
) &&
2374 (instruction
->oprs
[i
].type
& SIZE_MASK
& ~size
[i
]))
2375 return MERR_OPSIZEMISMATCH
;
2379 * Check template is okay at the set cpu level
2381 if (iflag_cmp_cpu_level(&insns_flags
[itemp
->iflag_idx
], &cpu
) > 0)
2385 * Verify the appropriate long mode flag.
2387 if (itemp_has(itemp
, (bits
== 64 ? IF_NOLONG
: IF_LONG
)))
2388 return MERR_BADMODE
;
2391 * If we have a HLE prefix, look for the NOHLE flag
2393 if (itemp_has(itemp
, IF_NOHLE
) &&
2394 (has_prefix(instruction
, PPS_REP
, P_XACQUIRE
) ||
2395 has_prefix(instruction
, PPS_REP
, P_XRELEASE
)))
2399 * Check if special handling needed for Jumps
2401 if ((itemp
->code
[0] & ~1) == 0370)
2405 * Check if BND prefix is allowed.
2406 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
2408 if (!itemp_has(itemp
, IF_BND
) &&
2409 (has_prefix(instruction
, PPS_REP
, P_BND
) ||
2410 has_prefix(instruction
, PPS_REP
, P_NOBND
)))
2412 else if (itemp_has(itemp
, IF_BND
) &&
2413 (has_prefix(instruction
, PPS_REP
, P_REPNE
) ||
2414 has_prefix(instruction
, PPS_REP
, P_REPNZ
)))
2415 return MERR_BADREPNE
;
2421 * Check if ModR/M.mod should/can be 01.
2422 * - EAF_BYTEOFFS is set
2423 * - offset can fit in a byte when EVEX is not used
2424 * - offset can be compressed when EVEX is used
2426 #define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2427 (o >= -128 && o <= 127 && \
2428 seg == NO_SEG && !forw_ref && \
2429 !(input->eaflags & EAF_WORDOFFS) && \
2430 !(ins->rex & REX_EV)) || \
2431 (ins->rex & REX_EV && \
2432 is_disp8n(input, ins, &output->disp8)))
2434 static enum ea_type
process_ea(operand
*input
, ea
*output
, int bits
,
2435 int rfield
, opflags_t rflags
, insn
*ins
)
2437 bool forw_ref
= !!(input
->opflags
& OPFLAG_UNKNOWN
);
2438 int addrbits
= ins
->addr_size
;
2439 int eaflags
= input
->eaflags
;
2441 output
->type
= EA_SCALAR
;
2442 output
->rip
= false;
2445 /* REX flags for the rfield operand */
2446 output
->rex
|= rexflags(rfield
, rflags
, REX_R
| REX_P
| REX_W
| REX_H
);
2447 /* EVEX.R' flag for the REG operand */
2448 ins
->evex_p
[0] |= evexflags(rfield
, 0, EVEX_P0RP
, 0);
2450 if (is_class(REGISTER
, input
->type
)) {
2452 * It's a direct register.
2454 if (!is_register(input
->basereg
))
2457 if (!is_reg_class(REG_EA
, input
->basereg
))
2460 /* broadcasting is not available with a direct register operand. */
2461 if (input
->decoflags
& BRDCAST_MASK
) {
2462 nasm_error(ERR_NONFATAL
, "Broadcasting not allowed from a register");
2466 output
->rex
|= op_rexflags(input
, REX_B
| REX_P
| REX_W
| REX_H
);
2467 ins
->evex_p
[0] |= op_evexflags(input
, EVEX_P0X
, 0);
2468 output
->sib_present
= false; /* no SIB necessary */
2469 output
->bytes
= 0; /* no offset necessary either */
2470 output
->modrm
= GEN_MODRM(3, rfield
, nasm_regvals
[input
->basereg
]);
2473 * It's a memory reference.
2476 /* Embedded rounding or SAE is not available with a mem ref operand. */
2477 if (input
->decoflags
& (ER
| SAE
)) {
2478 nasm_error(ERR_NONFATAL
,
2479 "Embedded rounding is available only with reg-reg op.");
2483 if (input
->basereg
== -1 &&
2484 (input
->indexreg
== -1 || input
->scale
== 0)) {
2486 * It's a pure offset.
2488 if (bits
== 64 && ((input
->type
& IP_REL
) == IP_REL
) &&
2489 input
->segment
== NO_SEG
) {
2490 nasm_error(ERR_WARNING
| ERR_PASS1
, "absolute address can not be RIP-relative");
2491 input
->type
&= ~IP_REL
;
2492 input
->type
|= MEMORY
;
2496 !(IP_REL
& ~input
->type
) && (eaflags
& EAF_MIB
)) {
2497 nasm_error(ERR_NONFATAL
, "RIP-relative addressing is prohibited for mib.");
2501 if (eaflags
& EAF_BYTEOFFS
||
2502 (eaflags
& EAF_WORDOFFS
&&
2503 input
->disp_size
!= (addrbits
!= 16 ? 32 : 16))) {
2504 nasm_error(ERR_WARNING
| ERR_PASS1
, "displacement size ignored on absolute address");
2507 if (bits
== 64 && (~input
->type
& IP_REL
)) {
2508 output
->sib_present
= true;
2509 output
->sib
= GEN_SIB(0, 4, 5);
2511 output
->modrm
= GEN_MODRM(0, rfield
, 4);
2512 output
->rip
= false;
2514 output
->sib_present
= false;
2515 output
->bytes
= (addrbits
!= 16 ? 4 : 2);
2516 output
->modrm
= GEN_MODRM(0, rfield
, (addrbits
!= 16 ? 5 : 6));
2517 output
->rip
= bits
== 64;
2521 * It's an indirection.
2523 int i
= input
->indexreg
, b
= input
->basereg
, s
= input
->scale
;
2524 int32_t seg
= input
->segment
;
2525 int hb
= input
->hintbase
, ht
= input
->hinttype
;
2526 int t
, it
, bt
; /* register numbers */
2527 opflags_t x
, ix
, bx
; /* register flags */
2530 i
= -1; /* make this easy, at least */
2532 if (is_register(i
)) {
2533 it
= nasm_regvals
[i
];
2534 ix
= nasm_reg_flags
[i
];
2540 if (is_register(b
)) {
2541 bt
= nasm_regvals
[b
];
2542 bx
= nasm_reg_flags
[b
];
2548 /* if either one are a vector register... */
2549 if ((ix
|bx
) & (XMMREG
|YMMREG
|ZMMREG
) & ~REG_EA
) {
2550 opflags_t sok
= BITS32
| BITS64
;
2551 int32_t o
= input
->offset
;
2552 int mod
, scale
, index
, base
;
2555 * For a vector SIB, one has to be a vector and the other,
2556 * if present, a GPR. The vector must be the index operand.
2558 if (it
== -1 || (bx
& (XMMREG
|YMMREG
|ZMMREG
) & ~REG_EA
)) {
2564 t
= bt
, bt
= it
, it
= t
;
2565 x
= bx
, bx
= ix
, ix
= x
;
2571 if (!(REG64
& ~bx
) || !(REG32
& ~bx
))
2578 * While we're here, ensure the user didn't specify
2581 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2584 if (addrbits
== 16 ||
2585 (addrbits
== 32 && !(sok
& BITS32
)) ||
2586 (addrbits
== 64 && !(sok
& BITS64
)))
2589 output
->type
= ((ix
& ZMMREG
& ~REG_EA
) ? EA_ZMMVSIB
2590 : ((ix
& YMMREG
& ~REG_EA
)
2591 ? EA_YMMVSIB
: EA_XMMVSIB
));
2593 output
->rex
|= rexflags(it
, ix
, REX_X
);
2594 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2595 ins
->evex_p
[2] |= evexflags(it
, 0, EVEX_P2VP
, 2);
2597 index
= it
& 7; /* it is known to be != -1 */
2612 default: /* then what the smeg is it? */
2613 goto err
; /* panic */
2621 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2622 seg
== NO_SEG
&& !forw_ref
&&
2623 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2625 else if (IS_MOD_01())
2631 output
->sib_present
= true;
2632 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2633 output
->modrm
= GEN_MODRM(mod
, rfield
, 4);
2634 output
->sib
= GEN_SIB(scale
, index
, base
);
2635 } else if ((ix
|bx
) & (BITS32
|BITS64
)) {
2637 * it must be a 32/64-bit memory reference. Firstly we have
2638 * to check that all registers involved are type E/Rxx.
2640 opflags_t sok
= BITS32
| BITS64
;
2641 int32_t o
= input
->offset
;
2644 if (!(REG64
& ~ix
) || !(REG32
& ~ix
))
2652 goto err
; /* Invalid register */
2653 if (~sok
& bx
& SIZE_MASK
)
2654 goto err
; /* Invalid size */
2659 * While we're here, ensure the user didn't specify
2662 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2665 if (addrbits
== 16 ||
2666 (addrbits
== 32 && !(sok
& BITS32
)) ||
2667 (addrbits
== 64 && !(sok
& BITS64
)))
2670 /* now reorganize base/index */
2671 if (s
== 1 && bt
!= it
&& bt
!= -1 && it
!= -1 &&
2672 ((hb
== b
&& ht
== EAH_NOTBASE
) ||
2673 (hb
== i
&& ht
== EAH_MAKEBASE
))) {
2674 /* swap if hints say so */
2675 t
= bt
, bt
= it
, it
= t
;
2676 x
= bx
, bx
= ix
, ix
= x
;
2679 if (bt
== -1 && s
== 1 && !(hb
== i
&& ht
== EAH_NOTBASE
)) {
2680 /* make single reg base, unless hint */
2681 bt
= it
, bx
= ix
, it
= -1, ix
= 0;
2683 if (eaflags
& EAF_MIB
) {
2684 /* only for mib operands */
2685 if (it
== -1 && (hb
== b
&& ht
== EAH_NOTBASE
)) {
2687 * make a single reg index [reg*1].
2688 * gas uses this form for an explicit index register.
2690 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2692 if ((ht
== EAH_SUMMED
) && bt
== -1) {
2693 /* separate once summed index into [base, index] */
2694 bt
= it
, bx
= ix
, s
--;
2697 if (((s
== 2 && it
!= REG_NUM_ESP
&&
2698 (!(eaflags
& EAF_TIMESTWO
) || (ht
== EAH_SUMMED
))) ||
2699 s
== 3 || s
== 5 || s
== 9) && bt
== -1) {
2700 /* convert 3*EAX to EAX+2*EAX */
2701 bt
= it
, bx
= ix
, s
--;
2703 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
&&
2704 (eaflags
& EAF_TIMESTWO
) &&
2705 (hb
== b
&& ht
== EAH_NOTBASE
)) {
2707 * convert [NOSPLIT EAX*1]
2708 * to sib format with 0x0 displacement - [EAX*1+0].
2710 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2713 if (s
== 1 && it
== REG_NUM_ESP
) {
2714 /* swap ESP into base if scale is 1 */
2715 t
= it
, it
= bt
, bt
= t
;
2716 x
= ix
, ix
= bx
, bx
= x
;
2718 if (it
== REG_NUM_ESP
||
2719 (s
!= 1 && s
!= 2 && s
!= 4 && s
!= 8 && it
!= -1))
2720 goto err
; /* wrong, for various reasons */
2722 output
->rex
|= rexflags(it
, ix
, REX_X
);
2723 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2725 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
) {
2734 if (rm
!= REG_NUM_EBP
&& o
== 0 &&
2735 seg
== NO_SEG
&& !forw_ref
&&
2736 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2738 else if (IS_MOD_01())
2744 output
->sib_present
= false;
2745 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2746 output
->modrm
= GEN_MODRM(mod
, rfield
, rm
);
2749 int mod
, scale
, index
, base
;
2769 default: /* then what the smeg is it? */
2770 goto err
; /* panic */
2778 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2779 seg
== NO_SEG
&& !forw_ref
&&
2780 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2782 else if (IS_MOD_01())
2788 output
->sib_present
= true;
2789 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2790 output
->modrm
= GEN_MODRM(mod
, rfield
, 4);
2791 output
->sib
= GEN_SIB(scale
, index
, base
);
2793 } else { /* it's 16-bit */
2795 int16_t o
= input
->offset
;
2797 /* check for 64-bit long mode */
2801 /* check all registers are BX, BP, SI or DI */
2802 if ((b
!= -1 && b
!= R_BP
&& b
!= R_BX
&& b
!= R_SI
&& b
!= R_DI
) ||
2803 (i
!= -1 && i
!= R_BP
&& i
!= R_BX
&& i
!= R_SI
&& i
!= R_DI
))
2806 /* ensure the user didn't specify DWORD/QWORD */
2807 if (input
->disp_size
== 32 || input
->disp_size
== 64)
2810 if (s
!= 1 && i
!= -1)
2811 goto err
; /* no can do, in 16-bit EA */
2812 if (b
== -1 && i
!= -1) {
2817 if ((b
== R_SI
|| b
== R_DI
) && i
!= -1) {
2822 /* have BX/BP as base, SI/DI index */
2824 goto err
; /* shouldn't ever happen, in theory */
2825 if (i
!= -1 && b
!= -1 &&
2826 (i
== R_BP
|| i
== R_BX
|| b
== R_SI
|| b
== R_DI
))
2827 goto err
; /* invalid combinations */
2828 if (b
== -1) /* pure offset: handled above */
2829 goto err
; /* so if it gets to here, panic! */
2833 switch (i
* 256 + b
) {
2834 case R_SI
* 256 + R_BX
:
2837 case R_DI
* 256 + R_BX
:
2840 case R_SI
* 256 + R_BP
:
2843 case R_DI
* 256 + R_BP
:
2861 if (rm
== -1) /* can't happen, in theory */
2862 goto err
; /* so panic if it does */
2864 if (o
== 0 && seg
== NO_SEG
&& !forw_ref
&& rm
!= 6 &&
2865 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2867 else if (IS_MOD_01())
2872 output
->sib_present
= false; /* no SIB - it's 16-bit */
2873 output
->bytes
= mod
; /* bytes of offset needed */
2874 output
->modrm
= GEN_MODRM(mod
, rfield
, rm
);
2879 output
->size
= 1 + output
->sib_present
+ output
->bytes
;
2880 return output
->type
;
2883 return output
->type
= EA_INVALID
;
2886 static void add_asp(insn
*ins
, int addrbits
)
2891 valid
= (addrbits
== 64) ? 64|32 : 32|16;
2893 switch (ins
->prefixes
[PPS_ASIZE
]) {
2904 valid
&= (addrbits
== 32) ? 16 : 32;
2910 for (j
= 0; j
< ins
->operands
; j
++) {
2911 if (is_class(MEMORY
, ins
->oprs
[j
].type
)) {
2914 /* Verify as Register */
2915 if (!is_register(ins
->oprs
[j
].indexreg
))
2918 i
= nasm_reg_flags
[ins
->oprs
[j
].indexreg
];
2920 /* Verify as Register */
2921 if (!is_register(ins
->oprs
[j
].basereg
))
2924 b
= nasm_reg_flags
[ins
->oprs
[j
].basereg
];
2926 if (ins
->oprs
[j
].scale
== 0)
2930 int ds
= ins
->oprs
[j
].disp_size
;
2931 if ((addrbits
!= 64 && ds
> 8) ||
2932 (addrbits
== 64 && ds
== 16))
2952 if (valid
& addrbits
) {
2953 ins
->addr_size
= addrbits
;
2954 } else if (valid
& ((addrbits
== 32) ? 16 : 32)) {
2955 /* Add an address size prefix */
2956 ins
->prefixes
[PPS_ASIZE
] = (addrbits
== 32) ? P_A16
: P_A32
;;
2957 ins
->addr_size
= (addrbits
== 32) ? 16 : 32;
2960 nasm_error(ERR_NONFATAL
, "impossible combination of address sizes");
2961 ins
->addr_size
= addrbits
; /* Error recovery */
2964 defdisp
= ins
->addr_size
== 16 ? 16 : 32;
2966 for (j
= 0; j
< ins
->operands
; j
++) {
2967 if (!(MEM_OFFS
& ~ins
->oprs
[j
].type
) &&
2968 (ins
->oprs
[j
].disp_size
? ins
->oprs
[j
].disp_size
: defdisp
) != ins
->addr_size
) {
2970 * mem_offs sizes must match the address size; if not,
2971 * strip the MEM_OFFS bit and match only EA instructions
2973 ins
->oprs
[j
].type
&= ~(MEM_OFFS
& ~MEMORY
);