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 */
230 static efunc errfunc
;
231 static struct ofmt
*outfmt
;
232 static ListGen
*list
;
234 static int64_t calcsize(int32_t, int64_t, int, insn
*,
235 const struct itemplate
*);
236 static void gencode(int32_t segment
, int64_t offset
, int bits
,
237 insn
* ins
, const struct itemplate
*temp
,
239 static enum match_result
find_match(const struct itemplate
**tempp
,
241 int32_t segment
, int64_t offset
, int bits
);
242 static enum match_result
matches(const struct itemplate
*, insn
*, int bits
);
243 static opflags_t
regflag(const operand
*);
244 static int32_t regval(const operand
*);
245 static int rexflags(int, opflags_t
, int);
246 static int op_rexflags(const operand
*, int);
247 static int op_evexflags(const operand
*, int, uint8_t);
248 static void add_asp(insn
*, int);
250 static enum ea_type
process_ea(operand
*, ea
*, int, int, opflags_t
, insn
*);
252 static int has_prefix(insn
* ins
, enum prefix_pos pos
, int prefix
)
254 return ins
->prefixes
[pos
] == prefix
;
257 static void assert_no_prefix(insn
* ins
, enum prefix_pos pos
)
259 if (ins
->prefixes
[pos
])
260 errfunc(ERR_NONFATAL
, "invalid %s prefix",
261 prefix_name(ins
->prefixes
[pos
]));
264 static const char *size_name(int size
)
288 static void warn_overflow(int pass
, int size
)
290 errfunc(ERR_WARNING
| pass
| ERR_WARN_NOV
,
291 "%s data exceeds bounds", size_name(size
));
294 static void warn_overflow_const(int64_t data
, int size
)
296 if (overflow_general(data
, size
))
297 warn_overflow(ERR_PASS1
, size
);
300 static void warn_overflow_opd(const struct operand
*o
, int size
)
302 if (o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
) {
303 if (overflow_general(o
->offset
, size
))
304 warn_overflow(ERR_PASS2
, size
);
309 * Size of an address relocation, or zero if not an address
311 static int addrsize(enum out_type type
, uint64_t size
)
315 return abs((int)size
);
330 * This routine wrappers the real output format's output routine,
331 * in order to pass a copy of the data off to the listing file
332 * generator at the same time, flatten unnecessary relocations,
333 * and verify backend compatibility.
335 static void out(int64_t offset
, int32_t segto
, const void *data
,
336 enum out_type type
, uint64_t size
,
337 int32_t segment
, int32_t wrt
)
339 static int32_t lineno
= 0; /* static!!! */
340 static char *lnfname
= NULL
;
342 int asize
= addrsize(type
, size
); /* Address size in bytes */
343 const int amax
= outfmt
->maxbits
>> 3; /* Maximum address size in bytes */
345 if (type
== OUT_ADDRESS
&& segment
== NO_SEG
&& wrt
== NO_SEG
) {
347 * This is a non-relocated address, and we're going to
348 * convert it into RAWDATA format.
353 errfunc(ERR_PANIC
, "OUT_ADDRESS with size > 8");
357 WRITEADDR(q
, *(int64_t *)data
, asize
);
361 asize
= 0; /* No longer an address */
364 list
->output(offset
, data
, type
, size
);
367 * this call to src_get determines when we call the
368 * debug-format-specific "linenum" function
369 * it updates lineno and lnfname to the current values
370 * returning 0 if "same as last time", -2 if lnfname
371 * changed, and the amount by which lineno changed,
372 * if it did. thus, these variables must be static
375 if (src_get(&lineno
, &lnfname
))
376 outfmt
->current_dfmt
->linenum(lnfname
, lineno
, segto
);
378 if (asize
&& asize
> amax
) {
379 if (type
!= OUT_ADDRESS
|| (int)size
< 0) {
380 errfunc(ERR_NONFATAL
,
381 "%d-bit signed relocation unsupported by output format %s\n",
382 asize
<< 3, outfmt
->shortname
);
384 errfunc(ERR_WARNING
| ERR_WARN_ZEXTRELOC
,
385 "%d-bit unsigned relocation zero-extended from %d bits\n",
386 asize
<< 3, outfmt
->maxbits
);
387 outfmt
->output(segto
, data
, type
, amax
, segment
, wrt
);
392 segment
= wrt
= NO_SEG
;
395 outfmt
->output(segto
, data
, type
, size
, segment
, wrt
);
398 static void out_imm8(int64_t offset
, int32_t segment
,
399 struct operand
*opx
, int asize
)
401 if (opx
->segment
!= NO_SEG
) {
402 uint64_t data
= opx
->offset
;
403 out(offset
, segment
, &data
, OUT_ADDRESS
, asize
, opx
->segment
, opx
->wrt
);
405 uint8_t byte
= opx
->offset
;
406 out(offset
, segment
, &byte
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
410 static bool jmp_match(int32_t segment
, int64_t offset
, int bits
,
411 insn
* ins
, const struct itemplate
*temp
)
414 const uint8_t *code
= temp
->code
;
418 if (((c
& ~1) != 0370) || (ins
->oprs
[0].type
& STRICT
))
422 if (optimizing
< 0 && c
== 0371)
425 isize
= calcsize(segment
, offset
, bits
, ins
, temp
);
427 if (ins
->oprs
[0].opflags
& OPFLAG_UNKNOWN
)
428 /* Be optimistic in pass 1 */
431 if (ins
->oprs
[0].segment
!= segment
)
434 isize
= ins
->oprs
[0].offset
- offset
- isize
; /* isize is delta */
435 is_byte
= (isize
>= -128 && isize
<= 127); /* is it byte size? */
437 if (is_byte
&& c
== 0371 && ins
->prefixes
[PPS_REP
] == P_BND
) {
438 /* jmp short (opcode eb) cannot be used with bnd prefix. */
439 ins
->prefixes
[PPS_REP
] = P_none
;
440 errfunc(ERR_WARNING
| ERR_WARN_BND
| ERR_PASS2
,
441 "jmp short does not init bnd regs - bnd prefix dropped.");
447 int64_t assemble(int32_t segment
, int64_t offset
, int bits
, iflag_t cp
,
448 insn
* instruction
, struct ofmt
*output
, efunc error
,
451 const struct itemplate
*temp
;
456 int64_t start
= offset
;
457 int64_t wsize
; /* size for DB etc. */
459 errfunc
= error
; /* to pass to other functions */
461 outfmt
= output
; /* likewise */
462 list
= listgen
; /* and again */
464 wsize
= idata_bytes(instruction
->opcode
);
470 int32_t t
= instruction
->times
;
473 "instruction->times < 0 (%ld) in assemble()", t
);
475 while (t
--) { /* repeat TIMES times */
476 list_for_each(e
, instruction
->eops
) {
477 if (e
->type
== EOT_DB_NUMBER
) {
479 errfunc(ERR_NONFATAL
,
480 "integer supplied to a DT, DO or DY"
483 out(offset
, segment
, &e
->offset
,
484 OUT_ADDRESS
, wsize
, e
->segment
, e
->wrt
);
487 } else if (e
->type
== EOT_DB_STRING
||
488 e
->type
== EOT_DB_STRING_FREE
) {
491 out(offset
, segment
, e
->stringval
,
492 OUT_RAWDATA
, e
->stringlen
, NO_SEG
, NO_SEG
);
493 align
= e
->stringlen
% wsize
;
496 align
= wsize
- align
;
497 out(offset
, segment
, zero_buffer
,
498 OUT_RAWDATA
, align
, NO_SEG
, NO_SEG
);
500 offset
+= e
->stringlen
+ align
;
503 if (t
> 0 && t
== instruction
->times
- 1) {
505 * Dummy call to list->output to give the offset to the
508 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
509 list
->uplevel(LIST_TIMES
);
512 if (instruction
->times
> 1)
513 list
->downlevel(LIST_TIMES
);
514 return offset
- start
;
517 if (instruction
->opcode
== I_INCBIN
) {
518 const char *fname
= instruction
->eops
->stringval
;
521 fp
= fopen(fname
, "rb");
523 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
525 } else if (fseek(fp
, 0L, SEEK_END
) < 0) {
526 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
530 static char buf
[4096];
531 size_t t
= instruction
->times
;
536 if (instruction
->eops
->next
) {
537 base
= instruction
->eops
->next
->offset
;
539 if (instruction
->eops
->next
->next
&&
540 len
> (size_t)instruction
->eops
->next
->next
->offset
)
541 len
= (size_t)instruction
->eops
->next
->next
->offset
;
544 * Dummy call to list->output to give the offset to the
547 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
548 list
->uplevel(LIST_INCBIN
);
552 fseek(fp
, base
, SEEK_SET
);
556 m
= fread(buf
, 1, l
> sizeof(buf
) ? sizeof(buf
) : l
, fp
);
559 * This shouldn't happen unless the file
560 * actually changes while we are reading
564 "`incbin': unexpected EOF while"
565 " reading file `%s'", fname
);
566 t
= 0; /* Try to exit cleanly */
569 out(offset
, segment
, buf
, OUT_RAWDATA
, m
,
574 list
->downlevel(LIST_INCBIN
);
575 if (instruction
->times
> 1) {
577 * Dummy call to list->output to give the offset to the
580 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
581 list
->uplevel(LIST_TIMES
);
582 list
->downlevel(LIST_TIMES
);
585 return instruction
->times
* len
;
587 return 0; /* if we're here, there's an error */
590 /* Check to see if we need an address-size prefix */
591 add_asp(instruction
, bits
);
593 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
597 int64_t insn_size
= calcsize(segment
, offset
, bits
, instruction
, temp
);
598 itimes
= instruction
->times
;
599 if (insn_size
< 0) /* shouldn't be, on pass two */
600 error(ERR_PANIC
, "errors made it through from pass one");
603 for (j
= 0; j
< MAXPREFIX
; j
++) {
605 switch (instruction
->prefixes
[j
]) {
626 error(ERR_WARNING
| ERR_PASS2
,
627 "cs segment base generated, but will be ignored in 64-bit mode");
633 error(ERR_WARNING
| ERR_PASS2
,
634 "ds segment base generated, but will be ignored in 64-bit mode");
640 error(ERR_WARNING
| ERR_PASS2
,
641 "es segment base generated, but will be ignored in 64-bit mode");
653 error(ERR_WARNING
| ERR_PASS2
,
654 "ss segment base generated, but will be ignored in 64-bit mode");
661 "segr6 and segr7 cannot be used as prefixes");
666 "16-bit addressing is not supported "
668 } else if (bits
!= 16)
678 "64-bit addressing is only supported "
706 error(ERR_PANIC
, "invalid instruction prefix");
709 out(offset
, segment
, &c
, OUT_RAWDATA
, 1,
714 insn_end
= offset
+ insn_size
;
715 gencode(segment
, offset
, bits
, instruction
,
718 if (itimes
> 0 && itimes
== instruction
->times
- 1) {
720 * Dummy call to list->output to give the offset to the
723 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
724 list
->uplevel(LIST_TIMES
);
727 if (instruction
->times
> 1)
728 list
->downlevel(LIST_TIMES
);
729 return offset
- start
;
733 case MERR_OPSIZEMISSING
:
734 error(ERR_NONFATAL
, "operation size not specified");
736 case MERR_OPSIZEMISMATCH
:
737 error(ERR_NONFATAL
, "mismatch in operand sizes");
739 case MERR_BRNUMMISMATCH
:
741 "mismatch in the number of broadcasting elements");
744 error(ERR_NONFATAL
, "no instruction for this cpu level");
747 error(ERR_NONFATAL
, "instruction not supported in %d-bit mode",
750 case MERR_ENCMISMATCH
:
751 error(ERR_NONFATAL
, "specific encoding scheme not available");
754 error(ERR_NONFATAL
, "bnd prefix is not allowed");
757 error(ERR_NONFATAL
, "%s prefix is not allowed",
758 (has_prefix(instruction
, PPS_REP
, P_REPNE
) ?
763 "invalid combination of opcode and operands");
770 int64_t insn_size(int32_t segment
, int64_t offset
, int bits
, iflag_t cp
,
771 insn
* instruction
, efunc error
)
773 const struct itemplate
*temp
;
776 errfunc
= error
; /* to pass to other functions */
779 if (instruction
->opcode
== I_none
)
782 if (instruction
->opcode
== I_DB
|| instruction
->opcode
== I_DW
||
783 instruction
->opcode
== I_DD
|| instruction
->opcode
== I_DQ
||
784 instruction
->opcode
== I_DT
|| instruction
->opcode
== I_DO
||
785 instruction
->opcode
== I_DY
) {
787 int32_t isize
, osize
, wsize
;
790 wsize
= idata_bytes(instruction
->opcode
);
792 list_for_each(e
, instruction
->eops
) {
796 if (e
->type
== EOT_DB_NUMBER
) {
798 warn_overflow_const(e
->offset
, wsize
);
799 } else if (e
->type
== EOT_DB_STRING
||
800 e
->type
== EOT_DB_STRING_FREE
)
801 osize
= e
->stringlen
;
803 align
= (-osize
) % wsize
;
806 isize
+= osize
+ align
;
808 return isize
* instruction
->times
;
811 if (instruction
->opcode
== I_INCBIN
) {
812 const char *fname
= instruction
->eops
->stringval
;
817 fp
= fopen(fname
, "rb");
819 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
821 else if (fseek(fp
, 0L, SEEK_END
) < 0)
822 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
826 if (instruction
->eops
->next
) {
827 len
-= instruction
->eops
->next
->offset
;
828 if (instruction
->eops
->next
->next
&&
829 len
> (size_t)instruction
->eops
->next
->next
->offset
) {
830 len
= (size_t)instruction
->eops
->next
->next
->offset
;
833 val
= instruction
->times
* len
;
840 /* Check to see if we need an address-size prefix */
841 add_asp(instruction
, bits
);
843 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
845 /* we've matched an instruction. */
849 isize
= calcsize(segment
, offset
, bits
, instruction
, temp
);
852 for (j
= 0; j
< MAXPREFIX
; j
++) {
853 switch (instruction
->prefixes
[j
]) {
883 return isize
* instruction
->times
;
885 return -1; /* didn't match any instruction */
889 static void bad_hle_warn(const insn
* ins
, uint8_t hleok
)
891 enum prefixes rep_pfx
= ins
->prefixes
[PPS_REP
];
892 enum whatwarn
{ w_none
, w_lock
, w_inval
} ww
;
893 static const enum whatwarn warn
[2][4] =
895 { w_inval
, w_inval
, w_none
, w_lock
}, /* XACQUIRE */
896 { w_inval
, w_none
, w_none
, w_lock
}, /* XRELEASE */
900 n
= (unsigned int)rep_pfx
- P_XACQUIRE
;
902 return; /* Not XACQUIRE/XRELEASE */
905 if (!is_class(MEMORY
, ins
->oprs
[0].type
))
906 ww
= w_inval
; /* HLE requires operand 0 to be memory */
913 if (ins
->prefixes
[PPS_LOCK
] != P_LOCK
) {
914 errfunc(ERR_WARNING
| ERR_WARN_HLE
| ERR_PASS2
,
915 "%s with this instruction requires lock",
916 prefix_name(rep_pfx
));
921 errfunc(ERR_WARNING
| ERR_WARN_HLE
| ERR_PASS2
,
922 "%s invalid with this instruction",
923 prefix_name(rep_pfx
));
928 /* Common construct */
929 #define case3(x) case (x): case (x)+1: case (x)+2
930 #define case4(x) case3(x): case (x)+3
932 static int64_t calcsize(int32_t segment
, int64_t offset
, int bits
,
933 insn
* ins
, const struct itemplate
*temp
)
935 const uint8_t *codes
= temp
->code
;
944 bool lockcheck
= true;
945 enum reg_enum mib_index
= R_none
; /* For a separate index MIB reg form */
947 ins
->rex
= 0; /* Ensure REX is reset */
948 eat
= EA_SCALAR
; /* Expect a scalar EA */
949 memset(ins
->evex_p
, 0, 3); /* Ensure EVEX is reset */
951 if (ins
->prefixes
[PPS_OSIZE
] == P_O64
)
954 (void)segment
; /* Don't warn that this parameter is unused */
955 (void)offset
; /* Don't warn that this parameter is unused */
959 op1
= (c
& 3) + ((opex
& 1) << 2);
960 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
961 opx
= &ins
->oprs
[op1
];
962 opex
= 0; /* For the next iteration */
966 codes
+= c
, length
+= c
;
975 op_rexflags(opx
, REX_B
|REX_H
|REX_P
|REX_W
);
980 /* this is an index reg of MIB operand */
981 mib_index
= opx
->basereg
;
994 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
995 length
+= (opx
->type
& BITS16
) ? 2 : 4;
997 length
+= (bits
== 16) ? 2 : 4;
1005 length
+= ins
->addr_size
>> 3;
1013 length
+= 8; /* MOV reg64/imm */
1021 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1022 length
+= (opx
->type
& BITS16
) ? 2 : 4;
1024 length
+= (bits
== 16) ? 2 : 4;
1047 ins
->vexreg
= regval(opx
);
1048 ins
->evex_p
[2] |= op_evexflags(opx
, EVEX_P2VP
, 2); /* High-16 NDS */
1049 ins
->vex_cm
= *codes
++;
1050 ins
->vex_wlp
= *codes
++;
1051 ins
->evex_tuple
= (*codes
++ - 0300);
1057 ins
->vex_cm
= *codes
++;
1058 ins
->vex_wlp
= *codes
++;
1059 ins
->evex_tuple
= (*codes
++ - 0300);
1068 ins
->vexreg
= regval(opx
);
1069 ins
->vex_cm
= *codes
++;
1070 ins
->vex_wlp
= *codes
++;
1076 ins
->vex_cm
= *codes
++;
1077 ins
->vex_wlp
= *codes
++;
1094 length
+= (bits
!= 16) && !has_prefix(ins
, PPS_ASIZE
, P_A16
);
1098 length
+= (bits
!= 32) && !has_prefix(ins
, PPS_ASIZE
, P_A32
);
1105 if (bits
!= 64 || has_prefix(ins
, PPS_ASIZE
, P_A16
) ||
1106 has_prefix(ins
, PPS_ASIZE
, P_A32
))
1115 enum prefixes pfx
= ins
->prefixes
[PPS_OSIZE
];
1119 errfunc(ERR_WARNING
| ERR_PASS2
, "invalid operand size prefix");
1121 ins
->prefixes
[PPS_OSIZE
] = P_O16
;
1127 enum prefixes pfx
= ins
->prefixes
[PPS_OSIZE
];
1131 errfunc(ERR_WARNING
| ERR_PASS2
, "invalid operand size prefix");
1133 ins
->prefixes
[PPS_OSIZE
] = P_O32
;
1175 if (!ins
->prefixes
[PPS_REP
])
1176 ins
->prefixes
[PPS_REP
] = P_REP
;
1180 if (!ins
->prefixes
[PPS_REP
])
1181 ins
->prefixes
[PPS_REP
] = P_REPNE
;
1185 if (ins
->oprs
[0].segment
!= NO_SEG
)
1186 errfunc(ERR_NONFATAL
, "attempt to reserve non-constant"
1187 " quantity of BSS space");
1189 length
+= ins
->oprs
[0].offset
;
1193 if (!ins
->prefixes
[PPS_WAIT
])
1194 ins
->prefixes
[PPS_WAIT
] = P_WAIT
;
1249 struct operand
*opy
= &ins
->oprs
[op2
];
1250 struct operand
*op_er_sae
;
1252 ea_data
.rex
= 0; /* Ensure ea.REX is initially 0 */
1255 /* pick rfield from operand b (opx) */
1256 rflags
= regflag(opx
);
1257 rfield
= nasm_regvals
[opx
->basereg
];
1263 /* EVEX.b1 : evex_brerop contains the operand position */
1264 op_er_sae
= (ins
->evex_brerop
>= 0 ?
1265 &ins
->oprs
[ins
->evex_brerop
] : NULL
);
1267 if (op_er_sae
&& (op_er_sae
->decoflags
& (ER
| SAE
))) {
1269 ins
->evex_p
[2] |= EVEX_P2B
;
1270 if (op_er_sae
->decoflags
& ER
) {
1271 /* set EVEX.RC (rounding control) */
1272 ins
->evex_p
[2] |= ((ins
->evex_rm
- BRC_RN
) << 5)
1276 /* set EVEX.L'L (vector length) */
1277 ins
->evex_p
[2] |= ((ins
->vex_wlp
<< (5 - 2)) & EVEX_P2LL
);
1278 ins
->evex_p
[1] |= ((ins
->vex_wlp
<< (7 - 4)) & EVEX_P1W
);
1279 if (opy
->decoflags
& BRDCAST_MASK
) {
1281 ins
->evex_p
[2] |= EVEX_P2B
;
1285 if (itemp_has(temp
, IF_MIB
)) {
1286 opy
->eaflags
|= EAF_MIB
;
1288 * if a separate form of MIB (ICC style) is used,
1289 * the index reg info is merged into mem operand
1291 if (mib_index
!= R_none
) {
1292 opy
->indexreg
= mib_index
;
1294 opy
->hintbase
= mib_index
;
1295 opy
->hinttype
= EAH_NOTBASE
;
1299 if (process_ea(opy
, &ea_data
, bits
,
1300 rfield
, rflags
, ins
) != eat
) {
1301 errfunc(ERR_NONFATAL
, "invalid effective address");
1304 ins
->rex
|= ea_data
.rex
;
1305 length
+= ea_data
.size
;
1311 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1312 ": instruction code \\%o (0x%02X) given", c
, c
);
1317 ins
->rex
&= rex_mask
;
1319 if (ins
->rex
& REX_NH
) {
1320 if (ins
->rex
& REX_H
) {
1321 errfunc(ERR_NONFATAL
, "instruction cannot use high registers");
1324 ins
->rex
&= ~REX_P
; /* Don't force REX prefix due to high reg */
1327 switch (ins
->prefixes
[PPS_VEX
]) {
1329 if (!(ins
->rex
& REX_EV
))
1334 if (!(ins
->rex
& REX_V
))
1341 if (ins
->rex
& (REX_V
| REX_EV
)) {
1342 int bad32
= REX_R
|REX_W
|REX_X
|REX_B
;
1344 if (ins
->rex
& REX_H
) {
1345 errfunc(ERR_NONFATAL
, "cannot use high register in AVX instruction");
1348 switch (ins
->vex_wlp
& 060) {
1362 if (bits
!= 64 && ((ins
->rex
& bad32
) || ins
->vexreg
> 7)) {
1363 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1365 } else if (!(ins
->rex
& REX_EV
) &&
1366 ((ins
->vexreg
> 15) || (ins
->evex_p
[0] & 0xf0))) {
1367 errfunc(ERR_NONFATAL
, "invalid high-16 register in non-AVX-512");
1370 if (ins
->rex
& REX_EV
)
1372 else if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
)) ||
1373 ins
->prefixes
[PPS_VEX
] == P_VEX3
)
1377 } else if (ins
->rex
& REX_MASK
) {
1378 if (ins
->rex
& REX_H
) {
1379 errfunc(ERR_NONFATAL
, "cannot use high register in rex instruction");
1381 } else if (bits
== 64) {
1383 } else if ((ins
->rex
& REX_L
) &&
1384 !(ins
->rex
& (REX_P
|REX_W
|REX_X
|REX_B
)) &&
1385 iflag_ffs(&cpu
) >= IF_X86_64
) {
1387 assert_no_prefix(ins
, PPS_LOCK
);
1388 lockcheck
= false; /* Already errored, no need for warning */
1391 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1396 if (has_prefix(ins
, PPS_LOCK
, P_LOCK
) && lockcheck
&&
1397 (!itemp_has(temp
,IF_LOCK
) || !is_class(MEMORY
, ins
->oprs
[0].type
))) {
1398 errfunc(ERR_WARNING
| ERR_WARN_LOCK
| ERR_PASS2
,
1399 "instruction is not lockable");
1402 bad_hle_warn(ins
, hleok
);
1405 * when BND prefix is set by DEFAULT directive,
1406 * BND prefix is added to every appropriate instruction line
1407 * unless it is overridden by NOBND prefix.
1410 (itemp_has(temp
, IF_BND
) && !has_prefix(ins
, PPS_REP
, P_NOBND
)))
1411 ins
->prefixes
[PPS_REP
] = P_BND
;
1416 static inline unsigned int emit_rex(insn
*ins
, int32_t segment
, int64_t offset
, int bits
)
1419 if ((ins
->rex
& REX_MASK
) &&
1420 !(ins
->rex
& (REX_V
| REX_EV
)) &&
1422 int rex
= (ins
->rex
& REX_MASK
) | REX_P
;
1423 out(offset
, segment
, &rex
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1424 ins
->rex_done
= true;
1432 static void gencode(int32_t segment
, int64_t offset
, int bits
,
1433 insn
* ins
, const struct itemplate
*temp
,
1441 struct operand
*opx
;
1442 const uint8_t *codes
= temp
->code
;
1444 enum ea_type eat
= EA_SCALAR
;
1446 ins
->rex_done
= false;
1450 op1
= (c
& 3) + ((opex
& 1) << 2);
1451 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
1452 opx
= &ins
->oprs
[op1
];
1453 opex
= 0; /* For the next iteration */
1460 offset
+= emit_rex(ins
, segment
, offset
, bits
);
1461 out(offset
, segment
, codes
, OUT_RAWDATA
, c
, NO_SEG
, NO_SEG
);
1473 offset
+= emit_rex(ins
, segment
, offset
, bits
);
1474 bytes
[0] = *codes
++ + (regval(opx
) & 7);
1475 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1483 if (opx
->offset
< -256 || opx
->offset
> 255) {
1484 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1485 "byte value exceeds bounds");
1487 out_imm8(offset
, segment
, opx
, -1);
1492 if (opx
->offset
< 0 || opx
->offset
> 255)
1493 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1494 "unsigned byte value exceeds bounds");
1495 out_imm8(offset
, segment
, opx
, 1);
1500 warn_overflow_opd(opx
, 2);
1502 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1503 opx
->segment
, opx
->wrt
);
1508 if (opx
->type
& (BITS16
| BITS32
))
1509 size
= (opx
->type
& BITS16
) ? 2 : 4;
1511 size
= (bits
== 16) ? 2 : 4;
1512 warn_overflow_opd(opx
, size
);
1514 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1515 opx
->segment
, opx
->wrt
);
1520 warn_overflow_opd(opx
, 4);
1522 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1523 opx
->segment
, opx
->wrt
);
1529 size
= ins
->addr_size
>> 3;
1530 warn_overflow_opd(opx
, size
);
1531 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1532 opx
->segment
, opx
->wrt
);
1537 if (opx
->segment
!= segment
) {
1539 out(offset
, segment
, &data
,
1540 OUT_REL1ADR
, insn_end
- offset
,
1541 opx
->segment
, opx
->wrt
);
1543 data
= opx
->offset
- insn_end
;
1544 if (data
> 127 || data
< -128)
1545 errfunc(ERR_NONFATAL
, "short jump is out of range");
1546 out(offset
, segment
, &data
,
1547 OUT_ADDRESS
, 1, NO_SEG
, NO_SEG
);
1553 data
= (int64_t)opx
->offset
;
1554 out(offset
, segment
, &data
, OUT_ADDRESS
, 8,
1555 opx
->segment
, opx
->wrt
);
1560 if (opx
->segment
!= segment
) {
1562 out(offset
, segment
, &data
,
1563 OUT_REL2ADR
, insn_end
- offset
,
1564 opx
->segment
, opx
->wrt
);
1566 data
= opx
->offset
- insn_end
;
1567 out(offset
, segment
, &data
,
1568 OUT_ADDRESS
, 2, NO_SEG
, NO_SEG
);
1574 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1575 size
= (opx
->type
& BITS16
) ? 2 : 4;
1577 size
= (bits
== 16) ? 2 : 4;
1578 if (opx
->segment
!= segment
) {
1580 out(offset
, segment
, &data
,
1581 size
== 2 ? OUT_REL2ADR
: OUT_REL4ADR
,
1582 insn_end
- offset
, opx
->segment
, opx
->wrt
);
1584 data
= opx
->offset
- insn_end
;
1585 out(offset
, segment
, &data
,
1586 OUT_ADDRESS
, size
, NO_SEG
, NO_SEG
);
1592 if (opx
->segment
!= segment
) {
1594 out(offset
, segment
, &data
,
1595 OUT_REL4ADR
, insn_end
- offset
,
1596 opx
->segment
, opx
->wrt
);
1598 data
= opx
->offset
- insn_end
;
1599 out(offset
, segment
, &data
,
1600 OUT_ADDRESS
, 4, NO_SEG
, NO_SEG
);
1606 if (opx
->segment
== NO_SEG
)
1607 errfunc(ERR_NONFATAL
, "value referenced by FAR is not"
1610 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1611 outfmt
->segbase(1 + opx
->segment
),
1618 opx
= &ins
->oprs
[c
>> 3];
1619 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1620 opx
= &ins
->oprs
[c
& 7];
1621 if (opx
->segment
!= NO_SEG
|| opx
->wrt
!= NO_SEG
) {
1622 errfunc(ERR_NONFATAL
,
1623 "non-absolute expression not permitted as argument %d",
1626 if (opx
->offset
& ~15) {
1627 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1628 "four-bit argument exceeds bounds");
1630 bytes
[0] |= opx
->offset
& 15;
1632 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1638 opx
= &ins
->oprs
[c
>> 4];
1639 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1641 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1646 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1647 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1653 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1654 (int32_t)data
!= (int64_t)data
) {
1655 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1656 "signed dword immediate exceeds bounds");
1658 out(offset
, segment
, &data
, OUT_ADDRESS
, -4,
1659 opx
->segment
, opx
->wrt
);
1666 ins
->evex_p
[2] |= op_evexflags(&ins
->oprs
[0],
1667 EVEX_P2Z
| EVEX_P2AAA
, 2);
1668 ins
->evex_p
[2] ^= EVEX_P2VP
; /* 1's complement */
1670 /* EVEX.X can be set by either REX or EVEX for different reasons */
1671 bytes
[1] = ((((ins
->rex
& 7) << 5) |
1672 (ins
->evex_p
[0] & (EVEX_P0X
| EVEX_P0RP
))) ^ 0xf0) |
1674 bytes
[2] = ((ins
->rex
& REX_W
) << (7 - 3)) |
1675 ((~ins
->vexreg
& 15) << 3) |
1676 (1 << 2) | (ins
->vex_wlp
& 3);
1677 bytes
[3] = ins
->evex_p
[2];
1678 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 4, NO_SEG
, NO_SEG
);
1685 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
)) ||
1686 ins
->prefixes
[PPS_VEX
] == P_VEX3
) {
1687 bytes
[0] = (ins
->vex_cm
>> 6) ? 0x8f : 0xc4;
1688 bytes
[1] = (ins
->vex_cm
& 31) | ((~ins
->rex
& 7) << 5);
1689 bytes
[2] = ((ins
->rex
& REX_W
) << (7-3)) |
1690 ((~ins
->vexreg
& 15)<< 3) | (ins
->vex_wlp
& 07);
1691 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 3, NO_SEG
, NO_SEG
);
1695 bytes
[1] = ((~ins
->rex
& REX_R
) << (7-2)) |
1696 ((~ins
->vexreg
& 15) << 3) | (ins
->vex_wlp
& 07);
1697 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 2, NO_SEG
, NO_SEG
);
1712 if (ins
->rex
& REX_W
)
1714 else if (ins
->prefixes
[PPS_OSIZE
] == P_O16
)
1716 else if (ins
->prefixes
[PPS_OSIZE
] == P_O32
)
1721 um
= (uint64_t)2 << (s
-1);
1724 if (uv
> 127 && uv
< (uint64_t)-128 &&
1725 (uv
< um
-128 || uv
> um
-1)) {
1726 /* If this wasn't explicitly byte-sized, warn as though we
1727 * had fallen through to the imm16/32/64 case.
1729 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1730 "%s value exceeds bounds",
1731 (opx
->type
& BITS8
) ? "signed byte" :
1736 if (opx
->segment
!= NO_SEG
) {
1738 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1739 opx
->segment
, opx
->wrt
);
1742 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1753 if (bits
== 32 && !has_prefix(ins
, PPS_ASIZE
, P_A16
)) {
1755 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1762 if (bits
!= 32 && !has_prefix(ins
, PPS_ASIZE
, P_A32
)) {
1764 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1799 *bytes
= *codes
++ ^ get_cond_opcode(ins
->condition
);
1800 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1809 *bytes
= c
- 0332 + 0xF2;
1810 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1815 if (ins
->rex
& REX_R
) {
1817 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1820 ins
->rex
&= ~(REX_L
|REX_R
);
1831 if (ins
->oprs
[0].segment
!= NO_SEG
)
1832 errfunc(ERR_PANIC
, "non-constant BSS size in pass two");
1834 int64_t size
= ins
->oprs
[0].offset
;
1836 out(offset
, segment
, NULL
,
1837 OUT_RESERVE
, size
, NO_SEG
, NO_SEG
);
1850 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1860 *bytes
= c
- 0366 + 0x66;
1861 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1869 *bytes
= bits
== 16 ? 3 : 5;
1870 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1904 struct operand
*opy
= &ins
->oprs
[op2
];
1907 /* pick rfield from operand b (opx) */
1908 rflags
= regflag(opx
);
1909 rfield
= nasm_regvals
[opx
->basereg
];
1911 /* rfield is constant */
1916 if (process_ea(opy
, &ea_data
, bits
,
1917 rfield
, rflags
, ins
) != eat
)
1918 errfunc(ERR_NONFATAL
, "invalid effective address");
1921 *p
++ = ea_data
.modrm
;
1922 if (ea_data
.sib_present
)
1926 out(offset
, segment
, bytes
, OUT_RAWDATA
, s
, NO_SEG
, NO_SEG
);
1929 * Make sure the address gets the right offset in case
1930 * the line breaks in the .lst file (BR 1197827)
1935 if (ea_data
.bytes
) {
1936 /* use compressed displacement, if available */
1937 data
= ea_data
.disp8
? ea_data
.disp8
: opy
->offset
;
1940 if (opy
->segment
== segment
) {
1942 if (overflow_signed(data
, ea_data
.bytes
))
1943 warn_overflow(ERR_PASS2
, ea_data
.bytes
);
1944 out(offset
, segment
, &data
, OUT_ADDRESS
,
1945 ea_data
.bytes
, NO_SEG
, NO_SEG
);
1947 /* overflow check in output/linker? */
1948 out(offset
, segment
, &data
, OUT_REL4ADR
,
1949 insn_end
- offset
, opy
->segment
, opy
->wrt
);
1952 int asize
= ins
->addr_size
>> 3;
1953 int atype
= ea_data
.bytes
;
1955 if (overflow_general(data
, asize
) ||
1956 signed_bits(data
, ins
->addr_size
) !=
1957 signed_bits(data
, ea_data
.bytes
<< 3))
1958 warn_overflow(ERR_PASS2
, ea_data
.bytes
);
1960 if (asize
> ea_data
.bytes
) {
1962 * If the address isn't the full width of
1963 * the address size, treat is as signed...
1968 out(offset
, segment
, &data
, OUT_ADDRESS
,
1969 atype
, opy
->segment
, opy
->wrt
);
1977 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1978 ": instruction code \\%o (0x%02X) given", c
, c
);
1984 static opflags_t
regflag(const operand
* o
)
1986 if (!is_register(o
->basereg
))
1987 errfunc(ERR_PANIC
, "invalid operand passed to regflag()");
1988 return nasm_reg_flags
[o
->basereg
];
1991 static int32_t regval(const operand
* o
)
1993 if (!is_register(o
->basereg
))
1994 errfunc(ERR_PANIC
, "invalid operand passed to regval()");
1995 return nasm_regvals
[o
->basereg
];
1998 static int op_rexflags(const operand
* o
, int mask
)
2003 if (!is_register(o
->basereg
))
2004 errfunc(ERR_PANIC
, "invalid operand passed to op_rexflags()");
2006 flags
= nasm_reg_flags
[o
->basereg
];
2007 val
= nasm_regvals
[o
->basereg
];
2009 return rexflags(val
, flags
, mask
);
2012 static int rexflags(int val
, opflags_t flags
, int mask
)
2016 if (val
>= 0 && (val
& 8))
2017 rex
|= REX_B
|REX_X
|REX_R
;
2020 if (!(REG_HIGH
& ~flags
)) /* AH, CH, DH, BH */
2022 else if (!(REG8
& ~flags
) && val
>= 4) /* SPL, BPL, SIL, DIL */
2028 static int evexflags(int val
, decoflags_t deco
,
2029 int mask
, uint8_t byte
)
2035 if (val
>= 0 && (val
& 16))
2036 evex
|= (EVEX_P0RP
| EVEX_P0X
);
2039 if (val
>= 0 && (val
& 16))
2043 if (deco
& OPMASK_MASK
)
2044 evex
|= deco
& EVEX_P2AAA
;
2050 static int op_evexflags(const operand
* o
, int mask
, uint8_t byte
)
2054 val
= nasm_regvals
[o
->basereg
];
2056 return evexflags(val
, o
->decoflags
, mask
, byte
);
2059 static enum match_result
find_match(const struct itemplate
**tempp
,
2061 int32_t segment
, int64_t offset
, int bits
)
2063 const struct itemplate
*temp
;
2064 enum match_result m
, merr
;
2065 opflags_t xsizeflags
[MAX_OPERANDS
];
2066 bool opsizemissing
= false;
2067 int8_t broadcast
= instruction
->evex_brerop
;
2070 /* broadcasting uses a different data element size */
2071 for (i
= 0; i
< instruction
->operands
; i
++)
2073 xsizeflags
[i
] = instruction
->oprs
[i
].decoflags
& BRSIZE_MASK
;
2075 xsizeflags
[i
] = instruction
->oprs
[i
].type
& SIZE_MASK
;
2077 merr
= MERR_INVALOP
;
2079 for (temp
= nasm_instructions
[instruction
->opcode
];
2080 temp
->opcode
!= I_none
; temp
++) {
2081 m
= matches(temp
, instruction
, bits
);
2082 if (m
== MOK_JUMP
) {
2083 if (jmp_match(segment
, offset
, bits
, instruction
, temp
))
2087 } else if (m
== MERR_OPSIZEMISSING
&& !itemp_has(temp
, IF_SX
)) {
2089 * Missing operand size and a candidate for fuzzy matching...
2091 for (i
= 0; i
< temp
->operands
; i
++)
2093 xsizeflags
[i
] |= temp
->deco
[i
] & BRSIZE_MASK
;
2095 xsizeflags
[i
] |= temp
->opd
[i
] & SIZE_MASK
;
2096 opsizemissing
= true;
2100 if (merr
== MOK_GOOD
)
2104 /* No match, but see if we can get a fuzzy operand size match... */
2108 for (i
= 0; i
< instruction
->operands
; i
++) {
2110 * We ignore extrinsic operand sizes on registers, so we should
2111 * never try to fuzzy-match on them. This also resolves the case
2112 * when we have e.g. "xmmrm128" in two different positions.
2114 if (is_class(REGISTER
, instruction
->oprs
[i
].type
))
2117 /* This tests if xsizeflags[i] has more than one bit set */
2118 if ((xsizeflags
[i
] & (xsizeflags
[i
]-1)))
2119 goto done
; /* No luck */
2121 if (i
== broadcast
) {
2122 instruction
->oprs
[i
].decoflags
|= xsizeflags
[i
];
2123 instruction
->oprs
[i
].type
|= (xsizeflags
[i
] == BR_BITS32
?
2126 instruction
->oprs
[i
].type
|= xsizeflags
[i
]; /* Set the size */
2130 /* Try matching again... */
2131 for (temp
= nasm_instructions
[instruction
->opcode
];
2132 temp
->opcode
!= I_none
; temp
++) {
2133 m
= matches(temp
, instruction
, bits
);
2134 if (m
== MOK_JUMP
) {
2135 if (jmp_match(segment
, offset
, bits
, instruction
, temp
))
2142 if (merr
== MOK_GOOD
)
2151 static uint8_t get_broadcast_num(opflags_t opflags
, opflags_t brsize
)
2153 opflags_t opsize
= opflags
& SIZE_MASK
;
2157 * Due to discontinuity between BITS64 and BITS128 (BITS80),
2158 * this cannot be a simple arithmetic calculation.
2160 if (brsize
> BITS64
)
2162 "size of broadcasting element is greater than 64 bits");
2166 brcast_num
= BITS64
/ brsize
;
2169 brcast_num
= (opsize
/ BITS128
) * (BITS64
/ brsize
) * 2;
2176 static enum match_result
matches(const struct itemplate
*itemp
,
2177 insn
*instruction
, int bits
)
2179 opflags_t size
[MAX_OPERANDS
], asize
;
2180 bool opsizemissing
= false;
2186 if (itemp
->opcode
!= instruction
->opcode
)
2187 return MERR_INVALOP
;
2190 * Count the operands
2192 if (itemp
->operands
!= instruction
->operands
)
2193 return MERR_INVALOP
;
2198 if (!(optimizing
> 0) && itemp_has(itemp
, IF_OPT
))
2199 return MERR_INVALOP
;
2204 switch (instruction
->prefixes
[PPS_VEX
]) {
2206 if (!itemp_has(itemp
, IF_EVEX
))
2207 return MERR_ENCMISMATCH
;
2211 if (!itemp_has(itemp
, IF_VEX
))
2212 return MERR_ENCMISMATCH
;
2219 * Check that no spurious colons or TOs are present
2221 for (i
= 0; i
< itemp
->operands
; i
++)
2222 if (instruction
->oprs
[i
].type
& ~itemp
->opd
[i
] & (COLON
| TO
))
2223 return MERR_INVALOP
;
2226 * Process size flags
2228 switch (itemp_smask(itemp
)) {
2229 case IF_GENBIT(IF_SB
):
2232 case IF_GENBIT(IF_SW
):
2235 case IF_GENBIT(IF_SD
):
2238 case IF_GENBIT(IF_SQ
):
2241 case IF_GENBIT(IF_SO
):
2244 case IF_GENBIT(IF_SY
):
2247 case IF_GENBIT(IF_SZ
):
2250 case IF_GENBIT(IF_SIZE
):
2271 if (itemp_armask(itemp
)) {
2272 /* S- flags only apply to a specific operand */
2273 i
= itemp_arg(itemp
);
2274 memset(size
, 0, sizeof size
);
2277 /* S- flags apply to all operands */
2278 for (i
= 0; i
< MAX_OPERANDS
; i
++)
2283 * Check that the operand flags all match up,
2284 * it's a bit tricky so lets be verbose:
2286 * 1) Find out the size of operand. If instruction
2287 * doesn't have one specified -- we're trying to
2288 * guess it either from template (IF_S* flag) or
2291 * 2) If template operand do not match the instruction OR
2292 * template has an operand size specified AND this size differ
2293 * from which instruction has (perhaps we got it from code bits)
2295 * a) Check that only size of instruction and operand is differ
2296 * other characteristics do match
2297 * b) Perhaps it's a register specified in instruction so
2298 * for such a case we just mark that operand as "size
2299 * missing" and this will turn on fuzzy operand size
2300 * logic facility (handled by a caller)
2302 for (i
= 0; i
< itemp
->operands
; i
++) {
2303 opflags_t type
= instruction
->oprs
[i
].type
;
2304 decoflags_t deco
= instruction
->oprs
[i
].decoflags
;
2305 bool is_broadcast
= deco
& BRDCAST_MASK
;
2306 uint8_t brcast_num
= 0;
2307 opflags_t template_opsize
, insn_opsize
;
2309 if (!(type
& SIZE_MASK
))
2312 insn_opsize
= type
& SIZE_MASK
;
2313 if (!is_broadcast
) {
2314 template_opsize
= itemp
->opd
[i
] & SIZE_MASK
;
2316 decoflags_t deco_brsize
= itemp
->deco
[i
] & BRSIZE_MASK
;
2318 * when broadcasting, the element size depends on
2319 * the instruction type. decorator flag should match.
2323 template_opsize
= (deco_brsize
== BR_BITS32
? BITS32
: BITS64
);
2324 /* calculate the proper number : {1to<brcast_num>} */
2325 brcast_num
= get_broadcast_num(itemp
->opd
[i
], template_opsize
);
2327 template_opsize
= 0;
2331 if ((itemp
->opd
[i
] & ~type
& ~SIZE_MASK
) ||
2332 (deco
& ~itemp
->deco
[i
] & ~BRNUM_MASK
)) {
2333 return MERR_INVALOP
;
2334 } else if (template_opsize
) {
2335 if (template_opsize
!= insn_opsize
) {
2337 return MERR_INVALOP
;
2338 } else if (!is_class(REGISTER
, type
)) {
2340 * Note: we don't honor extrinsic operand sizes for registers,
2341 * so "missing operand size" for a register should be
2342 * considered a wildcard match rather than an error.
2344 opsizemissing
= true;
2346 } else if (is_broadcast
&&
2348 (2U << ((deco
& BRNUM_MASK
) >> BRNUM_SHIFT
)))) {
2350 * broadcasting opsize matches but the number of repeated memory
2351 * element does not match.
2352 * if 64b double precision float is broadcasted to ymm (256b),
2353 * broadcasting decorator must be {1to4}.
2355 return MERR_BRNUMMISMATCH
;
2361 return MERR_OPSIZEMISSING
;
2364 * Check operand sizes
2366 if (itemp_has(itemp
, IF_SM
) || itemp_has(itemp
, IF_SM2
)) {
2367 oprs
= (itemp_has(itemp
, IF_SM2
) ? 2 : itemp
->operands
);
2368 for (i
= 0; i
< oprs
; i
++) {
2369 asize
= itemp
->opd
[i
] & SIZE_MASK
;
2371 for (i
= 0; i
< oprs
; i
++)
2377 oprs
= itemp
->operands
;
2380 for (i
= 0; i
< itemp
->operands
; i
++) {
2381 if (!(itemp
->opd
[i
] & SIZE_MASK
) &&
2382 (instruction
->oprs
[i
].type
& SIZE_MASK
& ~size
[i
]))
2383 return MERR_OPSIZEMISMATCH
;
2387 * Check template is okay at the set cpu level
2389 if (iflag_cmp_cpu_level(&insns_flags
[itemp
->iflag_idx
], &cpu
) > 0)
2393 * Verify the appropriate long mode flag.
2395 if (itemp_has(itemp
, (bits
== 64 ? IF_NOLONG
: IF_LONG
)))
2396 return MERR_BADMODE
;
2399 * If we have a HLE prefix, look for the NOHLE flag
2401 if (itemp_has(itemp
, IF_NOHLE
) &&
2402 (has_prefix(instruction
, PPS_REP
, P_XACQUIRE
) ||
2403 has_prefix(instruction
, PPS_REP
, P_XRELEASE
)))
2407 * Check if special handling needed for Jumps
2409 if ((itemp
->code
[0] & ~1) == 0370)
2413 * Check if BND prefix is allowed.
2414 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
2416 if (!itemp_has(itemp
, IF_BND
) &&
2417 (has_prefix(instruction
, PPS_REP
, P_BND
) ||
2418 has_prefix(instruction
, PPS_REP
, P_NOBND
)))
2420 else if (itemp_has(itemp
, IF_BND
) &&
2421 (has_prefix(instruction
, PPS_REP
, P_REPNE
) ||
2422 has_prefix(instruction
, PPS_REP
, P_REPNZ
)))
2423 return MERR_BADREPNE
;
2429 * Check if ModR/M.mod should/can be 01.
2430 * - EAF_BYTEOFFS is set
2431 * - offset can fit in a byte when EVEX is not used
2432 * - offset can be compressed when EVEX is used
2434 #define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2435 (o >= -128 && o <= 127 && \
2436 seg == NO_SEG && !forw_ref && \
2437 !(input->eaflags & EAF_WORDOFFS) && \
2438 !(ins->rex & REX_EV)) || \
2439 (ins->rex & REX_EV && \
2440 is_disp8n(input, ins, &output->disp8)))
2442 static enum ea_type
process_ea(operand
*input
, ea
*output
, int bits
,
2443 int rfield
, opflags_t rflags
, insn
*ins
)
2445 bool forw_ref
= !!(input
->opflags
& OPFLAG_UNKNOWN
);
2446 int addrbits
= ins
->addr_size
;
2447 int eaflags
= input
->eaflags
;
2449 output
->type
= EA_SCALAR
;
2450 output
->rip
= false;
2453 /* REX flags for the rfield operand */
2454 output
->rex
|= rexflags(rfield
, rflags
, REX_R
| REX_P
| REX_W
| REX_H
);
2455 /* EVEX.R' flag for the REG operand */
2456 ins
->evex_p
[0] |= evexflags(rfield
, 0, EVEX_P0RP
, 0);
2458 if (is_class(REGISTER
, input
->type
)) {
2460 * It's a direct register.
2462 if (!is_register(input
->basereg
))
2465 if (!is_reg_class(REG_EA
, input
->basereg
))
2468 /* broadcasting is not available with a direct register operand. */
2469 if (input
->decoflags
& BRDCAST_MASK
) {
2470 nasm_error(ERR_NONFATAL
, "Broadcasting not allowed from a register");
2474 output
->rex
|= op_rexflags(input
, REX_B
| REX_P
| REX_W
| REX_H
);
2475 ins
->evex_p
[0] |= op_evexflags(input
, EVEX_P0X
, 0);
2476 output
->sib_present
= false; /* no SIB necessary */
2477 output
->bytes
= 0; /* no offset necessary either */
2478 output
->modrm
= GEN_MODRM(3, rfield
, nasm_regvals
[input
->basereg
]);
2481 * It's a memory reference.
2484 /* Embedded rounding or SAE is not available with a mem ref operand. */
2485 if (input
->decoflags
& (ER
| SAE
)) {
2486 nasm_error(ERR_NONFATAL
,
2487 "Embedded rounding is available only with reg-reg op.");
2491 if (input
->basereg
== -1 &&
2492 (input
->indexreg
== -1 || input
->scale
== 0)) {
2494 * It's a pure offset.
2496 if (bits
== 64 && ((input
->type
& IP_REL
) == IP_REL
) &&
2497 input
->segment
== NO_SEG
) {
2498 nasm_error(ERR_WARNING
| ERR_PASS1
, "absolute address can not be RIP-relative");
2499 input
->type
&= ~IP_REL
;
2500 input
->type
|= MEMORY
;
2504 !(IP_REL
& ~input
->type
) && (eaflags
& EAF_MIB
)) {
2505 nasm_error(ERR_NONFATAL
, "RIP-relative addressing is prohibited for mib.");
2509 if (eaflags
& EAF_BYTEOFFS
||
2510 (eaflags
& EAF_WORDOFFS
&&
2511 input
->disp_size
!= (addrbits
!= 16 ? 32 : 16))) {
2512 nasm_error(ERR_WARNING
| ERR_PASS1
, "displacement size ignored on absolute address");
2515 if (bits
== 64 && (~input
->type
& IP_REL
)) {
2516 output
->sib_present
= true;
2517 output
->sib
= GEN_SIB(0, 4, 5);
2519 output
->modrm
= GEN_MODRM(0, rfield
, 4);
2520 output
->rip
= false;
2522 output
->sib_present
= false;
2523 output
->bytes
= (addrbits
!= 16 ? 4 : 2);
2524 output
->modrm
= GEN_MODRM(0, rfield
, (addrbits
!= 16 ? 5 : 6));
2525 output
->rip
= bits
== 64;
2529 * It's an indirection.
2531 int i
= input
->indexreg
, b
= input
->basereg
, s
= input
->scale
;
2532 int32_t seg
= input
->segment
;
2533 int hb
= input
->hintbase
, ht
= input
->hinttype
;
2534 int t
, it
, bt
; /* register numbers */
2535 opflags_t x
, ix
, bx
; /* register flags */
2538 i
= -1; /* make this easy, at least */
2540 if (is_register(i
)) {
2541 it
= nasm_regvals
[i
];
2542 ix
= nasm_reg_flags
[i
];
2548 if (is_register(b
)) {
2549 bt
= nasm_regvals
[b
];
2550 bx
= nasm_reg_flags
[b
];
2556 /* if either one are a vector register... */
2557 if ((ix
|bx
) & (XMMREG
|YMMREG
|ZMMREG
) & ~REG_EA
) {
2558 opflags_t sok
= BITS32
| BITS64
;
2559 int32_t o
= input
->offset
;
2560 int mod
, scale
, index
, base
;
2563 * For a vector SIB, one has to be a vector and the other,
2564 * if present, a GPR. The vector must be the index operand.
2566 if (it
== -1 || (bx
& (XMMREG
|YMMREG
|ZMMREG
) & ~REG_EA
)) {
2572 t
= bt
, bt
= it
, it
= t
;
2573 x
= bx
, bx
= ix
, ix
= x
;
2579 if (!(REG64
& ~bx
) || !(REG32
& ~bx
))
2586 * While we're here, ensure the user didn't specify
2589 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2592 if (addrbits
== 16 ||
2593 (addrbits
== 32 && !(sok
& BITS32
)) ||
2594 (addrbits
== 64 && !(sok
& BITS64
)))
2597 output
->type
= ((ix
& ZMMREG
& ~REG_EA
) ? EA_ZMMVSIB
2598 : ((ix
& YMMREG
& ~REG_EA
)
2599 ? EA_YMMVSIB
: EA_XMMVSIB
));
2601 output
->rex
|= rexflags(it
, ix
, REX_X
);
2602 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2603 ins
->evex_p
[2] |= evexflags(it
, 0, EVEX_P2VP
, 2);
2605 index
= it
& 7; /* it is known to be != -1 */
2620 default: /* then what the smeg is it? */
2621 goto err
; /* panic */
2629 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2630 seg
== NO_SEG
&& !forw_ref
&&
2631 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2633 else if (IS_MOD_01())
2639 output
->sib_present
= true;
2640 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2641 output
->modrm
= GEN_MODRM(mod
, rfield
, 4);
2642 output
->sib
= GEN_SIB(scale
, index
, base
);
2643 } else if ((ix
|bx
) & (BITS32
|BITS64
)) {
2645 * it must be a 32/64-bit memory reference. Firstly we have
2646 * to check that all registers involved are type E/Rxx.
2648 opflags_t sok
= BITS32
| BITS64
;
2649 int32_t o
= input
->offset
;
2652 if (!(REG64
& ~ix
) || !(REG32
& ~ix
))
2660 goto err
; /* Invalid register */
2661 if (~sok
& bx
& SIZE_MASK
)
2662 goto err
; /* Invalid size */
2667 * While we're here, ensure the user didn't specify
2670 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2673 if (addrbits
== 16 ||
2674 (addrbits
== 32 && !(sok
& BITS32
)) ||
2675 (addrbits
== 64 && !(sok
& BITS64
)))
2678 /* now reorganize base/index */
2679 if (s
== 1 && bt
!= it
&& bt
!= -1 && it
!= -1 &&
2680 ((hb
== b
&& ht
== EAH_NOTBASE
) ||
2681 (hb
== i
&& ht
== EAH_MAKEBASE
))) {
2682 /* swap if hints say so */
2683 t
= bt
, bt
= it
, it
= t
;
2684 x
= bx
, bx
= ix
, ix
= x
;
2687 if (bt
== -1 && s
== 1 && !(hb
== i
&& ht
== EAH_NOTBASE
)) {
2688 /* make single reg base, unless hint */
2689 bt
= it
, bx
= ix
, it
= -1, ix
= 0;
2691 if (eaflags
& EAF_MIB
) {
2692 /* only for mib operands */
2693 if (it
== -1 && (hb
== b
&& ht
== EAH_NOTBASE
)) {
2695 * make a single reg index [reg*1].
2696 * gas uses this form for an explicit index register.
2698 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2700 if ((ht
== EAH_SUMMED
) && bt
== -1) {
2701 /* separate once summed index into [base, index] */
2702 bt
= it
, bx
= ix
, s
--;
2705 if (((s
== 2 && it
!= REG_NUM_ESP
&&
2706 (!(eaflags
& EAF_TIMESTWO
) || (ht
== EAH_SUMMED
))) ||
2707 s
== 3 || s
== 5 || s
== 9) && bt
== -1) {
2708 /* convert 3*EAX to EAX+2*EAX */
2709 bt
= it
, bx
= ix
, s
--;
2711 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
&&
2712 (eaflags
& EAF_TIMESTWO
) &&
2713 (hb
== b
&& ht
== EAH_NOTBASE
)) {
2715 * convert [NOSPLIT EAX*1]
2716 * to sib format with 0x0 displacement - [EAX*1+0].
2718 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2721 if (s
== 1 && it
== REG_NUM_ESP
) {
2722 /* swap ESP into base if scale is 1 */
2723 t
= it
, it
= bt
, bt
= t
;
2724 x
= ix
, ix
= bx
, bx
= x
;
2726 if (it
== REG_NUM_ESP
||
2727 (s
!= 1 && s
!= 2 && s
!= 4 && s
!= 8 && it
!= -1))
2728 goto err
; /* wrong, for various reasons */
2730 output
->rex
|= rexflags(it
, ix
, REX_X
);
2731 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2733 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
) {
2742 if (rm
!= REG_NUM_EBP
&& o
== 0 &&
2743 seg
== NO_SEG
&& !forw_ref
&&
2744 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2746 else if (IS_MOD_01())
2752 output
->sib_present
= false;
2753 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2754 output
->modrm
= GEN_MODRM(mod
, rfield
, rm
);
2757 int mod
, scale
, index
, base
;
2777 default: /* then what the smeg is it? */
2778 goto err
; /* panic */
2786 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2787 seg
== NO_SEG
&& !forw_ref
&&
2788 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2790 else if (IS_MOD_01())
2796 output
->sib_present
= true;
2797 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2798 output
->modrm
= GEN_MODRM(mod
, rfield
, 4);
2799 output
->sib
= GEN_SIB(scale
, index
, base
);
2801 } else { /* it's 16-bit */
2803 int16_t o
= input
->offset
;
2805 /* check for 64-bit long mode */
2809 /* check all registers are BX, BP, SI or DI */
2810 if ((b
!= -1 && b
!= R_BP
&& b
!= R_BX
&& b
!= R_SI
&& b
!= R_DI
) ||
2811 (i
!= -1 && i
!= R_BP
&& i
!= R_BX
&& i
!= R_SI
&& i
!= R_DI
))
2814 /* ensure the user didn't specify DWORD/QWORD */
2815 if (input
->disp_size
== 32 || input
->disp_size
== 64)
2818 if (s
!= 1 && i
!= -1)
2819 goto err
; /* no can do, in 16-bit EA */
2820 if (b
== -1 && i
!= -1) {
2825 if ((b
== R_SI
|| b
== R_DI
) && i
!= -1) {
2830 /* have BX/BP as base, SI/DI index */
2832 goto err
; /* shouldn't ever happen, in theory */
2833 if (i
!= -1 && b
!= -1 &&
2834 (i
== R_BP
|| i
== R_BX
|| b
== R_SI
|| b
== R_DI
))
2835 goto err
; /* invalid combinations */
2836 if (b
== -1) /* pure offset: handled above */
2837 goto err
; /* so if it gets to here, panic! */
2841 switch (i
* 256 + b
) {
2842 case R_SI
* 256 + R_BX
:
2845 case R_DI
* 256 + R_BX
:
2848 case R_SI
* 256 + R_BP
:
2851 case R_DI
* 256 + R_BP
:
2869 if (rm
== -1) /* can't happen, in theory */
2870 goto err
; /* so panic if it does */
2872 if (o
== 0 && seg
== NO_SEG
&& !forw_ref
&& rm
!= 6 &&
2873 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2875 else if (IS_MOD_01())
2880 output
->sib_present
= false; /* no SIB - it's 16-bit */
2881 output
->bytes
= mod
; /* bytes of offset needed */
2882 output
->modrm
= GEN_MODRM(mod
, rfield
, rm
);
2887 output
->size
= 1 + output
->sib_present
+ output
->bytes
;
2888 return output
->type
;
2891 return output
->type
= EA_INVALID
;
2894 static void add_asp(insn
*ins
, int addrbits
)
2899 valid
= (addrbits
== 64) ? 64|32 : 32|16;
2901 switch (ins
->prefixes
[PPS_ASIZE
]) {
2912 valid
&= (addrbits
== 32) ? 16 : 32;
2918 for (j
= 0; j
< ins
->operands
; j
++) {
2919 if (is_class(MEMORY
, ins
->oprs
[j
].type
)) {
2922 /* Verify as Register */
2923 if (!is_register(ins
->oprs
[j
].indexreg
))
2926 i
= nasm_reg_flags
[ins
->oprs
[j
].indexreg
];
2928 /* Verify as Register */
2929 if (!is_register(ins
->oprs
[j
].basereg
))
2932 b
= nasm_reg_flags
[ins
->oprs
[j
].basereg
];
2934 if (ins
->oprs
[j
].scale
== 0)
2938 int ds
= ins
->oprs
[j
].disp_size
;
2939 if ((addrbits
!= 64 && ds
> 8) ||
2940 (addrbits
== 64 && ds
== 16))
2960 if (valid
& addrbits
) {
2961 ins
->addr_size
= addrbits
;
2962 } else if (valid
& ((addrbits
== 32) ? 16 : 32)) {
2963 /* Add an address size prefix */
2964 ins
->prefixes
[PPS_ASIZE
] = (addrbits
== 32) ? P_A16
: P_A32
;;
2965 ins
->addr_size
= (addrbits
== 32) ? 16 : 32;
2968 errfunc(ERR_NONFATAL
, "impossible combination of address sizes");
2969 ins
->addr_size
= addrbits
; /* Error recovery */
2972 defdisp
= ins
->addr_size
== 16 ? 16 : 32;
2974 for (j
= 0; j
< ins
->operands
; j
++) {
2975 if (!(MEM_OFFS
& ~ins
->oprs
[j
].type
) &&
2976 (ins
->oprs
[j
].disp_size
? ins
->oprs
[j
].disp_size
: defdisp
) != ins
->addr_size
) {
2978 * mem_offs sizes must match the address size; if not,
2979 * strip the MEM_OFFS bit and match only EA instructions
2981 ins
->oprs
[j
].type
&= ~(MEM_OFFS
& ~MEMORY
);