blob: 2047f307658eadd37ca7adf934ada7de3ce5a8ab [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", "" },
28 { kX86Nop, kNop, IS_UNARY_OP, { 0, 0, 0x90, 0, 0, 0, 0, 0 }, "nop", "" },
29
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" }, \
66{ 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" }, \
67{ 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" }, \
68{ 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" }, \
69{ 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]" }, \
70{ 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]" }, \
71{ 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]" }, \
72{ 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" }, \
73{ 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" }, \
74{ 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" }, \
75{ 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" }, \
76{ 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" }, \
77{ 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" }, \
78{ 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" }, \
79{ 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" }
80
81ENCODING_MAP(Add, IS_LOAD | IS_STORE, REG_DEF0, 0,
82 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */,
83 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */,
84 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */,
85 0x80, 0x0 /* RegMem8/imm8 */,
86 0x81, 0x0 /* RegMem32/imm32 */, 0x83, 0x0 /* RegMem32/imm8 */),
87ENCODING_MAP(Or, IS_LOAD | IS_STORE, REG_DEF0, 0,
88 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */,
89 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */,
90 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */,
91 0x80, 0x1 /* RegMem8/imm8 */,
92 0x81, 0x1 /* RegMem32/imm32 */, 0x83, 0x1 /* RegMem32/imm8 */),
93ENCODING_MAP(Adc, IS_LOAD | IS_STORE, REG_DEF0, USES_CCODES,
94 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */,
95 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */,
96 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */,
97 0x80, 0x2 /* RegMem8/imm8 */,
98 0x81, 0x2 /* RegMem32/imm32 */, 0x83, 0x2 /* RegMem32/imm8 */),
99ENCODING_MAP(Sbb, IS_LOAD | IS_STORE, REG_DEF0, USES_CCODES,
100 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */,
101 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */,
102 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */,
103 0x80, 0x3 /* RegMem8/imm8 */,
104 0x81, 0x3 /* RegMem32/imm32 */, 0x83, 0x3 /* RegMem32/imm8 */),
105ENCODING_MAP(And, IS_LOAD | IS_STORE, REG_DEF0, 0,
106 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */,
107 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */,
108 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */,
109 0x80, 0x4 /* RegMem8/imm8 */,
110 0x81, 0x4 /* RegMem32/imm32 */, 0x83, 0x4 /* RegMem32/imm8 */),
111ENCODING_MAP(Sub, IS_LOAD | IS_STORE, REG_DEF0, 0,
112 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */,
113 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */,
114 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */,
115 0x80, 0x5 /* RegMem8/imm8 */,
116 0x81, 0x5 /* RegMem32/imm32 */, 0x83, 0x5 /* RegMem32/imm8 */),
117ENCODING_MAP(Xor, IS_LOAD | IS_STORE, REG_DEF0, 0,
118 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */,
119 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */,
120 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */,
121 0x80, 0x6 /* RegMem8/imm8 */,
122 0x81, 0x6 /* RegMem32/imm32 */, 0x83, 0x6 /* RegMem32/imm8 */),
123ENCODING_MAP(Cmp, IS_LOAD, 0, 0,
124 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */,
125 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */,
126 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */,
127 0x80, 0x7 /* RegMem8/imm8 */,
128 0x81, 0x7 /* RegMem32/imm32 */, 0x83, 0x7 /* RegMem32/imm8 */),
129#undef ENCODING_MAP
130
131 { kX86Imul16RRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { 0x66, 0, 0x69, 0, 0, 0, 0, 2 }, "Imul16RRI", "!0r,!1r,!2d" },
132 { 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" },
133 { 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" },
134
135 { kX86Imul32RRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { 0, 0, 0x69, 0, 0, 0, 0, 4 }, "Imul32RRI", "!0r,!1r,!2d" },
136 { 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" },
137 { 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" },
138 { kX86Imul32RRI8, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { 0, 0, 0x6B, 0, 0, 0, 0, 1 }, "Imul32RRI8", "!0r,!1r,!2d" },
139 { 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" },
140 { 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" },
141
142 { kX86Mov8MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0, 0, 0x88, 0, 0, 0, 0, 0 }, "Mov8MR", "[!0r+!1d],!2r" },
143 { kX86Mov8AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0, 0, 0x88, 0, 0, 0, 0, 0 }, "Mov8AR", "[!0r+!1r<<!2d+!3d],!4r" },
144 { kX86Mov8TR, kThreadReg, IS_STORE | IS_BINARY_OP | REG_USE1, { THREAD_PREFIX, 0, 0x88, 0, 0, 0, 0, 0 }, "Mov8TR", "fs:[!0d],!1r" },
145 { kX86Mov8RR, kRegReg, IS_BINARY_OP | REG_DEF0_USE1, { 0, 0, 0x8A, 0, 0, 0, 0, 0 }, "Mov8RR", "!0r,!1r" },
146 { kX86Mov8RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { 0, 0, 0x8A, 0, 0, 0, 0, 0 }, "Mov8RM", "!0r,[!1r+!2d]" },
147 { kX86Mov8RA, kRegArray, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8A, 0, 0, 0, 0, 0 }, "Mov8RA", "!0r,[!1r+!2r<<!3d+!4d]" },
148 { kX86Mov8RT, kRegThread, IS_LOAD | IS_BINARY_OP | REG_DEF0, { THREAD_PREFIX, 0, 0x8A, 0, 0, 0, 0, 0 }, "Mov8RT", "!0r,fs:[!1d]" },
149 { kX86Mov8RI, kMovRegImm, IS_BINARY_OP | REG_DEF0, { 0, 0, 0xB0, 0, 0, 0, 0, 1 }, "Mov8RI", "!0r,!1d" },
150 { kX86Mov8MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { 0, 0, 0xC6, 0, 0, 0, 0, 1 }, "Mov8MI", "[!0r+!1d],!2d" },
151 { kX86Mov8AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { 0, 0, 0xC6, 0, 0, 0, 0, 1 }, "Mov8AI", "[!0r+!1r<<!2d+!3d],!4d" },
152 { kX86Mov8TI, kThreadImm, IS_STORE | IS_BINARY_OP, { THREAD_PREFIX, 0, 0xC6, 0, 0, 0, 0, 1 }, "Mov8TI", "fs:[!0d],!1d" },
153
154 { kX86Mov16MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov16MR", "[!0r+!1d],!2r" },
155 { kX86Mov16AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x66, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov16AR", "[!0r+!1r<<!2d+!3d],!4r" },
156 { kX86Mov16TR, kThreadReg, IS_STORE | IS_BINARY_OP | REG_USE1, { THREAD_PREFIX, 0x66, 0x89, 0, 0, 0, 0, 0 }, "Mov16TR", "fs:[!0d],!1r" },
157 { kX86Mov16RR, kRegReg, IS_BINARY_OP | REG_DEF0_USE1, { 0x66, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov16RR", "!0r,!1r" },
158 { kX86Mov16RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { 0x66, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov16RM", "!0r,[!1r+!2d]" },
159 { kX86Mov16RA, kRegArray, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0x66, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov16RA", "!0r,[!1r+!2r<<!3d+!4d]" },
160 { kX86Mov16RT, kRegThread, IS_LOAD | IS_BINARY_OP | REG_DEF0, { THREAD_PREFIX, 0x66, 0x8B, 0, 0, 0, 0, 0 }, "Mov16RT", "!0r,fs:[!1d]" },
161 { kX86Mov16RI, kMovRegImm, IS_BINARY_OP | REG_DEF0, { 0x66, 0, 0xB8, 0, 0, 0, 0, 2 }, "Mov16RI", "!0r,!1d" },
162 { kX86Mov16MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { 0x66, 0, 0xC7, 0, 0, 0, 0, 2 }, "Mov16MI", "[!0r+!1d],!2d" },
163 { kX86Mov16AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { 0x66, 0, 0xC7, 0, 0, 0, 0, 2 }, "Mov16AI", "[!0r+!1r<<!2d+!3d],!4d" },
164 { kX86Mov16TI, kThreadImm, IS_STORE | IS_BINARY_OP, { THREAD_PREFIX, 0x66, 0xC7, 0, 0, 0, 0, 2 }, "Mov16TI", "fs:[!0d],!1d" },
165
166 { kX86Mov32MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov32MR", "[!0r+!1d],!2r" },
167 { kX86Mov32AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov32AR", "[!0r+!1r<<!2d+!3d],!4r" },
168 { kX86Mov32TR, kThreadReg, IS_STORE | IS_BINARY_OP | REG_USE1, { THREAD_PREFIX, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov32TR", "fs:[!0d],!1r" },
169 { kX86Mov32RR, kRegReg, IS_BINARY_OP | REG_DEF0_USE1, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov32RR", "!0r,!1r" },
170 { kX86Mov32RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov32RM", "!0r,[!1r+!2d]" },
171 { kX86Mov32RA, kRegArray, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov32RA", "!0r,[!1r+!2r<<!3d+!4d]" },
172 { kX86Mov32RT, kRegThread, IS_LOAD | IS_BINARY_OP | REG_DEF0, { THREAD_PREFIX, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov32RT", "!0r,fs:[!1d]" },
173 { kX86Mov32RI, kMovRegImm, IS_BINARY_OP | REG_DEF0, { 0, 0, 0xB8, 0, 0, 0, 0, 4 }, "Mov32RI", "!0r,!1d" },
174 { kX86Mov32MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { 0, 0, 0xC7, 0, 0, 0, 0, 4 }, "Mov32MI", "[!0r+!1d],!2d" },
175 { kX86Mov32AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { 0, 0, 0xC7, 0, 0, 0, 0, 4 }, "Mov32AI", "[!0r+!1r<<!2d+!3d],!4d" },
176 { kX86Mov32TI, kThreadImm, IS_STORE | IS_BINARY_OP, { THREAD_PREFIX, 0, 0xC7, 0, 0, 0, 0, 4 }, "Mov32TI", "fs:[!0d],!1d" },
177
178 { kX86Lea32RA, kRegArray, IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8D, 0, 0, 0, 0, 0 }, "Lea32RA", "!0r,[!1r+!2r<<!3d+!4d]" },
179
180#define SHIFT_ENCODING_MAP(opname, modrm_opcode) \
181{ 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" }, \
182{ 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" }, \
183{ 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" }, \
184{ 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" }, \
185{ 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" }, \
186{ 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" }, \
187 \
188{ 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" }, \
189{ 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" }, \
190{ 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" }, \
191{ 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" }, \
192{ 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" }, \
193{ 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" }, \
194 \
195{ 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" }, \
196{ 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" }, \
197{ 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" }, \
198{ 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" }, \
199{ 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" }, \
200{ 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" }
201
202 SHIFT_ENCODING_MAP(Rol, 0x0),
203 SHIFT_ENCODING_MAP(Ror, 0x1),
204 SHIFT_ENCODING_MAP(Rcl, 0x2),
205 SHIFT_ENCODING_MAP(Rcr, 0x3),
206 SHIFT_ENCODING_MAP(Sal, 0x4),
207 SHIFT_ENCODING_MAP(Shr, 0x5),
208 SHIFT_ENCODING_MAP(Sar, 0x7),
209#undef SHIFT_ENCODING_MAP
210
211 { kX86Cmc, kNullary, NO_OPERAND, { 0, 0, 0xF5, 0, 0, 0, 0, 0}, "Cmc", "" },
212
213 { kX86Test8RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF6, 0, 0, 0, 0, 1}, "Test8RI", "!0r,!1d" },
214 { kX86Test8MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF6, 0, 0, 0, 0, 1}, "Test8MI", "[!0r+!1d],!2d" },
215 { 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" },
216 { kX86Test16RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0x66, 0, 0xF7, 0, 0, 0, 0, 2}, "Test16RI", "!0r,!1d" },
217 { kX86Test16MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0x66, 0, 0xF7, 0, 0, 0, 0, 2}, "Test16MI", "[!0r+!1d],!2d" },
218 { 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" },
219 { kX86Test32RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF7, 0, 0, 0, 0, 4}, "Test32RI", "!0r,!1d" },
220 { kX86Test32MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF7, 0, 0, 0, 0, 4}, "Test32MI", "[!0r+!1d],!2d" },
221 { 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" },
222 { kX86Test32RR, kRegReg, IS_BINARY_OP | REG_USE01 | SETS_CCODES, { 0, 0, 0x85, 0, 0, 0, 0, 0}, "Test32RR", "!0r,!1r" },
223
224#define UNARY_ENCODING_MAP(opname, modrm, is_store, sets_ccodes, \
225 reg, reg_kind, reg_flags, \
226 mem, mem_kind, mem_flags, \
227 arr, arr_kind, arr_flags, imm, \
228 b_flags, hw_flags, w_flags, \
229 b_format, hw_format, w_format) \
230{ 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" }, \
231{ 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]" }, \
232{ 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]" }, \
233{ 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" }, \
234{ 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]" }, \
235{ 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]" }, \
236{ 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" }, \
237{ 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]" }, \
238{ 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]" }
239
240 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, "", "", ""),
241 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, "", "", ""),
242
243 UNARY_ENCODING_MAP(Mul, 0x4, 0, SETS_CCODES, DaR, kRegRegReg, IS_UNARY_OP | REG_USE0, DaM, kRegRegMem, IS_BINARY_OP | REG_USE0, DaA, kRegRegArray, IS_QUAD_OP | REG_USE01, 0, REG_DEFA_USEA, REG_DEFAD_USEA, REG_DEFAD_USEA, "ax,al,", "dx:ax,ax,", "edx:eax,eax,"),
244 UNARY_ENCODING_MAP(Imul, 0x5, 0, SETS_CCODES, DaR, kRegRegReg, IS_UNARY_OP | REG_USE0, DaM, kRegRegMem, IS_BINARY_OP | REG_USE0, DaA, kRegRegArray, IS_QUAD_OP | REG_USE01, 0, REG_DEFA_USEA, REG_DEFAD_USEA, REG_DEFAD_USEA, "ax,al,", "dx:ax,ax,", "edx:eax,eax,"),
245 UNARY_ENCODING_MAP(Divmod, 0x6, 0, SETS_CCODES, DaR, kRegRegReg, IS_UNARY_OP | REG_USE0, DaM, kRegRegMem, IS_BINARY_OP | REG_USE0, DaA, kRegRegArray, 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,"),
246 UNARY_ENCODING_MAP(Idivmod, 0x7, 0, SETS_CCODES, DaR, kRegRegReg, IS_UNARY_OP | REG_USE0, DaM, kRegRegMem, IS_BINARY_OP | REG_USE0, DaA, kRegRegArray, 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,"),
247#undef UNARY_ENCODING_MAP
248
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100249 { kX86Bswap32R, kRegOpcode, IS_UNARY_OP | REG_DEF0_USE0, { 0, 0, 0x0F, 0xC8, 0, 0, 0, 0 }, "Bswap32R", "!0r" },
250
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251#define EXT_0F_ENCODING_MAP(opname, prefix, opcode, reg_def) \
252{ kX86 ## opname ## RR, kRegReg, IS_BINARY_OP | reg_def | REG_USE01, { prefix, 0, 0x0F, opcode, 0, 0, 0, 0 }, #opname "RR", "!0r,!1r" }, \
253{ kX86 ## opname ## RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | reg_def | REG_USE01, { prefix, 0, 0x0F, opcode, 0, 0, 0, 0 }, #opname "RM", "!0r,[!1r+!2d]" }, \
254{ kX86 ## opname ## RA, kRegArray, IS_LOAD | IS_QUIN_OP | reg_def | REG_USE012, { prefix, 0, 0x0F, opcode, 0, 0, 0, 0 }, #opname "RA", "!0r,[!1r+!2r<<!3d+!4d]" }
255
256 EXT_0F_ENCODING_MAP(Movsd, 0xF2, 0x10, REG_DEF0),
257 { kX86MovsdMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0xF2, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovsdMR", "[!0r+!1d],!2r" },
258 { kX86MovsdAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0xF2, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovsdAR", "[!0r+!1r<<!2d+!3d],!4r" },
259
260 EXT_0F_ENCODING_MAP(Movss, 0xF3, 0x10, REG_DEF0),
261 { kX86MovssMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0xF3, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovssMR", "[!0r+!1d],!2r" },
262 { kX86MovssAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0xF3, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovssAR", "[!0r+!1r<<!2d+!3d],!4r" },
263
264 EXT_0F_ENCODING_MAP(Cvtsi2sd, 0xF2, 0x2A, REG_DEF0),
265 EXT_0F_ENCODING_MAP(Cvtsi2ss, 0xF3, 0x2A, REG_DEF0),
266 EXT_0F_ENCODING_MAP(Cvttsd2si, 0xF2, 0x2C, REG_DEF0),
267 EXT_0F_ENCODING_MAP(Cvttss2si, 0xF3, 0x2C, REG_DEF0),
268 EXT_0F_ENCODING_MAP(Cvtsd2si, 0xF2, 0x2D, REG_DEF0),
269 EXT_0F_ENCODING_MAP(Cvtss2si, 0xF3, 0x2D, REG_DEF0),
270 EXT_0F_ENCODING_MAP(Ucomisd, 0x66, 0x2E, SETS_CCODES),
271 EXT_0F_ENCODING_MAP(Ucomiss, 0x00, 0x2E, SETS_CCODES),
272 EXT_0F_ENCODING_MAP(Comisd, 0x66, 0x2F, SETS_CCODES),
273 EXT_0F_ENCODING_MAP(Comiss, 0x00, 0x2F, SETS_CCODES),
274 EXT_0F_ENCODING_MAP(Orps, 0x00, 0x56, REG_DEF0),
275 EXT_0F_ENCODING_MAP(Xorps, 0x00, 0x57, REG_DEF0),
276 EXT_0F_ENCODING_MAP(Addsd, 0xF2, 0x58, REG_DEF0),
277 EXT_0F_ENCODING_MAP(Addss, 0xF3, 0x58, REG_DEF0),
278 EXT_0F_ENCODING_MAP(Mulsd, 0xF2, 0x59, REG_DEF0),
279 EXT_0F_ENCODING_MAP(Mulss, 0xF3, 0x59, REG_DEF0),
280 EXT_0F_ENCODING_MAP(Cvtsd2ss, 0xF2, 0x5A, REG_DEF0),
281 EXT_0F_ENCODING_MAP(Cvtss2sd, 0xF3, 0x5A, REG_DEF0),
282 EXT_0F_ENCODING_MAP(Subsd, 0xF2, 0x5C, REG_DEF0),
283 EXT_0F_ENCODING_MAP(Subss, 0xF3, 0x5C, REG_DEF0),
284 EXT_0F_ENCODING_MAP(Divsd, 0xF2, 0x5E, REG_DEF0),
285 EXT_0F_ENCODING_MAP(Divss, 0xF3, 0x5E, REG_DEF0),
286
287 { kX86PsrlqRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x73, 0, 2, 0, 1 }, "PsrlqRI", "!0r,!1d" },
288 { kX86PsllqRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x73, 0, 6, 0, 1 }, "PsllqRI", "!0r,!1d" },
289
290 EXT_0F_ENCODING_MAP(Movdxr, 0x66, 0x6E, REG_DEF0),
291 { kX86MovdrxRR, kRegRegStore, IS_BINARY_OP | REG_DEF0 | REG_USE01, { 0x66, 0, 0x0F, 0x7E, 0, 0, 0, 0 }, "MovdrxRR", "!0r,!1r" },
292 { kX86MovdrxMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, 0, 0x0F, 0x7E, 0, 0, 0, 0 }, "MovdrxMR", "[!0r+!1d],!2r" },
293 { kX86MovdrxAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x66, 0, 0x0F, 0x7E, 0, 0, 0, 0 }, "MovdrxAR", "[!0r+!1r<<!2d+!3d],!4r" },
294
295 { kX86Set8R, kRegCond, IS_BINARY_OP | REG_DEF0 | USES_CCODES, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0 }, "Set8R", "!1c !0r" },
296 { kX86Set8M, kMemCond, IS_STORE | IS_TERTIARY_OP | REG_USE0 | USES_CCODES, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0 }, "Set8M", "!2c [!0r+!1d]" },
297 { 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]" },
298
299 // TODO: load/store?
300 // Encode the modrm opcode as an extra opcode byte to avoid computation during assembly.
301 { kX86Mfence, kReg, NO_OPERAND, { 0, 0, 0x0F, 0xAE, 0, 6, 0, 0 }, "Mfence", "" },
302
303 EXT_0F_ENCODING_MAP(Imul16, 0x66, 0xAF, REG_DEF0 | SETS_CCODES),
304 EXT_0F_ENCODING_MAP(Imul32, 0x00, 0xAF, REG_DEF0 | SETS_CCODES),
305
306 { 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" },
307 { 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" },
308 { 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" },
309 { kX86LockCmpxchgRR, kRegRegStore, IS_BINARY_OP | REG_DEF0 | REG_USE01 | REG_DEFA_USEA | SETS_CCODES, { 0xF0, 0, 0x0F, 0xB1, 0, 0, 0, 0 }, "Lock Cmpxchg", "!0r,!1r" },
310 { 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" },
311 { 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" },
312
313 EXT_0F_ENCODING_MAP(Movzx8, 0x00, 0xB6, REG_DEF0),
314 EXT_0F_ENCODING_MAP(Movzx16, 0x00, 0xB7, REG_DEF0),
315 EXT_0F_ENCODING_MAP(Movsx8, 0x00, 0xBE, REG_DEF0),
316 EXT_0F_ENCODING_MAP(Movsx16, 0x00, 0xBF, REG_DEF0),
317#undef EXT_0F_ENCODING_MAP
318
319 { kX86Jcc8, kJcc, IS_BINARY_OP | IS_BRANCH | NEEDS_FIXUP | USES_CCODES, { 0, 0, 0x70, 0, 0, 0, 0, 0 }, "Jcc8", "!1c !0t" },
320 { kX86Jcc32, kJcc, IS_BINARY_OP | IS_BRANCH | NEEDS_FIXUP | USES_CCODES, { 0, 0, 0x0F, 0x80, 0, 0, 0, 0 }, "Jcc32", "!1c !0t" },
321 { kX86Jmp8, kJmp, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0xEB, 0, 0, 0, 0, 0 }, "Jmp8", "!0t" },
322 { kX86Jmp32, kJmp, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0xE9, 0, 0, 0, 0, 0 }, "Jmp32", "!0t" },
323 { kX86JmpR, kJmp, IS_UNARY_OP | IS_BRANCH | REG_USE0, { 0, 0, 0xFF, 0, 0, 4, 0, 0 }, "JmpR", "!0r" },
324 { kX86CallR, kCall, IS_UNARY_OP | IS_BRANCH | REG_USE0, { 0, 0, 0xE8, 0, 0, 0, 0, 0 }, "CallR", "!0r" },
325 { kX86CallM, kCall, IS_BINARY_OP | IS_BRANCH | IS_LOAD | REG_USE0, { 0, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallM", "[!0r+!1d]" },
326 { kX86CallA, kCall, IS_QUAD_OP | IS_BRANCH | IS_LOAD | REG_USE01, { 0, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallA", "[!0r+!1r<<!2d+!3d]" },
327 { kX86CallT, kCall, IS_UNARY_OP | IS_BRANCH | IS_LOAD, { THREAD_PREFIX, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallT", "fs:[!0d]" },
Brian Carlstromb1eba212013-07-17 18:07:19 -0700328 { kX86Ret, kNullary, NO_OPERAND | IS_BRANCH, { 0, 0, 0xC3, 0, 0, 0, 0, 0 }, "Ret", "" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700329
330 { kX86StartOfMethod, kMacro, IS_UNARY_OP | SETS_CCODES, { 0, 0, 0, 0, 0, 0, 0, 0 }, "StartOfMethod", "!0r" },
331 { kX86PcRelLoadRA, kPcRel, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "PcRelLoadRA", "!0r,[!1r+!2r<<!3d+!4p]" },
332 { kX86PcRelAdr, kPcRel, IS_LOAD | IS_BINARY_OP | REG_DEF0, { 0, 0, 0xB8, 0, 0, 0, 0, 4 }, "PcRelAdr", "!0r,!1d" },
333};
334
335static size_t ComputeSize(const X86EncodingMap* entry, int base, int displacement, bool has_sib) {
336 size_t size = 0;
337 if (entry->skeleton.prefix1 > 0) {
338 ++size;
339 if (entry->skeleton.prefix2 > 0) {
340 ++size;
341 }
342 }
343 ++size; // opcode
344 if (entry->skeleton.opcode == 0x0F) {
345 ++size;
346 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
347 ++size;
348 }
349 }
350 ++size; // modrm
351 if (has_sib || base == rX86_SP) {
352 // SP requires a SIB byte.
353 ++size;
354 }
355 if (displacement != 0 || base == rBP) {
356 // BP requires an explicit displacement, even when it's 0.
357 if (entry->opcode != kX86Lea32RA) {
358 DCHECK_NE(entry->flags & (IS_LOAD | IS_STORE), 0ULL) << entry->name;
359 }
360 size += IS_SIMM8(displacement) ? 1 : 4;
361 }
362 size += entry->skeleton.immediate_bytes;
363 return size;
364}
365
366int X86Mir2Lir::GetInsnSize(LIR* lir) {
buzbee409fe942013-10-11 10:49:56 -0700367 DCHECK(!IsPseudoLirOp(lir->opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368 const X86EncodingMap* entry = &X86Mir2Lir::EncodingMap[lir->opcode];
369 switch (entry->kind) {
370 case kData:
371 return 4; // 4 bytes of data
372 case kNop:
373 return lir->operands[0]; // length of nop is sole operand
374 case kNullary:
375 return 1; // 1 byte of opcode
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100376 case kRegOpcode: // lir operands - 0: reg
377 return ComputeSize(entry, 0, 0, false) - 1; // substract 1 for modrm
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378 case kReg: // lir operands - 0: reg
379 return ComputeSize(entry, 0, 0, false);
380 case kMem: // lir operands - 0: base, 1: disp
381 return ComputeSize(entry, lir->operands[0], lir->operands[1], false);
382 case kArray: // lir operands - 0: base, 1: index, 2: scale, 3: disp
383 return ComputeSize(entry, lir->operands[0], lir->operands[3], true);
384 case kMemReg: // lir operands - 0: base, 1: disp, 2: reg
385 return ComputeSize(entry, lir->operands[0], lir->operands[1], false);
386 case kArrayReg: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
387 return ComputeSize(entry, lir->operands[0], lir->operands[3], true);
388 case kThreadReg: // lir operands - 0: disp, 1: reg
389 return ComputeSize(entry, 0, lir->operands[0], false);
390 case kRegReg:
391 return ComputeSize(entry, 0, 0, false);
392 case kRegRegStore:
393 return ComputeSize(entry, 0, 0, false);
394 case kRegMem: // lir operands - 0: reg, 1: base, 2: disp
395 return ComputeSize(entry, lir->operands[1], lir->operands[2], false);
396 case kRegArray: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
397 return ComputeSize(entry, lir->operands[1], lir->operands[4], true);
398 case kRegThread: // lir operands - 0: reg, 1: disp
399 return ComputeSize(entry, 0, 0x12345678, false); // displacement size is always 32bit
400 case kRegImm: { // lir operands - 0: reg, 1: immediate
401 size_t size = ComputeSize(entry, 0, 0, false);
402 if (entry->skeleton.ax_opcode == 0) {
403 return size;
404 } else {
405 // AX opcodes don't require the modrm byte.
406 int reg = lir->operands[0];
407 return size - (reg == rAX ? 1 : 0);
408 }
409 }
410 case kMemImm: // lir operands - 0: base, 1: disp, 2: immediate
411 return ComputeSize(entry, lir->operands[0], lir->operands[1], false);
412 case kArrayImm: // lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
413 return ComputeSize(entry, lir->operands[0], lir->operands[3], true);
414 case kThreadImm: // lir operands - 0: disp, 1: imm
415 return ComputeSize(entry, 0, 0x12345678, false); // displacement size is always 32bit
416 case kRegRegImm: // lir operands - 0: reg, 1: reg, 2: imm
417 return ComputeSize(entry, 0, 0, false);
418 case kRegMemImm: // lir operands - 0: reg, 1: base, 2: disp, 3: imm
419 return ComputeSize(entry, lir->operands[1], lir->operands[2], false);
420 case kRegArrayImm: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp, 5: imm
421 return ComputeSize(entry, lir->operands[1], lir->operands[4], true);
422 case kMovRegImm: // lir operands - 0: reg, 1: immediate
423 return 1 + entry->skeleton.immediate_bytes;
424 case kShiftRegImm: // lir operands - 0: reg, 1: immediate
425 // Shift by immediate one has a shorter opcode.
426 return ComputeSize(entry, 0, 0, false) - (lir->operands[1] == 1 ? 1 : 0);
427 case kShiftMemImm: // lir operands - 0: base, 1: disp, 2: immediate
428 // Shift by immediate one has a shorter opcode.
429 return ComputeSize(entry, lir->operands[0], lir->operands[1], false) -
430 (lir->operands[2] == 1 ? 1 : 0);
431 case kShiftArrayImm: // lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
432 // Shift by immediate one has a shorter opcode.
433 return ComputeSize(entry, lir->operands[0], lir->operands[3], true) -
434 (lir->operands[4] == 1 ? 1 : 0);
435 case kShiftRegCl:
436 return ComputeSize(entry, 0, 0, false);
437 case kShiftMemCl: // lir operands - 0: base, 1: disp, 2: cl
438 return ComputeSize(entry, lir->operands[0], lir->operands[1], false);
439 case kShiftArrayCl: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
440 return ComputeSize(entry, lir->operands[0], lir->operands[3], true);
441 case kRegCond: // lir operands - 0: reg, 1: cond
442 return ComputeSize(entry, 0, 0, false);
443 case kMemCond: // lir operands - 0: base, 1: disp, 2: cond
444 return ComputeSize(entry, lir->operands[0], lir->operands[1], false);
445 case kArrayCond: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: cond
446 return ComputeSize(entry, lir->operands[0], lir->operands[3], true);
447 case kJcc:
448 if (lir->opcode == kX86Jcc8) {
449 return 2; // opcode + rel8
450 } else {
451 DCHECK(lir->opcode == kX86Jcc32);
452 return 6; // 2 byte opcode + rel32
453 }
454 case kJmp:
455 if (lir->opcode == kX86Jmp8) {
456 return 2; // opcode + rel8
457 } else if (lir->opcode == kX86Jmp32) {
458 return 5; // opcode + rel32
459 } else {
460 DCHECK(lir->opcode == kX86JmpR);
461 return 2; // opcode + modrm
462 }
463 case kCall:
464 switch (lir->opcode) {
465 case kX86CallR: return 2; // opcode modrm
466 case kX86CallM: // lir operands - 0: base, 1: disp
467 return ComputeSize(entry, lir->operands[0], lir->operands[1], false);
468 case kX86CallA: // lir operands - 0: base, 1: index, 2: scale, 3: disp
469 return ComputeSize(entry, lir->operands[0], lir->operands[3], true);
470 case kX86CallT: // lir operands - 0: disp
471 return ComputeSize(entry, 0, 0x12345678, false); // displacement size is always 32bit
472 default:
473 break;
474 }
475 break;
476 case kPcRel:
477 if (entry->opcode == kX86PcRelLoadRA) {
478 // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
479 return ComputeSize(entry, lir->operands[1], 0x12345678, true);
480 } else {
481 DCHECK(entry->opcode == kX86PcRelAdr);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700482 return 5; // opcode with reg + 4 byte immediate
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483 }
484 case kMacro:
485 DCHECK_EQ(lir->opcode, static_cast<int>(kX86StartOfMethod));
486 return 5 /* call opcode + 4 byte displacement */ + 1 /* pop reg */ +
487 ComputeSize(&X86Mir2Lir::EncodingMap[kX86Sub32RI], 0, 0, false) -
488 (lir->operands[0] == rAX ? 1 : 0); // shorter ax encoding
489 default:
490 break;
491 }
492 UNIMPLEMENTED(FATAL) << "Unimplemented size encoding for: " << entry->name;
493 return 0;
494}
495
496static uint8_t ModrmForDisp(int base, int disp) {
497 // BP requires an explicit disp, so do not omit it in the 0 case
498 if (disp == 0 && base != rBP) {
499 return 0;
500 } else if (IS_SIMM8(disp)) {
501 return 1;
502 } else {
503 return 2;
504 }
505}
506
507void X86Mir2Lir::EmitDisp(int base, int disp) {
508 // BP requires an explicit disp, so do not omit it in the 0 case
509 if (disp == 0 && base != rBP) {
510 return;
511 } else if (IS_SIMM8(disp)) {
512 code_buffer_.push_back(disp & 0xFF);
513 } else {
514 code_buffer_.push_back(disp & 0xFF);
515 code_buffer_.push_back((disp >> 8) & 0xFF);
516 code_buffer_.push_back((disp >> 16) & 0xFF);
517 code_buffer_.push_back((disp >> 24) & 0xFF);
518 }
519}
520
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100521void X86Mir2Lir::EmitOpRegOpcode(const X86EncodingMap* entry, uint8_t reg) {
522 if (entry->skeleton.prefix1 != 0) {
523 code_buffer_.push_back(entry->skeleton.prefix1);
524 if (entry->skeleton.prefix2 != 0) {
525 code_buffer_.push_back(entry->skeleton.prefix2);
526 }
527 } else {
528 DCHECK_EQ(0, entry->skeleton.prefix2);
529 }
530 code_buffer_.push_back(entry->skeleton.opcode);
531 if (entry->skeleton.opcode == 0x0F) {
532 code_buffer_.push_back(entry->skeleton.extra_opcode1);
533 // There's no 3-byte instruction with +rd
534 DCHECK_NE(0x38, entry->skeleton.extra_opcode1);
535 DCHECK_NE(0x3A, entry->skeleton.extra_opcode1);
536 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
537 } else {
538 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
539 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
540 }
541 DCHECK(!X86_FPREG(reg));
542 DCHECK_LT(reg, 8);
543 code_buffer_.back() += reg;
544 DCHECK_EQ(0, entry->skeleton.ax_opcode);
545 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
546}
547
Brian Carlstrom7940e442013-07-12 13:46:57 -0700548void X86Mir2Lir::EmitOpReg(const X86EncodingMap* entry, uint8_t reg) {
549 if (entry->skeleton.prefix1 != 0) {
550 code_buffer_.push_back(entry->skeleton.prefix1);
551 if (entry->skeleton.prefix2 != 0) {
552 code_buffer_.push_back(entry->skeleton.prefix2);
553 }
554 } else {
555 DCHECK_EQ(0, entry->skeleton.prefix2);
556 }
557 code_buffer_.push_back(entry->skeleton.opcode);
558 if (entry->skeleton.opcode == 0x0F) {
559 code_buffer_.push_back(entry->skeleton.extra_opcode1);
Vladimir Markoe6ed00b2013-10-24 14:52:37 +0100560 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700561 code_buffer_.push_back(entry->skeleton.extra_opcode2);
562 } else {
563 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
564 }
565 } else {
566 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
567 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
568 }
569 if (X86_FPREG(reg)) {
570 reg = reg & X86_FP_REG_MASK;
571 }
572 if (reg >= 4) {
573 DCHECK(strchr(entry->name, '8') == NULL) << entry->name << " " << static_cast<int>(reg)
574 << " in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
575 }
576 DCHECK_LT(reg, 8);
577 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
578 code_buffer_.push_back(modrm);
579 DCHECK_EQ(0, entry->skeleton.ax_opcode);
580 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
581}
582
583void X86Mir2Lir::EmitOpMem(const X86EncodingMap* entry, uint8_t base, int disp) {
584 if (entry->skeleton.prefix1 != 0) {
585 code_buffer_.push_back(entry->skeleton.prefix1);
586 if (entry->skeleton.prefix2 != 0) {
587 code_buffer_.push_back(entry->skeleton.prefix2);
588 }
589 } else {
590 DCHECK_EQ(0, entry->skeleton.prefix2);
591 }
592 code_buffer_.push_back(entry->skeleton.opcode);
593 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
594 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
595 DCHECK_LT(entry->skeleton.modrm_opcode, 8);
596 DCHECK_LT(base, 8);
597 uint8_t modrm = (ModrmForDisp(base, disp) << 6) | (entry->skeleton.modrm_opcode << 3) | base;
598 code_buffer_.push_back(modrm);
599 EmitDisp(base, disp);
600 DCHECK_EQ(0, entry->skeleton.ax_opcode);
601 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
602}
603
604void X86Mir2Lir::EmitMemReg(const X86EncodingMap* entry,
605 uint8_t base, int disp, uint8_t reg) {
606 if (entry->skeleton.prefix1 != 0) {
607 code_buffer_.push_back(entry->skeleton.prefix1);
608 if (entry->skeleton.prefix2 != 0) {
609 code_buffer_.push_back(entry->skeleton.prefix2);
610 }
611 } else {
612 DCHECK_EQ(0, entry->skeleton.prefix2);
613 }
614 code_buffer_.push_back(entry->skeleton.opcode);
615 if (entry->skeleton.opcode == 0x0F) {
616 code_buffer_.push_back(entry->skeleton.extra_opcode1);
Vladimir Markoe6ed00b2013-10-24 14:52:37 +0100617 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700618 code_buffer_.push_back(entry->skeleton.extra_opcode2);
619 } else {
620 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
621 }
622 } else {
623 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
624 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
625 }
626 if (X86_FPREG(reg)) {
627 reg = reg & X86_FP_REG_MASK;
628 }
629 if (reg >= 4) {
Vladimir Marko17088bb2013-10-28 13:55:34 +0000630 DCHECK(strchr(entry->name, '8') == NULL ||
631 entry->opcode == kX86Movzx8RM || entry->opcode == kX86Movsx8RM)
632 << entry->name << " " << static_cast<int>(reg)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700633 << " in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
634 }
635 DCHECK_LT(reg, 8);
636 DCHECK_LT(base, 8);
637 uint8_t modrm = (ModrmForDisp(base, disp) << 6) | (reg << 3) | base;
638 code_buffer_.push_back(modrm);
639 if (base == rX86_SP) {
640 // Special SIB for SP base
641 code_buffer_.push_back(0 << 6 | (rX86_SP << 3) | rX86_SP);
642 }
643 EmitDisp(base, disp);
644 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
645 DCHECK_EQ(0, entry->skeleton.ax_opcode);
646 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
647}
648
649void X86Mir2Lir::EmitRegMem(const X86EncodingMap* entry,
650 uint8_t reg, uint8_t base, int disp) {
651 // Opcode will flip operands.
652 EmitMemReg(entry, base, disp, reg);
653}
654
655void X86Mir2Lir::EmitRegArray(const X86EncodingMap* entry, uint8_t reg, uint8_t base, uint8_t index,
656 int scale, int disp) {
657 if (entry->skeleton.prefix1 != 0) {
658 code_buffer_.push_back(entry->skeleton.prefix1);
659 if (entry->skeleton.prefix2 != 0) {
660 code_buffer_.push_back(entry->skeleton.prefix2);
661 }
662 } else {
663 DCHECK_EQ(0, entry->skeleton.prefix2);
664 }
665 code_buffer_.push_back(entry->skeleton.opcode);
666 if (entry->skeleton.opcode == 0x0F) {
667 code_buffer_.push_back(entry->skeleton.extra_opcode1);
Vladimir Markoe6ed00b2013-10-24 14:52:37 +0100668 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669 code_buffer_.push_back(entry->skeleton.extra_opcode2);
670 } else {
671 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
672 }
673 } else {
674 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
675 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
676 }
677 if (X86_FPREG(reg)) {
678 reg = reg & X86_FP_REG_MASK;
679 }
680 DCHECK_LT(reg, 8);
681 uint8_t modrm = (ModrmForDisp(base, disp) << 6) | (reg << 3) | rX86_SP;
682 code_buffer_.push_back(modrm);
683 DCHECK_LT(scale, 4);
684 DCHECK_LT(index, 8);
685 DCHECK_LT(base, 8);
686 uint8_t sib = (scale << 6) | (index << 3) | base;
687 code_buffer_.push_back(sib);
688 EmitDisp(base, disp);
689 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
690 DCHECK_EQ(0, entry->skeleton.ax_opcode);
691 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
692}
693
694void X86Mir2Lir::EmitArrayReg(const X86EncodingMap* entry, uint8_t base, uint8_t index, int scale, int disp,
695 uint8_t reg) {
696 // Opcode will flip operands.
697 EmitRegArray(entry, reg, base, index, scale, disp);
698}
699
700void X86Mir2Lir::EmitRegThread(const X86EncodingMap* entry, uint8_t reg, int disp) {
701 DCHECK_NE(entry->skeleton.prefix1, 0);
702 code_buffer_.push_back(entry->skeleton.prefix1);
703 if (entry->skeleton.prefix2 != 0) {
704 code_buffer_.push_back(entry->skeleton.prefix2);
705 }
706 code_buffer_.push_back(entry->skeleton.opcode);
707 if (entry->skeleton.opcode == 0x0F) {
708 code_buffer_.push_back(entry->skeleton.extra_opcode1);
Vladimir Markoe6ed00b2013-10-24 14:52:37 +0100709 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700710 code_buffer_.push_back(entry->skeleton.extra_opcode2);
711 } else {
712 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
713 }
714 } else {
715 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
716 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
717 }
718 if (X86_FPREG(reg)) {
719 reg = reg & X86_FP_REG_MASK;
720 }
721 if (reg >= 4) {
722 DCHECK(strchr(entry->name, '8') == NULL) << entry->name << " " << static_cast<int>(reg)
723 << " in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
724 }
725 DCHECK_LT(reg, 8);
726 uint8_t modrm = (0 << 6) | (reg << 3) | rBP;
727 code_buffer_.push_back(modrm);
728 code_buffer_.push_back(disp & 0xFF);
729 code_buffer_.push_back((disp >> 8) & 0xFF);
730 code_buffer_.push_back((disp >> 16) & 0xFF);
731 code_buffer_.push_back((disp >> 24) & 0xFF);
732 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
733 DCHECK_EQ(0, entry->skeleton.ax_opcode);
734 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
735}
736
737void X86Mir2Lir::EmitRegReg(const X86EncodingMap* entry, uint8_t reg1, uint8_t reg2) {
738 if (entry->skeleton.prefix1 != 0) {
739 code_buffer_.push_back(entry->skeleton.prefix1);
740 if (entry->skeleton.prefix2 != 0) {
741 code_buffer_.push_back(entry->skeleton.prefix2);
742 }
743 } else {
744 DCHECK_EQ(0, entry->skeleton.prefix2);
745 }
746 code_buffer_.push_back(entry->skeleton.opcode);
747 if (entry->skeleton.opcode == 0x0F) {
748 code_buffer_.push_back(entry->skeleton.extra_opcode1);
Vladimir Markoe6ed00b2013-10-24 14:52:37 +0100749 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700750 code_buffer_.push_back(entry->skeleton.extra_opcode2);
751 } else {
752 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
753 }
754 } else {
755 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
756 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
757 }
758 if (X86_FPREG(reg1)) {
759 reg1 = reg1 & X86_FP_REG_MASK;
760 }
761 if (X86_FPREG(reg2)) {
762 reg2 = reg2 & X86_FP_REG_MASK;
763 }
764 DCHECK_LT(reg1, 8);
765 DCHECK_LT(reg2, 8);
766 uint8_t modrm = (3 << 6) | (reg1 << 3) | reg2;
767 code_buffer_.push_back(modrm);
768 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
769 DCHECK_EQ(0, entry->skeleton.ax_opcode);
770 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
771}
772
773void X86Mir2Lir::EmitRegRegImm(const X86EncodingMap* entry,
774 uint8_t reg1, uint8_t reg2, int32_t imm) {
775 if (entry->skeleton.prefix1 != 0) {
776 code_buffer_.push_back(entry->skeleton.prefix1);
777 if (entry->skeleton.prefix2 != 0) {
778 code_buffer_.push_back(entry->skeleton.prefix2);
779 }
780 } else {
781 DCHECK_EQ(0, entry->skeleton.prefix2);
782 }
783 code_buffer_.push_back(entry->skeleton.opcode);
784 if (entry->skeleton.opcode == 0x0F) {
785 code_buffer_.push_back(entry->skeleton.extra_opcode1);
Vladimir Markoe6ed00b2013-10-24 14:52:37 +0100786 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700787 code_buffer_.push_back(entry->skeleton.extra_opcode2);
788 } else {
789 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
790 }
791 } else {
792 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
793 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
794 }
795 if (X86_FPREG(reg1)) {
796 reg1 = reg1 & X86_FP_REG_MASK;
797 }
798 if (X86_FPREG(reg2)) {
799 reg2 = reg2 & X86_FP_REG_MASK;
800 }
801 DCHECK_LT(reg1, 8);
802 DCHECK_LT(reg2, 8);
803 uint8_t modrm = (3 << 6) | (reg1 << 3) | reg2;
804 code_buffer_.push_back(modrm);
805 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
806 DCHECK_EQ(0, entry->skeleton.ax_opcode);
807 switch (entry->skeleton.immediate_bytes) {
808 case 1:
809 DCHECK(IS_SIMM8(imm));
810 code_buffer_.push_back(imm & 0xFF);
811 break;
812 case 2:
813 DCHECK(IS_SIMM16(imm));
814 code_buffer_.push_back(imm & 0xFF);
815 code_buffer_.push_back((imm >> 8) & 0xFF);
816 break;
817 case 4:
818 code_buffer_.push_back(imm & 0xFF);
819 code_buffer_.push_back((imm >> 8) & 0xFF);
820 code_buffer_.push_back((imm >> 16) & 0xFF);
821 code_buffer_.push_back((imm >> 24) & 0xFF);
822 break;
823 default:
824 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
825 << ") for instruction: " << entry->name;
826 break;
827 }
828}
829
830void X86Mir2Lir::EmitRegImm(const X86EncodingMap* entry, uint8_t reg, int imm) {
831 if (entry->skeleton.prefix1 != 0) {
832 code_buffer_.push_back(entry->skeleton.prefix1);
833 if (entry->skeleton.prefix2 != 0) {
834 code_buffer_.push_back(entry->skeleton.prefix2);
835 }
836 } else {
837 DCHECK_EQ(0, entry->skeleton.prefix2);
838 }
839 if (reg == rAX && entry->skeleton.ax_opcode != 0) {
840 code_buffer_.push_back(entry->skeleton.ax_opcode);
841 } else {
842 code_buffer_.push_back(entry->skeleton.opcode);
843 if (entry->skeleton.opcode == 0x0F) {
844 code_buffer_.push_back(entry->skeleton.extra_opcode1);
Vladimir Markoe6ed00b2013-10-24 14:52:37 +0100845 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700846 code_buffer_.push_back(entry->skeleton.extra_opcode2);
847 } else {
848 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
849 }
850 } else {
851 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
852 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
853 }
854 if (X86_FPREG(reg)) {
855 reg = reg & X86_FP_REG_MASK;
856 }
857 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
858 code_buffer_.push_back(modrm);
859 }
860 switch (entry->skeleton.immediate_bytes) {
861 case 1:
862 DCHECK(IS_SIMM8(imm));
863 code_buffer_.push_back(imm & 0xFF);
864 break;
865 case 2:
866 DCHECK(IS_SIMM16(imm));
867 code_buffer_.push_back(imm & 0xFF);
868 code_buffer_.push_back((imm >> 8) & 0xFF);
869 break;
870 case 4:
871 code_buffer_.push_back(imm & 0xFF);
872 code_buffer_.push_back((imm >> 8) & 0xFF);
873 code_buffer_.push_back((imm >> 16) & 0xFF);
874 code_buffer_.push_back((imm >> 24) & 0xFF);
875 break;
876 default:
877 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
878 << ") for instruction: " << entry->name;
879 break;
880 }
881}
882
883void X86Mir2Lir::EmitThreadImm(const X86EncodingMap* entry, int disp, int imm) {
884 if (entry->skeleton.prefix1 != 0) {
885 code_buffer_.push_back(entry->skeleton.prefix1);
886 if (entry->skeleton.prefix2 != 0) {
887 code_buffer_.push_back(entry->skeleton.prefix2);
888 }
889 } else {
890 DCHECK_EQ(0, entry->skeleton.prefix2);
891 }
892 code_buffer_.push_back(entry->skeleton.opcode);
893 if (entry->skeleton.opcode == 0x0F) {
894 code_buffer_.push_back(entry->skeleton.extra_opcode1);
Vladimir Markoe6ed00b2013-10-24 14:52:37 +0100895 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700896 code_buffer_.push_back(entry->skeleton.extra_opcode2);
897 } else {
898 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
899 }
900 } else {
901 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
902 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
903 }
904 uint8_t modrm = (0 << 6) | (entry->skeleton.modrm_opcode << 3) | rBP;
905 code_buffer_.push_back(modrm);
906 code_buffer_.push_back(disp & 0xFF);
907 code_buffer_.push_back((disp >> 8) & 0xFF);
908 code_buffer_.push_back((disp >> 16) & 0xFF);
909 code_buffer_.push_back((disp >> 24) & 0xFF);
910 switch (entry->skeleton.immediate_bytes) {
911 case 1:
912 DCHECK(IS_SIMM8(imm));
913 code_buffer_.push_back(imm & 0xFF);
914 break;
915 case 2:
916 DCHECK(IS_SIMM16(imm));
917 code_buffer_.push_back(imm & 0xFF);
918 code_buffer_.push_back((imm >> 8) & 0xFF);
919 break;
920 case 4:
921 code_buffer_.push_back(imm & 0xFF);
922 code_buffer_.push_back((imm >> 8) & 0xFF);
923 code_buffer_.push_back((imm >> 16) & 0xFF);
924 code_buffer_.push_back((imm >> 24) & 0xFF);
925 break;
926 default:
927 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
928 << ") for instruction: " << entry->name;
929 break;
930 }
931 DCHECK_EQ(entry->skeleton.ax_opcode, 0);
932}
933
934void X86Mir2Lir::EmitMovRegImm(const X86EncodingMap* entry, uint8_t reg, int imm) {
935 DCHECK_LT(reg, 8);
936 code_buffer_.push_back(0xB8 + reg);
937 code_buffer_.push_back(imm & 0xFF);
938 code_buffer_.push_back((imm >> 8) & 0xFF);
939 code_buffer_.push_back((imm >> 16) & 0xFF);
940 code_buffer_.push_back((imm >> 24) & 0xFF);
941}
942
943void X86Mir2Lir::EmitShiftRegImm(const X86EncodingMap* entry, uint8_t reg, int imm) {
944 if (entry->skeleton.prefix1 != 0) {
945 code_buffer_.push_back(entry->skeleton.prefix1);
946 if (entry->skeleton.prefix2 != 0) {
947 code_buffer_.push_back(entry->skeleton.prefix2);
948 }
949 } else {
950 DCHECK_EQ(0, entry->skeleton.prefix2);
951 }
952 if (imm != 1) {
953 code_buffer_.push_back(entry->skeleton.opcode);
954 } else {
955 // Shorter encoding for 1 bit shift
956 code_buffer_.push_back(entry->skeleton.ax_opcode);
957 }
958 if (entry->skeleton.opcode == 0x0F) {
959 code_buffer_.push_back(entry->skeleton.extra_opcode1);
Vladimir Markoe6ed00b2013-10-24 14:52:37 +0100960 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700961 code_buffer_.push_back(entry->skeleton.extra_opcode2);
962 } else {
963 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
964 }
965 } else {
966 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
967 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
968 }
969 if (reg >= 4) {
970 DCHECK(strchr(entry->name, '8') == NULL) << entry->name << " " << static_cast<int>(reg)
971 << " in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
972 }
973 DCHECK_LT(reg, 8);
974 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
975 code_buffer_.push_back(modrm);
976 if (imm != 1) {
977 DCHECK_EQ(entry->skeleton.immediate_bytes, 1);
978 DCHECK(IS_SIMM8(imm));
979 code_buffer_.push_back(imm & 0xFF);
980 }
981}
982
983void X86Mir2Lir::EmitShiftRegCl(const X86EncodingMap* entry, uint8_t reg, uint8_t cl) {
984 DCHECK_EQ(cl, static_cast<uint8_t>(rCX));
985 if (entry->skeleton.prefix1 != 0) {
986 code_buffer_.push_back(entry->skeleton.prefix1);
987 if (entry->skeleton.prefix2 != 0) {
988 code_buffer_.push_back(entry->skeleton.prefix2);
989 }
990 } else {
991 DCHECK_EQ(0, entry->skeleton.prefix2);
992 }
993 code_buffer_.push_back(entry->skeleton.opcode);
994 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
995 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
996 DCHECK_LT(reg, 8);
997 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
998 code_buffer_.push_back(modrm);
999 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1000 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1001}
1002
1003void X86Mir2Lir::EmitRegCond(const X86EncodingMap* entry, uint8_t reg, uint8_t condition) {
1004 if (entry->skeleton.prefix1 != 0) {
1005 code_buffer_.push_back(entry->skeleton.prefix1);
1006 if (entry->skeleton.prefix2 != 0) {
1007 code_buffer_.push_back(entry->skeleton.prefix2);
1008 }
1009 } else {
1010 DCHECK_EQ(0, entry->skeleton.prefix2);
1011 }
1012 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1013 DCHECK_EQ(0x0F, entry->skeleton.opcode);
1014 code_buffer_.push_back(0x0F);
1015 DCHECK_EQ(0x90, entry->skeleton.extra_opcode1);
1016 code_buffer_.push_back(0x90 | condition);
1017 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1018 DCHECK_LT(reg, 8);
1019 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
1020 code_buffer_.push_back(modrm);
1021 DCHECK_EQ(entry->skeleton.immediate_bytes, 0);
1022}
1023
1024void X86Mir2Lir::EmitJmp(const X86EncodingMap* entry, int rel) {
1025 if (entry->opcode == kX86Jmp8) {
1026 DCHECK(IS_SIMM8(rel));
1027 code_buffer_.push_back(0xEB);
1028 code_buffer_.push_back(rel & 0xFF);
1029 } else if (entry->opcode == kX86Jmp32) {
1030 code_buffer_.push_back(0xE9);
1031 code_buffer_.push_back(rel & 0xFF);
1032 code_buffer_.push_back((rel >> 8) & 0xFF);
1033 code_buffer_.push_back((rel >> 16) & 0xFF);
1034 code_buffer_.push_back((rel >> 24) & 0xFF);
1035 } else {
1036 DCHECK(entry->opcode == kX86JmpR);
1037 code_buffer_.push_back(entry->skeleton.opcode);
1038 uint8_t reg = static_cast<uint8_t>(rel);
1039 DCHECK_LT(reg, 8);
1040 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
1041 code_buffer_.push_back(modrm);
1042 }
1043}
1044
1045void X86Mir2Lir::EmitJcc(const X86EncodingMap* entry, int rel, uint8_t cc) {
1046 DCHECK_LT(cc, 16);
1047 if (entry->opcode == kX86Jcc8) {
1048 DCHECK(IS_SIMM8(rel));
1049 code_buffer_.push_back(0x70 | cc);
1050 code_buffer_.push_back(rel & 0xFF);
1051 } else {
1052 DCHECK(entry->opcode == kX86Jcc32);
1053 code_buffer_.push_back(0x0F);
1054 code_buffer_.push_back(0x80 | cc);
1055 code_buffer_.push_back(rel & 0xFF);
1056 code_buffer_.push_back((rel >> 8) & 0xFF);
1057 code_buffer_.push_back((rel >> 16) & 0xFF);
1058 code_buffer_.push_back((rel >> 24) & 0xFF);
1059 }
1060}
1061
1062void X86Mir2Lir::EmitCallMem(const X86EncodingMap* entry, uint8_t base, int disp) {
1063 if (entry->skeleton.prefix1 != 0) {
1064 code_buffer_.push_back(entry->skeleton.prefix1);
1065 if (entry->skeleton.prefix2 != 0) {
1066 code_buffer_.push_back(entry->skeleton.prefix2);
1067 }
1068 } else {
1069 DCHECK_EQ(0, entry->skeleton.prefix2);
1070 }
1071 code_buffer_.push_back(entry->skeleton.opcode);
1072 if (entry->skeleton.opcode == 0x0F) {
1073 code_buffer_.push_back(entry->skeleton.extra_opcode1);
Vladimir Markoe6ed00b2013-10-24 14:52:37 +01001074 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001075 code_buffer_.push_back(entry->skeleton.extra_opcode2);
1076 } else {
1077 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1078 }
1079 } else {
1080 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1081 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1082 }
1083 uint8_t modrm = (ModrmForDisp(base, disp) << 6) | (entry->skeleton.modrm_opcode << 3) | base;
1084 code_buffer_.push_back(modrm);
1085 if (base == rX86_SP) {
1086 // Special SIB for SP base
1087 code_buffer_.push_back(0 << 6 | (rX86_SP << 3) | rX86_SP);
1088 }
1089 EmitDisp(base, disp);
1090 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1091 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1092}
1093
1094void X86Mir2Lir::EmitCallThread(const X86EncodingMap* entry, int disp) {
1095 DCHECK_NE(entry->skeleton.prefix1, 0);
1096 code_buffer_.push_back(entry->skeleton.prefix1);
1097 if (entry->skeleton.prefix2 != 0) {
1098 code_buffer_.push_back(entry->skeleton.prefix2);
1099 }
1100 code_buffer_.push_back(entry->skeleton.opcode);
1101 if (entry->skeleton.opcode == 0x0F) {
1102 code_buffer_.push_back(entry->skeleton.extra_opcode1);
Vladimir Markoe6ed00b2013-10-24 14:52:37 +01001103 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001104 code_buffer_.push_back(entry->skeleton.extra_opcode2);
1105 } else {
1106 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1107 }
1108 } else {
1109 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1110 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1111 }
1112 uint8_t modrm = (0 << 6) | (entry->skeleton.modrm_opcode << 3) | rBP;
1113 code_buffer_.push_back(modrm);
1114 code_buffer_.push_back(disp & 0xFF);
1115 code_buffer_.push_back((disp >> 8) & 0xFF);
1116 code_buffer_.push_back((disp >> 16) & 0xFF);
1117 code_buffer_.push_back((disp >> 24) & 0xFF);
1118 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1119 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1120}
1121
1122void X86Mir2Lir::EmitPcRel(const X86EncodingMap* entry, uint8_t reg,
1123 int base_or_table, uint8_t index, int scale, int table_or_disp) {
1124 int disp;
1125 if (entry->opcode == kX86PcRelLoadRA) {
buzbee0d829482013-10-11 15:24:55 -07001126 Mir2Lir::EmbeddedData *tab_rec =
1127 reinterpret_cast<Mir2Lir::EmbeddedData*>(UnwrapPointer(table_or_disp));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001128 disp = tab_rec->offset;
1129 } else {
1130 DCHECK(entry->opcode == kX86PcRelAdr);
buzbee0d829482013-10-11 15:24:55 -07001131 Mir2Lir::EmbeddedData *tab_rec =
1132 reinterpret_cast<Mir2Lir::EmbeddedData*>(UnwrapPointer(base_or_table));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001133 disp = tab_rec->offset;
1134 }
1135 if (entry->skeleton.prefix1 != 0) {
1136 code_buffer_.push_back(entry->skeleton.prefix1);
1137 if (entry->skeleton.prefix2 != 0) {
1138 code_buffer_.push_back(entry->skeleton.prefix2);
1139 }
1140 } else {
1141 DCHECK_EQ(0, entry->skeleton.prefix2);
1142 }
1143 if (X86_FPREG(reg)) {
1144 reg = reg & X86_FP_REG_MASK;
1145 }
1146 DCHECK_LT(reg, 8);
1147 if (entry->opcode == kX86PcRelLoadRA) {
1148 code_buffer_.push_back(entry->skeleton.opcode);
1149 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1150 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1151 uint8_t modrm = (2 << 6) | (reg << 3) | rX86_SP;
1152 code_buffer_.push_back(modrm);
1153 DCHECK_LT(scale, 4);
1154 DCHECK_LT(index, 8);
1155 DCHECK_LT(base_or_table, 8);
1156 uint8_t base = static_cast<uint8_t>(base_or_table);
1157 uint8_t sib = (scale << 6) | (index << 3) | base;
1158 code_buffer_.push_back(sib);
1159 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1160 } else {
1161 code_buffer_.push_back(entry->skeleton.opcode + reg);
1162 }
1163 code_buffer_.push_back(disp & 0xFF);
1164 code_buffer_.push_back((disp >> 8) & 0xFF);
1165 code_buffer_.push_back((disp >> 16) & 0xFF);
1166 code_buffer_.push_back((disp >> 24) & 0xFF);
1167 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1168 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1169}
1170
1171void X86Mir2Lir::EmitMacro(const X86EncodingMap* entry, uint8_t reg, int offset) {
1172 DCHECK(entry->opcode == kX86StartOfMethod) << entry->name;
1173 code_buffer_.push_back(0xE8); // call +0
1174 code_buffer_.push_back(0);
1175 code_buffer_.push_back(0);
1176 code_buffer_.push_back(0);
1177 code_buffer_.push_back(0);
1178
1179 DCHECK_LT(reg, 8);
1180 code_buffer_.push_back(0x58 + reg); // pop reg
1181
1182 EmitRegImm(&X86Mir2Lir::EncodingMap[kX86Sub32RI], reg, offset + 5 /* size of call +0 */);
1183}
1184
1185void X86Mir2Lir::EmitUnimplemented(const X86EncodingMap* entry, LIR* lir) {
1186 UNIMPLEMENTED(WARNING) << "encoding kind for " << entry->name << " "
1187 << BuildInsnString(entry->fmt, lir, 0);
1188 for (int i = 0; i < GetInsnSize(lir); ++i) {
1189 code_buffer_.push_back(0xCC); // push breakpoint instruction - int 3
1190 }
1191}
1192
1193/*
1194 * Assemble the LIR into binary instruction format. Note that we may
1195 * discover that pc-relative displacements may not fit the selected
1196 * instruction. In those cases we will try to substitute a new code
1197 * sequence or request that the trace be shortened and retried.
1198 */
buzbee0d829482013-10-11 15:24:55 -07001199AssemblerStatus X86Mir2Lir::AssembleInstructions(CodeOffset start_addr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001200 LIR *lir;
1201 AssemblerStatus res = kSuccess; // Assume success
1202
1203 const bool kVerbosePcFixup = false;
1204 for (lir = first_lir_insn_; lir != NULL; lir = NEXT_LIR(lir)) {
buzbee409fe942013-10-11 10:49:56 -07001205 if (IsPseudoLirOp(lir->opcode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001206 continue;
1207 }
1208
1209 if (lir->flags.is_nop) {
1210 continue;
1211 }
1212
buzbeeb48819d2013-09-14 16:15:25 -07001213 if (lir->flags.fixup != kFixupNone) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001214 switch (lir->opcode) {
1215 case kX86Jcc8: {
1216 LIR *target_lir = lir->target;
1217 DCHECK(target_lir != NULL);
1218 int delta = 0;
buzbee0d829482013-10-11 15:24:55 -07001219 CodeOffset pc;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001220 if (IS_SIMM8(lir->operands[0])) {
1221 pc = lir->offset + 2 /* opcode + rel8 */;
1222 } else {
1223 pc = lir->offset + 6 /* 2 byte opcode + rel32 */;
1224 }
buzbee0d829482013-10-11 15:24:55 -07001225 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001226 delta = target - pc;
1227 if (IS_SIMM8(delta) != IS_SIMM8(lir->operands[0])) {
1228 if (kVerbosePcFixup) {
1229 LOG(INFO) << "Retry for JCC growth at " << lir->offset
1230 << " delta: " << delta << " old delta: " << lir->operands[0];
1231 }
1232 lir->opcode = kX86Jcc32;
1233 SetupResourceMasks(lir);
1234 res = kRetryAll;
1235 }
1236 if (kVerbosePcFixup) {
1237 LOG(INFO) << "Source:";
1238 DumpLIRInsn(lir, 0);
1239 LOG(INFO) << "Target:";
1240 DumpLIRInsn(target_lir, 0);
1241 LOG(INFO) << "Delta " << delta;
1242 }
1243 lir->operands[0] = delta;
1244 break;
1245 }
1246 case kX86Jcc32: {
1247 LIR *target_lir = lir->target;
1248 DCHECK(target_lir != NULL);
buzbee0d829482013-10-11 15:24:55 -07001249 CodeOffset pc = lir->offset + 6 /* 2 byte opcode + rel32 */;
1250 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001251 int delta = target - pc;
1252 if (kVerbosePcFixup) {
1253 LOG(INFO) << "Source:";
1254 DumpLIRInsn(lir, 0);
1255 LOG(INFO) << "Target:";
1256 DumpLIRInsn(target_lir, 0);
1257 LOG(INFO) << "Delta " << delta;
1258 }
1259 lir->operands[0] = delta;
1260 break;
1261 }
1262 case kX86Jmp8: {
1263 LIR *target_lir = lir->target;
1264 DCHECK(target_lir != NULL);
1265 int delta = 0;
buzbee0d829482013-10-11 15:24:55 -07001266 CodeOffset pc;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001267 if (IS_SIMM8(lir->operands[0])) {
1268 pc = lir->offset + 2 /* opcode + rel8 */;
1269 } else {
1270 pc = lir->offset + 5 /* opcode + rel32 */;
1271 }
buzbee0d829482013-10-11 15:24:55 -07001272 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001273 delta = target - pc;
1274 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && delta == 0) {
1275 // Useless branch
buzbee252254b2013-09-08 16:20:53 -07001276 NopLIR(lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001277 if (kVerbosePcFixup) {
1278 LOG(INFO) << "Retry for useless branch at " << lir->offset;
1279 }
1280 res = kRetryAll;
1281 } else if (IS_SIMM8(delta) != IS_SIMM8(lir->operands[0])) {
1282 if (kVerbosePcFixup) {
1283 LOG(INFO) << "Retry for JMP growth at " << lir->offset;
1284 }
1285 lir->opcode = kX86Jmp32;
1286 SetupResourceMasks(lir);
1287 res = kRetryAll;
1288 }
1289 lir->operands[0] = delta;
1290 break;
1291 }
1292 case kX86Jmp32: {
1293 LIR *target_lir = lir->target;
1294 DCHECK(target_lir != NULL);
buzbee0d829482013-10-11 15:24:55 -07001295 CodeOffset pc = lir->offset + 5 /* opcode + rel32 */;
1296 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001297 int delta = target - pc;
1298 lir->operands[0] = delta;
1299 break;
1300 }
1301 default:
1302 break;
1303 }
1304 }
1305
1306 /*
1307 * If one of the pc-relative instructions expanded we'll have
1308 * to make another pass. Don't bother to fully assemble the
1309 * instruction.
1310 */
1311 if (res != kSuccess) {
1312 continue;
1313 }
1314 CHECK_EQ(static_cast<size_t>(lir->offset), code_buffer_.size());
1315 const X86EncodingMap *entry = &X86Mir2Lir::EncodingMap[lir->opcode];
1316 size_t starting_cbuf_size = code_buffer_.size();
1317 switch (entry->kind) {
1318 case kData: // 4 bytes of data
1319 code_buffer_.push_back(lir->operands[0]);
1320 break;
1321 case kNullary: // 1 byte of opcode
1322 DCHECK_EQ(0, entry->skeleton.prefix1);
1323 DCHECK_EQ(0, entry->skeleton.prefix2);
1324 code_buffer_.push_back(entry->skeleton.opcode);
1325 if (entry->skeleton.extra_opcode1 != 0) {
1326 code_buffer_.push_back(entry->skeleton.extra_opcode1);
1327 if (entry->skeleton.extra_opcode2 != 0) {
1328 code_buffer_.push_back(entry->skeleton.extra_opcode2);
1329 }
1330 } else {
1331 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1332 }
1333 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1334 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1335 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1336 break;
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001337 case kRegOpcode: // lir operands - 0: reg
1338 EmitOpRegOpcode(entry, lir->operands[0]);
1339 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001340 case kReg: // lir operands - 0: reg
1341 EmitOpReg(entry, lir->operands[0]);
1342 break;
1343 case kMem: // lir operands - 0: base, 1: disp
1344 EmitOpMem(entry, lir->operands[0], lir->operands[1]);
1345 break;
1346 case kMemReg: // lir operands - 0: base, 1: disp, 2: reg
1347 EmitMemReg(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1348 break;
1349 case kArrayReg: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
1350 EmitArrayReg(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1351 lir->operands[3], lir->operands[4]);
1352 break;
1353 case kRegMem: // lir operands - 0: reg, 1: base, 2: disp
1354 EmitRegMem(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1355 break;
1356 case kRegArray: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
1357 EmitRegArray(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1358 lir->operands[3], lir->operands[4]);
1359 break;
1360 case kRegThread: // lir operands - 0: reg, 1: disp
1361 EmitRegThread(entry, lir->operands[0], lir->operands[1]);
1362 break;
1363 case kRegReg: // lir operands - 0: reg1, 1: reg2
1364 EmitRegReg(entry, lir->operands[0], lir->operands[1]);
1365 break;
1366 case kRegRegStore: // lir operands - 0: reg2, 1: reg1
1367 EmitRegReg(entry, lir->operands[1], lir->operands[0]);
1368 break;
1369 case kRegRegImm:
1370 EmitRegRegImm(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1371 break;
1372 case kRegImm: // lir operands - 0: reg, 1: immediate
1373 EmitRegImm(entry, lir->operands[0], lir->operands[1]);
1374 break;
1375 case kThreadImm: // lir operands - 0: disp, 1: immediate
1376 EmitThreadImm(entry, lir->operands[0], lir->operands[1]);
1377 break;
1378 case kMovRegImm: // lir operands - 0: reg, 1: immediate
1379 EmitMovRegImm(entry, lir->operands[0], lir->operands[1]);
1380 break;
1381 case kShiftRegImm: // lir operands - 0: reg, 1: immediate
1382 EmitShiftRegImm(entry, lir->operands[0], lir->operands[1]);
1383 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001384 case kShiftRegCl: // lir operands - 0: reg, 1: cl
Brian Carlstrom7940e442013-07-12 13:46:57 -07001385 EmitShiftRegCl(entry, lir->operands[0], lir->operands[1]);
1386 break;
1387 case kRegCond: // lir operands - 0: reg, 1: condition
1388 EmitRegCond(entry, lir->operands[0], lir->operands[1]);
1389 break;
1390 case kJmp: // lir operands - 0: rel
1391 EmitJmp(entry, lir->operands[0]);
1392 break;
1393 case kJcc: // lir operands - 0: rel, 1: CC, target assigned
1394 EmitJcc(entry, lir->operands[0], lir->operands[1]);
1395 break;
1396 case kCall:
1397 switch (entry->opcode) {
1398 case kX86CallM: // lir operands - 0: base, 1: disp
1399 EmitCallMem(entry, lir->operands[0], lir->operands[1]);
1400 break;
1401 case kX86CallT: // lir operands - 0: disp
1402 EmitCallThread(entry, lir->operands[0]);
1403 break;
1404 default:
1405 EmitUnimplemented(entry, lir);
1406 break;
1407 }
1408 break;
1409 case kPcRel: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
1410 EmitPcRel(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1411 lir->operands[3], lir->operands[4]);
1412 break;
1413 case kMacro:
1414 EmitMacro(entry, lir->operands[0], lir->offset);
1415 break;
1416 default:
1417 EmitUnimplemented(entry, lir);
1418 break;
1419 }
1420 CHECK_EQ(static_cast<size_t>(GetInsnSize(lir)),
1421 code_buffer_.size() - starting_cbuf_size)
1422 << "Instruction size mismatch for entry: " << X86Mir2Lir::EncodingMap[lir->opcode].name;
1423 }
1424 return res;
1425}
1426
buzbeeb48819d2013-09-14 16:15:25 -07001427// LIR offset assignment.
1428// TODO: consolidate w/ Arm assembly mechanism.
1429int X86Mir2Lir::AssignInsnOffsets() {
1430 LIR* lir;
1431 int offset = 0;
1432
1433 for (lir = first_lir_insn_; lir != NULL; lir = NEXT_LIR(lir)) {
1434 lir->offset = offset;
buzbee409fe942013-10-11 10:49:56 -07001435 if (LIKELY(!IsPseudoLirOp(lir->opcode))) {
buzbeeb48819d2013-09-14 16:15:25 -07001436 if (!lir->flags.is_nop) {
1437 offset += lir->flags.size;
1438 }
1439 } else if (UNLIKELY(lir->opcode == kPseudoPseudoAlign4)) {
1440 if (offset & 0x2) {
1441 offset += 2;
1442 lir->operands[0] = 1;
1443 } else {
1444 lir->operands[0] = 0;
1445 }
1446 }
1447 /* Pseudo opcodes don't consume space */
1448 }
1449 return offset;
1450}
1451
1452/*
1453 * Walk the compilation unit and assign offsets to instructions
1454 * and literals and compute the total size of the compiled unit.
1455 * TODO: consolidate w/ Arm assembly mechanism.
1456 */
1457void X86Mir2Lir::AssignOffsets() {
1458 int offset = AssignInsnOffsets();
1459
1460 /* Const values have to be word aligned */
1461 offset = (offset + 3) & ~3;
1462
1463 /* Set up offsets for literals */
1464 data_offset_ = offset;
1465
1466 offset = AssignLiteralOffset(offset);
1467
1468 offset = AssignSwitchTablesOffset(offset);
1469
1470 offset = AssignFillArrayDataOffset(offset);
1471
1472 total_size_ = offset;
1473}
1474
1475/*
1476 * Go over each instruction in the list and calculate the offset from the top
1477 * before sending them off to the assembler. If out-of-range branch distance is
1478 * seen rearrange the instructions a bit to correct it.
1479 * TODO: consolidate w/ Arm assembly mechanism.
1480 */
1481void X86Mir2Lir::AssembleLIR() {
buzbeea61f4952013-08-23 14:27:06 -07001482 cu_->NewTimingSplit("Assemble");
buzbeeb48819d2013-09-14 16:15:25 -07001483 AssignOffsets();
1484 int assembler_retries = 0;
1485 /*
1486 * Assemble here. Note that we generate code with optimistic assumptions
1487 * and if found now to work, we'll have to redo the sequence and retry.
1488 */
1489
1490 while (true) {
1491 AssemblerStatus res = AssembleInstructions(0);
1492 if (res == kSuccess) {
1493 break;
1494 } else {
1495 assembler_retries++;
1496 if (assembler_retries > MAX_ASSEMBLER_RETRIES) {
1497 CodegenDump();
1498 LOG(FATAL) << "Assembler error - too many retries";
1499 }
1500 // Redo offsets and try again
1501 AssignOffsets();
1502 code_buffer_.clear();
1503 }
1504 }
1505
buzbeea61f4952013-08-23 14:27:06 -07001506 cu_->NewTimingSplit("LiteralData");
buzbeeb48819d2013-09-14 16:15:25 -07001507 // Install literals
1508 InstallLiteralPools();
1509
1510 // Install switch tables
1511 InstallSwitchTables();
1512
1513 // Install fill array data
1514 InstallFillArrayData();
1515
1516 // Create the mapping table and native offset to reference map.
buzbeea61f4952013-08-23 14:27:06 -07001517 cu_->NewTimingSplit("PcMappingTable");
buzbeeb48819d2013-09-14 16:15:25 -07001518 CreateMappingTables();
1519
buzbeea61f4952013-08-23 14:27:06 -07001520 cu_->NewTimingSplit("GcMap");
buzbeeb48819d2013-09-14 16:15:25 -07001521 CreateNativeGcMap();
1522}
1523
Brian Carlstrom7940e442013-07-12 13:46:57 -07001524} // namespace art