blob: 2ca84e5e5ba25db2e86645135b0f73fc10597cfe [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 Hughes0f3c5532012-03-30 14:51:51 -070026
Ian Rogers706a10e2012-03-23 17:00:55 -070027namespace art {
28namespace x86 {
29
Ian Rogersb23a7722012-10-09 16:54:26 -070030size_t DisassemblerX86::Dump(std::ostream& os, const uint8_t* begin) {
31 return DumpInstruction(os, begin);
32}
33
Ian Rogers706a10e2012-03-23 17:00:55 -070034void DisassemblerX86::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
35 size_t length = 0;
36 for (const uint8_t* cur = begin; cur < end; cur += length) {
37 length = DumpInstruction(os, cur);
38 }
39}
40
Vladimir Kostyukov122113a2014-05-30 17:56:23 +070041static const char* gReg8Names[] = {
42 "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh"
43};
44static const char* gExtReg8Names[] = {
45 "al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil",
46 "r8l", "r9l", "r10l", "r11l", "r12l", "r13l", "r14l", "r15l"
47};
48static const char* gReg16Names[] = {
49 "ax", "cx", "dx", "bx", "sp", "bp", "si", "di",
50 "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w"
51};
52static const char* gReg32Names[] = {
53 "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi",
54 "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d"
55};
Ian Rogers38e12032014-03-14 14:06:14 -070056static const char* gReg64Names[] = {
57 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
58 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
59};
Ian Rogers706a10e2012-03-23 17:00:55 -070060
Mark Mendella33720c2014-06-18 21:02:29 -040061// 64-bit opcode REX modifier.
Andreas Gampec8ccf682014-09-29 20:07:43 -070062constexpr uint8_t REX_W = 8U /* 0b1000 */;
63constexpr uint8_t REX_R = 4U /* 0b0100 */;
64constexpr uint8_t REX_X = 2U /* 0b0010 */;
65constexpr uint8_t REX_B = 1U /* 0b0001 */;
Mark Mendella33720c2014-06-18 21:02:29 -040066
Ian Rogers38e12032014-03-14 14:06:14 -070067static void DumpReg0(std::ostream& os, uint8_t rex, size_t reg,
Ian Rogers706a10e2012-03-23 17:00:55 -070068 bool byte_operand, uint8_t size_override) {
Ian Rogers38e12032014-03-14 14:06:14 -070069 DCHECK_LT(reg, (rex == 0) ? 8u : 16u);
Mark Mendella33720c2014-06-18 21:02:29 -040070 bool rex_w = (rex & REX_W) != 0;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +070071 if (byte_operand) {
72 os << ((rex == 0) ? gReg8Names[reg] : gExtReg8Names[reg]);
73 } else if (rex_w) {
74 os << gReg64Names[reg];
75 } else if (size_override == 0x66) {
76 os << gReg16Names[reg];
77 } else {
78 os << gReg32Names[reg];
Ian Rogers706a10e2012-03-23 17:00:55 -070079 }
80}
81
Mark Mendell88649c72014-06-04 21:20:00 -040082static void DumpAnyReg(std::ostream& os, uint8_t rex, size_t reg,
Vladimir Kostyukov122113a2014-05-30 17:56:23 +070083 bool byte_operand, uint8_t size_override, RegFile reg_file) {
84 if (reg_file == GPR) {
85 DumpReg0(os, rex, reg, byte_operand, size_override);
86 } else if (reg_file == SSE) {
87 os << "xmm" << reg;
88 } else {
89 os << "mm" << reg;
90 }
91}
92
Ian Rogers706a10e2012-03-23 17:00:55 -070093static void DumpReg(std::ostream& os, uint8_t rex, uint8_t reg,
Ian Rogersbf989802012-04-16 16:07:49 -070094 bool byte_operand, uint8_t size_override, RegFile reg_file) {
Mark Mendella33720c2014-06-18 21:02:29 -040095 bool rex_r = (rex & REX_R) != 0;
Ian Rogers38e12032014-03-14 14:06:14 -070096 size_t reg_num = rex_r ? (reg + 8) : reg;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +070097 DumpAnyReg(os, rex, reg_num, byte_operand, size_override, reg_file);
98}
99
100static void DumpRmReg(std::ostream& os, uint8_t rex, uint8_t reg,
101 bool byte_operand, uint8_t size_override, RegFile reg_file) {
Mark Mendella33720c2014-06-18 21:02:29 -0400102 bool rex_b = (rex & REX_B) != 0;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700103 size_t reg_num = rex_b ? (reg + 8) : reg;
104 DumpAnyReg(os, rex, reg_num, byte_operand, size_override, reg_file);
105}
106
107static void DumpAddrReg(std::ostream& os, uint8_t rex, uint8_t reg) {
108 if (rex != 0) {
109 os << gReg64Names[reg];
Ian Rogersbf989802012-04-16 16:07:49 -0700110 } else {
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700111 os << gReg32Names[reg];
Ian Rogersbf989802012-04-16 16:07:49 -0700112 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700113}
114
Ian Rogers7caad772012-03-30 01:07:54 -0700115static void DumpBaseReg(std::ostream& os, uint8_t rex, uint8_t reg) {
Mark Mendella33720c2014-06-18 21:02:29 -0400116 bool rex_b = (rex & REX_B) != 0;
Ian Rogers38e12032014-03-14 14:06:14 -0700117 size_t reg_num = rex_b ? (reg + 8) : reg;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700118 DumpAddrReg(os, rex, reg_num);
Ian Rogers706a10e2012-03-23 17:00:55 -0700119}
120
Vladimir Kostyukov79bb1842014-07-01 18:28:43 +0700121static void DumpOpcodeReg(std::ostream& os, uint8_t rex, uint8_t reg,
122 bool byte_operand, uint8_t size_override) {
Mark Mendella33720c2014-06-18 21:02:29 -0400123 bool rex_b = (rex & REX_B) != 0;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700124 size_t reg_num = rex_b ? (reg + 8) : reg;
Vladimir Kostyukov79bb1842014-07-01 18:28:43 +0700125 DumpReg0(os, rex, reg_num, byte_operand, size_override);
Ian Rogers706a10e2012-03-23 17:00:55 -0700126}
127
Elliott Hughes92301d92012-04-10 15:57:52 -0700128enum SegmentPrefix {
129 kCs = 0x2e,
130 kSs = 0x36,
131 kDs = 0x3e,
132 kEs = 0x26,
133 kFs = 0x64,
134 kGs = 0x65,
135};
136
Ian Rogers706a10e2012-03-23 17:00:55 -0700137static void DumpSegmentOverride(std::ostream& os, uint8_t segment_prefix) {
138 switch (segment_prefix) {
Elliott Hughes92301d92012-04-10 15:57:52 -0700139 case kCs: os << "cs:"; break;
140 case kSs: os << "ss:"; break;
141 case kDs: os << "ds:"; break;
142 case kEs: os << "es:"; break;
143 case kFs: os << "fs:"; break;
144 case kGs: os << "gs:"; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700145 default: break;
146 }
147}
148
Andreas Gampee5eb7062014-12-12 18:44:19 -0800149// Do not inline to avoid Clang stack frame problems. b/18733806
Andreas Gampe86830382014-12-12 21:41:29 -0800150NO_INLINE
151static std::string DumpCodeHex(const uint8_t* begin, const uint8_t* end) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800152 std::stringstream hex;
153 for (size_t i = 0; begin + i < end; ++i) {
154 hex << StringPrintf("%02X", begin[i]);
155 }
156 return hex.str();
157}
158
159std::string DisassemblerX86::DumpAddress(uint8_t mod, uint8_t rm, uint8_t rex64, uint8_t rex_w,
160 bool no_ops, bool byte_operand, bool byte_second_operand,
161 uint8_t* prefix, bool load, RegFile src_reg_file,
162 RegFile dst_reg_file, const uint8_t** instr,
163 uint32_t* address_bits) {
164 std::ostringstream address;
165 if (mod == 0 && rm == 5) {
166 if (!supports_rex_) { // Absolute address.
Nicolas Geoffray6a0b9202014-12-16 14:54:18 +0000167 *address_bits = *reinterpret_cast<const uint32_t*>(*instr);
Andreas Gampee5eb7062014-12-12 18:44:19 -0800168 address << StringPrintf("[0x%x]", *address_bits);
169 } else { // 64-bit RIP relative addressing.
170 address << StringPrintf("[RIP + 0x%x]", *reinterpret_cast<const uint32_t*>(*instr));
171 }
172 (*instr) += 4;
173 } else if (rm == 4 && mod != 3) { // SIB
174 uint8_t sib = **instr;
175 (*instr)++;
176 uint8_t scale = (sib >> 6) & 3;
177 uint8_t index = (sib >> 3) & 7;
178 uint8_t base = sib & 7;
179 address << "[";
Andreas Gampe031b00d2015-01-26 19:30:23 -0800180
181 // REX.x is bit 3 of index.
182 if ((rex64 & REX_X) != 0) {
183 index += 8;
184 }
185
186 // Mod = 0 && base = 5 (ebp): no base (ignores REX.b).
187 bool has_base = false;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800188 if (base != 5 || mod != 0) {
Andreas Gampe031b00d2015-01-26 19:30:23 -0800189 has_base = true;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800190 DumpBaseReg(address, rex64, base);
Andreas Gampe031b00d2015-01-26 19:30:23 -0800191 }
192
193 // Index = 4 (esp/rsp) is disallowed.
194 if (index != 4) {
195 if (has_base) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800196 address << " + ";
197 }
Andreas Gampe031b00d2015-01-26 19:30:23 -0800198 DumpAddrReg(address, rex64, index);
Andreas Gampee5eb7062014-12-12 18:44:19 -0800199 if (scale != 0) {
200 address << StringPrintf(" * %d", 1 << scale);
201 }
202 }
Andreas Gampe031b00d2015-01-26 19:30:23 -0800203
Andreas Gampee5eb7062014-12-12 18:44:19 -0800204 if (mod == 0) {
205 if (base == 5) {
206 if (index != 4) {
207 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(*instr));
208 } else {
209 // 64-bit low 32-bit absolute address, redundant absolute address encoding on 32-bit.
210 *address_bits = *reinterpret_cast<const uint32_t*>(*instr);
211 address << StringPrintf("%d", *address_bits);
212 }
213 (*instr) += 4;
214 }
215 } else if (mod == 1) {
216 address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(*instr));
217 (*instr)++;
218 } else if (mod == 2) {
219 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(*instr));
220 (*instr) += 4;
221 }
222 address << "]";
223 } else {
224 if (mod == 3) {
225 if (!no_ops) {
226 DumpRmReg(address, rex_w, rm, byte_operand || byte_second_operand,
227 prefix[2], load ? src_reg_file : dst_reg_file);
228 }
229 } else {
230 address << "[";
231 DumpBaseReg(address, rex64, rm);
232 if (mod == 1) {
233 address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(*instr));
234 (*instr)++;
235 } else if (mod == 2) {
236 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(*instr));
237 (*instr) += 4;
238 }
239 address << "]";
240 }
241 }
242 return address.str();
243}
244
Serdjuk, Nikolay Y44148222015-09-14 18:05:33 +0600245size_t DisassemblerX86::DumpNops(std::ostream& os, const uint8_t* instr) {
246static constexpr uint8_t kNops[][10] = {
247 { },
248 { 0x90 },
249 { 0x66, 0x90 },
250 { 0x0f, 0x1f, 0x00 },
251 { 0x0f, 0x1f, 0x40, 0x00 },
252 { 0x0f, 0x1f, 0x44, 0x00, 0x00 },
253 { 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 },
254 { 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00 },
255 { 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 },
256 { 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 },
257 { 0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 }
258 };
259
260 for (size_t i = 1; i < arraysize(kNops); ++i) {
261 if (memcmp(instr, kNops[i], i) == 0) {
262 os << FormatInstructionPointer(instr)
263 << StringPrintf(": %22s \t nop \n", DumpCodeHex(instr, instr + i).c_str());
264 return i;
265 }
266 }
267
268 return 0;
269}
270
Ian Rogers706a10e2012-03-23 17:00:55 -0700271size_t DisassemblerX86::DumpInstruction(std::ostream& os, const uint8_t* instr) {
Serdjuk, Nikolay Y44148222015-09-14 18:05:33 +0600272 size_t nop_size = DumpNops(os, instr);
273 if (nop_size != 0u) {
274 return nop_size;
275 }
276
Ian Rogers706a10e2012-03-23 17:00:55 -0700277 const uint8_t* begin_instr = instr;
278 bool have_prefixes = true;
279 uint8_t prefix[4] = {0, 0, 0, 0};
Ian Rogers706a10e2012-03-23 17:00:55 -0700280 do {
281 switch (*instr) {
Ian Rogers7caad772012-03-30 01:07:54 -0700282 // Group 1 - lock and repeat prefixes:
Ian Rogers706a10e2012-03-23 17:00:55 -0700283 case 0xF0:
284 case 0xF2:
285 case 0xF3:
286 prefix[0] = *instr;
287 break;
288 // Group 2 - segment override prefixes:
Elliott Hughes92301d92012-04-10 15:57:52 -0700289 case kCs:
290 case kSs:
291 case kDs:
292 case kEs:
293 case kFs:
294 case kGs:
Ian Rogers706a10e2012-03-23 17:00:55 -0700295 prefix[1] = *instr;
296 break;
297 // Group 3 - operand size override:
298 case 0x66:
299 prefix[2] = *instr;
300 break;
301 // Group 4 - address size override:
302 case 0x67:
303 prefix[3] = *instr;
304 break;
305 default:
306 have_prefixes = false;
307 break;
308 }
309 if (have_prefixes) {
310 instr++;
311 }
312 } while (have_prefixes);
Ian Rogers38e12032014-03-14 14:06:14 -0700313 uint8_t rex = (supports_rex_ && (*instr >= 0x40) && (*instr <= 0x4F)) ? *instr : 0;
Vladimir Kostyukove8861b32014-04-18 17:06:15 +0700314 if (rex != 0) {
315 instr++;
316 }
Ian Rogers677c12f2014-11-07 16:58:38 -0800317 const char** modrm_opcodes = nullptr;
Ian Rogers706a10e2012-03-23 17:00:55 -0700318 bool has_modrm = false;
319 bool reg_is_opcode = false;
320 size_t immediate_bytes = 0;
321 size_t branch_bytes = 0;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800322 std::string opcode_tmp; // Storage to keep StringPrintf result alive.
323 const char* opcode0 = ""; // Prefix part.
324 const char* opcode1 = ""; // Main opcode.
325 const char* opcode2 = ""; // Sub-opcode. E.g., jump type.
326 const char* opcode3 = ""; // Mod-rm part.
327 const char* opcode4 = ""; // Suffix part.
Ian Rogers706a10e2012-03-23 17:00:55 -0700328 bool store = false; // stores to memory (ie rm is on the left)
329 bool load = false; // loads from memory (ie rm is on the right)
Serguei Katkov94f3eb02014-06-24 13:23:17 +0700330 bool byte_operand = false; // true when the opcode is dealing with byte operands
Ian Rogers677c12f2014-11-07 16:58:38 -0800331 // true when the source operand is a byte register but the target register isn't
332 // (ie movsxb/movzxb).
333 bool byte_second_operand = false;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700334 bool target_specific = false; // register name depends on target (64 vs 32 bits).
Ian Rogers706a10e2012-03-23 17:00:55 -0700335 bool ax = false; // implicit use of ax
jeffhaoe2962482012-06-28 11:29:57 -0700336 bool cx = false; // implicit use of cx
Ian Rogers706a10e2012-03-23 17:00:55 -0700337 bool reg_in_opcode = false; // low 3-bits of opcode encode register parameter
jeffhao703f2cd2012-07-13 17:25:52 -0700338 bool no_ops = false;
Ian Rogersbf989802012-04-16 16:07:49 -0700339 RegFile src_reg_file = GPR;
340 RegFile dst_reg_file = GPR;
Ian Rogers706a10e2012-03-23 17:00:55 -0700341 switch (*instr) {
342#define DISASSEMBLER_ENTRY(opname, \
343 rm8_r8, rm32_r32, \
344 r8_rm8, r32_rm32, \
345 ax8_i8, ax32_i32) \
Andreas Gampee5eb7062014-12-12 18:44:19 -0800346 case rm8_r8: opcode1 = #opname; store = true; has_modrm = true; byte_operand = true; break; \
347 case rm32_r32: opcode1 = #opname; store = true; has_modrm = true; break; \
348 case r8_rm8: opcode1 = #opname; load = true; has_modrm = true; byte_operand = true; break; \
349 case r32_rm32: opcode1 = #opname; load = true; has_modrm = true; break; \
350 case ax8_i8: opcode1 = #opname; ax = true; immediate_bytes = 1; byte_operand = true; break; \
351 case ax32_i32: opcode1 = #opname; ax = true; immediate_bytes = 4; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700352
353DISASSEMBLER_ENTRY(add,
354 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */,
355 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */,
356 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */)
357DISASSEMBLER_ENTRY(or,
358 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */,
359 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */,
360 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */)
361DISASSEMBLER_ENTRY(adc,
362 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */,
363 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */,
364 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */)
365DISASSEMBLER_ENTRY(sbb,
366 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */,
367 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */,
368 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */)
369DISASSEMBLER_ENTRY(and,
370 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */,
371 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */,
372 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */)
373DISASSEMBLER_ENTRY(sub,
374 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */,
375 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */,
376 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */)
377DISASSEMBLER_ENTRY(xor,
378 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */,
379 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */,
380 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */)
381DISASSEMBLER_ENTRY(cmp,
382 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */,
383 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */,
384 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */)
385
386#undef DISASSEMBLER_ENTRY
387 case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800388 opcode1 = "push";
Ian Rogers706a10e2012-03-23 17:00:55 -0700389 reg_in_opcode = true;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700390 target_specific = true;
Ian Rogers706a10e2012-03-23 17:00:55 -0700391 break;
392 case 0x58: case 0x59: case 0x5A: case 0x5B: case 0x5C: case 0x5D: case 0x5E: case 0x5F:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800393 opcode1 = "pop";
Ian Rogers706a10e2012-03-23 17:00:55 -0700394 reg_in_opcode = true;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700395 target_specific = true;
Ian Rogers706a10e2012-03-23 17:00:55 -0700396 break;
Mark Mendell33ecf8d2014-06-06 15:19:45 -0400397 case 0x63:
Vladimir Kostyukovec95f722014-07-23 12:10:07 +0700398 if ((rex & REX_W) != 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800399 opcode1 = "movsxd";
Mark Mendell33ecf8d2014-06-06 15:19:45 -0400400 has_modrm = true;
401 load = true;
402 } else {
403 // In 32-bit mode (!supports_rex_) this is ARPL, with no REX prefix the functionality is the
404 // same as 'mov' but the use of the instruction is discouraged.
Andreas Gampee5eb7062014-12-12 18:44:19 -0800405 opcode_tmp = StringPrintf("unknown opcode '%02X'", *instr);
406 opcode1 = opcode_tmp.c_str();
Mark Mendell33ecf8d2014-06-06 15:19:45 -0400407 }
408 break;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800409 case 0x68: opcode1 = "push"; immediate_bytes = 4; break;
410 case 0x69: opcode1 = "imul"; load = true; has_modrm = true; immediate_bytes = 4; break;
411 case 0x6A: opcode1 = "push"; immediate_bytes = 1; break;
412 case 0x6B: opcode1 = "imul"; load = true; has_modrm = true; immediate_bytes = 1; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700413 case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77:
414 case 0x78: case 0x79: case 0x7A: case 0x7B: case 0x7C: case 0x7D: case 0x7E: case 0x7F:
415 static const char* condition_codes[] =
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700416 {"o", "no", "b/nae/c", "nb/ae/nc", "z/eq", "nz/ne", "be/na", "nbe/a",
417 "s", "ns", "p/pe", "np/po", "l/nge", "nl/ge", "le/ng", "nle/g"
Ian Rogers706a10e2012-03-23 17:00:55 -0700418 };
Andreas Gampee5eb7062014-12-12 18:44:19 -0800419 opcode1 = "j";
420 opcode2 = condition_codes[*instr & 0xF];
Ian Rogers706a10e2012-03-23 17:00:55 -0700421 branch_bytes = 1;
422 break;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800423 case 0x86: case 0x87:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800424 opcode1 = "xchg";
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800425 store = true;
426 has_modrm = true;
427 byte_operand = (*instr == 0x86);
428 break;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800429 case 0x88: opcode1 = "mov"; store = true; has_modrm = true; byte_operand = true; break;
430 case 0x89: opcode1 = "mov"; store = true; has_modrm = true; break;
431 case 0x8A: opcode1 = "mov"; load = true; has_modrm = true; byte_operand = true; break;
432 case 0x8B: opcode1 = "mov"; load = true; has_modrm = true; break;
Serdjuk, Nikolay Y44148222015-09-14 18:05:33 +0600433 case 0x9D: opcode1 = "popf"; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700434
435 case 0x0F: // 2 byte extended opcode
436 instr++;
437 switch (*instr) {
Ian Rogers7caad772012-03-30 01:07:54 -0700438 case 0x10: case 0x11:
439 if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800440 opcode1 = "movsd";
jeffhaofdffdf82012-07-11 16:08:43 -0700441 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700442 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800443 opcode1 = "movss";
jeffhaofdffdf82012-07-11 16:08:43 -0700444 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700445 } else if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800446 opcode1 = "movupd";
jeffhaofdffdf82012-07-11 16:08:43 -0700447 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700448 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800449 opcode1 = "movups";
Ian Rogers7caad772012-03-30 01:07:54 -0700450 }
451 has_modrm = true;
Ian Rogersbf989802012-04-16 16:07:49 -0700452 src_reg_file = dst_reg_file = SSE;
Ian Rogers7caad772012-03-30 01:07:54 -0700453 load = *instr == 0x10;
454 store = !load;
455 break;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800456 case 0x12: case 0x13:
457 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800458 opcode1 = "movlpd";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800459 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
460 } else if (prefix[0] == 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800461 opcode1 = "movlps";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800462 }
463 has_modrm = true;
464 src_reg_file = dst_reg_file = SSE;
465 load = *instr == 0x12;
466 store = !load;
467 break;
468 case 0x16: case 0x17:
469 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800470 opcode1 = "movhpd";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800471 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
472 } else if (prefix[0] == 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800473 opcode1 = "movhps";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800474 }
475 has_modrm = true;
476 src_reg_file = dst_reg_file = SSE;
477 load = *instr == 0x16;
478 store = !load;
479 break;
480 case 0x28: case 0x29:
481 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800482 opcode1 = "movapd";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800483 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
484 } else if (prefix[0] == 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800485 opcode1 = "movaps";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800486 }
487 has_modrm = true;
488 src_reg_file = dst_reg_file = SSE;
489 load = *instr == 0x28;
490 store = !load;
491 break;
jeffhaofdffdf82012-07-11 16:08:43 -0700492 case 0x2A:
493 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800494 opcode1 = "cvtpi2pd";
jeffhaofdffdf82012-07-11 16:08:43 -0700495 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
496 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800497 opcode1 = "cvtsi2sd";
jeffhaofdffdf82012-07-11 16:08:43 -0700498 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
499 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800500 opcode1 = "cvtsi2ss";
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 {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800503 opcode1 = "cvtpi2ps";
jeffhaofdffdf82012-07-11 16:08:43 -0700504 }
505 load = true;
506 has_modrm = true;
507 dst_reg_file = SSE;
508 break;
509 case 0x2C:
510 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800511 opcode1 = "cvttpd2pi";
jeffhaofdffdf82012-07-11 16:08:43 -0700512 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
513 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800514 opcode1 = "cvttsd2si";
jeffhaofdffdf82012-07-11 16:08:43 -0700515 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
516 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800517 opcode1 = "cvttss2si";
jeffhaofdffdf82012-07-11 16:08:43 -0700518 prefix[0] = 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 = "cvttps2pi";
jeffhaofdffdf82012-07-11 16:08:43 -0700521 }
522 load = true;
523 has_modrm = true;
524 src_reg_file = SSE;
525 break;
526 case 0x2D:
527 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800528 opcode1 = "cvtpd2pi";
jeffhaofdffdf82012-07-11 16:08:43 -0700529 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
530 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800531 opcode1 = "cvtsd2si";
jeffhaofdffdf82012-07-11 16:08:43 -0700532 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
533 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800534 opcode1 = "cvtss2si";
jeffhaofdffdf82012-07-11 16:08:43 -0700535 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
536 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800537 opcode1 = "cvtps2pi";
jeffhaofdffdf82012-07-11 16:08:43 -0700538 }
539 load = true;
540 has_modrm = true;
541 src_reg_file = SSE;
542 break;
543 case 0x2E:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800544 opcode0 = "u";
Ian Rogersfc787ec2014-10-09 21:56:44 -0700545 FALLTHROUGH_INTENDED;
jeffhaofdffdf82012-07-11 16:08:43 -0700546 case 0x2F:
547 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800548 opcode1 = "comisd";
jeffhaofdffdf82012-07-11 16:08:43 -0700549 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
550 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800551 opcode1 = "comiss";
jeffhaofdffdf82012-07-11 16:08:43 -0700552 }
553 has_modrm = true;
554 load = true;
555 src_reg_file = dst_reg_file = SSE;
556 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700557 case 0x38: // 3 byte extended opcode
Mark Mendellfe945782014-05-22 09:52:36 -0400558 instr++;
559 if (prefix[2] == 0x66) {
560 switch (*instr) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700561 case 0x01:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800562 opcode1 = "phaddw";
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700563 prefix[2] = 0;
564 has_modrm = true;
565 load = true;
566 src_reg_file = dst_reg_file = SSE;
567 break;
568 case 0x02:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800569 opcode1 = "phaddd";
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700570 prefix[2] = 0;
571 has_modrm = true;
572 load = true;
573 src_reg_file = dst_reg_file = SSE;
574 break;
Mark Mendellfe945782014-05-22 09:52:36 -0400575 case 0x40:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800576 opcode1 = "pmulld";
Mark Mendellfe945782014-05-22 09:52:36 -0400577 prefix[2] = 0;
578 has_modrm = true;
579 load = true;
580 src_reg_file = dst_reg_file = SSE;
581 break;
582 default:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800583 opcode_tmp = StringPrintf("unknown opcode '0F 38 %02X'", *instr);
584 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -0400585 }
586 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800587 opcode_tmp = StringPrintf("unknown opcode '0F 38 %02X'", *instr);
588 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -0400589 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700590 break;
591 case 0x3A: // 3 byte extended opcode
Mark Mendellfe945782014-05-22 09:52:36 -0400592 instr++;
593 if (prefix[2] == 0x66) {
594 switch (*instr) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400595 case 0x0A:
596 opcode1 = "roundss";
597 prefix[2] = 0;
598 has_modrm = true;
Aart Bik33dd9092016-08-01 15:55:36 -0700599 load = true;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400600 src_reg_file = SSE;
601 dst_reg_file = SSE;
602 immediate_bytes = 1;
603 break;
604 case 0x0B:
605 opcode1 = "roundsd";
606 prefix[2] = 0;
607 has_modrm = true;
Aart Bik33dd9092016-08-01 15:55:36 -0700608 load = true;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400609 src_reg_file = SSE;
610 dst_reg_file = SSE;
611 immediate_bytes = 1;
612 break;
Mark Mendellfe945782014-05-22 09:52:36 -0400613 case 0x14:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800614 opcode1 = "pextrb";
Mark Mendellfe945782014-05-22 09:52:36 -0400615 prefix[2] = 0;
616 has_modrm = true;
617 store = true;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700618 src_reg_file = SSE;
Mark Mendellfe945782014-05-22 09:52:36 -0400619 immediate_bytes = 1;
620 break;
nikolay serdjuke0705f52015-04-27 17:52:57 +0600621 case 0x15:
622 opcode1 = "pextrw";
623 prefix[2] = 0;
624 has_modrm = true;
625 store = true;
626 src_reg_file = SSE;
627 immediate_bytes = 1;
628 break;
Mark Mendellfe945782014-05-22 09:52:36 -0400629 case 0x16:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800630 opcode1 = "pextrd";
Mark Mendellfe945782014-05-22 09:52:36 -0400631 prefix[2] = 0;
632 has_modrm = true;
633 store = true;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700634 src_reg_file = SSE;
Mark Mendellfe945782014-05-22 09:52:36 -0400635 immediate_bytes = 1;
636 break;
637 default:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800638 opcode_tmp = StringPrintf("unknown opcode '0F 3A %02X'", *instr);
639 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -0400640 }
641 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800642 opcode_tmp = StringPrintf("unknown opcode '0F 3A %02X'", *instr);
643 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -0400644 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700645 break;
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800646 case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47:
647 case 0x48: case 0x49: case 0x4A: case 0x4B: case 0x4C: case 0x4D: case 0x4E: case 0x4F:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800648 opcode1 = "cmov";
649 opcode2 = condition_codes[*instr & 0xF];
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800650 has_modrm = true;
651 load = true;
652 break;
Ian Rogersbf989802012-04-16 16:07:49 -0700653 case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
654 case 0x58: case 0x59: case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
655 switch (*instr) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800656 case 0x50: opcode1 = "movmsk"; break;
657 case 0x51: opcode1 = "sqrt"; break;
658 case 0x52: opcode1 = "rsqrt"; break;
659 case 0x53: opcode1 = "rcp"; break;
660 case 0x54: opcode1 = "and"; break;
661 case 0x55: opcode1 = "andn"; break;
662 case 0x56: opcode1 = "or"; break;
663 case 0x57: opcode1 = "xor"; break;
664 case 0x58: opcode1 = "add"; break;
665 case 0x59: opcode1 = "mul"; break;
666 case 0x5C: opcode1 = "sub"; break;
667 case 0x5D: opcode1 = "min"; break;
668 case 0x5E: opcode1 = "div"; break;
669 case 0x5F: opcode1 = "max"; break;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700670 default: LOG(FATAL) << "Unreachable"; UNREACHABLE();
Ian Rogersbf989802012-04-16 16:07:49 -0700671 }
672 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800673 opcode2 = "pd";
jeffhaofdffdf82012-07-11 16:08:43 -0700674 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700675 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800676 opcode2 = "sd";
jeffhaofdffdf82012-07-11 16:08:43 -0700677 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700678 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800679 opcode2 = "ss";
jeffhaofdffdf82012-07-11 16:08:43 -0700680 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700681 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800682 opcode2 = "ps";
Ian Rogersbf989802012-04-16 16:07:49 -0700683 }
684 load = true;
685 has_modrm = true;
686 src_reg_file = dst_reg_file = SSE;
687 break;
688 }
689 case 0x5A:
690 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800691 opcode1 = "cvtpd2ps";
jeffhaofdffdf82012-07-11 16:08:43 -0700692 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700693 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800694 opcode1 = "cvtsd2ss";
jeffhaofdffdf82012-07-11 16:08:43 -0700695 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700696 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800697 opcode1 = "cvtss2sd";
jeffhaofdffdf82012-07-11 16:08:43 -0700698 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700699 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800700 opcode1 = "cvtps2pd";
Ian Rogersbf989802012-04-16 16:07:49 -0700701 }
702 load = true;
703 has_modrm = true;
704 src_reg_file = dst_reg_file = SSE;
705 break;
706 case 0x5B:
707 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800708 opcode1 = "cvtps2dq";
jeffhaofdffdf82012-07-11 16:08:43 -0700709 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700710 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800711 opcode1 = "bad opcode F2 0F 5B";
Ian Rogersbf989802012-04-16 16:07:49 -0700712 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800713 opcode1 = "cvttps2dq";
jeffhaofdffdf82012-07-11 16:08:43 -0700714 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700715 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800716 opcode1 = "cvtdq2ps";
Ian Rogersbf989802012-04-16 16:07:49 -0700717 }
718 load = true;
719 has_modrm = true;
720 src_reg_file = dst_reg_file = SSE;
721 break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700722 case 0x60: case 0x61: case 0x62: case 0x6C:
Razvan A Lupusorud3266bc2014-01-24 12:55:31 -0800723 if (prefix[2] == 0x66) {
724 src_reg_file = dst_reg_file = SSE;
725 prefix[2] = 0; // Clear prefix now. It has served its purpose as part of the opcode.
726 } else {
727 src_reg_file = dst_reg_file = MMX;
728 }
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700729 switch (*instr) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800730 case 0x60: opcode1 = "punpcklbw"; break;
731 case 0x61: opcode1 = "punpcklwd"; break;
732 case 0x62: opcode1 = "punpckldq"; break;
733 case 0x6c: opcode1 = "punpcklqdq"; break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700734 }
Razvan A Lupusorud3266bc2014-01-24 12:55:31 -0800735 load = true;
736 has_modrm = true;
737 break;
Ian Rogersbf989802012-04-16 16:07:49 -0700738 case 0x6E:
739 if (prefix[2] == 0x66) {
740 dst_reg_file = SSE;
jeffhaofdffdf82012-07-11 16:08:43 -0700741 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700742 } else {
743 dst_reg_file = MMX;
Ian Rogersbf989802012-04-16 16:07:49 -0700744 }
Andreas Gampee5eb7062014-12-12 18:44:19 -0800745 opcode1 = "movd";
Ian Rogersbf989802012-04-16 16:07:49 -0700746 load = true;
747 has_modrm = true;
748 break;
749 case 0x6F:
750 if (prefix[2] == 0x66) {
Mark Mendellfe945782014-05-22 09:52:36 -0400751 src_reg_file = dst_reg_file = SSE;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800752 opcode1 = "movdqa";
jeffhaofdffdf82012-07-11 16:08:43 -0700753 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700754 } else if (prefix[0] == 0xF3) {
Mark Mendellfe945782014-05-22 09:52:36 -0400755 src_reg_file = dst_reg_file = SSE;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800756 opcode1 = "movdqu";
jeffhaofdffdf82012-07-11 16:08:43 -0700757 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700758 } else {
759 dst_reg_file = MMX;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800760 opcode1 = "movq";
Ian Rogersbf989802012-04-16 16:07:49 -0700761 }
762 load = true;
763 has_modrm = true;
764 break;
Mark Mendellfe945782014-05-22 09:52:36 -0400765 case 0x70:
766 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800767 opcode1 = "pshufd";
Mark Mendellfe945782014-05-22 09:52:36 -0400768 prefix[2] = 0;
769 has_modrm = true;
770 store = true;
771 src_reg_file = dst_reg_file = SSE;
772 immediate_bytes = 1;
773 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800774 opcode1 = "pshuflw";
Mark Mendellfe945782014-05-22 09:52:36 -0400775 prefix[0] = 0;
776 has_modrm = true;
777 store = true;
778 src_reg_file = dst_reg_file = SSE;
779 immediate_bytes = 1;
780 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800781 opcode_tmp = StringPrintf("unknown opcode '0F %02X'", *instr);
782 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -0400783 }
784 break;
jeffhaofdffdf82012-07-11 16:08:43 -0700785 case 0x71:
786 if (prefix[2] == 0x66) {
787 dst_reg_file = SSE;
788 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
789 } else {
790 dst_reg_file = MMX;
791 }
Ian Rogers677c12f2014-11-07 16:58:38 -0800792 static const char* x71_opcodes[] = {
793 "unknown-71", "unknown-71", "psrlw", "unknown-71",
794 "psraw", "unknown-71", "psllw", "unknown-71"};
jeffhaofdffdf82012-07-11 16:08:43 -0700795 modrm_opcodes = x71_opcodes;
796 reg_is_opcode = true;
797 has_modrm = true;
798 store = true;
799 immediate_bytes = 1;
800 break;
801 case 0x72:
802 if (prefix[2] == 0x66) {
803 dst_reg_file = SSE;
804 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
805 } else {
806 dst_reg_file = MMX;
807 }
Ian Rogers677c12f2014-11-07 16:58:38 -0800808 static const char* x72_opcodes[] = {
809 "unknown-72", "unknown-72", "psrld", "unknown-72",
810 "psrad", "unknown-72", "pslld", "unknown-72"};
jeffhaofdffdf82012-07-11 16:08:43 -0700811 modrm_opcodes = x72_opcodes;
812 reg_is_opcode = true;
813 has_modrm = true;
814 store = true;
815 immediate_bytes = 1;
816 break;
817 case 0x73:
818 if (prefix[2] == 0x66) {
819 dst_reg_file = SSE;
820 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
821 } else {
822 dst_reg_file = MMX;
823 }
Ian Rogers677c12f2014-11-07 16:58:38 -0800824 static const char* x73_opcodes[] = {
825 "unknown-73", "unknown-73", "psrlq", "psrldq",
826 "unknown-73", "unknown-73", "psllq", "unknown-73"};
jeffhaofdffdf82012-07-11 16:08:43 -0700827 modrm_opcodes = x73_opcodes;
828 reg_is_opcode = true;
829 has_modrm = true;
830 store = true;
831 immediate_bytes = 1;
832 break;
Olivier Comefb0fecf2014-06-20 11:46:16 +0200833 case 0x7C:
834 if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800835 opcode1 = "haddps";
Olivier Comefb0fecf2014-06-20 11:46:16 +0200836 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
837 } else if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800838 opcode1 = "haddpd";
Olivier Comefb0fecf2014-06-20 11:46:16 +0200839 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
840 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800841 opcode_tmp = StringPrintf("unknown opcode '0F %02X'", *instr);
842 opcode1 = opcode_tmp.c_str();
Olivier Comefb0fecf2014-06-20 11:46:16 +0200843 break;
844 }
845 src_reg_file = dst_reg_file = SSE;
846 has_modrm = true;
847 load = true;
848 break;
jeffhaofdffdf82012-07-11 16:08:43 -0700849 case 0x7E:
850 if (prefix[2] == 0x66) {
851 src_reg_file = SSE;
852 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
853 } else {
854 src_reg_file = MMX;
855 }
Andreas Gampee5eb7062014-12-12 18:44:19 -0800856 opcode1 = "movd";
jeffhaofdffdf82012-07-11 16:08:43 -0700857 has_modrm = true;
858 store = true;
859 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700860 case 0x80: case 0x81: case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87:
861 case 0x88: case 0x89: case 0x8A: case 0x8B: case 0x8C: case 0x8D: case 0x8E: case 0x8F:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800862 opcode1 = "j";
863 opcode2 = condition_codes[*instr & 0xF];
Ian Rogers706a10e2012-03-23 17:00:55 -0700864 branch_bytes = 4;
865 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700866 case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97:
867 case 0x98: case 0x99: case 0x9A: case 0x9B: case 0x9C: case 0x9D: case 0x9E: case 0x9F:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800868 opcode1 = "set";
869 opcode2 = condition_codes[*instr & 0xF];
Ian Rogers677c12f2014-11-07 16:58:38 -0800870 modrm_opcodes = nullptr;
Ian Rogers7caad772012-03-30 01:07:54 -0700871 reg_is_opcode = true;
872 has_modrm = true;
873 store = true;
874 break;
Mark Mendell4708dcd2014-01-22 09:05:18 -0800875 case 0xA4:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800876 opcode1 = "shld";
Mark Mendell4708dcd2014-01-22 09:05:18 -0800877 has_modrm = true;
878 load = true;
879 immediate_bytes = 1;
880 break;
Yixin Shouf40f8902014-08-14 14:10:32 -0400881 case 0xA5:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800882 opcode1 = "shld";
Yixin Shouf40f8902014-08-14 14:10:32 -0400883 has_modrm = true;
884 load = true;
885 cx = true;
886 break;
Mark Mendell4708dcd2014-01-22 09:05:18 -0800887 case 0xAC:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800888 opcode1 = "shrd";
Mark Mendell4708dcd2014-01-22 09:05:18 -0800889 has_modrm = true;
890 load = true;
891 immediate_bytes = 1;
892 break;
Yixin Shouf40f8902014-08-14 14:10:32 -0400893 case 0xAD:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800894 opcode1 = "shrd";
Yixin Shouf40f8902014-08-14 14:10:32 -0400895 has_modrm = true;
896 load = true;
897 cx = true;
898 break;
jeffhao703f2cd2012-07-13 17:25:52 -0700899 case 0xAE:
900 if (prefix[0] == 0xF3) {
Ian Rogers5e588b32013-02-21 15:05:09 -0800901 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers677c12f2014-11-07 16:58:38 -0800902 static const char* xAE_opcodes[] = {
903 "rdfsbase", "rdgsbase", "wrfsbase", "wrgsbase",
904 "unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE"};
jeffhao703f2cd2012-07-13 17:25:52 -0700905 modrm_opcodes = xAE_opcodes;
906 reg_is_opcode = true;
907 has_modrm = true;
908 uint8_t reg_or_opcode = (instr[1] >> 3) & 7;
909 switch (reg_or_opcode) {
910 case 0:
911 prefix[1] = kFs;
912 load = true;
913 break;
914 case 1:
915 prefix[1] = kGs;
916 load = true;
917 break;
918 case 2:
919 prefix[1] = kFs;
920 store = true;
921 break;
922 case 3:
923 prefix[1] = kGs;
924 store = true;
925 break;
926 default:
927 load = true;
928 break;
929 }
930 } else {
Ian Rogers677c12f2014-11-07 16:58:38 -0800931 static const char* xAE_opcodes[] = {
932 "unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE",
933 "unknown-AE", "lfence", "mfence", "sfence"};
jeffhao703f2cd2012-07-13 17:25:52 -0700934 modrm_opcodes = xAE_opcodes;
935 reg_is_opcode = true;
936 has_modrm = true;
937 load = true;
938 no_ops = true;
939 }
940 break;
Ian Rogers677c12f2014-11-07 16:58:38 -0800941 case 0xAF:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800942 opcode1 = "imul";
Ian Rogers677c12f2014-11-07 16:58:38 -0800943 has_modrm = true;
944 load = true;
945 break;
946 case 0xB1:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800947 opcode1 = "cmpxchg";
Ian Rogers677c12f2014-11-07 16:58:38 -0800948 has_modrm = true;
949 store = true;
950 break;
951 case 0xB6:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800952 opcode1 = "movzxb";
Ian Rogers677c12f2014-11-07 16:58:38 -0800953 has_modrm = true;
954 load = true;
955 byte_second_operand = true;
956 break;
957 case 0xB7:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800958 opcode1 = "movzxw";
Ian Rogers677c12f2014-11-07 16:58:38 -0800959 has_modrm = true;
960 load = true;
961 break;
Mark Mendellbcee0922015-09-15 21:45:01 -0400962 case 0xBC:
963 opcode1 = "bsf";
964 has_modrm = true;
965 load = true;
966 break;
Mark Mendell8ae3ffb2015-08-12 21:16:41 -0400967 case 0xBD:
968 opcode1 = "bsr";
969 has_modrm = true;
970 load = true;
971 break;
Aart Bik3f67e692016-01-15 14:35:12 -0800972 case 0xB8:
973 opcode1 = "popcnt";
974 has_modrm = true;
975 load = true;
976 break;
Ian Rogers677c12f2014-11-07 16:58:38 -0800977 case 0xBE:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800978 opcode1 = "movsxb";
Ian Rogers677c12f2014-11-07 16:58:38 -0800979 has_modrm = true;
980 load = true;
981 byte_second_operand = true;
982 rex |= (rex == 0 ? 0 : REX_W);
983 break;
984 case 0xBF:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800985 opcode1 = "movsxw";
Ian Rogers677c12f2014-11-07 16:58:38 -0800986 has_modrm = true;
987 load = true;
988 break;
989 case 0xC3:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800990 opcode1 = "movnti";
Ian Rogers677c12f2014-11-07 16:58:38 -0800991 store = true;
992 has_modrm = true;
993 break;
Mark Mendellfe945782014-05-22 09:52:36 -0400994 case 0xC5:
995 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800996 opcode1 = "pextrw";
Mark Mendellfe945782014-05-22 09:52:36 -0400997 prefix[2] = 0;
998 has_modrm = true;
nikolay serdjukbd4e6a82015-03-27 16:32:27 +0600999 load = true;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001000 src_reg_file = SSE;
Mark Mendellfe945782014-05-22 09:52:36 -04001001 immediate_bytes = 1;
1002 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001003 opcode_tmp = StringPrintf("unknown opcode '0F %02X'", *instr);
1004 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -04001005 }
1006 break;
Olivier Comefb0fecf2014-06-20 11:46:16 +02001007 case 0xC6:
1008 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001009 opcode1 = "shufpd";
Olivier Comefb0fecf2014-06-20 11:46:16 +02001010 prefix[2] = 0;
1011 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001012 opcode1 = "shufps";
Olivier Comefb0fecf2014-06-20 11:46:16 +02001013 }
1014 has_modrm = true;
1015 store = true;
1016 src_reg_file = dst_reg_file = SSE;
1017 immediate_bytes = 1;
1018 break;
Vladimir Marko70b797d2013-12-03 15:25:24 +00001019 case 0xC7:
Ian Rogers677c12f2014-11-07 16:58:38 -08001020 static const char* x0FxC7_opcodes[] = {
1021 "unknown-0f-c7", "cmpxchg8b", "unknown-0f-c7", "unknown-0f-c7",
1022 "unknown-0f-c7", "unknown-0f-c7", "unknown-0f-c7", "unknown-0f-c7"};
Vladimir Marko70b797d2013-12-03 15:25:24 +00001023 modrm_opcodes = x0FxC7_opcodes;
1024 has_modrm = true;
1025 reg_is_opcode = true;
1026 store = true;
1027 break;
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001028 case 0xC8: case 0xC9: case 0xCA: case 0xCB: case 0xCC: case 0xCD: case 0xCE: case 0xCF:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001029 opcode1 = "bswap";
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001030 reg_in_opcode = true;
1031 break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001032 case 0xD4:
1033 if (prefix[2] == 0x66) {
1034 src_reg_file = dst_reg_file = SSE;
1035 prefix[2] = 0;
1036 } else {
1037 src_reg_file = dst_reg_file = MMX;
1038 }
Andreas Gampee5eb7062014-12-12 18:44:19 -08001039 opcode1 = "paddq";
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001040 prefix[2] = 0;
1041 has_modrm = true;
1042 load = true;
1043 break;
Mark Mendellfe945782014-05-22 09:52:36 -04001044 case 0xDB:
1045 if (prefix[2] == 0x66) {
1046 src_reg_file = dst_reg_file = SSE;
1047 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
1048 } else {
1049 src_reg_file = dst_reg_file = MMX;
1050 }
Andreas Gampee5eb7062014-12-12 18:44:19 -08001051 opcode1 = "pand";
Mark Mendellfe945782014-05-22 09:52:36 -04001052 prefix[2] = 0;
1053 has_modrm = true;
1054 load = true;
1055 break;
1056 case 0xD5:
1057 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001058 opcode1 = "pmullw";
Mark Mendellfe945782014-05-22 09:52:36 -04001059 prefix[2] = 0;
1060 has_modrm = true;
1061 load = true;
1062 src_reg_file = dst_reg_file = SSE;
1063 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001064 opcode_tmp = StringPrintf("unknown opcode '0F %02X'", *instr);
1065 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -04001066 }
1067 break;
1068 case 0xEB:
1069 if (prefix[2] == 0x66) {
1070 src_reg_file = dst_reg_file = SSE;
1071 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
1072 } else {
1073 src_reg_file = dst_reg_file = MMX;
1074 }
Andreas Gampee5eb7062014-12-12 18:44:19 -08001075 opcode1 = "por";
Mark Mendellfe945782014-05-22 09:52:36 -04001076 prefix[2] = 0;
1077 has_modrm = true;
1078 load = true;
1079 break;
1080 case 0xEF:
1081 if (prefix[2] == 0x66) {
1082 src_reg_file = dst_reg_file = SSE;
1083 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
1084 } else {
1085 src_reg_file = dst_reg_file = MMX;
1086 }
Andreas Gampee5eb7062014-12-12 18:44:19 -08001087 opcode1 = "pxor";
Mark Mendellfe945782014-05-22 09:52:36 -04001088 prefix[2] = 0;
1089 has_modrm = true;
1090 load = true;
1091 break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001092 case 0xF4:
1093 case 0xF6:
Mark Mendellfe945782014-05-22 09:52:36 -04001094 case 0xF8:
Mark Mendellfe945782014-05-22 09:52:36 -04001095 case 0xF9:
Mark Mendellfe945782014-05-22 09:52:36 -04001096 case 0xFA:
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001097 case 0xFB:
Mark Mendellfe945782014-05-22 09:52:36 -04001098 case 0xFC:
Mark Mendellfe945782014-05-22 09:52:36 -04001099 case 0xFD:
Mark Mendellfe945782014-05-22 09:52:36 -04001100 case 0xFE:
1101 if (prefix[2] == 0x66) {
1102 src_reg_file = dst_reg_file = SSE;
1103 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
1104 } else {
1105 src_reg_file = dst_reg_file = MMX;
1106 }
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001107 switch (*instr) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001108 case 0xF4: opcode1 = "pmuludq"; break;
1109 case 0xF6: opcode1 = "psadbw"; break;
1110 case 0xF8: opcode1 = "psubb"; break;
1111 case 0xF9: opcode1 = "psubw"; break;
1112 case 0xFA: opcode1 = "psubd"; break;
1113 case 0xFB: opcode1 = "psubq"; break;
1114 case 0xFC: opcode1 = "paddb"; break;
1115 case 0xFD: opcode1 = "paddw"; break;
1116 case 0xFE: opcode1 = "paddd"; break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001117 }
Mark Mendellfe945782014-05-22 09:52:36 -04001118 prefix[2] = 0;
1119 has_modrm = true;
1120 load = true;
1121 break;
Ian Rogers706a10e2012-03-23 17:00:55 -07001122 default:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001123 opcode_tmp = StringPrintf("unknown opcode '0F %02X'", *instr);
1124 opcode1 = opcode_tmp.c_str();
Ian Rogers706a10e2012-03-23 17:00:55 -07001125 break;
1126 }
1127 break;
1128 case 0x80: case 0x81: case 0x82: case 0x83:
1129 static const char* x80_opcodes[] = {"add", "or", "adc", "sbb", "and", "sub", "xor", "cmp"};
1130 modrm_opcodes = x80_opcodes;
1131 has_modrm = true;
1132 reg_is_opcode = true;
1133 store = true;
1134 byte_operand = (*instr & 1) == 0;
1135 immediate_bytes = *instr == 0x81 ? 4 : 1;
1136 break;
jeffhao703f2cd2012-07-13 17:25:52 -07001137 case 0x84: case 0x85:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001138 opcode1 = "test";
jeffhao703f2cd2012-07-13 17:25:52 -07001139 has_modrm = true;
1140 load = true;
1141 byte_operand = (*instr & 1) == 0;
1142 break;
Ian Rogers7caad772012-03-30 01:07:54 -07001143 case 0x8D:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001144 opcode1 = "lea";
Ian Rogers7caad772012-03-30 01:07:54 -07001145 has_modrm = true;
1146 load = true;
1147 break;
jeffhao703f2cd2012-07-13 17:25:52 -07001148 case 0x8F:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001149 opcode1 = "pop";
jeffhao703f2cd2012-07-13 17:25:52 -07001150 has_modrm = true;
1151 reg_is_opcode = true;
1152 store = true;
1153 break;
Mark Mendell2bf31e62014-01-23 12:13:40 -08001154 case 0x99:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001155 opcode1 = "cdq";
Mark Mendell2bf31e62014-01-23 12:13:40 -08001156 break;
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001157 case 0x9B:
1158 if (instr[1] == 0xDF && instr[2] == 0xE0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001159 opcode1 = "fstsw\tax";
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001160 instr += 2;
1161 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001162 opcode_tmp = StringPrintf("unknown opcode '%02X'", *instr);
1163 opcode1 = opcode_tmp.c_str();
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001164 }
1165 break;
Mark Mendellb9c4bbe2015-07-01 14:26:52 -04001166 case 0xA5:
1167 opcode1 = (prefix[2] == 0x66 ? "movsw" : "movsl");
1168 break;
agicsaki124b3922015-07-30 13:40:13 -07001169 case 0xA7:
1170 opcode1 = (prefix[2] == 0x66 ? "cmpsw" : "cmpsl");
1171 break;
Mark Mendell4028a6c2014-02-19 20:06:20 -08001172 case 0xAF:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001173 opcode1 = (prefix[2] == 0x66 ? "scasw" : "scasl");
Mark Mendell4028a6c2014-02-19 20:06:20 -08001174 break;
Ian Rogers706a10e2012-03-23 17:00:55 -07001175 case 0xB0: case 0xB1: case 0xB2: case 0xB3: case 0xB4: case 0xB5: case 0xB6: case 0xB7:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001176 opcode1 = "mov";
Ian Rogers706a10e2012-03-23 17:00:55 -07001177 immediate_bytes = 1;
Mark Mendella33720c2014-06-18 21:02:29 -04001178 byte_operand = true;
Ian Rogers706a10e2012-03-23 17:00:55 -07001179 reg_in_opcode = true;
Vladimir Kostyukov79bb1842014-07-01 18:28:43 +07001180 byte_operand = true;
Ian Rogers706a10e2012-03-23 17:00:55 -07001181 break;
1182 case 0xB8: case 0xB9: case 0xBA: case 0xBB: case 0xBC: case 0xBD: case 0xBE: case 0xBF:
Vladimir Kostyukovec95f722014-07-23 12:10:07 +07001183 if ((rex & REX_W) != 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001184 opcode1 = "movabsq";
Yixin Shou5192cbb2014-07-01 13:48:17 -04001185 immediate_bytes = 8;
1186 reg_in_opcode = true;
1187 break;
1188 }
Andreas Gampee5eb7062014-12-12 18:44:19 -08001189 opcode1 = "mov";
Ian Rogers706a10e2012-03-23 17:00:55 -07001190 immediate_bytes = 4;
1191 reg_in_opcode = true;
1192 break;
Ian Rogers7caad772012-03-30 01:07:54 -07001193 case 0xC0: case 0xC1:
jeffhaoe2962482012-06-28 11:29:57 -07001194 case 0xD0: case 0xD1: case 0xD2: case 0xD3:
Ian Rogers7caad772012-03-30 01:07:54 -07001195 static const char* shift_opcodes[] =
1196 {"rol", "ror", "rcl", "rcr", "shl", "shr", "unknown-shift", "sar"};
1197 modrm_opcodes = shift_opcodes;
1198 has_modrm = true;
1199 reg_is_opcode = true;
1200 store = true;
Elliott Hughes16b5c292012-04-16 20:37:16 -07001201 immediate_bytes = ((*instr & 0xf0) == 0xc0) ? 1 : 0;
jeffhaoe2962482012-06-28 11:29:57 -07001202 cx = (*instr == 0xD2) || (*instr == 0xD3);
1203 byte_operand = (*instr == 0xC0);
Ian Rogers7caad772012-03-30 01:07:54 -07001204 break;
Andreas Gampee5eb7062014-12-12 18:44:19 -08001205 case 0xC3: opcode1 = "ret"; break;
Mark Mendella33720c2014-06-18 21:02:29 -04001206 case 0xC6:
Ian Rogers677c12f2014-11-07 16:58:38 -08001207 static const char* c6_opcodes[] = {"mov", "unknown-c6", "unknown-c6",
1208 "unknown-c6", "unknown-c6", "unknown-c6",
1209 "unknown-c6", "unknown-c6"};
Mark Mendella33720c2014-06-18 21:02:29 -04001210 modrm_opcodes = c6_opcodes;
1211 store = true;
1212 immediate_bytes = 1;
1213 has_modrm = true;
1214 reg_is_opcode = true;
1215 byte_operand = true;
1216 break;
Elliott Hughes0589ca92012-04-09 18:26:20 -07001217 case 0xC7:
Ian Rogers677c12f2014-11-07 16:58:38 -08001218 static const char* c7_opcodes[] = {"mov", "unknown-c7", "unknown-c7",
1219 "unknown-c7", "unknown-c7", "unknown-c7",
1220 "unknown-c7", "unknown-c7"};
Elliott Hughes0589ca92012-04-09 18:26:20 -07001221 modrm_opcodes = c7_opcodes;
1222 store = true;
1223 immediate_bytes = 4;
1224 has_modrm = true;
1225 reg_is_opcode = true;
1226 break;
Andreas Gampee5eb7062014-12-12 18:44:19 -08001227 case 0xCC: opcode1 = "int 3"; break;
Mark Mendelld19b55a2013-12-12 09:55:34 -08001228 case 0xD9:
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001229 if (instr[1] == 0xF8) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001230 opcode1 = "fprem";
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001231 instr++;
1232 } else {
1233 static const char* d9_opcodes[] = {"flds", "unknown-d9", "fsts", "fstps", "fldenv", "fldcw",
1234 "fnstenv", "fnstcw"};
1235 modrm_opcodes = d9_opcodes;
1236 store = true;
1237 has_modrm = true;
1238 reg_is_opcode = true;
1239 }
1240 break;
1241 case 0xDA:
1242 if (instr[1] == 0xE9) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001243 opcode1 = "fucompp";
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001244 instr++;
1245 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001246 opcode_tmp = StringPrintf("unknown opcode '%02X'", *instr);
1247 opcode1 = opcode_tmp.c_str();
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001248 }
Mark Mendelld19b55a2013-12-12 09:55:34 -08001249 break;
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -08001250 case 0xDB:
Ian Rogers677c12f2014-11-07 16:58:38 -08001251 static const char* db_opcodes[] = {"fildl", "unknown-db", "unknown-db",
1252 "unknown-db", "unknown-db", "unknown-db",
1253 "unknown-db", "unknown-db"};
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -08001254 modrm_opcodes = db_opcodes;
1255 load = true;
1256 has_modrm = true;
1257 reg_is_opcode = true;
1258 break;
Mark Mendelld19b55a2013-12-12 09:55:34 -08001259 case 0xDD:
Ian Rogers677c12f2014-11-07 16:58:38 -08001260 static const char* dd_opcodes[] = {"fldl", "fisttp", "fstl",
1261 "fstpl", "frstor", "unknown-dd",
1262 "fnsave", "fnstsw"};
Mark Mendelld19b55a2013-12-12 09:55:34 -08001263 modrm_opcodes = dd_opcodes;
1264 store = true;
1265 has_modrm = true;
1266 reg_is_opcode = true;
1267 break;
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -08001268 case 0xDF:
Ian Rogers677c12f2014-11-07 16:58:38 -08001269 static const char* df_opcodes[] = {"fild", "unknown-df", "unknown-df",
1270 "unknown-df", "unknown-df", "fildll",
1271 "unknown-df", "unknown-df"};
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -08001272 modrm_opcodes = df_opcodes;
1273 load = true;
1274 has_modrm = true;
1275 reg_is_opcode = true;
1276 break;
Andreas Gampee5eb7062014-12-12 18:44:19 -08001277 case 0xE3: opcode1 = "jecxz"; branch_bytes = 1; break;
1278 case 0xE8: opcode1 = "call"; branch_bytes = 4; break;
1279 case 0xE9: opcode1 = "jmp"; branch_bytes = 4; break;
1280 case 0xEB: opcode1 = "jmp"; branch_bytes = 1; break;
1281 case 0xF5: opcode1 = "cmc"; break;
jeffhao174651d2012-04-19 15:27:22 -07001282 case 0xF6: case 0xF7:
Ian Rogers677c12f2014-11-07 16:58:38 -08001283 static const char* f7_opcodes[] = {
1284 "test", "unknown-f7", "not", "neg", "mul edx:eax, eax *",
1285 "imul edx:eax, eax *", "div edx:eax, edx:eax /",
1286 "idiv edx:eax, edx:eax /"};
jeffhao174651d2012-04-19 15:27:22 -07001287 modrm_opcodes = f7_opcodes;
1288 has_modrm = true;
1289 reg_is_opcode = true;
1290 store = true;
1291 immediate_bytes = ((instr[1] & 0x38) == 0) ? 1 : 0;
1292 break;
Ian Rogers706a10e2012-03-23 17:00:55 -07001293 case 0xFF:
Vladimir Kostyukove443a802014-06-30 15:44:12 +07001294 {
Ian Rogers677c12f2014-11-07 16:58:38 -08001295 static const char* ff_opcodes[] = {
1296 "inc", "dec", "call", "call",
1297 "jmp", "jmp", "push", "unknown-ff"};
Vladimir Kostyukove443a802014-06-30 15:44:12 +07001298 modrm_opcodes = ff_opcodes;
1299 has_modrm = true;
1300 reg_is_opcode = true;
1301 load = true;
1302 const uint8_t opcode_digit = (instr[1] >> 3) & 7;
1303 // 'call', 'jmp' and 'push' are target specific instructions
1304 if (opcode_digit == 2 || opcode_digit == 4 || opcode_digit == 6) {
1305 target_specific = true;
1306 }
1307 }
Ian Rogers706a10e2012-03-23 17:00:55 -07001308 break;
1309 default:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001310 opcode_tmp = StringPrintf("unknown opcode '%02X'", *instr);
1311 opcode1 = opcode_tmp.c_str();
Ian Rogers706a10e2012-03-23 17:00:55 -07001312 break;
1313 }
1314 std::ostringstream args;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +07001315 // We force the REX prefix to be available for 64-bit target
1316 // in order to dump addr (base/index) registers correctly.
1317 uint8_t rex64 = supports_rex_ ? (rex | 0x40) : rex;
Vladimir Kostyukove443a802014-06-30 15:44:12 +07001318 // REX.W should be forced for 64-target and target-specific instructions (i.e., push or pop).
1319 uint8_t rex_w = (supports_rex_ && target_specific) ? (rex | 0x48) : rex;
Ian Rogers706a10e2012-03-23 17:00:55 -07001320 if (reg_in_opcode) {
1321 DCHECK(!has_modrm);
Vladimir Kostyukov79bb1842014-07-01 18:28:43 +07001322 DumpOpcodeReg(args, rex_w, *instr & 0x7, byte_operand, prefix[2]);
Ian Rogers706a10e2012-03-23 17:00:55 -07001323 }
1324 instr++;
Elliott Hughes92301d92012-04-10 15:57:52 -07001325 uint32_t address_bits = 0;
Ian Rogers706a10e2012-03-23 17:00:55 -07001326 if (has_modrm) {
1327 uint8_t modrm = *instr;
1328 instr++;
1329 uint8_t mod = modrm >> 6;
1330 uint8_t reg_or_opcode = (modrm >> 3) & 7;
1331 uint8_t rm = modrm & 7;
Andreas Gampee5eb7062014-12-12 18:44:19 -08001332 std::string address = DumpAddress(mod, rm, rex64, rex_w, no_ops, byte_operand,
1333 byte_second_operand, prefix, load, src_reg_file, dst_reg_file,
1334 &instr, &address_bits);
Ian Rogers706a10e2012-03-23 17:00:55 -07001335
Ian Rogers677c12f2014-11-07 16:58:38 -08001336 if (reg_is_opcode && modrm_opcodes != nullptr) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001337 opcode3 = modrm_opcodes[reg_or_opcode];
Ian Rogers706a10e2012-03-23 17:00:55 -07001338 }
Mark Mendella33720c2014-06-18 21:02:29 -04001339
1340 // Add opcode suffixes to indicate size.
1341 if (byte_operand) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001342 opcode4 = "b";
Mark Mendella33720c2014-06-18 21:02:29 -04001343 } else if ((rex & REX_W) != 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001344 opcode4 = "q";
Mark Mendella33720c2014-06-18 21:02:29 -04001345 } else if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001346 opcode4 = "w";
Mark Mendella33720c2014-06-18 21:02:29 -04001347 }
1348
Ian Rogers706a10e2012-03-23 17:00:55 -07001349 if (load) {
1350 if (!reg_is_opcode) {
Ian Rogersbf989802012-04-16 16:07:49 -07001351 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2], dst_reg_file);
Ian Rogers706a10e2012-03-23 17:00:55 -07001352 args << ", ";
1353 }
1354 DumpSegmentOverride(args, prefix[1]);
Andreas Gampee5eb7062014-12-12 18:44:19 -08001355 args << address;
Ian Rogers706a10e2012-03-23 17:00:55 -07001356 } else {
1357 DCHECK(store);
1358 DumpSegmentOverride(args, prefix[1]);
Andreas Gampee5eb7062014-12-12 18:44:19 -08001359 args << address;
Ian Rogers706a10e2012-03-23 17:00:55 -07001360 if (!reg_is_opcode) {
1361 args << ", ";
Ian Rogersbf989802012-04-16 16:07:49 -07001362 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2], src_reg_file);
Ian Rogers706a10e2012-03-23 17:00:55 -07001363 }
1364 }
1365 }
1366 if (ax) {
jeffhaofdffdf82012-07-11 16:08:43 -07001367 // If this opcode implicitly uses ax, ax is always the first arg.
Ian Rogersbf989802012-04-16 16:07:49 -07001368 DumpReg(args, rex, 0 /* EAX */, byte_operand, prefix[2], GPR);
Ian Rogers706a10e2012-03-23 17:00:55 -07001369 }
jeffhaoe2962482012-06-28 11:29:57 -07001370 if (cx) {
1371 args << ", ";
1372 DumpReg(args, rex, 1 /* ECX */, true, prefix[2], GPR);
1373 }
Ian Rogers706a10e2012-03-23 17:00:55 -07001374 if (immediate_bytes > 0) {
jeffhaoe2962482012-06-28 11:29:57 -07001375 if (has_modrm || reg_in_opcode || ax || cx) {
Ian Rogers706a10e2012-03-23 17:00:55 -07001376 args << ", ";
1377 }
1378 if (immediate_bytes == 1) {
1379 args << StringPrintf("%d", *reinterpret_cast<const int8_t*>(instr));
1380 instr++;
Yixin Shou5192cbb2014-07-01 13:48:17 -04001381 } else if (immediate_bytes == 4) {
Mark Mendell67d18be2014-05-30 15:05:09 -04001382 if (prefix[2] == 0x66) { // Operand size override from 32-bit to 16-bit.
1383 args << StringPrintf("%d", *reinterpret_cast<const int16_t*>(instr));
1384 instr += 2;
1385 } else {
1386 args << StringPrintf("%d", *reinterpret_cast<const int32_t*>(instr));
1387 instr += 4;
1388 }
Yixin Shou5192cbb2014-07-01 13:48:17 -04001389 } else {
1390 CHECK_EQ(immediate_bytes, 8u);
1391 args << StringPrintf("%" PRId64, *reinterpret_cast<const int64_t*>(instr));
1392 instr += 8;
Ian Rogers706a10e2012-03-23 17:00:55 -07001393 }
1394 } else if (branch_bytes > 0) {
1395 DCHECK(!has_modrm);
1396 int32_t displacement;
1397 if (branch_bytes == 1) {
1398 displacement = *reinterpret_cast<const int8_t*>(instr);
1399 instr++;
1400 } else {
1401 CHECK_EQ(branch_bytes, 4u);
1402 displacement = *reinterpret_cast<const int32_t*>(instr);
1403 instr += 4;
1404 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001405 args << StringPrintf("%+d (", displacement)
1406 << FormatInstructionPointer(instr + displacement)
1407 << ")";
Ian Rogers706a10e2012-03-23 17:00:55 -07001408 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001409 if (prefix[1] == kFs && !supports_rex_) {
Elliott Hughes92301d92012-04-10 15:57:52 -07001410 args << " ; ";
Andreas Gampe372f3a32016-08-19 10:49:06 -07001411 GetDisassemblerOptions()->thread_offset_name_function_(args, address_bits);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001412 }
1413 if (prefix[1] == kGs && supports_rex_) {
1414 args << " ; ";
Andreas Gampe372f3a32016-08-19 10:49:06 -07001415 GetDisassemblerOptions()->thread_offset_name_function_(args, address_bits);
Elliott Hughes92301d92012-04-10 15:57:52 -07001416 }
Andreas Gampee5eb7062014-12-12 18:44:19 -08001417 const char* prefix_str;
Ian Rogers5e588b32013-02-21 15:05:09 -08001418 switch (prefix[0]) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001419 case 0xF0: prefix_str = "lock "; break;
1420 case 0xF2: prefix_str = "repne "; break;
1421 case 0xF3: prefix_str = "repe "; break;
1422 case 0: prefix_str = ""; break;
Ian Rogers2c4257b2014-10-24 14:20:06 -07001423 default: LOG(FATAL) << "Unreachable"; UNREACHABLE();
Ian Rogers5e588b32013-02-21 15:05:09 -08001424 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001425 os << FormatInstructionPointer(begin_instr)
Andreas Gampee5eb7062014-12-12 18:44:19 -08001426 << StringPrintf(": %22s \t%-7s%s%s%s%s%s ", DumpCodeHex(begin_instr, instr).c_str(),
1427 prefix_str, opcode0, opcode1, opcode2, opcode3, opcode4)
Ian Rogers5e588b32013-02-21 15:05:09 -08001428 << args.str() << '\n';
Ian Rogers706a10e2012-03-23 17:00:55 -07001429 return instr - begin_instr;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001430} // NOLINT(readability/fn_size)
Ian Rogers706a10e2012-03-23 17:00:55 -07001431
1432} // namespace x86
1433} // namespace art