blob: 91a66d38e023b9ef0831c72061879da2c5df9b75 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -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 "codegen_x86.h"
18#include "dex/quick/mir_to_lir-inl.h"
19#include "x86_lir.h"
20
21namespace art {
22
23#define MAX_ASSEMBLER_RETRIES 50
24
25const X86EncodingMap X86Mir2Lir::EncodingMap[kX86Last] = {
26 { kX8632BitData, kData, IS_UNARY_OP, { 0, 0, 0x00, 0, 0, 0, 0, 4 }, "data", "0x!0d" },
27 { kX86Bkpt, kNullary, NO_OPERAND | IS_BRANCH, { 0, 0, 0xCC, 0, 0, 0, 0, 0 }, "int 3", "" },
Mark Mendell2637f2e2014-04-30 10:10:47 -040028 { kX86Nop, kNop, NO_OPERAND, { 0, 0, 0x90, 0, 0, 0, 0, 0 }, "nop", "" },
Brian Carlstrom7940e442013-07-12 13:46:57 -070029
30#define ENCODING_MAP(opname, mem_use, reg_def, uses_ccodes, \
31 rm8_r8, rm32_r32, \
32 r8_rm8, r32_rm32, \
33 ax8_i8, ax32_i32, \
34 rm8_i8, rm8_i8_modrm, \
35 rm32_i32, rm32_i32_modrm, \
36 rm32_i8, rm32_i8_modrm) \
37{ kX86 ## opname ## 8MR, kMemReg, mem_use | IS_TERTIARY_OP | REG_USE02 | SETS_CCODES | uses_ccodes, { 0, 0, rm8_r8, 0, 0, 0, 0, 0 }, #opname "8MR", "[!0r+!1d],!2r" }, \
38{ kX86 ## opname ## 8AR, kArrayReg, mem_use | IS_QUIN_OP | REG_USE014 | SETS_CCODES | uses_ccodes, { 0, 0, rm8_r8, 0, 0, 0, 0, 0 }, #opname "8AR", "[!0r+!1r<<!2d+!3d],!4r" }, \
39{ kX86 ## opname ## 8TR, kThreadReg, mem_use | IS_BINARY_OP | REG_USE1 | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0, rm8_r8, 0, 0, 0, 0, 0 }, #opname "8TR", "fs:[!0d],!1r" }, \
40{ kX86 ## opname ## 8RR, kRegReg, IS_BINARY_OP | reg_def | REG_USE01 | SETS_CCODES | uses_ccodes, { 0, 0, r8_rm8, 0, 0, 0, 0, 0 }, #opname "8RR", "!0r,!1r" }, \
41{ kX86 ## opname ## 8RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | reg_def | REG_USE01 | SETS_CCODES | uses_ccodes, { 0, 0, r8_rm8, 0, 0, 0, 0, 0 }, #opname "8RM", "!0r,[!1r+!2d]" }, \
42{ kX86 ## opname ## 8RA, kRegArray, IS_LOAD | IS_QUIN_OP | reg_def | REG_USE012 | SETS_CCODES | uses_ccodes, { 0, 0, r8_rm8, 0, 0, 0, 0, 0 }, #opname "8RA", "!0r,[!1r+!2r<<!3d+!4d]" }, \
43{ kX86 ## opname ## 8RT, kRegThread, IS_LOAD | IS_BINARY_OP | reg_def | REG_USE0 | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0, r8_rm8, 0, 0, 0, 0, 0 }, #opname "8RT", "!0r,fs:[!1d]" }, \
44{ kX86 ## opname ## 8RI, kRegImm, IS_BINARY_OP | reg_def | REG_USE0 | SETS_CCODES | uses_ccodes, { 0, 0, rm8_i8, 0, 0, rm8_i8_modrm, ax8_i8, 1 }, #opname "8RI", "!0r,!1d" }, \
45{ kX86 ## opname ## 8MI, kMemImm, mem_use | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES | uses_ccodes, { 0, 0, rm8_i8, 0, 0, rm8_i8_modrm, 0, 1 }, #opname "8MI", "[!0r+!1d],!2d" }, \
46{ kX86 ## opname ## 8AI, kArrayImm, mem_use | IS_QUIN_OP | REG_USE01 | SETS_CCODES | uses_ccodes, { 0, 0, rm8_i8, 0, 0, rm8_i8_modrm, 0, 1 }, #opname "8AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
47{ kX86 ## opname ## 8TI, kThreadImm, mem_use | IS_BINARY_OP | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0, rm8_i8, 0, 0, rm8_i8_modrm, 0, 1 }, #opname "8TI", "fs:[!0d],!1d" }, \
48 \
49{ kX86 ## opname ## 16MR, kMemReg, mem_use | IS_TERTIARY_OP | REG_USE02 | SETS_CCODES | uses_ccodes, { 0x66, 0, rm32_r32, 0, 0, 0, 0, 0 }, #opname "16MR", "[!0r+!1d],!2r" }, \
50{ kX86 ## opname ## 16AR, kArrayReg, mem_use | IS_QUIN_OP | REG_USE014 | SETS_CCODES | uses_ccodes, { 0x66, 0, rm32_r32, 0, 0, 0, 0, 0 }, #opname "16AR", "[!0r+!1r<<!2d+!3d],!4r" }, \
51{ kX86 ## opname ## 16TR, kThreadReg, mem_use | IS_BINARY_OP | REG_USE1 | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0x66, rm32_r32, 0, 0, 0, 0, 0 }, #opname "16TR", "fs:[!0d],!1r" }, \
52{ kX86 ## opname ## 16RR, kRegReg, IS_BINARY_OP | reg_def | REG_USE01 | SETS_CCODES | uses_ccodes, { 0x66, 0, r32_rm32, 0, 0, 0, 0, 0 }, #opname "16RR", "!0r,!1r" }, \
53{ kX86 ## opname ## 16RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | reg_def | REG_USE01 | SETS_CCODES | uses_ccodes, { 0x66, 0, r32_rm32, 0, 0, 0, 0, 0 }, #opname "16RM", "!0r,[!1r+!2d]" }, \
54{ kX86 ## opname ## 16RA, kRegArray, IS_LOAD | IS_QUIN_OP | reg_def | REG_USE012 | SETS_CCODES | uses_ccodes, { 0x66, 0, r32_rm32, 0, 0, 0, 0, 0 }, #opname "16RA", "!0r,[!1r+!2r<<!3d+!4d]" }, \
55{ kX86 ## opname ## 16RT, kRegThread, IS_LOAD | IS_BINARY_OP | reg_def | REG_USE0 | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0x66, r32_rm32, 0, 0, 0, 0, 0 }, #opname "16RT", "!0r,fs:[!1d]" }, \
56{ kX86 ## opname ## 16RI, kRegImm, IS_BINARY_OP | reg_def | REG_USE0 | SETS_CCODES | uses_ccodes, { 0x66, 0, rm32_i32, 0, 0, rm32_i32_modrm, ax32_i32, 2 }, #opname "16RI", "!0r,!1d" }, \
57{ kX86 ## opname ## 16MI, kMemImm, mem_use | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES | uses_ccodes, { 0x66, 0, rm32_i32, 0, 0, rm32_i32_modrm, 0, 2 }, #opname "16MI", "[!0r+!1d],!2d" }, \
58{ kX86 ## opname ## 16AI, kArrayImm, mem_use | IS_QUIN_OP | REG_USE01 | SETS_CCODES | uses_ccodes, { 0x66, 0, rm32_i32, 0, 0, rm32_i32_modrm, 0, 2 }, #opname "16AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
59{ kX86 ## opname ## 16TI, kThreadImm, mem_use | IS_BINARY_OP | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0x66, rm32_i32, 0, 0, rm32_i32_modrm, 0, 2 }, #opname "16TI", "fs:[!0d],!1d" }, \
60{ kX86 ## opname ## 16RI8, kRegImm, IS_BINARY_OP | reg_def | REG_USE0 | SETS_CCODES | uses_ccodes, { 0x66, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "16RI8", "!0r,!1d" }, \
61{ kX86 ## opname ## 16MI8, kMemImm, mem_use | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES | uses_ccodes, { 0x66, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "16MI8", "[!0r+!1d],!2d" }, \
62{ kX86 ## opname ## 16AI8, kArrayImm, mem_use | IS_QUIN_OP | REG_USE01 | SETS_CCODES | uses_ccodes, { 0x66, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "16AI8", "[!0r+!1r<<!2d+!3d],!4d" }, \
63{ kX86 ## opname ## 16TI8, kThreadImm, mem_use | IS_BINARY_OP | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0x66, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "16TI8", "fs:[!0d],!1d" }, \
64 \
65{ kX86 ## opname ## 32MR, kMemReg, mem_use | IS_TERTIARY_OP | REG_USE02 | SETS_CCODES | uses_ccodes, { 0, 0, rm32_r32, 0, 0, 0, 0, 0 }, #opname "32MR", "[!0r+!1d],!2r" }, \
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070066{ kX86 ## opname ## 64MR, kMemReg64, mem_use | IS_TERTIARY_OP | REG_USE02 | SETS_CCODES | uses_ccodes, { REX_W, 0, rm32_r32, 0, 0, 0, 0, 0 }, #opname "64MR", "[!0r+!1d],!2r" }, \
Brian Carlstrom7940e442013-07-12 13:46:57 -070067{ kX86 ## opname ## 32AR, kArrayReg, mem_use | IS_QUIN_OP | REG_USE014 | SETS_CCODES | uses_ccodes, { 0, 0, rm32_r32, 0, 0, 0, 0, 0 }, #opname "32AR", "[!0r+!1r<<!2d+!3d],!4r" }, \
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070068{ kX86 ## opname ## 64AR, kArrayReg64, mem_use | IS_QUIN_OP | REG_USE014 | SETS_CCODES | uses_ccodes, { REX_W, 0, rm32_r32, 0, 0, 0, 0, 0 }, #opname "64AR", "[!0r+!1r<<!2d+!3d],!4r" }, \
Brian Carlstrom7940e442013-07-12 13:46:57 -070069{ kX86 ## opname ## 32TR, kThreadReg, mem_use | IS_BINARY_OP | REG_USE1 | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0, rm32_r32, 0, 0, 0, 0, 0 }, #opname "32TR", "fs:[!0d],!1r" }, \
70{ kX86 ## opname ## 32RR, kRegReg, IS_BINARY_OP | reg_def | REG_USE01 | SETS_CCODES | uses_ccodes, { 0, 0, r32_rm32, 0, 0, 0, 0, 0 }, #opname "32RR", "!0r,!1r" }, \
71{ kX86 ## opname ## 32RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | reg_def | REG_USE01 | SETS_CCODES | uses_ccodes, { 0, 0, r32_rm32, 0, 0, 0, 0, 0 }, #opname "32RM", "!0r,[!1r+!2d]" }, \
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070072{ kX86 ## opname ## 64RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | reg_def | REG_USE01 | SETS_CCODES | uses_ccodes, { REX_W, 0, r32_rm32, 0, 0, 0, 0, 0 }, #opname "64RM", "!0r,[!1r+!2d]" }, \
Brian Carlstrom7940e442013-07-12 13:46:57 -070073{ kX86 ## opname ## 32RA, kRegArray, IS_LOAD | IS_QUIN_OP | reg_def | REG_USE012 | SETS_CCODES | uses_ccodes, { 0, 0, r32_rm32, 0, 0, 0, 0, 0 }, #opname "32RA", "!0r,[!1r+!2r<<!3d+!4d]" }, \
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070074{ kX86 ## opname ## 64RA, kRegArray, IS_LOAD | IS_QUIN_OP | reg_def | REG_USE012 | SETS_CCODES | uses_ccodes, { REX_W, 0, r32_rm32, 0, 0, 0, 0, 0 }, #opname "64RA", "!0r,[!1r+!2r<<!3d+!4d]" }, \
Brian Carlstrom7940e442013-07-12 13:46:57 -070075{ kX86 ## opname ## 32RT, kRegThread, IS_LOAD | IS_BINARY_OP | reg_def | REG_USE0 | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0, r32_rm32, 0, 0, 0, 0, 0 }, #opname "32RT", "!0r,fs:[!1d]" }, \
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070076{ kX86 ## opname ## 64RT, kReg64Thread, IS_LOAD | IS_BINARY_OP | reg_def | REG_USE0 | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, REX_W, r32_rm32, 0, 0, 0, 0, 0 }, #opname "64RT", "!0r,fs:[!1d]" }, \
Brian Carlstrom7940e442013-07-12 13:46:57 -070077{ kX86 ## opname ## 32RI, kRegImm, IS_BINARY_OP | reg_def | REG_USE0 | SETS_CCODES | uses_ccodes, { 0, 0, rm32_i32, 0, 0, rm32_i32_modrm, ax32_i32, 4 }, #opname "32RI", "!0r,!1d" }, \
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070078{ kX86 ## opname ## 64RI, kReg64Imm, IS_BINARY_OP | reg_def | REG_USE0 | SETS_CCODES | uses_ccodes, { REX_W, 0, rm32_i32, 0, 0, rm32_i32_modrm, ax32_i32, 4 }, #opname "32RI", "!0r,!1d" }, \
Brian Carlstrom7940e442013-07-12 13:46:57 -070079{ kX86 ## opname ## 32MI, kMemImm, mem_use | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES | uses_ccodes, { 0, 0, rm32_i32, 0, 0, rm32_i32_modrm, 0, 4 }, #opname "32MI", "[!0r+!1d],!2d" }, \
80{ kX86 ## opname ## 32AI, kArrayImm, mem_use | IS_QUIN_OP | REG_USE01 | SETS_CCODES | uses_ccodes, { 0, 0, rm32_i32, 0, 0, rm32_i32_modrm, 0, 4 }, #opname "32AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
81{ kX86 ## opname ## 32TI, kThreadImm, mem_use | IS_BINARY_OP | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0, rm32_i32, 0, 0, rm32_i32_modrm, 0, 4 }, #opname "32TI", "fs:[!0d],!1d" }, \
82{ kX86 ## opname ## 32RI8, kRegImm, IS_BINARY_OP | reg_def | REG_USE0 | SETS_CCODES | uses_ccodes, { 0, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "32RI8", "!0r,!1d" }, \
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070083{ kX86 ## opname ## 64RI8, kReg64Imm, IS_BINARY_OP | reg_def | REG_USE0 | SETS_CCODES | uses_ccodes, { REX_W, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "64RI8", "!0r,!1d" }, \
Brian Carlstrom7940e442013-07-12 13:46:57 -070084{ kX86 ## opname ## 32MI8, kMemImm, mem_use | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES | uses_ccodes, { 0, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "32MI8", "[!0r+!1d],!2d" }, \
85{ kX86 ## opname ## 32AI8, kArrayImm, mem_use | IS_QUIN_OP | REG_USE01 | SETS_CCODES | uses_ccodes, { 0, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "32AI8", "[!0r+!1r<<!2d+!3d],!4d" }, \
86{ kX86 ## opname ## 32TI8, kThreadImm, mem_use | IS_BINARY_OP | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "32TI8", "fs:[!0d],!1d" }
87
88ENCODING_MAP(Add, IS_LOAD | IS_STORE, REG_DEF0, 0,
89 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */,
90 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */,
91 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */,
92 0x80, 0x0 /* RegMem8/imm8 */,
93 0x81, 0x0 /* RegMem32/imm32 */, 0x83, 0x0 /* RegMem32/imm8 */),
94ENCODING_MAP(Or, IS_LOAD | IS_STORE, REG_DEF0, 0,
95 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */,
96 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */,
97 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */,
98 0x80, 0x1 /* RegMem8/imm8 */,
99 0x81, 0x1 /* RegMem32/imm32 */, 0x83, 0x1 /* RegMem32/imm8 */),
100ENCODING_MAP(Adc, IS_LOAD | IS_STORE, REG_DEF0, USES_CCODES,
101 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */,
102 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */,
103 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */,
104 0x80, 0x2 /* RegMem8/imm8 */,
105 0x81, 0x2 /* RegMem32/imm32 */, 0x83, 0x2 /* RegMem32/imm8 */),
106ENCODING_MAP(Sbb, IS_LOAD | IS_STORE, REG_DEF0, USES_CCODES,
107 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */,
108 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */,
109 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */,
110 0x80, 0x3 /* RegMem8/imm8 */,
111 0x81, 0x3 /* RegMem32/imm32 */, 0x83, 0x3 /* RegMem32/imm8 */),
112ENCODING_MAP(And, IS_LOAD | IS_STORE, REG_DEF0, 0,
113 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */,
114 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */,
115 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */,
116 0x80, 0x4 /* RegMem8/imm8 */,
117 0x81, 0x4 /* RegMem32/imm32 */, 0x83, 0x4 /* RegMem32/imm8 */),
118ENCODING_MAP(Sub, IS_LOAD | IS_STORE, REG_DEF0, 0,
119 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */,
120 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */,
121 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */,
122 0x80, 0x5 /* RegMem8/imm8 */,
123 0x81, 0x5 /* RegMem32/imm32 */, 0x83, 0x5 /* RegMem32/imm8 */),
124ENCODING_MAP(Xor, IS_LOAD | IS_STORE, REG_DEF0, 0,
125 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */,
126 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */,
127 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */,
128 0x80, 0x6 /* RegMem8/imm8 */,
129 0x81, 0x6 /* RegMem32/imm32 */, 0x83, 0x6 /* RegMem32/imm8 */),
130ENCODING_MAP(Cmp, IS_LOAD, 0, 0,
131 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */,
132 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */,
133 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */,
134 0x80, 0x7 /* RegMem8/imm8 */,
135 0x81, 0x7 /* RegMem32/imm32 */, 0x83, 0x7 /* RegMem32/imm8 */),
136#undef ENCODING_MAP
137
138 { kX86Imul16RRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { 0x66, 0, 0x69, 0, 0, 0, 0, 2 }, "Imul16RRI", "!0r,!1r,!2d" },
139 { kX86Imul16RMI, kRegMemImm, IS_LOAD | IS_QUAD_OP | REG_DEF0_USE1 | SETS_CCODES, { 0x66, 0, 0x69, 0, 0, 0, 0, 2 }, "Imul16RMI", "!0r,[!1r+!2d],!3d" },
140 { kX86Imul16RAI, kRegArrayImm, IS_LOAD | IS_SEXTUPLE_OP | REG_DEF0_USE12 | SETS_CCODES, { 0x66, 0, 0x69, 0, 0, 0, 0, 2 }, "Imul16RAI", "!0r,[!1r+!2r<<!3d+!4d],!5d" },
141
142 { kX86Imul32RRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { 0, 0, 0x69, 0, 0, 0, 0, 4 }, "Imul32RRI", "!0r,!1r,!2d" },
143 { kX86Imul32RMI, kRegMemImm, IS_LOAD | IS_QUAD_OP | REG_DEF0_USE1 | SETS_CCODES, { 0, 0, 0x69, 0, 0, 0, 0, 4 }, "Imul32RMI", "!0r,[!1r+!2d],!3d" },
144 { kX86Imul32RAI, kRegArrayImm, IS_LOAD | IS_SEXTUPLE_OP | REG_DEF0_USE12 | SETS_CCODES, { 0, 0, 0x69, 0, 0, 0, 0, 4 }, "Imul32RAI", "!0r,[!1r+!2r<<!3d+!4d],!5d" },
145 { kX86Imul32RRI8, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { 0, 0, 0x6B, 0, 0, 0, 0, 1 }, "Imul32RRI8", "!0r,!1r,!2d" },
146 { kX86Imul32RMI8, kRegMemImm, IS_LOAD | IS_QUAD_OP | REG_DEF0_USE1 | SETS_CCODES, { 0, 0, 0x6B, 0, 0, 0, 0, 1 }, "Imul32RMI8", "!0r,[!1r+!2d],!3d" },
147 { kX86Imul32RAI8, kRegArrayImm, IS_LOAD | IS_SEXTUPLE_OP | REG_DEF0_USE12 | SETS_CCODES, { 0, 0, 0x6B, 0, 0, 0, 0, 1 }, "Imul32RAI8", "!0r,[!1r+!2r<<!3d+!4d],!5d" },
148
149 { kX86Mov8MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0, 0, 0x88, 0, 0, 0, 0, 0 }, "Mov8MR", "[!0r+!1d],!2r" },
150 { kX86Mov8AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0, 0, 0x88, 0, 0, 0, 0, 0 }, "Mov8AR", "[!0r+!1r<<!2d+!3d],!4r" },
151 { kX86Mov8TR, kThreadReg, IS_STORE | IS_BINARY_OP | REG_USE1, { THREAD_PREFIX, 0, 0x88, 0, 0, 0, 0, 0 }, "Mov8TR", "fs:[!0d],!1r" },
152 { kX86Mov8RR, kRegReg, IS_BINARY_OP | REG_DEF0_USE1, { 0, 0, 0x8A, 0, 0, 0, 0, 0 }, "Mov8RR", "!0r,!1r" },
153 { kX86Mov8RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { 0, 0, 0x8A, 0, 0, 0, 0, 0 }, "Mov8RM", "!0r,[!1r+!2d]" },
154 { kX86Mov8RA, kRegArray, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8A, 0, 0, 0, 0, 0 }, "Mov8RA", "!0r,[!1r+!2r<<!3d+!4d]" },
155 { kX86Mov8RT, kRegThread, IS_LOAD | IS_BINARY_OP | REG_DEF0, { THREAD_PREFIX, 0, 0x8A, 0, 0, 0, 0, 0 }, "Mov8RT", "!0r,fs:[!1d]" },
156 { kX86Mov8RI, kMovRegImm, IS_BINARY_OP | REG_DEF0, { 0, 0, 0xB0, 0, 0, 0, 0, 1 }, "Mov8RI", "!0r,!1d" },
157 { kX86Mov8MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { 0, 0, 0xC6, 0, 0, 0, 0, 1 }, "Mov8MI", "[!0r+!1d],!2d" },
158 { kX86Mov8AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { 0, 0, 0xC6, 0, 0, 0, 0, 1 }, "Mov8AI", "[!0r+!1r<<!2d+!3d],!4d" },
159 { kX86Mov8TI, kThreadImm, IS_STORE | IS_BINARY_OP, { THREAD_PREFIX, 0, 0xC6, 0, 0, 0, 0, 1 }, "Mov8TI", "fs:[!0d],!1d" },
160
161 { kX86Mov16MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov16MR", "[!0r+!1d],!2r" },
162 { kX86Mov16AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x66, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov16AR", "[!0r+!1r<<!2d+!3d],!4r" },
163 { kX86Mov16TR, kThreadReg, IS_STORE | IS_BINARY_OP | REG_USE1, { THREAD_PREFIX, 0x66, 0x89, 0, 0, 0, 0, 0 }, "Mov16TR", "fs:[!0d],!1r" },
164 { kX86Mov16RR, kRegReg, IS_BINARY_OP | REG_DEF0_USE1, { 0x66, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov16RR", "!0r,!1r" },
165 { kX86Mov16RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { 0x66, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov16RM", "!0r,[!1r+!2d]" },
166 { kX86Mov16RA, kRegArray, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0x66, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov16RA", "!0r,[!1r+!2r<<!3d+!4d]" },
167 { kX86Mov16RT, kRegThread, IS_LOAD | IS_BINARY_OP | REG_DEF0, { THREAD_PREFIX, 0x66, 0x8B, 0, 0, 0, 0, 0 }, "Mov16RT", "!0r,fs:[!1d]" },
168 { kX86Mov16RI, kMovRegImm, IS_BINARY_OP | REG_DEF0, { 0x66, 0, 0xB8, 0, 0, 0, 0, 2 }, "Mov16RI", "!0r,!1d" },
169 { kX86Mov16MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { 0x66, 0, 0xC7, 0, 0, 0, 0, 2 }, "Mov16MI", "[!0r+!1d],!2d" },
170 { kX86Mov16AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { 0x66, 0, 0xC7, 0, 0, 0, 0, 2 }, "Mov16AI", "[!0r+!1r<<!2d+!3d],!4d" },
171 { kX86Mov16TI, kThreadImm, IS_STORE | IS_BINARY_OP, { THREAD_PREFIX, 0x66, 0xC7, 0, 0, 0, 0, 2 }, "Mov16TI", "fs:[!0d],!1d" },
172
173 { kX86Mov32MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov32MR", "[!0r+!1d],!2r" },
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700174 { kX86Mov64MR, kMemReg64, IS_STORE | IS_TERTIARY_OP | REG_USE02, { REX_W, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov64MR", "[!0r+!1d],!2r" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700175 { kX86Mov32AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov32AR", "[!0r+!1r<<!2d+!3d],!4r" },
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700176 { kX86Mov64AR, kArrayReg64, IS_STORE | IS_QUIN_OP | REG_USE014, { REX_W, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov64AR", "[!0r+!1r<<!2d+!3d],!4r" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700177 { kX86Mov32TR, kThreadReg, IS_STORE | IS_BINARY_OP | REG_USE1, { THREAD_PREFIX, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov32TR", "fs:[!0d],!1r" },
178 { kX86Mov32RR, kRegReg, IS_BINARY_OP | REG_DEF0_USE1, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov32RR", "!0r,!1r" },
179 { kX86Mov32RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov32RM", "!0r,[!1r+!2d]" },
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700180 { kX86Mov64RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { REX_W, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov64RM", "!0r,[!1r+!2d]" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181 { kX86Mov32RA, kRegArray, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov32RA", "!0r,[!1r+!2r<<!3d+!4d]" },
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700182 { kX86Mov64RA, kRegArray, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { REX_W, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov64RA", "!0r,[!1r+!2r<<!3d+!4d]" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183 { kX86Mov32RT, kRegThread, IS_LOAD | IS_BINARY_OP | REG_DEF0, { THREAD_PREFIX, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov32RT", "!0r,fs:[!1d]" },
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700184 { kX86Mov64RT, kRegThread, IS_LOAD | IS_BINARY_OP | REG_DEF0, { THREAD_PREFIX, REX_W, 0x8B, 0, 0, 0, 0, 0 }, "Mov64RT", "!0r,fs:[!1d]" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700185 { kX86Mov32RI, kMovRegImm, IS_BINARY_OP | REG_DEF0, { 0, 0, 0xB8, 0, 0, 0, 0, 4 }, "Mov32RI", "!0r,!1d" },
186 { kX86Mov32MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { 0, 0, 0xC7, 0, 0, 0, 0, 4 }, "Mov32MI", "[!0r+!1d],!2d" },
187 { kX86Mov32AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { 0, 0, 0xC7, 0, 0, 0, 0, 4 }, "Mov32AI", "[!0r+!1r<<!2d+!3d],!4d" },
188 { kX86Mov32TI, kThreadImm, IS_STORE | IS_BINARY_OP, { THREAD_PREFIX, 0, 0xC7, 0, 0, 0, 0, 4 }, "Mov32TI", "fs:[!0d],!1d" },
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700189 { kX86Mov64TI, kThreadImm, IS_STORE | IS_BINARY_OP, { THREAD_PREFIX, REX_W, 0xC7, 0, 0, 0, 0, 4 }, "Mov64TI", "fs:[!0d],!1d" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700190
Mark Mendell2637f2e2014-04-30 10:10:47 -0400191 { kX86Lea32RM, kRegMem, IS_TERTIARY_OP | IS_LOAD | REG_DEF0_USE1, { 0, 0, 0x8D, 0, 0, 0, 0, 0 }, "Lea32RM", "!0r,[!1r+!2d]" },
Mark Mendell4028a6c2014-02-19 20:06:20 -0800192
Brian Carlstrom7940e442013-07-12 13:46:57 -0700193 { kX86Lea32RA, kRegArray, IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8D, 0, 0, 0, 0, 0 }, "Lea32RA", "!0r,[!1r+!2r<<!3d+!4d]" },
194
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800195 { kX86Cmov32RRC, kRegRegCond, IS_TERTIARY_OP | REG_DEF0_USE01 | USES_CCODES, {0, 0, 0x0F, 0x40, 0, 0, 0, 0}, "Cmovcc32RR", "!2c !0r,!1r" },
196
Mark Mendell2637f2e2014-04-30 10:10:47 -0400197 { kX86Cmov32RMC, kRegMemCond, IS_QUAD_OP | IS_LOAD | REG_DEF0_USE01 | USES_CCODES, {0, 0, 0x0F, 0x40, 0, 0, 0, 0}, "Cmovcc32RM", "!3c !0r,[!1r+!2d]" },
198
Brian Carlstrom7940e442013-07-12 13:46:57 -0700199#define SHIFT_ENCODING_MAP(opname, modrm_opcode) \
200{ kX86 ## opname ## 8RI, kShiftRegImm, IS_BINARY_OP | REG_DEF0_USE0 | SETS_CCODES, { 0, 0, 0xC0, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "8RI", "!0r,!1d" }, \
201{ kX86 ## opname ## 8MI, kShiftMemImm, IS_LOAD | IS_STORE | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xC0, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "8MI", "[!0r+!1d],!2d" }, \
202{ kX86 ## opname ## 8AI, kShiftArrayImm, IS_LOAD | IS_STORE | IS_QUIN_OP | REG_USE01 | SETS_CCODES, { 0, 0, 0xC0, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "8AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
203{ kX86 ## opname ## 8RC, kShiftRegCl, IS_BINARY_OP | REG_DEF0_USE0 | REG_USEC | SETS_CCODES, { 0, 0, 0xD2, 0, 0, modrm_opcode, 0, 1 }, #opname "8RC", "!0r,cl" }, \
204{ kX86 ## opname ## 8MC, kShiftMemCl, IS_LOAD | IS_STORE | IS_TERTIARY_OP | REG_USE0 | REG_USEC | SETS_CCODES, { 0, 0, 0xD2, 0, 0, modrm_opcode, 0, 1 }, #opname "8MC", "[!0r+!1d],cl" }, \
205{ kX86 ## opname ## 8AC, kShiftArrayCl, IS_LOAD | IS_STORE | IS_QUIN_OP | REG_USE01 | REG_USEC | SETS_CCODES, { 0, 0, 0xD2, 0, 0, modrm_opcode, 0, 1 }, #opname "8AC", "[!0r+!1r<<!2d+!3d],cl" }, \
206 \
207{ kX86 ## opname ## 16RI, kShiftRegImm, IS_BINARY_OP | REG_DEF0_USE0 | SETS_CCODES, { 0x66, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "16RI", "!0r,!1d" }, \
208{ kX86 ## opname ## 16MI, kShiftMemImm, IS_LOAD | IS_STORE | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0x66, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "16MI", "[!0r+!1d],!2d" }, \
209{ kX86 ## opname ## 16AI, kShiftArrayImm, IS_LOAD | IS_STORE | IS_QUIN_OP | REG_USE01 | SETS_CCODES, { 0x66, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "16AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
210{ kX86 ## opname ## 16RC, kShiftRegCl, IS_BINARY_OP | REG_DEF0_USE0 | REG_USEC | SETS_CCODES, { 0x66, 0, 0xD3, 0, 0, modrm_opcode, 0, 1 }, #opname "16RC", "!0r,cl" }, \
211{ kX86 ## opname ## 16MC, kShiftMemCl, IS_LOAD | IS_STORE | IS_TERTIARY_OP | REG_USE0 | REG_USEC | SETS_CCODES, { 0x66, 0, 0xD3, 0, 0, modrm_opcode, 0, 1 }, #opname "16MC", "[!0r+!1d],cl" }, \
212{ kX86 ## opname ## 16AC, kShiftArrayCl, IS_LOAD | IS_STORE | IS_QUIN_OP | REG_USE01 | REG_USEC | SETS_CCODES, { 0x66, 0, 0xD3, 0, 0, modrm_opcode, 0, 1 }, #opname "16AC", "[!0r+!1r<<!2d+!3d],cl" }, \
213 \
214{ kX86 ## opname ## 32RI, kShiftRegImm, IS_BINARY_OP | REG_DEF0_USE0 | SETS_CCODES, { 0, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "32RI", "!0r,!1d" }, \
215{ kX86 ## opname ## 32MI, kShiftMemImm, IS_LOAD | IS_STORE | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "32MI", "[!0r+!1d],!2d" }, \
216{ kX86 ## opname ## 32AI, kShiftArrayImm, IS_LOAD | IS_STORE | IS_QUIN_OP | REG_USE01 | SETS_CCODES, { 0, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "32AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
217{ kX86 ## opname ## 32RC, kShiftRegCl, IS_BINARY_OP | REG_DEF0_USE0 | REG_USEC | SETS_CCODES, { 0, 0, 0xD3, 0, 0, modrm_opcode, 0, 0 }, #opname "32RC", "!0r,cl" }, \
218{ kX86 ## opname ## 32MC, kShiftMemCl, IS_LOAD | IS_STORE | IS_TERTIARY_OP | REG_USE0 | REG_USEC | SETS_CCODES, { 0, 0, 0xD3, 0, 0, modrm_opcode, 0, 0 }, #opname "32MC", "[!0r+!1d],cl" }, \
219{ kX86 ## opname ## 32AC, kShiftArrayCl, IS_LOAD | IS_STORE | IS_QUIN_OP | REG_USE01 | REG_USEC | SETS_CCODES, { 0, 0, 0xD3, 0, 0, modrm_opcode, 0, 0 }, #opname "32AC", "[!0r+!1r<<!2d+!3d],cl" }
220
221 SHIFT_ENCODING_MAP(Rol, 0x0),
222 SHIFT_ENCODING_MAP(Ror, 0x1),
223 SHIFT_ENCODING_MAP(Rcl, 0x2),
224 SHIFT_ENCODING_MAP(Rcr, 0x3),
225 SHIFT_ENCODING_MAP(Sal, 0x4),
226 SHIFT_ENCODING_MAP(Shr, 0x5),
227 SHIFT_ENCODING_MAP(Sar, 0x7),
228#undef SHIFT_ENCODING_MAP
229
230 { kX86Cmc, kNullary, NO_OPERAND, { 0, 0, 0xF5, 0, 0, 0, 0, 0}, "Cmc", "" },
Mark Mendell2637f2e2014-04-30 10:10:47 -0400231 { kX86Shld32RRI, kRegRegImmRev, IS_TERTIARY_OP | REG_DEF0_USE01 | SETS_CCODES, { 0, 0, 0x0F, 0xA4, 0, 0, 0, 1}, "Shld32RRI", "!0r,!1r,!2d" },
232 { kX86Shld32MRI, kMemRegImm, IS_QUAD_OP | REG_USE02 | IS_LOAD | IS_STORE | SETS_CCODES, { 0, 0, 0x0F, 0xA4, 0, 0, 0, 1}, "Shld32MRI", "[!0r+!1d],!2r,!3d" },
233 { kX86Shrd32RRI, kRegRegImmRev, IS_TERTIARY_OP | REG_DEF0_USE01 | SETS_CCODES, { 0, 0, 0x0F, 0xAC, 0, 0, 0, 1}, "Shrd32RRI", "!0r,!1r,!2d" },
234 { kX86Shrd32MRI, kMemRegImm, IS_QUAD_OP | REG_USE02 | IS_LOAD | IS_STORE | SETS_CCODES, { 0, 0, 0x0F, 0xAC, 0, 0, 0, 1}, "Shrd32MRI", "[!0r+!1d],!2r,!3d" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700235
236 { kX86Test8RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF6, 0, 0, 0, 0, 1}, "Test8RI", "!0r,!1d" },
237 { kX86Test8MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF6, 0, 0, 0, 0, 1}, "Test8MI", "[!0r+!1d],!2d" },
238 { kX86Test8AI, kArrayImm, IS_LOAD | IS_QUIN_OP | REG_USE01 | SETS_CCODES, { 0, 0, 0xF6, 0, 0, 0, 0, 1}, "Test8AI", "[!0r+!1r<<!2d+!3d],!4d" },
239 { kX86Test16RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0x66, 0, 0xF7, 0, 0, 0, 0, 2}, "Test16RI", "!0r,!1d" },
240 { kX86Test16MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0x66, 0, 0xF7, 0, 0, 0, 0, 2}, "Test16MI", "[!0r+!1d],!2d" },
241 { kX86Test16AI, kArrayImm, IS_LOAD | IS_QUIN_OP | REG_USE01 | SETS_CCODES, { 0x66, 0, 0xF7, 0, 0, 0, 0, 2}, "Test16AI", "[!0r+!1r<<!2d+!3d],!4d" },
242 { kX86Test32RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF7, 0, 0, 0, 0, 4}, "Test32RI", "!0r,!1d" },
243 { kX86Test32MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF7, 0, 0, 0, 0, 4}, "Test32MI", "[!0r+!1d],!2d" },
244 { kX86Test32AI, kArrayImm, IS_LOAD | IS_QUIN_OP | REG_USE01 | SETS_CCODES, { 0, 0, 0xF7, 0, 0, 0, 0, 4}, "Test32AI", "[!0r+!1r<<!2d+!3d],!4d" },
245 { kX86Test32RR, kRegReg, IS_BINARY_OP | REG_USE01 | SETS_CCODES, { 0, 0, 0x85, 0, 0, 0, 0, 0}, "Test32RR", "!0r,!1r" },
246
247#define UNARY_ENCODING_MAP(opname, modrm, is_store, sets_ccodes, \
248 reg, reg_kind, reg_flags, \
249 mem, mem_kind, mem_flags, \
250 arr, arr_kind, arr_flags, imm, \
251 b_flags, hw_flags, w_flags, \
252 b_format, hw_format, w_format) \
Mark Mendell2637f2e2014-04-30 10:10:47 -0400253{ kX86 ## opname ## 8 ## reg, reg_kind, reg_flags | b_flags | sets_ccodes, { 0, 0, 0xF6, 0, 0, modrm, 0, imm << 0}, #opname "8" #reg, b_format "!0r" }, \
254{ kX86 ## opname ## 8 ## mem, mem_kind, IS_LOAD | is_store | mem_flags | b_flags | sets_ccodes, { 0, 0, 0xF6, 0, 0, modrm, 0, imm << 0}, #opname "8" #mem, b_format "[!0r+!1d]" }, \
255{ kX86 ## opname ## 8 ## arr, arr_kind, IS_LOAD | is_store | arr_flags | b_flags | sets_ccodes, { 0, 0, 0xF6, 0, 0, modrm, 0, imm << 0}, #opname "8" #arr, b_format "[!0r+!1r<<!2d+!3d]" }, \
256{ kX86 ## opname ## 16 ## reg, reg_kind, reg_flags | hw_flags | sets_ccodes, { 0x66, 0, 0xF7, 0, 0, modrm, 0, imm << 1}, #opname "16" #reg, hw_format "!0r" }, \
257{ kX86 ## opname ## 16 ## mem, mem_kind, IS_LOAD | is_store | mem_flags | hw_flags | sets_ccodes, { 0x66, 0, 0xF7, 0, 0, modrm, 0, imm << 1}, #opname "16" #mem, hw_format "[!0r+!1d]" }, \
258{ kX86 ## opname ## 16 ## arr, arr_kind, IS_LOAD | is_store | arr_flags | hw_flags | sets_ccodes, { 0x66, 0, 0xF7, 0, 0, modrm, 0, imm << 1}, #opname "16" #arr, hw_format "[!0r+!1r<<!2d+!3d]" }, \
259{ kX86 ## opname ## 32 ## reg, reg_kind, reg_flags | w_flags | sets_ccodes, { 0, 0, 0xF7, 0, 0, modrm, 0, imm << 2}, #opname "32" #reg, w_format "!0r" }, \
260{ kX86 ## opname ## 32 ## mem, mem_kind, IS_LOAD | is_store | mem_flags | w_flags | sets_ccodes, { 0, 0, 0xF7, 0, 0, modrm, 0, imm << 2}, #opname "32" #mem, w_format "[!0r+!1d]" }, \
261{ kX86 ## opname ## 32 ## arr, arr_kind, IS_LOAD | is_store | arr_flags | w_flags | sets_ccodes, { 0, 0, 0xF7, 0, 0, modrm, 0, imm << 2}, #opname "32" #arr, w_format "[!0r+!1r<<!2d+!3d]" }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262
263 UNARY_ENCODING_MAP(Not, 0x2, IS_STORE, 0, R, kReg, IS_UNARY_OP | REG_DEF0_USE0, M, kMem, IS_BINARY_OP | REG_USE0, A, kArray, IS_QUAD_OP | REG_USE01, 0, 0, 0, 0, "", "", ""),
264 UNARY_ENCODING_MAP(Neg, 0x3, IS_STORE, SETS_CCODES, R, kReg, IS_UNARY_OP | REG_DEF0_USE0, M, kMem, IS_BINARY_OP | REG_USE0, A, kArray, IS_QUAD_OP | REG_USE01, 0, 0, 0, 0, "", "", ""),
265
Mark Mendell2bf31e62014-01-23 12:13:40 -0800266 UNARY_ENCODING_MAP(Mul, 0x4, 0, SETS_CCODES, DaR, kReg, IS_UNARY_OP | REG_USE0, DaM, kMem, IS_BINARY_OP | REG_USE0, DaA, kArray, IS_QUAD_OP | REG_USE01, 0, REG_DEFA_USEA, REG_DEFAD_USEA, REG_DEFAD_USEA, "ax,al,", "dx:ax,ax,", "edx:eax,eax,"),
267 UNARY_ENCODING_MAP(Imul, 0x5, 0, SETS_CCODES, DaR, kReg, IS_UNARY_OP | REG_USE0, DaM, kMem, IS_BINARY_OP | REG_USE0, DaA, kArray, IS_QUAD_OP | REG_USE01, 0, REG_DEFA_USEA, REG_DEFAD_USEA, REG_DEFAD_USEA, "ax,al,", "dx:ax,ax,", "edx:eax,eax,"),
268 UNARY_ENCODING_MAP(Divmod, 0x6, 0, SETS_CCODES, DaR, kReg, IS_UNARY_OP | REG_USE0, DaM, kMem, IS_BINARY_OP | REG_USE0, DaA, kArray, IS_QUAD_OP | REG_USE01, 0, REG_DEFA_USEA, REG_DEFAD_USEAD, REG_DEFAD_USEAD, "ah:al,ax,", "dx:ax,dx:ax,", "edx:eax,edx:eax,"),
269 UNARY_ENCODING_MAP(Idivmod, 0x7, 0, SETS_CCODES, DaR, kReg, IS_UNARY_OP | REG_USE0, DaM, kMem, IS_BINARY_OP | REG_USE0, DaA, kArray, IS_QUAD_OP | REG_USE01, 0, REG_DEFA_USEA, REG_DEFAD_USEAD, REG_DEFAD_USEAD, "ah:al,ax,", "dx:ax,dx:ax,", "edx:eax,edx:eax,"),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270#undef UNARY_ENCODING_MAP
271
Mark Mendell2bf31e62014-01-23 12:13:40 -0800272 { kx86Cdq32Da, kRegOpcode, NO_OPERAND | REG_DEFAD_USEA, { 0, 0, 0x99, 0, 0, 0, 0, 0 }, "Cdq", "" },
Vladimir Markoa6fd8ba2013-12-13 10:53:49 +0000273 { kX86Bswap32R, kRegOpcode, IS_UNARY_OP | REG_DEF0_USE0, { 0, 0, 0x0F, 0xC8, 0, 0, 0, 0 }, "Bswap32R", "!0r" },
274 { kX86Push32R, kRegOpcode, IS_UNARY_OP | REG_USE0 | REG_USE_SP | REG_DEF_SP | IS_STORE, { 0, 0, 0x50, 0, 0, 0, 0, 0 }, "Push32R", "!0r" },
275 { kX86Pop32R, kRegOpcode, IS_UNARY_OP | REG_DEF0 | REG_USE_SP | REG_DEF_SP | IS_LOAD, { 0, 0, 0x58, 0, 0, 0, 0, 0 }, "Pop32R", "!0r" },
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100276
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277#define EXT_0F_ENCODING_MAP(opname, prefix, opcode, reg_def) \
Mark Mendell2637f2e2014-04-30 10:10:47 -0400278{ kX86 ## opname ## RR, kRegReg, IS_BINARY_OP | reg_def | REG_USE1, { prefix, 0, 0x0F, opcode, 0, 0, 0, 0 }, #opname "RR", "!0r,!1r" }, \
279{ kX86 ## opname ## RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | reg_def | REG_USE1, { prefix, 0, 0x0F, opcode, 0, 0, 0, 0 }, #opname "RM", "!0r,[!1r+!2d]" }, \
280{ kX86 ## opname ## RA, kRegArray, IS_LOAD | IS_QUIN_OP | reg_def | REG_USE12, { prefix, 0, 0x0F, opcode, 0, 0, 0, 0 }, #opname "RA", "!0r,[!1r+!2r<<!3d+!4d]" }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281
Mark Mendellfe945782014-05-22 09:52:36 -0400282#define EXT_0F_ENCODING2_MAP(opname, prefix, opcode, opcode2, reg_def) \
283{ kX86 ## opname ## RR, kRegReg, IS_BINARY_OP | reg_def | REG_USE1, { prefix, 0, 0x0F, opcode, opcode2, 0, 0, 0 }, #opname "RR", "!0r,!1r" }, \
284{ kX86 ## opname ## RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | reg_def | REG_USE1, { prefix, 0, 0x0F, opcode, opcode2, 0, 0, 0 }, #opname "RM", "!0r,[!1r+!2d]" }, \
285{ kX86 ## opname ## RA, kRegArray, IS_LOAD | IS_QUIN_OP | reg_def | REG_USE12, { prefix, 0, 0x0F, opcode, opcode2, 0, 0, 0 }, #opname "RA", "!0r,[!1r+!2r<<!3d+!4d]" }
286
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287 EXT_0F_ENCODING_MAP(Movsd, 0xF2, 0x10, REG_DEF0),
288 { kX86MovsdMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0xF2, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovsdMR", "[!0r+!1d],!2r" },
289 { kX86MovsdAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0xF2, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovsdAR", "[!0r+!1r<<!2d+!3d],!4r" },
290
291 EXT_0F_ENCODING_MAP(Movss, 0xF3, 0x10, REG_DEF0),
292 { kX86MovssMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0xF3, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovssMR", "[!0r+!1d],!2r" },
293 { kX86MovssAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0xF3, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovssAR", "[!0r+!1r<<!2d+!3d],!4r" },
294
295 EXT_0F_ENCODING_MAP(Cvtsi2sd, 0xF2, 0x2A, REG_DEF0),
296 EXT_0F_ENCODING_MAP(Cvtsi2ss, 0xF3, 0x2A, REG_DEF0),
297 EXT_0F_ENCODING_MAP(Cvttsd2si, 0xF2, 0x2C, REG_DEF0),
298 EXT_0F_ENCODING_MAP(Cvttss2si, 0xF3, 0x2C, REG_DEF0),
299 EXT_0F_ENCODING_MAP(Cvtsd2si, 0xF2, 0x2D, REG_DEF0),
300 EXT_0F_ENCODING_MAP(Cvtss2si, 0xF3, 0x2D, REG_DEF0),
Mark Mendell2637f2e2014-04-30 10:10:47 -0400301 EXT_0F_ENCODING_MAP(Ucomisd, 0x66, 0x2E, SETS_CCODES|REG_USE0),
302 EXT_0F_ENCODING_MAP(Ucomiss, 0x00, 0x2E, SETS_CCODES|REG_USE0),
303 EXT_0F_ENCODING_MAP(Comisd, 0x66, 0x2F, SETS_CCODES|REG_USE0),
304 EXT_0F_ENCODING_MAP(Comiss, 0x00, 0x2F, SETS_CCODES|REG_USE0),
305 EXT_0F_ENCODING_MAP(Orps, 0x00, 0x56, REG_DEF0_USE0),
306 EXT_0F_ENCODING_MAP(Xorps, 0x00, 0x57, REG_DEF0_USE0),
307 EXT_0F_ENCODING_MAP(Addsd, 0xF2, 0x58, REG_DEF0_USE0),
308 EXT_0F_ENCODING_MAP(Addss, 0xF3, 0x58, REG_DEF0_USE0),
309 EXT_0F_ENCODING_MAP(Mulsd, 0xF2, 0x59, REG_DEF0_USE0),
310 EXT_0F_ENCODING_MAP(Mulss, 0xF3, 0x59, REG_DEF0_USE0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311 EXT_0F_ENCODING_MAP(Cvtsd2ss, 0xF2, 0x5A, REG_DEF0),
312 EXT_0F_ENCODING_MAP(Cvtss2sd, 0xF3, 0x5A, REG_DEF0),
Mark Mendell2637f2e2014-04-30 10:10:47 -0400313 EXT_0F_ENCODING_MAP(Subsd, 0xF2, 0x5C, REG_DEF0_USE0),
314 EXT_0F_ENCODING_MAP(Subss, 0xF3, 0x5C, REG_DEF0_USE0),
315 EXT_0F_ENCODING_MAP(Divsd, 0xF2, 0x5E, REG_DEF0_USE0),
316 EXT_0F_ENCODING_MAP(Divss, 0xF3, 0x5E, REG_DEF0_USE0),
317 EXT_0F_ENCODING_MAP(Punpckldq, 0x66, 0x62, REG_DEF0_USE0),
Mark Mendellfe945782014-05-22 09:52:36 -0400318 EXT_0F_ENCODING_MAP(Sqrtsd, 0xF2, 0x51, REG_DEF0_USE0),
319 EXT_0F_ENCODING2_MAP(Pmulld, 0x66, 0x38, 0x40, REG_DEF0_USE0),
320 EXT_0F_ENCODING_MAP(Pmullw, 0x66, 0xD5, REG_DEF0_USE0),
321 EXT_0F_ENCODING_MAP(Mulps, 0x00, 0x59, REG_DEF0_USE0),
322 EXT_0F_ENCODING_MAP(Mulpd, 0x66, 0x59, REG_DEF0_USE0),
323 EXT_0F_ENCODING_MAP(Paddb, 0x66, 0xFC, REG_DEF0_USE0),
324 EXT_0F_ENCODING_MAP(Paddw, 0x66, 0xFD, REG_DEF0_USE0),
325 EXT_0F_ENCODING_MAP(Paddd, 0x66, 0xFE, REG_DEF0_USE0),
326 EXT_0F_ENCODING_MAP(Addps, 0x00, 0x58, REG_DEF0_USE0),
327 EXT_0F_ENCODING_MAP(Addpd, 0xF2, 0x58, REG_DEF0_USE0),
328 EXT_0F_ENCODING_MAP(Psubb, 0x66, 0xF8, REG_DEF0_USE0),
329 EXT_0F_ENCODING_MAP(Psubw, 0x66, 0xF9, REG_DEF0_USE0),
330 EXT_0F_ENCODING_MAP(Psubd, 0x66, 0xFA, REG_DEF0_USE0),
331 EXT_0F_ENCODING_MAP(Subps, 0x00, 0x5C, REG_DEF0_USE0),
332 EXT_0F_ENCODING_MAP(Subpd, 0x66, 0x5C, REG_DEF0_USE0),
333 EXT_0F_ENCODING_MAP(Pand, 0x66, 0xDB, REG_DEF0_USE0),
334 EXT_0F_ENCODING_MAP(Por, 0x66, 0xEB, REG_DEF0_USE0),
335 EXT_0F_ENCODING_MAP(Pxor, 0x66, 0xEF, REG_DEF0_USE0),
336 EXT_0F_ENCODING2_MAP(Phaddw, 0x66, 0x38, 0x01, REG_DEF0_USE0),
337 EXT_0F_ENCODING2_MAP(Phaddd, 0x66, 0x38, 0x02, REG_DEF0_USE0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700338
Mark Mendellfe945782014-05-22 09:52:36 -0400339 { kX86PextrbRRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0x66, 0, 0x0F, 0x3A, 0x14, 0, 0, 1 }, "PextbRRI", "!0r,!1r,!2d" },
340 { kX86PextrwRRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0x66, 0, 0x0F, 0xC5, 0x00, 0, 0, 1 }, "PextwRRI", "!0r,!1r,!2d" },
341 { kX86PextrdRRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0x66, 0, 0x0F, 0x3A, 0x16, 0, 0, 1 }, "PextdRRI", "!0r,!1r,!2d" },
342
343 { kX86PshuflwRRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0xF2, 0, 0x0F, 0x70, 0, 0, 0, 1 }, "PshuflwRRI", "!0r,!1r,!2d" },
344 { kX86PshufdRRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0x66, 0, 0x0F, 0x70, 0, 0, 0, 1 }, "PshuffRRI", "!0r,!1r,!2d" },
345
346 { kX86PsrawRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x71, 0, 4, 0, 1 }, "PsrawRI", "!0r,!1d" },
347 { kX86PsradRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x72, 0, 4, 0, 1 }, "PsradRI", "!0r,!1d" },
348 { kX86PsrlwRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x71, 0, 2, 0, 1 }, "PsrlwRI", "!0r,!1d" },
349 { kX86PsrldRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x72, 0, 2, 0, 1 }, "PsrldRI", "!0r,!1d" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700350 { kX86PsrlqRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x73, 0, 2, 0, 1 }, "PsrlqRI", "!0r,!1d" },
Mark Mendellfe945782014-05-22 09:52:36 -0400351 { kX86PsllwRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x71, 0, 6, 0, 1 }, "PsllwRI", "!0r,!1d" },
352 { kX86PslldRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x72, 0, 6, 0, 1 }, "PslldRI", "!0r,!1d" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353 { kX86PsllqRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x73, 0, 6, 0, 1 }, "PsllqRI", "!0r,!1d" },
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800354
Serguei Katkove90501d2014-03-12 15:56:54 +0700355 { kX86Fild32M, kMem, IS_LOAD | IS_UNARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xDB, 0x00, 0, 0, 0, 0 }, "Fild32M", "[!0r,!1d]" },
356 { kX86Fild64M, kMem, IS_LOAD | IS_UNARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xDF, 0x00, 0, 5, 0, 0 }, "Fild64M", "[!0r,!1d]" },
357 { kX86Fstp32M, kMem, IS_STORE | IS_UNARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xD9, 0x00, 0, 3, 0, 0 }, "FstpsM", "[!0r,!1d]" },
358 { kX86Fstp64M, kMem, IS_STORE | IS_UNARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xDD, 0x00, 0, 3, 0, 0 }, "FstpdM", "[!0r,!1d]" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700359
Mark Mendelld65c51a2014-04-29 16:55:20 -0400360 EXT_0F_ENCODING_MAP(Mova128, 0x66, 0x6F, REG_DEF0),
361 { kX86Mova128MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, 0, 0x0F, 0x6F, 0, 0, 0, 0 }, "Mova128MR", "[!0r+!1d],!2r" },
362 { kX86Mova128AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x66, 0, 0x0F, 0x6F, 0, 0, 0, 0 }, "Mova128AR", "[!0r+!1r<<!2d+!3d],!4r" },
363
364
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800365 EXT_0F_ENCODING_MAP(Movups, 0x0, 0x10, REG_DEF0),
366 { kX86MovupsMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x0, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovupsMR", "[!0r+!1d],!2r" },
367 { kX86MovupsAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x0, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovupsAR", "[!0r+!1r<<!2d+!3d],!4r" },
368
369 EXT_0F_ENCODING_MAP(Movaps, 0x0, 0x28, REG_DEF0),
370 { kX86MovapsMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x0, 0, 0x0F, 0x29, 0, 0, 0, 0 }, "MovapsMR", "[!0r+!1d],!2r" },
371 { kX86MovapsAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x0, 0, 0x0F, 0x29, 0, 0, 0, 0 }, "MovapsAR", "[!0r+!1r<<!2d+!3d],!4r" },
372
373 { kX86MovlpsRM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0 | REG_USE01, { 0x0, 0, 0x0F, 0x12, 0, 0, 0, 0 }, "MovlpsRM", "!0r,[!1r+!2d]" },
374 { kX86MovlpsRA, kRegArray, IS_LOAD | IS_QUIN_OP | REG_DEF0 | REG_USE012, { 0x0, 0, 0x0F, 0x12, 0, 0, 0, 0 }, "MovlpsRA", "!0r,[!1r+!2r<<!3d+!4d]" },
375 { kX86MovlpsMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x0, 0, 0x0F, 0x13, 0, 0, 0, 0 }, "MovlpsMR", "[!0r+!1d],!2r" },
376 { kX86MovlpsAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x0, 0, 0x0F, 0x13, 0, 0, 0, 0 }, "MovlpsAR", "[!0r+!1r<<!2d+!3d],!4r" },
377
378 { kX86MovhpsRM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0 | REG_USE01, { 0x0, 0, 0x0F, 0x16, 0, 0, 0, 0 }, "MovhpsRM", "!0r,[!1r+!2d]" },
379 { kX86MovhpsRA, kRegArray, IS_LOAD | IS_QUIN_OP | REG_DEF0 | REG_USE012, { 0x0, 0, 0x0F, 0x16, 0, 0, 0, 0 }, "MovhpsRA", "!0r,[!1r+!2r<<!3d+!4d]" },
380 { kX86MovhpsMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x0, 0, 0x0F, 0x17, 0, 0, 0, 0 }, "MovhpsMR", "[!0r+!1d],!2r" },
381 { kX86MovhpsAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x0, 0, 0x0F, 0x17, 0, 0, 0, 0 }, "MovhpsAR", "[!0r+!1r<<!2d+!3d],!4r" },
382
Brian Carlstrom7940e442013-07-12 13:46:57 -0700383 EXT_0F_ENCODING_MAP(Movdxr, 0x66, 0x6E, REG_DEF0),
Mark Mendell2637f2e2014-04-30 10:10:47 -0400384 { kX86MovdrxRR, kRegRegStore, IS_BINARY_OP | REG_DEF0 | REG_USE1, { 0x66, 0, 0x0F, 0x7E, 0, 0, 0, 0 }, "MovdrxRR", "!0r,!1r" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700385 { kX86MovdrxMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, 0, 0x0F, 0x7E, 0, 0, 0, 0 }, "MovdrxMR", "[!0r+!1d],!2r" },
386 { kX86MovdrxAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x66, 0, 0x0F, 0x7E, 0, 0, 0, 0 }, "MovdrxAR", "[!0r+!1r<<!2d+!3d],!4r" },
387
388 { kX86Set8R, kRegCond, IS_BINARY_OP | REG_DEF0 | USES_CCODES, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0 }, "Set8R", "!1c !0r" },
389 { kX86Set8M, kMemCond, IS_STORE | IS_TERTIARY_OP | REG_USE0 | USES_CCODES, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0 }, "Set8M", "!2c [!0r+!1d]" },
390 { kX86Set8A, kArrayCond, IS_STORE | IS_QUIN_OP | REG_USE01 | USES_CCODES, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0 }, "Set8A", "!4c [!0r+!1r<<!2d+!3d]" },
391
392 // TODO: load/store?
393 // Encode the modrm opcode as an extra opcode byte to avoid computation during assembly.
394 { kX86Mfence, kReg, NO_OPERAND, { 0, 0, 0x0F, 0xAE, 0, 6, 0, 0 }, "Mfence", "" },
395
Mark Mendell2637f2e2014-04-30 10:10:47 -0400396 EXT_0F_ENCODING_MAP(Imul16, 0x66, 0xAF, REG_USE0 | REG_DEF0 | SETS_CCODES),
397 EXT_0F_ENCODING_MAP(Imul32, 0x00, 0xAF, REG_USE0 | REG_DEF0 | SETS_CCODES),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700398
399 { kX86CmpxchgRR, kRegRegStore, IS_BINARY_OP | REG_DEF0 | REG_USE01 | REG_DEFA_USEA | SETS_CCODES, { 0, 0, 0x0F, 0xB1, 0, 0, 0, 0 }, "Cmpxchg", "!0r,!1r" },
400 { kX86CmpxchgMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02 | REG_DEFA_USEA | SETS_CCODES, { 0, 0, 0x0F, 0xB1, 0, 0, 0, 0 }, "Cmpxchg", "[!0r+!1d],!2r" },
401 { kX86CmpxchgAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014 | REG_DEFA_USEA | SETS_CCODES, { 0, 0, 0x0F, 0xB1, 0, 0, 0, 0 }, "Cmpxchg", "[!0r+!1r<<!2d+!3d],!4r" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700402 { kX86LockCmpxchgMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02 | REG_DEFA_USEA | SETS_CCODES, { 0xF0, 0, 0x0F, 0xB1, 0, 0, 0, 0 }, "Lock Cmpxchg", "[!0r+!1d],!2r" },
403 { kX86LockCmpxchgAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014 | REG_DEFA_USEA | SETS_CCODES, { 0xF0, 0, 0x0F, 0xB1, 0, 0, 0, 0 }, "Lock Cmpxchg", "[!0r+!1r<<!2d+!3d],!4r" },
Vladimir Marko70b797d2013-12-03 15:25:24 +0000404 { kX86LockCmpxchg8bM, kMem, IS_STORE | IS_BINARY_OP | REG_USE0 | REG_DEFAD_USEAD | REG_USEC | REG_USEB | SETS_CCODES, { 0xF0, 0, 0x0F, 0xC7, 0, 1, 0, 0 }, "Lock Cmpxchg8b", "[!0r+!1d]" },
405 { kX86LockCmpxchg8bA, kArray, IS_STORE | IS_QUAD_OP | REG_USE01 | REG_DEFAD_USEAD | REG_USEC | REG_USEB | SETS_CCODES, { 0xF0, 0, 0x0F, 0xC7, 0, 1, 0, 0 }, "Lock Cmpxchg8b", "[!0r+!1r<<!2d+!3d]" },
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800406 { kX86XchgMR, kMemReg, IS_STORE | IS_LOAD | IS_TERTIARY_OP | REG_DEF2 | REG_USE02, { 0, 0, 0x87, 0, 0, 0, 0, 0 }, "Xchg", "[!0r+!1d],!2r" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700407
408 EXT_0F_ENCODING_MAP(Movzx8, 0x00, 0xB6, REG_DEF0),
409 EXT_0F_ENCODING_MAP(Movzx16, 0x00, 0xB7, REG_DEF0),
410 EXT_0F_ENCODING_MAP(Movsx8, 0x00, 0xBE, REG_DEF0),
411 EXT_0F_ENCODING_MAP(Movsx16, 0x00, 0xBF, REG_DEF0),
412#undef EXT_0F_ENCODING_MAP
413
414 { kX86Jcc8, kJcc, IS_BINARY_OP | IS_BRANCH | NEEDS_FIXUP | USES_CCODES, { 0, 0, 0x70, 0, 0, 0, 0, 0 }, "Jcc8", "!1c !0t" },
415 { kX86Jcc32, kJcc, IS_BINARY_OP | IS_BRANCH | NEEDS_FIXUP | USES_CCODES, { 0, 0, 0x0F, 0x80, 0, 0, 0, 0 }, "Jcc32", "!1c !0t" },
416 { kX86Jmp8, kJmp, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0xEB, 0, 0, 0, 0, 0 }, "Jmp8", "!0t" },
417 { kX86Jmp32, kJmp, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0xE9, 0, 0, 0, 0, 0 }, "Jmp32", "!0t" },
418 { kX86JmpR, kJmp, IS_UNARY_OP | IS_BRANCH | REG_USE0, { 0, 0, 0xFF, 0, 0, 4, 0, 0 }, "JmpR", "!0r" },
Mark Mendell4028a6c2014-02-19 20:06:20 -0800419 { kX86Jecxz8, kJmp, NO_OPERAND | IS_BRANCH | NEEDS_FIXUP | REG_USEC, { 0, 0, 0xE3, 0, 0, 0, 0, 0 }, "Jecxz", "!0t" },
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700420 { kX86JmpT, kJmp, IS_UNARY_OP | IS_BRANCH | IS_LOAD, { THREAD_PREFIX, 0, 0xFF, 0, 0, 4, 0, 0 }, "JmpT", "fs:[!0d]" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700421 { kX86CallR, kCall, IS_UNARY_OP | IS_BRANCH | REG_USE0, { 0, 0, 0xE8, 0, 0, 0, 0, 0 }, "CallR", "!0r" },
422 { kX86CallM, kCall, IS_BINARY_OP | IS_BRANCH | IS_LOAD | REG_USE0, { 0, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallM", "[!0r+!1d]" },
423 { kX86CallA, kCall, IS_QUAD_OP | IS_BRANCH | IS_LOAD | REG_USE01, { 0, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallA", "[!0r+!1r<<!2d+!3d]" },
424 { kX86CallT, kCall, IS_UNARY_OP | IS_BRANCH | IS_LOAD, { THREAD_PREFIX, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallT", "fs:[!0d]" },
Mark Mendell55d0eac2014-02-06 11:02:52 -0800425 { kX86CallI, kCall, IS_UNARY_OP | IS_BRANCH, { 0, 0, 0xE8, 0, 0, 0, 0, 4 }, "CallI", "!0d" },
Brian Carlstromb1eba212013-07-17 18:07:19 -0700426 { kX86Ret, kNullary, NO_OPERAND | IS_BRANCH, { 0, 0, 0xC3, 0, 0, 0, 0, 0 }, "Ret", "" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700427
428 { kX86StartOfMethod, kMacro, IS_UNARY_OP | SETS_CCODES, { 0, 0, 0, 0, 0, 0, 0, 0 }, "StartOfMethod", "!0r" },
429 { kX86PcRelLoadRA, kPcRel, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "PcRelLoadRA", "!0r,[!1r+!2r<<!3d+!4p]" },
430 { kX86PcRelAdr, kPcRel, IS_LOAD | IS_BINARY_OP | REG_DEF0, { 0, 0, 0xB8, 0, 0, 0, 0, 4 }, "PcRelAdr", "!0r,!1d" },
Mark Mendell2637f2e2014-04-30 10:10:47 -0400431 { kX86RepneScasw, kPrefix2Nullary, NO_OPERAND | REG_USEA | REG_USEC | SETS_CCODES, { 0x66, 0xF2, 0xAF, 0, 0, 0, 0, 0 }, "RepNE ScasW", "" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432};
433
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700434size_t X86Mir2Lir::ComputeSize(const X86EncodingMap* entry, int base, int displacement, bool has_sib) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700435 size_t size = 0;
436 if (entry->skeleton.prefix1 > 0) {
437 ++size;
438 if (entry->skeleton.prefix2 > 0) {
439 ++size;
440 }
441 }
442 ++size; // opcode
443 if (entry->skeleton.opcode == 0x0F) {
444 ++size;
445 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
446 ++size;
447 }
448 }
449 ++size; // modrm
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700450 if (has_sib || RegStorage::RegNum(base) == rs_rX86_SP.GetRegNum()
451 || (Gen64Bit() && entry->skeleton.prefix1 == THREAD_PREFIX)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700452 // SP requires a SIB byte.
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700453 // GS access also needs a SIB byte for absolute adressing in 64-bit mode.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454 ++size;
455 }
buzbee091cc402014-03-31 10:14:40 -0700456 if (displacement != 0 || RegStorage::RegNum(base) == rs_rBP.GetRegNum()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457 // BP requires an explicit displacement, even when it's 0.
458 if (entry->opcode != kX86Lea32RA) {
459 DCHECK_NE(entry->flags & (IS_LOAD | IS_STORE), 0ULL) << entry->name;
460 }
461 size += IS_SIMM8(displacement) ? 1 : 4;
462 }
463 size += entry->skeleton.immediate_bytes;
464 return size;
465}
466
467int X86Mir2Lir::GetInsnSize(LIR* lir) {
buzbee409fe942013-10-11 10:49:56 -0700468 DCHECK(!IsPseudoLirOp(lir->opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700469 const X86EncodingMap* entry = &X86Mir2Lir::EncodingMap[lir->opcode];
470 switch (entry->kind) {
471 case kData:
472 return 4; // 4 bytes of data
473 case kNop:
474 return lir->operands[0]; // length of nop is sole operand
475 case kNullary:
476 return 1; // 1 byte of opcode
Mark Mendell4028a6c2014-02-19 20:06:20 -0800477 case kPrefix2Nullary:
478 return 3; // 1 byte of opcode + 2 prefixes
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100479 case kRegOpcode: // lir operands - 0: reg
480 return ComputeSize(entry, 0, 0, false) - 1; // substract 1 for modrm
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700481 case kReg64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700482 case kReg: // lir operands - 0: reg
483 return ComputeSize(entry, 0, 0, false);
484 case kMem: // lir operands - 0: base, 1: disp
485 return ComputeSize(entry, lir->operands[0], lir->operands[1], false);
486 case kArray: // lir operands - 0: base, 1: index, 2: scale, 3: disp
487 return ComputeSize(entry, lir->operands[0], lir->operands[3], true);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700488 case kMemReg64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700489 case kMemReg: // lir operands - 0: base, 1: disp, 2: reg
490 return ComputeSize(entry, lir->operands[0], lir->operands[1], false);
Mark Mendell2637f2e2014-04-30 10:10:47 -0400491 case kMemRegImm: // lir operands - 0: base, 1: disp, 2: reg 3: immediate
492 return ComputeSize(entry, lir->operands[0], lir->operands[1], false);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700493 case kArrayReg64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700494 case kArrayReg: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
495 return ComputeSize(entry, lir->operands[0], lir->operands[3], true);
496 case kThreadReg: // lir operands - 0: disp, 1: reg
497 return ComputeSize(entry, 0, lir->operands[0], false);
498 case kRegReg:
499 return ComputeSize(entry, 0, 0, false);
500 case kRegRegStore:
501 return ComputeSize(entry, 0, 0, false);
502 case kRegMem: // lir operands - 0: reg, 1: base, 2: disp
503 return ComputeSize(entry, lir->operands[1], lir->operands[2], false);
504 case kRegArray: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
505 return ComputeSize(entry, lir->operands[1], lir->operands[4], true);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700506 case kReg64Thread: // lir operands - 0: reg, 1: disp
Brian Carlstrom7940e442013-07-12 13:46:57 -0700507 case kRegThread: // lir operands - 0: reg, 1: disp
508 return ComputeSize(entry, 0, 0x12345678, false); // displacement size is always 32bit
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700509 case kReg64Imm:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700510 case kRegImm: { // lir operands - 0: reg, 1: immediate
511 size_t size = ComputeSize(entry, 0, 0, false);
512 if (entry->skeleton.ax_opcode == 0) {
513 return size;
514 } else {
515 // AX opcodes don't require the modrm byte.
516 int reg = lir->operands[0];
buzbee091cc402014-03-31 10:14:40 -0700517 return size - (RegStorage::RegNum(reg) == rs_rAX.GetRegNum() ? 1 : 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700518 }
519 }
520 case kMemImm: // lir operands - 0: base, 1: disp, 2: immediate
521 return ComputeSize(entry, lir->operands[0], lir->operands[1], false);
522 case kArrayImm: // lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
523 return ComputeSize(entry, lir->operands[0], lir->operands[3], true);
524 case kThreadImm: // lir operands - 0: disp, 1: imm
525 return ComputeSize(entry, 0, 0x12345678, false); // displacement size is always 32bit
526 case kRegRegImm: // lir operands - 0: reg, 1: reg, 2: imm
Mark Mendell4708dcd2014-01-22 09:05:18 -0800527 case kRegRegImmRev:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700528 return ComputeSize(entry, 0, 0, false);
529 case kRegMemImm: // lir operands - 0: reg, 1: base, 2: disp, 3: imm
530 return ComputeSize(entry, lir->operands[1], lir->operands[2], false);
531 case kRegArrayImm: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp, 5: imm
532 return ComputeSize(entry, lir->operands[1], lir->operands[4], true);
533 case kMovRegImm: // lir operands - 0: reg, 1: immediate
534 return 1 + entry->skeleton.immediate_bytes;
535 case kShiftRegImm: // lir operands - 0: reg, 1: immediate
536 // Shift by immediate one has a shorter opcode.
537 return ComputeSize(entry, 0, 0, false) - (lir->operands[1] == 1 ? 1 : 0);
538 case kShiftMemImm: // lir operands - 0: base, 1: disp, 2: immediate
539 // Shift by immediate one has a shorter opcode.
540 return ComputeSize(entry, lir->operands[0], lir->operands[1], false) -
541 (lir->operands[2] == 1 ? 1 : 0);
542 case kShiftArrayImm: // lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
543 // Shift by immediate one has a shorter opcode.
544 return ComputeSize(entry, lir->operands[0], lir->operands[3], true) -
545 (lir->operands[4] == 1 ? 1 : 0);
546 case kShiftRegCl:
547 return ComputeSize(entry, 0, 0, false);
548 case kShiftMemCl: // lir operands - 0: base, 1: disp, 2: cl
549 return ComputeSize(entry, lir->operands[0], lir->operands[1], false);
550 case kShiftArrayCl: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
551 return ComputeSize(entry, lir->operands[0], lir->operands[3], true);
552 case kRegCond: // lir operands - 0: reg, 1: cond
553 return ComputeSize(entry, 0, 0, false);
554 case kMemCond: // lir operands - 0: base, 1: disp, 2: cond
555 return ComputeSize(entry, lir->operands[0], lir->operands[1], false);
556 case kArrayCond: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: cond
557 return ComputeSize(entry, lir->operands[0], lir->operands[3], true);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800558 case kRegRegCond: // lir operands - 0: reg, 1: reg, 2: cond
559 return ComputeSize(entry, 0, 0, false);
Mark Mendell2637f2e2014-04-30 10:10:47 -0400560 case kRegMemCond: // lir operands - 0: reg, 1: reg, 2: disp, 3:cond
561 return ComputeSize(entry, lir->operands[1], lir->operands[2], false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700562 case kJcc:
563 if (lir->opcode == kX86Jcc8) {
564 return 2; // opcode + rel8
565 } else {
566 DCHECK(lir->opcode == kX86Jcc32);
567 return 6; // 2 byte opcode + rel32
568 }
569 case kJmp:
Mark Mendell4028a6c2014-02-19 20:06:20 -0800570 if (lir->opcode == kX86Jmp8 || lir->opcode == kX86Jecxz8) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700571 return 2; // opcode + rel8
572 } else if (lir->opcode == kX86Jmp32) {
573 return 5; // opcode + rel32
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700574 } else if (lir->opcode == kX86JmpT) {
575 return ComputeSize(entry, 0, 0x12345678, false); // displacement size is always 32bit
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576 } else {
577 DCHECK(lir->opcode == kX86JmpR);
578 return 2; // opcode + modrm
579 }
580 case kCall:
581 switch (lir->opcode) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800582 case kX86CallI: return 5; // opcode 0:disp
Brian Carlstrom7940e442013-07-12 13:46:57 -0700583 case kX86CallR: return 2; // opcode modrm
584 case kX86CallM: // lir operands - 0: base, 1: disp
585 return ComputeSize(entry, lir->operands[0], lir->operands[1], false);
586 case kX86CallA: // lir operands - 0: base, 1: index, 2: scale, 3: disp
587 return ComputeSize(entry, lir->operands[0], lir->operands[3], true);
588 case kX86CallT: // lir operands - 0: disp
589 return ComputeSize(entry, 0, 0x12345678, false); // displacement size is always 32bit
590 default:
591 break;
592 }
593 break;
594 case kPcRel:
595 if (entry->opcode == kX86PcRelLoadRA) {
596 // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
597 return ComputeSize(entry, lir->operands[1], 0x12345678, true);
598 } else {
599 DCHECK(entry->opcode == kX86PcRelAdr);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700600 return 5; // opcode with reg + 4 byte immediate
Brian Carlstrom7940e442013-07-12 13:46:57 -0700601 }
602 case kMacro:
603 DCHECK_EQ(lir->opcode, static_cast<int>(kX86StartOfMethod));
604 return 5 /* call opcode + 4 byte displacement */ + 1 /* pop reg */ +
605 ComputeSize(&X86Mir2Lir::EncodingMap[kX86Sub32RI], 0, 0, false) -
buzbee091cc402014-03-31 10:14:40 -0700606 (RegStorage::RegNum(lir->operands[0]) == rs_rAX.GetRegNum() ? 1 : 0); // shorter ax encoding
Brian Carlstrom7940e442013-07-12 13:46:57 -0700607 default:
608 break;
609 }
610 UNIMPLEMENTED(FATAL) << "Unimplemented size encoding for: " << entry->name;
611 return 0;
612}
613
Vladimir Marko057c74a2013-12-03 15:20:45 +0000614void X86Mir2Lir::EmitPrefix(const X86EncodingMap* entry) {
615 if (entry->skeleton.prefix1 != 0) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700616 if (Gen64Bit() && entry->skeleton.prefix1 == THREAD_PREFIX) {
617 // 64 bit adresses by GS, not FS
618 code_buffer_.push_back(THREAD_PREFIX_GS);
619 } else {
620 code_buffer_.push_back(entry->skeleton.prefix1);
621 }
Vladimir Marko057c74a2013-12-03 15:20:45 +0000622 if (entry->skeleton.prefix2 != 0) {
623 code_buffer_.push_back(entry->skeleton.prefix2);
624 }
625 } else {
626 DCHECK_EQ(0, entry->skeleton.prefix2);
627 }
628}
629
630void X86Mir2Lir::EmitOpcode(const X86EncodingMap* entry) {
631 code_buffer_.push_back(entry->skeleton.opcode);
632 if (entry->skeleton.opcode == 0x0F) {
633 code_buffer_.push_back(entry->skeleton.extra_opcode1);
634 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
635 code_buffer_.push_back(entry->skeleton.extra_opcode2);
636 } else {
637 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
638 }
639 } else {
640 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
641 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
642 }
643}
644
645void X86Mir2Lir::EmitPrefixAndOpcode(const X86EncodingMap* entry) {
646 EmitPrefix(entry);
647 EmitOpcode(entry);
648}
649
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650static uint8_t ModrmForDisp(int base, int disp) {
651 // BP requires an explicit disp, so do not omit it in the 0 case
buzbee091cc402014-03-31 10:14:40 -0700652 if (disp == 0 && RegStorage::RegNum(base) != rs_rBP.GetRegNum()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653 return 0;
654 } else if (IS_SIMM8(disp)) {
655 return 1;
656 } else {
657 return 2;
658 }
659}
660
Vladimir Marko057c74a2013-12-03 15:20:45 +0000661void X86Mir2Lir::EmitDisp(uint8_t base, int disp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700662 // BP requires an explicit disp, so do not omit it in the 0 case
buzbee091cc402014-03-31 10:14:40 -0700663 if (disp == 0 && RegStorage::RegNum(base) != rs_rBP.GetRegNum()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700664 return;
665 } else if (IS_SIMM8(disp)) {
666 code_buffer_.push_back(disp & 0xFF);
667 } else {
668 code_buffer_.push_back(disp & 0xFF);
669 code_buffer_.push_back((disp >> 8) & 0xFF);
670 code_buffer_.push_back((disp >> 16) & 0xFF);
671 code_buffer_.push_back((disp >> 24) & 0xFF);
672 }
673}
674
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700675void X86Mir2Lir::EmitModrmThread(uint8_t reg_or_opcode) {
676 if (Gen64Bit()) {
677 // Absolute adressing for GS access.
678 uint8_t modrm = (0 << 6) | (reg_or_opcode << 3) | rs_rX86_SP.GetRegNum();
679 code_buffer_.push_back(modrm);
680 uint8_t sib = (0/*TIMES_1*/ << 6) | (rs_rX86_SP.GetRegNum() << 3) | rs_rBP.GetRegNum();
681 code_buffer_.push_back(sib);
682 } else {
683 uint8_t modrm = (0 << 6) | (reg_or_opcode << 3) | rs_rBP.GetRegNum();
684 code_buffer_.push_back(modrm);
685 }
686}
687
Vladimir Marko057c74a2013-12-03 15:20:45 +0000688void X86Mir2Lir::EmitModrmDisp(uint8_t reg_or_opcode, uint8_t base, int disp) {
buzbee091cc402014-03-31 10:14:40 -0700689 DCHECK_LT(RegStorage::RegNum(reg_or_opcode), 8);
690 DCHECK_LT(RegStorage::RegNum(base), 8);
691 uint8_t modrm = (ModrmForDisp(base, disp) << 6) | (RegStorage::RegNum(reg_or_opcode) << 3) |
692 RegStorage::RegNum(base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700693 code_buffer_.push_back(modrm);
buzbee091cc402014-03-31 10:14:40 -0700694 if (RegStorage::RegNum(base) == rs_rX86_SP.GetRegNum()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700695 // Special SIB for SP base
buzbee091cc402014-03-31 10:14:40 -0700696 code_buffer_.push_back(0 << 6 | rs_rX86_SP.GetRegNum() << 3 | rs_rX86_SP.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697 }
698 EmitDisp(base, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699}
700
Vladimir Marko057c74a2013-12-03 15:20:45 +0000701void X86Mir2Lir::EmitModrmSibDisp(uint8_t reg_or_opcode, uint8_t base, uint8_t index,
702 int scale, int disp) {
buzbee091cc402014-03-31 10:14:40 -0700703 DCHECK_LT(RegStorage::RegNum(reg_or_opcode), 8);
704 uint8_t modrm = (ModrmForDisp(base, disp) << 6) | RegStorage::RegNum(reg_or_opcode) << 3 |
705 rs_rX86_SP.GetRegNum();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700706 code_buffer_.push_back(modrm);
707 DCHECK_LT(scale, 4);
buzbee091cc402014-03-31 10:14:40 -0700708 DCHECK_LT(RegStorage::RegNum(index), 8);
709 DCHECK_LT(RegStorage::RegNum(base), 8);
710 uint8_t sib = (scale << 6) | (RegStorage::RegNum(index) << 3) | RegStorage::RegNum(base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700711 code_buffer_.push_back(sib);
712 EmitDisp(base, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700713}
714
Vladimir Marko057c74a2013-12-03 15:20:45 +0000715void X86Mir2Lir::EmitImm(const X86EncodingMap* entry, int imm) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700716 switch (entry->skeleton.immediate_bytes) {
717 case 1:
718 DCHECK(IS_SIMM8(imm));
719 code_buffer_.push_back(imm & 0xFF);
720 break;
721 case 2:
722 DCHECK(IS_SIMM16(imm));
723 code_buffer_.push_back(imm & 0xFF);
724 code_buffer_.push_back((imm >> 8) & 0xFF);
725 break;
726 case 4:
727 code_buffer_.push_back(imm & 0xFF);
728 code_buffer_.push_back((imm >> 8) & 0xFF);
729 code_buffer_.push_back((imm >> 16) & 0xFF);
730 code_buffer_.push_back((imm >> 24) & 0xFF);
731 break;
732 default:
733 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
734 << ") for instruction: " << entry->name;
735 break;
736 }
737}
738
Vladimir Marko057c74a2013-12-03 15:20:45 +0000739void X86Mir2Lir::EmitOpRegOpcode(const X86EncodingMap* entry, uint8_t reg) {
740 EmitPrefixAndOpcode(entry);
741 // There's no 3-byte instruction with +rd
742 DCHECK(entry->skeleton.opcode != 0x0F ||
743 (entry->skeleton.extra_opcode1 != 0x38 && entry->skeleton.extra_opcode1 != 0x3A));
buzbee091cc402014-03-31 10:14:40 -0700744 DCHECK(!RegStorage::IsFloat(reg));
745 DCHECK_LT(RegStorage::RegNum(reg), 8);
746 code_buffer_.back() += RegStorage::RegNum(reg);
Vladimir Marko057c74a2013-12-03 15:20:45 +0000747 DCHECK_EQ(0, entry->skeleton.ax_opcode);
748 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
749}
750
751void X86Mir2Lir::EmitOpReg(const X86EncodingMap* entry, uint8_t reg) {
752 EmitPrefixAndOpcode(entry);
buzbee091cc402014-03-31 10:14:40 -0700753 if (RegStorage::RegNum(reg) >= 4) {
754 DCHECK(strchr(entry->name, '8') == NULL) << entry->name << " "
755 << static_cast<int>(RegStorage::RegNum(reg))
Vladimir Marko057c74a2013-12-03 15:20:45 +0000756 << " in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
757 }
buzbee091cc402014-03-31 10:14:40 -0700758 DCHECK_LT(RegStorage::RegNum(reg), 8);
759 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | RegStorage::RegNum(reg);
Vladimir Marko057c74a2013-12-03 15:20:45 +0000760 code_buffer_.push_back(modrm);
761 DCHECK_EQ(0, entry->skeleton.ax_opcode);
762 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
763}
764
765void X86Mir2Lir::EmitOpMem(const X86EncodingMap* entry, uint8_t base, int disp) {
766 EmitPrefix(entry);
767 code_buffer_.push_back(entry->skeleton.opcode);
768 DCHECK_NE(0x0F, entry->skeleton.opcode);
769 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
770 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Vladimir Marko057c74a2013-12-03 15:20:45 +0000771 EmitModrmDisp(entry->skeleton.modrm_opcode, base, disp);
772 DCHECK_EQ(0, entry->skeleton.ax_opcode);
773 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
774}
775
776void X86Mir2Lir::EmitOpArray(const X86EncodingMap* entry, uint8_t base, uint8_t index,
777 int scale, int disp) {
778 EmitPrefixAndOpcode(entry);
779 EmitModrmSibDisp(entry->skeleton.modrm_opcode, base, index, scale, disp);
780 DCHECK_EQ(0, entry->skeleton.ax_opcode);
781 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
782}
783
784void X86Mir2Lir::EmitMemReg(const X86EncodingMap* entry,
785 uint8_t base, int disp, uint8_t reg) {
786 EmitPrefixAndOpcode(entry);
buzbee091cc402014-03-31 10:14:40 -0700787 if (RegStorage::RegNum(reg) >= 4) {
Vladimir Marko057c74a2013-12-03 15:20:45 +0000788 DCHECK(strchr(entry->name, '8') == NULL ||
789 entry->opcode == kX86Movzx8RM || entry->opcode == kX86Movsx8RM)
buzbee091cc402014-03-31 10:14:40 -0700790 << entry->name << " " << static_cast<int>(RegStorage::RegNum(reg))
Vladimir Marko057c74a2013-12-03 15:20:45 +0000791 << " in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
792 }
793 EmitModrmDisp(reg, base, disp);
794 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
795 DCHECK_EQ(0, entry->skeleton.ax_opcode);
796 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
797}
798
799void X86Mir2Lir::EmitRegMem(const X86EncodingMap* entry,
800 uint8_t reg, uint8_t base, int disp) {
801 // Opcode will flip operands.
802 EmitMemReg(entry, base, disp, reg);
803}
804
805void X86Mir2Lir::EmitRegArray(const X86EncodingMap* entry, uint8_t reg, uint8_t base, uint8_t index,
806 int scale, int disp) {
807 EmitPrefixAndOpcode(entry);
Vladimir Marko057c74a2013-12-03 15:20:45 +0000808 EmitModrmSibDisp(reg, base, index, scale, disp);
809 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
810 DCHECK_EQ(0, entry->skeleton.ax_opcode);
811 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
812}
813
buzbee091cc402014-03-31 10:14:40 -0700814void X86Mir2Lir::EmitArrayReg(const X86EncodingMap* entry, uint8_t base, uint8_t index, int scale,
815 int disp, uint8_t reg) {
Vladimir Marko057c74a2013-12-03 15:20:45 +0000816 // Opcode will flip operands.
817 EmitRegArray(entry, reg, base, index, scale, disp);
818}
819
Mark Mendell2637f2e2014-04-30 10:10:47 -0400820void X86Mir2Lir::EmitArrayImm(const X86EncodingMap* entry, uint8_t base, uint8_t index, int scale,
821 int disp, int32_t imm) {
822 EmitPrefixAndOpcode(entry);
823 EmitModrmSibDisp(entry->skeleton.modrm_opcode, base, index, scale, disp);
824 DCHECK_EQ(0, entry->skeleton.ax_opcode);
Mark Mendell9ed42772014-05-07 17:26:12 -0400825 EmitImm(entry, imm);
Mark Mendell2637f2e2014-04-30 10:10:47 -0400826}
827
Vladimir Marko057c74a2013-12-03 15:20:45 +0000828void X86Mir2Lir::EmitRegThread(const X86EncodingMap* entry, uint8_t reg, int disp) {
829 DCHECK_NE(entry->skeleton.prefix1, 0);
830 EmitPrefixAndOpcode(entry);
buzbee091cc402014-03-31 10:14:40 -0700831 if (RegStorage::RegNum(reg) >= 4) {
832 DCHECK(strchr(entry->name, '8') == NULL) << entry->name << " "
833 << static_cast<int>(RegStorage::RegNum(reg))
Vladimir Marko057c74a2013-12-03 15:20:45 +0000834 << " in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
835 }
buzbee091cc402014-03-31 10:14:40 -0700836 DCHECK_LT(RegStorage::RegNum(reg), 8);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700837 EmitModrmThread(RegStorage::RegNum(reg));
Vladimir Marko057c74a2013-12-03 15:20:45 +0000838 code_buffer_.push_back(disp & 0xFF);
839 code_buffer_.push_back((disp >> 8) & 0xFF);
840 code_buffer_.push_back((disp >> 16) & 0xFF);
841 code_buffer_.push_back((disp >> 24) & 0xFF);
842 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
843 DCHECK_EQ(0, entry->skeleton.ax_opcode);
844 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
845}
846
847void X86Mir2Lir::EmitRegReg(const X86EncodingMap* entry, uint8_t reg1, uint8_t reg2) {
848 EmitPrefixAndOpcode(entry);
buzbee091cc402014-03-31 10:14:40 -0700849 DCHECK_LT(RegStorage::RegNum(reg1), 8);
850 DCHECK_LT(RegStorage::RegNum(reg2), 8);
851 uint8_t modrm = (3 << 6) | (RegStorage::RegNum(reg1) << 3) | RegStorage::RegNum(reg2);
Vladimir Marko057c74a2013-12-03 15:20:45 +0000852 code_buffer_.push_back(modrm);
853 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
854 DCHECK_EQ(0, entry->skeleton.ax_opcode);
855 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
856}
857
858void X86Mir2Lir::EmitRegRegImm(const X86EncodingMap* entry,
859 uint8_t reg1, uint8_t reg2, int32_t imm) {
860 EmitPrefixAndOpcode(entry);
buzbee091cc402014-03-31 10:14:40 -0700861 DCHECK_LT(RegStorage::RegNum(reg1), 8);
862 DCHECK_LT(RegStorage::RegNum(reg2), 8);
863 uint8_t modrm = (3 << 6) | (RegStorage::RegNum(reg1) << 3) | RegStorage::RegNum(reg2);
Vladimir Marko057c74a2013-12-03 15:20:45 +0000864 code_buffer_.push_back(modrm);
865 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
866 DCHECK_EQ(0, entry->skeleton.ax_opcode);
867 EmitImm(entry, imm);
868}
869
Mark Mendell4708dcd2014-01-22 09:05:18 -0800870void X86Mir2Lir::EmitRegRegImmRev(const X86EncodingMap* entry,
871 uint8_t reg1, uint8_t reg2, int32_t imm) {
872 EmitRegRegImm(entry, reg2, reg1, imm);
873}
874
875void X86Mir2Lir::EmitRegMemImm(const X86EncodingMap* entry,
876 uint8_t reg, uint8_t base, int disp, int32_t imm) {
877 EmitPrefixAndOpcode(entry);
buzbee091cc402014-03-31 10:14:40 -0700878 DCHECK(!RegStorage::IsFloat(reg));
879 DCHECK_LT(RegStorage::RegNum(reg), 8);
Mark Mendell4708dcd2014-01-22 09:05:18 -0800880 EmitModrmDisp(reg, base, disp);
881 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
882 DCHECK_EQ(0, entry->skeleton.ax_opcode);
883 EmitImm(entry, imm);
884}
885
Mark Mendell2637f2e2014-04-30 10:10:47 -0400886void X86Mir2Lir::EmitMemRegImm(const X86EncodingMap* entry,
887 uint8_t base, int disp, uint8_t reg, int32_t imm) {
888 EmitRegMemImm(entry, reg, base, disp, imm);
889}
890
Brian Carlstrom7940e442013-07-12 13:46:57 -0700891void X86Mir2Lir::EmitRegImm(const X86EncodingMap* entry, uint8_t reg, int imm) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700892 EmitPrefix(entry);
buzbee091cc402014-03-31 10:14:40 -0700893 if (RegStorage::RegNum(reg) == rs_rAX.GetRegNum() && entry->skeleton.ax_opcode != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700894 code_buffer_.push_back(entry->skeleton.ax_opcode);
895 } else {
Vladimir Marko057c74a2013-12-03 15:20:45 +0000896 EmitOpcode(entry);
buzbee091cc402014-03-31 10:14:40 -0700897 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | RegStorage::RegNum(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700898 code_buffer_.push_back(modrm);
899 }
Vladimir Marko057c74a2013-12-03 15:20:45 +0000900 EmitImm(entry, imm);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700901}
902
Mark Mendell343adb52013-12-18 06:02:17 -0800903void X86Mir2Lir::EmitMemImm(const X86EncodingMap* entry, uint8_t base, int disp, int32_t imm) {
904 EmitPrefixAndOpcode(entry);
905 EmitModrmDisp(entry->skeleton.modrm_opcode, base, disp);
906 DCHECK_EQ(0, entry->skeleton.ax_opcode);
907 EmitImm(entry, imm);
908}
909
Brian Carlstrom7940e442013-07-12 13:46:57 -0700910void X86Mir2Lir::EmitThreadImm(const X86EncodingMap* entry, int disp, int imm) {
Vladimir Marko057c74a2013-12-03 15:20:45 +0000911 EmitPrefixAndOpcode(entry);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700912 EmitModrmThread(entry->skeleton.modrm_opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700913 code_buffer_.push_back(disp & 0xFF);
914 code_buffer_.push_back((disp >> 8) & 0xFF);
915 code_buffer_.push_back((disp >> 16) & 0xFF);
916 code_buffer_.push_back((disp >> 24) & 0xFF);
Vladimir Marko057c74a2013-12-03 15:20:45 +0000917 EmitImm(entry, imm);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700918 DCHECK_EQ(entry->skeleton.ax_opcode, 0);
919}
920
921void X86Mir2Lir::EmitMovRegImm(const X86EncodingMap* entry, uint8_t reg, int imm) {
buzbee091cc402014-03-31 10:14:40 -0700922 DCHECK_LT(RegStorage::RegNum(reg), 8);
923 code_buffer_.push_back(0xB8 + RegStorage::RegNum(reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700924 code_buffer_.push_back(imm & 0xFF);
925 code_buffer_.push_back((imm >> 8) & 0xFF);
926 code_buffer_.push_back((imm >> 16) & 0xFF);
927 code_buffer_.push_back((imm >> 24) & 0xFF);
928}
929
930void X86Mir2Lir::EmitShiftRegImm(const X86EncodingMap* entry, uint8_t reg, int imm) {
Vladimir Marko057c74a2013-12-03 15:20:45 +0000931 EmitPrefix(entry);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700932 if (imm != 1) {
933 code_buffer_.push_back(entry->skeleton.opcode);
934 } else {
935 // Shorter encoding for 1 bit shift
936 code_buffer_.push_back(entry->skeleton.ax_opcode);
937 }
Vladimir Marko057c74a2013-12-03 15:20:45 +0000938 DCHECK_NE(0x0F, entry->skeleton.opcode);
939 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
940 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
buzbee091cc402014-03-31 10:14:40 -0700941 if (RegStorage::RegNum(reg) >= 4) {
942 DCHECK(strchr(entry->name, '8') == NULL) << entry->name << " "
943 << static_cast<int>(RegStorage::RegNum(reg))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700944 << " in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
945 }
buzbee091cc402014-03-31 10:14:40 -0700946 DCHECK_LT(RegStorage::RegNum(reg), 8);
947 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | RegStorage::RegNum(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700948 code_buffer_.push_back(modrm);
949 if (imm != 1) {
950 DCHECK_EQ(entry->skeleton.immediate_bytes, 1);
951 DCHECK(IS_SIMM8(imm));
952 code_buffer_.push_back(imm & 0xFF);
953 }
954}
955
956void X86Mir2Lir::EmitShiftRegCl(const X86EncodingMap* entry, uint8_t reg, uint8_t cl) {
buzbee091cc402014-03-31 10:14:40 -0700957 DCHECK_EQ(cl, static_cast<uint8_t>(rs_rCX.GetReg()));
Vladimir Marko057c74a2013-12-03 15:20:45 +0000958 EmitPrefix(entry);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700959 code_buffer_.push_back(entry->skeleton.opcode);
Vladimir Marko057c74a2013-12-03 15:20:45 +0000960 DCHECK_NE(0x0F, entry->skeleton.opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700961 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
962 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
buzbee091cc402014-03-31 10:14:40 -0700963 DCHECK_LT(RegStorage::RegNum(reg), 8);
964 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | RegStorage::RegNum(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700965 code_buffer_.push_back(modrm);
966 DCHECK_EQ(0, entry->skeleton.ax_opcode);
967 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
968}
969
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800970void X86Mir2Lir::EmitShiftMemCl(const X86EncodingMap* entry, uint8_t base,
971 int displacement, uint8_t cl) {
buzbee091cc402014-03-31 10:14:40 -0700972 DCHECK_EQ(cl, static_cast<uint8_t>(rs_rCX.GetReg()));
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800973 EmitPrefix(entry);
974 code_buffer_.push_back(entry->skeleton.opcode);
975 DCHECK_NE(0x0F, entry->skeleton.opcode);
976 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
977 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
buzbee091cc402014-03-31 10:14:40 -0700978 DCHECK_LT(RegStorage::RegNum(base), 8);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800979 EmitModrmDisp(entry->skeleton.modrm_opcode, base, displacement);
980 DCHECK_EQ(0, entry->skeleton.ax_opcode);
981 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
982}
983
Mark Mendell2637f2e2014-04-30 10:10:47 -0400984void X86Mir2Lir::EmitShiftMemImm(const X86EncodingMap* entry, uint8_t base,
985 int displacement, int imm) {
986 EmitPrefix(entry);
987 if (imm != 1) {
988 code_buffer_.push_back(entry->skeleton.opcode);
989 } else {
990 // Shorter encoding for 1 bit shift
991 code_buffer_.push_back(entry->skeleton.ax_opcode);
992 }
993 DCHECK_NE(0x0F, entry->skeleton.opcode);
994 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
995 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
996 EmitModrmDisp(entry->skeleton.modrm_opcode, base, displacement);
997 if (imm != 1) {
998 DCHECK_EQ(entry->skeleton.immediate_bytes, 1);
999 DCHECK(IS_SIMM8(imm));
1000 code_buffer_.push_back(imm & 0xFF);
1001 }
1002}
1003
Brian Carlstrom7940e442013-07-12 13:46:57 -07001004void X86Mir2Lir::EmitRegCond(const X86EncodingMap* entry, uint8_t reg, uint8_t condition) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001005 EmitPrefix(entry);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001006 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1007 DCHECK_EQ(0x0F, entry->skeleton.opcode);
1008 code_buffer_.push_back(0x0F);
1009 DCHECK_EQ(0x90, entry->skeleton.extra_opcode1);
1010 code_buffer_.push_back(0x90 | condition);
1011 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
buzbee091cc402014-03-31 10:14:40 -07001012 DCHECK_LT(RegStorage::RegNum(reg), 8);
1013 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | RegStorage::RegNum(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001014 code_buffer_.push_back(modrm);
1015 DCHECK_EQ(entry->skeleton.immediate_bytes, 0);
1016}
1017
Mark Mendell2637f2e2014-04-30 10:10:47 -04001018void X86Mir2Lir::EmitMemCond(const X86EncodingMap* entry, uint8_t base, int displacement, uint8_t condition) {
1019 if (entry->skeleton.prefix1 != 0) {
1020 code_buffer_.push_back(entry->skeleton.prefix1);
1021 if (entry->skeleton.prefix2 != 0) {
1022 code_buffer_.push_back(entry->skeleton.prefix2);
1023 }
1024 } else {
1025 DCHECK_EQ(0, entry->skeleton.prefix2);
1026 }
1027 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1028 DCHECK_EQ(0x0F, entry->skeleton.opcode);
1029 code_buffer_.push_back(0x0F);
1030 DCHECK_EQ(0x90, entry->skeleton.extra_opcode1);
1031 code_buffer_.push_back(0x90 | condition);
1032 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1033 EmitModrmDisp(entry->skeleton.modrm_opcode, base, displacement);
1034 DCHECK_EQ(entry->skeleton.immediate_bytes, 0);
1035}
1036
buzbee091cc402014-03-31 10:14:40 -07001037void X86Mir2Lir::EmitRegRegCond(const X86EncodingMap* entry, uint8_t reg1, uint8_t reg2,
1038 uint8_t condition) {
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001039 // Generate prefix and opcode without the condition
1040 EmitPrefixAndOpcode(entry);
1041
1042 // Now add the condition. The last byte of opcode is the one that receives it.
1043 DCHECK_LE(condition, 0xF);
1044 code_buffer_.back() += condition;
1045
1046 // Not expecting to have to encode immediate or do anything special for ModR/M since there are two registers.
1047 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1048 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1049
1050 // Check that registers requested for encoding are sane.
buzbee091cc402014-03-31 10:14:40 -07001051 DCHECK_LT(RegStorage::RegNum(reg1), 8);
1052 DCHECK_LT(RegStorage::RegNum(reg2), 8);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001053
1054 // For register to register encoding, the mod is 3.
1055 const uint8_t mod = (3 << 6);
1056
1057 // Encode the ModR/M byte now.
buzbee091cc402014-03-31 10:14:40 -07001058 const uint8_t modrm = mod | (RegStorage::RegNum(reg1) << 3) | RegStorage::RegNum(reg2);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001059 code_buffer_.push_back(modrm);
1060}
1061
Mark Mendell2637f2e2014-04-30 10:10:47 -04001062void X86Mir2Lir::EmitRegMemCond(const X86EncodingMap* entry, uint8_t reg1, uint8_t base, int displacement, uint8_t condition) {
1063 // Generate prefix and opcode without the condition
1064 EmitPrefixAndOpcode(entry);
1065
1066 // Now add the condition. The last byte of opcode is the one that receives it.
1067 DCHECK_LE(condition, 0xF);
1068 code_buffer_.back() += condition;
1069
1070 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1071 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1072
1073 // Check that registers requested for encoding are sane.
1074 DCHECK_LT(reg1, 8);
1075 DCHECK_LT(base, 8);
1076
1077 EmitModrmDisp(reg1, base, displacement);
1078}
1079
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080void X86Mir2Lir::EmitJmp(const X86EncodingMap* entry, int rel) {
1081 if (entry->opcode == kX86Jmp8) {
1082 DCHECK(IS_SIMM8(rel));
1083 code_buffer_.push_back(0xEB);
1084 code_buffer_.push_back(rel & 0xFF);
1085 } else if (entry->opcode == kX86Jmp32) {
1086 code_buffer_.push_back(0xE9);
1087 code_buffer_.push_back(rel & 0xFF);
1088 code_buffer_.push_back((rel >> 8) & 0xFF);
1089 code_buffer_.push_back((rel >> 16) & 0xFF);
1090 code_buffer_.push_back((rel >> 24) & 0xFF);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001091 } else if (entry->opcode == kX86Jecxz8) {
1092 DCHECK(IS_SIMM8(rel));
1093 code_buffer_.push_back(0xE3);
1094 code_buffer_.push_back(rel & 0xFF);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095 } else {
1096 DCHECK(entry->opcode == kX86JmpR);
1097 code_buffer_.push_back(entry->skeleton.opcode);
1098 uint8_t reg = static_cast<uint8_t>(rel);
buzbee091cc402014-03-31 10:14:40 -07001099 DCHECK_LT(RegStorage::RegNum(reg), 8);
1100 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | RegStorage::RegNum(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001101 code_buffer_.push_back(modrm);
1102 }
1103}
1104
1105void X86Mir2Lir::EmitJcc(const X86EncodingMap* entry, int rel, uint8_t cc) {
1106 DCHECK_LT(cc, 16);
1107 if (entry->opcode == kX86Jcc8) {
1108 DCHECK(IS_SIMM8(rel));
1109 code_buffer_.push_back(0x70 | cc);
1110 code_buffer_.push_back(rel & 0xFF);
1111 } else {
1112 DCHECK(entry->opcode == kX86Jcc32);
1113 code_buffer_.push_back(0x0F);
1114 code_buffer_.push_back(0x80 | cc);
1115 code_buffer_.push_back(rel & 0xFF);
1116 code_buffer_.push_back((rel >> 8) & 0xFF);
1117 code_buffer_.push_back((rel >> 16) & 0xFF);
1118 code_buffer_.push_back((rel >> 24) & 0xFF);
1119 }
1120}
1121
1122void X86Mir2Lir::EmitCallMem(const X86EncodingMap* entry, uint8_t base, int disp) {
Vladimir Marko057c74a2013-12-03 15:20:45 +00001123 EmitPrefixAndOpcode(entry);
1124 EmitModrmDisp(entry->skeleton.modrm_opcode, base, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001125 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1126 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1127}
1128
Mark Mendell55d0eac2014-02-06 11:02:52 -08001129void X86Mir2Lir::EmitCallImmediate(const X86EncodingMap* entry, int disp) {
1130 EmitPrefixAndOpcode(entry);
1131 DCHECK_EQ(4, entry->skeleton.immediate_bytes);
1132 code_buffer_.push_back(disp & 0xFF);
1133 code_buffer_.push_back((disp >> 8) & 0xFF);
1134 code_buffer_.push_back((disp >> 16) & 0xFF);
1135 code_buffer_.push_back((disp >> 24) & 0xFF);
1136 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1137}
1138
Brian Carlstrom7940e442013-07-12 13:46:57 -07001139void X86Mir2Lir::EmitCallThread(const X86EncodingMap* entry, int disp) {
1140 DCHECK_NE(entry->skeleton.prefix1, 0);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001141 EmitPrefixAndOpcode(entry);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001142 EmitModrmThread(entry->skeleton.modrm_opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001143 code_buffer_.push_back(disp & 0xFF);
1144 code_buffer_.push_back((disp >> 8) & 0xFF);
1145 code_buffer_.push_back((disp >> 16) & 0xFF);
1146 code_buffer_.push_back((disp >> 24) & 0xFF);
1147 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1148 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1149}
1150
1151void X86Mir2Lir::EmitPcRel(const X86EncodingMap* entry, uint8_t reg,
1152 int base_or_table, uint8_t index, int scale, int table_or_disp) {
1153 int disp;
1154 if (entry->opcode == kX86PcRelLoadRA) {
buzbee0d829482013-10-11 15:24:55 -07001155 Mir2Lir::EmbeddedData *tab_rec =
1156 reinterpret_cast<Mir2Lir::EmbeddedData*>(UnwrapPointer(table_or_disp));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001157 disp = tab_rec->offset;
1158 } else {
1159 DCHECK(entry->opcode == kX86PcRelAdr);
buzbee0d829482013-10-11 15:24:55 -07001160 Mir2Lir::EmbeddedData *tab_rec =
1161 reinterpret_cast<Mir2Lir::EmbeddedData*>(UnwrapPointer(base_or_table));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001162 disp = tab_rec->offset;
1163 }
Vladimir Marko057c74a2013-12-03 15:20:45 +00001164 EmitPrefix(entry);
buzbee091cc402014-03-31 10:14:40 -07001165 DCHECK_LT(RegStorage::RegNum(reg), 8);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001166 if (entry->opcode == kX86PcRelLoadRA) {
1167 code_buffer_.push_back(entry->skeleton.opcode);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001168 DCHECK_NE(0x0F, entry->skeleton.opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001169 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1170 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
buzbee091cc402014-03-31 10:14:40 -07001171 uint8_t modrm = (2 << 6) | (RegStorage::RegNum(reg) << 3) | rs_rX86_SP.GetRegNum();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001172 code_buffer_.push_back(modrm);
1173 DCHECK_LT(scale, 4);
buzbee091cc402014-03-31 10:14:40 -07001174 DCHECK_LT(RegStorage::RegNum(index), 8);
1175 DCHECK_LT(RegStorage::RegNum(base_or_table), 8);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001176 uint8_t base = static_cast<uint8_t>(base_or_table);
buzbee091cc402014-03-31 10:14:40 -07001177 uint8_t sib = (scale << 6) | (RegStorage::RegNum(index) << 3) | RegStorage::RegNum(base);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001178 code_buffer_.push_back(sib);
1179 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1180 } else {
buzbee091cc402014-03-31 10:14:40 -07001181 code_buffer_.push_back(entry->skeleton.opcode + RegStorage::RegNum(reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001182 }
1183 code_buffer_.push_back(disp & 0xFF);
1184 code_buffer_.push_back((disp >> 8) & 0xFF);
1185 code_buffer_.push_back((disp >> 16) & 0xFF);
1186 code_buffer_.push_back((disp >> 24) & 0xFF);
1187 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1188 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1189}
1190
1191void X86Mir2Lir::EmitMacro(const X86EncodingMap* entry, uint8_t reg, int offset) {
1192 DCHECK(entry->opcode == kX86StartOfMethod) << entry->name;
1193 code_buffer_.push_back(0xE8); // call +0
1194 code_buffer_.push_back(0);
1195 code_buffer_.push_back(0);
1196 code_buffer_.push_back(0);
1197 code_buffer_.push_back(0);
1198
buzbee091cc402014-03-31 10:14:40 -07001199 DCHECK_LT(RegStorage::RegNum(reg), 8);
1200 code_buffer_.push_back(0x58 + RegStorage::RegNum(reg)); // pop reg
Brian Carlstrom7940e442013-07-12 13:46:57 -07001201
buzbee091cc402014-03-31 10:14:40 -07001202 EmitRegImm(&X86Mir2Lir::EncodingMap[kX86Sub32RI], RegStorage::RegNum(reg),
1203 offset + 5 /* size of call +0 */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001204}
1205
1206void X86Mir2Lir::EmitUnimplemented(const X86EncodingMap* entry, LIR* lir) {
1207 UNIMPLEMENTED(WARNING) << "encoding kind for " << entry->name << " "
1208 << BuildInsnString(entry->fmt, lir, 0);
1209 for (int i = 0; i < GetInsnSize(lir); ++i) {
1210 code_buffer_.push_back(0xCC); // push breakpoint instruction - int 3
1211 }
1212}
1213
1214/*
1215 * Assemble the LIR into binary instruction format. Note that we may
1216 * discover that pc-relative displacements may not fit the selected
1217 * instruction. In those cases we will try to substitute a new code
1218 * sequence or request that the trace be shortened and retried.
1219 */
buzbee0d829482013-10-11 15:24:55 -07001220AssemblerStatus X86Mir2Lir::AssembleInstructions(CodeOffset start_addr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001221 LIR *lir;
1222 AssemblerStatus res = kSuccess; // Assume success
1223
1224 const bool kVerbosePcFixup = false;
1225 for (lir = first_lir_insn_; lir != NULL; lir = NEXT_LIR(lir)) {
buzbee409fe942013-10-11 10:49:56 -07001226 if (IsPseudoLirOp(lir->opcode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001227 continue;
1228 }
1229
1230 if (lir->flags.is_nop) {
1231 continue;
1232 }
1233
buzbeeb48819d2013-09-14 16:15:25 -07001234 if (lir->flags.fixup != kFixupNone) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001235 switch (lir->opcode) {
1236 case kX86Jcc8: {
1237 LIR *target_lir = lir->target;
1238 DCHECK(target_lir != NULL);
1239 int delta = 0;
buzbee0d829482013-10-11 15:24:55 -07001240 CodeOffset pc;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001241 if (IS_SIMM8(lir->operands[0])) {
1242 pc = lir->offset + 2 /* opcode + rel8 */;
1243 } else {
1244 pc = lir->offset + 6 /* 2 byte opcode + rel32 */;
1245 }
buzbee0d829482013-10-11 15:24:55 -07001246 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001247 delta = target - pc;
1248 if (IS_SIMM8(delta) != IS_SIMM8(lir->operands[0])) {
1249 if (kVerbosePcFixup) {
1250 LOG(INFO) << "Retry for JCC growth at " << lir->offset
1251 << " delta: " << delta << " old delta: " << lir->operands[0];
1252 }
1253 lir->opcode = kX86Jcc32;
1254 SetupResourceMasks(lir);
1255 res = kRetryAll;
1256 }
1257 if (kVerbosePcFixup) {
1258 LOG(INFO) << "Source:";
1259 DumpLIRInsn(lir, 0);
1260 LOG(INFO) << "Target:";
1261 DumpLIRInsn(target_lir, 0);
1262 LOG(INFO) << "Delta " << delta;
1263 }
1264 lir->operands[0] = delta;
1265 break;
1266 }
1267 case kX86Jcc32: {
1268 LIR *target_lir = lir->target;
1269 DCHECK(target_lir != NULL);
buzbee0d829482013-10-11 15:24:55 -07001270 CodeOffset pc = lir->offset + 6 /* 2 byte opcode + rel32 */;
1271 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001272 int delta = target - pc;
1273 if (kVerbosePcFixup) {
1274 LOG(INFO) << "Source:";
1275 DumpLIRInsn(lir, 0);
1276 LOG(INFO) << "Target:";
1277 DumpLIRInsn(target_lir, 0);
1278 LOG(INFO) << "Delta " << delta;
1279 }
1280 lir->operands[0] = delta;
1281 break;
1282 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001283 case kX86Jecxz8: {
1284 LIR *target_lir = lir->target;
1285 DCHECK(target_lir != NULL);
1286 CodeOffset pc;
1287 pc = lir->offset + 2; // opcode + rel8
1288 CodeOffset target = target_lir->offset;
1289 int delta = target - pc;
1290 lir->operands[0] = delta;
1291 DCHECK(IS_SIMM8(delta));
1292 break;
1293 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001294 case kX86Jmp8: {
1295 LIR *target_lir = lir->target;
1296 DCHECK(target_lir != NULL);
1297 int delta = 0;
buzbee0d829482013-10-11 15:24:55 -07001298 CodeOffset pc;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001299 if (IS_SIMM8(lir->operands[0])) {
1300 pc = lir->offset + 2 /* opcode + rel8 */;
1301 } else {
1302 pc = lir->offset + 5 /* opcode + rel32 */;
1303 }
buzbee0d829482013-10-11 15:24:55 -07001304 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001305 delta = target - pc;
1306 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && delta == 0) {
1307 // Useless branch
buzbee252254b2013-09-08 16:20:53 -07001308 NopLIR(lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001309 if (kVerbosePcFixup) {
1310 LOG(INFO) << "Retry for useless branch at " << lir->offset;
1311 }
1312 res = kRetryAll;
1313 } else if (IS_SIMM8(delta) != IS_SIMM8(lir->operands[0])) {
1314 if (kVerbosePcFixup) {
1315 LOG(INFO) << "Retry for JMP growth at " << lir->offset;
1316 }
1317 lir->opcode = kX86Jmp32;
1318 SetupResourceMasks(lir);
1319 res = kRetryAll;
1320 }
1321 lir->operands[0] = delta;
1322 break;
1323 }
1324 case kX86Jmp32: {
1325 LIR *target_lir = lir->target;
1326 DCHECK(target_lir != NULL);
buzbee0d829482013-10-11 15:24:55 -07001327 CodeOffset pc = lir->offset + 5 /* opcode + rel32 */;
1328 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001329 int delta = target - pc;
1330 lir->operands[0] = delta;
1331 break;
1332 }
1333 default:
Mark Mendell67c39c42014-01-31 17:28:00 -08001334 if (lir->flags.fixup == kFixupLoad) {
1335 LIR *target_lir = lir->target;
1336 DCHECK(target_lir != NULL);
1337 CodeOffset target = target_lir->offset;
1338 lir->operands[2] = target;
1339 int newSize = GetInsnSize(lir);
1340 if (newSize != lir->flags.size) {
1341 lir->flags.size = newSize;
1342 res = kRetryAll;
1343 }
1344 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001345 break;
1346 }
1347 }
1348
1349 /*
1350 * If one of the pc-relative instructions expanded we'll have
1351 * to make another pass. Don't bother to fully assemble the
1352 * instruction.
1353 */
1354 if (res != kSuccess) {
1355 continue;
1356 }
1357 CHECK_EQ(static_cast<size_t>(lir->offset), code_buffer_.size());
1358 const X86EncodingMap *entry = &X86Mir2Lir::EncodingMap[lir->opcode];
1359 size_t starting_cbuf_size = code_buffer_.size();
1360 switch (entry->kind) {
1361 case kData: // 4 bytes of data
1362 code_buffer_.push_back(lir->operands[0]);
1363 break;
1364 case kNullary: // 1 byte of opcode
1365 DCHECK_EQ(0, entry->skeleton.prefix1);
1366 DCHECK_EQ(0, entry->skeleton.prefix2);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001367 EmitOpcode(entry);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001368 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1369 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1370 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1371 break;
Mark Mendell4028a6c2014-02-19 20:06:20 -08001372 case kPrefix2Nullary: // 1 byte of opcode + 2 prefixes.
1373 DCHECK_NE(0, entry->skeleton.prefix1);
1374 DCHECK_NE(0, entry->skeleton.prefix2);
1375 EmitPrefixAndOpcode(entry);
1376 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1377 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1378 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1379 break;
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001380 case kRegOpcode: // lir operands - 0: reg
1381 EmitOpRegOpcode(entry, lir->operands[0]);
1382 break;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001383 case kReg64:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001384 case kReg: // lir operands - 0: reg
1385 EmitOpReg(entry, lir->operands[0]);
1386 break;
1387 case kMem: // lir operands - 0: base, 1: disp
1388 EmitOpMem(entry, lir->operands[0], lir->operands[1]);
1389 break;
Vladimir Marko057c74a2013-12-03 15:20:45 +00001390 case kArray: // lir operands - 0: base, 1: index, 2: scale, 3: disp
1391 EmitOpArray(entry, lir->operands[0], lir->operands[1], lir->operands[2], lir->operands[3]);
1392 break;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001393 case kMemReg64:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001394 case kMemReg: // lir operands - 0: base, 1: disp, 2: reg
1395 EmitMemReg(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1396 break;
Mark Mendell343adb52013-12-18 06:02:17 -08001397 case kMemImm: // lir operands - 0: base, 1: disp, 2: immediate
1398 EmitMemImm(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1399 break;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001400 case kArrayImm: // lir operands - 0: base, 1: index, 2: disp, 3:scale, 4:immediate
1401 EmitArrayImm(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1402 lir->operands[3], lir->operands[4]);
1403 break;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001404 case kArrayReg64:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001405 case kArrayReg: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
1406 EmitArrayReg(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1407 lir->operands[3], lir->operands[4]);
1408 break;
1409 case kRegMem: // lir operands - 0: reg, 1: base, 2: disp
1410 EmitRegMem(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1411 break;
1412 case kRegArray: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
1413 EmitRegArray(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1414 lir->operands[3], lir->operands[4]);
1415 break;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001416 case kReg64Thread: // lir operands - 0: reg, 1: disp
Brian Carlstrom7940e442013-07-12 13:46:57 -07001417 case kRegThread: // lir operands - 0: reg, 1: disp
1418 EmitRegThread(entry, lir->operands[0], lir->operands[1]);
1419 break;
1420 case kRegReg: // lir operands - 0: reg1, 1: reg2
1421 EmitRegReg(entry, lir->operands[0], lir->operands[1]);
1422 break;
1423 case kRegRegStore: // lir operands - 0: reg2, 1: reg1
1424 EmitRegReg(entry, lir->operands[1], lir->operands[0]);
1425 break;
Mark Mendell4708dcd2014-01-22 09:05:18 -08001426 case kRegRegImmRev:
1427 EmitRegRegImmRev(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1428 break;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001429 case kMemRegImm:
1430 EmitMemRegImm(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1431 lir->operands[3]);
1432 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001433 case kRegRegImm:
1434 EmitRegRegImm(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1435 break;
Mark Mendell4708dcd2014-01-22 09:05:18 -08001436 case kRegMemImm:
1437 EmitRegMemImm(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1438 lir->operands[3]);
1439 break;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001440 case kReg64Imm:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001441 case kRegImm: // lir operands - 0: reg, 1: immediate
1442 EmitRegImm(entry, lir->operands[0], lir->operands[1]);
1443 break;
1444 case kThreadImm: // lir operands - 0: disp, 1: immediate
1445 EmitThreadImm(entry, lir->operands[0], lir->operands[1]);
1446 break;
1447 case kMovRegImm: // lir operands - 0: reg, 1: immediate
1448 EmitMovRegImm(entry, lir->operands[0], lir->operands[1]);
1449 break;
1450 case kShiftRegImm: // lir operands - 0: reg, 1: immediate
1451 EmitShiftRegImm(entry, lir->operands[0], lir->operands[1]);
1452 break;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001453 case kShiftMemImm: // lir operands - 0: base, 1: disp, 2:immediate
1454 EmitShiftMemImm(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1455 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001456 case kShiftRegCl: // lir operands - 0: reg, 1: cl
Brian Carlstrom7940e442013-07-12 13:46:57 -07001457 EmitShiftRegCl(entry, lir->operands[0], lir->operands[1]);
1458 break;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001459 case kShiftMemCl: // lir operands - 0: base, 1:displacement, 2: cl
1460 EmitShiftMemCl(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1461 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001462 case kRegCond: // lir operands - 0: reg, 1: condition
1463 EmitRegCond(entry, lir->operands[0], lir->operands[1]);
1464 break;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001465 case kMemCond: // lir operands - 0: base, 1: displacement, 2: condition
1466 EmitMemCond(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1467 break;
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001468 case kRegRegCond: // lir operands - 0: reg, 1: reg, 2: condition
1469 EmitRegRegCond(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1470 break;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001471 case kRegMemCond: // lir operands - 0: reg, 1: reg, displacement, 3: condition
1472 EmitRegMemCond(entry, lir->operands[0], lir->operands[1], lir->operands[2], lir->operands[3]);
1473 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001474 case kJmp: // lir operands - 0: rel
Brian Carlstrom60d7a652014-03-13 18:10:08 -07001475 if (entry->opcode == kX86JmpT) {
1476 // This works since the instruction format for jmp and call is basically the same and
1477 // EmitCallThread loads opcode info.
1478 EmitCallThread(entry, lir->operands[0]);
1479 } else {
1480 EmitJmp(entry, lir->operands[0]);
1481 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001482 break;
1483 case kJcc: // lir operands - 0: rel, 1: CC, target assigned
1484 EmitJcc(entry, lir->operands[0], lir->operands[1]);
1485 break;
1486 case kCall:
1487 switch (entry->opcode) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001488 case kX86CallI: // lir operands - 0: disp
1489 EmitCallImmediate(entry, lir->operands[0]);
1490 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001491 case kX86CallM: // lir operands - 0: base, 1: disp
1492 EmitCallMem(entry, lir->operands[0], lir->operands[1]);
1493 break;
1494 case kX86CallT: // lir operands - 0: disp
1495 EmitCallThread(entry, lir->operands[0]);
1496 break;
1497 default:
1498 EmitUnimplemented(entry, lir);
1499 break;
1500 }
1501 break;
1502 case kPcRel: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
1503 EmitPcRel(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1504 lir->operands[3], lir->operands[4]);
1505 break;
1506 case kMacro:
1507 EmitMacro(entry, lir->operands[0], lir->offset);
1508 break;
1509 default:
1510 EmitUnimplemented(entry, lir);
1511 break;
1512 }
1513 CHECK_EQ(static_cast<size_t>(GetInsnSize(lir)),
1514 code_buffer_.size() - starting_cbuf_size)
1515 << "Instruction size mismatch for entry: " << X86Mir2Lir::EncodingMap[lir->opcode].name;
1516 }
1517 return res;
1518}
1519
buzbeeb48819d2013-09-14 16:15:25 -07001520// LIR offset assignment.
1521// TODO: consolidate w/ Arm assembly mechanism.
1522int X86Mir2Lir::AssignInsnOffsets() {
1523 LIR* lir;
1524 int offset = 0;
1525
1526 for (lir = first_lir_insn_; lir != NULL; lir = NEXT_LIR(lir)) {
1527 lir->offset = offset;
buzbee409fe942013-10-11 10:49:56 -07001528 if (LIKELY(!IsPseudoLirOp(lir->opcode))) {
buzbeeb48819d2013-09-14 16:15:25 -07001529 if (!lir->flags.is_nop) {
1530 offset += lir->flags.size;
1531 }
1532 } else if (UNLIKELY(lir->opcode == kPseudoPseudoAlign4)) {
1533 if (offset & 0x2) {
1534 offset += 2;
1535 lir->operands[0] = 1;
1536 } else {
1537 lir->operands[0] = 0;
1538 }
1539 }
1540 /* Pseudo opcodes don't consume space */
1541 }
1542 return offset;
1543}
1544
1545/*
1546 * Walk the compilation unit and assign offsets to instructions
1547 * and literals and compute the total size of the compiled unit.
1548 * TODO: consolidate w/ Arm assembly mechanism.
1549 */
1550void X86Mir2Lir::AssignOffsets() {
1551 int offset = AssignInsnOffsets();
1552
Mark Mendelld65c51a2014-04-29 16:55:20 -04001553 if (const_vectors_ != nullptr) {
1554 /* assign offsets to vector literals */
1555
1556 // First, get offset to 12 mod 16 to align to 16 byte boundary.
1557 // This will ensure that the vector is 16 byte aligned, as the procedure is
1558 // always aligned at at 4 mod 16.
1559 int align_size = (16-4) - (offset & 0xF);
1560 if (align_size < 0) {
1561 align_size += 16;
1562 }
1563
1564 offset += align_size;
1565
1566 // Now assign each literal the right offset.
1567 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
1568 p->offset = offset;
1569 offset += 16;
1570 }
1571 }
1572
buzbeeb48819d2013-09-14 16:15:25 -07001573 /* Const values have to be word aligned */
Andreas Gampe66018822014-05-05 20:47:19 -07001574 offset = RoundUp(offset, 4);
buzbeeb48819d2013-09-14 16:15:25 -07001575
1576 /* Set up offsets for literals */
1577 data_offset_ = offset;
1578
1579 offset = AssignLiteralOffset(offset);
1580
1581 offset = AssignSwitchTablesOffset(offset);
1582
1583 offset = AssignFillArrayDataOffset(offset);
1584
1585 total_size_ = offset;
1586}
1587
1588/*
1589 * Go over each instruction in the list and calculate the offset from the top
1590 * before sending them off to the assembler. If out-of-range branch distance is
1591 * seen rearrange the instructions a bit to correct it.
1592 * TODO: consolidate w/ Arm assembly mechanism.
1593 */
1594void X86Mir2Lir::AssembleLIR() {
buzbeea61f4952013-08-23 14:27:06 -07001595 cu_->NewTimingSplit("Assemble");
Mark Mendell55d0eac2014-02-06 11:02:52 -08001596
1597 // We will remove the method address if we never ended up using it
1598 if (store_method_addr_ && !store_method_addr_used_) {
1599 setup_method_address_[0]->flags.is_nop = true;
1600 setup_method_address_[1]->flags.is_nop = true;
1601 }
1602
buzbeeb48819d2013-09-14 16:15:25 -07001603 AssignOffsets();
1604 int assembler_retries = 0;
1605 /*
1606 * Assemble here. Note that we generate code with optimistic assumptions
1607 * and if found now to work, we'll have to redo the sequence and retry.
1608 */
1609
1610 while (true) {
1611 AssemblerStatus res = AssembleInstructions(0);
1612 if (res == kSuccess) {
1613 break;
1614 } else {
1615 assembler_retries++;
1616 if (assembler_retries > MAX_ASSEMBLER_RETRIES) {
1617 CodegenDump();
1618 LOG(FATAL) << "Assembler error - too many retries";
1619 }
1620 // Redo offsets and try again
1621 AssignOffsets();
1622 code_buffer_.clear();
1623 }
1624 }
1625
1626 // Install literals
1627 InstallLiteralPools();
1628
1629 // Install switch tables
1630 InstallSwitchTables();
1631
1632 // Install fill array data
1633 InstallFillArrayData();
1634
1635 // Create the mapping table and native offset to reference map.
buzbeea61f4952013-08-23 14:27:06 -07001636 cu_->NewTimingSplit("PcMappingTable");
buzbeeb48819d2013-09-14 16:15:25 -07001637 CreateMappingTables();
1638
buzbeea61f4952013-08-23 14:27:06 -07001639 cu_->NewTimingSplit("GcMap");
buzbeeb48819d2013-09-14 16:15:25 -07001640 CreateNativeGcMap();
1641}
1642
Brian Carlstrom7940e442013-07-12 13:46:57 -07001643} // namespace art