blob: acaf24bf35ea7a89457fcb30e483396a147e6211 [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 Anvinb6412502016-02-11 21:07:40 -0800360
361 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 Anvind24dd5f2016-02-08 10:32:13 -0800383 } else {
384 errfunc(ERR_WARNING | ERR_WARN_ZEXTRELOC,
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800385 "%d-bit unsigned relocation zero-extended from %d bits\n",
H. Peter Anvinb03d91e2016-02-11 21:13:54 -0800386 asize << 3, outfmt->maxbits);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800387 outfmt->output(segto, data, type, amax, segment, wrt);
388 size -= amax;
389 }
390 data = zero_buffer;
391 type = OUT_RAWDATA;
H. Peter Anvinb03d91e2016-02-11 21:13:54 -0800392 segment = wrt = NO_SEG;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800393 }
394
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800395 outfmt->output(segto, data, type, size, segment, wrt);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000396}
397
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800398static void out_imm8(int64_t offset, int32_t segment,
399 struct operand *opx, int asize)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400400{
401 if (opx->segment != NO_SEG) {
402 uint64_t data = opx->offset;
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800403 out(offset, segment, &data, OUT_ADDRESS, asize, opx->segment, opx->wrt);
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400404 } else {
405 uint8_t byte = opx->offset;
406 out(offset, segment, &byte, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
407 }
408}
409
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700410static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800411 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000412{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800413 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800414 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000415 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800416 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000417
H. Peter Anvin755f5212012-02-25 11:41:34 -0800418 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700419 return false;
420 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400421 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700422 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400423 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700424
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800425 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100426
Victor van den Elzen154e5922009-02-25 17:32:00 +0100427 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100428 /* Be optimistic in pass 1 */
429 return true;
430
H. Peter Anvine2c80182005-01-15 22:15:51 +0000431 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700432 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000433
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700434 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800435 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
436
437 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
438 /* jmp short (opcode eb) cannot be used with bnd prefix. */
439 ins->prefixes[PPS_REP] = P_none;
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800440 errfunc(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
441 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800442 }
443
444 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000445}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000446
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400447int64_t assemble(int32_t segment, int64_t offset, int bits, iflag_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400448 insn * instruction, struct ofmt *output, efunc error,
449 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000450{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000451 const struct itemplate *temp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000452 int j;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700453 enum match_result m;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800454 int64_t insn_end;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000455 int32_t itimes;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800456 int64_t start = offset;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300457 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000458
H. Peter Anvine2c80182005-01-15 22:15:51 +0000459 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000460 cpu = cp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000461 outfmt = output; /* likewise */
462 list = listgen; /* and again */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000463
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300464 wsize = idata_bytes(instruction->opcode);
465 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000466 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000467
H. Peter Anvineba20a72002-04-30 20:53:55 +0000468 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000469 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000470 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000471 if (t < 0)
472 errfunc(ERR_PANIC,
473 "instruction->times < 0 (%ld) in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000474
H. Peter Anvine2c80182005-01-15 22:15:51 +0000475 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400476 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000477 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400478 if (wsize > 8) {
H. Peter Anvin3be5d852008-05-20 14:49:32 -0700479 errfunc(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400480 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000481 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700482 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000483 out(offset, segment, &e->offset,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800484 OUT_ADDRESS, wsize, e->segment, e->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400485 offset += wsize;
486 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700487 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400488 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000489 int align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000490
H. Peter Anvine2c80182005-01-15 22:15:51 +0000491 out(offset, segment, e->stringval,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800492 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000493 align = e->stringlen % wsize;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000494
H. Peter Anvine2c80182005-01-15 22:15:51 +0000495 if (align) {
496 align = wsize - align;
H. Peter Anvin999868f2009-02-09 11:03:33 +0100497 out(offset, segment, zero_buffer,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800498 OUT_RAWDATA, align, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000499 }
500 offset += e->stringlen + align;
501 }
502 }
503 if (t > 0 && t == instruction->times - 1) {
504 /*
505 * Dummy call to list->output to give the offset to the
506 * listing module.
507 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800508 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000509 list->uplevel(LIST_TIMES);
510 }
511 }
512 if (instruction->times > 1)
513 list->downlevel(LIST_TIMES);
514 return offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000515 }
516
H. Peter Anvine2c80182005-01-15 22:15:51 +0000517 if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700518 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000519 FILE *fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000520
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400521 fp = fopen(fname, "rb");
522 if (!fp) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000523 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
524 fname);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400525 } else if (fseek(fp, 0L, SEEK_END) < 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000526 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
527 fname);
Philipp Klokedae212d2013-03-31 12:02:30 +0200528 fclose(fp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400529 } else {
H. Peter Anvin518df302008-06-14 16:53:48 -0700530 static char buf[4096];
531 size_t t = instruction->times;
532 size_t base = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400533 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000534
H. Peter Anvine2c80182005-01-15 22:15:51 +0000535 len = ftell(fp);
536 if (instruction->eops->next) {
537 base = instruction->eops->next->offset;
538 len -= base;
539 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700540 len > (size_t)instruction->eops->next->next->offset)
541 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000542 }
543 /*
544 * Dummy call to list->output to give the offset to the
545 * listing module.
546 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800547 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000548 list->uplevel(LIST_INCBIN);
549 while (t--) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700550 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000551
H. Peter Anvine2c80182005-01-15 22:15:51 +0000552 fseek(fp, base, SEEK_SET);
553 l = len;
554 while (l > 0) {
H. Peter Anvin4a5a6df2009-06-27 16:14:18 -0700555 int32_t m;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400556 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000557 if (!m) {
558 /*
559 * This shouldn't happen unless the file
560 * actually changes while we are reading
561 * it.
562 */
563 error(ERR_NONFATAL,
564 "`incbin': unexpected EOF while"
565 " reading file `%s'", fname);
566 t = 0; /* Try to exit cleanly */
567 break;
568 }
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800569 out(offset, segment, buf, OUT_RAWDATA, m,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 NO_SEG, NO_SEG);
571 l -= m;
572 }
573 }
574 list->downlevel(LIST_INCBIN);
575 if (instruction->times > 1) {
576 /*
577 * Dummy call to list->output to give the offset to the
578 * listing module.
579 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800580 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000581 list->uplevel(LIST_TIMES);
582 list->downlevel(LIST_TIMES);
583 }
584 fclose(fp);
585 return instruction->times * len;
586 }
587 return 0; /* if we're here, there's an error */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000588 }
589
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700590 /* Check to see if we need an address-size prefix */
591 add_asp(instruction, bits);
592
H. Peter Anvin23595f52009-07-25 17:44:25 -0700593 m = find_match(&temp, instruction, segment, offset, bits);
H. Peter Anvin70653092007-10-19 14:42:29 -0700594
H. Peter Anvin23595f52009-07-25 17:44:25 -0700595 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400596 /* Matches! */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800597 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400598 itimes = instruction->times;
599 if (insn_size < 0) /* shouldn't be, on pass two */
600 error(ERR_PANIC, "errors made it through from pass one");
601 else
602 while (itimes--) {
603 for (j = 0; j < MAXPREFIX; j++) {
604 uint8_t c = 0;
605 switch (instruction->prefixes[j]) {
606 case P_WAIT:
607 c = 0x9B;
608 break;
609 case P_LOCK:
610 c = 0xF0;
611 break;
612 case P_REPNE:
613 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800614 case P_XACQUIRE:
Jin Kyu Song03041092013-10-15 19:38:51 -0700615 case P_BND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400616 c = 0xF2;
617 break;
618 case P_REPE:
619 case P_REPZ:
620 case P_REP:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800621 case P_XRELEASE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400622 c = 0xF3;
623 break;
624 case R_CS:
625 if (bits == 64) {
626 error(ERR_WARNING | ERR_PASS2,
627 "cs segment base generated, but will be ignored in 64-bit mode");
628 }
629 c = 0x2E;
630 break;
631 case R_DS:
632 if (bits == 64) {
633 error(ERR_WARNING | ERR_PASS2,
634 "ds segment base generated, but will be ignored in 64-bit mode");
635 }
636 c = 0x3E;
637 break;
638 case R_ES:
639 if (bits == 64) {
640 error(ERR_WARNING | ERR_PASS2,
641 "es segment base generated, but will be ignored in 64-bit mode");
642 }
643 c = 0x26;
644 break;
645 case R_FS:
646 c = 0x64;
647 break;
648 case R_GS:
649 c = 0x65;
650 break;
651 case R_SS:
652 if (bits == 64) {
653 error(ERR_WARNING | ERR_PASS2,
654 "ss segment base generated, but will be ignored in 64-bit mode");
655 }
656 c = 0x36;
657 break;
658 case R_SEGR6:
659 case R_SEGR7:
660 error(ERR_NONFATAL,
661 "segr6 and segr7 cannot be used as prefixes");
662 break;
663 case P_A16:
664 if (bits == 64) {
665 error(ERR_NONFATAL,
666 "16-bit addressing is not supported "
667 "in 64-bit mode");
668 } else if (bits != 16)
669 c = 0x67;
670 break;
671 case P_A32:
672 if (bits != 32)
673 c = 0x67;
674 break;
675 case P_A64:
676 if (bits != 64) {
677 error(ERR_NONFATAL,
678 "64-bit addressing is only supported "
679 "in 64-bit mode");
680 }
681 break;
682 case P_ASP:
683 c = 0x67;
684 break;
685 case P_O16:
686 if (bits != 16)
687 c = 0x66;
688 break;
689 case P_O32:
690 if (bits == 16)
691 c = 0x66;
692 break;
693 case P_O64:
694 /* REX.W */
695 break;
696 case P_OSP:
697 c = 0x66;
698 break;
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700699 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800700 case P_VEX3:
701 case P_VEX2:
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800702 case P_NOBND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400703 case P_none:
704 break;
705 default:
706 error(ERR_PANIC, "invalid instruction prefix");
707 }
708 if (c != 0) {
709 out(offset, segment, &c, OUT_RAWDATA, 1,
710 NO_SEG, NO_SEG);
711 offset++;
712 }
713 }
714 insn_end = offset + insn_size;
715 gencode(segment, offset, bits, instruction,
716 temp, insn_end);
717 offset += insn_size;
718 if (itimes > 0 && itimes == instruction->times - 1) {
719 /*
720 * Dummy call to list->output to give the offset to the
721 * listing module.
722 */
723 list->output(offset, NULL, OUT_RAWDATA, 0);
724 list->uplevel(LIST_TIMES);
725 }
726 }
727 if (instruction->times > 1)
728 list->downlevel(LIST_TIMES);
729 return offset - start;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700730 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400731 /* No match */
732 switch (m) {
733 case MERR_OPSIZEMISSING:
734 error(ERR_NONFATAL, "operation size not specified");
735 break;
736 case MERR_OPSIZEMISMATCH:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000737 error(ERR_NONFATAL, "mismatch in operand sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400738 break;
Jin Kyu Song25c22122013-10-30 03:12:45 -0700739 case MERR_BRNUMMISMATCH:
740 error(ERR_NONFATAL,
741 "mismatch in the number of broadcasting elements");
742 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400743 case MERR_BADCPU:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000744 error(ERR_NONFATAL, "no instruction for this cpu level");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400745 break;
746 case MERR_BADMODE:
H. Peter Anvin6cda4142008-12-29 20:52:28 -0800747 error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400748 bits);
749 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -0800750 case MERR_ENCMISMATCH:
751 error(ERR_NONFATAL, "specific encoding scheme not available");
752 break;
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800753 case MERR_BADBND:
754 error(ERR_NONFATAL, "bnd prefix is not allowed");
755 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800756 case MERR_BADREPNE:
757 error(ERR_NONFATAL, "%s prefix is not allowed",
758 (has_prefix(instruction, PPS_REP, P_REPNE) ?
759 "repne" : "repnz"));
760 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400761 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000762 error(ERR_NONFATAL,
763 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400764 break;
765 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000766 }
767 return 0;
768}
769
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400770int64_t insn_size(int32_t segment, int64_t offset, int bits, iflag_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400771 insn * instruction, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000772{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000773 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700774 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000775
H. Peter Anvine2c80182005-01-15 22:15:51 +0000776 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000777 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000778
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400779 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000780 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000781
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700782 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
783 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400784 instruction->opcode == I_DT || instruction->opcode == I_DO ||
785 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000786 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300787 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000788
H. Peter Anvine2c80182005-01-15 22:15:51 +0000789 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300790 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000791
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400792 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000793 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000794
H. Peter Anvine2c80182005-01-15 22:15:51 +0000795 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400796 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000797 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400798 warn_overflow_const(e->offset, wsize);
799 } else if (e->type == EOT_DB_STRING ||
800 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000801 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000802
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 align = (-osize) % wsize;
804 if (align < 0)
805 align += wsize;
806 isize += osize + align;
807 }
808 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000809 }
810
H. Peter Anvine2c80182005-01-15 22:15:51 +0000811 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400812 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000813 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300814 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700815 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000816
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400817 fp = fopen(fname, "rb");
818 if (!fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
820 fname);
821 else if (fseek(fp, 0L, SEEK_END) < 0)
822 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
823 fname);
824 else {
825 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000826 if (instruction->eops->next) {
827 len -= instruction->eops->next->offset;
828 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700829 len > (size_t)instruction->eops->next->next->offset) {
830 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 }
832 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300833 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000834 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300835 if (fp)
836 fclose(fp);
837 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000838 }
839
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700840 /* Check to see if we need an address-size prefix */
841 add_asp(instruction, bits);
842
H. Peter Anvin23595f52009-07-25 17:44:25 -0700843 m = find_match(&temp, instruction, segment, offset, bits);
844 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400845 /* we've matched an instruction. */
846 int64_t isize;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400847 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100848
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800849 isize = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400850 if (isize < 0)
851 return -1;
852 for (j = 0; j < MAXPREFIX; j++) {
853 switch (instruction->prefixes[j]) {
854 case P_A16:
855 if (bits != 16)
856 isize++;
857 break;
858 case P_A32:
859 if (bits != 32)
860 isize++;
861 break;
862 case P_O16:
863 if (bits != 16)
864 isize++;
865 break;
866 case P_O32:
867 if (bits == 16)
868 isize++;
869 break;
870 case P_A64:
871 case P_O64:
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700872 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800873 case P_VEX3:
874 case P_VEX2:
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800875 case P_NOBND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400876 case P_none:
877 break;
878 default:
879 isize++;
880 break;
881 }
882 }
883 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700884 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400885 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000886 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000887}
888
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800889static void bad_hle_warn(const insn * ins, uint8_t hleok)
890{
891 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800892 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800893 static const enum whatwarn warn[2][4] =
894 {
895 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
896 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
897 };
898 unsigned int n;
899
900 n = (unsigned int)rep_pfx - P_XACQUIRE;
901 if (n > 1)
902 return; /* Not XACQUIRE/XRELEASE */
903
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800904 ww = warn[n][hleok];
905 if (!is_class(MEMORY, ins->oprs[0].type))
906 ww = w_inval; /* HLE requires operand 0 to be memory */
907
908 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800909 case w_none:
910 break;
911
912 case w_lock:
913 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800914 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800915 "%s with this instruction requires lock",
916 prefix_name(rep_pfx));
917 }
918 break;
919
920 case w_inval:
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800921 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800922 "%s invalid with this instruction",
923 prefix_name(rep_pfx));
924 break;
925 }
926}
927
H. Peter Anvin507ae032008-10-09 15:37:10 -0700928/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400929#define case3(x) case (x): case (x)+1: case (x)+2
930#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700931
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800932static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800933 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000934{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800935 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800936 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000937 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000938 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700939 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700940 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700941 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700942 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800943 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800944 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700945 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000946
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700947 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700948 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700949 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700950
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700951 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400952 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700953
H. Peter Anvine2c80182005-01-15 22:15:51 +0000954 (void)segment; /* Don't warn that this parameter is unused */
955 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000956
H. Peter Anvin839eca22007-10-29 23:12:47 -0700957 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400958 c = *codes++;
959 op1 = (c & 3) + ((opex & 1) << 2);
960 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
961 opx = &ins->oprs[op1];
962 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700963
H. Peter Anvin839eca22007-10-29 23:12:47 -0700964 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400965 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 codes += c, length += c;
967 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700968
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400969 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400970 opex = c;
971 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700972
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400973 case4(010):
974 ins->rex |=
975 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000976 codes++, length++;
977 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700978
Jin Kyu Song164d6072013-10-15 19:10:13 -0700979 case4(014):
980 /* this is an index reg of MIB operand */
981 mib_index = opx->basereg;
982 break;
983
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400984 case4(020):
985 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 length++;
987 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700988
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400989 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 length += 2;
991 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700992
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400993 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700994 if (opx->type & (BITS16 | BITS32 | BITS64))
995 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 else
997 length += (bits == 16) ? 2 : 4;
998 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700999
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001000 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 length += 4;
1002 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001003
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001004 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001005 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001007
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001008 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 length++;
1010 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001011
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001012 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001013 length += 8; /* MOV reg64/imm */
1014 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001015
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001016 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 length += 2;
1018 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001019
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001020 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001021 if (opx->type & (BITS16 | BITS32 | BITS64))
1022 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 else
1024 length += (bits == 16) ? 2 : 4;
1025 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001026
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001027 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 length += 4;
1029 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001030
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001031 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001032 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001033 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001034
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001035 case 0172:
1036 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001037 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001038 length++;
1039 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001040
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001041 case4(0174):
1042 length++;
1043 break;
1044
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001045 case4(0240):
1046 ins->rex |= REX_EV;
1047 ins->vexreg = regval(opx);
1048 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1049 ins->vex_cm = *codes++;
1050 ins->vex_wlp = *codes++;
1051 ins->evex_tuple = (*codes++ - 0300);
1052 break;
1053
1054 case 0250:
1055 ins->rex |= REX_EV;
1056 ins->vexreg = 0;
1057 ins->vex_cm = *codes++;
1058 ins->vex_wlp = *codes++;
1059 ins->evex_tuple = (*codes++ - 0300);
1060 break;
1061
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001062 case4(0254):
1063 length += 4;
1064 break;
1065
1066 case4(0260):
1067 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001068 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001069 ins->vex_cm = *codes++;
1070 ins->vex_wlp = *codes++;
1071 break;
1072
1073 case 0270:
1074 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001075 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001076 ins->vex_cm = *codes++;
1077 ins->vex_wlp = *codes++;
1078 break;
1079
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001080 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001081 hleok = c & 3;
1082 break;
1083
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001084 case4(0274):
1085 length++;
1086 break;
1087
1088 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001090
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001092 if (bits == 64)
1093 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001094 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001096
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001098 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001100
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001102 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001103
Keith Kaniosb7a89542007-04-12 02:40:54 +00001104 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001105 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1106 has_prefix(ins, PPS_ASIZE, P_A32))
1107 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001108 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001109
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001110 case4(0314):
1111 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001112
H. Peter Anvine2c80182005-01-15 22:15:51 +00001113 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001114 {
1115 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1116 if (pfx == P_O16)
1117 break;
1118 if (pfx != P_none)
1119 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1120 else
1121 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001122 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001123 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001124
H. Peter Anvine2c80182005-01-15 22:15:51 +00001125 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001126 {
1127 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1128 if (pfx == P_O32)
1129 break;
1130 if (pfx != P_none)
1131 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1132 else
1133 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001135 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001136
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 case 0322:
1138 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001139
Keith Kaniosb7a89542007-04-12 02:40:54 +00001140 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001141 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001142 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001143
Keith Kaniosb7a89542007-04-12 02:40:54 +00001144 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001145 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001146 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001147
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001148 case 0325:
1149 ins->rex |= REX_NH;
1150 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001151
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001152 case 0326:
1153 break;
1154
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155 case 0330:
1156 codes++, length++;
1157 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001158
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001161
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001162 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163 case 0333:
1164 length++;
1165 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001166
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001167 case 0334:
1168 ins->rex |= REX_L;
1169 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001170
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001171 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001172 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001173
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001174 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001175 if (!ins->prefixes[PPS_REP])
1176 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001177 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001178
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001179 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001180 if (!ins->prefixes[PPS_REP])
1181 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001182 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001183
H. Peter Anvine2c80182005-01-15 22:15:51 +00001184 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001185 if (ins->oprs[0].segment != NO_SEG)
1186 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1187 " quantity of BSS space");
1188 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001189 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001190 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001191
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001192 case 0341:
1193 if (!ins->prefixes[PPS_WAIT])
1194 ins->prefixes[PPS_WAIT] = P_WAIT;
1195 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001196
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001197 case 0360:
1198 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001199
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001200 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001201 length++;
1202 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001203
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001204 case 0364:
1205 case 0365:
1206 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001207
Keith Kanios48af1772007-08-17 07:37:52 +00001208 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001209 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001210 length++;
1211 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001212
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001213 case 0370:
1214 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001215 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001216
H. Peter Anvine2c80182005-01-15 22:15:51 +00001217 case 0373:
1218 length++;
1219 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001220
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001221 case 0374:
1222 eat = EA_XMMVSIB;
1223 break;
1224
1225 case 0375:
1226 eat = EA_YMMVSIB;
1227 break;
1228
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001229 case 0376:
1230 eat = EA_ZMMVSIB;
1231 break;
1232
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001233 case4(0100):
1234 case4(0110):
1235 case4(0120):
1236 case4(0130):
1237 case4(0200):
1238 case4(0204):
1239 case4(0210):
1240 case4(0214):
1241 case4(0220):
1242 case4(0224):
1243 case4(0230):
1244 case4(0234):
1245 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001246 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001247 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001248 opflags_t rflags;
1249 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001250 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001251
Keith Kaniosb7a89542007-04-12 02:40:54 +00001252 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001253
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001254 if (c <= 0177) {
1255 /* pick rfield from operand b (opx) */
1256 rflags = regflag(opx);
1257 rfield = nasm_regvals[opx->basereg];
1258 } else {
1259 rflags = 0;
1260 rfield = c & 7;
1261 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001262
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001263 /* EVEX.b1 : evex_brerop contains the operand position */
1264 op_er_sae = (ins->evex_brerop >= 0 ?
1265 &ins->oprs[ins->evex_brerop] : NULL);
1266
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001267 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1268 /* set EVEX.b */
1269 ins->evex_p[2] |= EVEX_P2B;
1270 if (op_er_sae->decoflags & ER) {
1271 /* set EVEX.RC (rounding control) */
1272 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1273 & EVEX_P2RC;
1274 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001275 } else {
1276 /* set EVEX.L'L (vector length) */
1277 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001278 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001279 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001280 /* set EVEX.b */
1281 ins->evex_p[2] |= EVEX_P2B;
1282 }
1283 }
1284
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001285 if (itemp_has(temp, IF_MIB)) {
1286 opy->eaflags |= EAF_MIB;
1287 /*
1288 * if a separate form of MIB (ICC style) is used,
1289 * the index reg info is merged into mem operand
1290 */
1291 if (mib_index != R_none) {
1292 opy->indexreg = mib_index;
1293 opy->scale = 1;
1294 opy->hintbase = mib_index;
1295 opy->hinttype = EAH_NOTBASE;
1296 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001297 }
1298
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001299 if (process_ea(opy, &ea_data, bits,
1300 rfield, rflags, ins) != eat) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001301 errfunc(ERR_NONFATAL, "invalid effective address");
1302 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001303 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001304 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001305 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001306 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001307 }
1308 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001309
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001310 default:
1311 errfunc(ERR_PANIC, "internal instruction table corrupt"
1312 ": instruction code \\%o (0x%02X) given", c, c);
1313 break;
1314 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001315 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001316
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001317 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001318
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001319 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001320 if (ins->rex & REX_H) {
1321 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1322 return -1;
1323 }
1324 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001325 }
1326
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001327 switch (ins->prefixes[PPS_VEX]) {
1328 case P_EVEX:
1329 if (!(ins->rex & REX_EV))
1330 return -1;
1331 break;
1332 case P_VEX3:
1333 case P_VEX2:
1334 if (!(ins->rex & REX_V))
1335 return -1;
1336 break;
1337 default:
1338 break;
1339 }
1340
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001341 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001342 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001343
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001344 if (ins->rex & REX_H) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001345 errfunc(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001346 return -1;
1347 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001348 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001349 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001350 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001351 ins->rex &= ~REX_W;
1352 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001353 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001354 ins->rex |= REX_W;
1355 bad32 &= ~REX_W;
1356 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001357 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001358 /* Follow REX_W */
1359 break;
1360 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001361
H. Peter Anvinfc561202011-07-07 16:58:22 -07001362 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001363 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1364 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001365 } else if (!(ins->rex & REX_EV) &&
1366 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
1367 errfunc(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
1368 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001369 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001370 if (ins->rex & REX_EV)
1371 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001372 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1373 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001374 length += 3;
1375 else
1376 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001377 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001378 if (ins->rex & REX_H) {
1379 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1380 return -1;
1381 } else if (bits == 64) {
1382 length++;
1383 } else if ((ins->rex & REX_L) &&
1384 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001385 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001386 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001387 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001388 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001389 length++;
1390 } else {
1391 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1392 return -1;
1393 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001394 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001395
1396 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001397 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -08001398 errfunc(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001399 "instruction is not lockable");
1400 }
1401
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001402 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001403
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001404 /*
1405 * when BND prefix is set by DEFAULT directive,
1406 * BND prefix is added to every appropriate instruction line
1407 * unless it is overridden by NOBND prefix.
1408 */
1409 if (globalbnd &&
1410 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1411 ins->prefixes[PPS_REP] = P_BND;
1412
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001413 return length;
1414}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001415
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001416static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1417{
1418 if (bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001419 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001420 !(ins->rex & (REX_V | REX_EV)) &&
1421 !ins->rex_done) {
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001422 int rex = (ins->rex & REX_MASK) | REX_P;
Cyrill Gorcunovaa29b1d2014-05-05 00:30:58 +04001423 out(offset, segment, &rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001424 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001425 return 1;
1426 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001427 }
1428
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001429 return 0;
1430}
1431
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001432static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001433 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001434 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001435{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001436 uint8_t c;
1437 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001438 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001439 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001440 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001441 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001442 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001443 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001444 enum ea_type eat = EA_SCALAR;
H. Peter Anvin70653092007-10-19 14:42:29 -07001445
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001446 ins->rex_done = false;
1447
H. Peter Anvin839eca22007-10-29 23:12:47 -07001448 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001449 c = *codes++;
1450 op1 = (c & 3) + ((opex & 1) << 2);
1451 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1452 opx = &ins->oprs[op1];
1453 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001454
H. Peter Anvin839eca22007-10-29 23:12:47 -07001455 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001456 case 01:
1457 case 02:
1458 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001459 case 04:
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001460 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001461 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 codes += c;
1463 offset += c;
1464 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001465
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001466 case 05:
1467 case 06:
1468 case 07:
1469 opex = c;
1470 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001471
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001472 case4(010):
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001473 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001474 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001475 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001476 offset += 1;
1477 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001478
Jin Kyu Song164d6072013-10-15 19:10:13 -07001479 case4(014):
1480 break;
1481
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001482 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001483 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001484 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001485 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001486 }
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001487 out_imm8(offset, segment, opx, -1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001488 offset += 1;
1489 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001490
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001491 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001492 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001493 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001494 "unsigned byte value exceeds bounds");
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001495 out_imm8(offset, segment, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001496 offset += 1;
1497 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001498
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001499 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001500 warn_overflow_opd(opx, 2);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001501 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001502 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001503 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001504 offset += 2;
1505 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001506
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001507 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001508 if (opx->type & (BITS16 | BITS32))
1509 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001510 else
1511 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001512 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001513 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001514 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001515 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001516 offset += size;
1517 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001518
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001519 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001520 warn_overflow_opd(opx, 4);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001521 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001522 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001523 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001524 offset += 4;
1525 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001526
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001527 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001528 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001529 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001530 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001531 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001532 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001533 offset += size;
1534 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001535
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001536 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001537 if (opx->segment != segment) {
1538 data = opx->offset;
1539 out(offset, segment, &data,
1540 OUT_REL1ADR, insn_end - offset,
1541 opx->segment, opx->wrt);
1542 } else {
1543 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001544 if (data > 127 || data < -128)
1545 errfunc(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001546 out(offset, segment, &data,
1547 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1548 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001549 offset += 1;
1550 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001551
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001552 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001553 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001554 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001555 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001556 offset += 8;
1557 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001558
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001559 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001560 if (opx->segment != segment) {
1561 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001562 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001563 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001564 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001565 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001566 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001567 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001568 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001569 }
1570 offset += 2;
1571 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001572
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001573 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001574 if (opx->type & (BITS16 | BITS32 | BITS64))
1575 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001576 else
1577 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001578 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001579 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001580 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001581 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1582 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001583 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001584 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001585 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001586 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 }
1588 offset += size;
1589 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001590
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001591 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001592 if (opx->segment != segment) {
1593 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001594 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001595 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001596 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001597 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001598 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001599 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001600 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001601 }
1602 offset += 4;
1603 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001604
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001605 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001606 if (opx->segment == NO_SEG)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001607 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1608 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001609 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001610 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001611 outfmt->segbase(1 + opx->segment),
1612 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001613 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001614 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001615
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001616 case 0172:
1617 c = *codes++;
1618 opx = &ins->oprs[c >> 3];
1619 bytes[0] = nasm_regvals[opx->basereg] << 4;
1620 opx = &ins->oprs[c & 7];
1621 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1622 errfunc(ERR_NONFATAL,
1623 "non-absolute expression not permitted as argument %d",
1624 c & 7);
1625 } else {
1626 if (opx->offset & ~15) {
1627 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1628 "four-bit argument exceeds bounds");
1629 }
1630 bytes[0] |= opx->offset & 15;
1631 }
1632 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1633 offset++;
1634 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001635
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001636 case 0173:
1637 c = *codes++;
1638 opx = &ins->oprs[c >> 4];
1639 bytes[0] = nasm_regvals[opx->basereg] << 4;
1640 bytes[0] |= c & 15;
1641 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1642 offset++;
1643 break;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001644
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001645 case4(0174):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001646 bytes[0] = nasm_regvals[opx->basereg] << 4;
1647 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1648 offset++;
1649 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001650
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001651 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001652 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001653 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1654 (int32_t)data != (int64_t)data) {
1655 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1656 "signed dword immediate exceeds bounds");
1657 }
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001658 out(offset, segment, &data, OUT_ADDRESS, -4,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001659 opx->segment, opx->wrt);
1660 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001661 break;
1662
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001663 case4(0240):
1664 case 0250:
1665 codes += 3;
1666 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1667 EVEX_P2Z | EVEX_P2AAA, 2);
1668 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1669 bytes[0] = 0x62;
1670 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001671 bytes[1] = ((((ins->rex & 7) << 5) |
1672 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
1673 (ins->vex_cm & 3);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001674 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1675 ((~ins->vexreg & 15) << 3) |
1676 (1 << 2) | (ins->vex_wlp & 3);
1677 bytes[3] = ins->evex_p[2];
1678 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1679 offset += 4;
1680 break;
1681
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001682 case4(0260):
1683 case 0270:
1684 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001685 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1686 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001687 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1688 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1689 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001690 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001691 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1692 offset += 3;
1693 } else {
1694 bytes[0] = 0xc5;
1695 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001696 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001697 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1698 offset += 2;
1699 }
1700 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001701
H. Peter Anvine014f352012-02-25 22:35:19 -08001702 case 0271:
1703 case 0272:
1704 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001705 break;
1706
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001707 case4(0274):
1708 {
1709 uint64_t uv, um;
1710 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001711
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001712 if (ins->rex & REX_W)
1713 s = 64;
1714 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1715 s = 16;
1716 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1717 s = 32;
1718 else
1719 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001720
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001721 um = (uint64_t)2 << (s-1);
1722 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001723
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001724 if (uv > 127 && uv < (uint64_t)-128 &&
1725 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001726 /* If this wasn't explicitly byte-sized, warn as though we
1727 * had fallen through to the imm16/32/64 case.
1728 */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001729 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001730 "%s value exceeds bounds",
1731 (opx->type & BITS8) ? "signed byte" :
1732 s == 16 ? "word" :
1733 s == 32 ? "dword" :
1734 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001735 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001736 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001737 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001738 out(offset, segment, &data, OUT_ADDRESS, 1,
1739 opx->segment, opx->wrt);
1740 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001741 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001742 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1743 NO_SEG);
1744 }
1745 offset += 1;
1746 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001747 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001748
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001749 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001750 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001751
H. Peter Anvine2c80182005-01-15 22:15:51 +00001752 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001753 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001754 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001755 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001756 offset += 1;
1757 } else
1758 offset += 0;
1759 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001760
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001762 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001764 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001765 offset += 1;
1766 } else
1767 offset += 0;
1768 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001769
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 case 0312:
1771 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001772
Keith Kaniosb7a89542007-04-12 02:40:54 +00001773 case 0313:
1774 ins->rex = 0;
1775 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001776
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001777 case4(0314):
1778 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001779
H. Peter Anvine2c80182005-01-15 22:15:51 +00001780 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001783
H. Peter Anvine2c80182005-01-15 22:15:51 +00001784 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001785 case 0323:
1786 break;
1787
Keith Kaniosb7a89542007-04-12 02:40:54 +00001788 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001789 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001791
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001792 case 0325:
1793 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001794
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001795 case 0326:
1796 break;
1797
H. Peter Anvine2c80182005-01-15 22:15:51 +00001798 case 0330:
Cyrill Gorcunov83e69242013-03-03 14:34:31 +04001799 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001800 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001801 offset += 1;
1802 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001803
H. Peter Anvine2c80182005-01-15 22:15:51 +00001804 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001805 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001806
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001807 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001809 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001810 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001811 offset += 1;
1812 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001813
Keith Kanios48af1772007-08-17 07:37:52 +00001814 case 0334:
1815 if (ins->rex & REX_R) {
1816 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001817 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001818 offset += 1;
1819 }
1820 ins->rex &= ~(REX_L|REX_R);
1821 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001822
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001823 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001824 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001825
H. Peter Anvin962e3052008-08-28 17:47:16 -07001826 case 0336:
1827 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001828 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001829
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 if (ins->oprs[0].segment != NO_SEG)
1832 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1833 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001834 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001835 if (size > 0)
1836 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001837 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001838 offset += size;
1839 }
1840 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001841
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001842 case 0341:
1843 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001844
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001845 case 0360:
1846 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001847
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001848 case 0361:
1849 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001850 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1851 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001852 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001853
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001854 case 0364:
1855 case 0365:
1856 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001857
Keith Kanios48af1772007-08-17 07:37:52 +00001858 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001859 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001860 *bytes = c - 0366 + 0x66;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001861 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001862 offset += 1;
1863 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001864
Jin Kyu Song03041092013-10-15 19:38:51 -07001865 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001866 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001867
H. Peter Anvine2c80182005-01-15 22:15:51 +00001868 case 0373:
1869 *bytes = bits == 16 ? 3 : 5;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001870 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001871 offset += 1;
1872 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001873
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001874 case 0374:
1875 eat = EA_XMMVSIB;
1876 break;
1877
1878 case 0375:
1879 eat = EA_YMMVSIB;
1880 break;
1881
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001882 case 0376:
1883 eat = EA_ZMMVSIB;
1884 break;
1885
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001886 case4(0100):
1887 case4(0110):
1888 case4(0120):
1889 case4(0130):
1890 case4(0200):
1891 case4(0204):
1892 case4(0210):
1893 case4(0214):
1894 case4(0220):
1895 case4(0224):
1896 case4(0230):
1897 case4(0234):
1898 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001899 ea ea_data;
1900 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001901 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001902 uint8_t *p;
1903 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001904 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001905
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001906 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001907 /* pick rfield from operand b (opx) */
1908 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001909 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001910 } else {
1911 /* rfield is constant */
1912 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001913 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001914 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001915
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001916 if (process_ea(opy, &ea_data, bits,
1917 rfield, rflags, ins) != eat)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001918 errfunc(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001919
H. Peter Anvine2c80182005-01-15 22:15:51 +00001920 p = bytes;
1921 *p++ = ea_data.modrm;
1922 if (ea_data.sib_present)
1923 *p++ = ea_data.sib;
1924
1925 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001926 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001927
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001928 /*
1929 * Make sure the address gets the right offset in case
1930 * the line breaks in the .lst file (BR 1197827)
1931 */
1932 offset += s;
1933 s = 0;
1934
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001935 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001936 /* use compressed displacement, if available */
1937 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001938 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001939 if (ea_data.rip) {
1940 if (opy->segment == segment) {
1941 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001942 if (overflow_signed(data, ea_data.bytes))
1943 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001944 out(offset, segment, &data, OUT_ADDRESS,
1945 ea_data.bytes, NO_SEG, NO_SEG);
1946 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001947 /* overflow check in output/linker? */
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001948 out(offset, segment, &data, OUT_REL4ADR,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001949 insn_end - offset, opy->segment, opy->wrt);
1950 }
1951 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001952 int asize = ins->addr_size >> 3;
1953 int atype = ea_data.bytes;
1954
1955 if (overflow_general(data, asize) ||
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001956 signed_bits(data, ins->addr_size) !=
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001957 signed_bits(data, ea_data.bytes << 3))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001958 warn_overflow(ERR_PASS2, ea_data.bytes);
1959
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001960 if (asize > ea_data.bytes) {
1961 /*
1962 * If the address isn't the full width of
1963 * the address size, treat is as signed...
1964 */
1965 atype = -atype;
1966 }
1967
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001968 out(offset, segment, &data, OUT_ADDRESS,
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001969 atype, opy->segment, opy->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001970 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001971 }
1972 offset += s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001973 }
1974 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001975
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001976 default:
1977 errfunc(ERR_PANIC, "internal instruction table corrupt"
1978 ": instruction code \\%o (0x%02X) given", c, c);
1979 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001980 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001981 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001982}
1983
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001984static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001985{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001986 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001987 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001988 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001989}
1990
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001991static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001992{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001993 if (!is_register(o->basereg))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001994 errfunc(ERR_PANIC, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001995 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001996}
1997
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001998static int op_rexflags(const operand * o, int mask)
1999{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002000 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002001 int val;
2002
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002003 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002004 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002005
H. Peter Anvina4835d42008-05-20 14:21:29 -07002006 flags = nasm_reg_flags[o->basereg];
2007 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002008
2009 return rexflags(val, flags, mask);
2010}
2011
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002012static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002013{
2014 int rex = 0;
2015
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002016 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002017 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002018 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002019 rex |= REX_W;
2020 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2021 rex |= REX_H;
2022 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2023 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002024
2025 return rex & mask;
2026}
2027
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002028static int evexflags(int val, decoflags_t deco,
2029 int mask, uint8_t byte)
2030{
2031 int evex = 0;
2032
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002033 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002034 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002035 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002036 evex |= (EVEX_P0RP | EVEX_P0X);
2037 break;
2038 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002039 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002040 evex |= EVEX_P2VP;
2041 if (deco & Z)
2042 evex |= EVEX_P2Z;
2043 if (deco & OPMASK_MASK)
2044 evex |= deco & EVEX_P2AAA;
2045 break;
2046 }
2047 return evex & mask;
2048}
2049
2050static int op_evexflags(const operand * o, int mask, uint8_t byte)
2051{
2052 int val;
2053
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002054 val = nasm_regvals[o->basereg];
2055
2056 return evexflags(val, o->decoflags, mask, byte);
2057}
2058
H. Peter Anvin23595f52009-07-25 17:44:25 -07002059static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002060 insn *instruction,
2061 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002062{
2063 const struct itemplate *temp;
2064 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002065 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002066 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002067 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002068 int i;
2069
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002070 /* broadcasting uses a different data element size */
2071 for (i = 0; i < instruction->operands; i++)
2072 if (i == broadcast)
2073 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2074 else
2075 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002076
2077 merr = MERR_INVALOP;
2078
2079 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002080 temp->opcode != I_none; temp++) {
2081 m = matches(temp, instruction, bits);
2082 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002083 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002084 m = MOK_GOOD;
2085 else
2086 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002087 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002088 /*
2089 * Missing operand size and a candidate for fuzzy matching...
2090 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002091 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002092 if (i == broadcast)
2093 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2094 else
2095 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002096 opsizemissing = true;
2097 }
2098 if (m > merr)
2099 merr = m;
2100 if (merr == MOK_GOOD)
2101 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002102 }
2103
2104 /* No match, but see if we can get a fuzzy operand size match... */
2105 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002106 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002107
2108 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002109 /*
2110 * We ignore extrinsic operand sizes on registers, so we should
2111 * never try to fuzzy-match on them. This also resolves the case
2112 * when we have e.g. "xmmrm128" in two different positions.
2113 */
2114 if (is_class(REGISTER, instruction->oprs[i].type))
2115 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002116
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002117 /* This tests if xsizeflags[i] has more than one bit set */
2118 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2119 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002120
Jin Kyu Song7903c072013-10-30 03:00:12 -07002121 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002122 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002123 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2124 BITS32 : BITS64);
2125 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002126 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002127 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002128 }
2129
2130 /* Try matching again... */
2131 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002132 temp->opcode != I_none; temp++) {
2133 m = matches(temp, instruction, bits);
2134 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002135 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002136 m = MOK_GOOD;
2137 else
2138 m = MERR_INVALOP;
2139 }
2140 if (m > merr)
2141 merr = m;
2142 if (merr == MOK_GOOD)
2143 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002144 }
2145
H. Peter Anvina81655b2009-07-25 18:15:28 -07002146done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002147 *tempp = temp;
2148 return merr;
2149}
2150
Mark Charneydcaef4b2014-10-09 13:45:17 -04002151static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2152{
2153 opflags_t opsize = opflags & SIZE_MASK;
2154 uint8_t brcast_num;
2155
2156 /*
2157 * Due to discontinuity between BITS64 and BITS128 (BITS80),
2158 * this cannot be a simple arithmetic calculation.
2159 */
2160 if (brsize > BITS64)
2161 errfunc(ERR_FATAL,
2162 "size of broadcasting element is greater than 64 bits");
2163
2164 switch (opsize) {
2165 case BITS64:
2166 brcast_num = BITS64 / brsize;
2167 break;
2168 default:
2169 brcast_num = (opsize / BITS128) * (BITS64 / brsize) * 2;
2170 break;
2171 }
2172
2173 return brcast_num;
2174}
2175
H. Peter Anvin65289e82009-07-25 17:25:11 -07002176static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002177 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002178{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002179 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002180 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002181 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002182
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002183 /*
2184 * Check the opcode
2185 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002186 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002187 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002188
2189 /*
2190 * Count the operands
2191 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002192 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002193 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002194
2195 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002196 * Is it legal?
2197 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002198 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002199 return MERR_INVALOP;
2200
2201 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002202 * {evex} available?
2203 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002204 switch (instruction->prefixes[PPS_VEX]) {
2205 case P_EVEX:
2206 if (!itemp_has(itemp, IF_EVEX))
2207 return MERR_ENCMISMATCH;
2208 break;
2209 case P_VEX3:
2210 case P_VEX2:
2211 if (!itemp_has(itemp, IF_VEX))
2212 return MERR_ENCMISMATCH;
2213 break;
2214 default:
2215 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002216 }
2217
2218 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002219 * Check that no spurious colons or TOs are present
2220 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002221 for (i = 0; i < itemp->operands; i++)
2222 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002223 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002224
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002225 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002226 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002227 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002228 switch (itemp_smask(itemp)) {
2229 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002230 asize = BITS8;
2231 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002232 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002233 asize = BITS16;
2234 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002235 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002236 asize = BITS32;
2237 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002238 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002239 asize = BITS64;
2240 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002241 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002242 asize = BITS128;
2243 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002244 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002245 asize = BITS256;
2246 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002247 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002248 asize = BITS512;
2249 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002250 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002251 switch (bits) {
2252 case 16:
2253 asize = BITS16;
2254 break;
2255 case 32:
2256 asize = BITS32;
2257 break;
2258 case 64:
2259 asize = BITS64;
2260 break;
2261 default:
2262 asize = 0;
2263 break;
2264 }
2265 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002266 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002267 asize = 0;
2268 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002269 }
2270
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002271 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002272 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002273 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002274 memset(size, 0, sizeof size);
2275 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002276 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002277 /* S- flags apply to all operands */
2278 for (i = 0; i < MAX_OPERANDS; i++)
2279 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002280 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002281
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002282 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002283 * Check that the operand flags all match up,
2284 * it's a bit tricky so lets be verbose:
2285 *
2286 * 1) Find out the size of operand. If instruction
2287 * doesn't have one specified -- we're trying to
2288 * guess it either from template (IF_S* flag) or
2289 * from code bits.
2290 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002291 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002292 * template has an operand size specified AND this size differ
2293 * from which instruction has (perhaps we got it from code bits)
2294 * we are:
2295 * a) Check that only size of instruction and operand is differ
2296 * other characteristics do match
2297 * b) Perhaps it's a register specified in instruction so
2298 * for such a case we just mark that operand as "size
2299 * missing" and this will turn on fuzzy operand size
2300 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002301 */
2302 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002303 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002304 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002305 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002306 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002307 opflags_t template_opsize, insn_opsize;
2308
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002309 if (!(type & SIZE_MASK))
2310 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002311
Jin Kyu Song7903c072013-10-30 03:00:12 -07002312 insn_opsize = type & SIZE_MASK;
2313 if (!is_broadcast) {
2314 template_opsize = itemp->opd[i] & SIZE_MASK;
2315 } else {
2316 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2317 /*
2318 * when broadcasting, the element size depends on
2319 * the instruction type. decorator flag should match.
2320 */
2321
2322 if (deco_brsize) {
2323 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002324 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002325 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002326 } else {
2327 template_opsize = 0;
2328 }
2329 }
2330
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002331 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002332 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002333 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002334 } else if (template_opsize) {
2335 if (template_opsize != insn_opsize) {
2336 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002337 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002338 } else if (!is_class(REGISTER, type)) {
2339 /*
2340 * Note: we don't honor extrinsic operand sizes for registers,
2341 * so "missing operand size" for a register should be
2342 * considered a wildcard match rather than an error.
2343 */
2344 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002345 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002346 } else if (is_broadcast &&
2347 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002348 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002349 /*
2350 * broadcasting opsize matches but the number of repeated memory
2351 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002352 * if 64b double precision float is broadcasted to ymm (256b),
2353 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002354 */
2355 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002356 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002357 }
2358 }
2359
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002360 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002361 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002362
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002363 /*
2364 * Check operand sizes
2365 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002366 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2367 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002368 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002369 asize = itemp->opd[i] & SIZE_MASK;
2370 if (asize) {
2371 for (i = 0; i < oprs; i++)
2372 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002373 break;
2374 }
2375 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002376 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002377 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002378 }
2379
Keith Kaniosb7a89542007-04-12 02:40:54 +00002380 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002381 if (!(itemp->opd[i] & SIZE_MASK) &&
2382 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002383 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002384 }
2385
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002386 /*
2387 * Check template is okay at the set cpu level
2388 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002389 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002390 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002391
Keith Kaniosb7a89542007-04-12 02:40:54 +00002392 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002393 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002394 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002395 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002396 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002397
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002398 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002399 * If we have a HLE prefix, look for the NOHLE flag
2400 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002401 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002402 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2403 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2404 return MERR_BADHLE;
2405
2406 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002407 * Check if special handling needed for Jumps
2408 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002409 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002410 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002411
Jin Kyu Song03041092013-10-15 19:38:51 -07002412 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002413 * Check if BND prefix is allowed.
2414 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002415 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002416 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002417 (has_prefix(instruction, PPS_REP, P_BND) ||
2418 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002419 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002420 else if (itemp_has(itemp, IF_BND) &&
2421 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2422 has_prefix(instruction, PPS_REP, P_REPNZ)))
2423 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002424
H. Peter Anvin60926242009-07-26 16:25:38 -07002425 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002426}
2427
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002428/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002429 * Check if ModR/M.mod should/can be 01.
2430 * - EAF_BYTEOFFS is set
2431 * - offset can fit in a byte when EVEX is not used
2432 * - offset can be compressed when EVEX is used
2433 */
2434#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2435 (o >= -128 && o <= 127 && \
2436 seg == NO_SEG && !forw_ref && \
2437 !(input->eaflags & EAF_WORDOFFS) && \
2438 !(ins->rex & REX_EV)) || \
2439 (ins->rex & REX_EV && \
2440 is_disp8n(input, ins, &output->disp8)))
2441
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002442static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002443 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002444{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002445 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002446 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002447 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002448
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002449 output->type = EA_SCALAR;
2450 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002451 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002452
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002453 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002454 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002455 /* EVEX.R' flag for the REG operand */
2456 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002457
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002458 if (is_class(REGISTER, input->type)) {
2459 /*
2460 * It's a direct register.
2461 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002462 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002463 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002464
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002465 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002466 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002467
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002468 /* broadcasting is not available with a direct register operand. */
2469 if (input->decoflags & BRDCAST_MASK) {
2470 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2471 goto err;
2472 }
2473
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002474 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002475 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002476 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002477 output->bytes = 0; /* no offset necessary either */
2478 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2479 } else {
2480 /*
2481 * It's a memory reference.
2482 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002483
2484 /* Embedded rounding or SAE is not available with a mem ref operand. */
2485 if (input->decoflags & (ER | SAE)) {
2486 nasm_error(ERR_NONFATAL,
2487 "Embedded rounding is available only with reg-reg op.");
2488 return -1;
2489 }
2490
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002491 if (input->basereg == -1 &&
2492 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002493 /*
2494 * It's a pure offset.
2495 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002496 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2497 input->segment == NO_SEG) {
2498 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2499 input->type &= ~IP_REL;
2500 input->type |= MEMORY;
2501 }
2502
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002503 if (bits == 64 &&
2504 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
2505 nasm_error(ERR_NONFATAL, "RIP-relative addressing is prohibited for mib.");
2506 return -1;
2507 }
2508
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002509 if (eaflags & EAF_BYTEOFFS ||
2510 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002511 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2512 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2513 }
2514
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002515 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002516 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002517 output->sib = GEN_SIB(0, 4, 5);
2518 output->bytes = 4;
2519 output->modrm = GEN_MODRM(0, rfield, 4);
2520 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002521 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002522 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002523 output->bytes = (addrbits != 16 ? 4 : 2);
2524 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2525 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002526 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002527 } else {
2528 /*
2529 * It's an indirection.
2530 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002531 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002532 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002533 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002534 int t, it, bt; /* register numbers */
2535 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002536
H. Peter Anvine2c80182005-01-15 22:15:51 +00002537 if (s == 0)
2538 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002539
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002540 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002541 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002542 ix = nasm_reg_flags[i];
2543 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002544 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002545 ix = 0;
2546 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002547
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002548 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002549 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002550 bx = nasm_reg_flags[b];
2551 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002552 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002553 bx = 0;
2554 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002555
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002556 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002557 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002558 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002559 int32_t o = input->offset;
2560 int mod, scale, index, base;
2561
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002562 /*
2563 * For a vector SIB, one has to be a vector and the other,
2564 * if present, a GPR. The vector must be the index operand.
2565 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002566 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002567 if (s == 0)
2568 s = 1;
2569 else if (s != 1)
2570 goto err;
2571
2572 t = bt, bt = it, it = t;
2573 x = bx, bx = ix, ix = x;
2574 }
2575
2576 if (bt != -1) {
2577 if (REG_GPR & ~bx)
2578 goto err;
2579 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2580 sok &= bx;
2581 else
2582 goto err;
2583 }
2584
2585 /*
2586 * While we're here, ensure the user didn't specify
2587 * WORD or QWORD
2588 */
2589 if (input->disp_size == 16 || input->disp_size == 64)
2590 goto err;
2591
2592 if (addrbits == 16 ||
2593 (addrbits == 32 && !(sok & BITS32)) ||
2594 (addrbits == 64 && !(sok & BITS64)))
2595 goto err;
2596
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002597 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2598 : ((ix & YMMREG & ~REG_EA)
2599 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002600
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002601 output->rex |= rexflags(it, ix, REX_X);
2602 output->rex |= rexflags(bt, bx, REX_B);
2603 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002604
2605 index = it & 7; /* it is known to be != -1 */
2606
2607 switch (s) {
2608 case 1:
2609 scale = 0;
2610 break;
2611 case 2:
2612 scale = 1;
2613 break;
2614 case 4:
2615 scale = 2;
2616 break;
2617 case 8:
2618 scale = 3;
2619 break;
2620 default: /* then what the smeg is it? */
2621 goto err; /* panic */
2622 }
2623
2624 if (bt == -1) {
2625 base = 5;
2626 mod = 0;
2627 } else {
2628 base = (bt & 7);
2629 if (base != REG_NUM_EBP && o == 0 &&
2630 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002631 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002632 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002633 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002634 mod = 1;
2635 else
2636 mod = 2;
2637 }
2638
2639 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002640 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2641 output->modrm = GEN_MODRM(mod, rfield, 4);
2642 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002643 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002644 /*
2645 * it must be a 32/64-bit memory reference. Firstly we have
2646 * to check that all registers involved are type E/Rxx.
2647 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002648 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002649 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002650
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002651 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002652 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2653 sok &= ix;
2654 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002655 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002656 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002657
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002658 if (bt != -1) {
2659 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002660 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002661 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002662 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002663 sok &= bx;
2664 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002665
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002666 /*
2667 * While we're here, ensure the user didn't specify
2668 * WORD or QWORD
2669 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002670 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002671 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002672
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002673 if (addrbits == 16 ||
2674 (addrbits == 32 && !(sok & BITS32)) ||
2675 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002676 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002677
Keith Kaniosb7a89542007-04-12 02:40:54 +00002678 /* now reorganize base/index */
2679 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002680 ((hb == b && ht == EAH_NOTBASE) ||
2681 (hb == i && ht == EAH_MAKEBASE))) {
2682 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002683 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002684 x = bx, bx = ix, ix = x;
2685 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002686
Jin Kyu Song164d6072013-10-15 19:10:13 -07002687 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002688 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002689 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002690 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002691 if (eaflags & EAF_MIB) {
2692 /* only for mib operands */
2693 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2694 /*
2695 * make a single reg index [reg*1].
2696 * gas uses this form for an explicit index register.
2697 */
2698 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2699 }
2700 if ((ht == EAH_SUMMED) && bt == -1) {
2701 /* separate once summed index into [base, index] */
2702 bt = it, bx = ix, s--;
2703 }
2704 } else {
2705 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002706 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002707 s == 3 || s == 5 || s == 9) && bt == -1) {
2708 /* convert 3*EAX to EAX+2*EAX */
2709 bt = it, bx = ix, s--;
2710 }
2711 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002712 (eaflags & EAF_TIMESTWO) &&
2713 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002714 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002715 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002716 * to sib format with 0x0 displacement - [EAX*1+0].
2717 */
2718 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2719 }
2720 }
Keith Kanios48af1772007-08-17 07:37:52 +00002721 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002722 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002723 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002724 x = ix, ix = bx, bx = x;
2725 }
2726 if (it == REG_NUM_ESP ||
2727 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002728 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002729
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002730 output->rex |= rexflags(it, ix, REX_X);
2731 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002732
Keith Kanios48af1772007-08-17 07:37:52 +00002733 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002734 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002735 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002736
Keith Kaniosb7a89542007-04-12 02:40:54 +00002737 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002738 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002739 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002740 } else {
2741 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002742 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002743 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002744 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002745 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002746 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002747 mod = 1;
2748 else
2749 mod = 2;
2750 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002751
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002752 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002753 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2754 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002755 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002756 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002757 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002758
Keith Kaniosb7a89542007-04-12 02:40:54 +00002759 if (it == -1)
2760 index = 4, s = 1;
2761 else
2762 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002763
H. Peter Anvine2c80182005-01-15 22:15:51 +00002764 switch (s) {
2765 case 1:
2766 scale = 0;
2767 break;
2768 case 2:
2769 scale = 1;
2770 break;
2771 case 4:
2772 scale = 2;
2773 break;
2774 case 8:
2775 scale = 3;
2776 break;
2777 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002778 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002779 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002780
Keith Kaniosb7a89542007-04-12 02:40:54 +00002781 if (bt == -1) {
2782 base = 5;
2783 mod = 0;
2784 } else {
2785 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002786 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002787 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002788 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002789 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002790 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002791 mod = 1;
2792 else
2793 mod = 2;
2794 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002795
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002796 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002797 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2798 output->modrm = GEN_MODRM(mod, rfield, 4);
2799 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002800 }
2801 } else { /* it's 16-bit */
2802 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002803 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002804
Keith Kaniosb7a89542007-04-12 02:40:54 +00002805 /* check for 64-bit long mode */
2806 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002807 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002808
H. Peter Anvine2c80182005-01-15 22:15:51 +00002809 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002810 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2811 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002812 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002813
Keith Kaniosb7a89542007-04-12 02:40:54 +00002814 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002815 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002816 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002817
H. Peter Anvine2c80182005-01-15 22:15:51 +00002818 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002819 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002820 if (b == -1 && i != -1) {
2821 int tmp = b;
2822 b = i;
2823 i = tmp;
2824 } /* swap */
2825 if ((b == R_SI || b == R_DI) && i != -1) {
2826 int tmp = b;
2827 b = i;
2828 i = tmp;
2829 }
2830 /* have BX/BP as base, SI/DI index */
2831 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002832 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002833 if (i != -1 && b != -1 &&
2834 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002835 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002836 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002837 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002838
H. Peter Anvine2c80182005-01-15 22:15:51 +00002839 rm = -1;
2840 if (i != -1)
2841 switch (i * 256 + b) {
2842 case R_SI * 256 + R_BX:
2843 rm = 0;
2844 break;
2845 case R_DI * 256 + R_BX:
2846 rm = 1;
2847 break;
2848 case R_SI * 256 + R_BP:
2849 rm = 2;
2850 break;
2851 case R_DI * 256 + R_BP:
2852 rm = 3;
2853 break;
2854 } else
2855 switch (b) {
2856 case R_SI:
2857 rm = 4;
2858 break;
2859 case R_DI:
2860 rm = 5;
2861 break;
2862 case R_BP:
2863 rm = 6;
2864 break;
2865 case R_BX:
2866 rm = 7;
2867 break;
2868 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002869 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002870 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002871
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002872 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002873 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002874 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002875 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002876 mod = 1;
2877 else
2878 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002879
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002880 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002881 output->bytes = mod; /* bytes of offset needed */
2882 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002883 }
2884 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002885 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002886
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002887 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002888 return output->type;
2889
2890err:
2891 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002892}
2893
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002894static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002895{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002896 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002897 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002898
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002899 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002900
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002901 switch (ins->prefixes[PPS_ASIZE]) {
2902 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002903 valid &= 16;
2904 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002905 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002906 valid &= 32;
2907 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002908 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002909 valid &= 64;
2910 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002911 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002912 valid &= (addrbits == 32) ? 16 : 32;
2913 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002914 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002915 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002916 }
2917
2918 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002919 if (is_class(MEMORY, ins->oprs[j].type)) {
2920 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002921
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002922 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002923 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002924 i = 0;
2925 else
2926 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002927
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002928 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002929 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002930 b = 0;
2931 else
2932 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002933
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002934 if (ins->oprs[j].scale == 0)
2935 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002936
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002937 if (!i && !b) {
2938 int ds = ins->oprs[j].disp_size;
2939 if ((addrbits != 64 && ds > 8) ||
2940 (addrbits == 64 && ds == 16))
2941 valid &= ds;
2942 } else {
2943 if (!(REG16 & ~b))
2944 valid &= 16;
2945 if (!(REG32 & ~b))
2946 valid &= 32;
2947 if (!(REG64 & ~b))
2948 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002949
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002950 if (!(REG16 & ~i))
2951 valid &= 16;
2952 if (!(REG32 & ~i))
2953 valid &= 32;
2954 if (!(REG64 & ~i))
2955 valid &= 64;
2956 }
2957 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002958 }
2959
2960 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002961 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002962 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002963 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002964 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002965 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002966 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002967 /* Impossible... */
2968 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2969 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002970 }
2971
2972 defdisp = ins->addr_size == 16 ? 16 : 32;
2973
2974 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002975 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2976 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2977 /*
2978 * mem_offs sizes must match the address size; if not,
2979 * strip the MEM_OFFS bit and match only EA instructions
2980 */
2981 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2982 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002983 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002984}