blob: ba0c0bdebd7b263447d0f80eb3bd6a3218a41780 [file] [log] [blame]
Ian Rogers706a10e2012-03-23 17:00:55 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "disassembler_x86.h"
18
Ian Rogerscf7f1912014-10-22 22:06:39 -070019#include <inttypes.h>
20
21#include <ostream>
Ian Rogersc7dd2952014-10-21 23:31:19 -070022#include <sstream>
Ian Rogers706a10e2012-03-23 17:00:55 -070023
Elliott Hughes07ed66b2012-12-12 18:34:25 -080024#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080025#include "base/stringprintf.h"
Elliott Hughes92301d92012-04-10 15:57:52 -070026#include "thread.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070027
Ian Rogers706a10e2012-03-23 17:00:55 -070028namespace art {
29namespace x86 {
30
Ian Rogersb23a7722012-10-09 16:54:26 -070031size_t DisassemblerX86::Dump(std::ostream& os, const uint8_t* begin) {
32 return DumpInstruction(os, begin);
33}
34
Ian Rogers706a10e2012-03-23 17:00:55 -070035void DisassemblerX86::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
36 size_t length = 0;
37 for (const uint8_t* cur = begin; cur < end; cur += length) {
38 length = DumpInstruction(os, cur);
39 }
40}
41
Vladimir Kostyukov122113a2014-05-30 17:56:23 +070042static const char* gReg8Names[] = {
43 "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh"
44};
45static const char* gExtReg8Names[] = {
46 "al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil",
47 "r8l", "r9l", "r10l", "r11l", "r12l", "r13l", "r14l", "r15l"
48};
49static const char* gReg16Names[] = {
50 "ax", "cx", "dx", "bx", "sp", "bp", "si", "di",
51 "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w"
52};
53static const char* gReg32Names[] = {
54 "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi",
55 "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d"
56};
Ian Rogers38e12032014-03-14 14:06:14 -070057static const char* gReg64Names[] = {
58 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
59 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
60};
Ian Rogers706a10e2012-03-23 17:00:55 -070061
Mark Mendella33720c2014-06-18 21:02:29 -040062// 64-bit opcode REX modifier.
Andreas Gampec8ccf682014-09-29 20:07:43 -070063constexpr uint8_t REX_W = 8U /* 0b1000 */;
64constexpr uint8_t REX_R = 4U /* 0b0100 */;
65constexpr uint8_t REX_X = 2U /* 0b0010 */;
66constexpr uint8_t REX_B = 1U /* 0b0001 */;
Mark Mendella33720c2014-06-18 21:02:29 -040067
Ian Rogers38e12032014-03-14 14:06:14 -070068static void DumpReg0(std::ostream& os, uint8_t rex, size_t reg,
Ian Rogers706a10e2012-03-23 17:00:55 -070069 bool byte_operand, uint8_t size_override) {
Ian Rogers38e12032014-03-14 14:06:14 -070070 DCHECK_LT(reg, (rex == 0) ? 8u : 16u);
Mark Mendella33720c2014-06-18 21:02:29 -040071 bool rex_w = (rex & REX_W) != 0;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +070072 if (byte_operand) {
73 os << ((rex == 0) ? gReg8Names[reg] : gExtReg8Names[reg]);
74 } else if (rex_w) {
75 os << gReg64Names[reg];
76 } else if (size_override == 0x66) {
77 os << gReg16Names[reg];
78 } else {
79 os << gReg32Names[reg];
Ian Rogers706a10e2012-03-23 17:00:55 -070080 }
81}
82
Mark Mendell88649c72014-06-04 21:20:00 -040083static void DumpAnyReg(std::ostream& os, uint8_t rex, size_t reg,
Vladimir Kostyukov122113a2014-05-30 17:56:23 +070084 bool byte_operand, uint8_t size_override, RegFile reg_file) {
85 if (reg_file == GPR) {
86 DumpReg0(os, rex, reg, byte_operand, size_override);
87 } else if (reg_file == SSE) {
88 os << "xmm" << reg;
89 } else {
90 os << "mm" << reg;
91 }
92}
93
Ian Rogers706a10e2012-03-23 17:00:55 -070094static void DumpReg(std::ostream& os, uint8_t rex, uint8_t reg,
Ian Rogersbf989802012-04-16 16:07:49 -070095 bool byte_operand, uint8_t size_override, RegFile reg_file) {
Mark Mendella33720c2014-06-18 21:02:29 -040096 bool rex_r = (rex & REX_R) != 0;
Ian Rogers38e12032014-03-14 14:06:14 -070097 size_t reg_num = rex_r ? (reg + 8) : reg;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +070098 DumpAnyReg(os, rex, reg_num, byte_operand, size_override, reg_file);
99}
100
101static void DumpRmReg(std::ostream& os, uint8_t rex, uint8_t reg,
102 bool byte_operand, uint8_t size_override, RegFile reg_file) {
Mark Mendella33720c2014-06-18 21:02:29 -0400103 bool rex_b = (rex & REX_B) != 0;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700104 size_t reg_num = rex_b ? (reg + 8) : reg;
105 DumpAnyReg(os, rex, reg_num, byte_operand, size_override, reg_file);
106}
107
108static void DumpAddrReg(std::ostream& os, uint8_t rex, uint8_t reg) {
109 if (rex != 0) {
110 os << gReg64Names[reg];
Ian Rogersbf989802012-04-16 16:07:49 -0700111 } else {
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700112 os << gReg32Names[reg];
Ian Rogersbf989802012-04-16 16:07:49 -0700113 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700114}
115
Ian Rogers7caad772012-03-30 01:07:54 -0700116static void DumpBaseReg(std::ostream& os, uint8_t rex, uint8_t reg) {
Mark Mendella33720c2014-06-18 21:02:29 -0400117 bool rex_b = (rex & REX_B) != 0;
Ian Rogers38e12032014-03-14 14:06:14 -0700118 size_t reg_num = rex_b ? (reg + 8) : reg;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700119 DumpAddrReg(os, rex, reg_num);
Ian Rogers706a10e2012-03-23 17:00:55 -0700120}
121
Vladimir Kostyukov79bb1842014-07-01 18:28:43 +0700122static void DumpOpcodeReg(std::ostream& os, uint8_t rex, uint8_t reg,
123 bool byte_operand, uint8_t size_override) {
Mark Mendella33720c2014-06-18 21:02:29 -0400124 bool rex_b = (rex & REX_B) != 0;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700125 size_t reg_num = rex_b ? (reg + 8) : reg;
Vladimir Kostyukov79bb1842014-07-01 18:28:43 +0700126 DumpReg0(os, rex, reg_num, byte_operand, size_override);
Ian Rogers706a10e2012-03-23 17:00:55 -0700127}
128
Elliott Hughes92301d92012-04-10 15:57:52 -0700129enum SegmentPrefix {
130 kCs = 0x2e,
131 kSs = 0x36,
132 kDs = 0x3e,
133 kEs = 0x26,
134 kFs = 0x64,
135 kGs = 0x65,
136};
137
Ian Rogers706a10e2012-03-23 17:00:55 -0700138static void DumpSegmentOverride(std::ostream& os, uint8_t segment_prefix) {
139 switch (segment_prefix) {
Elliott Hughes92301d92012-04-10 15:57:52 -0700140 case kCs: os << "cs:"; break;
141 case kSs: os << "ss:"; break;
142 case kDs: os << "ds:"; break;
143 case kEs: os << "es:"; break;
144 case kFs: os << "fs:"; break;
145 case kGs: os << "gs:"; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700146 default: break;
147 }
148}
149
Andreas Gampee5eb7062014-12-12 18:44:19 -0800150// Do not inline to avoid Clang stack frame problems. b/18733806
Andreas Gampe86830382014-12-12 21:41:29 -0800151NO_INLINE
152static std::string DumpCodeHex(const uint8_t* begin, const uint8_t* end) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800153 std::stringstream hex;
154 for (size_t i = 0; begin + i < end; ++i) {
155 hex << StringPrintf("%02X", begin[i]);
156 }
157 return hex.str();
158}
159
160std::string DisassemblerX86::DumpAddress(uint8_t mod, uint8_t rm, uint8_t rex64, uint8_t rex_w,
161 bool no_ops, bool byte_operand, bool byte_second_operand,
162 uint8_t* prefix, bool load, RegFile src_reg_file,
163 RegFile dst_reg_file, const uint8_t** instr,
164 uint32_t* address_bits) {
165 std::ostringstream address;
166 if (mod == 0 && rm == 5) {
167 if (!supports_rex_) { // Absolute address.
Nicolas Geoffray6a0b9202014-12-16 14:54:18 +0000168 *address_bits = *reinterpret_cast<const uint32_t*>(*instr);
Andreas Gampee5eb7062014-12-12 18:44:19 -0800169 address << StringPrintf("[0x%x]", *address_bits);
170 } else { // 64-bit RIP relative addressing.
171 address << StringPrintf("[RIP + 0x%x]", *reinterpret_cast<const uint32_t*>(*instr));
172 }
173 (*instr) += 4;
174 } else if (rm == 4 && mod != 3) { // SIB
175 uint8_t sib = **instr;
176 (*instr)++;
177 uint8_t scale = (sib >> 6) & 3;
178 uint8_t index = (sib >> 3) & 7;
179 uint8_t base = sib & 7;
180 address << "[";
Andreas Gampe031b00d2015-01-26 19:30:23 -0800181
182 // REX.x is bit 3 of index.
183 if ((rex64 & REX_X) != 0) {
184 index += 8;
185 }
186
187 // Mod = 0 && base = 5 (ebp): no base (ignores REX.b).
188 bool has_base = false;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800189 if (base != 5 || mod != 0) {
Andreas Gampe031b00d2015-01-26 19:30:23 -0800190 has_base = true;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800191 DumpBaseReg(address, rex64, base);
Andreas Gampe031b00d2015-01-26 19:30:23 -0800192 }
193
194 // Index = 4 (esp/rsp) is disallowed.
195 if (index != 4) {
196 if (has_base) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800197 address << " + ";
198 }
Andreas Gampe031b00d2015-01-26 19:30:23 -0800199 DumpAddrReg(address, rex64, index);
Andreas Gampee5eb7062014-12-12 18:44:19 -0800200 if (scale != 0) {
201 address << StringPrintf(" * %d", 1 << scale);
202 }
203 }
Andreas Gampe031b00d2015-01-26 19:30:23 -0800204
Andreas Gampee5eb7062014-12-12 18:44:19 -0800205 if (mod == 0) {
206 if (base == 5) {
207 if (index != 4) {
208 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(*instr));
209 } else {
210 // 64-bit low 32-bit absolute address, redundant absolute address encoding on 32-bit.
211 *address_bits = *reinterpret_cast<const uint32_t*>(*instr);
212 address << StringPrintf("%d", *address_bits);
213 }
214 (*instr) += 4;
215 }
216 } else if (mod == 1) {
217 address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(*instr));
218 (*instr)++;
219 } else if (mod == 2) {
220 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(*instr));
221 (*instr) += 4;
222 }
223 address << "]";
224 } else {
225 if (mod == 3) {
226 if (!no_ops) {
227 DumpRmReg(address, rex_w, rm, byte_operand || byte_second_operand,
228 prefix[2], load ? src_reg_file : dst_reg_file);
229 }
230 } else {
231 address << "[";
232 DumpBaseReg(address, rex64, rm);
233 if (mod == 1) {
234 address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(*instr));
235 (*instr)++;
236 } else if (mod == 2) {
237 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(*instr));
238 (*instr) += 4;
239 }
240 address << "]";
241 }
242 }
243 return address.str();
244}
245
Ian Rogers706a10e2012-03-23 17:00:55 -0700246size_t DisassemblerX86::DumpInstruction(std::ostream& os, const uint8_t* instr) {
247 const uint8_t* begin_instr = instr;
248 bool have_prefixes = true;
249 uint8_t prefix[4] = {0, 0, 0, 0};
Ian Rogers706a10e2012-03-23 17:00:55 -0700250 do {
251 switch (*instr) {
Ian Rogers7caad772012-03-30 01:07:54 -0700252 // Group 1 - lock and repeat prefixes:
Ian Rogers706a10e2012-03-23 17:00:55 -0700253 case 0xF0:
254 case 0xF2:
255 case 0xF3:
256 prefix[0] = *instr;
257 break;
258 // Group 2 - segment override prefixes:
Elliott Hughes92301d92012-04-10 15:57:52 -0700259 case kCs:
260 case kSs:
261 case kDs:
262 case kEs:
263 case kFs:
264 case kGs:
Ian Rogers706a10e2012-03-23 17:00:55 -0700265 prefix[1] = *instr;
266 break;
267 // Group 3 - operand size override:
268 case 0x66:
269 prefix[2] = *instr;
270 break;
271 // Group 4 - address size override:
272 case 0x67:
273 prefix[3] = *instr;
274 break;
275 default:
276 have_prefixes = false;
277 break;
278 }
279 if (have_prefixes) {
280 instr++;
281 }
282 } while (have_prefixes);
Ian Rogers38e12032014-03-14 14:06:14 -0700283 uint8_t rex = (supports_rex_ && (*instr >= 0x40) && (*instr <= 0x4F)) ? *instr : 0;
Vladimir Kostyukove8861b32014-04-18 17:06:15 +0700284 if (rex != 0) {
285 instr++;
286 }
Ian Rogers677c12f2014-11-07 16:58:38 -0800287 const char** modrm_opcodes = nullptr;
Ian Rogers706a10e2012-03-23 17:00:55 -0700288 bool has_modrm = false;
289 bool reg_is_opcode = false;
290 size_t immediate_bytes = 0;
291 size_t branch_bytes = 0;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800292 std::string opcode_tmp; // Storage to keep StringPrintf result alive.
293 const char* opcode0 = ""; // Prefix part.
294 const char* opcode1 = ""; // Main opcode.
295 const char* opcode2 = ""; // Sub-opcode. E.g., jump type.
296 const char* opcode3 = ""; // Mod-rm part.
297 const char* opcode4 = ""; // Suffix part.
Ian Rogers706a10e2012-03-23 17:00:55 -0700298 bool store = false; // stores to memory (ie rm is on the left)
299 bool load = false; // loads from memory (ie rm is on the right)
Serguei Katkov94f3eb02014-06-24 13:23:17 +0700300 bool byte_operand = false; // true when the opcode is dealing with byte operands
Ian Rogers677c12f2014-11-07 16:58:38 -0800301 // true when the source operand is a byte register but the target register isn't
302 // (ie movsxb/movzxb).
303 bool byte_second_operand = false;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700304 bool target_specific = false; // register name depends on target (64 vs 32 bits).
Ian Rogers706a10e2012-03-23 17:00:55 -0700305 bool ax = false; // implicit use of ax
jeffhaoe2962482012-06-28 11:29:57 -0700306 bool cx = false; // implicit use of cx
Ian Rogers706a10e2012-03-23 17:00:55 -0700307 bool reg_in_opcode = false; // low 3-bits of opcode encode register parameter
jeffhao703f2cd2012-07-13 17:25:52 -0700308 bool no_ops = false;
Ian Rogersbf989802012-04-16 16:07:49 -0700309 RegFile src_reg_file = GPR;
310 RegFile dst_reg_file = GPR;
Ian Rogers706a10e2012-03-23 17:00:55 -0700311 switch (*instr) {
312#define DISASSEMBLER_ENTRY(opname, \
313 rm8_r8, rm32_r32, \
314 r8_rm8, r32_rm32, \
315 ax8_i8, ax32_i32) \
Andreas Gampee5eb7062014-12-12 18:44:19 -0800316 case rm8_r8: opcode1 = #opname; store = true; has_modrm = true; byte_operand = true; break; \
317 case rm32_r32: opcode1 = #opname; store = true; has_modrm = true; break; \
318 case r8_rm8: opcode1 = #opname; load = true; has_modrm = true; byte_operand = true; break; \
319 case r32_rm32: opcode1 = #opname; load = true; has_modrm = true; break; \
320 case ax8_i8: opcode1 = #opname; ax = true; immediate_bytes = 1; byte_operand = true; break; \
321 case ax32_i32: opcode1 = #opname; ax = true; immediate_bytes = 4; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700322
323DISASSEMBLER_ENTRY(add,
324 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */,
325 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */,
326 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */)
327DISASSEMBLER_ENTRY(or,
328 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */,
329 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */,
330 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */)
331DISASSEMBLER_ENTRY(adc,
332 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */,
333 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */,
334 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */)
335DISASSEMBLER_ENTRY(sbb,
336 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */,
337 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */,
338 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */)
339DISASSEMBLER_ENTRY(and,
340 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */,
341 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */,
342 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */)
343DISASSEMBLER_ENTRY(sub,
344 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */,
345 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */,
346 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */)
347DISASSEMBLER_ENTRY(xor,
348 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */,
349 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */,
350 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */)
351DISASSEMBLER_ENTRY(cmp,
352 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */,
353 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */,
354 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */)
355
356#undef DISASSEMBLER_ENTRY
357 case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800358 opcode1 = "push";
Ian Rogers706a10e2012-03-23 17:00:55 -0700359 reg_in_opcode = true;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700360 target_specific = true;
Ian Rogers706a10e2012-03-23 17:00:55 -0700361 break;
362 case 0x58: case 0x59: case 0x5A: case 0x5B: case 0x5C: case 0x5D: case 0x5E: case 0x5F:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800363 opcode1 = "pop";
Ian Rogers706a10e2012-03-23 17:00:55 -0700364 reg_in_opcode = true;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700365 target_specific = true;
Ian Rogers706a10e2012-03-23 17:00:55 -0700366 break;
Mark Mendell33ecf8d2014-06-06 15:19:45 -0400367 case 0x63:
Vladimir Kostyukovec95f722014-07-23 12:10:07 +0700368 if ((rex & REX_W) != 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800369 opcode1 = "movsxd";
Mark Mendell33ecf8d2014-06-06 15:19:45 -0400370 has_modrm = true;
371 load = true;
372 } else {
373 // In 32-bit mode (!supports_rex_) this is ARPL, with no REX prefix the functionality is the
374 // same as 'mov' but the use of the instruction is discouraged.
Andreas Gampee5eb7062014-12-12 18:44:19 -0800375 opcode_tmp = StringPrintf("unknown opcode '%02X'", *instr);
376 opcode1 = opcode_tmp.c_str();
Mark Mendell33ecf8d2014-06-06 15:19:45 -0400377 }
378 break;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800379 case 0x68: opcode1 = "push"; immediate_bytes = 4; break;
380 case 0x69: opcode1 = "imul"; load = true; has_modrm = true; immediate_bytes = 4; break;
381 case 0x6A: opcode1 = "push"; immediate_bytes = 1; break;
382 case 0x6B: opcode1 = "imul"; load = true; has_modrm = true; immediate_bytes = 1; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700383 case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77:
384 case 0x78: case 0x79: case 0x7A: case 0x7B: case 0x7C: case 0x7D: case 0x7E: case 0x7F:
385 static const char* condition_codes[] =
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700386 {"o", "no", "b/nae/c", "nb/ae/nc", "z/eq", "nz/ne", "be/na", "nbe/a",
387 "s", "ns", "p/pe", "np/po", "l/nge", "nl/ge", "le/ng", "nle/g"
Ian Rogers706a10e2012-03-23 17:00:55 -0700388 };
Andreas Gampee5eb7062014-12-12 18:44:19 -0800389 opcode1 = "j";
390 opcode2 = condition_codes[*instr & 0xF];
Ian Rogers706a10e2012-03-23 17:00:55 -0700391 branch_bytes = 1;
392 break;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800393 case 0x86: case 0x87:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800394 opcode1 = "xchg";
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800395 store = true;
396 has_modrm = true;
397 byte_operand = (*instr == 0x86);
398 break;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800399 case 0x88: opcode1 = "mov"; store = true; has_modrm = true; byte_operand = true; break;
400 case 0x89: opcode1 = "mov"; store = true; has_modrm = true; break;
401 case 0x8A: opcode1 = "mov"; load = true; has_modrm = true; byte_operand = true; break;
402 case 0x8B: opcode1 = "mov"; load = true; has_modrm = true; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700403
404 case 0x0F: // 2 byte extended opcode
405 instr++;
406 switch (*instr) {
Ian Rogers7caad772012-03-30 01:07:54 -0700407 case 0x10: case 0x11:
408 if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800409 opcode1 = "movsd";
jeffhaofdffdf82012-07-11 16:08:43 -0700410 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700411 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800412 opcode1 = "movss";
jeffhaofdffdf82012-07-11 16:08:43 -0700413 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700414 } else if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800415 opcode1 = "movupd";
jeffhaofdffdf82012-07-11 16:08:43 -0700416 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700417 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800418 opcode1 = "movups";
Ian Rogers7caad772012-03-30 01:07:54 -0700419 }
420 has_modrm = true;
Ian Rogersbf989802012-04-16 16:07:49 -0700421 src_reg_file = dst_reg_file = SSE;
Ian Rogers7caad772012-03-30 01:07:54 -0700422 load = *instr == 0x10;
423 store = !load;
424 break;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800425 case 0x12: case 0x13:
426 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800427 opcode1 = "movlpd";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800428 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
429 } else if (prefix[0] == 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800430 opcode1 = "movlps";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800431 }
432 has_modrm = true;
433 src_reg_file = dst_reg_file = SSE;
434 load = *instr == 0x12;
435 store = !load;
436 break;
437 case 0x16: case 0x17:
438 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800439 opcode1 = "movhpd";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800440 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
441 } else if (prefix[0] == 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800442 opcode1 = "movhps";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800443 }
444 has_modrm = true;
445 src_reg_file = dst_reg_file = SSE;
446 load = *instr == 0x16;
447 store = !load;
448 break;
449 case 0x28: case 0x29:
450 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800451 opcode1 = "movapd";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800452 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
453 } else if (prefix[0] == 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800454 opcode1 = "movaps";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800455 }
456 has_modrm = true;
457 src_reg_file = dst_reg_file = SSE;
458 load = *instr == 0x28;
459 store = !load;
460 break;
jeffhaofdffdf82012-07-11 16:08:43 -0700461 case 0x2A:
462 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800463 opcode1 = "cvtpi2pd";
jeffhaofdffdf82012-07-11 16:08:43 -0700464 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
465 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800466 opcode1 = "cvtsi2sd";
jeffhaofdffdf82012-07-11 16:08:43 -0700467 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
468 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800469 opcode1 = "cvtsi2ss";
jeffhaofdffdf82012-07-11 16:08:43 -0700470 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
471 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800472 opcode1 = "cvtpi2ps";
jeffhaofdffdf82012-07-11 16:08:43 -0700473 }
474 load = true;
475 has_modrm = true;
476 dst_reg_file = SSE;
477 break;
478 case 0x2C:
479 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800480 opcode1 = "cvttpd2pi";
jeffhaofdffdf82012-07-11 16:08:43 -0700481 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
482 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800483 opcode1 = "cvttsd2si";
jeffhaofdffdf82012-07-11 16:08:43 -0700484 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
485 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800486 opcode1 = "cvttss2si";
jeffhaofdffdf82012-07-11 16:08:43 -0700487 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
488 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800489 opcode1 = "cvttps2pi";
jeffhaofdffdf82012-07-11 16:08:43 -0700490 }
491 load = true;
492 has_modrm = true;
493 src_reg_file = SSE;
494 break;
495 case 0x2D:
496 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800497 opcode1 = "cvtpd2pi";
jeffhaofdffdf82012-07-11 16:08:43 -0700498 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
499 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800500 opcode1 = "cvtsd2si";
jeffhaofdffdf82012-07-11 16:08:43 -0700501 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
502 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800503 opcode1 = "cvtss2si";
jeffhaofdffdf82012-07-11 16:08:43 -0700504 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
505 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800506 opcode1 = "cvtps2pi";
jeffhaofdffdf82012-07-11 16:08:43 -0700507 }
508 load = true;
509 has_modrm = true;
510 src_reg_file = SSE;
511 break;
512 case 0x2E:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800513 opcode0 = "u";
Ian Rogersfc787ec2014-10-09 21:56:44 -0700514 FALLTHROUGH_INTENDED;
jeffhaofdffdf82012-07-11 16:08:43 -0700515 case 0x2F:
516 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800517 opcode1 = "comisd";
jeffhaofdffdf82012-07-11 16:08:43 -0700518 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
519 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800520 opcode1 = "comiss";
jeffhaofdffdf82012-07-11 16:08:43 -0700521 }
522 has_modrm = true;
523 load = true;
524 src_reg_file = dst_reg_file = SSE;
525 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700526 case 0x38: // 3 byte extended opcode
Mark Mendellfe945782014-05-22 09:52:36 -0400527 instr++;
528 if (prefix[2] == 0x66) {
529 switch (*instr) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700530 case 0x01:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800531 opcode1 = "phaddw";
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700532 prefix[2] = 0;
533 has_modrm = true;
534 load = true;
535 src_reg_file = dst_reg_file = SSE;
536 break;
537 case 0x02:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800538 opcode1 = "phaddd";
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700539 prefix[2] = 0;
540 has_modrm = true;
541 load = true;
542 src_reg_file = dst_reg_file = SSE;
543 break;
Mark Mendellfe945782014-05-22 09:52:36 -0400544 case 0x40:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800545 opcode1 = "pmulld";
Mark Mendellfe945782014-05-22 09:52:36 -0400546 prefix[2] = 0;
547 has_modrm = true;
548 load = true;
549 src_reg_file = dst_reg_file = SSE;
550 break;
551 default:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800552 opcode_tmp = StringPrintf("unknown opcode '0F 38 %02X'", *instr);
553 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -0400554 }
555 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800556 opcode_tmp = StringPrintf("unknown opcode '0F 38 %02X'", *instr);
557 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -0400558 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700559 break;
560 case 0x3A: // 3 byte extended opcode
Mark Mendellfe945782014-05-22 09:52:36 -0400561 instr++;
562 if (prefix[2] == 0x66) {
563 switch (*instr) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400564 case 0x0A:
565 opcode1 = "roundss";
566 prefix[2] = 0;
567 has_modrm = true;
568 store = true;
569 src_reg_file = SSE;
570 dst_reg_file = SSE;
571 immediate_bytes = 1;
572 break;
573 case 0x0B:
574 opcode1 = "roundsd";
575 prefix[2] = 0;
576 has_modrm = true;
577 store = true;
578 src_reg_file = SSE;
579 dst_reg_file = SSE;
580 immediate_bytes = 1;
581 break;
Mark Mendellfe945782014-05-22 09:52:36 -0400582 case 0x14:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800583 opcode1 = "pextrb";
Mark Mendellfe945782014-05-22 09:52:36 -0400584 prefix[2] = 0;
585 has_modrm = true;
586 store = true;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700587 src_reg_file = SSE;
Mark Mendellfe945782014-05-22 09:52:36 -0400588 immediate_bytes = 1;
589 break;
590 case 0x16:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800591 opcode1 = "pextrd";
Mark Mendellfe945782014-05-22 09:52:36 -0400592 prefix[2] = 0;
593 has_modrm = true;
594 store = true;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700595 src_reg_file = SSE;
Mark Mendellfe945782014-05-22 09:52:36 -0400596 immediate_bytes = 1;
597 break;
598 default:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800599 opcode_tmp = StringPrintf("unknown opcode '0F 3A %02X'", *instr);
600 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -0400601 }
602 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800603 opcode_tmp = StringPrintf("unknown opcode '0F 3A %02X'", *instr);
604 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -0400605 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700606 break;
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800607 case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47:
608 case 0x48: case 0x49: case 0x4A: case 0x4B: case 0x4C: case 0x4D: case 0x4E: case 0x4F:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800609 opcode1 = "cmov";
610 opcode2 = condition_codes[*instr & 0xF];
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800611 has_modrm = true;
612 load = true;
613 break;
Ian Rogersbf989802012-04-16 16:07:49 -0700614 case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
615 case 0x58: case 0x59: case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
616 switch (*instr) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800617 case 0x50: opcode1 = "movmsk"; break;
618 case 0x51: opcode1 = "sqrt"; break;
619 case 0x52: opcode1 = "rsqrt"; break;
620 case 0x53: opcode1 = "rcp"; break;
621 case 0x54: opcode1 = "and"; break;
622 case 0x55: opcode1 = "andn"; break;
623 case 0x56: opcode1 = "or"; break;
624 case 0x57: opcode1 = "xor"; break;
625 case 0x58: opcode1 = "add"; break;
626 case 0x59: opcode1 = "mul"; break;
627 case 0x5C: opcode1 = "sub"; break;
628 case 0x5D: opcode1 = "min"; break;
629 case 0x5E: opcode1 = "div"; break;
630 case 0x5F: opcode1 = "max"; break;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700631 default: LOG(FATAL) << "Unreachable"; UNREACHABLE();
Ian Rogersbf989802012-04-16 16:07:49 -0700632 }
633 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800634 opcode2 = "pd";
jeffhaofdffdf82012-07-11 16:08:43 -0700635 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700636 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800637 opcode2 = "sd";
jeffhaofdffdf82012-07-11 16:08:43 -0700638 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700639 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800640 opcode2 = "ss";
jeffhaofdffdf82012-07-11 16:08:43 -0700641 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700642 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800643 opcode2 = "ps";
Ian Rogersbf989802012-04-16 16:07:49 -0700644 }
645 load = true;
646 has_modrm = true;
647 src_reg_file = dst_reg_file = SSE;
648 break;
649 }
650 case 0x5A:
651 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800652 opcode1 = "cvtpd2ps";
jeffhaofdffdf82012-07-11 16:08:43 -0700653 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700654 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800655 opcode1 = "cvtsd2ss";
jeffhaofdffdf82012-07-11 16:08:43 -0700656 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700657 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800658 opcode1 = "cvtss2sd";
jeffhaofdffdf82012-07-11 16:08:43 -0700659 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700660 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800661 opcode1 = "cvtps2pd";
Ian Rogersbf989802012-04-16 16:07:49 -0700662 }
663 load = true;
664 has_modrm = true;
665 src_reg_file = dst_reg_file = SSE;
666 break;
667 case 0x5B:
668 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800669 opcode1 = "cvtps2dq";
jeffhaofdffdf82012-07-11 16:08:43 -0700670 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700671 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800672 opcode1 = "bad opcode F2 0F 5B";
Ian Rogersbf989802012-04-16 16:07:49 -0700673 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800674 opcode1 = "cvttps2dq";
jeffhaofdffdf82012-07-11 16:08:43 -0700675 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700676 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800677 opcode1 = "cvtdq2ps";
Ian Rogersbf989802012-04-16 16:07:49 -0700678 }
679 load = true;
680 has_modrm = true;
681 src_reg_file = dst_reg_file = SSE;
682 break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700683 case 0x60: case 0x61: case 0x62: case 0x6C:
Razvan A Lupusorud3266bc2014-01-24 12:55:31 -0800684 if (prefix[2] == 0x66) {
685 src_reg_file = dst_reg_file = SSE;
686 prefix[2] = 0; // Clear prefix now. It has served its purpose as part of the opcode.
687 } else {
688 src_reg_file = dst_reg_file = MMX;
689 }
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700690 switch (*instr) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800691 case 0x60: opcode1 = "punpcklbw"; break;
692 case 0x61: opcode1 = "punpcklwd"; break;
693 case 0x62: opcode1 = "punpckldq"; break;
694 case 0x6c: opcode1 = "punpcklqdq"; break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700695 }
Razvan A Lupusorud3266bc2014-01-24 12:55:31 -0800696 load = true;
697 has_modrm = true;
698 break;
Ian Rogersbf989802012-04-16 16:07:49 -0700699 case 0x6E:
700 if (prefix[2] == 0x66) {
701 dst_reg_file = SSE;
jeffhaofdffdf82012-07-11 16:08:43 -0700702 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700703 } else {
704 dst_reg_file = MMX;
Ian Rogersbf989802012-04-16 16:07:49 -0700705 }
Andreas Gampee5eb7062014-12-12 18:44:19 -0800706 opcode1 = "movd";
Ian Rogersbf989802012-04-16 16:07:49 -0700707 load = true;
708 has_modrm = true;
709 break;
710 case 0x6F:
711 if (prefix[2] == 0x66) {
Mark Mendellfe945782014-05-22 09:52:36 -0400712 src_reg_file = dst_reg_file = SSE;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800713 opcode1 = "movdqa";
jeffhaofdffdf82012-07-11 16:08:43 -0700714 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700715 } else if (prefix[0] == 0xF3) {
Mark Mendellfe945782014-05-22 09:52:36 -0400716 src_reg_file = dst_reg_file = SSE;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800717 opcode1 = "movdqu";
jeffhaofdffdf82012-07-11 16:08:43 -0700718 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700719 } else {
720 dst_reg_file = MMX;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800721 opcode1 = "movq";
Ian Rogersbf989802012-04-16 16:07:49 -0700722 }
723 load = true;
724 has_modrm = true;
725 break;
Mark Mendellfe945782014-05-22 09:52:36 -0400726 case 0x70:
727 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800728 opcode1 = "pshufd";
Mark Mendellfe945782014-05-22 09:52:36 -0400729 prefix[2] = 0;
730 has_modrm = true;
731 store = true;
732 src_reg_file = dst_reg_file = SSE;
733 immediate_bytes = 1;
734 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800735 opcode1 = "pshuflw";
Mark Mendellfe945782014-05-22 09:52:36 -0400736 prefix[0] = 0;
737 has_modrm = true;
738 store = true;
739 src_reg_file = dst_reg_file = SSE;
740 immediate_bytes = 1;
741 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800742 opcode_tmp = StringPrintf("unknown opcode '0F %02X'", *instr);
743 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -0400744 }
745 break;
jeffhaofdffdf82012-07-11 16:08:43 -0700746 case 0x71:
747 if (prefix[2] == 0x66) {
748 dst_reg_file = SSE;
749 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
750 } else {
751 dst_reg_file = MMX;
752 }
Ian Rogers677c12f2014-11-07 16:58:38 -0800753 static const char* x71_opcodes[] = {
754 "unknown-71", "unknown-71", "psrlw", "unknown-71",
755 "psraw", "unknown-71", "psllw", "unknown-71"};
jeffhaofdffdf82012-07-11 16:08:43 -0700756 modrm_opcodes = x71_opcodes;
757 reg_is_opcode = true;
758 has_modrm = true;
759 store = true;
760 immediate_bytes = 1;
761 break;
762 case 0x72:
763 if (prefix[2] == 0x66) {
764 dst_reg_file = SSE;
765 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
766 } else {
767 dst_reg_file = MMX;
768 }
Ian Rogers677c12f2014-11-07 16:58:38 -0800769 static const char* x72_opcodes[] = {
770 "unknown-72", "unknown-72", "psrld", "unknown-72",
771 "psrad", "unknown-72", "pslld", "unknown-72"};
jeffhaofdffdf82012-07-11 16:08:43 -0700772 modrm_opcodes = x72_opcodes;
773 reg_is_opcode = true;
774 has_modrm = true;
775 store = true;
776 immediate_bytes = 1;
777 break;
778 case 0x73:
779 if (prefix[2] == 0x66) {
780 dst_reg_file = SSE;
781 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
782 } else {
783 dst_reg_file = MMX;
784 }
Ian Rogers677c12f2014-11-07 16:58:38 -0800785 static const char* x73_opcodes[] = {
786 "unknown-73", "unknown-73", "psrlq", "psrldq",
787 "unknown-73", "unknown-73", "psllq", "unknown-73"};
jeffhaofdffdf82012-07-11 16:08:43 -0700788 modrm_opcodes = x73_opcodes;
789 reg_is_opcode = true;
790 has_modrm = true;
791 store = true;
792 immediate_bytes = 1;
793 break;
Olivier Comefb0fecf2014-06-20 11:46:16 +0200794 case 0x7C:
795 if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800796 opcode1 = "haddps";
Olivier Comefb0fecf2014-06-20 11:46:16 +0200797 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
798 } else if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800799 opcode1 = "haddpd";
Olivier Comefb0fecf2014-06-20 11:46:16 +0200800 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
801 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800802 opcode_tmp = StringPrintf("unknown opcode '0F %02X'", *instr);
803 opcode1 = opcode_tmp.c_str();
Olivier Comefb0fecf2014-06-20 11:46:16 +0200804 break;
805 }
806 src_reg_file = dst_reg_file = SSE;
807 has_modrm = true;
808 load = true;
809 break;
jeffhaofdffdf82012-07-11 16:08:43 -0700810 case 0x7E:
811 if (prefix[2] == 0x66) {
812 src_reg_file = SSE;
813 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
814 } else {
815 src_reg_file = MMX;
816 }
Andreas Gampee5eb7062014-12-12 18:44:19 -0800817 opcode1 = "movd";
jeffhaofdffdf82012-07-11 16:08:43 -0700818 has_modrm = true;
819 store = true;
820 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700821 case 0x80: case 0x81: case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87:
822 case 0x88: case 0x89: case 0x8A: case 0x8B: case 0x8C: case 0x8D: case 0x8E: case 0x8F:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800823 opcode1 = "j";
824 opcode2 = condition_codes[*instr & 0xF];
Ian Rogers706a10e2012-03-23 17:00:55 -0700825 branch_bytes = 4;
826 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700827 case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97:
828 case 0x98: case 0x99: case 0x9A: case 0x9B: case 0x9C: case 0x9D: case 0x9E: case 0x9F:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800829 opcode1 = "set";
830 opcode2 = condition_codes[*instr & 0xF];
Ian Rogers677c12f2014-11-07 16:58:38 -0800831 modrm_opcodes = nullptr;
Ian Rogers7caad772012-03-30 01:07:54 -0700832 reg_is_opcode = true;
833 has_modrm = true;
834 store = true;
835 break;
Mark Mendell4708dcd2014-01-22 09:05:18 -0800836 case 0xA4:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800837 opcode1 = "shld";
Mark Mendell4708dcd2014-01-22 09:05:18 -0800838 has_modrm = true;
839 load = true;
840 immediate_bytes = 1;
841 break;
Yixin Shouf40f8902014-08-14 14:10:32 -0400842 case 0xA5:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800843 opcode1 = "shld";
Yixin Shouf40f8902014-08-14 14:10:32 -0400844 has_modrm = true;
845 load = true;
846 cx = true;
847 break;
Mark Mendell4708dcd2014-01-22 09:05:18 -0800848 case 0xAC:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800849 opcode1 = "shrd";
Mark Mendell4708dcd2014-01-22 09:05:18 -0800850 has_modrm = true;
851 load = true;
852 immediate_bytes = 1;
853 break;
Yixin Shouf40f8902014-08-14 14:10:32 -0400854 case 0xAD:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800855 opcode1 = "shrd";
Yixin Shouf40f8902014-08-14 14:10:32 -0400856 has_modrm = true;
857 load = true;
858 cx = true;
859 break;
jeffhao703f2cd2012-07-13 17:25:52 -0700860 case 0xAE:
861 if (prefix[0] == 0xF3) {
Ian Rogers5e588b32013-02-21 15:05:09 -0800862 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers677c12f2014-11-07 16:58:38 -0800863 static const char* xAE_opcodes[] = {
864 "rdfsbase", "rdgsbase", "wrfsbase", "wrgsbase",
865 "unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE"};
jeffhao703f2cd2012-07-13 17:25:52 -0700866 modrm_opcodes = xAE_opcodes;
867 reg_is_opcode = true;
868 has_modrm = true;
869 uint8_t reg_or_opcode = (instr[1] >> 3) & 7;
870 switch (reg_or_opcode) {
871 case 0:
872 prefix[1] = kFs;
873 load = true;
874 break;
875 case 1:
876 prefix[1] = kGs;
877 load = true;
878 break;
879 case 2:
880 prefix[1] = kFs;
881 store = true;
882 break;
883 case 3:
884 prefix[1] = kGs;
885 store = true;
886 break;
887 default:
888 load = true;
889 break;
890 }
891 } else {
Ian Rogers677c12f2014-11-07 16:58:38 -0800892 static const char* xAE_opcodes[] = {
893 "unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE",
894 "unknown-AE", "lfence", "mfence", "sfence"};
jeffhao703f2cd2012-07-13 17:25:52 -0700895 modrm_opcodes = xAE_opcodes;
896 reg_is_opcode = true;
897 has_modrm = true;
898 load = true;
899 no_ops = true;
900 }
901 break;
Ian Rogers677c12f2014-11-07 16:58:38 -0800902 case 0xAF:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800903 opcode1 = "imul";
Ian Rogers677c12f2014-11-07 16:58:38 -0800904 has_modrm = true;
905 load = true;
906 break;
907 case 0xB1:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800908 opcode1 = "cmpxchg";
Ian Rogers677c12f2014-11-07 16:58:38 -0800909 has_modrm = true;
910 store = true;
911 break;
912 case 0xB6:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800913 opcode1 = "movzxb";
Ian Rogers677c12f2014-11-07 16:58:38 -0800914 has_modrm = true;
915 load = true;
916 byte_second_operand = true;
917 break;
918 case 0xB7:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800919 opcode1 = "movzxw";
Ian Rogers677c12f2014-11-07 16:58:38 -0800920 has_modrm = true;
921 load = true;
922 break;
923 case 0xBE:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800924 opcode1 = "movsxb";
Ian Rogers677c12f2014-11-07 16:58:38 -0800925 has_modrm = true;
926 load = true;
927 byte_second_operand = true;
928 rex |= (rex == 0 ? 0 : REX_W);
929 break;
930 case 0xBF:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800931 opcode1 = "movsxw";
Ian Rogers677c12f2014-11-07 16:58:38 -0800932 has_modrm = true;
933 load = true;
934 break;
935 case 0xC3:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800936 opcode1 = "movnti";
Ian Rogers677c12f2014-11-07 16:58:38 -0800937 store = true;
938 has_modrm = true;
939 break;
Mark Mendellfe945782014-05-22 09:52:36 -0400940 case 0xC5:
941 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800942 opcode1 = "pextrw";
Mark Mendellfe945782014-05-22 09:52:36 -0400943 prefix[2] = 0;
944 has_modrm = true;
nikolay serdjukbd4e6a82015-03-27 16:32:27 +0600945 load = true;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700946 src_reg_file = SSE;
Mark Mendellfe945782014-05-22 09:52:36 -0400947 immediate_bytes = 1;
948 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800949 opcode_tmp = StringPrintf("unknown opcode '0F %02X'", *instr);
950 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -0400951 }
952 break;
Olivier Comefb0fecf2014-06-20 11:46:16 +0200953 case 0xC6:
954 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800955 opcode1 = "shufpd";
Olivier Comefb0fecf2014-06-20 11:46:16 +0200956 prefix[2] = 0;
957 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800958 opcode1 = "shufps";
Olivier Comefb0fecf2014-06-20 11:46:16 +0200959 }
960 has_modrm = true;
961 store = true;
962 src_reg_file = dst_reg_file = SSE;
963 immediate_bytes = 1;
964 break;
Vladimir Marko70b797d2013-12-03 15:25:24 +0000965 case 0xC7:
Ian Rogers677c12f2014-11-07 16:58:38 -0800966 static const char* x0FxC7_opcodes[] = {
967 "unknown-0f-c7", "cmpxchg8b", "unknown-0f-c7", "unknown-0f-c7",
968 "unknown-0f-c7", "unknown-0f-c7", "unknown-0f-c7", "unknown-0f-c7"};
Vladimir Marko70b797d2013-12-03 15:25:24 +0000969 modrm_opcodes = x0FxC7_opcodes;
970 has_modrm = true;
971 reg_is_opcode = true;
972 store = true;
973 break;
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100974 case 0xC8: case 0xC9: case 0xCA: case 0xCB: case 0xCC: case 0xCD: case 0xCE: case 0xCF:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800975 opcode1 = "bswap";
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100976 reg_in_opcode = true;
977 break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700978 case 0xD4:
979 if (prefix[2] == 0x66) {
980 src_reg_file = dst_reg_file = SSE;
981 prefix[2] = 0;
982 } else {
983 src_reg_file = dst_reg_file = MMX;
984 }
Andreas Gampee5eb7062014-12-12 18:44:19 -0800985 opcode1 = "paddq";
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700986 prefix[2] = 0;
987 has_modrm = true;
988 load = true;
989 break;
Mark Mendellfe945782014-05-22 09:52:36 -0400990 case 0xDB:
991 if (prefix[2] == 0x66) {
992 src_reg_file = dst_reg_file = SSE;
993 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
994 } else {
995 src_reg_file = dst_reg_file = MMX;
996 }
Andreas Gampee5eb7062014-12-12 18:44:19 -0800997 opcode1 = "pand";
Mark Mendellfe945782014-05-22 09:52:36 -0400998 prefix[2] = 0;
999 has_modrm = true;
1000 load = true;
1001 break;
1002 case 0xD5:
1003 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001004 opcode1 = "pmullw";
Mark Mendellfe945782014-05-22 09:52:36 -04001005 prefix[2] = 0;
1006 has_modrm = true;
1007 load = true;
1008 src_reg_file = dst_reg_file = SSE;
1009 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001010 opcode_tmp = StringPrintf("unknown opcode '0F %02X'", *instr);
1011 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -04001012 }
1013 break;
1014 case 0xEB:
1015 if (prefix[2] == 0x66) {
1016 src_reg_file = dst_reg_file = SSE;
1017 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
1018 } else {
1019 src_reg_file = dst_reg_file = MMX;
1020 }
Andreas Gampee5eb7062014-12-12 18:44:19 -08001021 opcode1 = "por";
Mark Mendellfe945782014-05-22 09:52:36 -04001022 prefix[2] = 0;
1023 has_modrm = true;
1024 load = true;
1025 break;
1026 case 0xEF:
1027 if (prefix[2] == 0x66) {
1028 src_reg_file = dst_reg_file = SSE;
1029 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
1030 } else {
1031 src_reg_file = dst_reg_file = MMX;
1032 }
Andreas Gampee5eb7062014-12-12 18:44:19 -08001033 opcode1 = "pxor";
Mark Mendellfe945782014-05-22 09:52:36 -04001034 prefix[2] = 0;
1035 has_modrm = true;
1036 load = true;
1037 break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001038 case 0xF4:
1039 case 0xF6:
Mark Mendellfe945782014-05-22 09:52:36 -04001040 case 0xF8:
Mark Mendellfe945782014-05-22 09:52:36 -04001041 case 0xF9:
Mark Mendellfe945782014-05-22 09:52:36 -04001042 case 0xFA:
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001043 case 0xFB:
Mark Mendellfe945782014-05-22 09:52:36 -04001044 case 0xFC:
Mark Mendellfe945782014-05-22 09:52:36 -04001045 case 0xFD:
Mark Mendellfe945782014-05-22 09:52:36 -04001046 case 0xFE:
1047 if (prefix[2] == 0x66) {
1048 src_reg_file = dst_reg_file = SSE;
1049 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
1050 } else {
1051 src_reg_file = dst_reg_file = MMX;
1052 }
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001053 switch (*instr) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001054 case 0xF4: opcode1 = "pmuludq"; break;
1055 case 0xF6: opcode1 = "psadbw"; break;
1056 case 0xF8: opcode1 = "psubb"; break;
1057 case 0xF9: opcode1 = "psubw"; break;
1058 case 0xFA: opcode1 = "psubd"; break;
1059 case 0xFB: opcode1 = "psubq"; break;
1060 case 0xFC: opcode1 = "paddb"; break;
1061 case 0xFD: opcode1 = "paddw"; break;
1062 case 0xFE: opcode1 = "paddd"; break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001063 }
Mark Mendellfe945782014-05-22 09:52:36 -04001064 prefix[2] = 0;
1065 has_modrm = true;
1066 load = true;
1067 break;
Ian Rogers706a10e2012-03-23 17:00:55 -07001068 default:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001069 opcode_tmp = StringPrintf("unknown opcode '0F %02X'", *instr);
1070 opcode1 = opcode_tmp.c_str();
Ian Rogers706a10e2012-03-23 17:00:55 -07001071 break;
1072 }
1073 break;
1074 case 0x80: case 0x81: case 0x82: case 0x83:
1075 static const char* x80_opcodes[] = {"add", "or", "adc", "sbb", "and", "sub", "xor", "cmp"};
1076 modrm_opcodes = x80_opcodes;
1077 has_modrm = true;
1078 reg_is_opcode = true;
1079 store = true;
1080 byte_operand = (*instr & 1) == 0;
1081 immediate_bytes = *instr == 0x81 ? 4 : 1;
1082 break;
jeffhao703f2cd2012-07-13 17:25:52 -07001083 case 0x84: case 0x85:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001084 opcode1 = "test";
jeffhao703f2cd2012-07-13 17:25:52 -07001085 has_modrm = true;
1086 load = true;
1087 byte_operand = (*instr & 1) == 0;
1088 break;
Ian Rogers7caad772012-03-30 01:07:54 -07001089 case 0x8D:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001090 opcode1 = "lea";
Ian Rogers7caad772012-03-30 01:07:54 -07001091 has_modrm = true;
1092 load = true;
1093 break;
jeffhao703f2cd2012-07-13 17:25:52 -07001094 case 0x8F:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001095 opcode1 = "pop";
jeffhao703f2cd2012-07-13 17:25:52 -07001096 has_modrm = true;
1097 reg_is_opcode = true;
1098 store = true;
1099 break;
Mark Mendell2bf31e62014-01-23 12:13:40 -08001100 case 0x99:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001101 opcode1 = "cdq";
Mark Mendell2bf31e62014-01-23 12:13:40 -08001102 break;
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001103 case 0x9B:
1104 if (instr[1] == 0xDF && instr[2] == 0xE0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001105 opcode1 = "fstsw\tax";
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001106 instr += 2;
1107 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001108 opcode_tmp = StringPrintf("unknown opcode '%02X'", *instr);
1109 opcode1 = opcode_tmp.c_str();
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001110 }
1111 break;
Mark Mendell4028a6c2014-02-19 20:06:20 -08001112 case 0xAF:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001113 opcode1 = (prefix[2] == 0x66 ? "scasw" : "scasl");
Mark Mendell4028a6c2014-02-19 20:06:20 -08001114 break;
Ian Rogers706a10e2012-03-23 17:00:55 -07001115 case 0xB0: case 0xB1: case 0xB2: case 0xB3: case 0xB4: case 0xB5: case 0xB6: case 0xB7:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001116 opcode1 = "mov";
Ian Rogers706a10e2012-03-23 17:00:55 -07001117 immediate_bytes = 1;
Mark Mendella33720c2014-06-18 21:02:29 -04001118 byte_operand = true;
Ian Rogers706a10e2012-03-23 17:00:55 -07001119 reg_in_opcode = true;
Vladimir Kostyukov79bb1842014-07-01 18:28:43 +07001120 byte_operand = true;
Ian Rogers706a10e2012-03-23 17:00:55 -07001121 break;
1122 case 0xB8: case 0xB9: case 0xBA: case 0xBB: case 0xBC: case 0xBD: case 0xBE: case 0xBF:
Vladimir Kostyukovec95f722014-07-23 12:10:07 +07001123 if ((rex & REX_W) != 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001124 opcode1 = "movabsq";
Yixin Shou5192cbb2014-07-01 13:48:17 -04001125 immediate_bytes = 8;
1126 reg_in_opcode = true;
1127 break;
1128 }
Andreas Gampee5eb7062014-12-12 18:44:19 -08001129 opcode1 = "mov";
Ian Rogers706a10e2012-03-23 17:00:55 -07001130 immediate_bytes = 4;
1131 reg_in_opcode = true;
1132 break;
Ian Rogers7caad772012-03-30 01:07:54 -07001133 case 0xC0: case 0xC1:
jeffhaoe2962482012-06-28 11:29:57 -07001134 case 0xD0: case 0xD1: case 0xD2: case 0xD3:
Ian Rogers7caad772012-03-30 01:07:54 -07001135 static const char* shift_opcodes[] =
1136 {"rol", "ror", "rcl", "rcr", "shl", "shr", "unknown-shift", "sar"};
1137 modrm_opcodes = shift_opcodes;
1138 has_modrm = true;
1139 reg_is_opcode = true;
1140 store = true;
Elliott Hughes16b5c292012-04-16 20:37:16 -07001141 immediate_bytes = ((*instr & 0xf0) == 0xc0) ? 1 : 0;
jeffhaoe2962482012-06-28 11:29:57 -07001142 cx = (*instr == 0xD2) || (*instr == 0xD3);
1143 byte_operand = (*instr == 0xC0);
Ian Rogers7caad772012-03-30 01:07:54 -07001144 break;
Andreas Gampee5eb7062014-12-12 18:44:19 -08001145 case 0xC3: opcode1 = "ret"; break;
Mark Mendella33720c2014-06-18 21:02:29 -04001146 case 0xC6:
Ian Rogers677c12f2014-11-07 16:58:38 -08001147 static const char* c6_opcodes[] = {"mov", "unknown-c6", "unknown-c6",
1148 "unknown-c6", "unknown-c6", "unknown-c6",
1149 "unknown-c6", "unknown-c6"};
Mark Mendella33720c2014-06-18 21:02:29 -04001150 modrm_opcodes = c6_opcodes;
1151 store = true;
1152 immediate_bytes = 1;
1153 has_modrm = true;
1154 reg_is_opcode = true;
1155 byte_operand = true;
1156 break;
Elliott Hughes0589ca92012-04-09 18:26:20 -07001157 case 0xC7:
Ian Rogers677c12f2014-11-07 16:58:38 -08001158 static const char* c7_opcodes[] = {"mov", "unknown-c7", "unknown-c7",
1159 "unknown-c7", "unknown-c7", "unknown-c7",
1160 "unknown-c7", "unknown-c7"};
Elliott Hughes0589ca92012-04-09 18:26:20 -07001161 modrm_opcodes = c7_opcodes;
1162 store = true;
1163 immediate_bytes = 4;
1164 has_modrm = true;
1165 reg_is_opcode = true;
1166 break;
Andreas Gampee5eb7062014-12-12 18:44:19 -08001167 case 0xCC: opcode1 = "int 3"; break;
Mark Mendelld19b55a2013-12-12 09:55:34 -08001168 case 0xD9:
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001169 if (instr[1] == 0xF8) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001170 opcode1 = "fprem";
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001171 instr++;
1172 } else {
1173 static const char* d9_opcodes[] = {"flds", "unknown-d9", "fsts", "fstps", "fldenv", "fldcw",
1174 "fnstenv", "fnstcw"};
1175 modrm_opcodes = d9_opcodes;
1176 store = true;
1177 has_modrm = true;
1178 reg_is_opcode = true;
1179 }
1180 break;
1181 case 0xDA:
1182 if (instr[1] == 0xE9) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001183 opcode1 = "fucompp";
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001184 instr++;
1185 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001186 opcode_tmp = StringPrintf("unknown opcode '%02X'", *instr);
1187 opcode1 = opcode_tmp.c_str();
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001188 }
Mark Mendelld19b55a2013-12-12 09:55:34 -08001189 break;
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -08001190 case 0xDB:
Ian Rogers677c12f2014-11-07 16:58:38 -08001191 static const char* db_opcodes[] = {"fildl", "unknown-db", "unknown-db",
1192 "unknown-db", "unknown-db", "unknown-db",
1193 "unknown-db", "unknown-db"};
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -08001194 modrm_opcodes = db_opcodes;
1195 load = true;
1196 has_modrm = true;
1197 reg_is_opcode = true;
1198 break;
Mark Mendelld19b55a2013-12-12 09:55:34 -08001199 case 0xDD:
Ian Rogers677c12f2014-11-07 16:58:38 -08001200 static const char* dd_opcodes[] = {"fldl", "fisttp", "fstl",
1201 "fstpl", "frstor", "unknown-dd",
1202 "fnsave", "fnstsw"};
Mark Mendelld19b55a2013-12-12 09:55:34 -08001203 modrm_opcodes = dd_opcodes;
1204 store = true;
1205 has_modrm = true;
1206 reg_is_opcode = true;
1207 break;
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -08001208 case 0xDF:
Ian Rogers677c12f2014-11-07 16:58:38 -08001209 static const char* df_opcodes[] = {"fild", "unknown-df", "unknown-df",
1210 "unknown-df", "unknown-df", "fildll",
1211 "unknown-df", "unknown-df"};
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -08001212 modrm_opcodes = df_opcodes;
1213 load = true;
1214 has_modrm = true;
1215 reg_is_opcode = true;
1216 break;
Andreas Gampee5eb7062014-12-12 18:44:19 -08001217 case 0xE3: opcode1 = "jecxz"; branch_bytes = 1; break;
1218 case 0xE8: opcode1 = "call"; branch_bytes = 4; break;
1219 case 0xE9: opcode1 = "jmp"; branch_bytes = 4; break;
1220 case 0xEB: opcode1 = "jmp"; branch_bytes = 1; break;
1221 case 0xF5: opcode1 = "cmc"; break;
jeffhao174651d2012-04-19 15:27:22 -07001222 case 0xF6: case 0xF7:
Ian Rogers677c12f2014-11-07 16:58:38 -08001223 static const char* f7_opcodes[] = {
1224 "test", "unknown-f7", "not", "neg", "mul edx:eax, eax *",
1225 "imul edx:eax, eax *", "div edx:eax, edx:eax /",
1226 "idiv edx:eax, edx:eax /"};
jeffhao174651d2012-04-19 15:27:22 -07001227 modrm_opcodes = f7_opcodes;
1228 has_modrm = true;
1229 reg_is_opcode = true;
1230 store = true;
1231 immediate_bytes = ((instr[1] & 0x38) == 0) ? 1 : 0;
1232 break;
Ian Rogers706a10e2012-03-23 17:00:55 -07001233 case 0xFF:
Vladimir Kostyukove443a802014-06-30 15:44:12 +07001234 {
Ian Rogers677c12f2014-11-07 16:58:38 -08001235 static const char* ff_opcodes[] = {
1236 "inc", "dec", "call", "call",
1237 "jmp", "jmp", "push", "unknown-ff"};
Vladimir Kostyukove443a802014-06-30 15:44:12 +07001238 modrm_opcodes = ff_opcodes;
1239 has_modrm = true;
1240 reg_is_opcode = true;
1241 load = true;
1242 const uint8_t opcode_digit = (instr[1] >> 3) & 7;
1243 // 'call', 'jmp' and 'push' are target specific instructions
1244 if (opcode_digit == 2 || opcode_digit == 4 || opcode_digit == 6) {
1245 target_specific = true;
1246 }
1247 }
Ian Rogers706a10e2012-03-23 17:00:55 -07001248 break;
1249 default:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001250 opcode_tmp = StringPrintf("unknown opcode '%02X'", *instr);
1251 opcode1 = opcode_tmp.c_str();
Ian Rogers706a10e2012-03-23 17:00:55 -07001252 break;
1253 }
1254 std::ostringstream args;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +07001255 // We force the REX prefix to be available for 64-bit target
1256 // in order to dump addr (base/index) registers correctly.
1257 uint8_t rex64 = supports_rex_ ? (rex | 0x40) : rex;
Vladimir Kostyukove443a802014-06-30 15:44:12 +07001258 // REX.W should be forced for 64-target and target-specific instructions (i.e., push or pop).
1259 uint8_t rex_w = (supports_rex_ && target_specific) ? (rex | 0x48) : rex;
Ian Rogers706a10e2012-03-23 17:00:55 -07001260 if (reg_in_opcode) {
1261 DCHECK(!has_modrm);
Vladimir Kostyukov79bb1842014-07-01 18:28:43 +07001262 DumpOpcodeReg(args, rex_w, *instr & 0x7, byte_operand, prefix[2]);
Ian Rogers706a10e2012-03-23 17:00:55 -07001263 }
1264 instr++;
Elliott Hughes92301d92012-04-10 15:57:52 -07001265 uint32_t address_bits = 0;
Ian Rogers706a10e2012-03-23 17:00:55 -07001266 if (has_modrm) {
1267 uint8_t modrm = *instr;
1268 instr++;
1269 uint8_t mod = modrm >> 6;
1270 uint8_t reg_or_opcode = (modrm >> 3) & 7;
1271 uint8_t rm = modrm & 7;
Andreas Gampee5eb7062014-12-12 18:44:19 -08001272 std::string address = DumpAddress(mod, rm, rex64, rex_w, no_ops, byte_operand,
1273 byte_second_operand, prefix, load, src_reg_file, dst_reg_file,
1274 &instr, &address_bits);
Ian Rogers706a10e2012-03-23 17:00:55 -07001275
Ian Rogers677c12f2014-11-07 16:58:38 -08001276 if (reg_is_opcode && modrm_opcodes != nullptr) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001277 opcode3 = modrm_opcodes[reg_or_opcode];
Ian Rogers706a10e2012-03-23 17:00:55 -07001278 }
Mark Mendella33720c2014-06-18 21:02:29 -04001279
1280 // Add opcode suffixes to indicate size.
1281 if (byte_operand) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001282 opcode4 = "b";
Mark Mendella33720c2014-06-18 21:02:29 -04001283 } else if ((rex & REX_W) != 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001284 opcode4 = "q";
Mark Mendella33720c2014-06-18 21:02:29 -04001285 } else if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001286 opcode4 = "w";
Mark Mendella33720c2014-06-18 21:02:29 -04001287 }
1288
Ian Rogers706a10e2012-03-23 17:00:55 -07001289 if (load) {
1290 if (!reg_is_opcode) {
Ian Rogersbf989802012-04-16 16:07:49 -07001291 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2], dst_reg_file);
Ian Rogers706a10e2012-03-23 17:00:55 -07001292 args << ", ";
1293 }
1294 DumpSegmentOverride(args, prefix[1]);
Andreas Gampee5eb7062014-12-12 18:44:19 -08001295 args << address;
Ian Rogers706a10e2012-03-23 17:00:55 -07001296 } else {
1297 DCHECK(store);
1298 DumpSegmentOverride(args, prefix[1]);
Andreas Gampee5eb7062014-12-12 18:44:19 -08001299 args << address;
Ian Rogers706a10e2012-03-23 17:00:55 -07001300 if (!reg_is_opcode) {
1301 args << ", ";
Ian Rogersbf989802012-04-16 16:07:49 -07001302 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2], src_reg_file);
Ian Rogers706a10e2012-03-23 17:00:55 -07001303 }
1304 }
1305 }
1306 if (ax) {
jeffhaofdffdf82012-07-11 16:08:43 -07001307 // If this opcode implicitly uses ax, ax is always the first arg.
Ian Rogersbf989802012-04-16 16:07:49 -07001308 DumpReg(args, rex, 0 /* EAX */, byte_operand, prefix[2], GPR);
Ian Rogers706a10e2012-03-23 17:00:55 -07001309 }
jeffhaoe2962482012-06-28 11:29:57 -07001310 if (cx) {
1311 args << ", ";
1312 DumpReg(args, rex, 1 /* ECX */, true, prefix[2], GPR);
1313 }
Ian Rogers706a10e2012-03-23 17:00:55 -07001314 if (immediate_bytes > 0) {
jeffhaoe2962482012-06-28 11:29:57 -07001315 if (has_modrm || reg_in_opcode || ax || cx) {
Ian Rogers706a10e2012-03-23 17:00:55 -07001316 args << ", ";
1317 }
1318 if (immediate_bytes == 1) {
1319 args << StringPrintf("%d", *reinterpret_cast<const int8_t*>(instr));
1320 instr++;
Yixin Shou5192cbb2014-07-01 13:48:17 -04001321 } else if (immediate_bytes == 4) {
Mark Mendell67d18be2014-05-30 15:05:09 -04001322 if (prefix[2] == 0x66) { // Operand size override from 32-bit to 16-bit.
1323 args << StringPrintf("%d", *reinterpret_cast<const int16_t*>(instr));
1324 instr += 2;
1325 } else {
1326 args << StringPrintf("%d", *reinterpret_cast<const int32_t*>(instr));
1327 instr += 4;
1328 }
Yixin Shou5192cbb2014-07-01 13:48:17 -04001329 } else {
1330 CHECK_EQ(immediate_bytes, 8u);
1331 args << StringPrintf("%" PRId64, *reinterpret_cast<const int64_t*>(instr));
1332 instr += 8;
Ian Rogers706a10e2012-03-23 17:00:55 -07001333 }
1334 } else if (branch_bytes > 0) {
1335 DCHECK(!has_modrm);
1336 int32_t displacement;
1337 if (branch_bytes == 1) {
1338 displacement = *reinterpret_cast<const int8_t*>(instr);
1339 instr++;
1340 } else {
1341 CHECK_EQ(branch_bytes, 4u);
1342 displacement = *reinterpret_cast<const int32_t*>(instr);
1343 instr += 4;
1344 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001345 args << StringPrintf("%+d (", displacement)
1346 << FormatInstructionPointer(instr + displacement)
1347 << ")";
Ian Rogers706a10e2012-03-23 17:00:55 -07001348 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001349 if (prefix[1] == kFs && !supports_rex_) {
Elliott Hughes92301d92012-04-10 15:57:52 -07001350 args << " ; ";
Ian Rogersdd7624d2014-03-14 17:43:00 -07001351 Thread::DumpThreadOffset<4>(args, address_bits);
1352 }
1353 if (prefix[1] == kGs && supports_rex_) {
1354 args << " ; ";
1355 Thread::DumpThreadOffset<8>(args, address_bits);
Elliott Hughes92301d92012-04-10 15:57:52 -07001356 }
Andreas Gampee5eb7062014-12-12 18:44:19 -08001357 const char* prefix_str;
Ian Rogers5e588b32013-02-21 15:05:09 -08001358 switch (prefix[0]) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001359 case 0xF0: prefix_str = "lock "; break;
1360 case 0xF2: prefix_str = "repne "; break;
1361 case 0xF3: prefix_str = "repe "; break;
1362 case 0: prefix_str = ""; break;
Ian Rogers2c4257b2014-10-24 14:20:06 -07001363 default: LOG(FATAL) << "Unreachable"; UNREACHABLE();
Ian Rogers5e588b32013-02-21 15:05:09 -08001364 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001365 os << FormatInstructionPointer(begin_instr)
Andreas Gampee5eb7062014-12-12 18:44:19 -08001366 << StringPrintf(": %22s \t%-7s%s%s%s%s%s ", DumpCodeHex(begin_instr, instr).c_str(),
1367 prefix_str, opcode0, opcode1, opcode2, opcode3, opcode4)
Ian Rogers5e588b32013-02-21 15:05:09 -08001368 << args.str() << '\n';
Ian Rogers706a10e2012-03-23 17:00:55 -07001369 return instr - begin_instr;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001370} // NOLINT(readability/fn_size)
Ian Rogers706a10e2012-03-23 17:00:55 -07001371
1372} // namespace x86
1373} // namespace art