blob: ed8b9f8dd440145be300e2f8f9e233f0d6c4b9c4 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
H. Peter Anvind24dd5f2016-02-08 10:32:13 -08003 * Copyright 1996-2016 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
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.
Cyrill Gorcunov1de95002009-11-06 00:08:38 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * 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.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * assemble.c code generation for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +040037 * Bytecode specification
38 * ----------------------
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070039 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +040040 *
41 * Codes Mnemonic Explanation
42 *
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.
76 *
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.
81 *
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070082 * EVEX prefixes are followed by the sequence:
83 * \cm\wlp\tup where cm is:
84 * cc 000 0mm
85 * c = 2 for EVEX and m is the legacy escape (0f, 0f38, 0f3a)
86 * and wlp is:
87 * 00 wwl lpp
88 * [l0] ll = 0 (.128, .lz)
89 * [l1] ll = 1 (.256)
90 * [l2] ll = 2 (.512)
91 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
92 *
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
97 *
98 * [p0] pp = 0 for no prefix
99 * [60] pp = 1 for legacy prefix 60
100 * [f3] pp = 2
101 * [f2] pp = 3
102 *
103 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
104 * (compressed displacement encoding)
105 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400106 * \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.
H. Peter Anvind85d2502008-05-04 17:53:31 -0700111 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700112 * VEX/XOP prefixes are followed by the sequence:
113 * \tmm\wlp where mm is the M field; and wlp is:
H. Peter Anvin421059c2010-08-16 14:56:33 -0700114 * 00 wwl lpp
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700115 * [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)
H. Peter Anvin421059c2010-08-16 14:56:33 -0700118 *
H. Peter Anvin978c2172010-08-16 13:48:43 -0700119 * [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
H. Peter Anvinbd420c72008-05-22 11:24:35 -0700123 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700124 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
H. Peter Anvind85d2502008-05-04 17:53:31 -0700125 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400126 * \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"
Cyrill Gorcunov8a5d3e62014-08-25 20:04:30 +0400163 * \360 np no SSE prefix (== \364\331)
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400164 * \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
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000176 */
177
H. Peter Anvinfe501952007-10-02 21:53:51 -0700178#include "compiler.h"
179
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000180#include <stdio.h>
181#include <string.h>
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800182#include <stdlib.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +0000183#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000184
185#include "nasm.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000186#include "nasmlib.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000187#include "assemble.h"
188#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700189#include "tables.h"
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -0800190#include "disp8.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000191
H. Peter Anvin65289e82009-07-25 17:25:11 -0700192enum match_result {
193 /*
194 * Matching errors. These should be sorted so that more specific
195 * errors come later in the sequence.
196 */
197 MERR_INVALOP,
198 MERR_OPSIZEMISSING,
199 MERR_OPSIZEMISMATCH,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700200 MERR_BRNUMMISMATCH,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700201 MERR_BADCPU,
202 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800203 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700204 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700205 MERR_BADBND,
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800206 MERR_BADREPNE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700207 /*
208 * Matching success; the conditional ones first
209 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400210 MOK_JUMP, /* Matching OK but needs jmp_match() */
211 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700212};
213
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000214typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700215 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 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700220 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000221} ea;
222
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400223#define GEN_SIB(scale, index, base) \
224 (((scale) << 6) | ((index) << 3) | ((base)))
225
226#define GEN_MODRM(mod, reg, rm) \
227 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
228
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400229static iflag_t cpu; /* cpu level received from nasm.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000230static efunc errfunc;
231static struct ofmt *outfmt;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000232static ListGen *list;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000233
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800234static int64_t calcsize(int32_t, int64_t, int, insn *,
235 const struct itemplate *);
H. Peter Anvin833caea2008-10-04 19:02:30 -0700236static void gencode(int32_t segment, int64_t offset, int bits,
237 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400238 int64_t insn_end);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700239static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400240 insn *instruction,
241 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700242static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700243static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000244static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700245static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000246static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700247static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700248static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000249
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700250static enum ea_type process_ea(operand *, ea *, int, int, opflags_t, insn *);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700251
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400252static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000253{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700254 return ins->prefixes[pos] == prefix;
255}
256
257static void assert_no_prefix(insn * ins, enum prefix_pos pos)
258{
259 if (ins->prefixes[pos])
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400260 errfunc(ERR_NONFATAL, "invalid %s prefix",
261 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700262}
263
264static const char *size_name(int size)
265{
266 switch (size) {
267 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400268 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700269 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400270 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700271 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400272 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700273 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400274 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700275 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400276 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700277 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400278 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700279 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400280 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700281 case 64:
282 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700283 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400284 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000285 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700286}
287
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400288static void warn_overflow(int pass, int size)
289{
290 errfunc(ERR_WARNING | pass | ERR_WARN_NOV,
291 "%s data exceeds bounds", size_name(size));
292}
293
294static void warn_overflow_const(int64_t data, int size)
295{
296 if (overflow_general(data, size))
297 warn_overflow(ERR_PASS1, size);
298}
299
300static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700301{
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100302 if (o->wrt == NO_SEG && o->segment == NO_SEG) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400303 if (overflow_general(o->offset, size))
304 warn_overflow(ERR_PASS2, size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700305 }
306}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400307
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000308/*
H. Peter Anvinb6412502016-02-11 21:07:40 -0800309 * Size of an address relocation, or zero if not an address
310 */
311static int addrsize(enum out_type type, uint64_t size)
312{
313 switch (type) {
314 case OUT_ADDRESS:
315 return abs((int)size);
316 case OUT_REL1ADR:
317 return 1;
318 case OUT_REL2ADR:
319 return 2;
320 case OUT_REL4ADR:
321 return 4;
322 case OUT_REL8ADR:
323 return 8;
324 default:
325 return 0;
326 }
327}
328
329/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000330 * 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
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800332 * generator at the same time, flatten unnecessary relocations,
333 * and verify backend compatibility.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000334 */
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800335static void out(int64_t offset, int32_t segto, const void *data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800336 enum out_type type, uint64_t size,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400337 int32_t segment, int32_t wrt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000338{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000339 static int32_t lineno = 0; /* static!!! */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000340 static char *lnfname = NULL;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800341 uint8_t p[8];
H. Peter Anvinb6412502016-02-11 21:07:40 -0800342 int asize = addrsize(type, size); /* Address size in bytes */
H. Peter Anvin33814132016-02-11 20:40:07 -0800343 const int amax = outfmt->maxbits >> 3; /* Maximum address size in bytes */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000344
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800345 if (type == OUT_ADDRESS && segment == NO_SEG && wrt == NO_SEG) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400346 /*
347 * This is a non-relocated address, and we're going to
348 * convert it into RAWDATA format.
349 */
350 uint8_t *q = p;
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800351
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800352 if (asize > 8) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400353 errfunc(ERR_PANIC, "OUT_ADDRESS with size > 8");
354 return;
355 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700356
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800357 WRITEADDR(q, *(int64_t *)data, asize);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400358 data = p;
359 type = OUT_RAWDATA;
H. Peter Anvinca351fa2016-02-12 13:46:39 -0800360 size = asize;
H. Peter Anvinb6412502016-02-11 21:07:40 -0800361 asize = 0; /* No longer an address */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000362 }
363
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800364 list->output(offset, data, type, size);
365
Frank Kotlerabebb082003-09-06 04:45:37 +0000366 /*
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
373 */
374
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400375 if (src_get(&lineno, &lnfname))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000376 outfmt->current_dfmt->linenum(lnfname, lineno, segto);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000377
H. Peter Anvinb6412502016-02-11 21:07:40 -0800378 if (asize && asize > amax) {
379 if (type != OUT_ADDRESS || (int)size < 0) {
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800380 errfunc(ERR_NONFATAL,
381 "%d-bit signed relocation unsupported by output format %s\n",
H. Peter Anvin33814132016-02-11 20:40:07 -0800382 asize << 3, outfmt->shortname);
H. Peter Anvinca351fa2016-02-12 13:46:39 -0800383 size = asize;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800384 } else {
385 errfunc(ERR_WARNING | ERR_WARN_ZEXTRELOC,
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800386 "%d-bit unsigned relocation zero-extended from %d bits\n",
H. Peter Anvinb03d91e2016-02-11 21:13:54 -0800387 asize << 3, outfmt->maxbits);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800388 outfmt->output(segto, data, type, amax, segment, wrt);
H. Peter Anvinca351fa2016-02-12 13:46:39 -0800389 size = asize - amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800390 }
391 data = zero_buffer;
392 type = OUT_RAWDATA;
H. Peter Anvinb03d91e2016-02-11 21:13:54 -0800393 segment = wrt = NO_SEG;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800394 }
395
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800396 outfmt->output(segto, data, type, size, segment, wrt);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000397}
398
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800399static void out_imm8(int64_t offset, int32_t segment,
400 struct operand *opx, int asize)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400401{
402 if (opx->segment != NO_SEG) {
403 uint64_t data = opx->offset;
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800404 out(offset, segment, &data, OUT_ADDRESS, asize, opx->segment, opx->wrt);
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400405 } else {
406 uint8_t byte = opx->offset;
407 out(offset, segment, &byte, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
408 }
409}
410
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700411static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800412 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000413{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800414 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800415 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000416 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800417 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000418
H. Peter Anvin755f5212012-02-25 11:41:34 -0800419 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700420 return false;
421 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400422 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700423 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400424 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700425
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800426 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100427
Victor van den Elzen154e5922009-02-25 17:32:00 +0100428 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100429 /* Be optimistic in pass 1 */
430 return true;
431
H. Peter Anvine2c80182005-01-15 22:15:51 +0000432 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700433 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000434
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700435 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800436 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
437
438 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
439 /* jmp short (opcode eb) cannot be used with bnd prefix. */
440 ins->prefixes[PPS_REP] = P_none;
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800441 errfunc(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
442 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800443 }
444
445 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000446}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000447
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400448int64_t assemble(int32_t segment, int64_t offset, int bits, iflag_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400449 insn * instruction, struct ofmt *output, efunc error,
450 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000451{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000452 const struct itemplate *temp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000453 int j;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700454 enum match_result m;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800455 int64_t insn_end;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000456 int32_t itimes;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800457 int64_t start = offset;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300458 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000459
H. Peter Anvine2c80182005-01-15 22:15:51 +0000460 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000461 cpu = cp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000462 outfmt = output; /* likewise */
463 list = listgen; /* and again */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000464
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300465 wsize = idata_bytes(instruction->opcode);
466 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000467 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000468
H. Peter Anvineba20a72002-04-30 20:53:55 +0000469 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000471 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000472 if (t < 0)
473 errfunc(ERR_PANIC,
474 "instruction->times < 0 (%ld) in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000475
H. Peter Anvine2c80182005-01-15 22:15:51 +0000476 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400477 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000478 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400479 if (wsize > 8) {
H. Peter Anvin3be5d852008-05-20 14:49:32 -0700480 errfunc(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400481 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000482 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700483 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000484 out(offset, segment, &e->offset,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800485 OUT_ADDRESS, wsize, e->segment, e->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400486 offset += wsize;
487 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700488 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400489 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000490 int align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000491
H. Peter Anvine2c80182005-01-15 22:15:51 +0000492 out(offset, segment, e->stringval,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800493 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000494 align = e->stringlen % wsize;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000495
H. Peter Anvine2c80182005-01-15 22:15:51 +0000496 if (align) {
497 align = wsize - align;
H. Peter Anvin999868f2009-02-09 11:03:33 +0100498 out(offset, segment, zero_buffer,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800499 OUT_RAWDATA, align, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500 }
501 offset += e->stringlen + align;
502 }
503 }
504 if (t > 0 && t == instruction->times - 1) {
505 /*
506 * Dummy call to list->output to give the offset to the
507 * listing module.
508 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800509 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000510 list->uplevel(LIST_TIMES);
511 }
512 }
513 if (instruction->times > 1)
514 list->downlevel(LIST_TIMES);
515 return offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000516 }
517
H. Peter Anvine2c80182005-01-15 22:15:51 +0000518 if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700519 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000520 FILE *fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000521
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400522 fp = fopen(fname, "rb");
523 if (!fp) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000524 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
525 fname);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400526 } else if (fseek(fp, 0L, SEEK_END) < 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000527 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
528 fname);
Philipp Klokedae212d2013-03-31 12:02:30 +0200529 fclose(fp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400530 } else {
H. Peter Anvin518df302008-06-14 16:53:48 -0700531 static char buf[4096];
532 size_t t = instruction->times;
533 size_t base = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400534 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000535
H. Peter Anvine2c80182005-01-15 22:15:51 +0000536 len = ftell(fp);
537 if (instruction->eops->next) {
538 base = instruction->eops->next->offset;
539 len -= base;
540 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700541 len > (size_t)instruction->eops->next->next->offset)
542 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000543 }
544 /*
545 * Dummy call to list->output to give the offset to the
546 * listing module.
547 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800548 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000549 list->uplevel(LIST_INCBIN);
550 while (t--) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700551 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000552
H. Peter Anvine2c80182005-01-15 22:15:51 +0000553 fseek(fp, base, SEEK_SET);
554 l = len;
555 while (l > 0) {
H. Peter Anvin4a5a6df2009-06-27 16:14:18 -0700556 int32_t m;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400557 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000558 if (!m) {
559 /*
560 * This shouldn't happen unless the file
561 * actually changes while we are reading
562 * it.
563 */
564 error(ERR_NONFATAL,
565 "`incbin': unexpected EOF while"
566 " reading file `%s'", fname);
567 t = 0; /* Try to exit cleanly */
568 break;
569 }
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800570 out(offset, segment, buf, OUT_RAWDATA, m,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000571 NO_SEG, NO_SEG);
572 l -= m;
573 }
574 }
575 list->downlevel(LIST_INCBIN);
576 if (instruction->times > 1) {
577 /*
578 * Dummy call to list->output to give the offset to the
579 * listing module.
580 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800581 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000582 list->uplevel(LIST_TIMES);
583 list->downlevel(LIST_TIMES);
584 }
585 fclose(fp);
586 return instruction->times * len;
587 }
588 return 0; /* if we're here, there's an error */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000589 }
590
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700591 /* Check to see if we need an address-size prefix */
592 add_asp(instruction, bits);
593
H. Peter Anvin23595f52009-07-25 17:44:25 -0700594 m = find_match(&temp, instruction, segment, offset, bits);
H. Peter Anvin70653092007-10-19 14:42:29 -0700595
H. Peter Anvin23595f52009-07-25 17:44:25 -0700596 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400597 /* Matches! */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800598 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400599 itimes = instruction->times;
600 if (insn_size < 0) /* shouldn't be, on pass two */
601 error(ERR_PANIC, "errors made it through from pass one");
602 else
603 while (itimes--) {
604 for (j = 0; j < MAXPREFIX; j++) {
605 uint8_t c = 0;
606 switch (instruction->prefixes[j]) {
607 case P_WAIT:
608 c = 0x9B;
609 break;
610 case P_LOCK:
611 c = 0xF0;
612 break;
613 case P_REPNE:
614 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800615 case P_XACQUIRE:
Jin Kyu Song03041092013-10-15 19:38:51 -0700616 case P_BND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400617 c = 0xF2;
618 break;
619 case P_REPE:
620 case P_REPZ:
621 case P_REP:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800622 case P_XRELEASE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400623 c = 0xF3;
624 break;
625 case R_CS:
626 if (bits == 64) {
627 error(ERR_WARNING | ERR_PASS2,
628 "cs segment base generated, but will be ignored in 64-bit mode");
629 }
630 c = 0x2E;
631 break;
632 case R_DS:
633 if (bits == 64) {
634 error(ERR_WARNING | ERR_PASS2,
635 "ds segment base generated, but will be ignored in 64-bit mode");
636 }
637 c = 0x3E;
638 break;
639 case R_ES:
640 if (bits == 64) {
641 error(ERR_WARNING | ERR_PASS2,
642 "es segment base generated, but will be ignored in 64-bit mode");
643 }
644 c = 0x26;
645 break;
646 case R_FS:
647 c = 0x64;
648 break;
649 case R_GS:
650 c = 0x65;
651 break;
652 case R_SS:
653 if (bits == 64) {
654 error(ERR_WARNING | ERR_PASS2,
655 "ss segment base generated, but will be ignored in 64-bit mode");
656 }
657 c = 0x36;
658 break;
659 case R_SEGR6:
660 case R_SEGR7:
661 error(ERR_NONFATAL,
662 "segr6 and segr7 cannot be used as prefixes");
663 break;
664 case P_A16:
665 if (bits == 64) {
666 error(ERR_NONFATAL,
667 "16-bit addressing is not supported "
668 "in 64-bit mode");
669 } else if (bits != 16)
670 c = 0x67;
671 break;
672 case P_A32:
673 if (bits != 32)
674 c = 0x67;
675 break;
676 case P_A64:
677 if (bits != 64) {
678 error(ERR_NONFATAL,
679 "64-bit addressing is only supported "
680 "in 64-bit mode");
681 }
682 break;
683 case P_ASP:
684 c = 0x67;
685 break;
686 case P_O16:
687 if (bits != 16)
688 c = 0x66;
689 break;
690 case P_O32:
691 if (bits == 16)
692 c = 0x66;
693 break;
694 case P_O64:
695 /* REX.W */
696 break;
697 case P_OSP:
698 c = 0x66;
699 break;
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700700 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800701 case P_VEX3:
702 case P_VEX2:
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800703 case P_NOBND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400704 case P_none:
705 break;
706 default:
707 error(ERR_PANIC, "invalid instruction prefix");
708 }
709 if (c != 0) {
710 out(offset, segment, &c, OUT_RAWDATA, 1,
711 NO_SEG, NO_SEG);
712 offset++;
713 }
714 }
715 insn_end = offset + insn_size;
716 gencode(segment, offset, bits, instruction,
717 temp, insn_end);
718 offset += insn_size;
719 if (itimes > 0 && itimes == instruction->times - 1) {
720 /*
721 * Dummy call to list->output to give the offset to the
722 * listing module.
723 */
724 list->output(offset, NULL, OUT_RAWDATA, 0);
725 list->uplevel(LIST_TIMES);
726 }
727 }
728 if (instruction->times > 1)
729 list->downlevel(LIST_TIMES);
730 return offset - start;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700731 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400732 /* No match */
733 switch (m) {
734 case MERR_OPSIZEMISSING:
735 error(ERR_NONFATAL, "operation size not specified");
736 break;
737 case MERR_OPSIZEMISMATCH:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000738 error(ERR_NONFATAL, "mismatch in operand sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400739 break;
Jin Kyu Song25c22122013-10-30 03:12:45 -0700740 case MERR_BRNUMMISMATCH:
741 error(ERR_NONFATAL,
742 "mismatch in the number of broadcasting elements");
743 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400744 case MERR_BADCPU:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000745 error(ERR_NONFATAL, "no instruction for this cpu level");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400746 break;
747 case MERR_BADMODE:
H. Peter Anvin6cda4142008-12-29 20:52:28 -0800748 error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400749 bits);
750 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -0800751 case MERR_ENCMISMATCH:
752 error(ERR_NONFATAL, "specific encoding scheme not available");
753 break;
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800754 case MERR_BADBND:
755 error(ERR_NONFATAL, "bnd prefix is not allowed");
756 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800757 case MERR_BADREPNE:
758 error(ERR_NONFATAL, "%s prefix is not allowed",
759 (has_prefix(instruction, PPS_REP, P_REPNE) ?
760 "repne" : "repnz"));
761 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400762 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000763 error(ERR_NONFATAL,
764 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400765 break;
766 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000767 }
768 return 0;
769}
770
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400771int64_t insn_size(int32_t segment, int64_t offset, int bits, iflag_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400772 insn * instruction, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000773{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000774 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700775 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000776
H. Peter Anvine2c80182005-01-15 22:15:51 +0000777 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000778 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000779
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400780 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000781 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000782
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700783 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
784 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400785 instruction->opcode == I_DT || instruction->opcode == I_DO ||
786 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000787 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300788 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000789
H. Peter Anvine2c80182005-01-15 22:15:51 +0000790 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300791 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000792
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400793 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000794 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000795
H. Peter Anvine2c80182005-01-15 22:15:51 +0000796 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400797 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000798 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400799 warn_overflow_const(e->offset, wsize);
800 } else if (e->type == EOT_DB_STRING ||
801 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000802 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000803
H. Peter Anvine2c80182005-01-15 22:15:51 +0000804 align = (-osize) % wsize;
805 if (align < 0)
806 align += wsize;
807 isize += osize + align;
808 }
809 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000810 }
811
H. Peter Anvine2c80182005-01-15 22:15:51 +0000812 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400813 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000814 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300815 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700816 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000817
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400818 fp = fopen(fname, "rb");
819 if (!fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000820 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
821 fname);
822 else if (fseek(fp, 0L, SEEK_END) < 0)
823 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
824 fname);
825 else {
826 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000827 if (instruction->eops->next) {
828 len -= instruction->eops->next->offset;
829 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700830 len > (size_t)instruction->eops->next->next->offset) {
831 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000832 }
833 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300834 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000835 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300836 if (fp)
837 fclose(fp);
838 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000839 }
840
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700841 /* Check to see if we need an address-size prefix */
842 add_asp(instruction, bits);
843
H. Peter Anvin23595f52009-07-25 17:44:25 -0700844 m = find_match(&temp, instruction, segment, offset, bits);
845 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400846 /* we've matched an instruction. */
847 int64_t isize;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400848 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100849
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800850 isize = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400851 if (isize < 0)
852 return -1;
853 for (j = 0; j < MAXPREFIX; j++) {
854 switch (instruction->prefixes[j]) {
855 case P_A16:
856 if (bits != 16)
857 isize++;
858 break;
859 case P_A32:
860 if (bits != 32)
861 isize++;
862 break;
863 case P_O16:
864 if (bits != 16)
865 isize++;
866 break;
867 case P_O32:
868 if (bits == 16)
869 isize++;
870 break;
871 case P_A64:
872 case P_O64:
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700873 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800874 case P_VEX3:
875 case P_VEX2:
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800876 case P_NOBND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400877 case P_none:
878 break;
879 default:
880 isize++;
881 break;
882 }
883 }
884 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700885 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400886 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000887 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000888}
889
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800890static void bad_hle_warn(const insn * ins, uint8_t hleok)
891{
892 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800893 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800894 static const enum whatwarn warn[2][4] =
895 {
896 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
897 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
898 };
899 unsigned int n;
900
901 n = (unsigned int)rep_pfx - P_XACQUIRE;
902 if (n > 1)
903 return; /* Not XACQUIRE/XRELEASE */
904
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800905 ww = warn[n][hleok];
906 if (!is_class(MEMORY, ins->oprs[0].type))
907 ww = w_inval; /* HLE requires operand 0 to be memory */
908
909 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800910 case w_none:
911 break;
912
913 case w_lock:
914 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800915 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800916 "%s with this instruction requires lock",
917 prefix_name(rep_pfx));
918 }
919 break;
920
921 case w_inval:
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800922 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800923 "%s invalid with this instruction",
924 prefix_name(rep_pfx));
925 break;
926 }
927}
928
H. Peter Anvin507ae032008-10-09 15:37:10 -0700929/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400930#define case3(x) case (x): case (x)+1: case (x)+2
931#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700932
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800933static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800934 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000935{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800936 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800937 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000938 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000939 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700940 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700941 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700942 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700943 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800944 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800945 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700946 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000947
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700948 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700949 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700950 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700951
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700952 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400953 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700954
H. Peter Anvine2c80182005-01-15 22:15:51 +0000955 (void)segment; /* Don't warn that this parameter is unused */
956 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000957
H. Peter Anvin839eca22007-10-29 23:12:47 -0700958 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400959 c = *codes++;
960 op1 = (c & 3) + ((opex & 1) << 2);
961 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
962 opx = &ins->oprs[op1];
963 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700964
H. Peter Anvin839eca22007-10-29 23:12:47 -0700965 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400966 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 codes += c, length += c;
968 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700969
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400970 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400971 opex = c;
972 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700973
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400974 case4(010):
975 ins->rex |=
976 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000977 codes++, length++;
978 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700979
Jin Kyu Song164d6072013-10-15 19:10:13 -0700980 case4(014):
981 /* this is an index reg of MIB operand */
982 mib_index = opx->basereg;
983 break;
984
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400985 case4(020):
986 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000987 length++;
988 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700989
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400990 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 length += 2;
992 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700993
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400994 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700995 if (opx->type & (BITS16 | BITS32 | BITS64))
996 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 else
998 length += (bits == 16) ? 2 : 4;
999 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001000
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001001 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 length += 4;
1003 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001004
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001005 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001006 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001008
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001009 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 length++;
1011 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001012
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001013 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001014 length += 8; /* MOV reg64/imm */
1015 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001016
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001017 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018 length += 2;
1019 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001020
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001021 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001022 if (opx->type & (BITS16 | BITS32 | BITS64))
1023 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001024 else
1025 length += (bits == 16) ? 2 : 4;
1026 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001027
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001028 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 length += 4;
1030 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001031
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001032 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001033 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001034 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001035
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001036 case 0172:
1037 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001038 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001039 length++;
1040 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001041
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001042 case4(0174):
1043 length++;
1044 break;
1045
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001046 case4(0240):
1047 ins->rex |= REX_EV;
1048 ins->vexreg = regval(opx);
1049 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1050 ins->vex_cm = *codes++;
1051 ins->vex_wlp = *codes++;
1052 ins->evex_tuple = (*codes++ - 0300);
1053 break;
1054
1055 case 0250:
1056 ins->rex |= REX_EV;
1057 ins->vexreg = 0;
1058 ins->vex_cm = *codes++;
1059 ins->vex_wlp = *codes++;
1060 ins->evex_tuple = (*codes++ - 0300);
1061 break;
1062
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001063 case4(0254):
1064 length += 4;
1065 break;
1066
1067 case4(0260):
1068 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001069 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001070 ins->vex_cm = *codes++;
1071 ins->vex_wlp = *codes++;
1072 break;
1073
1074 case 0270:
1075 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001076 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001077 ins->vex_cm = *codes++;
1078 ins->vex_wlp = *codes++;
1079 break;
1080
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001081 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001082 hleok = c & 3;
1083 break;
1084
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001085 case4(0274):
1086 length++;
1087 break;
1088
1089 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001091
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001093 if (bits == 64)
1094 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001095 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001096 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001097
H. Peter Anvine2c80182005-01-15 22:15:51 +00001098 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001099 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001100 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001101
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001103 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001104
Keith Kaniosb7a89542007-04-12 02:40:54 +00001105 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001106 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1107 has_prefix(ins, PPS_ASIZE, P_A32))
1108 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001110
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001111 case4(0314):
1112 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001113
H. Peter Anvine2c80182005-01-15 22:15:51 +00001114 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001115 {
1116 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1117 if (pfx == P_O16)
1118 break;
1119 if (pfx != P_none)
1120 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1121 else
1122 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001124 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001125
H. Peter Anvine2c80182005-01-15 22:15:51 +00001126 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001127 {
1128 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1129 if (pfx == P_O32)
1130 break;
1131 if (pfx != P_none)
1132 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1133 else
1134 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001135 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001136 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001137
H. Peter Anvine2c80182005-01-15 22:15:51 +00001138 case 0322:
1139 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001140
Keith Kaniosb7a89542007-04-12 02:40:54 +00001141 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001142 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001143 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001144
Keith Kaniosb7a89542007-04-12 02:40:54 +00001145 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001146 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001147 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001148
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001149 case 0325:
1150 ins->rex |= REX_NH;
1151 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001152
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001153 case 0326:
1154 break;
1155
H. Peter Anvine2c80182005-01-15 22:15:51 +00001156 case 0330:
1157 codes++, length++;
1158 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001159
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001162
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001163 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164 case 0333:
1165 length++;
1166 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001167
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001168 case 0334:
1169 ins->rex |= REX_L;
1170 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001171
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001172 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001173 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001174
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001175 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001176 if (!ins->prefixes[PPS_REP])
1177 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001178 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001179
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001180 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001181 if (!ins->prefixes[PPS_REP])
1182 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001183 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001184
H. Peter Anvine2c80182005-01-15 22:15:51 +00001185 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 if (ins->oprs[0].segment != NO_SEG)
1187 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1188 " quantity of BSS space");
1189 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001190 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001191 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001192
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001193 case 0341:
1194 if (!ins->prefixes[PPS_WAIT])
1195 ins->prefixes[PPS_WAIT] = P_WAIT;
1196 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001197
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001198 case 0360:
1199 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001200
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001201 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001202 length++;
1203 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001204
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001205 case 0364:
1206 case 0365:
1207 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001208
Keith Kanios48af1772007-08-17 07:37:52 +00001209 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001210 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001211 length++;
1212 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001213
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001214 case 0370:
1215 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001216 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001217
H. Peter Anvine2c80182005-01-15 22:15:51 +00001218 case 0373:
1219 length++;
1220 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001221
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001222 case 0374:
1223 eat = EA_XMMVSIB;
1224 break;
1225
1226 case 0375:
1227 eat = EA_YMMVSIB;
1228 break;
1229
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001230 case 0376:
1231 eat = EA_ZMMVSIB;
1232 break;
1233
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001234 case4(0100):
1235 case4(0110):
1236 case4(0120):
1237 case4(0130):
1238 case4(0200):
1239 case4(0204):
1240 case4(0210):
1241 case4(0214):
1242 case4(0220):
1243 case4(0224):
1244 case4(0230):
1245 case4(0234):
1246 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001247 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001248 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001249 opflags_t rflags;
1250 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001251 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001252
Keith Kaniosb7a89542007-04-12 02:40:54 +00001253 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001254
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001255 if (c <= 0177) {
1256 /* pick rfield from operand b (opx) */
1257 rflags = regflag(opx);
1258 rfield = nasm_regvals[opx->basereg];
1259 } else {
1260 rflags = 0;
1261 rfield = c & 7;
1262 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001263
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001264 /* EVEX.b1 : evex_brerop contains the operand position */
1265 op_er_sae = (ins->evex_brerop >= 0 ?
1266 &ins->oprs[ins->evex_brerop] : NULL);
1267
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001268 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1269 /* set EVEX.b */
1270 ins->evex_p[2] |= EVEX_P2B;
1271 if (op_er_sae->decoflags & ER) {
1272 /* set EVEX.RC (rounding control) */
1273 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1274 & EVEX_P2RC;
1275 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001276 } else {
1277 /* set EVEX.L'L (vector length) */
1278 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001279 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001280 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001281 /* set EVEX.b */
1282 ins->evex_p[2] |= EVEX_P2B;
1283 }
1284 }
1285
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001286 if (itemp_has(temp, IF_MIB)) {
1287 opy->eaflags |= EAF_MIB;
1288 /*
1289 * if a separate form of MIB (ICC style) is used,
1290 * the index reg info is merged into mem operand
1291 */
1292 if (mib_index != R_none) {
1293 opy->indexreg = mib_index;
1294 opy->scale = 1;
1295 opy->hintbase = mib_index;
1296 opy->hinttype = EAH_NOTBASE;
1297 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001298 }
1299
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001300 if (process_ea(opy, &ea_data, bits,
1301 rfield, rflags, ins) != eat) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001302 errfunc(ERR_NONFATAL, "invalid effective address");
1303 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001304 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001305 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001306 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001307 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001308 }
1309 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001310
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001311 default:
1312 errfunc(ERR_PANIC, "internal instruction table corrupt"
1313 ": instruction code \\%o (0x%02X) given", c, c);
1314 break;
1315 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001316 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001317
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001318 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001319
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001320 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001321 if (ins->rex & REX_H) {
1322 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1323 return -1;
1324 }
1325 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001326 }
1327
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001328 switch (ins->prefixes[PPS_VEX]) {
1329 case P_EVEX:
1330 if (!(ins->rex & REX_EV))
1331 return -1;
1332 break;
1333 case P_VEX3:
1334 case P_VEX2:
1335 if (!(ins->rex & REX_V))
1336 return -1;
1337 break;
1338 default:
1339 break;
1340 }
1341
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001342 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001343 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001344
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001345 if (ins->rex & REX_H) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001346 errfunc(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001347 return -1;
1348 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001349 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001350 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001351 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001352 ins->rex &= ~REX_W;
1353 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001354 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001355 ins->rex |= REX_W;
1356 bad32 &= ~REX_W;
1357 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001358 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001359 /* Follow REX_W */
1360 break;
1361 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001362
H. Peter Anvinfc561202011-07-07 16:58:22 -07001363 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001364 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1365 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001366 } else if (!(ins->rex & REX_EV) &&
1367 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
1368 errfunc(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
1369 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001370 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001371 if (ins->rex & REX_EV)
1372 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001373 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1374 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001375 length += 3;
1376 else
1377 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001378 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001379 if (ins->rex & REX_H) {
1380 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1381 return -1;
1382 } else if (bits == 64) {
1383 length++;
1384 } else if ((ins->rex & REX_L) &&
1385 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001386 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001387 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001388 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001389 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001390 length++;
1391 } else {
1392 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1393 return -1;
1394 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001395 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001396
1397 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001398 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -08001399 errfunc(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001400 "instruction is not lockable");
1401 }
1402
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001403 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001404
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001405 /*
1406 * when BND prefix is set by DEFAULT directive,
1407 * BND prefix is added to every appropriate instruction line
1408 * unless it is overridden by NOBND prefix.
1409 */
1410 if (globalbnd &&
1411 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1412 ins->prefixes[PPS_REP] = P_BND;
1413
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001414 return length;
1415}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001416
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001417static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1418{
1419 if (bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001420 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001421 !(ins->rex & (REX_V | REX_EV)) &&
1422 !ins->rex_done) {
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001423 int rex = (ins->rex & REX_MASK) | REX_P;
Cyrill Gorcunovaa29b1d2014-05-05 00:30:58 +04001424 out(offset, segment, &rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001425 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001426 return 1;
1427 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001428 }
1429
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001430 return 0;
1431}
1432
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001433static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001434 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001435 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001436{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001437 uint8_t c;
1438 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001439 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001440 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001441 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001442 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001443 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001444 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001445 enum ea_type eat = EA_SCALAR;
H. Peter Anvin70653092007-10-19 14:42:29 -07001446
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001447 ins->rex_done = false;
1448
H. Peter Anvin839eca22007-10-29 23:12:47 -07001449 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001450 c = *codes++;
1451 op1 = (c & 3) + ((opex & 1) << 2);
1452 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1453 opx = &ins->oprs[op1];
1454 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001455
H. Peter Anvin839eca22007-10-29 23:12:47 -07001456 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001457 case 01:
1458 case 02:
1459 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001460 case 04:
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001461 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001462 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001463 codes += c;
1464 offset += c;
1465 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001466
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001467 case 05:
1468 case 06:
1469 case 07:
1470 opex = c;
1471 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001472
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001473 case4(010):
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001474 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001475 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001476 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001477 offset += 1;
1478 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001479
Jin Kyu Song164d6072013-10-15 19:10:13 -07001480 case4(014):
1481 break;
1482
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001483 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001484 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001485 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001486 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001487 }
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001488 out_imm8(offset, segment, opx, -1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001489 offset += 1;
1490 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001491
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001492 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001493 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001494 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001495 "unsigned byte value exceeds bounds");
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001496 out_imm8(offset, segment, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001497 offset += 1;
1498 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001499
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001500 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001501 warn_overflow_opd(opx, 2);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001502 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001503 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001504 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001505 offset += 2;
1506 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001507
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001508 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001509 if (opx->type & (BITS16 | BITS32))
1510 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001511 else
1512 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001513 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001514 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001515 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001516 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001517 offset += size;
1518 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001519
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001520 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001521 warn_overflow_opd(opx, 4);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001522 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001523 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001524 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001525 offset += 4;
1526 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001527
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001528 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001529 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001530 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001531 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001532 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001533 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001534 offset += size;
1535 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001536
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001537 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001538 if (opx->segment != segment) {
1539 data = opx->offset;
1540 out(offset, segment, &data,
1541 OUT_REL1ADR, insn_end - offset,
1542 opx->segment, opx->wrt);
1543 } else {
1544 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001545 if (data > 127 || data < -128)
1546 errfunc(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001547 out(offset, segment, &data,
1548 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1549 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001550 offset += 1;
1551 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001552
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001553 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001554 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001555 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001556 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001557 offset += 8;
1558 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001559
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001560 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001561 if (opx->segment != segment) {
1562 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001563 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001564 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001565 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001566 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001567 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001568 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001569 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001570 }
1571 offset += 2;
1572 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001573
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001574 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001575 if (opx->type & (BITS16 | BITS32 | BITS64))
1576 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001577 else
1578 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001579 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001580 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001581 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001582 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1583 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001584 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001585 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001586 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001587 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001588 }
1589 offset += size;
1590 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001591
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001592 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001593 if (opx->segment != segment) {
1594 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001595 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001596 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001597 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001598 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001599 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001600 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001601 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001602 }
1603 offset += 4;
1604 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001605
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001606 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001607 if (opx->segment == NO_SEG)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001608 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1609 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001610 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001611 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001612 outfmt->segbase(1 + opx->segment),
1613 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001614 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001616
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001617 case 0172:
1618 c = *codes++;
1619 opx = &ins->oprs[c >> 3];
1620 bytes[0] = nasm_regvals[opx->basereg] << 4;
1621 opx = &ins->oprs[c & 7];
1622 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1623 errfunc(ERR_NONFATAL,
1624 "non-absolute expression not permitted as argument %d",
1625 c & 7);
1626 } else {
1627 if (opx->offset & ~15) {
1628 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1629 "four-bit argument exceeds bounds");
1630 }
1631 bytes[0] |= opx->offset & 15;
1632 }
1633 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1634 offset++;
1635 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001636
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001637 case 0173:
1638 c = *codes++;
1639 opx = &ins->oprs[c >> 4];
1640 bytes[0] = nasm_regvals[opx->basereg] << 4;
1641 bytes[0] |= c & 15;
1642 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1643 offset++;
1644 break;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001645
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001646 case4(0174):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001647 bytes[0] = nasm_regvals[opx->basereg] << 4;
1648 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1649 offset++;
1650 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001651
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001652 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001653 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001654 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1655 (int32_t)data != (int64_t)data) {
1656 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1657 "signed dword immediate exceeds bounds");
1658 }
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001659 out(offset, segment, &data, OUT_ADDRESS, -4,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001660 opx->segment, opx->wrt);
1661 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001662 break;
1663
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001664 case4(0240):
1665 case 0250:
1666 codes += 3;
1667 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1668 EVEX_P2Z | EVEX_P2AAA, 2);
1669 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1670 bytes[0] = 0x62;
1671 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001672 bytes[1] = ((((ins->rex & 7) << 5) |
1673 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
1674 (ins->vex_cm & 3);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001675 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1676 ((~ins->vexreg & 15) << 3) |
1677 (1 << 2) | (ins->vex_wlp & 3);
1678 bytes[3] = ins->evex_p[2];
1679 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1680 offset += 4;
1681 break;
1682
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001683 case4(0260):
1684 case 0270:
1685 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001686 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1687 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001688 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1689 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1690 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001691 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001692 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1693 offset += 3;
1694 } else {
1695 bytes[0] = 0xc5;
1696 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001697 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001698 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1699 offset += 2;
1700 }
1701 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001702
H. Peter Anvine014f352012-02-25 22:35:19 -08001703 case 0271:
1704 case 0272:
1705 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001706 break;
1707
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001708 case4(0274):
1709 {
1710 uint64_t uv, um;
1711 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001712
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001713 if (ins->rex & REX_W)
1714 s = 64;
1715 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1716 s = 16;
1717 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1718 s = 32;
1719 else
1720 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001721
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001722 um = (uint64_t)2 << (s-1);
1723 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001724
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001725 if (uv > 127 && uv < (uint64_t)-128 &&
1726 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001727 /* If this wasn't explicitly byte-sized, warn as though we
1728 * had fallen through to the imm16/32/64 case.
1729 */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001730 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001731 "%s value exceeds bounds",
1732 (opx->type & BITS8) ? "signed byte" :
1733 s == 16 ? "word" :
1734 s == 32 ? "dword" :
1735 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001736 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001737 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001738 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001739 out(offset, segment, &data, OUT_ADDRESS, 1,
1740 opx->segment, opx->wrt);
1741 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001742 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001743 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1744 NO_SEG);
1745 }
1746 offset += 1;
1747 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001748 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001749
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001750 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001752
H. Peter Anvine2c80182005-01-15 22:15:51 +00001753 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001754 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001756 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001757 offset += 1;
1758 } else
1759 offset += 0;
1760 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001761
H. Peter Anvine2c80182005-01-15 22:15:51 +00001762 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001763 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001764 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001765 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 offset += 1;
1767 } else
1768 offset += 0;
1769 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001770
H. Peter Anvine2c80182005-01-15 22:15:51 +00001771 case 0312:
1772 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001773
Keith Kaniosb7a89542007-04-12 02:40:54 +00001774 case 0313:
1775 ins->rex = 0;
1776 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001777
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001778 case4(0314):
1779 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001780
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001784
H. Peter Anvine2c80182005-01-15 22:15:51 +00001785 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001786 case 0323:
1787 break;
1788
Keith Kaniosb7a89542007-04-12 02:40:54 +00001789 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001790 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001791 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001792
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001793 case 0325:
1794 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001795
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001796 case 0326:
1797 break;
1798
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 case 0330:
Cyrill Gorcunov83e69242013-03-03 14:34:31 +04001800 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001801 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 offset += 1;
1803 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001804
H. Peter Anvine2c80182005-01-15 22:15:51 +00001805 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001807
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001808 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001810 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001811 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 offset += 1;
1813 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001814
Keith Kanios48af1772007-08-17 07:37:52 +00001815 case 0334:
1816 if (ins->rex & REX_R) {
1817 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001818 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001819 offset += 1;
1820 }
1821 ins->rex &= ~(REX_L|REX_R);
1822 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001823
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001824 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001825 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001826
H. Peter Anvin962e3052008-08-28 17:47:16 -07001827 case 0336:
1828 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001829 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001830
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001832 if (ins->oprs[0].segment != NO_SEG)
1833 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1834 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001835 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 if (size > 0)
1837 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001838 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001839 offset += size;
1840 }
1841 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001842
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001843 case 0341:
1844 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001845
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001846 case 0360:
1847 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001848
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001849 case 0361:
1850 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001851 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1852 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001853 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001854
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001855 case 0364:
1856 case 0365:
1857 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001858
Keith Kanios48af1772007-08-17 07:37:52 +00001859 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001860 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001861 *bytes = c - 0366 + 0x66;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001862 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001863 offset += 1;
1864 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001865
Jin Kyu Song03041092013-10-15 19:38:51 -07001866 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001867 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001868
H. Peter Anvine2c80182005-01-15 22:15:51 +00001869 case 0373:
1870 *bytes = bits == 16 ? 3 : 5;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001871 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001872 offset += 1;
1873 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001874
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001875 case 0374:
1876 eat = EA_XMMVSIB;
1877 break;
1878
1879 case 0375:
1880 eat = EA_YMMVSIB;
1881 break;
1882
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001883 case 0376:
1884 eat = EA_ZMMVSIB;
1885 break;
1886
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001887 case4(0100):
1888 case4(0110):
1889 case4(0120):
1890 case4(0130):
1891 case4(0200):
1892 case4(0204):
1893 case4(0210):
1894 case4(0214):
1895 case4(0220):
1896 case4(0224):
1897 case4(0230):
1898 case4(0234):
1899 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001900 ea ea_data;
1901 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001902 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001903 uint8_t *p;
1904 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001905 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001906
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001907 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001908 /* pick rfield from operand b (opx) */
1909 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001910 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001911 } else {
1912 /* rfield is constant */
1913 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001914 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001915 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001916
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001917 if (process_ea(opy, &ea_data, bits,
1918 rfield, rflags, ins) != eat)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001919 errfunc(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001920
H. Peter Anvine2c80182005-01-15 22:15:51 +00001921 p = bytes;
1922 *p++ = ea_data.modrm;
1923 if (ea_data.sib_present)
1924 *p++ = ea_data.sib;
1925
1926 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001927 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001928
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001929 /*
1930 * Make sure the address gets the right offset in case
1931 * the line breaks in the .lst file (BR 1197827)
1932 */
1933 offset += s;
1934 s = 0;
1935
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001936 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001937 /* use compressed displacement, if available */
1938 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001939 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001940 if (ea_data.rip) {
1941 if (opy->segment == segment) {
1942 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001943 if (overflow_signed(data, ea_data.bytes))
1944 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001945 out(offset, segment, &data, OUT_ADDRESS,
1946 ea_data.bytes, NO_SEG, NO_SEG);
1947 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001948 /* overflow check in output/linker? */
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001949 out(offset, segment, &data, OUT_REL4ADR,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001950 insn_end - offset, opy->segment, opy->wrt);
1951 }
1952 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001953 int asize = ins->addr_size >> 3;
1954 int atype = ea_data.bytes;
1955
1956 if (overflow_general(data, asize) ||
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001957 signed_bits(data, ins->addr_size) !=
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001958 signed_bits(data, ea_data.bytes << 3))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001959 warn_overflow(ERR_PASS2, ea_data.bytes);
1960
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001961 if (asize > ea_data.bytes) {
1962 /*
1963 * If the address isn't the full width of
1964 * the address size, treat is as signed...
1965 */
1966 atype = -atype;
1967 }
1968
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001969 out(offset, segment, &data, OUT_ADDRESS,
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001970 atype, opy->segment, opy->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001971 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001972 }
1973 offset += s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001974 }
1975 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001976
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001977 default:
1978 errfunc(ERR_PANIC, "internal instruction table corrupt"
1979 ": instruction code \\%o (0x%02X) given", c, c);
1980 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001981 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001982 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001983}
1984
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001985static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001986{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001987 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001988 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001989 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001990}
1991
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001992static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001993{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001994 if (!is_register(o->basereg))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001995 errfunc(ERR_PANIC, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001996 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001997}
1998
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001999static int op_rexflags(const operand * o, int mask)
2000{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002001 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002002 int val;
2003
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002004 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002005 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002006
H. Peter Anvina4835d42008-05-20 14:21:29 -07002007 flags = nasm_reg_flags[o->basereg];
2008 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002009
2010 return rexflags(val, flags, mask);
2011}
2012
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002013static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002014{
2015 int rex = 0;
2016
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002017 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002018 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002019 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002020 rex |= REX_W;
2021 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2022 rex |= REX_H;
2023 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2024 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002025
2026 return rex & mask;
2027}
2028
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002029static int evexflags(int val, decoflags_t deco,
2030 int mask, uint8_t byte)
2031{
2032 int evex = 0;
2033
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002034 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002035 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002036 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002037 evex |= (EVEX_P0RP | EVEX_P0X);
2038 break;
2039 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002040 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002041 evex |= EVEX_P2VP;
2042 if (deco & Z)
2043 evex |= EVEX_P2Z;
2044 if (deco & OPMASK_MASK)
2045 evex |= deco & EVEX_P2AAA;
2046 break;
2047 }
2048 return evex & mask;
2049}
2050
2051static int op_evexflags(const operand * o, int mask, uint8_t byte)
2052{
2053 int val;
2054
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002055 val = nasm_regvals[o->basereg];
2056
2057 return evexflags(val, o->decoflags, mask, byte);
2058}
2059
H. Peter Anvin23595f52009-07-25 17:44:25 -07002060static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002061 insn *instruction,
2062 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002063{
2064 const struct itemplate *temp;
2065 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002066 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002067 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002068 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002069 int i;
2070
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002071 /* broadcasting uses a different data element size */
2072 for (i = 0; i < instruction->operands; i++)
2073 if (i == broadcast)
2074 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2075 else
2076 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002077
2078 merr = MERR_INVALOP;
2079
2080 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002081 temp->opcode != I_none; temp++) {
2082 m = matches(temp, instruction, bits);
2083 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002084 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002085 m = MOK_GOOD;
2086 else
2087 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002088 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002089 /*
2090 * Missing operand size and a candidate for fuzzy matching...
2091 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002092 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002093 if (i == broadcast)
2094 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2095 else
2096 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002097 opsizemissing = true;
2098 }
2099 if (m > merr)
2100 merr = m;
2101 if (merr == MOK_GOOD)
2102 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002103 }
2104
2105 /* No match, but see if we can get a fuzzy operand size match... */
2106 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002107 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002108
2109 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002110 /*
2111 * We ignore extrinsic operand sizes on registers, so we should
2112 * never try to fuzzy-match on them. This also resolves the case
2113 * when we have e.g. "xmmrm128" in two different positions.
2114 */
2115 if (is_class(REGISTER, instruction->oprs[i].type))
2116 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002117
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002118 /* This tests if xsizeflags[i] has more than one bit set */
2119 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2120 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002121
Jin Kyu Song7903c072013-10-30 03:00:12 -07002122 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002123 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002124 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2125 BITS32 : BITS64);
2126 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002127 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002128 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002129 }
2130
2131 /* Try matching again... */
2132 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002133 temp->opcode != I_none; temp++) {
2134 m = matches(temp, instruction, bits);
2135 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002136 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002137 m = MOK_GOOD;
2138 else
2139 m = MERR_INVALOP;
2140 }
2141 if (m > merr)
2142 merr = m;
2143 if (merr == MOK_GOOD)
2144 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002145 }
2146
H. Peter Anvina81655b2009-07-25 18:15:28 -07002147done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002148 *tempp = temp;
2149 return merr;
2150}
2151
Mark Charneydcaef4b2014-10-09 13:45:17 -04002152static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2153{
2154 opflags_t opsize = opflags & SIZE_MASK;
2155 uint8_t brcast_num;
2156
2157 /*
2158 * Due to discontinuity between BITS64 and BITS128 (BITS80),
2159 * this cannot be a simple arithmetic calculation.
2160 */
2161 if (brsize > BITS64)
2162 errfunc(ERR_FATAL,
2163 "size of broadcasting element is greater than 64 bits");
2164
2165 switch (opsize) {
2166 case BITS64:
2167 brcast_num = BITS64 / brsize;
2168 break;
2169 default:
2170 brcast_num = (opsize / BITS128) * (BITS64 / brsize) * 2;
2171 break;
2172 }
2173
2174 return brcast_num;
2175}
2176
H. Peter Anvin65289e82009-07-25 17:25:11 -07002177static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002178 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002179{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002180 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002181 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002182 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002183
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002184 /*
2185 * Check the opcode
2186 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002187 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002188 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002189
2190 /*
2191 * Count the operands
2192 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002193 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002194 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002195
2196 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002197 * Is it legal?
2198 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002199 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002200 return MERR_INVALOP;
2201
2202 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002203 * {evex} available?
2204 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002205 switch (instruction->prefixes[PPS_VEX]) {
2206 case P_EVEX:
2207 if (!itemp_has(itemp, IF_EVEX))
2208 return MERR_ENCMISMATCH;
2209 break;
2210 case P_VEX3:
2211 case P_VEX2:
2212 if (!itemp_has(itemp, IF_VEX))
2213 return MERR_ENCMISMATCH;
2214 break;
2215 default:
2216 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002217 }
2218
2219 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002220 * Check that no spurious colons or TOs are present
2221 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002222 for (i = 0; i < itemp->operands; i++)
2223 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002224 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002225
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002226 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002227 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002228 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002229 switch (itemp_smask(itemp)) {
2230 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002231 asize = BITS8;
2232 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002233 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002234 asize = BITS16;
2235 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002236 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002237 asize = BITS32;
2238 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002239 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002240 asize = BITS64;
2241 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002242 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002243 asize = BITS128;
2244 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002245 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002246 asize = BITS256;
2247 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002248 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002249 asize = BITS512;
2250 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002251 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002252 switch (bits) {
2253 case 16:
2254 asize = BITS16;
2255 break;
2256 case 32:
2257 asize = BITS32;
2258 break;
2259 case 64:
2260 asize = BITS64;
2261 break;
2262 default:
2263 asize = 0;
2264 break;
2265 }
2266 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002267 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002268 asize = 0;
2269 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002270 }
2271
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002272 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002273 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002274 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002275 memset(size, 0, sizeof size);
2276 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002277 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002278 /* S- flags apply to all operands */
2279 for (i = 0; i < MAX_OPERANDS; i++)
2280 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002281 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002282
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002283 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002284 * Check that the operand flags all match up,
2285 * it's a bit tricky so lets be verbose:
2286 *
2287 * 1) Find out the size of operand. If instruction
2288 * doesn't have one specified -- we're trying to
2289 * guess it either from template (IF_S* flag) or
2290 * from code bits.
2291 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002292 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002293 * template has an operand size specified AND this size differ
2294 * from which instruction has (perhaps we got it from code bits)
2295 * we are:
2296 * a) Check that only size of instruction and operand is differ
2297 * other characteristics do match
2298 * b) Perhaps it's a register specified in instruction so
2299 * for such a case we just mark that operand as "size
2300 * missing" and this will turn on fuzzy operand size
2301 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002302 */
2303 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002304 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002305 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002306 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002307 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002308 opflags_t template_opsize, insn_opsize;
2309
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002310 if (!(type & SIZE_MASK))
2311 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002312
Jin Kyu Song7903c072013-10-30 03:00:12 -07002313 insn_opsize = type & SIZE_MASK;
2314 if (!is_broadcast) {
2315 template_opsize = itemp->opd[i] & SIZE_MASK;
2316 } else {
2317 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2318 /*
2319 * when broadcasting, the element size depends on
2320 * the instruction type. decorator flag should match.
2321 */
2322
2323 if (deco_brsize) {
2324 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002325 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002326 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002327 } else {
2328 template_opsize = 0;
2329 }
2330 }
2331
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002332 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002333 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002334 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002335 } else if (template_opsize) {
2336 if (template_opsize != insn_opsize) {
2337 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002338 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002339 } else if (!is_class(REGISTER, type)) {
2340 /*
2341 * Note: we don't honor extrinsic operand sizes for registers,
2342 * so "missing operand size" for a register should be
2343 * considered a wildcard match rather than an error.
2344 */
2345 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002346 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002347 } else if (is_broadcast &&
2348 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002349 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002350 /*
2351 * broadcasting opsize matches but the number of repeated memory
2352 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002353 * if 64b double precision float is broadcasted to ymm (256b),
2354 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002355 */
2356 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002357 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002358 }
2359 }
2360
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002361 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002362 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002363
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002364 /*
2365 * Check operand sizes
2366 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002367 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2368 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002369 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002370 asize = itemp->opd[i] & SIZE_MASK;
2371 if (asize) {
2372 for (i = 0; i < oprs; i++)
2373 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374 break;
2375 }
2376 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002377 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002378 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002379 }
2380
Keith Kaniosb7a89542007-04-12 02:40:54 +00002381 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 if (!(itemp->opd[i] & SIZE_MASK) &&
2383 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002384 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002385 }
2386
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002387 /*
2388 * Check template is okay at the set cpu level
2389 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002390 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002391 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002392
Keith Kaniosb7a89542007-04-12 02:40:54 +00002393 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002394 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002395 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002396 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002397 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002398
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002399 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002400 * If we have a HLE prefix, look for the NOHLE flag
2401 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002402 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002403 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2404 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2405 return MERR_BADHLE;
2406
2407 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002408 * Check if special handling needed for Jumps
2409 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002410 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002411 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002412
Jin Kyu Song03041092013-10-15 19:38:51 -07002413 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002414 * Check if BND prefix is allowed.
2415 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002416 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002417 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002418 (has_prefix(instruction, PPS_REP, P_BND) ||
2419 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002420 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002421 else if (itemp_has(itemp, IF_BND) &&
2422 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2423 has_prefix(instruction, PPS_REP, P_REPNZ)))
2424 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002425
H. Peter Anvin60926242009-07-26 16:25:38 -07002426 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002427}
2428
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002429/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002430 * Check if ModR/M.mod should/can be 01.
2431 * - EAF_BYTEOFFS is set
2432 * - offset can fit in a byte when EVEX is not used
2433 * - offset can be compressed when EVEX is used
2434 */
2435#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2436 (o >= -128 && o <= 127 && \
2437 seg == NO_SEG && !forw_ref && \
2438 !(input->eaflags & EAF_WORDOFFS) && \
2439 !(ins->rex & REX_EV)) || \
2440 (ins->rex & REX_EV && \
2441 is_disp8n(input, ins, &output->disp8)))
2442
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002443static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002444 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002445{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002446 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002447 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002448 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002449
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002450 output->type = EA_SCALAR;
2451 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002452 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002453
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002454 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002455 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002456 /* EVEX.R' flag for the REG operand */
2457 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002458
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002459 if (is_class(REGISTER, input->type)) {
2460 /*
2461 * It's a direct register.
2462 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002463 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002464 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002465
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002466 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002467 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002468
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002469 /* broadcasting is not available with a direct register operand. */
2470 if (input->decoflags & BRDCAST_MASK) {
2471 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2472 goto err;
2473 }
2474
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002475 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002476 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002477 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002478 output->bytes = 0; /* no offset necessary either */
2479 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2480 } else {
2481 /*
2482 * It's a memory reference.
2483 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002484
2485 /* Embedded rounding or SAE is not available with a mem ref operand. */
2486 if (input->decoflags & (ER | SAE)) {
2487 nasm_error(ERR_NONFATAL,
2488 "Embedded rounding is available only with reg-reg op.");
2489 return -1;
2490 }
2491
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002492 if (input->basereg == -1 &&
2493 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002494 /*
2495 * It's a pure offset.
2496 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002497 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2498 input->segment == NO_SEG) {
2499 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2500 input->type &= ~IP_REL;
2501 input->type |= MEMORY;
2502 }
2503
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002504 if (bits == 64 &&
2505 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
2506 nasm_error(ERR_NONFATAL, "RIP-relative addressing is prohibited for mib.");
2507 return -1;
2508 }
2509
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002510 if (eaflags & EAF_BYTEOFFS ||
2511 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002512 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2513 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2514 }
2515
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002516 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002517 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002518 output->sib = GEN_SIB(0, 4, 5);
2519 output->bytes = 4;
2520 output->modrm = GEN_MODRM(0, rfield, 4);
2521 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002522 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002523 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002524 output->bytes = (addrbits != 16 ? 4 : 2);
2525 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2526 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002527 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002528 } else {
2529 /*
2530 * It's an indirection.
2531 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002532 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002533 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002535 int t, it, bt; /* register numbers */
2536 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002537
H. Peter Anvine2c80182005-01-15 22:15:51 +00002538 if (s == 0)
2539 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002540
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002541 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002542 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002543 ix = nasm_reg_flags[i];
2544 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002545 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002546 ix = 0;
2547 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002548
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002549 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002550 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002551 bx = nasm_reg_flags[b];
2552 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002553 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002554 bx = 0;
2555 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002556
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002557 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002558 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002559 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002560 int32_t o = input->offset;
2561 int mod, scale, index, base;
2562
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002563 /*
2564 * For a vector SIB, one has to be a vector and the other,
2565 * if present, a GPR. The vector must be the index operand.
2566 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002567 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002568 if (s == 0)
2569 s = 1;
2570 else if (s != 1)
2571 goto err;
2572
2573 t = bt, bt = it, it = t;
2574 x = bx, bx = ix, ix = x;
2575 }
2576
2577 if (bt != -1) {
2578 if (REG_GPR & ~bx)
2579 goto err;
2580 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2581 sok &= bx;
2582 else
2583 goto err;
2584 }
2585
2586 /*
2587 * While we're here, ensure the user didn't specify
2588 * WORD or QWORD
2589 */
2590 if (input->disp_size == 16 || input->disp_size == 64)
2591 goto err;
2592
2593 if (addrbits == 16 ||
2594 (addrbits == 32 && !(sok & BITS32)) ||
2595 (addrbits == 64 && !(sok & BITS64)))
2596 goto err;
2597
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002598 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2599 : ((ix & YMMREG & ~REG_EA)
2600 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002601
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002602 output->rex |= rexflags(it, ix, REX_X);
2603 output->rex |= rexflags(bt, bx, REX_B);
2604 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002605
2606 index = it & 7; /* it is known to be != -1 */
2607
2608 switch (s) {
2609 case 1:
2610 scale = 0;
2611 break;
2612 case 2:
2613 scale = 1;
2614 break;
2615 case 4:
2616 scale = 2;
2617 break;
2618 case 8:
2619 scale = 3;
2620 break;
2621 default: /* then what the smeg is it? */
2622 goto err; /* panic */
2623 }
2624
2625 if (bt == -1) {
2626 base = 5;
2627 mod = 0;
2628 } else {
2629 base = (bt & 7);
2630 if (base != REG_NUM_EBP && o == 0 &&
2631 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002632 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002633 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002634 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002635 mod = 1;
2636 else
2637 mod = 2;
2638 }
2639
2640 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002641 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2642 output->modrm = GEN_MODRM(mod, rfield, 4);
2643 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002644 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002645 /*
2646 * it must be a 32/64-bit memory reference. Firstly we have
2647 * to check that all registers involved are type E/Rxx.
2648 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002649 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002650 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002651
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002652 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002653 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2654 sok &= ix;
2655 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002656 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002657 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002658
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002659 if (bt != -1) {
2660 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002661 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002662 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002663 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002664 sok &= bx;
2665 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002666
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002667 /*
2668 * While we're here, ensure the user didn't specify
2669 * WORD or QWORD
2670 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002671 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002672 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002673
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002674 if (addrbits == 16 ||
2675 (addrbits == 32 && !(sok & BITS32)) ||
2676 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002677 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002678
Keith Kaniosb7a89542007-04-12 02:40:54 +00002679 /* now reorganize base/index */
2680 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002681 ((hb == b && ht == EAH_NOTBASE) ||
2682 (hb == i && ht == EAH_MAKEBASE))) {
2683 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002684 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002685 x = bx, bx = ix, ix = x;
2686 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002687
Jin Kyu Song164d6072013-10-15 19:10:13 -07002688 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002689 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002690 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002691 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002692 if (eaflags & EAF_MIB) {
2693 /* only for mib operands */
2694 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2695 /*
2696 * make a single reg index [reg*1].
2697 * gas uses this form for an explicit index register.
2698 */
2699 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2700 }
2701 if ((ht == EAH_SUMMED) && bt == -1) {
2702 /* separate once summed index into [base, index] */
2703 bt = it, bx = ix, s--;
2704 }
2705 } else {
2706 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002707 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002708 s == 3 || s == 5 || s == 9) && bt == -1) {
2709 /* convert 3*EAX to EAX+2*EAX */
2710 bt = it, bx = ix, s--;
2711 }
2712 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002713 (eaflags & EAF_TIMESTWO) &&
2714 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002715 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002716 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002717 * to sib format with 0x0 displacement - [EAX*1+0].
2718 */
2719 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2720 }
2721 }
Keith Kanios48af1772007-08-17 07:37:52 +00002722 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002723 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002724 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002725 x = ix, ix = bx, bx = x;
2726 }
2727 if (it == REG_NUM_ESP ||
2728 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002729 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002730
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002731 output->rex |= rexflags(it, ix, REX_X);
2732 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002733
Keith Kanios48af1772007-08-17 07:37:52 +00002734 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002735 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002736 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002737
Keith Kaniosb7a89542007-04-12 02:40:54 +00002738 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002739 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002740 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002741 } else {
2742 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002743 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002744 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002745 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002746 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002747 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002748 mod = 1;
2749 else
2750 mod = 2;
2751 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002752
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002753 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002754 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2755 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002756 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002757 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002758 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002759
Keith Kaniosb7a89542007-04-12 02:40:54 +00002760 if (it == -1)
2761 index = 4, s = 1;
2762 else
2763 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002764
H. Peter Anvine2c80182005-01-15 22:15:51 +00002765 switch (s) {
2766 case 1:
2767 scale = 0;
2768 break;
2769 case 2:
2770 scale = 1;
2771 break;
2772 case 4:
2773 scale = 2;
2774 break;
2775 case 8:
2776 scale = 3;
2777 break;
2778 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002779 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002780 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002781
Keith Kaniosb7a89542007-04-12 02:40:54 +00002782 if (bt == -1) {
2783 base = 5;
2784 mod = 0;
2785 } else {
2786 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002787 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002788 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002789 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002790 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002791 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002792 mod = 1;
2793 else
2794 mod = 2;
2795 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002796
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002797 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002798 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2799 output->modrm = GEN_MODRM(mod, rfield, 4);
2800 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002801 }
2802 } else { /* it's 16-bit */
2803 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002804 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002805
Keith Kaniosb7a89542007-04-12 02:40:54 +00002806 /* check for 64-bit long mode */
2807 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002808 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002809
H. Peter Anvine2c80182005-01-15 22:15:51 +00002810 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002811 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2812 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002813 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002814
Keith Kaniosb7a89542007-04-12 02:40:54 +00002815 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002816 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002817 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002818
H. Peter Anvine2c80182005-01-15 22:15:51 +00002819 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002820 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002821 if (b == -1 && i != -1) {
2822 int tmp = b;
2823 b = i;
2824 i = tmp;
2825 } /* swap */
2826 if ((b == R_SI || b == R_DI) && i != -1) {
2827 int tmp = b;
2828 b = i;
2829 i = tmp;
2830 }
2831 /* have BX/BP as base, SI/DI index */
2832 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002833 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002834 if (i != -1 && b != -1 &&
2835 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002836 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002837 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002838 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002839
H. Peter Anvine2c80182005-01-15 22:15:51 +00002840 rm = -1;
2841 if (i != -1)
2842 switch (i * 256 + b) {
2843 case R_SI * 256 + R_BX:
2844 rm = 0;
2845 break;
2846 case R_DI * 256 + R_BX:
2847 rm = 1;
2848 break;
2849 case R_SI * 256 + R_BP:
2850 rm = 2;
2851 break;
2852 case R_DI * 256 + R_BP:
2853 rm = 3;
2854 break;
2855 } else
2856 switch (b) {
2857 case R_SI:
2858 rm = 4;
2859 break;
2860 case R_DI:
2861 rm = 5;
2862 break;
2863 case R_BP:
2864 rm = 6;
2865 break;
2866 case R_BX:
2867 rm = 7;
2868 break;
2869 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002870 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002871 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002872
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002873 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002874 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002875 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002876 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002877 mod = 1;
2878 else
2879 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002880
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002881 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002882 output->bytes = mod; /* bytes of offset needed */
2883 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002884 }
2885 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002886 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002887
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002888 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002889 return output->type;
2890
2891err:
2892 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002893}
2894
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002895static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002896{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002897 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002898 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002899
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002900 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002901
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002902 switch (ins->prefixes[PPS_ASIZE]) {
2903 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002904 valid &= 16;
2905 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002906 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002907 valid &= 32;
2908 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002909 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002910 valid &= 64;
2911 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002912 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002913 valid &= (addrbits == 32) ? 16 : 32;
2914 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002915 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002916 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002917 }
2918
2919 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002920 if (is_class(MEMORY, ins->oprs[j].type)) {
2921 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002922
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002923 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002924 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002925 i = 0;
2926 else
2927 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002928
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002929 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002930 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002931 b = 0;
2932 else
2933 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002934
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002935 if (ins->oprs[j].scale == 0)
2936 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002937
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002938 if (!i && !b) {
2939 int ds = ins->oprs[j].disp_size;
2940 if ((addrbits != 64 && ds > 8) ||
2941 (addrbits == 64 && ds == 16))
2942 valid &= ds;
2943 } else {
2944 if (!(REG16 & ~b))
2945 valid &= 16;
2946 if (!(REG32 & ~b))
2947 valid &= 32;
2948 if (!(REG64 & ~b))
2949 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002950
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002951 if (!(REG16 & ~i))
2952 valid &= 16;
2953 if (!(REG32 & ~i))
2954 valid &= 32;
2955 if (!(REG64 & ~i))
2956 valid &= 64;
2957 }
2958 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002959 }
2960
2961 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002962 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002963 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002964 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002965 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002966 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002967 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002968 /* Impossible... */
2969 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2970 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002971 }
2972
2973 defdisp = ins->addr_size == 16 ? 16 : 32;
2974
2975 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002976 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2977 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2978 /*
2979 * mem_offs sizes must match the address size; if not,
2980 * strip the MEM_OFFS bit and match only EA instructions
2981 */
2982 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2983 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002984 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002985}