blob: bac6b062d44058e37e4726c6aa1fa2748d200d5e [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
Serdjuk, Nikolay Y44148222015-09-14 18:05:33 +0600246size_t DisassemblerX86::DumpNops(std::ostream& os, const uint8_t* instr) {
247static constexpr uint8_t kNops[][10] = {
248 { },
249 { 0x90 },
250 { 0x66, 0x90 },
251 { 0x0f, 0x1f, 0x00 },
252 { 0x0f, 0x1f, 0x40, 0x00 },
253 { 0x0f, 0x1f, 0x44, 0x00, 0x00 },
254 { 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 },
255 { 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00 },
256 { 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 },
257 { 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 },
258 { 0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 }
259 };
260
261 for (size_t i = 1; i < arraysize(kNops); ++i) {
262 if (memcmp(instr, kNops[i], i) == 0) {
263 os << FormatInstructionPointer(instr)
264 << StringPrintf(": %22s \t nop \n", DumpCodeHex(instr, instr + i).c_str());
265 return i;
266 }
267 }
268
269 return 0;
270}
271
Ian Rogers706a10e2012-03-23 17:00:55 -0700272size_t DisassemblerX86::DumpInstruction(std::ostream& os, const uint8_t* instr) {
Serdjuk, Nikolay Y44148222015-09-14 18:05:33 +0600273 size_t nop_size = DumpNops(os, instr);
274 if (nop_size != 0u) {
275 return nop_size;
276 }
277
Ian Rogers706a10e2012-03-23 17:00:55 -0700278 const uint8_t* begin_instr = instr;
279 bool have_prefixes = true;
280 uint8_t prefix[4] = {0, 0, 0, 0};
Ian Rogers706a10e2012-03-23 17:00:55 -0700281 do {
282 switch (*instr) {
Ian Rogers7caad772012-03-30 01:07:54 -0700283 // Group 1 - lock and repeat prefixes:
Ian Rogers706a10e2012-03-23 17:00:55 -0700284 case 0xF0:
285 case 0xF2:
286 case 0xF3:
287 prefix[0] = *instr;
288 break;
289 // Group 2 - segment override prefixes:
Elliott Hughes92301d92012-04-10 15:57:52 -0700290 case kCs:
291 case kSs:
292 case kDs:
293 case kEs:
294 case kFs:
295 case kGs:
Ian Rogers706a10e2012-03-23 17:00:55 -0700296 prefix[1] = *instr;
297 break;
298 // Group 3 - operand size override:
299 case 0x66:
300 prefix[2] = *instr;
301 break;
302 // Group 4 - address size override:
303 case 0x67:
304 prefix[3] = *instr;
305 break;
306 default:
307 have_prefixes = false;
308 break;
309 }
310 if (have_prefixes) {
311 instr++;
312 }
313 } while (have_prefixes);
Ian Rogers38e12032014-03-14 14:06:14 -0700314 uint8_t rex = (supports_rex_ && (*instr >= 0x40) && (*instr <= 0x4F)) ? *instr : 0;
Vladimir Kostyukove8861b32014-04-18 17:06:15 +0700315 if (rex != 0) {
316 instr++;
317 }
Ian Rogers677c12f2014-11-07 16:58:38 -0800318 const char** modrm_opcodes = nullptr;
Ian Rogers706a10e2012-03-23 17:00:55 -0700319 bool has_modrm = false;
320 bool reg_is_opcode = false;
321 size_t immediate_bytes = 0;
322 size_t branch_bytes = 0;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800323 std::string opcode_tmp; // Storage to keep StringPrintf result alive.
324 const char* opcode0 = ""; // Prefix part.
325 const char* opcode1 = ""; // Main opcode.
326 const char* opcode2 = ""; // Sub-opcode. E.g., jump type.
327 const char* opcode3 = ""; // Mod-rm part.
328 const char* opcode4 = ""; // Suffix part.
Ian Rogers706a10e2012-03-23 17:00:55 -0700329 bool store = false; // stores to memory (ie rm is on the left)
330 bool load = false; // loads from memory (ie rm is on the right)
Serguei Katkov94f3eb02014-06-24 13:23:17 +0700331 bool byte_operand = false; // true when the opcode is dealing with byte operands
Ian Rogers677c12f2014-11-07 16:58:38 -0800332 // true when the source operand is a byte register but the target register isn't
333 // (ie movsxb/movzxb).
334 bool byte_second_operand = false;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700335 bool target_specific = false; // register name depends on target (64 vs 32 bits).
Ian Rogers706a10e2012-03-23 17:00:55 -0700336 bool ax = false; // implicit use of ax
jeffhaoe2962482012-06-28 11:29:57 -0700337 bool cx = false; // implicit use of cx
Ian Rogers706a10e2012-03-23 17:00:55 -0700338 bool reg_in_opcode = false; // low 3-bits of opcode encode register parameter
jeffhao703f2cd2012-07-13 17:25:52 -0700339 bool no_ops = false;
Ian Rogersbf989802012-04-16 16:07:49 -0700340 RegFile src_reg_file = GPR;
341 RegFile dst_reg_file = GPR;
Ian Rogers706a10e2012-03-23 17:00:55 -0700342 switch (*instr) {
343#define DISASSEMBLER_ENTRY(opname, \
344 rm8_r8, rm32_r32, \
345 r8_rm8, r32_rm32, \
346 ax8_i8, ax32_i32) \
Andreas Gampee5eb7062014-12-12 18:44:19 -0800347 case rm8_r8: opcode1 = #opname; store = true; has_modrm = true; byte_operand = true; break; \
348 case rm32_r32: opcode1 = #opname; store = true; has_modrm = true; break; \
349 case r8_rm8: opcode1 = #opname; load = true; has_modrm = true; byte_operand = true; break; \
350 case r32_rm32: opcode1 = #opname; load = true; has_modrm = true; break; \
351 case ax8_i8: opcode1 = #opname; ax = true; immediate_bytes = 1; byte_operand = true; break; \
352 case ax32_i32: opcode1 = #opname; ax = true; immediate_bytes = 4; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700353
354DISASSEMBLER_ENTRY(add,
355 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */,
356 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */,
357 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */)
358DISASSEMBLER_ENTRY(or,
359 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */,
360 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */,
361 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */)
362DISASSEMBLER_ENTRY(adc,
363 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */,
364 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */,
365 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */)
366DISASSEMBLER_ENTRY(sbb,
367 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */,
368 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */,
369 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */)
370DISASSEMBLER_ENTRY(and,
371 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */,
372 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */,
373 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */)
374DISASSEMBLER_ENTRY(sub,
375 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */,
376 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */,
377 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */)
378DISASSEMBLER_ENTRY(xor,
379 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */,
380 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */,
381 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */)
382DISASSEMBLER_ENTRY(cmp,
383 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */,
384 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */,
385 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */)
386
387#undef DISASSEMBLER_ENTRY
388 case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800389 opcode1 = "push";
Ian Rogers706a10e2012-03-23 17:00:55 -0700390 reg_in_opcode = true;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700391 target_specific = true;
Ian Rogers706a10e2012-03-23 17:00:55 -0700392 break;
393 case 0x58: case 0x59: case 0x5A: case 0x5B: case 0x5C: case 0x5D: case 0x5E: case 0x5F:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800394 opcode1 = "pop";
Ian Rogers706a10e2012-03-23 17:00:55 -0700395 reg_in_opcode = true;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700396 target_specific = true;
Ian Rogers706a10e2012-03-23 17:00:55 -0700397 break;
Mark Mendell33ecf8d2014-06-06 15:19:45 -0400398 case 0x63:
Vladimir Kostyukovec95f722014-07-23 12:10:07 +0700399 if ((rex & REX_W) != 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800400 opcode1 = "movsxd";
Mark Mendell33ecf8d2014-06-06 15:19:45 -0400401 has_modrm = true;
402 load = true;
403 } else {
404 // In 32-bit mode (!supports_rex_) this is ARPL, with no REX prefix the functionality is the
405 // same as 'mov' but the use of the instruction is discouraged.
Andreas Gampee5eb7062014-12-12 18:44:19 -0800406 opcode_tmp = StringPrintf("unknown opcode '%02X'", *instr);
407 opcode1 = opcode_tmp.c_str();
Mark Mendell33ecf8d2014-06-06 15:19:45 -0400408 }
409 break;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800410 case 0x68: opcode1 = "push"; immediate_bytes = 4; break;
411 case 0x69: opcode1 = "imul"; load = true; has_modrm = true; immediate_bytes = 4; break;
412 case 0x6A: opcode1 = "push"; immediate_bytes = 1; break;
413 case 0x6B: opcode1 = "imul"; load = true; has_modrm = true; immediate_bytes = 1; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700414 case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77:
415 case 0x78: case 0x79: case 0x7A: case 0x7B: case 0x7C: case 0x7D: case 0x7E: case 0x7F:
416 static const char* condition_codes[] =
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700417 {"o", "no", "b/nae/c", "nb/ae/nc", "z/eq", "nz/ne", "be/na", "nbe/a",
418 "s", "ns", "p/pe", "np/po", "l/nge", "nl/ge", "le/ng", "nle/g"
Ian Rogers706a10e2012-03-23 17:00:55 -0700419 };
Andreas Gampee5eb7062014-12-12 18:44:19 -0800420 opcode1 = "j";
421 opcode2 = condition_codes[*instr & 0xF];
Ian Rogers706a10e2012-03-23 17:00:55 -0700422 branch_bytes = 1;
423 break;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800424 case 0x86: case 0x87:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800425 opcode1 = "xchg";
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800426 store = true;
427 has_modrm = true;
428 byte_operand = (*instr == 0x86);
429 break;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800430 case 0x88: opcode1 = "mov"; store = true; has_modrm = true; byte_operand = true; break;
431 case 0x89: opcode1 = "mov"; store = true; has_modrm = true; break;
432 case 0x8A: opcode1 = "mov"; load = true; has_modrm = true; byte_operand = true; break;
433 case 0x8B: opcode1 = "mov"; load = true; has_modrm = true; break;
Serdjuk, Nikolay Y44148222015-09-14 18:05:33 +0600434 case 0x9D: opcode1 = "popf"; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700435
436 case 0x0F: // 2 byte extended opcode
437 instr++;
438 switch (*instr) {
Ian Rogers7caad772012-03-30 01:07:54 -0700439 case 0x10: case 0x11:
440 if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800441 opcode1 = "movsd";
jeffhaofdffdf82012-07-11 16:08:43 -0700442 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700443 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800444 opcode1 = "movss";
jeffhaofdffdf82012-07-11 16:08:43 -0700445 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700446 } else if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800447 opcode1 = "movupd";
jeffhaofdffdf82012-07-11 16:08:43 -0700448 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700449 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800450 opcode1 = "movups";
Ian Rogers7caad772012-03-30 01:07:54 -0700451 }
452 has_modrm = true;
Ian Rogersbf989802012-04-16 16:07:49 -0700453 src_reg_file = dst_reg_file = SSE;
Ian Rogers7caad772012-03-30 01:07:54 -0700454 load = *instr == 0x10;
455 store = !load;
456 break;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800457 case 0x12: case 0x13:
458 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800459 opcode1 = "movlpd";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800460 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
461 } else if (prefix[0] == 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800462 opcode1 = "movlps";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800463 }
464 has_modrm = true;
465 src_reg_file = dst_reg_file = SSE;
466 load = *instr == 0x12;
467 store = !load;
468 break;
469 case 0x16: case 0x17:
470 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800471 opcode1 = "movhpd";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800472 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
473 } else if (prefix[0] == 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800474 opcode1 = "movhps";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800475 }
476 has_modrm = true;
477 src_reg_file = dst_reg_file = SSE;
478 load = *instr == 0x16;
479 store = !load;
480 break;
481 case 0x28: case 0x29:
482 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800483 opcode1 = "movapd";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800484 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
485 } else if (prefix[0] == 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800486 opcode1 = "movaps";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800487 }
488 has_modrm = true;
489 src_reg_file = dst_reg_file = SSE;
490 load = *instr == 0x28;
491 store = !load;
492 break;
jeffhaofdffdf82012-07-11 16:08:43 -0700493 case 0x2A:
494 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800495 opcode1 = "cvtpi2pd";
jeffhaofdffdf82012-07-11 16:08:43 -0700496 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
497 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800498 opcode1 = "cvtsi2sd";
jeffhaofdffdf82012-07-11 16:08:43 -0700499 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
500 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800501 opcode1 = "cvtsi2ss";
jeffhaofdffdf82012-07-11 16:08:43 -0700502 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
503 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800504 opcode1 = "cvtpi2ps";
jeffhaofdffdf82012-07-11 16:08:43 -0700505 }
506 load = true;
507 has_modrm = true;
508 dst_reg_file = SSE;
509 break;
510 case 0x2C:
511 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800512 opcode1 = "cvttpd2pi";
jeffhaofdffdf82012-07-11 16:08:43 -0700513 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
514 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800515 opcode1 = "cvttsd2si";
jeffhaofdffdf82012-07-11 16:08:43 -0700516 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
517 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800518 opcode1 = "cvttss2si";
jeffhaofdffdf82012-07-11 16:08:43 -0700519 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
520 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800521 opcode1 = "cvttps2pi";
jeffhaofdffdf82012-07-11 16:08:43 -0700522 }
523 load = true;
524 has_modrm = true;
525 src_reg_file = SSE;
526 break;
527 case 0x2D:
528 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800529 opcode1 = "cvtpd2pi";
jeffhaofdffdf82012-07-11 16:08:43 -0700530 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
531 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800532 opcode1 = "cvtsd2si";
jeffhaofdffdf82012-07-11 16:08:43 -0700533 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
534 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800535 opcode1 = "cvtss2si";
jeffhaofdffdf82012-07-11 16:08:43 -0700536 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
537 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800538 opcode1 = "cvtps2pi";
jeffhaofdffdf82012-07-11 16:08:43 -0700539 }
540 load = true;
541 has_modrm = true;
542 src_reg_file = SSE;
543 break;
544 case 0x2E:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800545 opcode0 = "u";
Ian Rogersfc787ec2014-10-09 21:56:44 -0700546 FALLTHROUGH_INTENDED;
jeffhaofdffdf82012-07-11 16:08:43 -0700547 case 0x2F:
548 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800549 opcode1 = "comisd";
jeffhaofdffdf82012-07-11 16:08:43 -0700550 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
551 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800552 opcode1 = "comiss";
jeffhaofdffdf82012-07-11 16:08:43 -0700553 }
554 has_modrm = true;
555 load = true;
556 src_reg_file = dst_reg_file = SSE;
557 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700558 case 0x38: // 3 byte extended opcode
Mark Mendellfe945782014-05-22 09:52:36 -0400559 instr++;
560 if (prefix[2] == 0x66) {
561 switch (*instr) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700562 case 0x01:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800563 opcode1 = "phaddw";
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700564 prefix[2] = 0;
565 has_modrm = true;
566 load = true;
567 src_reg_file = dst_reg_file = SSE;
568 break;
569 case 0x02:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800570 opcode1 = "phaddd";
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700571 prefix[2] = 0;
572 has_modrm = true;
573 load = true;
574 src_reg_file = dst_reg_file = SSE;
575 break;
Mark Mendellfe945782014-05-22 09:52:36 -0400576 case 0x40:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800577 opcode1 = "pmulld";
Mark Mendellfe945782014-05-22 09:52:36 -0400578 prefix[2] = 0;
579 has_modrm = true;
580 load = true;
581 src_reg_file = dst_reg_file = SSE;
582 break;
583 default:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800584 opcode_tmp = StringPrintf("unknown opcode '0F 38 %02X'", *instr);
585 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -0400586 }
587 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800588 opcode_tmp = StringPrintf("unknown opcode '0F 38 %02X'", *instr);
589 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -0400590 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700591 break;
592 case 0x3A: // 3 byte extended opcode
Mark Mendellfe945782014-05-22 09:52:36 -0400593 instr++;
594 if (prefix[2] == 0x66) {
595 switch (*instr) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400596 case 0x0A:
597 opcode1 = "roundss";
598 prefix[2] = 0;
599 has_modrm = true;
Aart Bik33dd9092016-08-01 15:55:36 -0700600 load = true;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400601 src_reg_file = SSE;
602 dst_reg_file = SSE;
603 immediate_bytes = 1;
604 break;
605 case 0x0B:
606 opcode1 = "roundsd";
607 prefix[2] = 0;
608 has_modrm = true;
Aart Bik33dd9092016-08-01 15:55:36 -0700609 load = true;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400610 src_reg_file = SSE;
611 dst_reg_file = SSE;
612 immediate_bytes = 1;
613 break;
Mark Mendellfe945782014-05-22 09:52:36 -0400614 case 0x14:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800615 opcode1 = "pextrb";
Mark Mendellfe945782014-05-22 09:52:36 -0400616 prefix[2] = 0;
617 has_modrm = true;
618 store = true;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700619 src_reg_file = SSE;
Mark Mendellfe945782014-05-22 09:52:36 -0400620 immediate_bytes = 1;
621 break;
nikolay serdjuke0705f52015-04-27 17:52:57 +0600622 case 0x15:
623 opcode1 = "pextrw";
624 prefix[2] = 0;
625 has_modrm = true;
626 store = true;
627 src_reg_file = SSE;
628 immediate_bytes = 1;
629 break;
Mark Mendellfe945782014-05-22 09:52:36 -0400630 case 0x16:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800631 opcode1 = "pextrd";
Mark Mendellfe945782014-05-22 09:52:36 -0400632 prefix[2] = 0;
633 has_modrm = true;
634 store = true;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700635 src_reg_file = SSE;
Mark Mendellfe945782014-05-22 09:52:36 -0400636 immediate_bytes = 1;
637 break;
638 default:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800639 opcode_tmp = StringPrintf("unknown opcode '0F 3A %02X'", *instr);
640 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -0400641 }
642 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800643 opcode_tmp = StringPrintf("unknown opcode '0F 3A %02X'", *instr);
644 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -0400645 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700646 break;
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800647 case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47:
648 case 0x48: case 0x49: case 0x4A: case 0x4B: case 0x4C: case 0x4D: case 0x4E: case 0x4F:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800649 opcode1 = "cmov";
650 opcode2 = condition_codes[*instr & 0xF];
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800651 has_modrm = true;
652 load = true;
653 break;
Ian Rogersbf989802012-04-16 16:07:49 -0700654 case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
655 case 0x58: case 0x59: case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
656 switch (*instr) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800657 case 0x50: opcode1 = "movmsk"; break;
658 case 0x51: opcode1 = "sqrt"; break;
659 case 0x52: opcode1 = "rsqrt"; break;
660 case 0x53: opcode1 = "rcp"; break;
661 case 0x54: opcode1 = "and"; break;
662 case 0x55: opcode1 = "andn"; break;
663 case 0x56: opcode1 = "or"; break;
664 case 0x57: opcode1 = "xor"; break;
665 case 0x58: opcode1 = "add"; break;
666 case 0x59: opcode1 = "mul"; break;
667 case 0x5C: opcode1 = "sub"; break;
668 case 0x5D: opcode1 = "min"; break;
669 case 0x5E: opcode1 = "div"; break;
670 case 0x5F: opcode1 = "max"; break;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700671 default: LOG(FATAL) << "Unreachable"; UNREACHABLE();
Ian Rogersbf989802012-04-16 16:07:49 -0700672 }
673 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800674 opcode2 = "pd";
jeffhaofdffdf82012-07-11 16:08:43 -0700675 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700676 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800677 opcode2 = "sd";
jeffhaofdffdf82012-07-11 16:08:43 -0700678 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700679 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800680 opcode2 = "ss";
jeffhaofdffdf82012-07-11 16:08:43 -0700681 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700682 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800683 opcode2 = "ps";
Ian Rogersbf989802012-04-16 16:07:49 -0700684 }
685 load = true;
686 has_modrm = true;
687 src_reg_file = dst_reg_file = SSE;
688 break;
689 }
690 case 0x5A:
691 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800692 opcode1 = "cvtpd2ps";
jeffhaofdffdf82012-07-11 16:08:43 -0700693 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700694 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800695 opcode1 = "cvtsd2ss";
jeffhaofdffdf82012-07-11 16:08:43 -0700696 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700697 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800698 opcode1 = "cvtss2sd";
jeffhaofdffdf82012-07-11 16:08:43 -0700699 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700700 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800701 opcode1 = "cvtps2pd";
Ian Rogersbf989802012-04-16 16:07:49 -0700702 }
703 load = true;
704 has_modrm = true;
705 src_reg_file = dst_reg_file = SSE;
706 break;
707 case 0x5B:
708 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800709 opcode1 = "cvtps2dq";
jeffhaofdffdf82012-07-11 16:08:43 -0700710 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700711 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800712 opcode1 = "bad opcode F2 0F 5B";
Ian Rogersbf989802012-04-16 16:07:49 -0700713 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800714 opcode1 = "cvttps2dq";
jeffhaofdffdf82012-07-11 16:08:43 -0700715 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700716 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800717 opcode1 = "cvtdq2ps";
Ian Rogersbf989802012-04-16 16:07:49 -0700718 }
719 load = true;
720 has_modrm = true;
721 src_reg_file = dst_reg_file = SSE;
722 break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700723 case 0x60: case 0x61: case 0x62: case 0x6C:
Razvan A Lupusorud3266bc2014-01-24 12:55:31 -0800724 if (prefix[2] == 0x66) {
725 src_reg_file = dst_reg_file = SSE;
726 prefix[2] = 0; // Clear prefix now. It has served its purpose as part of the opcode.
727 } else {
728 src_reg_file = dst_reg_file = MMX;
729 }
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700730 switch (*instr) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800731 case 0x60: opcode1 = "punpcklbw"; break;
732 case 0x61: opcode1 = "punpcklwd"; break;
733 case 0x62: opcode1 = "punpckldq"; break;
734 case 0x6c: opcode1 = "punpcklqdq"; break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700735 }
Razvan A Lupusorud3266bc2014-01-24 12:55:31 -0800736 load = true;
737 has_modrm = true;
738 break;
Ian Rogersbf989802012-04-16 16:07:49 -0700739 case 0x6E:
740 if (prefix[2] == 0x66) {
741 dst_reg_file = SSE;
jeffhaofdffdf82012-07-11 16:08:43 -0700742 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700743 } else {
744 dst_reg_file = MMX;
Ian Rogersbf989802012-04-16 16:07:49 -0700745 }
Andreas Gampee5eb7062014-12-12 18:44:19 -0800746 opcode1 = "movd";
Ian Rogersbf989802012-04-16 16:07:49 -0700747 load = true;
748 has_modrm = true;
749 break;
750 case 0x6F:
751 if (prefix[2] == 0x66) {
Mark Mendellfe945782014-05-22 09:52:36 -0400752 src_reg_file = dst_reg_file = SSE;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800753 opcode1 = "movdqa";
jeffhaofdffdf82012-07-11 16:08:43 -0700754 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700755 } else if (prefix[0] == 0xF3) {
Mark Mendellfe945782014-05-22 09:52:36 -0400756 src_reg_file = dst_reg_file = SSE;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800757 opcode1 = "movdqu";
jeffhaofdffdf82012-07-11 16:08:43 -0700758 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700759 } else {
760 dst_reg_file = MMX;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800761 opcode1 = "movq";
Ian Rogersbf989802012-04-16 16:07:49 -0700762 }
763 load = true;
764 has_modrm = true;
765 break;
Mark Mendellfe945782014-05-22 09:52:36 -0400766 case 0x70:
767 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800768 opcode1 = "pshufd";
Mark Mendellfe945782014-05-22 09:52:36 -0400769 prefix[2] = 0;
770 has_modrm = true;
771 store = true;
772 src_reg_file = dst_reg_file = SSE;
773 immediate_bytes = 1;
774 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800775 opcode1 = "pshuflw";
Mark Mendellfe945782014-05-22 09:52:36 -0400776 prefix[0] = 0;
777 has_modrm = true;
778 store = true;
779 src_reg_file = dst_reg_file = SSE;
780 immediate_bytes = 1;
781 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800782 opcode_tmp = StringPrintf("unknown opcode '0F %02X'", *instr);
783 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -0400784 }
785 break;
jeffhaofdffdf82012-07-11 16:08:43 -0700786 case 0x71:
787 if (prefix[2] == 0x66) {
788 dst_reg_file = SSE;
789 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
790 } else {
791 dst_reg_file = MMX;
792 }
Ian Rogers677c12f2014-11-07 16:58:38 -0800793 static const char* x71_opcodes[] = {
794 "unknown-71", "unknown-71", "psrlw", "unknown-71",
795 "psraw", "unknown-71", "psllw", "unknown-71"};
jeffhaofdffdf82012-07-11 16:08:43 -0700796 modrm_opcodes = x71_opcodes;
797 reg_is_opcode = true;
798 has_modrm = true;
799 store = true;
800 immediate_bytes = 1;
801 break;
802 case 0x72:
803 if (prefix[2] == 0x66) {
804 dst_reg_file = SSE;
805 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
806 } else {
807 dst_reg_file = MMX;
808 }
Ian Rogers677c12f2014-11-07 16:58:38 -0800809 static const char* x72_opcodes[] = {
810 "unknown-72", "unknown-72", "psrld", "unknown-72",
811 "psrad", "unknown-72", "pslld", "unknown-72"};
jeffhaofdffdf82012-07-11 16:08:43 -0700812 modrm_opcodes = x72_opcodes;
813 reg_is_opcode = true;
814 has_modrm = true;
815 store = true;
816 immediate_bytes = 1;
817 break;
818 case 0x73:
819 if (prefix[2] == 0x66) {
820 dst_reg_file = SSE;
821 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
822 } else {
823 dst_reg_file = MMX;
824 }
Ian Rogers677c12f2014-11-07 16:58:38 -0800825 static const char* x73_opcodes[] = {
826 "unknown-73", "unknown-73", "psrlq", "psrldq",
827 "unknown-73", "unknown-73", "psllq", "unknown-73"};
jeffhaofdffdf82012-07-11 16:08:43 -0700828 modrm_opcodes = x73_opcodes;
829 reg_is_opcode = true;
830 has_modrm = true;
831 store = true;
832 immediate_bytes = 1;
833 break;
Olivier Comefb0fecf2014-06-20 11:46:16 +0200834 case 0x7C:
835 if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800836 opcode1 = "haddps";
Olivier Comefb0fecf2014-06-20 11:46:16 +0200837 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
838 } else if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800839 opcode1 = "haddpd";
Olivier Comefb0fecf2014-06-20 11:46:16 +0200840 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
841 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800842 opcode_tmp = StringPrintf("unknown opcode '0F %02X'", *instr);
843 opcode1 = opcode_tmp.c_str();
Olivier Comefb0fecf2014-06-20 11:46:16 +0200844 break;
845 }
846 src_reg_file = dst_reg_file = SSE;
847 has_modrm = true;
848 load = true;
849 break;
jeffhaofdffdf82012-07-11 16:08:43 -0700850 case 0x7E:
851 if (prefix[2] == 0x66) {
852 src_reg_file = SSE;
853 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
854 } else {
855 src_reg_file = MMX;
856 }
Andreas Gampee5eb7062014-12-12 18:44:19 -0800857 opcode1 = "movd";
jeffhaofdffdf82012-07-11 16:08:43 -0700858 has_modrm = true;
859 store = true;
860 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700861 case 0x80: case 0x81: case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87:
862 case 0x88: case 0x89: case 0x8A: case 0x8B: case 0x8C: case 0x8D: case 0x8E: case 0x8F:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800863 opcode1 = "j";
864 opcode2 = condition_codes[*instr & 0xF];
Ian Rogers706a10e2012-03-23 17:00:55 -0700865 branch_bytes = 4;
866 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700867 case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97:
868 case 0x98: case 0x99: case 0x9A: case 0x9B: case 0x9C: case 0x9D: case 0x9E: case 0x9F:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800869 opcode1 = "set";
870 opcode2 = condition_codes[*instr & 0xF];
Ian Rogers677c12f2014-11-07 16:58:38 -0800871 modrm_opcodes = nullptr;
Ian Rogers7caad772012-03-30 01:07:54 -0700872 reg_is_opcode = true;
873 has_modrm = true;
874 store = true;
875 break;
Mark Mendell4708dcd2014-01-22 09:05:18 -0800876 case 0xA4:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800877 opcode1 = "shld";
Mark Mendell4708dcd2014-01-22 09:05:18 -0800878 has_modrm = true;
879 load = true;
880 immediate_bytes = 1;
881 break;
Yixin Shouf40f8902014-08-14 14:10:32 -0400882 case 0xA5:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800883 opcode1 = "shld";
Yixin Shouf40f8902014-08-14 14:10:32 -0400884 has_modrm = true;
885 load = true;
886 cx = true;
887 break;
Mark Mendell4708dcd2014-01-22 09:05:18 -0800888 case 0xAC:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800889 opcode1 = "shrd";
Mark Mendell4708dcd2014-01-22 09:05:18 -0800890 has_modrm = true;
891 load = true;
892 immediate_bytes = 1;
893 break;
Yixin Shouf40f8902014-08-14 14:10:32 -0400894 case 0xAD:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800895 opcode1 = "shrd";
Yixin Shouf40f8902014-08-14 14:10:32 -0400896 has_modrm = true;
897 load = true;
898 cx = true;
899 break;
jeffhao703f2cd2012-07-13 17:25:52 -0700900 case 0xAE:
901 if (prefix[0] == 0xF3) {
Ian Rogers5e588b32013-02-21 15:05:09 -0800902 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers677c12f2014-11-07 16:58:38 -0800903 static const char* xAE_opcodes[] = {
904 "rdfsbase", "rdgsbase", "wrfsbase", "wrgsbase",
905 "unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE"};
jeffhao703f2cd2012-07-13 17:25:52 -0700906 modrm_opcodes = xAE_opcodes;
907 reg_is_opcode = true;
908 has_modrm = true;
909 uint8_t reg_or_opcode = (instr[1] >> 3) & 7;
910 switch (reg_or_opcode) {
911 case 0:
912 prefix[1] = kFs;
913 load = true;
914 break;
915 case 1:
916 prefix[1] = kGs;
917 load = true;
918 break;
919 case 2:
920 prefix[1] = kFs;
921 store = true;
922 break;
923 case 3:
924 prefix[1] = kGs;
925 store = true;
926 break;
927 default:
928 load = true;
929 break;
930 }
931 } else {
Ian Rogers677c12f2014-11-07 16:58:38 -0800932 static const char* xAE_opcodes[] = {
933 "unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE",
934 "unknown-AE", "lfence", "mfence", "sfence"};
jeffhao703f2cd2012-07-13 17:25:52 -0700935 modrm_opcodes = xAE_opcodes;
936 reg_is_opcode = true;
937 has_modrm = true;
938 load = true;
939 no_ops = true;
940 }
941 break;
Ian Rogers677c12f2014-11-07 16:58:38 -0800942 case 0xAF:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800943 opcode1 = "imul";
Ian Rogers677c12f2014-11-07 16:58:38 -0800944 has_modrm = true;
945 load = true;
946 break;
947 case 0xB1:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800948 opcode1 = "cmpxchg";
Ian Rogers677c12f2014-11-07 16:58:38 -0800949 has_modrm = true;
950 store = true;
951 break;
952 case 0xB6:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800953 opcode1 = "movzxb";
Ian Rogers677c12f2014-11-07 16:58:38 -0800954 has_modrm = true;
955 load = true;
956 byte_second_operand = true;
957 break;
958 case 0xB7:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800959 opcode1 = "movzxw";
Ian Rogers677c12f2014-11-07 16:58:38 -0800960 has_modrm = true;
961 load = true;
962 break;
Mark Mendellbcee0922015-09-15 21:45:01 -0400963 case 0xBC:
964 opcode1 = "bsf";
965 has_modrm = true;
966 load = true;
967 break;
Mark Mendell8ae3ffb2015-08-12 21:16:41 -0400968 case 0xBD:
969 opcode1 = "bsr";
970 has_modrm = true;
971 load = true;
972 break;
Aart Bik3f67e692016-01-15 14:35:12 -0800973 case 0xB8:
974 opcode1 = "popcnt";
975 has_modrm = true;
976 load = true;
977 break;
Ian Rogers677c12f2014-11-07 16:58:38 -0800978 case 0xBE:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800979 opcode1 = "movsxb";
Ian Rogers677c12f2014-11-07 16:58:38 -0800980 has_modrm = true;
981 load = true;
982 byte_second_operand = true;
983 rex |= (rex == 0 ? 0 : REX_W);
984 break;
985 case 0xBF:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800986 opcode1 = "movsxw";
Ian Rogers677c12f2014-11-07 16:58:38 -0800987 has_modrm = true;
988 load = true;
989 break;
990 case 0xC3:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800991 opcode1 = "movnti";
Ian Rogers677c12f2014-11-07 16:58:38 -0800992 store = true;
993 has_modrm = true;
994 break;
Mark Mendellfe945782014-05-22 09:52:36 -0400995 case 0xC5:
996 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800997 opcode1 = "pextrw";
Mark Mendellfe945782014-05-22 09:52:36 -0400998 prefix[2] = 0;
999 has_modrm = true;
nikolay serdjukbd4e6a82015-03-27 16:32:27 +06001000 load = true;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001001 src_reg_file = SSE;
Mark Mendellfe945782014-05-22 09:52:36 -04001002 immediate_bytes = 1;
1003 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001004 opcode_tmp = StringPrintf("unknown opcode '0F %02X'", *instr);
1005 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -04001006 }
1007 break;
Olivier Comefb0fecf2014-06-20 11:46:16 +02001008 case 0xC6:
1009 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001010 opcode1 = "shufpd";
Olivier Comefb0fecf2014-06-20 11:46:16 +02001011 prefix[2] = 0;
1012 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001013 opcode1 = "shufps";
Olivier Comefb0fecf2014-06-20 11:46:16 +02001014 }
1015 has_modrm = true;
1016 store = true;
1017 src_reg_file = dst_reg_file = SSE;
1018 immediate_bytes = 1;
1019 break;
Vladimir Marko70b797d2013-12-03 15:25:24 +00001020 case 0xC7:
Ian Rogers677c12f2014-11-07 16:58:38 -08001021 static const char* x0FxC7_opcodes[] = {
1022 "unknown-0f-c7", "cmpxchg8b", "unknown-0f-c7", "unknown-0f-c7",
1023 "unknown-0f-c7", "unknown-0f-c7", "unknown-0f-c7", "unknown-0f-c7"};
Vladimir Marko70b797d2013-12-03 15:25:24 +00001024 modrm_opcodes = x0FxC7_opcodes;
1025 has_modrm = true;
1026 reg_is_opcode = true;
1027 store = true;
1028 break;
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001029 case 0xC8: case 0xC9: case 0xCA: case 0xCB: case 0xCC: case 0xCD: case 0xCE: case 0xCF:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001030 opcode1 = "bswap";
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001031 reg_in_opcode = true;
1032 break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001033 case 0xD4:
1034 if (prefix[2] == 0x66) {
1035 src_reg_file = dst_reg_file = SSE;
1036 prefix[2] = 0;
1037 } else {
1038 src_reg_file = dst_reg_file = MMX;
1039 }
Andreas Gampee5eb7062014-12-12 18:44:19 -08001040 opcode1 = "paddq";
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001041 prefix[2] = 0;
1042 has_modrm = true;
1043 load = true;
1044 break;
Mark Mendellfe945782014-05-22 09:52:36 -04001045 case 0xDB:
1046 if (prefix[2] == 0x66) {
1047 src_reg_file = dst_reg_file = SSE;
1048 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
1049 } else {
1050 src_reg_file = dst_reg_file = MMX;
1051 }
Andreas Gampee5eb7062014-12-12 18:44:19 -08001052 opcode1 = "pand";
Mark Mendellfe945782014-05-22 09:52:36 -04001053 prefix[2] = 0;
1054 has_modrm = true;
1055 load = true;
1056 break;
1057 case 0xD5:
1058 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001059 opcode1 = "pmullw";
Mark Mendellfe945782014-05-22 09:52:36 -04001060 prefix[2] = 0;
1061 has_modrm = true;
1062 load = true;
1063 src_reg_file = dst_reg_file = SSE;
1064 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001065 opcode_tmp = StringPrintf("unknown opcode '0F %02X'", *instr);
1066 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -04001067 }
1068 break;
1069 case 0xEB:
1070 if (prefix[2] == 0x66) {
1071 src_reg_file = dst_reg_file = SSE;
1072 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
1073 } else {
1074 src_reg_file = dst_reg_file = MMX;
1075 }
Andreas Gampee5eb7062014-12-12 18:44:19 -08001076 opcode1 = "por";
Mark Mendellfe945782014-05-22 09:52:36 -04001077 prefix[2] = 0;
1078 has_modrm = true;
1079 load = true;
1080 break;
1081 case 0xEF:
1082 if (prefix[2] == 0x66) {
1083 src_reg_file = dst_reg_file = SSE;
1084 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
1085 } else {
1086 src_reg_file = dst_reg_file = MMX;
1087 }
Andreas Gampee5eb7062014-12-12 18:44:19 -08001088 opcode1 = "pxor";
Mark Mendellfe945782014-05-22 09:52:36 -04001089 prefix[2] = 0;
1090 has_modrm = true;
1091 load = true;
1092 break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001093 case 0xF4:
1094 case 0xF6:
Mark Mendellfe945782014-05-22 09:52:36 -04001095 case 0xF8:
Mark Mendellfe945782014-05-22 09:52:36 -04001096 case 0xF9:
Mark Mendellfe945782014-05-22 09:52:36 -04001097 case 0xFA:
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001098 case 0xFB:
Mark Mendellfe945782014-05-22 09:52:36 -04001099 case 0xFC:
Mark Mendellfe945782014-05-22 09:52:36 -04001100 case 0xFD:
Mark Mendellfe945782014-05-22 09:52:36 -04001101 case 0xFE:
1102 if (prefix[2] == 0x66) {
1103 src_reg_file = dst_reg_file = SSE;
1104 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
1105 } else {
1106 src_reg_file = dst_reg_file = MMX;
1107 }
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001108 switch (*instr) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001109 case 0xF4: opcode1 = "pmuludq"; break;
1110 case 0xF6: opcode1 = "psadbw"; break;
1111 case 0xF8: opcode1 = "psubb"; break;
1112 case 0xF9: opcode1 = "psubw"; break;
1113 case 0xFA: opcode1 = "psubd"; break;
1114 case 0xFB: opcode1 = "psubq"; break;
1115 case 0xFC: opcode1 = "paddb"; break;
1116 case 0xFD: opcode1 = "paddw"; break;
1117 case 0xFE: opcode1 = "paddd"; break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001118 }
Mark Mendellfe945782014-05-22 09:52:36 -04001119 prefix[2] = 0;
1120 has_modrm = true;
1121 load = true;
1122 break;
Ian Rogers706a10e2012-03-23 17:00:55 -07001123 default:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001124 opcode_tmp = StringPrintf("unknown opcode '0F %02X'", *instr);
1125 opcode1 = opcode_tmp.c_str();
Ian Rogers706a10e2012-03-23 17:00:55 -07001126 break;
1127 }
1128 break;
1129 case 0x80: case 0x81: case 0x82: case 0x83:
1130 static const char* x80_opcodes[] = {"add", "or", "adc", "sbb", "and", "sub", "xor", "cmp"};
1131 modrm_opcodes = x80_opcodes;
1132 has_modrm = true;
1133 reg_is_opcode = true;
1134 store = true;
1135 byte_operand = (*instr & 1) == 0;
1136 immediate_bytes = *instr == 0x81 ? 4 : 1;
1137 break;
jeffhao703f2cd2012-07-13 17:25:52 -07001138 case 0x84: case 0x85:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001139 opcode1 = "test";
jeffhao703f2cd2012-07-13 17:25:52 -07001140 has_modrm = true;
1141 load = true;
1142 byte_operand = (*instr & 1) == 0;
1143 break;
Ian Rogers7caad772012-03-30 01:07:54 -07001144 case 0x8D:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001145 opcode1 = "lea";
Ian Rogers7caad772012-03-30 01:07:54 -07001146 has_modrm = true;
1147 load = true;
1148 break;
jeffhao703f2cd2012-07-13 17:25:52 -07001149 case 0x8F:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001150 opcode1 = "pop";
jeffhao703f2cd2012-07-13 17:25:52 -07001151 has_modrm = true;
1152 reg_is_opcode = true;
1153 store = true;
1154 break;
Mark Mendell2bf31e62014-01-23 12:13:40 -08001155 case 0x99:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001156 opcode1 = "cdq";
Mark Mendell2bf31e62014-01-23 12:13:40 -08001157 break;
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001158 case 0x9B:
1159 if (instr[1] == 0xDF && instr[2] == 0xE0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001160 opcode1 = "fstsw\tax";
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001161 instr += 2;
1162 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001163 opcode_tmp = StringPrintf("unknown opcode '%02X'", *instr);
1164 opcode1 = opcode_tmp.c_str();
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001165 }
1166 break;
Mark Mendellb9c4bbe2015-07-01 14:26:52 -04001167 case 0xA5:
1168 opcode1 = (prefix[2] == 0x66 ? "movsw" : "movsl");
1169 break;
agicsaki124b3922015-07-30 13:40:13 -07001170 case 0xA7:
1171 opcode1 = (prefix[2] == 0x66 ? "cmpsw" : "cmpsl");
1172 break;
Mark Mendell4028a6c2014-02-19 20:06:20 -08001173 case 0xAF:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001174 opcode1 = (prefix[2] == 0x66 ? "scasw" : "scasl");
Mark Mendell4028a6c2014-02-19 20:06:20 -08001175 break;
Ian Rogers706a10e2012-03-23 17:00:55 -07001176 case 0xB0: case 0xB1: case 0xB2: case 0xB3: case 0xB4: case 0xB5: case 0xB6: case 0xB7:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001177 opcode1 = "mov";
Ian Rogers706a10e2012-03-23 17:00:55 -07001178 immediate_bytes = 1;
Mark Mendella33720c2014-06-18 21:02:29 -04001179 byte_operand = true;
Ian Rogers706a10e2012-03-23 17:00:55 -07001180 reg_in_opcode = true;
Vladimir Kostyukov79bb1842014-07-01 18:28:43 +07001181 byte_operand = true;
Ian Rogers706a10e2012-03-23 17:00:55 -07001182 break;
1183 case 0xB8: case 0xB9: case 0xBA: case 0xBB: case 0xBC: case 0xBD: case 0xBE: case 0xBF:
Vladimir Kostyukovec95f722014-07-23 12:10:07 +07001184 if ((rex & REX_W) != 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001185 opcode1 = "movabsq";
Yixin Shou5192cbb2014-07-01 13:48:17 -04001186 immediate_bytes = 8;
1187 reg_in_opcode = true;
1188 break;
1189 }
Andreas Gampee5eb7062014-12-12 18:44:19 -08001190 opcode1 = "mov";
Ian Rogers706a10e2012-03-23 17:00:55 -07001191 immediate_bytes = 4;
1192 reg_in_opcode = true;
1193 break;
Ian Rogers7caad772012-03-30 01:07:54 -07001194 case 0xC0: case 0xC1:
jeffhaoe2962482012-06-28 11:29:57 -07001195 case 0xD0: case 0xD1: case 0xD2: case 0xD3:
Ian Rogers7caad772012-03-30 01:07:54 -07001196 static const char* shift_opcodes[] =
1197 {"rol", "ror", "rcl", "rcr", "shl", "shr", "unknown-shift", "sar"};
1198 modrm_opcodes = shift_opcodes;
1199 has_modrm = true;
1200 reg_is_opcode = true;
1201 store = true;
Elliott Hughes16b5c292012-04-16 20:37:16 -07001202 immediate_bytes = ((*instr & 0xf0) == 0xc0) ? 1 : 0;
jeffhaoe2962482012-06-28 11:29:57 -07001203 cx = (*instr == 0xD2) || (*instr == 0xD3);
1204 byte_operand = (*instr == 0xC0);
Ian Rogers7caad772012-03-30 01:07:54 -07001205 break;
Andreas Gampee5eb7062014-12-12 18:44:19 -08001206 case 0xC3: opcode1 = "ret"; break;
Mark Mendella33720c2014-06-18 21:02:29 -04001207 case 0xC6:
Ian Rogers677c12f2014-11-07 16:58:38 -08001208 static const char* c6_opcodes[] = {"mov", "unknown-c6", "unknown-c6",
1209 "unknown-c6", "unknown-c6", "unknown-c6",
1210 "unknown-c6", "unknown-c6"};
Mark Mendella33720c2014-06-18 21:02:29 -04001211 modrm_opcodes = c6_opcodes;
1212 store = true;
1213 immediate_bytes = 1;
1214 has_modrm = true;
1215 reg_is_opcode = true;
1216 byte_operand = true;
1217 break;
Elliott Hughes0589ca92012-04-09 18:26:20 -07001218 case 0xC7:
Ian Rogers677c12f2014-11-07 16:58:38 -08001219 static const char* c7_opcodes[] = {"mov", "unknown-c7", "unknown-c7",
1220 "unknown-c7", "unknown-c7", "unknown-c7",
1221 "unknown-c7", "unknown-c7"};
Elliott Hughes0589ca92012-04-09 18:26:20 -07001222 modrm_opcodes = c7_opcodes;
1223 store = true;
1224 immediate_bytes = 4;
1225 has_modrm = true;
1226 reg_is_opcode = true;
1227 break;
Andreas Gampee5eb7062014-12-12 18:44:19 -08001228 case 0xCC: opcode1 = "int 3"; break;
Mark Mendelld19b55a2013-12-12 09:55:34 -08001229 case 0xD9:
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001230 if (instr[1] == 0xF8) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001231 opcode1 = "fprem";
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001232 instr++;
1233 } else {
1234 static const char* d9_opcodes[] = {"flds", "unknown-d9", "fsts", "fstps", "fldenv", "fldcw",
1235 "fnstenv", "fnstcw"};
1236 modrm_opcodes = d9_opcodes;
1237 store = true;
1238 has_modrm = true;
1239 reg_is_opcode = true;
1240 }
1241 break;
1242 case 0xDA:
1243 if (instr[1] == 0xE9) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001244 opcode1 = "fucompp";
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001245 instr++;
1246 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001247 opcode_tmp = StringPrintf("unknown opcode '%02X'", *instr);
1248 opcode1 = opcode_tmp.c_str();
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001249 }
Mark Mendelld19b55a2013-12-12 09:55:34 -08001250 break;
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -08001251 case 0xDB:
Ian Rogers677c12f2014-11-07 16:58:38 -08001252 static const char* db_opcodes[] = {"fildl", "unknown-db", "unknown-db",
1253 "unknown-db", "unknown-db", "unknown-db",
1254 "unknown-db", "unknown-db"};
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -08001255 modrm_opcodes = db_opcodes;
1256 load = true;
1257 has_modrm = true;
1258 reg_is_opcode = true;
1259 break;
Mark Mendelld19b55a2013-12-12 09:55:34 -08001260 case 0xDD:
Ian Rogers677c12f2014-11-07 16:58:38 -08001261 static const char* dd_opcodes[] = {"fldl", "fisttp", "fstl",
1262 "fstpl", "frstor", "unknown-dd",
1263 "fnsave", "fnstsw"};
Mark Mendelld19b55a2013-12-12 09:55:34 -08001264 modrm_opcodes = dd_opcodes;
1265 store = true;
1266 has_modrm = true;
1267 reg_is_opcode = true;
1268 break;
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -08001269 case 0xDF:
Ian Rogers677c12f2014-11-07 16:58:38 -08001270 static const char* df_opcodes[] = {"fild", "unknown-df", "unknown-df",
1271 "unknown-df", "unknown-df", "fildll",
1272 "unknown-df", "unknown-df"};
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -08001273 modrm_opcodes = df_opcodes;
1274 load = true;
1275 has_modrm = true;
1276 reg_is_opcode = true;
1277 break;
Andreas Gampee5eb7062014-12-12 18:44:19 -08001278 case 0xE3: opcode1 = "jecxz"; branch_bytes = 1; break;
1279 case 0xE8: opcode1 = "call"; branch_bytes = 4; break;
1280 case 0xE9: opcode1 = "jmp"; branch_bytes = 4; break;
1281 case 0xEB: opcode1 = "jmp"; branch_bytes = 1; break;
1282 case 0xF5: opcode1 = "cmc"; break;
jeffhao174651d2012-04-19 15:27:22 -07001283 case 0xF6: case 0xF7:
Ian Rogers677c12f2014-11-07 16:58:38 -08001284 static const char* f7_opcodes[] = {
1285 "test", "unknown-f7", "not", "neg", "mul edx:eax, eax *",
1286 "imul edx:eax, eax *", "div edx:eax, edx:eax /",
1287 "idiv edx:eax, edx:eax /"};
jeffhao174651d2012-04-19 15:27:22 -07001288 modrm_opcodes = f7_opcodes;
1289 has_modrm = true;
1290 reg_is_opcode = true;
1291 store = true;
1292 immediate_bytes = ((instr[1] & 0x38) == 0) ? 1 : 0;
1293 break;
Ian Rogers706a10e2012-03-23 17:00:55 -07001294 case 0xFF:
Vladimir Kostyukove443a802014-06-30 15:44:12 +07001295 {
Ian Rogers677c12f2014-11-07 16:58:38 -08001296 static const char* ff_opcodes[] = {
1297 "inc", "dec", "call", "call",
1298 "jmp", "jmp", "push", "unknown-ff"};
Vladimir Kostyukove443a802014-06-30 15:44:12 +07001299 modrm_opcodes = ff_opcodes;
1300 has_modrm = true;
1301 reg_is_opcode = true;
1302 load = true;
1303 const uint8_t opcode_digit = (instr[1] >> 3) & 7;
1304 // 'call', 'jmp' and 'push' are target specific instructions
1305 if (opcode_digit == 2 || opcode_digit == 4 || opcode_digit == 6) {
1306 target_specific = true;
1307 }
1308 }
Ian Rogers706a10e2012-03-23 17:00:55 -07001309 break;
1310 default:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001311 opcode_tmp = StringPrintf("unknown opcode '%02X'", *instr);
1312 opcode1 = opcode_tmp.c_str();
Ian Rogers706a10e2012-03-23 17:00:55 -07001313 break;
1314 }
1315 std::ostringstream args;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +07001316 // We force the REX prefix to be available for 64-bit target
1317 // in order to dump addr (base/index) registers correctly.
1318 uint8_t rex64 = supports_rex_ ? (rex | 0x40) : rex;
Vladimir Kostyukove443a802014-06-30 15:44:12 +07001319 // REX.W should be forced for 64-target and target-specific instructions (i.e., push or pop).
1320 uint8_t rex_w = (supports_rex_ && target_specific) ? (rex | 0x48) : rex;
Ian Rogers706a10e2012-03-23 17:00:55 -07001321 if (reg_in_opcode) {
1322 DCHECK(!has_modrm);
Vladimir Kostyukov79bb1842014-07-01 18:28:43 +07001323 DumpOpcodeReg(args, rex_w, *instr & 0x7, byte_operand, prefix[2]);
Ian Rogers706a10e2012-03-23 17:00:55 -07001324 }
1325 instr++;
Elliott Hughes92301d92012-04-10 15:57:52 -07001326 uint32_t address_bits = 0;
Ian Rogers706a10e2012-03-23 17:00:55 -07001327 if (has_modrm) {
1328 uint8_t modrm = *instr;
1329 instr++;
1330 uint8_t mod = modrm >> 6;
1331 uint8_t reg_or_opcode = (modrm >> 3) & 7;
1332 uint8_t rm = modrm & 7;
Andreas Gampee5eb7062014-12-12 18:44:19 -08001333 std::string address = DumpAddress(mod, rm, rex64, rex_w, no_ops, byte_operand,
1334 byte_second_operand, prefix, load, src_reg_file, dst_reg_file,
1335 &instr, &address_bits);
Ian Rogers706a10e2012-03-23 17:00:55 -07001336
Ian Rogers677c12f2014-11-07 16:58:38 -08001337 if (reg_is_opcode && modrm_opcodes != nullptr) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001338 opcode3 = modrm_opcodes[reg_or_opcode];
Ian Rogers706a10e2012-03-23 17:00:55 -07001339 }
Mark Mendella33720c2014-06-18 21:02:29 -04001340
1341 // Add opcode suffixes to indicate size.
1342 if (byte_operand) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001343 opcode4 = "b";
Mark Mendella33720c2014-06-18 21:02:29 -04001344 } else if ((rex & REX_W) != 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001345 opcode4 = "q";
Mark Mendella33720c2014-06-18 21:02:29 -04001346 } else if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001347 opcode4 = "w";
Mark Mendella33720c2014-06-18 21:02:29 -04001348 }
1349
Ian Rogers706a10e2012-03-23 17:00:55 -07001350 if (load) {
1351 if (!reg_is_opcode) {
Ian Rogersbf989802012-04-16 16:07:49 -07001352 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2], dst_reg_file);
Ian Rogers706a10e2012-03-23 17:00:55 -07001353 args << ", ";
1354 }
1355 DumpSegmentOverride(args, prefix[1]);
Andreas Gampee5eb7062014-12-12 18:44:19 -08001356 args << address;
Ian Rogers706a10e2012-03-23 17:00:55 -07001357 } else {
1358 DCHECK(store);
1359 DumpSegmentOverride(args, prefix[1]);
Andreas Gampee5eb7062014-12-12 18:44:19 -08001360 args << address;
Ian Rogers706a10e2012-03-23 17:00:55 -07001361 if (!reg_is_opcode) {
1362 args << ", ";
Ian Rogersbf989802012-04-16 16:07:49 -07001363 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2], src_reg_file);
Ian Rogers706a10e2012-03-23 17:00:55 -07001364 }
1365 }
1366 }
1367 if (ax) {
jeffhaofdffdf82012-07-11 16:08:43 -07001368 // If this opcode implicitly uses ax, ax is always the first arg.
Ian Rogersbf989802012-04-16 16:07:49 -07001369 DumpReg(args, rex, 0 /* EAX */, byte_operand, prefix[2], GPR);
Ian Rogers706a10e2012-03-23 17:00:55 -07001370 }
jeffhaoe2962482012-06-28 11:29:57 -07001371 if (cx) {
1372 args << ", ";
1373 DumpReg(args, rex, 1 /* ECX */, true, prefix[2], GPR);
1374 }
Ian Rogers706a10e2012-03-23 17:00:55 -07001375 if (immediate_bytes > 0) {
jeffhaoe2962482012-06-28 11:29:57 -07001376 if (has_modrm || reg_in_opcode || ax || cx) {
Ian Rogers706a10e2012-03-23 17:00:55 -07001377 args << ", ";
1378 }
1379 if (immediate_bytes == 1) {
1380 args << StringPrintf("%d", *reinterpret_cast<const int8_t*>(instr));
1381 instr++;
Yixin Shou5192cbb2014-07-01 13:48:17 -04001382 } else if (immediate_bytes == 4) {
Mark Mendell67d18be2014-05-30 15:05:09 -04001383 if (prefix[2] == 0x66) { // Operand size override from 32-bit to 16-bit.
1384 args << StringPrintf("%d", *reinterpret_cast<const int16_t*>(instr));
1385 instr += 2;
1386 } else {
1387 args << StringPrintf("%d", *reinterpret_cast<const int32_t*>(instr));
1388 instr += 4;
1389 }
Yixin Shou5192cbb2014-07-01 13:48:17 -04001390 } else {
1391 CHECK_EQ(immediate_bytes, 8u);
1392 args << StringPrintf("%" PRId64, *reinterpret_cast<const int64_t*>(instr));
1393 instr += 8;
Ian Rogers706a10e2012-03-23 17:00:55 -07001394 }
1395 } else if (branch_bytes > 0) {
1396 DCHECK(!has_modrm);
1397 int32_t displacement;
1398 if (branch_bytes == 1) {
1399 displacement = *reinterpret_cast<const int8_t*>(instr);
1400 instr++;
1401 } else {
1402 CHECK_EQ(branch_bytes, 4u);
1403 displacement = *reinterpret_cast<const int32_t*>(instr);
1404 instr += 4;
1405 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001406 args << StringPrintf("%+d (", displacement)
1407 << FormatInstructionPointer(instr + displacement)
1408 << ")";
Ian Rogers706a10e2012-03-23 17:00:55 -07001409 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001410 if (prefix[1] == kFs && !supports_rex_) {
Elliott Hughes92301d92012-04-10 15:57:52 -07001411 args << " ; ";
Ian Rogersdd7624d2014-03-14 17:43:00 -07001412 Thread::DumpThreadOffset<4>(args, address_bits);
1413 }
1414 if (prefix[1] == kGs && supports_rex_) {
1415 args << " ; ";
1416 Thread::DumpThreadOffset<8>(args, address_bits);
Elliott Hughes92301d92012-04-10 15:57:52 -07001417 }
Andreas Gampee5eb7062014-12-12 18:44:19 -08001418 const char* prefix_str;
Ian Rogers5e588b32013-02-21 15:05:09 -08001419 switch (prefix[0]) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001420 case 0xF0: prefix_str = "lock "; break;
1421 case 0xF2: prefix_str = "repne "; break;
1422 case 0xF3: prefix_str = "repe "; break;
1423 case 0: prefix_str = ""; break;
Ian Rogers2c4257b2014-10-24 14:20:06 -07001424 default: LOG(FATAL) << "Unreachable"; UNREACHABLE();
Ian Rogers5e588b32013-02-21 15:05:09 -08001425 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001426 os << FormatInstructionPointer(begin_instr)
Andreas Gampee5eb7062014-12-12 18:44:19 -08001427 << StringPrintf(": %22s \t%-7s%s%s%s%s%s ", DumpCodeHex(begin_instr, instr).c_str(),
1428 prefix_str, opcode0, opcode1, opcode2, opcode3, opcode4)
Ian Rogers5e588b32013-02-21 15:05:09 -08001429 << args.str() << '\n';
Ian Rogers706a10e2012-03-23 17:00:55 -07001430 return instr - begin_instr;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001431} // NOLINT(readability/fn_size)
Ian Rogers706a10e2012-03-23 17:00:55 -07001432
1433} // namespace x86
1434} // namespace art