blob: ff05733345bb527fd1c6b256b23a204d051c036c [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
Andreas Gampebda1d602016-08-29 17:43:45 -070024#include "android-base/logging.h"
25#include "android-base/stringprintf.h"
26
27using android::base::StringPrintf;
Elliott Hughes0f3c5532012-03-30 14:51:51 -070028
Ian Rogers706a10e2012-03-23 17:00:55 -070029namespace art {
30namespace x86 {
31
Ian Rogersb23a7722012-10-09 16:54:26 -070032size_t DisassemblerX86::Dump(std::ostream& os, const uint8_t* begin) {
33 return DumpInstruction(os, begin);
34}
35
Ian Rogers706a10e2012-03-23 17:00:55 -070036void DisassemblerX86::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
37 size_t length = 0;
38 for (const uint8_t* cur = begin; cur < end; cur += length) {
39 length = DumpInstruction(os, cur);
40 }
41}
42
Vladimir Kostyukov122113a2014-05-30 17:56:23 +070043static const char* gReg8Names[] = {
44 "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh"
45};
46static const char* gExtReg8Names[] = {
47 "al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil",
48 "r8l", "r9l", "r10l", "r11l", "r12l", "r13l", "r14l", "r15l"
49};
50static const char* gReg16Names[] = {
51 "ax", "cx", "dx", "bx", "sp", "bp", "si", "di",
52 "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w"
53};
54static const char* gReg32Names[] = {
55 "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi",
56 "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d"
57};
Ian Rogers38e12032014-03-14 14:06:14 -070058static const char* gReg64Names[] = {
59 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
60 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
61};
Ian Rogers706a10e2012-03-23 17:00:55 -070062
Mark Mendella33720c2014-06-18 21:02:29 -040063// 64-bit opcode REX modifier.
Andreas Gampec8ccf682014-09-29 20:07:43 -070064constexpr uint8_t REX_W = 8U /* 0b1000 */;
65constexpr uint8_t REX_R = 4U /* 0b0100 */;
66constexpr uint8_t REX_X = 2U /* 0b0010 */;
67constexpr uint8_t REX_B = 1U /* 0b0001 */;
Mark Mendella33720c2014-06-18 21:02:29 -040068
Ian Rogers38e12032014-03-14 14:06:14 -070069static void DumpReg0(std::ostream& os, uint8_t rex, size_t reg,
Ian Rogers706a10e2012-03-23 17:00:55 -070070 bool byte_operand, uint8_t size_override) {
Ian Rogers38e12032014-03-14 14:06:14 -070071 DCHECK_LT(reg, (rex == 0) ? 8u : 16u);
Mark Mendella33720c2014-06-18 21:02:29 -040072 bool rex_w = (rex & REX_W) != 0;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +070073 if (byte_operand) {
74 os << ((rex == 0) ? gReg8Names[reg] : gExtReg8Names[reg]);
75 } else if (rex_w) {
76 os << gReg64Names[reg];
77 } else if (size_override == 0x66) {
78 os << gReg16Names[reg];
79 } else {
80 os << gReg32Names[reg];
Ian Rogers706a10e2012-03-23 17:00:55 -070081 }
82}
83
Mark Mendell88649c72014-06-04 21:20:00 -040084static void DumpAnyReg(std::ostream& os, uint8_t rex, size_t reg,
Vladimir Kostyukov122113a2014-05-30 17:56:23 +070085 bool byte_operand, uint8_t size_override, RegFile reg_file) {
86 if (reg_file == GPR) {
87 DumpReg0(os, rex, reg, byte_operand, size_override);
88 } else if (reg_file == SSE) {
89 os << "xmm" << reg;
90 } else {
91 os << "mm" << reg;
92 }
93}
94
Ian Rogers706a10e2012-03-23 17:00:55 -070095static void DumpReg(std::ostream& os, uint8_t rex, uint8_t reg,
Ian Rogersbf989802012-04-16 16:07:49 -070096 bool byte_operand, uint8_t size_override, RegFile reg_file) {
Mark Mendella33720c2014-06-18 21:02:29 -040097 bool rex_r = (rex & REX_R) != 0;
Ian Rogers38e12032014-03-14 14:06:14 -070098 size_t reg_num = rex_r ? (reg + 8) : reg;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +070099 DumpAnyReg(os, rex, reg_num, byte_operand, size_override, reg_file);
100}
101
102static void DumpRmReg(std::ostream& os, uint8_t rex, uint8_t reg,
103 bool byte_operand, uint8_t size_override, RegFile reg_file) {
Mark Mendella33720c2014-06-18 21:02:29 -0400104 bool rex_b = (rex & REX_B) != 0;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700105 size_t reg_num = rex_b ? (reg + 8) : reg;
106 DumpAnyReg(os, rex, reg_num, byte_operand, size_override, reg_file);
107}
108
109static void DumpAddrReg(std::ostream& os, uint8_t rex, uint8_t reg) {
110 if (rex != 0) {
111 os << gReg64Names[reg];
Ian Rogersbf989802012-04-16 16:07:49 -0700112 } else {
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700113 os << gReg32Names[reg];
Ian Rogersbf989802012-04-16 16:07:49 -0700114 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700115}
116
Ian Rogers7caad772012-03-30 01:07:54 -0700117static void DumpBaseReg(std::ostream& os, uint8_t rex, uint8_t reg) {
Mark Mendella33720c2014-06-18 21:02:29 -0400118 bool rex_b = (rex & REX_B) != 0;
Ian Rogers38e12032014-03-14 14:06:14 -0700119 size_t reg_num = rex_b ? (reg + 8) : reg;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700120 DumpAddrReg(os, rex, reg_num);
Ian Rogers706a10e2012-03-23 17:00:55 -0700121}
122
Vladimir Kostyukov79bb1842014-07-01 18:28:43 +0700123static void DumpOpcodeReg(std::ostream& os, uint8_t rex, uint8_t reg,
124 bool byte_operand, uint8_t size_override) {
Mark Mendella33720c2014-06-18 21:02:29 -0400125 bool rex_b = (rex & REX_B) != 0;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700126 size_t reg_num = rex_b ? (reg + 8) : reg;
Vladimir Kostyukov79bb1842014-07-01 18:28:43 +0700127 DumpReg0(os, rex, reg_num, byte_operand, size_override);
Ian Rogers706a10e2012-03-23 17:00:55 -0700128}
129
Elliott Hughes92301d92012-04-10 15:57:52 -0700130enum SegmentPrefix {
131 kCs = 0x2e,
132 kSs = 0x36,
133 kDs = 0x3e,
134 kEs = 0x26,
135 kFs = 0x64,
136 kGs = 0x65,
137};
138
Ian Rogers706a10e2012-03-23 17:00:55 -0700139static void DumpSegmentOverride(std::ostream& os, uint8_t segment_prefix) {
140 switch (segment_prefix) {
Elliott Hughes92301d92012-04-10 15:57:52 -0700141 case kCs: os << "cs:"; break;
142 case kSs: os << "ss:"; break;
143 case kDs: os << "ds:"; break;
144 case kEs: os << "es:"; break;
145 case kFs: os << "fs:"; break;
146 case kGs: os << "gs:"; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700147 default: break;
148 }
149}
150
Andreas Gampee5eb7062014-12-12 18:44:19 -0800151// Do not inline to avoid Clang stack frame problems. b/18733806
Andreas Gampe86830382014-12-12 21:41:29 -0800152NO_INLINE
153static std::string DumpCodeHex(const uint8_t* begin, const uint8_t* end) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800154 std::stringstream hex;
155 for (size_t i = 0; begin + i < end; ++i) {
156 hex << StringPrintf("%02X", begin[i]);
157 }
158 return hex.str();
159}
160
161std::string DisassemblerX86::DumpAddress(uint8_t mod, uint8_t rm, uint8_t rex64, uint8_t rex_w,
162 bool no_ops, bool byte_operand, bool byte_second_operand,
163 uint8_t* prefix, bool load, RegFile src_reg_file,
164 RegFile dst_reg_file, const uint8_t** instr,
165 uint32_t* address_bits) {
166 std::ostringstream address;
167 if (mod == 0 && rm == 5) {
168 if (!supports_rex_) { // Absolute address.
Nicolas Geoffray6a0b9202014-12-16 14:54:18 +0000169 *address_bits = *reinterpret_cast<const uint32_t*>(*instr);
Andreas Gampee5eb7062014-12-12 18:44:19 -0800170 address << StringPrintf("[0x%x]", *address_bits);
171 } else { // 64-bit RIP relative addressing.
172 address << StringPrintf("[RIP + 0x%x]", *reinterpret_cast<const uint32_t*>(*instr));
173 }
174 (*instr) += 4;
175 } else if (rm == 4 && mod != 3) { // SIB
176 uint8_t sib = **instr;
177 (*instr)++;
178 uint8_t scale = (sib >> 6) & 3;
179 uint8_t index = (sib >> 3) & 7;
180 uint8_t base = sib & 7;
181 address << "[";
Andreas Gampe031b00d2015-01-26 19:30:23 -0800182
183 // REX.x is bit 3 of index.
184 if ((rex64 & REX_X) != 0) {
185 index += 8;
186 }
187
188 // Mod = 0 && base = 5 (ebp): no base (ignores REX.b).
189 bool has_base = false;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800190 if (base != 5 || mod != 0) {
Andreas Gampe031b00d2015-01-26 19:30:23 -0800191 has_base = true;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800192 DumpBaseReg(address, rex64, base);
Andreas Gampe031b00d2015-01-26 19:30:23 -0800193 }
194
195 // Index = 4 (esp/rsp) is disallowed.
196 if (index != 4) {
197 if (has_base) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800198 address << " + ";
199 }
Andreas Gampe031b00d2015-01-26 19:30:23 -0800200 DumpAddrReg(address, rex64, index);
Andreas Gampee5eb7062014-12-12 18:44:19 -0800201 if (scale != 0) {
202 address << StringPrintf(" * %d", 1 << scale);
203 }
204 }
Andreas Gampe031b00d2015-01-26 19:30:23 -0800205
Andreas Gampee5eb7062014-12-12 18:44:19 -0800206 if (mod == 0) {
207 if (base == 5) {
208 if (index != 4) {
209 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(*instr));
210 } else {
211 // 64-bit low 32-bit absolute address, redundant absolute address encoding on 32-bit.
212 *address_bits = *reinterpret_cast<const uint32_t*>(*instr);
213 address << StringPrintf("%d", *address_bits);
214 }
215 (*instr) += 4;
216 }
217 } else if (mod == 1) {
218 address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(*instr));
219 (*instr)++;
220 } else if (mod == 2) {
221 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(*instr));
222 (*instr) += 4;
223 }
224 address << "]";
225 } else {
226 if (mod == 3) {
227 if (!no_ops) {
228 DumpRmReg(address, rex_w, rm, byte_operand || byte_second_operand,
229 prefix[2], load ? src_reg_file : dst_reg_file);
230 }
231 } else {
232 address << "[";
233 DumpBaseReg(address, rex64, rm);
234 if (mod == 1) {
235 address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(*instr));
236 (*instr)++;
237 } else if (mod == 2) {
238 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(*instr));
239 (*instr) += 4;
240 }
241 address << "]";
242 }
243 }
244 return address.str();
245}
246
Serdjuk, Nikolay Y44148222015-09-14 18:05:33 +0600247size_t DisassemblerX86::DumpNops(std::ostream& os, const uint8_t* instr) {
248static constexpr uint8_t kNops[][10] = {
249 { },
250 { 0x90 },
251 { 0x66, 0x90 },
252 { 0x0f, 0x1f, 0x00 },
253 { 0x0f, 0x1f, 0x40, 0x00 },
254 { 0x0f, 0x1f, 0x44, 0x00, 0x00 },
255 { 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 },
256 { 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00 },
257 { 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 },
258 { 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 },
259 { 0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 }
260 };
261
262 for (size_t i = 1; i < arraysize(kNops); ++i) {
263 if (memcmp(instr, kNops[i], i) == 0) {
264 os << FormatInstructionPointer(instr)
265 << StringPrintf(": %22s \t nop \n", DumpCodeHex(instr, instr + i).c_str());
266 return i;
267 }
268 }
269
270 return 0;
271}
272
Ian Rogers706a10e2012-03-23 17:00:55 -0700273size_t DisassemblerX86::DumpInstruction(std::ostream& os, const uint8_t* instr) {
Serdjuk, Nikolay Y44148222015-09-14 18:05:33 +0600274 size_t nop_size = DumpNops(os, instr);
275 if (nop_size != 0u) {
276 return nop_size;
277 }
278
Ian Rogers706a10e2012-03-23 17:00:55 -0700279 const uint8_t* begin_instr = instr;
280 bool have_prefixes = true;
281 uint8_t prefix[4] = {0, 0, 0, 0};
Ian Rogers706a10e2012-03-23 17:00:55 -0700282 do {
283 switch (*instr) {
Ian Rogers7caad772012-03-30 01:07:54 -0700284 // Group 1 - lock and repeat prefixes:
Ian Rogers706a10e2012-03-23 17:00:55 -0700285 case 0xF0:
286 case 0xF2:
287 case 0xF3:
288 prefix[0] = *instr;
289 break;
290 // Group 2 - segment override prefixes:
Elliott Hughes92301d92012-04-10 15:57:52 -0700291 case kCs:
292 case kSs:
293 case kDs:
294 case kEs:
295 case kFs:
296 case kGs:
Ian Rogers706a10e2012-03-23 17:00:55 -0700297 prefix[1] = *instr;
298 break;
299 // Group 3 - operand size override:
300 case 0x66:
301 prefix[2] = *instr;
302 break;
303 // Group 4 - address size override:
304 case 0x67:
305 prefix[3] = *instr;
306 break;
307 default:
308 have_prefixes = false;
309 break;
310 }
311 if (have_prefixes) {
312 instr++;
313 }
314 } while (have_prefixes);
Ian Rogers38e12032014-03-14 14:06:14 -0700315 uint8_t rex = (supports_rex_ && (*instr >= 0x40) && (*instr <= 0x4F)) ? *instr : 0;
Vladimir Kostyukove8861b32014-04-18 17:06:15 +0700316 if (rex != 0) {
317 instr++;
318 }
Ian Rogers677c12f2014-11-07 16:58:38 -0800319 const char** modrm_opcodes = nullptr;
Ian Rogers706a10e2012-03-23 17:00:55 -0700320 bool has_modrm = false;
321 bool reg_is_opcode = false;
322 size_t immediate_bytes = 0;
323 size_t branch_bytes = 0;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800324 std::string opcode_tmp; // Storage to keep StringPrintf result alive.
325 const char* opcode0 = ""; // Prefix part.
326 const char* opcode1 = ""; // Main opcode.
327 const char* opcode2 = ""; // Sub-opcode. E.g., jump type.
328 const char* opcode3 = ""; // Mod-rm part.
329 const char* opcode4 = ""; // Suffix part.
Ian Rogers706a10e2012-03-23 17:00:55 -0700330 bool store = false; // stores to memory (ie rm is on the left)
331 bool load = false; // loads from memory (ie rm is on the right)
Serguei Katkov94f3eb02014-06-24 13:23:17 +0700332 bool byte_operand = false; // true when the opcode is dealing with byte operands
Ian Rogers677c12f2014-11-07 16:58:38 -0800333 // true when the source operand is a byte register but the target register isn't
334 // (ie movsxb/movzxb).
335 bool byte_second_operand = false;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700336 bool target_specific = false; // register name depends on target (64 vs 32 bits).
Ian Rogers706a10e2012-03-23 17:00:55 -0700337 bool ax = false; // implicit use of ax
jeffhaoe2962482012-06-28 11:29:57 -0700338 bool cx = false; // implicit use of cx
Ian Rogers706a10e2012-03-23 17:00:55 -0700339 bool reg_in_opcode = false; // low 3-bits of opcode encode register parameter
jeffhao703f2cd2012-07-13 17:25:52 -0700340 bool no_ops = false;
Ian Rogersbf989802012-04-16 16:07:49 -0700341 RegFile src_reg_file = GPR;
342 RegFile dst_reg_file = GPR;
Ian Rogers706a10e2012-03-23 17:00:55 -0700343 switch (*instr) {
344#define DISASSEMBLER_ENTRY(opname, \
345 rm8_r8, rm32_r32, \
346 r8_rm8, r32_rm32, \
347 ax8_i8, ax32_i32) \
Andreas Gampee5eb7062014-12-12 18:44:19 -0800348 case rm8_r8: opcode1 = #opname; store = true; has_modrm = true; byte_operand = true; break; \
349 case rm32_r32: opcode1 = #opname; store = true; has_modrm = true; break; \
350 case r8_rm8: opcode1 = #opname; load = true; has_modrm = true; byte_operand = true; break; \
351 case r32_rm32: opcode1 = #opname; load = true; has_modrm = true; break; \
352 case ax8_i8: opcode1 = #opname; ax = true; immediate_bytes = 1; byte_operand = true; break; \
353 case ax32_i32: opcode1 = #opname; ax = true; immediate_bytes = 4; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700354
355DISASSEMBLER_ENTRY(add,
356 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */,
357 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */,
358 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */)
359DISASSEMBLER_ENTRY(or,
360 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */,
361 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */,
362 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */)
363DISASSEMBLER_ENTRY(adc,
364 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */,
365 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */,
366 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */)
367DISASSEMBLER_ENTRY(sbb,
368 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */,
369 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */,
370 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */)
371DISASSEMBLER_ENTRY(and,
372 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */,
373 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */,
374 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */)
375DISASSEMBLER_ENTRY(sub,
376 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */,
377 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */,
378 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */)
379DISASSEMBLER_ENTRY(xor,
380 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */,
381 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */,
382 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */)
383DISASSEMBLER_ENTRY(cmp,
384 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */,
385 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */,
386 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */)
387
388#undef DISASSEMBLER_ENTRY
389 case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800390 opcode1 = "push";
Ian Rogers706a10e2012-03-23 17:00:55 -0700391 reg_in_opcode = true;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700392 target_specific = true;
Ian Rogers706a10e2012-03-23 17:00:55 -0700393 break;
394 case 0x58: case 0x59: case 0x5A: case 0x5B: case 0x5C: case 0x5D: case 0x5E: case 0x5F:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800395 opcode1 = "pop";
Ian Rogers706a10e2012-03-23 17:00:55 -0700396 reg_in_opcode = true;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +0700397 target_specific = true;
Ian Rogers706a10e2012-03-23 17:00:55 -0700398 break;
Mark Mendell33ecf8d2014-06-06 15:19:45 -0400399 case 0x63:
Vladimir Kostyukovec95f722014-07-23 12:10:07 +0700400 if ((rex & REX_W) != 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800401 opcode1 = "movsxd";
Mark Mendell33ecf8d2014-06-06 15:19:45 -0400402 has_modrm = true;
403 load = true;
404 } else {
405 // In 32-bit mode (!supports_rex_) this is ARPL, with no REX prefix the functionality is the
406 // same as 'mov' but the use of the instruction is discouraged.
Andreas Gampee5eb7062014-12-12 18:44:19 -0800407 opcode_tmp = StringPrintf("unknown opcode '%02X'", *instr);
408 opcode1 = opcode_tmp.c_str();
Mark Mendell33ecf8d2014-06-06 15:19:45 -0400409 }
410 break;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800411 case 0x68: opcode1 = "push"; immediate_bytes = 4; break;
412 case 0x69: opcode1 = "imul"; load = true; has_modrm = true; immediate_bytes = 4; break;
413 case 0x6A: opcode1 = "push"; immediate_bytes = 1; break;
414 case 0x6B: opcode1 = "imul"; load = true; has_modrm = true; immediate_bytes = 1; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700415 case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77:
416 case 0x78: case 0x79: case 0x7A: case 0x7B: case 0x7C: case 0x7D: case 0x7E: case 0x7F:
417 static const char* condition_codes[] =
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700418 {"o", "no", "b/nae/c", "nb/ae/nc", "z/eq", "nz/ne", "be/na", "nbe/a",
419 "s", "ns", "p/pe", "np/po", "l/nge", "nl/ge", "le/ng", "nle/g"
Ian Rogers706a10e2012-03-23 17:00:55 -0700420 };
Andreas Gampee5eb7062014-12-12 18:44:19 -0800421 opcode1 = "j";
422 opcode2 = condition_codes[*instr & 0xF];
Ian Rogers706a10e2012-03-23 17:00:55 -0700423 branch_bytes = 1;
424 break;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800425 case 0x86: case 0x87:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800426 opcode1 = "xchg";
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800427 store = true;
428 has_modrm = true;
429 byte_operand = (*instr == 0x86);
430 break;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800431 case 0x88: opcode1 = "mov"; store = true; has_modrm = true; byte_operand = true; break;
432 case 0x89: opcode1 = "mov"; store = true; has_modrm = true; break;
433 case 0x8A: opcode1 = "mov"; load = true; has_modrm = true; byte_operand = true; break;
434 case 0x8B: opcode1 = "mov"; load = true; has_modrm = true; break;
Serdjuk, Nikolay Y44148222015-09-14 18:05:33 +0600435 case 0x9D: opcode1 = "popf"; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700436
437 case 0x0F: // 2 byte extended opcode
438 instr++;
439 switch (*instr) {
Ian Rogers7caad772012-03-30 01:07:54 -0700440 case 0x10: case 0x11:
441 if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800442 opcode1 = "movsd";
jeffhaofdffdf82012-07-11 16:08:43 -0700443 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700444 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800445 opcode1 = "movss";
jeffhaofdffdf82012-07-11 16:08:43 -0700446 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700447 } else if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800448 opcode1 = "movupd";
jeffhaofdffdf82012-07-11 16:08:43 -0700449 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700450 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800451 opcode1 = "movups";
Ian Rogers7caad772012-03-30 01:07:54 -0700452 }
453 has_modrm = true;
Ian Rogersbf989802012-04-16 16:07:49 -0700454 src_reg_file = dst_reg_file = SSE;
Ian Rogers7caad772012-03-30 01:07:54 -0700455 load = *instr == 0x10;
456 store = !load;
457 break;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800458 case 0x12: case 0x13:
459 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800460 opcode1 = "movlpd";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800461 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
462 } else if (prefix[0] == 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800463 opcode1 = "movlps";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800464 }
465 has_modrm = true;
466 src_reg_file = dst_reg_file = SSE;
467 load = *instr == 0x12;
468 store = !load;
469 break;
470 case 0x16: case 0x17:
471 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800472 opcode1 = "movhpd";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800473 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
474 } else if (prefix[0] == 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800475 opcode1 = "movhps";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800476 }
477 has_modrm = true;
478 src_reg_file = dst_reg_file = SSE;
479 load = *instr == 0x16;
480 store = !load;
481 break;
482 case 0x28: case 0x29:
483 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800484 opcode1 = "movapd";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800485 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
486 } else if (prefix[0] == 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800487 opcode1 = "movaps";
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800488 }
489 has_modrm = true;
490 src_reg_file = dst_reg_file = SSE;
491 load = *instr == 0x28;
492 store = !load;
493 break;
jeffhaofdffdf82012-07-11 16:08:43 -0700494 case 0x2A:
495 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800496 opcode1 = "cvtpi2pd";
jeffhaofdffdf82012-07-11 16:08:43 -0700497 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
498 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800499 opcode1 = "cvtsi2sd";
jeffhaofdffdf82012-07-11 16:08:43 -0700500 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
501 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800502 opcode1 = "cvtsi2ss";
jeffhaofdffdf82012-07-11 16:08:43 -0700503 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
504 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800505 opcode1 = "cvtpi2ps";
jeffhaofdffdf82012-07-11 16:08:43 -0700506 }
507 load = true;
508 has_modrm = true;
509 dst_reg_file = SSE;
510 break;
511 case 0x2C:
512 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800513 opcode1 = "cvttpd2pi";
jeffhaofdffdf82012-07-11 16:08:43 -0700514 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
515 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800516 opcode1 = "cvttsd2si";
jeffhaofdffdf82012-07-11 16:08:43 -0700517 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
518 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800519 opcode1 = "cvttss2si";
jeffhaofdffdf82012-07-11 16:08:43 -0700520 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
521 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800522 opcode1 = "cvttps2pi";
jeffhaofdffdf82012-07-11 16:08:43 -0700523 }
524 load = true;
525 has_modrm = true;
526 src_reg_file = SSE;
527 break;
528 case 0x2D:
529 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800530 opcode1 = "cvtpd2pi";
jeffhaofdffdf82012-07-11 16:08:43 -0700531 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
532 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800533 opcode1 = "cvtsd2si";
jeffhaofdffdf82012-07-11 16:08:43 -0700534 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
535 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800536 opcode1 = "cvtss2si";
jeffhaofdffdf82012-07-11 16:08:43 -0700537 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
538 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800539 opcode1 = "cvtps2pi";
jeffhaofdffdf82012-07-11 16:08:43 -0700540 }
541 load = true;
542 has_modrm = true;
543 src_reg_file = SSE;
544 break;
545 case 0x2E:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800546 opcode0 = "u";
Ian Rogersfc787ec2014-10-09 21:56:44 -0700547 FALLTHROUGH_INTENDED;
jeffhaofdffdf82012-07-11 16:08:43 -0700548 case 0x2F:
549 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800550 opcode1 = "comisd";
jeffhaofdffdf82012-07-11 16:08:43 -0700551 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
552 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800553 opcode1 = "comiss";
jeffhaofdffdf82012-07-11 16:08:43 -0700554 }
555 has_modrm = true;
556 load = true;
557 src_reg_file = dst_reg_file = SSE;
558 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700559 case 0x38: // 3 byte extended opcode
Mark Mendellfe945782014-05-22 09:52:36 -0400560 instr++;
561 if (prefix[2] == 0x66) {
562 switch (*instr) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700563 case 0x01:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800564 opcode1 = "phaddw";
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700565 prefix[2] = 0;
566 has_modrm = true;
567 load = true;
568 src_reg_file = dst_reg_file = SSE;
569 break;
570 case 0x02:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800571 opcode1 = "phaddd";
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700572 prefix[2] = 0;
573 has_modrm = true;
574 load = true;
575 src_reg_file = dst_reg_file = SSE;
576 break;
Mark Mendellfe945782014-05-22 09:52:36 -0400577 case 0x40:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800578 opcode1 = "pmulld";
Mark Mendellfe945782014-05-22 09:52:36 -0400579 prefix[2] = 0;
580 has_modrm = true;
581 load = true;
582 src_reg_file = dst_reg_file = SSE;
583 break;
584 default:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800585 opcode_tmp = StringPrintf("unknown opcode '0F 38 %02X'", *instr);
586 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -0400587 }
588 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800589 opcode_tmp = StringPrintf("unknown opcode '0F 38 %02X'", *instr);
590 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -0400591 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700592 break;
593 case 0x3A: // 3 byte extended opcode
Mark Mendellfe945782014-05-22 09:52:36 -0400594 instr++;
595 if (prefix[2] == 0x66) {
596 switch (*instr) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400597 case 0x0A:
598 opcode1 = "roundss";
599 prefix[2] = 0;
600 has_modrm = true;
Aart Bik33dd9092016-08-01 15:55:36 -0700601 load = true;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400602 src_reg_file = SSE;
603 dst_reg_file = SSE;
604 immediate_bytes = 1;
605 break;
606 case 0x0B:
607 opcode1 = "roundsd";
608 prefix[2] = 0;
609 has_modrm = true;
Aart Bik33dd9092016-08-01 15:55:36 -0700610 load = true;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400611 src_reg_file = SSE;
612 dst_reg_file = SSE;
613 immediate_bytes = 1;
614 break;
Mark Mendellfe945782014-05-22 09:52:36 -0400615 case 0x14:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800616 opcode1 = "pextrb";
Mark Mendellfe945782014-05-22 09:52:36 -0400617 prefix[2] = 0;
618 has_modrm = true;
619 store = true;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700620 src_reg_file = SSE;
Mark Mendellfe945782014-05-22 09:52:36 -0400621 immediate_bytes = 1;
622 break;
nikolay serdjuke0705f52015-04-27 17:52:57 +0600623 case 0x15:
624 opcode1 = "pextrw";
625 prefix[2] = 0;
626 has_modrm = true;
627 store = true;
628 src_reg_file = SSE;
629 immediate_bytes = 1;
630 break;
Mark Mendellfe945782014-05-22 09:52:36 -0400631 case 0x16:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800632 opcode1 = "pextrd";
Mark Mendellfe945782014-05-22 09:52:36 -0400633 prefix[2] = 0;
634 has_modrm = true;
635 store = true;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700636 src_reg_file = SSE;
Mark Mendellfe945782014-05-22 09:52:36 -0400637 immediate_bytes = 1;
638 break;
639 default:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800640 opcode_tmp = StringPrintf("unknown opcode '0F 3A %02X'", *instr);
641 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -0400642 }
643 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800644 opcode_tmp = StringPrintf("unknown opcode '0F 3A %02X'", *instr);
645 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -0400646 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700647 break;
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800648 case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47:
649 case 0x48: case 0x49: case 0x4A: case 0x4B: case 0x4C: case 0x4D: case 0x4E: case 0x4F:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800650 opcode1 = "cmov";
651 opcode2 = condition_codes[*instr & 0xF];
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800652 has_modrm = true;
653 load = true;
654 break;
Ian Rogersbf989802012-04-16 16:07:49 -0700655 case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
656 case 0x58: case 0x59: case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
657 switch (*instr) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800658 case 0x50: opcode1 = "movmsk"; break;
659 case 0x51: opcode1 = "sqrt"; break;
660 case 0x52: opcode1 = "rsqrt"; break;
661 case 0x53: opcode1 = "rcp"; break;
662 case 0x54: opcode1 = "and"; break;
663 case 0x55: opcode1 = "andn"; break;
664 case 0x56: opcode1 = "or"; break;
665 case 0x57: opcode1 = "xor"; break;
666 case 0x58: opcode1 = "add"; break;
667 case 0x59: opcode1 = "mul"; break;
668 case 0x5C: opcode1 = "sub"; break;
669 case 0x5D: opcode1 = "min"; break;
670 case 0x5E: opcode1 = "div"; break;
671 case 0x5F: opcode1 = "max"; break;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700672 default: LOG(FATAL) << "Unreachable"; UNREACHABLE();
Ian Rogersbf989802012-04-16 16:07:49 -0700673 }
674 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800675 opcode2 = "pd";
jeffhaofdffdf82012-07-11 16:08:43 -0700676 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700677 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800678 opcode2 = "sd";
jeffhaofdffdf82012-07-11 16:08:43 -0700679 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700680 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800681 opcode2 = "ss";
jeffhaofdffdf82012-07-11 16:08:43 -0700682 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700683 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800684 opcode2 = "ps";
Ian Rogersbf989802012-04-16 16:07:49 -0700685 }
686 load = true;
687 has_modrm = true;
688 src_reg_file = dst_reg_file = SSE;
689 break;
690 }
691 case 0x5A:
692 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800693 opcode1 = "cvtpd2ps";
jeffhaofdffdf82012-07-11 16:08:43 -0700694 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700695 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800696 opcode1 = "cvtsd2ss";
jeffhaofdffdf82012-07-11 16:08:43 -0700697 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700698 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800699 opcode1 = "cvtss2sd";
jeffhaofdffdf82012-07-11 16:08:43 -0700700 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700701 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800702 opcode1 = "cvtps2pd";
Ian Rogersbf989802012-04-16 16:07:49 -0700703 }
704 load = true;
705 has_modrm = true;
706 src_reg_file = dst_reg_file = SSE;
707 break;
708 case 0x5B:
709 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800710 opcode1 = "cvtps2dq";
jeffhaofdffdf82012-07-11 16:08:43 -0700711 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700712 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800713 opcode1 = "bad opcode F2 0F 5B";
Ian Rogersbf989802012-04-16 16:07:49 -0700714 } else if (prefix[0] == 0xF3) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800715 opcode1 = "cvttps2dq";
jeffhaofdffdf82012-07-11 16:08:43 -0700716 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700717 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800718 opcode1 = "cvtdq2ps";
Ian Rogersbf989802012-04-16 16:07:49 -0700719 }
720 load = true;
721 has_modrm = true;
722 src_reg_file = dst_reg_file = SSE;
723 break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700724 case 0x60: case 0x61: case 0x62: case 0x6C:
Razvan A Lupusorud3266bc2014-01-24 12:55:31 -0800725 if (prefix[2] == 0x66) {
726 src_reg_file = dst_reg_file = SSE;
727 prefix[2] = 0; // Clear prefix now. It has served its purpose as part of the opcode.
728 } else {
729 src_reg_file = dst_reg_file = MMX;
730 }
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700731 switch (*instr) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800732 case 0x60: opcode1 = "punpcklbw"; break;
733 case 0x61: opcode1 = "punpcklwd"; break;
734 case 0x62: opcode1 = "punpckldq"; break;
735 case 0x6c: opcode1 = "punpcklqdq"; break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700736 }
Razvan A Lupusorud3266bc2014-01-24 12:55:31 -0800737 load = true;
738 has_modrm = true;
739 break;
Ian Rogersbf989802012-04-16 16:07:49 -0700740 case 0x6E:
741 if (prefix[2] == 0x66) {
742 dst_reg_file = SSE;
jeffhaofdffdf82012-07-11 16:08:43 -0700743 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700744 } else {
745 dst_reg_file = MMX;
Ian Rogersbf989802012-04-16 16:07:49 -0700746 }
Andreas Gampee5eb7062014-12-12 18:44:19 -0800747 opcode1 = "movd";
Ian Rogersbf989802012-04-16 16:07:49 -0700748 load = true;
749 has_modrm = true;
750 break;
751 case 0x6F:
752 if (prefix[2] == 0x66) {
Mark Mendellfe945782014-05-22 09:52:36 -0400753 src_reg_file = dst_reg_file = SSE;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800754 opcode1 = "movdqa";
jeffhaofdffdf82012-07-11 16:08:43 -0700755 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700756 } else if (prefix[0] == 0xF3) {
Mark Mendellfe945782014-05-22 09:52:36 -0400757 src_reg_file = dst_reg_file = SSE;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800758 opcode1 = "movdqu";
jeffhaofdffdf82012-07-11 16:08:43 -0700759 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700760 } else {
761 dst_reg_file = MMX;
Andreas Gampee5eb7062014-12-12 18:44:19 -0800762 opcode1 = "movq";
Ian Rogersbf989802012-04-16 16:07:49 -0700763 }
764 load = true;
765 has_modrm = true;
766 break;
Mark Mendellfe945782014-05-22 09:52:36 -0400767 case 0x70:
768 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800769 opcode1 = "pshufd";
Mark Mendellfe945782014-05-22 09:52:36 -0400770 prefix[2] = 0;
771 has_modrm = true;
772 store = true;
773 src_reg_file = dst_reg_file = SSE;
774 immediate_bytes = 1;
775 } else if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800776 opcode1 = "pshuflw";
Mark Mendellfe945782014-05-22 09:52:36 -0400777 prefix[0] = 0;
778 has_modrm = true;
779 store = true;
780 src_reg_file = dst_reg_file = SSE;
781 immediate_bytes = 1;
782 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800783 opcode_tmp = StringPrintf("unknown opcode '0F %02X'", *instr);
784 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -0400785 }
786 break;
jeffhaofdffdf82012-07-11 16:08:43 -0700787 case 0x71:
788 if (prefix[2] == 0x66) {
789 dst_reg_file = SSE;
790 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
791 } else {
792 dst_reg_file = MMX;
793 }
Ian Rogers677c12f2014-11-07 16:58:38 -0800794 static const char* x71_opcodes[] = {
795 "unknown-71", "unknown-71", "psrlw", "unknown-71",
796 "psraw", "unknown-71", "psllw", "unknown-71"};
jeffhaofdffdf82012-07-11 16:08:43 -0700797 modrm_opcodes = x71_opcodes;
798 reg_is_opcode = true;
799 has_modrm = true;
800 store = true;
801 immediate_bytes = 1;
802 break;
803 case 0x72:
804 if (prefix[2] == 0x66) {
805 dst_reg_file = SSE;
806 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
807 } else {
808 dst_reg_file = MMX;
809 }
Ian Rogers677c12f2014-11-07 16:58:38 -0800810 static const char* x72_opcodes[] = {
811 "unknown-72", "unknown-72", "psrld", "unknown-72",
812 "psrad", "unknown-72", "pslld", "unknown-72"};
jeffhaofdffdf82012-07-11 16:08:43 -0700813 modrm_opcodes = x72_opcodes;
814 reg_is_opcode = true;
815 has_modrm = true;
816 store = true;
817 immediate_bytes = 1;
818 break;
819 case 0x73:
820 if (prefix[2] == 0x66) {
821 dst_reg_file = SSE;
822 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
823 } else {
824 dst_reg_file = MMX;
825 }
Ian Rogers677c12f2014-11-07 16:58:38 -0800826 static const char* x73_opcodes[] = {
827 "unknown-73", "unknown-73", "psrlq", "psrldq",
828 "unknown-73", "unknown-73", "psllq", "unknown-73"};
jeffhaofdffdf82012-07-11 16:08:43 -0700829 modrm_opcodes = x73_opcodes;
830 reg_is_opcode = true;
831 has_modrm = true;
832 store = true;
833 immediate_bytes = 1;
834 break;
Olivier Comefb0fecf2014-06-20 11:46:16 +0200835 case 0x7C:
836 if (prefix[0] == 0xF2) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800837 opcode1 = "haddps";
Olivier Comefb0fecf2014-06-20 11:46:16 +0200838 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
839 } else if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800840 opcode1 = "haddpd";
Olivier Comefb0fecf2014-06-20 11:46:16 +0200841 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
842 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -0800843 opcode_tmp = StringPrintf("unknown opcode '0F %02X'", *instr);
844 opcode1 = opcode_tmp.c_str();
Olivier Comefb0fecf2014-06-20 11:46:16 +0200845 break;
846 }
847 src_reg_file = dst_reg_file = SSE;
848 has_modrm = true;
849 load = true;
850 break;
jeffhaofdffdf82012-07-11 16:08:43 -0700851 case 0x7E:
852 if (prefix[2] == 0x66) {
853 src_reg_file = SSE;
854 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
855 } else {
856 src_reg_file = MMX;
857 }
Andreas Gampee5eb7062014-12-12 18:44:19 -0800858 opcode1 = "movd";
jeffhaofdffdf82012-07-11 16:08:43 -0700859 has_modrm = true;
860 store = true;
861 break;
Aart Bik68555e92017-02-13 14:28:45 -0800862 case 0x7F:
863 if (prefix[2] == 0x66) {
864 src_reg_file = dst_reg_file = SSE;
865 opcode1 = "movdqa";
866 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
867 } else if (prefix[0] == 0xF3) {
868 src_reg_file = dst_reg_file = SSE;
869 opcode1 = "movdqu";
870 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
871 } else {
872 dst_reg_file = MMX;
873 opcode1 = "movq";
874 }
875 store = true;
876 has_modrm = true;
877 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700878 case 0x80: case 0x81: case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87:
879 case 0x88: case 0x89: case 0x8A: case 0x8B: case 0x8C: case 0x8D: case 0x8E: case 0x8F:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800880 opcode1 = "j";
881 opcode2 = condition_codes[*instr & 0xF];
Ian Rogers706a10e2012-03-23 17:00:55 -0700882 branch_bytes = 4;
883 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700884 case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97:
885 case 0x98: case 0x99: case 0x9A: case 0x9B: case 0x9C: case 0x9D: case 0x9E: case 0x9F:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800886 opcode1 = "set";
887 opcode2 = condition_codes[*instr & 0xF];
Ian Rogers677c12f2014-11-07 16:58:38 -0800888 modrm_opcodes = nullptr;
Ian Rogers7caad772012-03-30 01:07:54 -0700889 reg_is_opcode = true;
890 has_modrm = true;
891 store = true;
892 break;
Mark Mendell4708dcd2014-01-22 09:05:18 -0800893 case 0xA4:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800894 opcode1 = "shld";
Mark Mendell4708dcd2014-01-22 09:05:18 -0800895 has_modrm = true;
896 load = true;
897 immediate_bytes = 1;
898 break;
Yixin Shouf40f8902014-08-14 14:10:32 -0400899 case 0xA5:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800900 opcode1 = "shld";
Yixin Shouf40f8902014-08-14 14:10:32 -0400901 has_modrm = true;
902 load = true;
903 cx = true;
904 break;
Mark Mendell4708dcd2014-01-22 09:05:18 -0800905 case 0xAC:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800906 opcode1 = "shrd";
Mark Mendell4708dcd2014-01-22 09:05:18 -0800907 has_modrm = true;
908 load = true;
909 immediate_bytes = 1;
910 break;
Yixin Shouf40f8902014-08-14 14:10:32 -0400911 case 0xAD:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800912 opcode1 = "shrd";
Yixin Shouf40f8902014-08-14 14:10:32 -0400913 has_modrm = true;
914 load = true;
915 cx = true;
916 break;
jeffhao703f2cd2012-07-13 17:25:52 -0700917 case 0xAE:
918 if (prefix[0] == 0xF3) {
Ian Rogers5e588b32013-02-21 15:05:09 -0800919 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers677c12f2014-11-07 16:58:38 -0800920 static const char* xAE_opcodes[] = {
921 "rdfsbase", "rdgsbase", "wrfsbase", "wrgsbase",
922 "unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE"};
jeffhao703f2cd2012-07-13 17:25:52 -0700923 modrm_opcodes = xAE_opcodes;
924 reg_is_opcode = true;
925 has_modrm = true;
926 uint8_t reg_or_opcode = (instr[1] >> 3) & 7;
927 switch (reg_or_opcode) {
928 case 0:
929 prefix[1] = kFs;
930 load = true;
931 break;
932 case 1:
933 prefix[1] = kGs;
934 load = true;
935 break;
936 case 2:
937 prefix[1] = kFs;
938 store = true;
939 break;
940 case 3:
941 prefix[1] = kGs;
942 store = true;
943 break;
944 default:
945 load = true;
946 break;
947 }
948 } else {
Ian Rogers677c12f2014-11-07 16:58:38 -0800949 static const char* xAE_opcodes[] = {
950 "unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE",
951 "unknown-AE", "lfence", "mfence", "sfence"};
jeffhao703f2cd2012-07-13 17:25:52 -0700952 modrm_opcodes = xAE_opcodes;
953 reg_is_opcode = true;
954 has_modrm = true;
955 load = true;
956 no_ops = true;
957 }
958 break;
Ian Rogers677c12f2014-11-07 16:58:38 -0800959 case 0xAF:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800960 opcode1 = "imul";
Ian Rogers677c12f2014-11-07 16:58:38 -0800961 has_modrm = true;
962 load = true;
963 break;
964 case 0xB1:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800965 opcode1 = "cmpxchg";
Ian Rogers677c12f2014-11-07 16:58:38 -0800966 has_modrm = true;
967 store = true;
968 break;
969 case 0xB6:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800970 opcode1 = "movzxb";
Ian Rogers677c12f2014-11-07 16:58:38 -0800971 has_modrm = true;
972 load = true;
973 byte_second_operand = true;
974 break;
975 case 0xB7:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800976 opcode1 = "movzxw";
Ian Rogers677c12f2014-11-07 16:58:38 -0800977 has_modrm = true;
978 load = true;
979 break;
Mark Mendellbcee0922015-09-15 21:45:01 -0400980 case 0xBC:
981 opcode1 = "bsf";
982 has_modrm = true;
983 load = true;
984 break;
Mark Mendell8ae3ffb2015-08-12 21:16:41 -0400985 case 0xBD:
986 opcode1 = "bsr";
987 has_modrm = true;
988 load = true;
989 break;
Aart Bik3f67e692016-01-15 14:35:12 -0800990 case 0xB8:
991 opcode1 = "popcnt";
992 has_modrm = true;
993 load = true;
994 break;
Ian Rogers677c12f2014-11-07 16:58:38 -0800995 case 0xBE:
Andreas Gampee5eb7062014-12-12 18:44:19 -0800996 opcode1 = "movsxb";
Ian Rogers677c12f2014-11-07 16:58:38 -0800997 has_modrm = true;
998 load = true;
999 byte_second_operand = true;
1000 rex |= (rex == 0 ? 0 : REX_W);
1001 break;
1002 case 0xBF:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001003 opcode1 = "movsxw";
Ian Rogers677c12f2014-11-07 16:58:38 -08001004 has_modrm = true;
1005 load = true;
1006 break;
1007 case 0xC3:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001008 opcode1 = "movnti";
Ian Rogers677c12f2014-11-07 16:58:38 -08001009 store = true;
1010 has_modrm = true;
1011 break;
Mark Mendellfe945782014-05-22 09:52:36 -04001012 case 0xC5:
1013 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001014 opcode1 = "pextrw";
Mark Mendellfe945782014-05-22 09:52:36 -04001015 prefix[2] = 0;
1016 has_modrm = true;
nikolay serdjukbd4e6a82015-03-27 16:32:27 +06001017 load = true;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001018 src_reg_file = SSE;
Mark Mendellfe945782014-05-22 09:52:36 -04001019 immediate_bytes = 1;
1020 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001021 opcode_tmp = StringPrintf("unknown opcode '0F %02X'", *instr);
1022 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -04001023 }
1024 break;
Olivier Comefb0fecf2014-06-20 11:46:16 +02001025 case 0xC6:
1026 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001027 opcode1 = "shufpd";
Olivier Comefb0fecf2014-06-20 11:46:16 +02001028 prefix[2] = 0;
1029 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001030 opcode1 = "shufps";
Olivier Comefb0fecf2014-06-20 11:46:16 +02001031 }
1032 has_modrm = true;
1033 store = true;
1034 src_reg_file = dst_reg_file = SSE;
1035 immediate_bytes = 1;
1036 break;
Vladimir Marko70b797d2013-12-03 15:25:24 +00001037 case 0xC7:
Ian Rogers677c12f2014-11-07 16:58:38 -08001038 static const char* x0FxC7_opcodes[] = {
1039 "unknown-0f-c7", "cmpxchg8b", "unknown-0f-c7", "unknown-0f-c7",
1040 "unknown-0f-c7", "unknown-0f-c7", "unknown-0f-c7", "unknown-0f-c7"};
Vladimir Marko70b797d2013-12-03 15:25:24 +00001041 modrm_opcodes = x0FxC7_opcodes;
1042 has_modrm = true;
1043 reg_is_opcode = true;
1044 store = true;
1045 break;
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001046 case 0xC8: case 0xC9: case 0xCA: case 0xCB: case 0xCC: case 0xCD: case 0xCE: case 0xCF:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001047 opcode1 = "bswap";
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001048 reg_in_opcode = true;
1049 break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001050 case 0xD4:
1051 if (prefix[2] == 0x66) {
1052 src_reg_file = dst_reg_file = SSE;
1053 prefix[2] = 0;
1054 } else {
1055 src_reg_file = dst_reg_file = MMX;
1056 }
Andreas Gampee5eb7062014-12-12 18:44:19 -08001057 opcode1 = "paddq";
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001058 prefix[2] = 0;
1059 has_modrm = true;
1060 load = true;
1061 break;
Mark Mendellfe945782014-05-22 09:52:36 -04001062 case 0xDB:
1063 if (prefix[2] == 0x66) {
1064 src_reg_file = dst_reg_file = SSE;
1065 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
1066 } else {
1067 src_reg_file = dst_reg_file = MMX;
1068 }
Andreas Gampee5eb7062014-12-12 18:44:19 -08001069 opcode1 = "pand";
Mark Mendellfe945782014-05-22 09:52:36 -04001070 prefix[2] = 0;
1071 has_modrm = true;
1072 load = true;
1073 break;
1074 case 0xD5:
1075 if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001076 opcode1 = "pmullw";
Mark Mendellfe945782014-05-22 09:52:36 -04001077 prefix[2] = 0;
1078 has_modrm = true;
1079 load = true;
1080 src_reg_file = dst_reg_file = SSE;
1081 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001082 opcode_tmp = StringPrintf("unknown opcode '0F %02X'", *instr);
1083 opcode1 = opcode_tmp.c_str();
Mark Mendellfe945782014-05-22 09:52:36 -04001084 }
1085 break;
1086 case 0xEB:
1087 if (prefix[2] == 0x66) {
1088 src_reg_file = dst_reg_file = SSE;
1089 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
1090 } else {
1091 src_reg_file = dst_reg_file = MMX;
1092 }
Andreas Gampee5eb7062014-12-12 18:44:19 -08001093 opcode1 = "por";
Mark Mendellfe945782014-05-22 09:52:36 -04001094 prefix[2] = 0;
1095 has_modrm = true;
1096 load = true;
1097 break;
1098 case 0xEF:
1099 if (prefix[2] == 0x66) {
1100 src_reg_file = dst_reg_file = SSE;
1101 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
1102 } else {
1103 src_reg_file = dst_reg_file = MMX;
1104 }
Andreas Gampee5eb7062014-12-12 18:44:19 -08001105 opcode1 = "pxor";
Mark Mendellfe945782014-05-22 09:52:36 -04001106 prefix[2] = 0;
1107 has_modrm = true;
1108 load = true;
1109 break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001110 case 0xF4:
1111 case 0xF6:
Mark Mendellfe945782014-05-22 09:52:36 -04001112 case 0xF8:
Mark Mendellfe945782014-05-22 09:52:36 -04001113 case 0xF9:
Mark Mendellfe945782014-05-22 09:52:36 -04001114 case 0xFA:
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001115 case 0xFB:
Mark Mendellfe945782014-05-22 09:52:36 -04001116 case 0xFC:
Mark Mendellfe945782014-05-22 09:52:36 -04001117 case 0xFD:
Mark Mendellfe945782014-05-22 09:52:36 -04001118 case 0xFE:
1119 if (prefix[2] == 0x66) {
1120 src_reg_file = dst_reg_file = SSE;
1121 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
1122 } else {
1123 src_reg_file = dst_reg_file = MMX;
1124 }
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001125 switch (*instr) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001126 case 0xF4: opcode1 = "pmuludq"; break;
1127 case 0xF6: opcode1 = "psadbw"; break;
1128 case 0xF8: opcode1 = "psubb"; break;
1129 case 0xF9: opcode1 = "psubw"; break;
1130 case 0xFA: opcode1 = "psubd"; break;
1131 case 0xFB: opcode1 = "psubq"; break;
1132 case 0xFC: opcode1 = "paddb"; break;
1133 case 0xFD: opcode1 = "paddw"; break;
1134 case 0xFE: opcode1 = "paddd"; break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001135 }
Mark Mendellfe945782014-05-22 09:52:36 -04001136 prefix[2] = 0;
1137 has_modrm = true;
1138 load = true;
1139 break;
Ian Rogers706a10e2012-03-23 17:00:55 -07001140 default:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001141 opcode_tmp = StringPrintf("unknown opcode '0F %02X'", *instr);
1142 opcode1 = opcode_tmp.c_str();
Ian Rogers706a10e2012-03-23 17:00:55 -07001143 break;
1144 }
1145 break;
1146 case 0x80: case 0x81: case 0x82: case 0x83:
1147 static const char* x80_opcodes[] = {"add", "or", "adc", "sbb", "and", "sub", "xor", "cmp"};
1148 modrm_opcodes = x80_opcodes;
1149 has_modrm = true;
1150 reg_is_opcode = true;
1151 store = true;
1152 byte_operand = (*instr & 1) == 0;
1153 immediate_bytes = *instr == 0x81 ? 4 : 1;
1154 break;
jeffhao703f2cd2012-07-13 17:25:52 -07001155 case 0x84: case 0x85:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001156 opcode1 = "test";
jeffhao703f2cd2012-07-13 17:25:52 -07001157 has_modrm = true;
1158 load = true;
1159 byte_operand = (*instr & 1) == 0;
1160 break;
Ian Rogers7caad772012-03-30 01:07:54 -07001161 case 0x8D:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001162 opcode1 = "lea";
Ian Rogers7caad772012-03-30 01:07:54 -07001163 has_modrm = true;
1164 load = true;
1165 break;
jeffhao703f2cd2012-07-13 17:25:52 -07001166 case 0x8F:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001167 opcode1 = "pop";
jeffhao703f2cd2012-07-13 17:25:52 -07001168 has_modrm = true;
1169 reg_is_opcode = true;
1170 store = true;
1171 break;
Mark Mendell2bf31e62014-01-23 12:13:40 -08001172 case 0x99:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001173 opcode1 = "cdq";
Mark Mendell2bf31e62014-01-23 12:13:40 -08001174 break;
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001175 case 0x9B:
1176 if (instr[1] == 0xDF && instr[2] == 0xE0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001177 opcode1 = "fstsw\tax";
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001178 instr += 2;
1179 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001180 opcode_tmp = StringPrintf("unknown opcode '%02X'", *instr);
1181 opcode1 = opcode_tmp.c_str();
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001182 }
1183 break;
Mark Mendellb9c4bbe2015-07-01 14:26:52 -04001184 case 0xA5:
1185 opcode1 = (prefix[2] == 0x66 ? "movsw" : "movsl");
1186 break;
agicsaki124b3922015-07-30 13:40:13 -07001187 case 0xA7:
1188 opcode1 = (prefix[2] == 0x66 ? "cmpsw" : "cmpsl");
1189 break;
Mark Mendell4028a6c2014-02-19 20:06:20 -08001190 case 0xAF:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001191 opcode1 = (prefix[2] == 0x66 ? "scasw" : "scasl");
Mark Mendell4028a6c2014-02-19 20:06:20 -08001192 break;
Ian Rogers706a10e2012-03-23 17:00:55 -07001193 case 0xB0: case 0xB1: case 0xB2: case 0xB3: case 0xB4: case 0xB5: case 0xB6: case 0xB7:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001194 opcode1 = "mov";
Ian Rogers706a10e2012-03-23 17:00:55 -07001195 immediate_bytes = 1;
Mark Mendella33720c2014-06-18 21:02:29 -04001196 byte_operand = true;
Ian Rogers706a10e2012-03-23 17:00:55 -07001197 reg_in_opcode = true;
Vladimir Kostyukov79bb1842014-07-01 18:28:43 +07001198 byte_operand = true;
Ian Rogers706a10e2012-03-23 17:00:55 -07001199 break;
1200 case 0xB8: case 0xB9: case 0xBA: case 0xBB: case 0xBC: case 0xBD: case 0xBE: case 0xBF:
Vladimir Kostyukovec95f722014-07-23 12:10:07 +07001201 if ((rex & REX_W) != 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001202 opcode1 = "movabsq";
Yixin Shou5192cbb2014-07-01 13:48:17 -04001203 immediate_bytes = 8;
1204 reg_in_opcode = true;
1205 break;
1206 }
Andreas Gampee5eb7062014-12-12 18:44:19 -08001207 opcode1 = "mov";
Ian Rogers706a10e2012-03-23 17:00:55 -07001208 immediate_bytes = 4;
1209 reg_in_opcode = true;
1210 break;
Ian Rogers7caad772012-03-30 01:07:54 -07001211 case 0xC0: case 0xC1:
jeffhaoe2962482012-06-28 11:29:57 -07001212 case 0xD0: case 0xD1: case 0xD2: case 0xD3:
Ian Rogers7caad772012-03-30 01:07:54 -07001213 static const char* shift_opcodes[] =
1214 {"rol", "ror", "rcl", "rcr", "shl", "shr", "unknown-shift", "sar"};
1215 modrm_opcodes = shift_opcodes;
1216 has_modrm = true;
1217 reg_is_opcode = true;
1218 store = true;
Elliott Hughes16b5c292012-04-16 20:37:16 -07001219 immediate_bytes = ((*instr & 0xf0) == 0xc0) ? 1 : 0;
jeffhaoe2962482012-06-28 11:29:57 -07001220 cx = (*instr == 0xD2) || (*instr == 0xD3);
1221 byte_operand = (*instr == 0xC0);
Ian Rogers7caad772012-03-30 01:07:54 -07001222 break;
Andreas Gampee5eb7062014-12-12 18:44:19 -08001223 case 0xC3: opcode1 = "ret"; break;
Mark Mendella33720c2014-06-18 21:02:29 -04001224 case 0xC6:
Ian Rogers677c12f2014-11-07 16:58:38 -08001225 static const char* c6_opcodes[] = {"mov", "unknown-c6", "unknown-c6",
1226 "unknown-c6", "unknown-c6", "unknown-c6",
1227 "unknown-c6", "unknown-c6"};
Mark Mendella33720c2014-06-18 21:02:29 -04001228 modrm_opcodes = c6_opcodes;
1229 store = true;
1230 immediate_bytes = 1;
1231 has_modrm = true;
1232 reg_is_opcode = true;
1233 byte_operand = true;
1234 break;
Elliott Hughes0589ca92012-04-09 18:26:20 -07001235 case 0xC7:
Ian Rogers677c12f2014-11-07 16:58:38 -08001236 static const char* c7_opcodes[] = {"mov", "unknown-c7", "unknown-c7",
1237 "unknown-c7", "unknown-c7", "unknown-c7",
1238 "unknown-c7", "unknown-c7"};
Elliott Hughes0589ca92012-04-09 18:26:20 -07001239 modrm_opcodes = c7_opcodes;
1240 store = true;
1241 immediate_bytes = 4;
1242 has_modrm = true;
1243 reg_is_opcode = true;
1244 break;
Andreas Gampee5eb7062014-12-12 18:44:19 -08001245 case 0xCC: opcode1 = "int 3"; break;
Mark Mendelld19b55a2013-12-12 09:55:34 -08001246 case 0xD9:
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001247 if (instr[1] == 0xF8) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001248 opcode1 = "fprem";
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001249 instr++;
1250 } else {
1251 static const char* d9_opcodes[] = {"flds", "unknown-d9", "fsts", "fstps", "fldenv", "fldcw",
1252 "fnstenv", "fnstcw"};
1253 modrm_opcodes = d9_opcodes;
1254 store = true;
1255 has_modrm = true;
1256 reg_is_opcode = true;
1257 }
1258 break;
1259 case 0xDA:
1260 if (instr[1] == 0xE9) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001261 opcode1 = "fucompp";
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001262 instr++;
1263 } else {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001264 opcode_tmp = StringPrintf("unknown opcode '%02X'", *instr);
1265 opcode1 = opcode_tmp.c_str();
Vladimir Kostyukovd48b8a22014-06-24 16:40:19 +07001266 }
Mark Mendelld19b55a2013-12-12 09:55:34 -08001267 break;
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -08001268 case 0xDB:
Ian Rogers677c12f2014-11-07 16:58:38 -08001269 static const char* db_opcodes[] = {"fildl", "unknown-db", "unknown-db",
1270 "unknown-db", "unknown-db", "unknown-db",
1271 "unknown-db", "unknown-db"};
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -08001272 modrm_opcodes = db_opcodes;
1273 load = true;
1274 has_modrm = true;
1275 reg_is_opcode = true;
1276 break;
Mark Mendelld19b55a2013-12-12 09:55:34 -08001277 case 0xDD:
Ian Rogers677c12f2014-11-07 16:58:38 -08001278 static const char* dd_opcodes[] = {"fldl", "fisttp", "fstl",
1279 "fstpl", "frstor", "unknown-dd",
1280 "fnsave", "fnstsw"};
Mark Mendelld19b55a2013-12-12 09:55:34 -08001281 modrm_opcodes = dd_opcodes;
1282 store = true;
1283 has_modrm = true;
1284 reg_is_opcode = true;
1285 break;
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -08001286 case 0xDF:
Ian Rogers677c12f2014-11-07 16:58:38 -08001287 static const char* df_opcodes[] = {"fild", "unknown-df", "unknown-df",
1288 "unknown-df", "unknown-df", "fildll",
1289 "unknown-df", "unknown-df"};
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -08001290 modrm_opcodes = df_opcodes;
1291 load = true;
1292 has_modrm = true;
1293 reg_is_opcode = true;
1294 break;
Andreas Gampee5eb7062014-12-12 18:44:19 -08001295 case 0xE3: opcode1 = "jecxz"; branch_bytes = 1; break;
1296 case 0xE8: opcode1 = "call"; branch_bytes = 4; break;
1297 case 0xE9: opcode1 = "jmp"; branch_bytes = 4; break;
1298 case 0xEB: opcode1 = "jmp"; branch_bytes = 1; break;
1299 case 0xF5: opcode1 = "cmc"; break;
jeffhao174651d2012-04-19 15:27:22 -07001300 case 0xF6: case 0xF7:
Ian Rogers677c12f2014-11-07 16:58:38 -08001301 static const char* f7_opcodes[] = {
1302 "test", "unknown-f7", "not", "neg", "mul edx:eax, eax *",
1303 "imul edx:eax, eax *", "div edx:eax, edx:eax /",
1304 "idiv edx:eax, edx:eax /"};
jeffhao174651d2012-04-19 15:27:22 -07001305 modrm_opcodes = f7_opcodes;
1306 has_modrm = true;
1307 reg_is_opcode = true;
1308 store = true;
1309 immediate_bytes = ((instr[1] & 0x38) == 0) ? 1 : 0;
1310 break;
Ian Rogers706a10e2012-03-23 17:00:55 -07001311 case 0xFF:
Vladimir Kostyukove443a802014-06-30 15:44:12 +07001312 {
Ian Rogers677c12f2014-11-07 16:58:38 -08001313 static const char* ff_opcodes[] = {
1314 "inc", "dec", "call", "call",
1315 "jmp", "jmp", "push", "unknown-ff"};
Vladimir Kostyukove443a802014-06-30 15:44:12 +07001316 modrm_opcodes = ff_opcodes;
1317 has_modrm = true;
1318 reg_is_opcode = true;
1319 load = true;
1320 const uint8_t opcode_digit = (instr[1] >> 3) & 7;
1321 // 'call', 'jmp' and 'push' are target specific instructions
1322 if (opcode_digit == 2 || opcode_digit == 4 || opcode_digit == 6) {
1323 target_specific = true;
1324 }
1325 }
Ian Rogers706a10e2012-03-23 17:00:55 -07001326 break;
1327 default:
Andreas Gampee5eb7062014-12-12 18:44:19 -08001328 opcode_tmp = StringPrintf("unknown opcode '%02X'", *instr);
1329 opcode1 = opcode_tmp.c_str();
Ian Rogers706a10e2012-03-23 17:00:55 -07001330 break;
1331 }
1332 std::ostringstream args;
Vladimir Kostyukov122113a2014-05-30 17:56:23 +07001333 // We force the REX prefix to be available for 64-bit target
1334 // in order to dump addr (base/index) registers correctly.
1335 uint8_t rex64 = supports_rex_ ? (rex | 0x40) : rex;
Vladimir Kostyukove443a802014-06-30 15:44:12 +07001336 // REX.W should be forced for 64-target and target-specific instructions (i.e., push or pop).
1337 uint8_t rex_w = (supports_rex_ && target_specific) ? (rex | 0x48) : rex;
Ian Rogers706a10e2012-03-23 17:00:55 -07001338 if (reg_in_opcode) {
1339 DCHECK(!has_modrm);
Vladimir Kostyukov79bb1842014-07-01 18:28:43 +07001340 DumpOpcodeReg(args, rex_w, *instr & 0x7, byte_operand, prefix[2]);
Ian Rogers706a10e2012-03-23 17:00:55 -07001341 }
1342 instr++;
Elliott Hughes92301d92012-04-10 15:57:52 -07001343 uint32_t address_bits = 0;
Ian Rogers706a10e2012-03-23 17:00:55 -07001344 if (has_modrm) {
1345 uint8_t modrm = *instr;
1346 instr++;
1347 uint8_t mod = modrm >> 6;
1348 uint8_t reg_or_opcode = (modrm >> 3) & 7;
1349 uint8_t rm = modrm & 7;
Andreas Gampee5eb7062014-12-12 18:44:19 -08001350 std::string address = DumpAddress(mod, rm, rex64, rex_w, no_ops, byte_operand,
1351 byte_second_operand, prefix, load, src_reg_file, dst_reg_file,
1352 &instr, &address_bits);
Ian Rogers706a10e2012-03-23 17:00:55 -07001353
Ian Rogers677c12f2014-11-07 16:58:38 -08001354 if (reg_is_opcode && modrm_opcodes != nullptr) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001355 opcode3 = modrm_opcodes[reg_or_opcode];
Ian Rogers706a10e2012-03-23 17:00:55 -07001356 }
Mark Mendella33720c2014-06-18 21:02:29 -04001357
1358 // Add opcode suffixes to indicate size.
1359 if (byte_operand) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001360 opcode4 = "b";
Mark Mendella33720c2014-06-18 21:02:29 -04001361 } else if ((rex & REX_W) != 0) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001362 opcode4 = "q";
Mark Mendella33720c2014-06-18 21:02:29 -04001363 } else if (prefix[2] == 0x66) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001364 opcode4 = "w";
Mark Mendella33720c2014-06-18 21:02:29 -04001365 }
1366
Ian Rogers706a10e2012-03-23 17:00:55 -07001367 if (load) {
1368 if (!reg_is_opcode) {
Ian Rogersbf989802012-04-16 16:07:49 -07001369 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2], dst_reg_file);
Ian Rogers706a10e2012-03-23 17:00:55 -07001370 args << ", ";
1371 }
1372 DumpSegmentOverride(args, prefix[1]);
Andreas Gampee5eb7062014-12-12 18:44:19 -08001373 args << address;
Ian Rogers706a10e2012-03-23 17:00:55 -07001374 } else {
1375 DCHECK(store);
1376 DumpSegmentOverride(args, prefix[1]);
Andreas Gampee5eb7062014-12-12 18:44:19 -08001377 args << address;
Ian Rogers706a10e2012-03-23 17:00:55 -07001378 if (!reg_is_opcode) {
1379 args << ", ";
Ian Rogersbf989802012-04-16 16:07:49 -07001380 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2], src_reg_file);
Ian Rogers706a10e2012-03-23 17:00:55 -07001381 }
1382 }
1383 }
1384 if (ax) {
jeffhaofdffdf82012-07-11 16:08:43 -07001385 // If this opcode implicitly uses ax, ax is always the first arg.
Ian Rogersbf989802012-04-16 16:07:49 -07001386 DumpReg(args, rex, 0 /* EAX */, byte_operand, prefix[2], GPR);
Ian Rogers706a10e2012-03-23 17:00:55 -07001387 }
jeffhaoe2962482012-06-28 11:29:57 -07001388 if (cx) {
1389 args << ", ";
1390 DumpReg(args, rex, 1 /* ECX */, true, prefix[2], GPR);
1391 }
Ian Rogers706a10e2012-03-23 17:00:55 -07001392 if (immediate_bytes > 0) {
jeffhaoe2962482012-06-28 11:29:57 -07001393 if (has_modrm || reg_in_opcode || ax || cx) {
Ian Rogers706a10e2012-03-23 17:00:55 -07001394 args << ", ";
1395 }
1396 if (immediate_bytes == 1) {
1397 args << StringPrintf("%d", *reinterpret_cast<const int8_t*>(instr));
1398 instr++;
Yixin Shou5192cbb2014-07-01 13:48:17 -04001399 } else if (immediate_bytes == 4) {
Mark Mendell67d18be2014-05-30 15:05:09 -04001400 if (prefix[2] == 0x66) { // Operand size override from 32-bit to 16-bit.
1401 args << StringPrintf("%d", *reinterpret_cast<const int16_t*>(instr));
1402 instr += 2;
1403 } else {
1404 args << StringPrintf("%d", *reinterpret_cast<const int32_t*>(instr));
1405 instr += 4;
1406 }
Yixin Shou5192cbb2014-07-01 13:48:17 -04001407 } else {
1408 CHECK_EQ(immediate_bytes, 8u);
1409 args << StringPrintf("%" PRId64, *reinterpret_cast<const int64_t*>(instr));
1410 instr += 8;
Ian Rogers706a10e2012-03-23 17:00:55 -07001411 }
1412 } else if (branch_bytes > 0) {
1413 DCHECK(!has_modrm);
1414 int32_t displacement;
1415 if (branch_bytes == 1) {
1416 displacement = *reinterpret_cast<const int8_t*>(instr);
1417 instr++;
1418 } else {
1419 CHECK_EQ(branch_bytes, 4u);
1420 displacement = *reinterpret_cast<const int32_t*>(instr);
1421 instr += 4;
1422 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001423 args << StringPrintf("%+d (", displacement)
1424 << FormatInstructionPointer(instr + displacement)
1425 << ")";
Ian Rogers706a10e2012-03-23 17:00:55 -07001426 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001427 if (prefix[1] == kFs && !supports_rex_) {
Elliott Hughes92301d92012-04-10 15:57:52 -07001428 args << " ; ";
Andreas Gampe372f3a32016-08-19 10:49:06 -07001429 GetDisassemblerOptions()->thread_offset_name_function_(args, address_bits);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001430 }
1431 if (prefix[1] == kGs && supports_rex_) {
1432 args << " ; ";
Andreas Gampe372f3a32016-08-19 10:49:06 -07001433 GetDisassemblerOptions()->thread_offset_name_function_(args, address_bits);
Elliott Hughes92301d92012-04-10 15:57:52 -07001434 }
Andreas Gampee5eb7062014-12-12 18:44:19 -08001435 const char* prefix_str;
Ian Rogers5e588b32013-02-21 15:05:09 -08001436 switch (prefix[0]) {
Andreas Gampee5eb7062014-12-12 18:44:19 -08001437 case 0xF0: prefix_str = "lock "; break;
1438 case 0xF2: prefix_str = "repne "; break;
1439 case 0xF3: prefix_str = "repe "; break;
1440 case 0: prefix_str = ""; break;
Ian Rogers2c4257b2014-10-24 14:20:06 -07001441 default: LOG(FATAL) << "Unreachable"; UNREACHABLE();
Ian Rogers5e588b32013-02-21 15:05:09 -08001442 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001443 os << FormatInstructionPointer(begin_instr)
Andreas Gampee5eb7062014-12-12 18:44:19 -08001444 << StringPrintf(": %22s \t%-7s%s%s%s%s%s ", DumpCodeHex(begin_instr, instr).c_str(),
1445 prefix_str, opcode0, opcode1, opcode2, opcode3, opcode4)
Ian Rogers5e588b32013-02-21 15:05:09 -08001446 << args.str() << '\n';
Ian Rogers706a10e2012-03-23 17:00:55 -07001447 return instr - begin_instr;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001448} // NOLINT(readability/fn_size)
Ian Rogers706a10e2012-03-23 17:00:55 -07001449
1450} // namespace x86
1451} // namespace art