blob: 39a036560ee466497468aaf9c35dddfe76768710 [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" }, \
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" }, \
Dmitry Petrochenko96992e82014-05-20 04:03:46 +070079{ 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 \
81{ kX86 ## opname ## 64MR, kMemReg, 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" }, \
82{ kX86 ## opname ## 64AR, kArrayReg, 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" }, \
83{ kX86 ## opname ## 64TR, kThreadReg, mem_use | IS_BINARY_OP | REG_USE1 | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, REX_W, rm32_r32, 0, 0, 0, 0, 0 }, #opname "64TR", "fs:[!0d],!1r" }, \
84{ kX86 ## opname ## 64RR, kRegReg, IS_BINARY_OP | reg_def | REG_USE01 | SETS_CCODES | uses_ccodes, { REX_W, 0, r32_rm32, 0, 0, 0, 0, 0 }, #opname "64RR", "!0r,!1r" }, \
85{ 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]" }, \
86{ 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]" }, \
87{ kX86 ## opname ## 64RT, kRegThread, 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]" }, \
88{ kX86 ## opname ## 64RI, kRegImm, 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 "64RI", "!0r,!1d" }, \
89{ kX86 ## opname ## 64MI, kMemImm, mem_use | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES | uses_ccodes, { REX_W, 0, rm32_i32, 0, 0, rm32_i32_modrm, 0, 4 }, #opname "64MI", "[!0r+!1d],!2d" }, \
90{ kX86 ## opname ## 64AI, kArrayImm, mem_use | IS_QUIN_OP | REG_USE01 | SETS_CCODES | uses_ccodes, { REX_W, 0, rm32_i32, 0, 0, rm32_i32_modrm, 0, 4 }, #opname "64AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
91{ kX86 ## opname ## 64TI, kThreadImm, mem_use | IS_BINARY_OP | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, REX_W, rm32_i32, 0, 0, rm32_i32_modrm, 0, 4 }, #opname "64TI", "fs:[!0d],!1d" }, \
92{ kX86 ## opname ## 64RI8, kRegImm, 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" }, \
93{ kX86 ## opname ## 64MI8, kMemImm, mem_use | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES | uses_ccodes, { REX_W, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "64MI8", "[!0r+!1d],!2d" }, \
94{ kX86 ## opname ## 64AI8, kArrayImm, mem_use | IS_QUIN_OP | REG_USE01 | SETS_CCODES | uses_ccodes, { REX_W, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "64AI8", "[!0r+!1r<<!2d+!3d],!4d" }, \
95{ kX86 ## opname ## 64TI8, kThreadImm, mem_use | IS_BINARY_OP | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, REX_W, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "64TI8", "fs:[!0d],!1d" }
Brian Carlstrom7940e442013-07-12 13:46:57 -070096
97ENCODING_MAP(Add, IS_LOAD | IS_STORE, REG_DEF0, 0,
98 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */,
99 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */,
100 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */,
101 0x80, 0x0 /* RegMem8/imm8 */,
102 0x81, 0x0 /* RegMem32/imm32 */, 0x83, 0x0 /* RegMem32/imm8 */),
103ENCODING_MAP(Or, IS_LOAD | IS_STORE, REG_DEF0, 0,
104 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */,
105 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */,
106 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */,
107 0x80, 0x1 /* RegMem8/imm8 */,
108 0x81, 0x1 /* RegMem32/imm32 */, 0x83, 0x1 /* RegMem32/imm8 */),
109ENCODING_MAP(Adc, IS_LOAD | IS_STORE, REG_DEF0, USES_CCODES,
110 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */,
111 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */,
112 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */,
113 0x80, 0x2 /* RegMem8/imm8 */,
114 0x81, 0x2 /* RegMem32/imm32 */, 0x83, 0x2 /* RegMem32/imm8 */),
115ENCODING_MAP(Sbb, IS_LOAD | IS_STORE, REG_DEF0, USES_CCODES,
116 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */,
117 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */,
118 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */,
119 0x80, 0x3 /* RegMem8/imm8 */,
120 0x81, 0x3 /* RegMem32/imm32 */, 0x83, 0x3 /* RegMem32/imm8 */),
121ENCODING_MAP(And, IS_LOAD | IS_STORE, REG_DEF0, 0,
122 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */,
123 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */,
124 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */,
125 0x80, 0x4 /* RegMem8/imm8 */,
126 0x81, 0x4 /* RegMem32/imm32 */, 0x83, 0x4 /* RegMem32/imm8 */),
127ENCODING_MAP(Sub, IS_LOAD | IS_STORE, REG_DEF0, 0,
128 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */,
129 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */,
130 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */,
131 0x80, 0x5 /* RegMem8/imm8 */,
132 0x81, 0x5 /* RegMem32/imm32 */, 0x83, 0x5 /* RegMem32/imm8 */),
133ENCODING_MAP(Xor, IS_LOAD | IS_STORE, REG_DEF0, 0,
134 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */,
135 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */,
136 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */,
137 0x80, 0x6 /* RegMem8/imm8 */,
138 0x81, 0x6 /* RegMem32/imm32 */, 0x83, 0x6 /* RegMem32/imm8 */),
139ENCODING_MAP(Cmp, IS_LOAD, 0, 0,
140 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */,
141 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */,
142 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */,
143 0x80, 0x7 /* RegMem8/imm8 */,
144 0x81, 0x7 /* RegMem32/imm32 */, 0x83, 0x7 /* RegMem32/imm8 */),
145#undef ENCODING_MAP
146
147 { kX86Imul16RRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { 0x66, 0, 0x69, 0, 0, 0, 0, 2 }, "Imul16RRI", "!0r,!1r,!2d" },
148 { 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" },
149 { 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" },
150
151 { kX86Imul32RRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { 0, 0, 0x69, 0, 0, 0, 0, 4 }, "Imul32RRI", "!0r,!1r,!2d" },
152 { 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" },
153 { 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" },
154 { kX86Imul32RRI8, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { 0, 0, 0x6B, 0, 0, 0, 0, 1 }, "Imul32RRI8", "!0r,!1r,!2d" },
155 { 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" },
156 { 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" },
157
Dmitry Petrochenko96992e82014-05-20 04:03:46 +0700158 { kX86Imul64RRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { REX_W, 0, 0x69, 0, 0, 0, 0, 8 }, "Imul64RRI", "!0r,!1r,!2d" },
159 { kX86Imul64RMI, kRegMemImm, IS_LOAD | IS_QUAD_OP | REG_DEF0_USE1 | SETS_CCODES, { REX_W, 0, 0x69, 0, 0, 0, 0, 8 }, "Imul64RMI", "!0r,[!1r+!2d],!3d" },
160 { kX86Imul64RAI, kRegArrayImm, IS_LOAD | IS_SEXTUPLE_OP | REG_DEF0_USE12 | SETS_CCODES, { REX_W, 0, 0x69, 0, 0, 0, 0, 8 }, "Imul64RAI", "!0r,[!1r+!2r<<!3d+!4d],!5d" },
161 { kX86Imul64RRI8, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { REX_W, 0, 0x6B, 0, 0, 0, 0, 1 }, "Imul64RRI8", "!0r,!1r,!2d" },
162 { kX86Imul64RMI8, kRegMemImm, IS_LOAD | IS_QUAD_OP | REG_DEF0_USE1 | SETS_CCODES, { REX_W, 0, 0x6B, 0, 0, 0, 0, 1 }, "Imul64RMI8", "!0r,[!1r+!2d],!3d" },
163 { kX86Imul64RAI8, kRegArrayImm, IS_LOAD | IS_SEXTUPLE_OP | REG_DEF0_USE12 | SETS_CCODES, { REX_W, 0, 0x6B, 0, 0, 0, 0, 1 }, "Imul64RAI8", "!0r,[!1r+!2r<<!3d+!4d],!5d" },
164
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 { kX86Mov8MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0, 0, 0x88, 0, 0, 0, 0, 0 }, "Mov8MR", "[!0r+!1d],!2r" },
166 { kX86Mov8AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0, 0, 0x88, 0, 0, 0, 0, 0 }, "Mov8AR", "[!0r+!1r<<!2d+!3d],!4r" },
167 { kX86Mov8TR, kThreadReg, IS_STORE | IS_BINARY_OP | REG_USE1, { THREAD_PREFIX, 0, 0x88, 0, 0, 0, 0, 0 }, "Mov8TR", "fs:[!0d],!1r" },
168 { kX86Mov8RR, kRegReg, IS_BINARY_OP | REG_DEF0_USE1, { 0, 0, 0x8A, 0, 0, 0, 0, 0 }, "Mov8RR", "!0r,!1r" },
169 { kX86Mov8RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { 0, 0, 0x8A, 0, 0, 0, 0, 0 }, "Mov8RM", "!0r,[!1r+!2d]" },
170 { kX86Mov8RA, kRegArray, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8A, 0, 0, 0, 0, 0 }, "Mov8RA", "!0r,[!1r+!2r<<!3d+!4d]" },
171 { kX86Mov8RT, kRegThread, IS_LOAD | IS_BINARY_OP | REG_DEF0, { THREAD_PREFIX, 0, 0x8A, 0, 0, 0, 0, 0 }, "Mov8RT", "!0r,fs:[!1d]" },
172 { kX86Mov8RI, kMovRegImm, IS_BINARY_OP | REG_DEF0, { 0, 0, 0xB0, 0, 0, 0, 0, 1 }, "Mov8RI", "!0r,!1d" },
173 { kX86Mov8MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { 0, 0, 0xC6, 0, 0, 0, 0, 1 }, "Mov8MI", "[!0r+!1d],!2d" },
174 { kX86Mov8AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { 0, 0, 0xC6, 0, 0, 0, 0, 1 }, "Mov8AI", "[!0r+!1r<<!2d+!3d],!4d" },
175 { kX86Mov8TI, kThreadImm, IS_STORE | IS_BINARY_OP, { THREAD_PREFIX, 0, 0xC6, 0, 0, 0, 0, 1 }, "Mov8TI", "fs:[!0d],!1d" },
176
177 { kX86Mov16MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov16MR", "[!0r+!1d],!2r" },
178 { kX86Mov16AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x66, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov16AR", "[!0r+!1r<<!2d+!3d],!4r" },
179 { kX86Mov16TR, kThreadReg, IS_STORE | IS_BINARY_OP | REG_USE1, { THREAD_PREFIX, 0x66, 0x89, 0, 0, 0, 0, 0 }, "Mov16TR", "fs:[!0d],!1r" },
180 { kX86Mov16RR, kRegReg, IS_BINARY_OP | REG_DEF0_USE1, { 0x66, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov16RR", "!0r,!1r" },
181 { kX86Mov16RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { 0x66, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov16RM", "!0r,[!1r+!2d]" },
182 { kX86Mov16RA, kRegArray, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0x66, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov16RA", "!0r,[!1r+!2r<<!3d+!4d]" },
183 { kX86Mov16RT, kRegThread, IS_LOAD | IS_BINARY_OP | REG_DEF0, { THREAD_PREFIX, 0x66, 0x8B, 0, 0, 0, 0, 0 }, "Mov16RT", "!0r,fs:[!1d]" },
184 { kX86Mov16RI, kMovRegImm, IS_BINARY_OP | REG_DEF0, { 0x66, 0, 0xB8, 0, 0, 0, 0, 2 }, "Mov16RI", "!0r,!1d" },
185 { kX86Mov16MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { 0x66, 0, 0xC7, 0, 0, 0, 0, 2 }, "Mov16MI", "[!0r+!1d],!2d" },
186 { kX86Mov16AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { 0x66, 0, 0xC7, 0, 0, 0, 0, 2 }, "Mov16AI", "[!0r+!1r<<!2d+!3d],!4d" },
187 { kX86Mov16TI, kThreadImm, IS_STORE | IS_BINARY_OP, { THREAD_PREFIX, 0x66, 0xC7, 0, 0, 0, 0, 2 }, "Mov16TI", "fs:[!0d],!1d" },
188
189 { kX86Mov32MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov32MR", "[!0r+!1d],!2r" },
190 { kX86Mov32AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov32AR", "[!0r+!1r<<!2d+!3d],!4r" },
191 { kX86Mov32TR, kThreadReg, IS_STORE | IS_BINARY_OP | REG_USE1, { THREAD_PREFIX, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov32TR", "fs:[!0d],!1r" },
192 { kX86Mov32RR, kRegReg, IS_BINARY_OP | REG_DEF0_USE1, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov32RR", "!0r,!1r" },
193 { kX86Mov32RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov32RM", "!0r,[!1r+!2d]" },
194 { kX86Mov32RA, kRegArray, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov32RA", "!0r,[!1r+!2r<<!3d+!4d]" },
195 { kX86Mov32RT, kRegThread, IS_LOAD | IS_BINARY_OP | REG_DEF0, { THREAD_PREFIX, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov32RT", "!0r,fs:[!1d]" },
196 { kX86Mov32RI, kMovRegImm, IS_BINARY_OP | REG_DEF0, { 0, 0, 0xB8, 0, 0, 0, 0, 4 }, "Mov32RI", "!0r,!1d" },
197 { kX86Mov32MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { 0, 0, 0xC7, 0, 0, 0, 0, 4 }, "Mov32MI", "[!0r+!1d],!2d" },
198 { kX86Mov32AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { 0, 0, 0xC7, 0, 0, 0, 0, 4 }, "Mov32AI", "[!0r+!1r<<!2d+!3d],!4d" },
199 { kX86Mov32TI, kThreadImm, IS_STORE | IS_BINARY_OP, { THREAD_PREFIX, 0, 0xC7, 0, 0, 0, 0, 4 }, "Mov32TI", "fs:[!0d],!1d" },
200
Dmitry Petrochenko96992e82014-05-20 04:03:46 +0700201 { 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 -0800202
Dmitry Petrochenko96992e82014-05-20 04:03:46 +0700203 { kX86Lea32RA, kRegArray, IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8D, 0, 0, 0, 0, 0 }, "Lea32RA", "!0r,[!1r+!2r<<!3d+!4d]" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700204
Dmitry Petrochenko96992e82014-05-20 04:03:46 +0700205 { kX86Mov64MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { REX_W, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov64MR", "[!0r+!1d],!2r" },
206 { kX86Mov64AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { REX_W, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov64AR", "[!0r+!1r<<!2d+!3d],!4r" },
207 { kX86Mov64TR, kThreadReg, IS_STORE | IS_BINARY_OP | REG_USE1, { THREAD_PREFIX, REX_W, 0x89, 0, 0, 0, 0, 0 }, "Mov64TR", "fs:[!0d],!1r" },
208 { kX86Mov64RR, kRegReg, IS_BINARY_OP | REG_DEF0_USE1, { REX_W, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov64RR", "!0r,!1r" },
209 { kX86Mov64RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { REX_W, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov64RM", "!0r,[!1r+!2d]" },
210 { 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]" },
211 { kX86Mov64RT, kRegThread, IS_LOAD | IS_BINARY_OP | REG_DEF0, { THREAD_PREFIX, REX_W, 0x8B, 0, 0, 0, 0, 0 }, "Mov64RT", "!0r,fs:[!1d]" },
212 { kX86Mov64RI, kMovRegImm, IS_BINARY_OP | REG_DEF0, { REX_W, 0, 0xB8, 0, 0, 0, 0, 8 }, "Mov64RI", "!0r,!1d" },
213 { kX86Mov64MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { REX_W, 0, 0xC7, 0, 0, 0, 0, 8 }, "Mov64MI", "[!0r+!1d],!2d" },
214 { kX86Mov64AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { REX_W, 0, 0xC7, 0, 0, 0, 0, 8 }, "Mov64AI", "[!0r+!1r<<!2d+!3d],!4d" },
215 { kX86Mov64TI, kThreadImm, IS_STORE | IS_BINARY_OP, { THREAD_PREFIX, REX_W, 0xC7, 0, 0, 0, 0, 8 }, "Mov64TI", "fs:[!0d],!1d" },
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800216
Dmitry Petrochenko96992e82014-05-20 04:03:46 +0700217 { kX86Lea64RM, kRegMem, IS_TERTIARY_OP | IS_LOAD | REG_DEF0_USE1, { REX_W, 0, 0x8D, 0, 0, 0, 0, 0 }, "Lea64RM", "!0r,[!1r+!2d]" },
218
219 { kX86Lea64RA, kRegArray, IS_QUIN_OP | REG_DEF0_USE12, { REX_W, 0, 0x8D, 0, 0, 0, 0, 0 }, "Lea64RA", "!0r,[!1r+!2r<<!3d+!4d]" },
220
221 { kX86Cmov32RRC, kRegRegCond, IS_TERTIARY_OP | REG_DEF0_USE01 | USES_CCODES, {0, 0, 0x0F, 0x40, 0, 0, 0, 0}, "Cmovcc32RR", "!2c !0r,!1r" },
222 { kX86Cmov64RRC, kRegRegCond, IS_TERTIARY_OP | REG_DEF0_USE01 | USES_CCODES, {REX_W, 0, 0x0F, 0x40, 0, 0, 0, 0}, "Cmovcc64RR", "!2c !0r,!1r" },
223
224 { 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]" },
225 { kX86Cmov64RMC, kRegMemCond, IS_QUAD_OP | IS_LOAD | REG_DEF0_USE01 | USES_CCODES, {REX_W, 0, 0x0F, 0x40, 0, 0, 0, 0}, "Cmovcc64RM", "!3c !0r,[!1r+!2d]" },
Mark Mendell2637f2e2014-04-30 10:10:47 -0400226
Brian Carlstrom7940e442013-07-12 13:46:57 -0700227#define SHIFT_ENCODING_MAP(opname, modrm_opcode) \
228{ 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" }, \
229{ 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" }, \
230{ 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" }, \
231{ 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" }, \
232{ 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" }, \
233{ 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" }, \
234 \
235{ 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" }, \
236{ 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" }, \
237{ 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" }, \
238{ 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" }, \
239{ 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" }, \
240{ 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" }, \
241 \
242{ 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" }, \
243{ 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" }, \
244{ 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" }, \
245{ 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" }, \
246{ 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" }, \
Dmitry Petrochenko96992e82014-05-20 04:03:46 +0700247{ 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" }, \
248 \
249{ kX86 ## opname ## 64RI, kShiftRegImm, IS_BINARY_OP | REG_DEF0_USE0 | SETS_CCODES, { REX_W, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "64RI", "!0r,!1d" }, \
250{ kX86 ## opname ## 64MI, kShiftMemImm, IS_LOAD | IS_STORE | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { REX_W, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "64MI", "[!0r+!1d],!2d" }, \
251{ kX86 ## opname ## 64AI, kShiftArrayImm, IS_LOAD | IS_STORE | IS_QUIN_OP | REG_USE01 | SETS_CCODES, { REX_W, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "64AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
252{ kX86 ## opname ## 64RC, kShiftRegCl, IS_BINARY_OP | REG_DEF0_USE0 | REG_USEC | SETS_CCODES, { REX_W, 0, 0xD3, 0, 0, modrm_opcode, 0, 0 }, #opname "64RC", "!0r,cl" }, \
253{ kX86 ## opname ## 64MC, kShiftMemCl, IS_LOAD | IS_STORE | IS_TERTIARY_OP | REG_USE0 | REG_USEC | SETS_CCODES, { REX_W, 0, 0xD3, 0, 0, modrm_opcode, 0, 0 }, #opname "64MC", "[!0r+!1d],cl" }, \
254{ kX86 ## opname ## 64AC, kShiftArrayCl, IS_LOAD | IS_STORE | IS_QUIN_OP | REG_USE01 | REG_USEC | SETS_CCODES, { REX_W, 0, 0xD3, 0, 0, modrm_opcode, 0, 0 }, #opname "64AC", "[!0r+!1r<<!2d+!3d],cl" }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700255
256 SHIFT_ENCODING_MAP(Rol, 0x0),
257 SHIFT_ENCODING_MAP(Ror, 0x1),
258 SHIFT_ENCODING_MAP(Rcl, 0x2),
259 SHIFT_ENCODING_MAP(Rcr, 0x3),
260 SHIFT_ENCODING_MAP(Sal, 0x4),
261 SHIFT_ENCODING_MAP(Shr, 0x5),
262 SHIFT_ENCODING_MAP(Sar, 0x7),
263#undef SHIFT_ENCODING_MAP
264
265 { kX86Cmc, kNullary, NO_OPERAND, { 0, 0, 0xF5, 0, 0, 0, 0, 0}, "Cmc", "" },
Mark Mendell2637f2e2014-04-30 10:10:47 -0400266 { kX86Shld32RRI, kRegRegImmRev, IS_TERTIARY_OP | REG_DEF0_USE01 | SETS_CCODES, { 0, 0, 0x0F, 0xA4, 0, 0, 0, 1}, "Shld32RRI", "!0r,!1r,!2d" },
267 { 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" },
268 { kX86Shrd32RRI, kRegRegImmRev, IS_TERTIARY_OP | REG_DEF0_USE01 | SETS_CCODES, { 0, 0, 0x0F, 0xAC, 0, 0, 0, 1}, "Shrd32RRI", "!0r,!1r,!2d" },
269 { 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" },
Dmitry Petrochenko96992e82014-05-20 04:03:46 +0700270 { kX86Shld64RRI, kRegRegImmRev, IS_TERTIARY_OP | REG_DEF0_USE01 | SETS_CCODES, { REX_W, 0, 0x0F, 0xA4, 0, 0, 0, 1}, "Shld64RRI", "!0r,!1r,!2d" },
271 { kX86Shld64MRI, kMemRegImm, IS_QUAD_OP | REG_USE02 | IS_LOAD | IS_STORE | SETS_CCODES, { REX_W, 0, 0x0F, 0xA4, 0, 0, 0, 1}, "Shld64MRI", "[!0r+!1d],!2r,!3d" },
272 { kX86Shrd64RRI, kRegRegImmRev, IS_TERTIARY_OP | REG_DEF0_USE01 | SETS_CCODES, { REX_W, 0, 0x0F, 0xAC, 0, 0, 0, 1}, "Shrd64RRI", "!0r,!1r,!2d" },
273 { kX86Shrd64MRI, kMemRegImm, IS_QUAD_OP | REG_USE02 | IS_LOAD | IS_STORE | SETS_CCODES, { REX_W, 0, 0x0F, 0xAC, 0, 0, 0, 1}, "Shrd64MRI", "[!0r+!1d],!2r,!3d" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274
275 { kX86Test8RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF6, 0, 0, 0, 0, 1}, "Test8RI", "!0r,!1d" },
276 { kX86Test8MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF6, 0, 0, 0, 0, 1}, "Test8MI", "[!0r+!1d],!2d" },
277 { 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" },
278 { kX86Test16RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0x66, 0, 0xF7, 0, 0, 0, 0, 2}, "Test16RI", "!0r,!1d" },
279 { kX86Test16MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0x66, 0, 0xF7, 0, 0, 0, 0, 2}, "Test16MI", "[!0r+!1d],!2d" },
280 { 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" },
281 { kX86Test32RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF7, 0, 0, 0, 0, 4}, "Test32RI", "!0r,!1d" },
282 { kX86Test32MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF7, 0, 0, 0, 0, 4}, "Test32MI", "[!0r+!1d],!2d" },
283 { 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" },
Dmitry Petrochenko96992e82014-05-20 04:03:46 +0700284 { kX86Test64RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { REX_W, 0, 0xF7, 0, 0, 0, 0, 8}, "Test64RI", "!0r,!1d" },
285 { kX86Test64MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { REX_W, 0, 0xF7, 0, 0, 0, 0, 8}, "Test64MI", "[!0r+!1d],!2d" },
286 { kX86Test64AI, kArrayImm, IS_LOAD | IS_QUIN_OP | REG_USE01 | SETS_CCODES, { REX_W, 0, 0xF7, 0, 0, 0, 0, 8}, "Test64AI", "[!0r+!1r<<!2d+!3d],!4d" },
287
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288 { kX86Test32RR, kRegReg, IS_BINARY_OP | REG_USE01 | SETS_CCODES, { 0, 0, 0x85, 0, 0, 0, 0, 0}, "Test32RR", "!0r,!1r" },
Dmitry Petrochenko96992e82014-05-20 04:03:46 +0700289 { kX86Test64RR, kRegReg, IS_BINARY_OP | REG_USE01 | SETS_CCODES, { REX_W, 0, 0x85, 0, 0, 0, 0, 0}, "Test64RR", "!0r,!1r" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700290
291#define UNARY_ENCODING_MAP(opname, modrm, is_store, sets_ccodes, \
292 reg, reg_kind, reg_flags, \
293 mem, mem_kind, mem_flags, \
294 arr, arr_kind, arr_flags, imm, \
295 b_flags, hw_flags, w_flags, \
296 b_format, hw_format, w_format) \
Mark Mendell2637f2e2014-04-30 10:10:47 -0400297{ 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" }, \
298{ 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]" }, \
299{ 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]" }, \
300{ 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" }, \
301{ 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]" }, \
302{ 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]" }, \
303{ 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" }, \
304{ 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]" }, \
Dmitry Petrochenko96992e82014-05-20 04:03:46 +0700305{ 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]" }, \
306{ kX86 ## opname ## 64 ## reg, reg_kind, reg_flags | w_flags | sets_ccodes, { REX_W, 0, 0xF7, 0, 0, modrm, 0, imm << 2}, #opname "64" #reg, w_format "!0r" }, \
307{ kX86 ## opname ## 64 ## mem, mem_kind, IS_LOAD | is_store | mem_flags | w_flags | sets_ccodes, { REX_W, 0, 0xF7, 0, 0, modrm, 0, imm << 2}, #opname "64" #mem, w_format "[!0r+!1d]" }, \
308{ kX86 ## opname ## 64 ## arr, arr_kind, IS_LOAD | is_store | arr_flags | w_flags | sets_ccodes, { REX_W, 0, 0xF7, 0, 0, modrm, 0, imm << 2}, #opname "64" #arr, w_format "[!0r+!1r<<!2d+!3d]" }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700309
310 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, "", "", ""),
311 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, "", "", ""),
312
Mark Mendell2bf31e62014-01-23 12:13:40 -0800313 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,"),
314 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,"),
315 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,"),
316 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 -0700317#undef UNARY_ENCODING_MAP
318
Mark Mendell2bf31e62014-01-23 12:13:40 -0800319 { kx86Cdq32Da, kRegOpcode, NO_OPERAND | REG_DEFAD_USEA, { 0, 0, 0x99, 0, 0, 0, 0, 0 }, "Cdq", "" },
Vladimir Markoa6fd8ba2013-12-13 10:53:49 +0000320 { kX86Bswap32R, kRegOpcode, IS_UNARY_OP | REG_DEF0_USE0, { 0, 0, 0x0F, 0xC8, 0, 0, 0, 0 }, "Bswap32R", "!0r" },
321 { 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" },
322 { 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 +0100323
Brian Carlstrom7940e442013-07-12 13:46:57 -0700324#define EXT_0F_ENCODING_MAP(opname, prefix, opcode, reg_def) \
Mark Mendell2637f2e2014-04-30 10:10:47 -0400325{ kX86 ## opname ## RR, kRegReg, IS_BINARY_OP | reg_def | REG_USE1, { prefix, 0, 0x0F, opcode, 0, 0, 0, 0 }, #opname "RR", "!0r,!1r" }, \
326{ 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]" }, \
327{ 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 -0700328
Mark Mendellfe945782014-05-22 09:52:36 -0400329#define EXT_0F_ENCODING2_MAP(opname, prefix, opcode, opcode2, reg_def) \
330{ kX86 ## opname ## RR, kRegReg, IS_BINARY_OP | reg_def | REG_USE1, { prefix, 0, 0x0F, opcode, opcode2, 0, 0, 0 }, #opname "RR", "!0r,!1r" }, \
331{ 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]" }, \
332{ 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]" }
333
Brian Carlstrom7940e442013-07-12 13:46:57 -0700334 EXT_0F_ENCODING_MAP(Movsd, 0xF2, 0x10, REG_DEF0),
335 { kX86MovsdMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0xF2, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovsdMR", "[!0r+!1d],!2r" },
336 { kX86MovsdAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0xF2, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovsdAR", "[!0r+!1r<<!2d+!3d],!4r" },
337
338 EXT_0F_ENCODING_MAP(Movss, 0xF3, 0x10, REG_DEF0),
339 { kX86MovssMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0xF3, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovssMR", "[!0r+!1d],!2r" },
340 { kX86MovssAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0xF3, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovssAR", "[!0r+!1r<<!2d+!3d],!4r" },
341
342 EXT_0F_ENCODING_MAP(Cvtsi2sd, 0xF2, 0x2A, REG_DEF0),
343 EXT_0F_ENCODING_MAP(Cvtsi2ss, 0xF3, 0x2A, REG_DEF0),
344 EXT_0F_ENCODING_MAP(Cvttsd2si, 0xF2, 0x2C, REG_DEF0),
345 EXT_0F_ENCODING_MAP(Cvttss2si, 0xF3, 0x2C, REG_DEF0),
346 EXT_0F_ENCODING_MAP(Cvtsd2si, 0xF2, 0x2D, REG_DEF0),
347 EXT_0F_ENCODING_MAP(Cvtss2si, 0xF3, 0x2D, REG_DEF0),
Mark Mendell2637f2e2014-04-30 10:10:47 -0400348 EXT_0F_ENCODING_MAP(Ucomisd, 0x66, 0x2E, SETS_CCODES|REG_USE0),
349 EXT_0F_ENCODING_MAP(Ucomiss, 0x00, 0x2E, SETS_CCODES|REG_USE0),
350 EXT_0F_ENCODING_MAP(Comisd, 0x66, 0x2F, SETS_CCODES|REG_USE0),
351 EXT_0F_ENCODING_MAP(Comiss, 0x00, 0x2F, SETS_CCODES|REG_USE0),
352 EXT_0F_ENCODING_MAP(Orps, 0x00, 0x56, REG_DEF0_USE0),
353 EXT_0F_ENCODING_MAP(Xorps, 0x00, 0x57, REG_DEF0_USE0),
354 EXT_0F_ENCODING_MAP(Addsd, 0xF2, 0x58, REG_DEF0_USE0),
355 EXT_0F_ENCODING_MAP(Addss, 0xF3, 0x58, REG_DEF0_USE0),
356 EXT_0F_ENCODING_MAP(Mulsd, 0xF2, 0x59, REG_DEF0_USE0),
357 EXT_0F_ENCODING_MAP(Mulss, 0xF3, 0x59, REG_DEF0_USE0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700358 EXT_0F_ENCODING_MAP(Cvtsd2ss, 0xF2, 0x5A, REG_DEF0),
359 EXT_0F_ENCODING_MAP(Cvtss2sd, 0xF3, 0x5A, REG_DEF0),
Mark Mendell2637f2e2014-04-30 10:10:47 -0400360 EXT_0F_ENCODING_MAP(Subsd, 0xF2, 0x5C, REG_DEF0_USE0),
361 EXT_0F_ENCODING_MAP(Subss, 0xF3, 0x5C, REG_DEF0_USE0),
362 EXT_0F_ENCODING_MAP(Divsd, 0xF2, 0x5E, REG_DEF0_USE0),
363 EXT_0F_ENCODING_MAP(Divss, 0xF3, 0x5E, REG_DEF0_USE0),
364 EXT_0F_ENCODING_MAP(Punpckldq, 0x66, 0x62, REG_DEF0_USE0),
Mark Mendellfe945782014-05-22 09:52:36 -0400365 EXT_0F_ENCODING_MAP(Sqrtsd, 0xF2, 0x51, REG_DEF0_USE0),
366 EXT_0F_ENCODING2_MAP(Pmulld, 0x66, 0x38, 0x40, REG_DEF0_USE0),
367 EXT_0F_ENCODING_MAP(Pmullw, 0x66, 0xD5, REG_DEF0_USE0),
368 EXT_0F_ENCODING_MAP(Mulps, 0x00, 0x59, REG_DEF0_USE0),
369 EXT_0F_ENCODING_MAP(Mulpd, 0x66, 0x59, REG_DEF0_USE0),
370 EXT_0F_ENCODING_MAP(Paddb, 0x66, 0xFC, REG_DEF0_USE0),
371 EXT_0F_ENCODING_MAP(Paddw, 0x66, 0xFD, REG_DEF0_USE0),
372 EXT_0F_ENCODING_MAP(Paddd, 0x66, 0xFE, REG_DEF0_USE0),
373 EXT_0F_ENCODING_MAP(Addps, 0x00, 0x58, REG_DEF0_USE0),
374 EXT_0F_ENCODING_MAP(Addpd, 0xF2, 0x58, REG_DEF0_USE0),
375 EXT_0F_ENCODING_MAP(Psubb, 0x66, 0xF8, REG_DEF0_USE0),
376 EXT_0F_ENCODING_MAP(Psubw, 0x66, 0xF9, REG_DEF0_USE0),
377 EXT_0F_ENCODING_MAP(Psubd, 0x66, 0xFA, REG_DEF0_USE0),
378 EXT_0F_ENCODING_MAP(Subps, 0x00, 0x5C, REG_DEF0_USE0),
379 EXT_0F_ENCODING_MAP(Subpd, 0x66, 0x5C, REG_DEF0_USE0),
380 EXT_0F_ENCODING_MAP(Pand, 0x66, 0xDB, REG_DEF0_USE0),
381 EXT_0F_ENCODING_MAP(Por, 0x66, 0xEB, REG_DEF0_USE0),
382 EXT_0F_ENCODING_MAP(Pxor, 0x66, 0xEF, REG_DEF0_USE0),
383 EXT_0F_ENCODING2_MAP(Phaddw, 0x66, 0x38, 0x01, REG_DEF0_USE0),
384 EXT_0F_ENCODING2_MAP(Phaddd, 0x66, 0x38, 0x02, REG_DEF0_USE0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700385
Mark Mendellfe945782014-05-22 09:52:36 -0400386 { kX86PextrbRRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0x66, 0, 0x0F, 0x3A, 0x14, 0, 0, 1 }, "PextbRRI", "!0r,!1r,!2d" },
387 { kX86PextrwRRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0x66, 0, 0x0F, 0xC5, 0x00, 0, 0, 1 }, "PextwRRI", "!0r,!1r,!2d" },
388 { kX86PextrdRRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0x66, 0, 0x0F, 0x3A, 0x16, 0, 0, 1 }, "PextdRRI", "!0r,!1r,!2d" },
389
390 { kX86PshuflwRRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0xF2, 0, 0x0F, 0x70, 0, 0, 0, 1 }, "PshuflwRRI", "!0r,!1r,!2d" },
391 { kX86PshufdRRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0x66, 0, 0x0F, 0x70, 0, 0, 0, 1 }, "PshuffRRI", "!0r,!1r,!2d" },
392
393 { kX86PsrawRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x71, 0, 4, 0, 1 }, "PsrawRI", "!0r,!1d" },
394 { kX86PsradRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x72, 0, 4, 0, 1 }, "PsradRI", "!0r,!1d" },
395 { kX86PsrlwRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x71, 0, 2, 0, 1 }, "PsrlwRI", "!0r,!1d" },
396 { 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 -0700397 { 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 -0400398 { kX86PsllwRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x71, 0, 6, 0, 1 }, "PsllwRI", "!0r,!1d" },
399 { 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 -0700400 { 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 -0800401
Serguei Katkove90501d2014-03-12 15:56:54 +0700402 { kX86Fild32M, kMem, IS_LOAD | IS_UNARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xDB, 0x00, 0, 0, 0, 0 }, "Fild32M", "[!0r,!1d]" },
403 { kX86Fild64M, kMem, IS_LOAD | IS_UNARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xDF, 0x00, 0, 5, 0, 0 }, "Fild64M", "[!0r,!1d]" },
404 { kX86Fstp32M, kMem, IS_STORE | IS_UNARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xD9, 0x00, 0, 3, 0, 0 }, "FstpsM", "[!0r,!1d]" },
405 { 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 -0700406
Mark Mendelld65c51a2014-04-29 16:55:20 -0400407 EXT_0F_ENCODING_MAP(Mova128, 0x66, 0x6F, REG_DEF0),
408 { kX86Mova128MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, 0, 0x0F, 0x6F, 0, 0, 0, 0 }, "Mova128MR", "[!0r+!1d],!2r" },
409 { kX86Mova128AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x66, 0, 0x0F, 0x6F, 0, 0, 0, 0 }, "Mova128AR", "[!0r+!1r<<!2d+!3d],!4r" },
410
411
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800412 EXT_0F_ENCODING_MAP(Movups, 0x0, 0x10, REG_DEF0),
413 { kX86MovupsMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x0, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovupsMR", "[!0r+!1d],!2r" },
414 { kX86MovupsAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x0, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovupsAR", "[!0r+!1r<<!2d+!3d],!4r" },
415
416 EXT_0F_ENCODING_MAP(Movaps, 0x0, 0x28, REG_DEF0),
417 { kX86MovapsMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x0, 0, 0x0F, 0x29, 0, 0, 0, 0 }, "MovapsMR", "[!0r+!1d],!2r" },
418 { kX86MovapsAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x0, 0, 0x0F, 0x29, 0, 0, 0, 0 }, "MovapsAR", "[!0r+!1r<<!2d+!3d],!4r" },
419
420 { kX86MovlpsRM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0 | REG_USE01, { 0x0, 0, 0x0F, 0x12, 0, 0, 0, 0 }, "MovlpsRM", "!0r,[!1r+!2d]" },
421 { 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]" },
422 { kX86MovlpsMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x0, 0, 0x0F, 0x13, 0, 0, 0, 0 }, "MovlpsMR", "[!0r+!1d],!2r" },
423 { kX86MovlpsAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x0, 0, 0x0F, 0x13, 0, 0, 0, 0 }, "MovlpsAR", "[!0r+!1r<<!2d+!3d],!4r" },
424
425 { kX86MovhpsRM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0 | REG_USE01, { 0x0, 0, 0x0F, 0x16, 0, 0, 0, 0 }, "MovhpsRM", "!0r,[!1r+!2d]" },
426 { 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]" },
427 { kX86MovhpsMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x0, 0, 0x0F, 0x17, 0, 0, 0, 0 }, "MovhpsMR", "[!0r+!1d],!2r" },
428 { kX86MovhpsAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x0, 0, 0x0F, 0x17, 0, 0, 0, 0 }, "MovhpsAR", "[!0r+!1r<<!2d+!3d],!4r" },
429
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430 EXT_0F_ENCODING_MAP(Movdxr, 0x66, 0x6E, REG_DEF0),
Mark Mendell2637f2e2014-04-30 10:10:47 -0400431 { 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 -0700432 { kX86MovdrxMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, 0, 0x0F, 0x7E, 0, 0, 0, 0 }, "MovdrxMR", "[!0r+!1d],!2r" },
433 { kX86MovdrxAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x66, 0, 0x0F, 0x7E, 0, 0, 0, 0 }, "MovdrxAR", "[!0r+!1r<<!2d+!3d],!4r" },
434
435 { kX86Set8R, kRegCond, IS_BINARY_OP | REG_DEF0 | USES_CCODES, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0 }, "Set8R", "!1c !0r" },
436 { kX86Set8M, kMemCond, IS_STORE | IS_TERTIARY_OP | REG_USE0 | USES_CCODES, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0 }, "Set8M", "!2c [!0r+!1d]" },
437 { 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]" },
438
439 // TODO: load/store?
440 // Encode the modrm opcode as an extra opcode byte to avoid computation during assembly.
441 { kX86Mfence, kReg, NO_OPERAND, { 0, 0, 0x0F, 0xAE, 0, 6, 0, 0 }, "Mfence", "" },
442
Mark Mendell2637f2e2014-04-30 10:10:47 -0400443 EXT_0F_ENCODING_MAP(Imul16, 0x66, 0xAF, REG_USE0 | REG_DEF0 | SETS_CCODES),
444 EXT_0F_ENCODING_MAP(Imul32, 0x00, 0xAF, REG_USE0 | REG_DEF0 | SETS_CCODES),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700445
446 { 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" },
447 { 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" },
448 { 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 -0700449 { 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" },
450 { 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 +0000451 { 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]" },
452 { 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 -0800453 { 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 -0700454
455 EXT_0F_ENCODING_MAP(Movzx8, 0x00, 0xB6, REG_DEF0),
456 EXT_0F_ENCODING_MAP(Movzx16, 0x00, 0xB7, REG_DEF0),
457 EXT_0F_ENCODING_MAP(Movsx8, 0x00, 0xBE, REG_DEF0),
458 EXT_0F_ENCODING_MAP(Movsx16, 0x00, 0xBF, REG_DEF0),
459#undef EXT_0F_ENCODING_MAP
460
461 { kX86Jcc8, kJcc, IS_BINARY_OP | IS_BRANCH | NEEDS_FIXUP | USES_CCODES, { 0, 0, 0x70, 0, 0, 0, 0, 0 }, "Jcc8", "!1c !0t" },
462 { kX86Jcc32, kJcc, IS_BINARY_OP | IS_BRANCH | NEEDS_FIXUP | USES_CCODES, { 0, 0, 0x0F, 0x80, 0, 0, 0, 0 }, "Jcc32", "!1c !0t" },
463 { kX86Jmp8, kJmp, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0xEB, 0, 0, 0, 0, 0 }, "Jmp8", "!0t" },
464 { kX86Jmp32, kJmp, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0xE9, 0, 0, 0, 0, 0 }, "Jmp32", "!0t" },
465 { 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 -0800466 { 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 -0700467 { 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 -0700468 { kX86CallR, kCall, IS_UNARY_OP | IS_BRANCH | REG_USE0, { 0, 0, 0xE8, 0, 0, 0, 0, 0 }, "CallR", "!0r" },
469 { kX86CallM, kCall, IS_BINARY_OP | IS_BRANCH | IS_LOAD | REG_USE0, { 0, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallM", "[!0r+!1d]" },
470 { kX86CallA, kCall, IS_QUAD_OP | IS_BRANCH | IS_LOAD | REG_USE01, { 0, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallA", "[!0r+!1r<<!2d+!3d]" },
471 { 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 -0800472 { kX86CallI, kCall, IS_UNARY_OP | IS_BRANCH, { 0, 0, 0xE8, 0, 0, 0, 0, 4 }, "CallI", "!0d" },
Brian Carlstromb1eba212013-07-17 18:07:19 -0700473 { kX86Ret, kNullary, NO_OPERAND | IS_BRANCH, { 0, 0, 0xC3, 0, 0, 0, 0, 0 }, "Ret", "" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700474
475 { kX86StartOfMethod, kMacro, IS_UNARY_OP | SETS_CCODES, { 0, 0, 0, 0, 0, 0, 0, 0 }, "StartOfMethod", "!0r" },
476 { kX86PcRelLoadRA, kPcRel, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "PcRelLoadRA", "!0r,[!1r+!2r<<!3d+!4p]" },
477 { 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 -0400478 { 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 -0700479};
480
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700481size_t X86Mir2Lir::ComputeSize(const X86EncodingMap* entry, int base, int displacement,
482 int reg_r, int reg_x, bool has_sib) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483 size_t size = 0;
484 if (entry->skeleton.prefix1 > 0) {
485 ++size;
486 if (entry->skeleton.prefix2 > 0) {
487 ++size;
488 }
489 }
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700490 if ((NeedsRex(base) || NeedsRex(reg_r) || NeedsRex(reg_x)) &&
491 entry->skeleton.prefix1 != REX_W && entry->skeleton.prefix2 != REX_W) {
492 ++size; // REX_R
493 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700494 ++size; // opcode
495 if (entry->skeleton.opcode == 0x0F) {
496 ++size;
497 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
498 ++size;
499 }
500 }
501 ++size; // modrm
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700502 if (has_sib || LowRegisterBits(RegStorage::RegNum(base)) == rs_rX86_SP.GetRegNum()
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700503 || (Gen64Bit() && entry->skeleton.prefix1 == THREAD_PREFIX)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700504 // SP requires a SIB byte.
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700505 // GS access also needs a SIB byte for absolute adressing in 64-bit mode.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700506 ++size;
507 }
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700508 if (displacement != 0 || LowRegisterBits(RegStorage::RegNum(base)) == rs_rBP.GetRegNum()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509 // BP requires an explicit displacement, even when it's 0.
510 if (entry->opcode != kX86Lea32RA) {
511 DCHECK_NE(entry->flags & (IS_LOAD | IS_STORE), 0ULL) << entry->name;
512 }
513 size += IS_SIMM8(displacement) ? 1 : 4;
514 }
515 size += entry->skeleton.immediate_bytes;
516 return size;
517}
518
519int X86Mir2Lir::GetInsnSize(LIR* lir) {
buzbee409fe942013-10-11 10:49:56 -0700520 DCHECK(!IsPseudoLirOp(lir->opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700521 const X86EncodingMap* entry = &X86Mir2Lir::EncodingMap[lir->opcode];
522 switch (entry->kind) {
523 case kData:
524 return 4; // 4 bytes of data
525 case kNop:
526 return lir->operands[0]; // length of nop is sole operand
527 case kNullary:
528 return 1; // 1 byte of opcode
Mark Mendell4028a6c2014-02-19 20:06:20 -0800529 case kPrefix2Nullary:
530 return 3; // 1 byte of opcode + 2 prefixes
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100531 case kRegOpcode: // lir operands - 0: reg
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700532 // substract 1 for modrm
533 return ComputeSize(entry, 0, 0, lir->operands[0], NO_REG, false) - 1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700534 case kReg: // lir operands - 0: reg
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700535 return ComputeSize(entry, 0, 0, lir->operands[0], NO_REG, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700536 case kMem: // lir operands - 0: base, 1: disp
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700537 return ComputeSize(entry, lir->operands[0], lir->operands[1], NO_REG, NO_REG, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700538 case kArray: // lir operands - 0: base, 1: index, 2: scale, 3: disp
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700539 return ComputeSize(entry, lir->operands[0], lir->operands[3],
540 NO_REG, lir->operands[1], true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700541 case kMemReg: // lir operands - 0: base, 1: disp, 2: reg
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700542 return ComputeSize(entry, lir->operands[0], lir->operands[1],
543 lir->operands[2], NO_REG, false);
Mark Mendell2637f2e2014-04-30 10:10:47 -0400544 case kMemRegImm: // lir operands - 0: base, 1: disp, 2: reg 3: immediate
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700545 return ComputeSize(entry, lir->operands[0], lir->operands[1],
546 lir->operands[2], NO_REG, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700547 case kArrayReg: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700548 return ComputeSize(entry, lir->operands[0], lir->operands[3],
549 lir->operands[4], lir->operands[1], true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700550 case kThreadReg: // lir operands - 0: disp, 1: reg
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700551 return ComputeSize(entry, 0, lir->operands[0], lir->operands[1], NO_REG, false);
552 case kRegReg: // lir operands - 0: reg1, 1: reg2
553 return ComputeSize(entry, 0, 0, lir->operands[0], lir->operands[1], false);
554 case kRegRegStore: // lir operands - 0: reg2, 1: reg1
555 return ComputeSize(entry, 0, 0, lir->operands[1], lir->operands[0], false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700556 case kRegMem: // lir operands - 0: reg, 1: base, 2: disp
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700557 return ComputeSize(entry, lir->operands[1], lir->operands[2],
558 lir->operands[0], NO_REG, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700559 case kRegArray: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700560 return ComputeSize(entry, lir->operands[1], lir->operands[4],
561 lir->operands[0], lir->operands[2], true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700562 case kRegThread: // lir operands - 0: reg, 1: disp
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700563 // displacement size is always 32bit
564 return ComputeSize(entry, 0, 0x12345678, lir->operands[0], NO_REG, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700565 case kRegImm: { // lir operands - 0: reg, 1: immediate
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700566 size_t size = ComputeSize(entry, 0, 0, lir->operands[0], NO_REG, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700567 if (entry->skeleton.ax_opcode == 0) {
568 return size;
569 } else {
570 // AX opcodes don't require the modrm byte.
571 int reg = lir->operands[0];
buzbee091cc402014-03-31 10:14:40 -0700572 return size - (RegStorage::RegNum(reg) == rs_rAX.GetRegNum() ? 1 : 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700573 }
574 }
575 case kMemImm: // lir operands - 0: base, 1: disp, 2: immediate
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700576 return ComputeSize(entry, lir->operands[0], lir->operands[1],
577 NO_REG, lir->operands[0], false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700578 case kArrayImm: // lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700579 return ComputeSize(entry, lir->operands[0], lir->operands[3],
580 NO_REG, lir->operands[1], true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700581 case kThreadImm: // lir operands - 0: disp, 1: imm
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700582 // displacement size is always 32bit
583 return ComputeSize(entry, 0, 0x12345678, NO_REG, NO_REG, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700584 case kRegRegImm: // lir operands - 0: reg, 1: reg, 2: imm
Mark Mendell4708dcd2014-01-22 09:05:18 -0800585 case kRegRegImmRev:
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700586 return ComputeSize(entry, 0, 0, lir->operands[0], lir->operands[1], false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700587 case kRegMemImm: // lir operands - 0: reg, 1: base, 2: disp, 3: imm
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700588 return ComputeSize(entry, lir->operands[1], lir->operands[2],
589 lir->operands[0], NO_REG, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700590 case kRegArrayImm: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp, 5: imm
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700591 return ComputeSize(entry, lir->operands[1], lir->operands[4],
592 lir->operands[0], lir->operands[2], true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700593 case kMovRegImm: // lir operands - 0: reg, 1: immediate
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700594 return (entry->skeleton.prefix1 != 0 || NeedsRex(lir->operands[0])?1:0) +
595 1 + entry->skeleton.immediate_bytes;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700596 case kShiftRegImm: // lir operands - 0: reg, 1: immediate
597 // Shift by immediate one has a shorter opcode.
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700598 return ComputeSize(entry, 0, 0, lir->operands[0], NO_REG, false) -
599 (lir->operands[1] == 1 ? 1 : 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700600 case kShiftMemImm: // lir operands - 0: base, 1: disp, 2: immediate
601 // Shift by immediate one has a shorter opcode.
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700602 return ComputeSize(entry, lir->operands[0], lir->operands[1], NO_REG, NO_REG, false) -
Brian Carlstrom7940e442013-07-12 13:46:57 -0700603 (lir->operands[2] == 1 ? 1 : 0);
604 case kShiftArrayImm: // lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
605 // Shift by immediate one has a shorter opcode.
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700606 return ComputeSize(entry, lir->operands[0], lir->operands[3],
607 NO_REG, lir->operands[1], true) -
Brian Carlstrom7940e442013-07-12 13:46:57 -0700608 (lir->operands[4] == 1 ? 1 : 0);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700609 case kShiftRegCl: // lir operands - 0: reg, 1: cl
610 return ComputeSize(entry, 0, 0, lir->operands[0], NO_REG, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611 case kShiftMemCl: // lir operands - 0: base, 1: disp, 2: cl
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700612 return ComputeSize(entry, lir->operands[0], lir->operands[1], NO_REG, NO_REG, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700613 case kShiftArrayCl: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700614 return ComputeSize(entry, lir->operands[0], lir->operands[3],
615 lir->operands[4], lir->operands[1], true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700616 case kRegCond: // lir operands - 0: reg, 1: cond
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700617 return ComputeSize(entry, 0, 0, lir->operands[0], NO_REG, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700618 case kMemCond: // lir operands - 0: base, 1: disp, 2: cond
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700619 return ComputeSize(entry, lir->operands[0], lir->operands[1], NO_REG, NO_REG, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700620 case kArrayCond: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: cond
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700621 return ComputeSize(entry, lir->operands[0], lir->operands[3],
622 NO_REG, lir->operands[1], true);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800623 case kRegRegCond: // lir operands - 0: reg, 1: reg, 2: cond
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700624 return ComputeSize(entry, 0, 0, lir->operands[0], lir->operands[1], false);
Mark Mendell2637f2e2014-04-30 10:10:47 -0400625 case kRegMemCond: // lir operands - 0: reg, 1: reg, 2: disp, 3:cond
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700626 return ComputeSize(entry, lir->operands[1], lir->operands[2],
627 lir->operands[0], lir->operands[1], false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700628 case kJcc:
629 if (lir->opcode == kX86Jcc8) {
630 return 2; // opcode + rel8
631 } else {
632 DCHECK(lir->opcode == kX86Jcc32);
633 return 6; // 2 byte opcode + rel32
634 }
635 case kJmp:
Mark Mendell4028a6c2014-02-19 20:06:20 -0800636 if (lir->opcode == kX86Jmp8 || lir->opcode == kX86Jecxz8) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637 return 2; // opcode + rel8
638 } else if (lir->opcode == kX86Jmp32) {
639 return 5; // opcode + rel32
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700640 } else if (lir->opcode == kX86JmpT) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700641 // displacement size is always 32bit
642 return ComputeSize(entry, 0, 0x12345678, NO_REG, NO_REG, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700643 } else {
644 DCHECK(lir->opcode == kX86JmpR);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700645 if (NeedsRex(lir->operands[0])) {
646 return 3; // REX.B + opcode + modrm
647 } else {
648 return 2; // opcode + modrm
649 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650 }
651 case kCall:
652 switch (lir->opcode) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800653 case kX86CallI: return 5; // opcode 0:disp
Brian Carlstrom7940e442013-07-12 13:46:57 -0700654 case kX86CallR: return 2; // opcode modrm
655 case kX86CallM: // lir operands - 0: base, 1: disp
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700656 return ComputeSize(entry, lir->operands[0], lir->operands[1], NO_REG, NO_REG, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700657 case kX86CallA: // lir operands - 0: base, 1: index, 2: scale, 3: disp
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700658 return ComputeSize(entry, lir->operands[0], lir->operands[3],
659 NO_REG, lir->operands[1], true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700660 case kX86CallT: // lir operands - 0: disp
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700661 // displacement size is always 32bit
662 return ComputeSize(entry, 0, 0x12345678, NO_REG, NO_REG, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663 default:
664 break;
665 }
666 break;
667 case kPcRel:
668 if (entry->opcode == kX86PcRelLoadRA) {
669 // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700670 return ComputeSize(entry, lir->operands[1], 0x12345678,
671 lir->operands[0], lir->operands[2], true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700672 } else {
673 DCHECK(entry->opcode == kX86PcRelAdr);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700674 return 5; // opcode with reg + 4 byte immediate
Brian Carlstrom7940e442013-07-12 13:46:57 -0700675 }
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700676 case kMacro: // lir operands - 0: reg
Brian Carlstrom7940e442013-07-12 13:46:57 -0700677 DCHECK_EQ(lir->opcode, static_cast<int>(kX86StartOfMethod));
678 return 5 /* call opcode + 4 byte displacement */ + 1 /* pop reg */ +
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700679 ComputeSize(&X86Mir2Lir::EncodingMap[kX86Sub32RI], 0, 0,
680 lir->operands[0], NO_REG, false) -
681 // shorter ax encoding
682 (RegStorage::RegNum(lir->operands[0]) == rs_rAX.GetRegNum() ? 1 : 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700683 default:
684 break;
685 }
686 UNIMPLEMENTED(FATAL) << "Unimplemented size encoding for: " << entry->name;
687 return 0;
688}
689
Vladimir Marko057c74a2013-12-03 15:20:45 +0000690void X86Mir2Lir::EmitPrefix(const X86EncodingMap* entry) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700691 EmitPrefix(entry, NO_REG, NO_REG, NO_REG);
692}
693
694void X86Mir2Lir::EmitPrefix(const X86EncodingMap* entry,
695 uint8_t reg_r, uint8_t reg_x, uint8_t reg_b) {
696 // REX.WRXB
697 // W - 64-bit operand
698 // R - MODRM.reg
699 // X - SIB.index
700 // B - MODRM.rm/SIB.base
701 bool force = false;
702 bool w = (entry->skeleton.prefix1 == REX_W) || (entry->skeleton.prefix2 == REX_W);
703 bool r = NeedsRex(reg_r);
704 bool x = NeedsRex(reg_x);
705 bool b = NeedsRex(reg_b);
706 uint8_t rex = force ? 0x40 : 0;
707 if (w) {
708 rex |= 0x48; // REX.W000
709 }
710 if (r) {
711 rex |= 0x44; // REX.0R00
712 }
713 if (x) {
714 rex |= 0x42; // REX.00X0
715 }
716 if (b) {
717 rex |= 0x41; // REX.000B
718 }
Vladimir Marko057c74a2013-12-03 15:20:45 +0000719 if (entry->skeleton.prefix1 != 0) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700720 if (Gen64Bit() && entry->skeleton.prefix1 == THREAD_PREFIX) {
721 // 64 bit adresses by GS, not FS
722 code_buffer_.push_back(THREAD_PREFIX_GS);
723 } else {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700724 if (entry->skeleton.prefix1 == REX_W) {
725 rex |= entry->skeleton.prefix1;
726 code_buffer_.push_back(rex);
727 rex = 0;
728 } else {
729 code_buffer_.push_back(entry->skeleton.prefix1);
730 }
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700731 }
Vladimir Marko057c74a2013-12-03 15:20:45 +0000732 if (entry->skeleton.prefix2 != 0) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700733 if (entry->skeleton.prefix2 == REX_W) {
734 rex |= entry->skeleton.prefix2;
735 code_buffer_.push_back(rex);
736 rex = 0;
737 } else {
738 code_buffer_.push_back(entry->skeleton.prefix2);
739 }
Vladimir Marko057c74a2013-12-03 15:20:45 +0000740 }
741 } else {
742 DCHECK_EQ(0, entry->skeleton.prefix2);
743 }
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700744 if (rex != 0) {
745 code_buffer_.push_back(rex);
746 }
Vladimir Marko057c74a2013-12-03 15:20:45 +0000747}
748
749void X86Mir2Lir::EmitOpcode(const X86EncodingMap* entry) {
750 code_buffer_.push_back(entry->skeleton.opcode);
751 if (entry->skeleton.opcode == 0x0F) {
752 code_buffer_.push_back(entry->skeleton.extra_opcode1);
753 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
754 code_buffer_.push_back(entry->skeleton.extra_opcode2);
755 } else {
756 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
757 }
758 } else {
759 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
760 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
761 }
762}
763
764void X86Mir2Lir::EmitPrefixAndOpcode(const X86EncodingMap* entry) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700765 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, NO_REG);
766}
767
768void X86Mir2Lir::EmitPrefixAndOpcode(const X86EncodingMap* entry,
769 uint8_t reg_r, uint8_t reg_x, uint8_t reg_b) {
770 EmitPrefix(entry, reg_r, reg_x, reg_b);
Vladimir Marko057c74a2013-12-03 15:20:45 +0000771 EmitOpcode(entry);
772}
773
Brian Carlstrom7940e442013-07-12 13:46:57 -0700774static uint8_t ModrmForDisp(int base, int disp) {
775 // BP requires an explicit disp, so do not omit it in the 0 case
buzbee091cc402014-03-31 10:14:40 -0700776 if (disp == 0 && RegStorage::RegNum(base) != rs_rBP.GetRegNum()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700777 return 0;
778 } else if (IS_SIMM8(disp)) {
779 return 1;
780 } else {
781 return 2;
782 }
783}
784
Vladimir Marko057c74a2013-12-03 15:20:45 +0000785void X86Mir2Lir::EmitDisp(uint8_t base, int disp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700786 // BP requires an explicit disp, so do not omit it in the 0 case
buzbee091cc402014-03-31 10:14:40 -0700787 if (disp == 0 && RegStorage::RegNum(base) != rs_rBP.GetRegNum()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700788 return;
789 } else if (IS_SIMM8(disp)) {
790 code_buffer_.push_back(disp & 0xFF);
791 } else {
792 code_buffer_.push_back(disp & 0xFF);
793 code_buffer_.push_back((disp >> 8) & 0xFF);
794 code_buffer_.push_back((disp >> 16) & 0xFF);
795 code_buffer_.push_back((disp >> 24) & 0xFF);
796 }
797}
798
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700799void X86Mir2Lir::EmitModrmThread(uint8_t reg_or_opcode) {
800 if (Gen64Bit()) {
801 // Absolute adressing for GS access.
802 uint8_t modrm = (0 << 6) | (reg_or_opcode << 3) | rs_rX86_SP.GetRegNum();
803 code_buffer_.push_back(modrm);
804 uint8_t sib = (0/*TIMES_1*/ << 6) | (rs_rX86_SP.GetRegNum() << 3) | rs_rBP.GetRegNum();
805 code_buffer_.push_back(sib);
806 } else {
807 uint8_t modrm = (0 << 6) | (reg_or_opcode << 3) | rs_rBP.GetRegNum();
808 code_buffer_.push_back(modrm);
809 }
810}
811
Vladimir Marko057c74a2013-12-03 15:20:45 +0000812void X86Mir2Lir::EmitModrmDisp(uint8_t reg_or_opcode, uint8_t base, int disp) {
buzbee091cc402014-03-31 10:14:40 -0700813 DCHECK_LT(RegStorage::RegNum(reg_or_opcode), 8);
814 DCHECK_LT(RegStorage::RegNum(base), 8);
815 uint8_t modrm = (ModrmForDisp(base, disp) << 6) | (RegStorage::RegNum(reg_or_opcode) << 3) |
816 RegStorage::RegNum(base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700817 code_buffer_.push_back(modrm);
buzbee091cc402014-03-31 10:14:40 -0700818 if (RegStorage::RegNum(base) == rs_rX86_SP.GetRegNum()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700819 // Special SIB for SP base
buzbee091cc402014-03-31 10:14:40 -0700820 code_buffer_.push_back(0 << 6 | rs_rX86_SP.GetRegNum() << 3 | rs_rX86_SP.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700821 }
822 EmitDisp(base, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700823}
824
Vladimir Marko057c74a2013-12-03 15:20:45 +0000825void X86Mir2Lir::EmitModrmSibDisp(uint8_t reg_or_opcode, uint8_t base, uint8_t index,
826 int scale, int disp) {
buzbee091cc402014-03-31 10:14:40 -0700827 DCHECK_LT(RegStorage::RegNum(reg_or_opcode), 8);
828 uint8_t modrm = (ModrmForDisp(base, disp) << 6) | RegStorage::RegNum(reg_or_opcode) << 3 |
829 rs_rX86_SP.GetRegNum();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700830 code_buffer_.push_back(modrm);
831 DCHECK_LT(scale, 4);
buzbee091cc402014-03-31 10:14:40 -0700832 DCHECK_LT(RegStorage::RegNum(index), 8);
833 DCHECK_LT(RegStorage::RegNum(base), 8);
834 uint8_t sib = (scale << 6) | (RegStorage::RegNum(index) << 3) | RegStorage::RegNum(base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700835 code_buffer_.push_back(sib);
836 EmitDisp(base, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700837}
838
Dmitry Petrochenko96992e82014-05-20 04:03:46 +0700839void X86Mir2Lir::EmitImm(const X86EncodingMap* entry, int64_t imm) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700840 switch (entry->skeleton.immediate_bytes) {
841 case 1:
842 DCHECK(IS_SIMM8(imm));
843 code_buffer_.push_back(imm & 0xFF);
844 break;
845 case 2:
846 DCHECK(IS_SIMM16(imm));
847 code_buffer_.push_back(imm & 0xFF);
848 code_buffer_.push_back((imm >> 8) & 0xFF);
849 break;
850 case 4:
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700851 if (imm <0) {
852 CHECK_EQ((-imm) & 0x0FFFFFFFFl, -imm);
853 } else {
854 CHECK_EQ(imm & 0x0FFFFFFFFl, imm);
855 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700856 code_buffer_.push_back(imm & 0xFF);
857 code_buffer_.push_back((imm >> 8) & 0xFF);
858 code_buffer_.push_back((imm >> 16) & 0xFF);
859 code_buffer_.push_back((imm >> 24) & 0xFF);
860 break;
Dmitry Petrochenko96992e82014-05-20 04:03:46 +0700861 case 8:
862 code_buffer_.push_back(imm & 0xFF);
863 code_buffer_.push_back((imm >> 8) & 0xFF);
864 code_buffer_.push_back((imm >> 16) & 0xFF);
865 code_buffer_.push_back((imm >> 24) & 0xFF);
866 code_buffer_.push_back((imm >> 32) & 0xFF);
867 code_buffer_.push_back((imm >> 40) & 0xFF);
868 code_buffer_.push_back((imm >> 48) & 0xFF);
869 code_buffer_.push_back((imm >> 56) & 0xFF);
870 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700871 default:
872 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
873 << ") for instruction: " << entry->name;
874 break;
875 }
876}
877
Vladimir Marko057c74a2013-12-03 15:20:45 +0000878void X86Mir2Lir::EmitOpRegOpcode(const X86EncodingMap* entry, uint8_t reg) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700879 EmitPrefixAndOpcode(entry, reg, NO_REG, NO_REG);
880 reg = LowRegisterBits(reg);
Vladimir Marko057c74a2013-12-03 15:20:45 +0000881 // There's no 3-byte instruction with +rd
882 DCHECK(entry->skeleton.opcode != 0x0F ||
883 (entry->skeleton.extra_opcode1 != 0x38 && entry->skeleton.extra_opcode1 != 0x3A));
buzbee091cc402014-03-31 10:14:40 -0700884 DCHECK(!RegStorage::IsFloat(reg));
885 DCHECK_LT(RegStorage::RegNum(reg), 8);
886 code_buffer_.back() += RegStorage::RegNum(reg);
Vladimir Marko057c74a2013-12-03 15:20:45 +0000887 DCHECK_EQ(0, entry->skeleton.ax_opcode);
888 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
889}
890
891void X86Mir2Lir::EmitOpReg(const X86EncodingMap* entry, uint8_t reg) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700892 EmitPrefixAndOpcode(entry, reg, NO_REG, NO_REG);
893 reg = LowRegisterBits(reg);
buzbee091cc402014-03-31 10:14:40 -0700894 if (RegStorage::RegNum(reg) >= 4) {
895 DCHECK(strchr(entry->name, '8') == NULL) << entry->name << " "
896 << static_cast<int>(RegStorage::RegNum(reg))
Vladimir Marko057c74a2013-12-03 15:20:45 +0000897 << " in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
898 }
buzbee091cc402014-03-31 10:14:40 -0700899 DCHECK_LT(RegStorage::RegNum(reg), 8);
900 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | RegStorage::RegNum(reg);
Vladimir Marko057c74a2013-12-03 15:20:45 +0000901 code_buffer_.push_back(modrm);
902 DCHECK_EQ(0, entry->skeleton.ax_opcode);
903 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
904}
905
906void X86Mir2Lir::EmitOpMem(const X86EncodingMap* entry, uint8_t base, int disp) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700907 EmitPrefix(entry, NO_REG, NO_REG, base);
908 base = LowRegisterBits(base);
Vladimir Marko057c74a2013-12-03 15:20:45 +0000909 code_buffer_.push_back(entry->skeleton.opcode);
910 DCHECK_NE(0x0F, entry->skeleton.opcode);
911 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
912 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Vladimir Marko057c74a2013-12-03 15:20:45 +0000913 EmitModrmDisp(entry->skeleton.modrm_opcode, base, disp);
914 DCHECK_EQ(0, entry->skeleton.ax_opcode);
915 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
916}
917
918void X86Mir2Lir::EmitOpArray(const X86EncodingMap* entry, uint8_t base, uint8_t index,
919 int scale, int disp) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700920 EmitPrefixAndOpcode(entry, NO_REG, index, base);
921 index = LowRegisterBits(index);
922 base = LowRegisterBits(base);
Vladimir Marko057c74a2013-12-03 15:20:45 +0000923 EmitModrmSibDisp(entry->skeleton.modrm_opcode, base, index, scale, disp);
924 DCHECK_EQ(0, entry->skeleton.ax_opcode);
925 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
926}
927
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700928uint8_t X86Mir2Lir::LowRegisterBits(uint8_t reg) {
929 uint8_t res = reg;
930 res = reg & kRegNumMask32; // 3 bits
931 return res;
932}
933
934bool X86Mir2Lir::NeedsRex(uint8_t reg) {
935 return RegStorage::RegNum(reg) > 7;
936}
937
Vladimir Marko057c74a2013-12-03 15:20:45 +0000938void X86Mir2Lir::EmitMemReg(const X86EncodingMap* entry,
939 uint8_t base, int disp, uint8_t reg) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700940 EmitPrefixAndOpcode(entry, reg, NO_REG, base);
941 reg = LowRegisterBits(reg);
942 base = LowRegisterBits(base);
buzbee091cc402014-03-31 10:14:40 -0700943 if (RegStorage::RegNum(reg) >= 4) {
Vladimir Marko057c74a2013-12-03 15:20:45 +0000944 DCHECK(strchr(entry->name, '8') == NULL ||
945 entry->opcode == kX86Movzx8RM || entry->opcode == kX86Movsx8RM)
buzbee091cc402014-03-31 10:14:40 -0700946 << entry->name << " " << static_cast<int>(RegStorage::RegNum(reg))
Vladimir Marko057c74a2013-12-03 15:20:45 +0000947 << " in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
948 }
949 EmitModrmDisp(reg, base, disp);
950 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
951 DCHECK_EQ(0, entry->skeleton.ax_opcode);
952 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
953}
954
955void X86Mir2Lir::EmitRegMem(const X86EncodingMap* entry,
956 uint8_t reg, uint8_t base, int disp) {
957 // Opcode will flip operands.
958 EmitMemReg(entry, base, disp, reg);
959}
960
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700961void X86Mir2Lir::EmitRegArray(const X86EncodingMap* entry, uint8_t reg, uint8_t base,
962 uint8_t index, int scale, int disp) {
963 EmitPrefixAndOpcode(entry, reg, index, base);
964 reg = LowRegisterBits(reg);
965 index = LowRegisterBits(index);
966 base = LowRegisterBits(base);
Vladimir Marko057c74a2013-12-03 15:20:45 +0000967 EmitModrmSibDisp(reg, base, index, scale, disp);
968 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
969 DCHECK_EQ(0, entry->skeleton.ax_opcode);
970 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
971}
972
buzbee091cc402014-03-31 10:14:40 -0700973void X86Mir2Lir::EmitArrayReg(const X86EncodingMap* entry, uint8_t base, uint8_t index, int scale,
974 int disp, uint8_t reg) {
Vladimir Marko057c74a2013-12-03 15:20:45 +0000975 // Opcode will flip operands.
976 EmitRegArray(entry, reg, base, index, scale, disp);
977}
978
Mark Mendell2637f2e2014-04-30 10:10:47 -0400979void X86Mir2Lir::EmitArrayImm(const X86EncodingMap* entry, uint8_t base, uint8_t index, int scale,
980 int disp, int32_t imm) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700981 EmitPrefixAndOpcode(entry, NO_REG, index, base);
982 index = LowRegisterBits(index);
983 base = LowRegisterBits(base);
Mark Mendell2637f2e2014-04-30 10:10:47 -0400984 EmitModrmSibDisp(entry->skeleton.modrm_opcode, base, index, scale, disp);
985 DCHECK_EQ(0, entry->skeleton.ax_opcode);
Mark Mendell9ed42772014-05-07 17:26:12 -0400986 EmitImm(entry, imm);
Mark Mendell2637f2e2014-04-30 10:10:47 -0400987}
988
Vladimir Marko057c74a2013-12-03 15:20:45 +0000989void X86Mir2Lir::EmitRegThread(const X86EncodingMap* entry, uint8_t reg, int disp) {
990 DCHECK_NE(entry->skeleton.prefix1, 0);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700991 EmitPrefixAndOpcode(entry, reg, NO_REG, NO_REG);
992 reg = LowRegisterBits(reg);
buzbee091cc402014-03-31 10:14:40 -0700993 if (RegStorage::RegNum(reg) >= 4) {
994 DCHECK(strchr(entry->name, '8') == NULL) << entry->name << " "
995 << static_cast<int>(RegStorage::RegNum(reg))
Vladimir Marko057c74a2013-12-03 15:20:45 +0000996 << " in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
997 }
buzbee091cc402014-03-31 10:14:40 -0700998 DCHECK_LT(RegStorage::RegNum(reg), 8);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700999 EmitModrmThread(RegStorage::RegNum(reg));
Vladimir Marko057c74a2013-12-03 15:20:45 +00001000 code_buffer_.push_back(disp & 0xFF);
1001 code_buffer_.push_back((disp >> 8) & 0xFF);
1002 code_buffer_.push_back((disp >> 16) & 0xFF);
1003 code_buffer_.push_back((disp >> 24) & 0xFF);
1004 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1005 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1006 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1007}
1008
1009void X86Mir2Lir::EmitRegReg(const X86EncodingMap* entry, uint8_t reg1, uint8_t reg2) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001010 EmitPrefixAndOpcode(entry, reg1, NO_REG, reg2);
1011 reg1 = LowRegisterBits(reg1);
1012 reg2 = LowRegisterBits(reg2);
buzbee091cc402014-03-31 10:14:40 -07001013 DCHECK_LT(RegStorage::RegNum(reg1), 8);
1014 DCHECK_LT(RegStorage::RegNum(reg2), 8);
1015 uint8_t modrm = (3 << 6) | (RegStorage::RegNum(reg1) << 3) | RegStorage::RegNum(reg2);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001016 code_buffer_.push_back(modrm);
1017 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1018 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1019 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1020}
1021
1022void X86Mir2Lir::EmitRegRegImm(const X86EncodingMap* entry,
1023 uint8_t reg1, uint8_t reg2, int32_t imm) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001024 EmitPrefixAndOpcode(entry, reg1, NO_REG, reg2);
1025 reg1 = LowRegisterBits(reg1);
1026 reg2 = LowRegisterBits(reg2);
buzbee091cc402014-03-31 10:14:40 -07001027 DCHECK_LT(RegStorage::RegNum(reg1), 8);
1028 DCHECK_LT(RegStorage::RegNum(reg2), 8);
1029 uint8_t modrm = (3 << 6) | (RegStorage::RegNum(reg1) << 3) | RegStorage::RegNum(reg2);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001030 code_buffer_.push_back(modrm);
1031 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1032 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1033 EmitImm(entry, imm);
1034}
1035
Mark Mendell4708dcd2014-01-22 09:05:18 -08001036void X86Mir2Lir::EmitRegRegImmRev(const X86EncodingMap* entry,
1037 uint8_t reg1, uint8_t reg2, int32_t imm) {
1038 EmitRegRegImm(entry, reg2, reg1, imm);
1039}
1040
1041void X86Mir2Lir::EmitRegMemImm(const X86EncodingMap* entry,
1042 uint8_t reg, uint8_t base, int disp, int32_t imm) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001043 EmitPrefixAndOpcode(entry, reg, NO_REG, base);
1044 reg = LowRegisterBits(reg);
1045 base = LowRegisterBits(base);
buzbee091cc402014-03-31 10:14:40 -07001046 DCHECK(!RegStorage::IsFloat(reg));
1047 DCHECK_LT(RegStorage::RegNum(reg), 8);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001048 EmitModrmDisp(reg, base, disp);
1049 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1050 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1051 EmitImm(entry, imm);
1052}
1053
Mark Mendell2637f2e2014-04-30 10:10:47 -04001054void X86Mir2Lir::EmitMemRegImm(const X86EncodingMap* entry,
1055 uint8_t base, int disp, uint8_t reg, int32_t imm) {
1056 EmitRegMemImm(entry, reg, base, disp, imm);
1057}
1058
Brian Carlstrom7940e442013-07-12 13:46:57 -07001059void X86Mir2Lir::EmitRegImm(const X86EncodingMap* entry, uint8_t reg, int imm) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001060 EmitPrefix(entry, NO_REG, NO_REG, reg);
buzbee091cc402014-03-31 10:14:40 -07001061 if (RegStorage::RegNum(reg) == rs_rAX.GetRegNum() && entry->skeleton.ax_opcode != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001062 code_buffer_.push_back(entry->skeleton.ax_opcode);
1063 } else {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001064 reg = LowRegisterBits(reg);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001065 EmitOpcode(entry);
buzbee091cc402014-03-31 10:14:40 -07001066 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | RegStorage::RegNum(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001067 code_buffer_.push_back(modrm);
1068 }
Vladimir Marko057c74a2013-12-03 15:20:45 +00001069 EmitImm(entry, imm);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001070}
1071
Mark Mendell343adb52013-12-18 06:02:17 -08001072void X86Mir2Lir::EmitMemImm(const X86EncodingMap* entry, uint8_t base, int disp, int32_t imm) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001073 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, base);
1074 base = LowRegisterBits(base);
Mark Mendell343adb52013-12-18 06:02:17 -08001075 EmitModrmDisp(entry->skeleton.modrm_opcode, base, disp);
1076 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1077 EmitImm(entry, imm);
1078}
1079
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080void X86Mir2Lir::EmitThreadImm(const X86EncodingMap* entry, int disp, int imm) {
Vladimir Marko057c74a2013-12-03 15:20:45 +00001081 EmitPrefixAndOpcode(entry);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001082 EmitModrmThread(entry->skeleton.modrm_opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001083 code_buffer_.push_back(disp & 0xFF);
1084 code_buffer_.push_back((disp >> 8) & 0xFF);
1085 code_buffer_.push_back((disp >> 16) & 0xFF);
1086 code_buffer_.push_back((disp >> 24) & 0xFF);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001087 EmitImm(entry, imm);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001088 DCHECK_EQ(entry->skeleton.ax_opcode, 0);
1089}
1090
Dmitry Petrochenko96992e82014-05-20 04:03:46 +07001091void X86Mir2Lir::EmitMovRegImm(const X86EncodingMap* entry, uint8_t reg, int64_t imm) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001092 EmitPrefix(entry, NO_REG, NO_REG, reg);
1093 reg = LowRegisterBits(reg);
buzbee091cc402014-03-31 10:14:40 -07001094 DCHECK_LT(RegStorage::RegNum(reg), 8);
1095 code_buffer_.push_back(0xB8 + RegStorage::RegNum(reg));
Dmitry Petrochenko96992e82014-05-20 04:03:46 +07001096 switch (entry->skeleton.immediate_bytes) {
1097 case 4:
1098 code_buffer_.push_back(imm & 0xFF);
1099 code_buffer_.push_back((imm >> 8) & 0xFF);
1100 code_buffer_.push_back((imm >> 16) & 0xFF);
1101 code_buffer_.push_back((imm >> 24) & 0xFF);
1102 break;
1103 case 8:
1104 code_buffer_.push_back(imm & 0xFF);
1105 code_buffer_.push_back((imm >> 8) & 0xFF);
1106 code_buffer_.push_back((imm >> 16) & 0xFF);
1107 code_buffer_.push_back((imm >> 24) & 0xFF);
1108 code_buffer_.push_back((imm >> 32) & 0xFF);
1109 code_buffer_.push_back((imm >> 40) & 0xFF);
1110 code_buffer_.push_back((imm >> 48) & 0xFF);
1111 code_buffer_.push_back((imm >> 56) & 0xFF);
1112 break;
1113 default:
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001114 LOG(FATAL) << "Unsupported immediate size for EmitMovRegImm: "
1115 << static_cast<uint32_t>(entry->skeleton.immediate_bytes);
Dmitry Petrochenko96992e82014-05-20 04:03:46 +07001116 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001117}
1118
1119void X86Mir2Lir::EmitShiftRegImm(const X86EncodingMap* entry, uint8_t reg, int imm) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001120 EmitPrefix(entry, NO_REG, NO_REG, reg);
1121 reg = LowRegisterBits(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122 if (imm != 1) {
1123 code_buffer_.push_back(entry->skeleton.opcode);
1124 } else {
1125 // Shorter encoding for 1 bit shift
1126 code_buffer_.push_back(entry->skeleton.ax_opcode);
1127 }
Vladimir Marko057c74a2013-12-03 15:20:45 +00001128 DCHECK_NE(0x0F, entry->skeleton.opcode);
1129 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1130 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
buzbee091cc402014-03-31 10:14:40 -07001131 if (RegStorage::RegNum(reg) >= 4) {
1132 DCHECK(strchr(entry->name, '8') == NULL) << entry->name << " "
1133 << static_cast<int>(RegStorage::RegNum(reg))
Brian Carlstrom7940e442013-07-12 13:46:57 -07001134 << " in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
1135 }
buzbee091cc402014-03-31 10:14:40 -07001136 DCHECK_LT(RegStorage::RegNum(reg), 8);
1137 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | RegStorage::RegNum(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001138 code_buffer_.push_back(modrm);
1139 if (imm != 1) {
1140 DCHECK_EQ(entry->skeleton.immediate_bytes, 1);
1141 DCHECK(IS_SIMM8(imm));
1142 code_buffer_.push_back(imm & 0xFF);
1143 }
1144}
1145
1146void X86Mir2Lir::EmitShiftRegCl(const X86EncodingMap* entry, uint8_t reg, uint8_t cl) {
buzbee091cc402014-03-31 10:14:40 -07001147 DCHECK_EQ(cl, static_cast<uint8_t>(rs_rCX.GetReg()));
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001148 EmitPrefix(entry, reg, NO_REG, NO_REG);
1149 reg = LowRegisterBits(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001150 code_buffer_.push_back(entry->skeleton.opcode);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001151 DCHECK_NE(0x0F, entry->skeleton.opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001152 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1153 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
buzbee091cc402014-03-31 10:14:40 -07001154 DCHECK_LT(RegStorage::RegNum(reg), 8);
1155 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | RegStorage::RegNum(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001156 code_buffer_.push_back(modrm);
1157 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1158 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1159}
1160
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001161void X86Mir2Lir::EmitShiftMemCl(const X86EncodingMap* entry, uint8_t base,
1162 int displacement, uint8_t cl) {
buzbee091cc402014-03-31 10:14:40 -07001163 DCHECK_EQ(cl, static_cast<uint8_t>(rs_rCX.GetReg()));
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001164 EmitPrefix(entry, NO_REG, NO_REG, base);
1165 base = LowRegisterBits(base);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001166 code_buffer_.push_back(entry->skeleton.opcode);
1167 DCHECK_NE(0x0F, entry->skeleton.opcode);
1168 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1169 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
buzbee091cc402014-03-31 10:14:40 -07001170 DCHECK_LT(RegStorage::RegNum(base), 8);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001171 EmitModrmDisp(entry->skeleton.modrm_opcode, base, displacement);
1172 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1173 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1174}
1175
Mark Mendell2637f2e2014-04-30 10:10:47 -04001176void X86Mir2Lir::EmitShiftMemImm(const X86EncodingMap* entry, uint8_t base,
1177 int displacement, int imm) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001178 EmitPrefix(entry, NO_REG, NO_REG, base);
1179 base = LowRegisterBits(base);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001180 if (imm != 1) {
1181 code_buffer_.push_back(entry->skeleton.opcode);
1182 } else {
1183 // Shorter encoding for 1 bit shift
1184 code_buffer_.push_back(entry->skeleton.ax_opcode);
1185 }
1186 DCHECK_NE(0x0F, entry->skeleton.opcode);
1187 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1188 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1189 EmitModrmDisp(entry->skeleton.modrm_opcode, base, displacement);
1190 if (imm != 1) {
1191 DCHECK_EQ(entry->skeleton.immediate_bytes, 1);
1192 DCHECK(IS_SIMM8(imm));
1193 code_buffer_.push_back(imm & 0xFF);
1194 }
1195}
1196
Brian Carlstrom7940e442013-07-12 13:46:57 -07001197void X86Mir2Lir::EmitRegCond(const X86EncodingMap* entry, uint8_t reg, uint8_t condition) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001198 EmitPrefix(entry, reg, NO_REG, NO_REG);
1199 reg = LowRegisterBits(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001200 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1201 DCHECK_EQ(0x0F, entry->skeleton.opcode);
1202 code_buffer_.push_back(0x0F);
1203 DCHECK_EQ(0x90, entry->skeleton.extra_opcode1);
1204 code_buffer_.push_back(0x90 | condition);
1205 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
buzbee091cc402014-03-31 10:14:40 -07001206 DCHECK_LT(RegStorage::RegNum(reg), 8);
1207 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | RegStorage::RegNum(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001208 code_buffer_.push_back(modrm);
1209 DCHECK_EQ(entry->skeleton.immediate_bytes, 0);
1210}
1211
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001212void X86Mir2Lir::EmitMemCond(const X86EncodingMap* entry, uint8_t base, int displacement,
1213 uint8_t condition) {
Mark Mendell2637f2e2014-04-30 10:10:47 -04001214 if (entry->skeleton.prefix1 != 0) {
1215 code_buffer_.push_back(entry->skeleton.prefix1);
1216 if (entry->skeleton.prefix2 != 0) {
1217 code_buffer_.push_back(entry->skeleton.prefix2);
1218 }
1219 } else {
1220 DCHECK_EQ(0, entry->skeleton.prefix2);
1221 }
1222 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1223 DCHECK_EQ(0x0F, entry->skeleton.opcode);
1224 code_buffer_.push_back(0x0F);
1225 DCHECK_EQ(0x90, entry->skeleton.extra_opcode1);
1226 code_buffer_.push_back(0x90 | condition);
1227 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1228 EmitModrmDisp(entry->skeleton.modrm_opcode, base, displacement);
1229 DCHECK_EQ(entry->skeleton.immediate_bytes, 0);
1230}
1231
buzbee091cc402014-03-31 10:14:40 -07001232void X86Mir2Lir::EmitRegRegCond(const X86EncodingMap* entry, uint8_t reg1, uint8_t reg2,
1233 uint8_t condition) {
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001234 // Generate prefix and opcode without the condition
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001235 EmitPrefixAndOpcode(entry, reg1, NO_REG, reg2);
1236 reg1 = LowRegisterBits(reg1);
1237 reg2 = LowRegisterBits(reg2);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001238
1239 // Now add the condition. The last byte of opcode is the one that receives it.
1240 DCHECK_LE(condition, 0xF);
1241 code_buffer_.back() += condition;
1242
1243 // Not expecting to have to encode immediate or do anything special for ModR/M since there are two registers.
1244 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1245 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1246
1247 // Check that registers requested for encoding are sane.
buzbee091cc402014-03-31 10:14:40 -07001248 DCHECK_LT(RegStorage::RegNum(reg1), 8);
1249 DCHECK_LT(RegStorage::RegNum(reg2), 8);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001250
1251 // For register to register encoding, the mod is 3.
1252 const uint8_t mod = (3 << 6);
1253
1254 // Encode the ModR/M byte now.
buzbee091cc402014-03-31 10:14:40 -07001255 const uint8_t modrm = mod | (RegStorage::RegNum(reg1) << 3) | RegStorage::RegNum(reg2);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001256 code_buffer_.push_back(modrm);
1257}
1258
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001259void X86Mir2Lir::EmitRegMemCond(const X86EncodingMap* entry, uint8_t reg1, uint8_t base,
1260 int displacement, uint8_t condition) {
Mark Mendell2637f2e2014-04-30 10:10:47 -04001261 // Generate prefix and opcode without the condition
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001262 EmitPrefixAndOpcode(entry, reg1, NO_REG, base);
1263 reg1 = LowRegisterBits(reg1);
1264 base = LowRegisterBits(base);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001265
1266 // Now add the condition. The last byte of opcode is the one that receives it.
1267 DCHECK_LE(condition, 0xF);
1268 code_buffer_.back() += condition;
1269
1270 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1271 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1272
1273 // Check that registers requested for encoding are sane.
1274 DCHECK_LT(reg1, 8);
1275 DCHECK_LT(base, 8);
1276
1277 EmitModrmDisp(reg1, base, displacement);
1278}
1279
Brian Carlstrom7940e442013-07-12 13:46:57 -07001280void X86Mir2Lir::EmitJmp(const X86EncodingMap* entry, int rel) {
1281 if (entry->opcode == kX86Jmp8) {
1282 DCHECK(IS_SIMM8(rel));
1283 code_buffer_.push_back(0xEB);
1284 code_buffer_.push_back(rel & 0xFF);
1285 } else if (entry->opcode == kX86Jmp32) {
1286 code_buffer_.push_back(0xE9);
1287 code_buffer_.push_back(rel & 0xFF);
1288 code_buffer_.push_back((rel >> 8) & 0xFF);
1289 code_buffer_.push_back((rel >> 16) & 0xFF);
1290 code_buffer_.push_back((rel >> 24) & 0xFF);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001291 } else if (entry->opcode == kX86Jecxz8) {
1292 DCHECK(IS_SIMM8(rel));
1293 code_buffer_.push_back(0xE3);
1294 code_buffer_.push_back(rel & 0xFF);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001295 } else {
1296 DCHECK(entry->opcode == kX86JmpR);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001297 uint8_t reg = static_cast<uint8_t>(rel);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001298 EmitPrefix(entry, NO_REG, NO_REG, reg);
1299 code_buffer_.push_back(entry->skeleton.opcode);
1300 reg = LowRegisterBits(reg);
buzbee091cc402014-03-31 10:14:40 -07001301 DCHECK_LT(RegStorage::RegNum(reg), 8);
1302 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | RegStorage::RegNum(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001303 code_buffer_.push_back(modrm);
1304 }
1305}
1306
1307void X86Mir2Lir::EmitJcc(const X86EncodingMap* entry, int rel, uint8_t cc) {
1308 DCHECK_LT(cc, 16);
1309 if (entry->opcode == kX86Jcc8) {
1310 DCHECK(IS_SIMM8(rel));
1311 code_buffer_.push_back(0x70 | cc);
1312 code_buffer_.push_back(rel & 0xFF);
1313 } else {
1314 DCHECK(entry->opcode == kX86Jcc32);
1315 code_buffer_.push_back(0x0F);
1316 code_buffer_.push_back(0x80 | cc);
1317 code_buffer_.push_back(rel & 0xFF);
1318 code_buffer_.push_back((rel >> 8) & 0xFF);
1319 code_buffer_.push_back((rel >> 16) & 0xFF);
1320 code_buffer_.push_back((rel >> 24) & 0xFF);
1321 }
1322}
1323
1324void X86Mir2Lir::EmitCallMem(const X86EncodingMap* entry, uint8_t base, int disp) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001325 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, base);
1326 base = LowRegisterBits(base);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001327 EmitModrmDisp(entry->skeleton.modrm_opcode, base, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001328 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1329 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1330}
1331
Mark Mendell55d0eac2014-02-06 11:02:52 -08001332void X86Mir2Lir::EmitCallImmediate(const X86EncodingMap* entry, int disp) {
1333 EmitPrefixAndOpcode(entry);
1334 DCHECK_EQ(4, entry->skeleton.immediate_bytes);
1335 code_buffer_.push_back(disp & 0xFF);
1336 code_buffer_.push_back((disp >> 8) & 0xFF);
1337 code_buffer_.push_back((disp >> 16) & 0xFF);
1338 code_buffer_.push_back((disp >> 24) & 0xFF);
1339 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1340}
1341
Brian Carlstrom7940e442013-07-12 13:46:57 -07001342void X86Mir2Lir::EmitCallThread(const X86EncodingMap* entry, int disp) {
1343 DCHECK_NE(entry->skeleton.prefix1, 0);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001344 EmitPrefixAndOpcode(entry);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001345 EmitModrmThread(entry->skeleton.modrm_opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001346 code_buffer_.push_back(disp & 0xFF);
1347 code_buffer_.push_back((disp >> 8) & 0xFF);
1348 code_buffer_.push_back((disp >> 16) & 0xFF);
1349 code_buffer_.push_back((disp >> 24) & 0xFF);
1350 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1351 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1352}
1353
1354void X86Mir2Lir::EmitPcRel(const X86EncodingMap* entry, uint8_t reg,
1355 int base_or_table, uint8_t index, int scale, int table_or_disp) {
1356 int disp;
1357 if (entry->opcode == kX86PcRelLoadRA) {
buzbee0d829482013-10-11 15:24:55 -07001358 Mir2Lir::EmbeddedData *tab_rec =
1359 reinterpret_cast<Mir2Lir::EmbeddedData*>(UnwrapPointer(table_or_disp));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001360 disp = tab_rec->offset;
1361 } else {
1362 DCHECK(entry->opcode == kX86PcRelAdr);
buzbee0d829482013-10-11 15:24:55 -07001363 Mir2Lir::EmbeddedData *tab_rec =
1364 reinterpret_cast<Mir2Lir::EmbeddedData*>(UnwrapPointer(base_or_table));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001365 disp = tab_rec->offset;
1366 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001367 if (entry->opcode == kX86PcRelLoadRA) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001368 EmitPrefix(entry, reg, index, base_or_table);
1369 reg = LowRegisterBits(reg);
1370 base_or_table = LowRegisterBits(base_or_table);
1371 index = LowRegisterBits(index);
1372 DCHECK_LT(RegStorage::RegNum(reg), 8);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001373 code_buffer_.push_back(entry->skeleton.opcode);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001374 DCHECK_NE(0x0F, entry->skeleton.opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001375 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1376 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
buzbee091cc402014-03-31 10:14:40 -07001377 uint8_t modrm = (2 << 6) | (RegStorage::RegNum(reg) << 3) | rs_rX86_SP.GetRegNum();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001378 code_buffer_.push_back(modrm);
1379 DCHECK_LT(scale, 4);
buzbee091cc402014-03-31 10:14:40 -07001380 DCHECK_LT(RegStorage::RegNum(index), 8);
1381 DCHECK_LT(RegStorage::RegNum(base_or_table), 8);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001382 uint8_t base = static_cast<uint8_t>(base_or_table);
buzbee091cc402014-03-31 10:14:40 -07001383 uint8_t sib = (scale << 6) | (RegStorage::RegNum(index) << 3) | RegStorage::RegNum(base);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001384 code_buffer_.push_back(sib);
1385 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1386 } else {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001387 DCHECK_LT(RegStorage::RegNum(reg), 8);
buzbee091cc402014-03-31 10:14:40 -07001388 code_buffer_.push_back(entry->skeleton.opcode + RegStorage::RegNum(reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001389 }
1390 code_buffer_.push_back(disp & 0xFF);
1391 code_buffer_.push_back((disp >> 8) & 0xFF);
1392 code_buffer_.push_back((disp >> 16) & 0xFF);
1393 code_buffer_.push_back((disp >> 24) & 0xFF);
1394 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1395 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1396}
1397
1398void X86Mir2Lir::EmitMacro(const X86EncodingMap* entry, uint8_t reg, int offset) {
1399 DCHECK(entry->opcode == kX86StartOfMethod) << entry->name;
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001400 EmitPrefix(entry, reg, NO_REG, NO_REG);
1401 reg = LowRegisterBits(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001402 code_buffer_.push_back(0xE8); // call +0
1403 code_buffer_.push_back(0);
1404 code_buffer_.push_back(0);
1405 code_buffer_.push_back(0);
1406 code_buffer_.push_back(0);
1407
buzbee091cc402014-03-31 10:14:40 -07001408 DCHECK_LT(RegStorage::RegNum(reg), 8);
1409 code_buffer_.push_back(0x58 + RegStorage::RegNum(reg)); // pop reg
Brian Carlstrom7940e442013-07-12 13:46:57 -07001410
buzbee091cc402014-03-31 10:14:40 -07001411 EmitRegImm(&X86Mir2Lir::EncodingMap[kX86Sub32RI], RegStorage::RegNum(reg),
1412 offset + 5 /* size of call +0 */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001413}
1414
1415void X86Mir2Lir::EmitUnimplemented(const X86EncodingMap* entry, LIR* lir) {
1416 UNIMPLEMENTED(WARNING) << "encoding kind for " << entry->name << " "
1417 << BuildInsnString(entry->fmt, lir, 0);
1418 for (int i = 0; i < GetInsnSize(lir); ++i) {
1419 code_buffer_.push_back(0xCC); // push breakpoint instruction - int 3
1420 }
1421}
1422
1423/*
1424 * Assemble the LIR into binary instruction format. Note that we may
1425 * discover that pc-relative displacements may not fit the selected
1426 * instruction. In those cases we will try to substitute a new code
1427 * sequence or request that the trace be shortened and retried.
1428 */
buzbee0d829482013-10-11 15:24:55 -07001429AssemblerStatus X86Mir2Lir::AssembleInstructions(CodeOffset start_addr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001430 LIR *lir;
1431 AssemblerStatus res = kSuccess; // Assume success
1432
1433 const bool kVerbosePcFixup = false;
1434 for (lir = first_lir_insn_; lir != NULL; lir = NEXT_LIR(lir)) {
buzbee409fe942013-10-11 10:49:56 -07001435 if (IsPseudoLirOp(lir->opcode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001436 continue;
1437 }
1438
1439 if (lir->flags.is_nop) {
1440 continue;
1441 }
1442
buzbeeb48819d2013-09-14 16:15:25 -07001443 if (lir->flags.fixup != kFixupNone) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001444 switch (lir->opcode) {
1445 case kX86Jcc8: {
1446 LIR *target_lir = lir->target;
1447 DCHECK(target_lir != NULL);
1448 int delta = 0;
buzbee0d829482013-10-11 15:24:55 -07001449 CodeOffset pc;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001450 if (IS_SIMM8(lir->operands[0])) {
1451 pc = lir->offset + 2 /* opcode + rel8 */;
1452 } else {
1453 pc = lir->offset + 6 /* 2 byte opcode + rel32 */;
1454 }
buzbee0d829482013-10-11 15:24:55 -07001455 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001456 delta = target - pc;
1457 if (IS_SIMM8(delta) != IS_SIMM8(lir->operands[0])) {
1458 if (kVerbosePcFixup) {
1459 LOG(INFO) << "Retry for JCC growth at " << lir->offset
1460 << " delta: " << delta << " old delta: " << lir->operands[0];
1461 }
1462 lir->opcode = kX86Jcc32;
1463 SetupResourceMasks(lir);
1464 res = kRetryAll;
1465 }
1466 if (kVerbosePcFixup) {
1467 LOG(INFO) << "Source:";
1468 DumpLIRInsn(lir, 0);
1469 LOG(INFO) << "Target:";
1470 DumpLIRInsn(target_lir, 0);
1471 LOG(INFO) << "Delta " << delta;
1472 }
1473 lir->operands[0] = delta;
1474 break;
1475 }
1476 case kX86Jcc32: {
1477 LIR *target_lir = lir->target;
1478 DCHECK(target_lir != NULL);
buzbee0d829482013-10-11 15:24:55 -07001479 CodeOffset pc = lir->offset + 6 /* 2 byte opcode + rel32 */;
1480 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001481 int delta = target - pc;
1482 if (kVerbosePcFixup) {
1483 LOG(INFO) << "Source:";
1484 DumpLIRInsn(lir, 0);
1485 LOG(INFO) << "Target:";
1486 DumpLIRInsn(target_lir, 0);
1487 LOG(INFO) << "Delta " << delta;
1488 }
1489 lir->operands[0] = delta;
1490 break;
1491 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001492 case kX86Jecxz8: {
1493 LIR *target_lir = lir->target;
1494 DCHECK(target_lir != NULL);
1495 CodeOffset pc;
1496 pc = lir->offset + 2; // opcode + rel8
1497 CodeOffset target = target_lir->offset;
1498 int delta = target - pc;
1499 lir->operands[0] = delta;
1500 DCHECK(IS_SIMM8(delta));
1501 break;
1502 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001503 case kX86Jmp8: {
1504 LIR *target_lir = lir->target;
1505 DCHECK(target_lir != NULL);
1506 int delta = 0;
buzbee0d829482013-10-11 15:24:55 -07001507 CodeOffset pc;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001508 if (IS_SIMM8(lir->operands[0])) {
1509 pc = lir->offset + 2 /* opcode + rel8 */;
1510 } else {
1511 pc = lir->offset + 5 /* opcode + rel32 */;
1512 }
buzbee0d829482013-10-11 15:24:55 -07001513 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001514 delta = target - pc;
1515 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && delta == 0) {
1516 // Useless branch
buzbee252254b2013-09-08 16:20:53 -07001517 NopLIR(lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001518 if (kVerbosePcFixup) {
1519 LOG(INFO) << "Retry for useless branch at " << lir->offset;
1520 }
1521 res = kRetryAll;
1522 } else if (IS_SIMM8(delta) != IS_SIMM8(lir->operands[0])) {
1523 if (kVerbosePcFixup) {
1524 LOG(INFO) << "Retry for JMP growth at " << lir->offset;
1525 }
1526 lir->opcode = kX86Jmp32;
1527 SetupResourceMasks(lir);
1528 res = kRetryAll;
1529 }
1530 lir->operands[0] = delta;
1531 break;
1532 }
1533 case kX86Jmp32: {
1534 LIR *target_lir = lir->target;
1535 DCHECK(target_lir != NULL);
buzbee0d829482013-10-11 15:24:55 -07001536 CodeOffset pc = lir->offset + 5 /* opcode + rel32 */;
1537 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001538 int delta = target - pc;
1539 lir->operands[0] = delta;
1540 break;
1541 }
1542 default:
Mark Mendell67c39c42014-01-31 17:28:00 -08001543 if (lir->flags.fixup == kFixupLoad) {
1544 LIR *target_lir = lir->target;
1545 DCHECK(target_lir != NULL);
1546 CodeOffset target = target_lir->offset;
1547 lir->operands[2] = target;
1548 int newSize = GetInsnSize(lir);
1549 if (newSize != lir->flags.size) {
1550 lir->flags.size = newSize;
1551 res = kRetryAll;
1552 }
1553 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001554 break;
1555 }
1556 }
1557
1558 /*
1559 * If one of the pc-relative instructions expanded we'll have
1560 * to make another pass. Don't bother to fully assemble the
1561 * instruction.
1562 */
1563 if (res != kSuccess) {
1564 continue;
1565 }
1566 CHECK_EQ(static_cast<size_t>(lir->offset), code_buffer_.size());
1567 const X86EncodingMap *entry = &X86Mir2Lir::EncodingMap[lir->opcode];
1568 size_t starting_cbuf_size = code_buffer_.size();
1569 switch (entry->kind) {
1570 case kData: // 4 bytes of data
1571 code_buffer_.push_back(lir->operands[0]);
1572 break;
1573 case kNullary: // 1 byte of opcode
1574 DCHECK_EQ(0, entry->skeleton.prefix1);
1575 DCHECK_EQ(0, entry->skeleton.prefix2);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001576 EmitOpcode(entry);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001577 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1578 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1579 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1580 break;
Mark Mendell4028a6c2014-02-19 20:06:20 -08001581 case kPrefix2Nullary: // 1 byte of opcode + 2 prefixes.
1582 DCHECK_NE(0, entry->skeleton.prefix1);
1583 DCHECK_NE(0, entry->skeleton.prefix2);
1584 EmitPrefixAndOpcode(entry);
1585 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1586 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1587 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1588 break;
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001589 case kRegOpcode: // lir operands - 0: reg
1590 EmitOpRegOpcode(entry, lir->operands[0]);
1591 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001592 case kReg: // lir operands - 0: reg
1593 EmitOpReg(entry, lir->operands[0]);
1594 break;
1595 case kMem: // lir operands - 0: base, 1: disp
1596 EmitOpMem(entry, lir->operands[0], lir->operands[1]);
1597 break;
Vladimir Marko057c74a2013-12-03 15:20:45 +00001598 case kArray: // lir operands - 0: base, 1: index, 2: scale, 3: disp
1599 EmitOpArray(entry, lir->operands[0], lir->operands[1], lir->operands[2], lir->operands[3]);
1600 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001601 case kMemReg: // lir operands - 0: base, 1: disp, 2: reg
1602 EmitMemReg(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1603 break;
Mark Mendell343adb52013-12-18 06:02:17 -08001604 case kMemImm: // lir operands - 0: base, 1: disp, 2: immediate
1605 EmitMemImm(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1606 break;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001607 case kArrayImm: // lir operands - 0: base, 1: index, 2: disp, 3:scale, 4:immediate
1608 EmitArrayImm(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1609 lir->operands[3], lir->operands[4]);
1610 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001611 case kArrayReg: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
1612 EmitArrayReg(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1613 lir->operands[3], lir->operands[4]);
1614 break;
1615 case kRegMem: // lir operands - 0: reg, 1: base, 2: disp
1616 EmitRegMem(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1617 break;
1618 case kRegArray: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
1619 EmitRegArray(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1620 lir->operands[3], lir->operands[4]);
1621 break;
1622 case kRegThread: // lir operands - 0: reg, 1: disp
1623 EmitRegThread(entry, lir->operands[0], lir->operands[1]);
1624 break;
1625 case kRegReg: // lir operands - 0: reg1, 1: reg2
1626 EmitRegReg(entry, lir->operands[0], lir->operands[1]);
1627 break;
1628 case kRegRegStore: // lir operands - 0: reg2, 1: reg1
1629 EmitRegReg(entry, lir->operands[1], lir->operands[0]);
1630 break;
Mark Mendell4708dcd2014-01-22 09:05:18 -08001631 case kRegRegImmRev:
1632 EmitRegRegImmRev(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1633 break;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001634 case kMemRegImm:
1635 EmitMemRegImm(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1636 lir->operands[3]);
1637 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001638 case kRegRegImm:
1639 EmitRegRegImm(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1640 break;
Mark Mendell4708dcd2014-01-22 09:05:18 -08001641 case kRegMemImm:
1642 EmitRegMemImm(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1643 lir->operands[3]);
1644 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001645 case kRegImm: // lir operands - 0: reg, 1: immediate
1646 EmitRegImm(entry, lir->operands[0], lir->operands[1]);
1647 break;
1648 case kThreadImm: // lir operands - 0: disp, 1: immediate
1649 EmitThreadImm(entry, lir->operands[0], lir->operands[1]);
1650 break;
1651 case kMovRegImm: // lir operands - 0: reg, 1: immediate
1652 EmitMovRegImm(entry, lir->operands[0], lir->operands[1]);
1653 break;
1654 case kShiftRegImm: // lir operands - 0: reg, 1: immediate
1655 EmitShiftRegImm(entry, lir->operands[0], lir->operands[1]);
1656 break;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001657 case kShiftMemImm: // lir operands - 0: base, 1: disp, 2:immediate
1658 EmitShiftMemImm(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1659 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001660 case kShiftRegCl: // lir operands - 0: reg, 1: cl
Brian Carlstrom7940e442013-07-12 13:46:57 -07001661 EmitShiftRegCl(entry, lir->operands[0], lir->operands[1]);
1662 break;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001663 case kShiftMemCl: // lir operands - 0: base, 1:displacement, 2: cl
1664 EmitShiftMemCl(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1665 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001666 case kRegCond: // lir operands - 0: reg, 1: condition
1667 EmitRegCond(entry, lir->operands[0], lir->operands[1]);
1668 break;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001669 case kMemCond: // lir operands - 0: base, 1: displacement, 2: condition
1670 EmitMemCond(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1671 break;
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001672 case kRegRegCond: // lir operands - 0: reg, 1: reg, 2: condition
1673 EmitRegRegCond(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1674 break;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001675 case kRegMemCond: // lir operands - 0: reg, 1: reg, displacement, 3: condition
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001676 EmitRegMemCond(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1677 lir->operands[3]);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001678 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001679 case kJmp: // lir operands - 0: rel
Brian Carlstrom60d7a652014-03-13 18:10:08 -07001680 if (entry->opcode == kX86JmpT) {
1681 // This works since the instruction format for jmp and call is basically the same and
1682 // EmitCallThread loads opcode info.
1683 EmitCallThread(entry, lir->operands[0]);
1684 } else {
1685 EmitJmp(entry, lir->operands[0]);
1686 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001687 break;
1688 case kJcc: // lir operands - 0: rel, 1: CC, target assigned
1689 EmitJcc(entry, lir->operands[0], lir->operands[1]);
1690 break;
1691 case kCall:
1692 switch (entry->opcode) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001693 case kX86CallI: // lir operands - 0: disp
1694 EmitCallImmediate(entry, lir->operands[0]);
1695 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001696 case kX86CallM: // lir operands - 0: base, 1: disp
1697 EmitCallMem(entry, lir->operands[0], lir->operands[1]);
1698 break;
1699 case kX86CallT: // lir operands - 0: disp
1700 EmitCallThread(entry, lir->operands[0]);
1701 break;
1702 default:
1703 EmitUnimplemented(entry, lir);
1704 break;
1705 }
1706 break;
1707 case kPcRel: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
1708 EmitPcRel(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1709 lir->operands[3], lir->operands[4]);
1710 break;
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001711 case kMacro: // lir operands - 0: reg
Brian Carlstrom7940e442013-07-12 13:46:57 -07001712 EmitMacro(entry, lir->operands[0], lir->offset);
1713 break;
1714 default:
1715 EmitUnimplemented(entry, lir);
1716 break;
1717 }
1718 CHECK_EQ(static_cast<size_t>(GetInsnSize(lir)),
1719 code_buffer_.size() - starting_cbuf_size)
1720 << "Instruction size mismatch for entry: " << X86Mir2Lir::EncodingMap[lir->opcode].name;
1721 }
1722 return res;
1723}
1724
buzbeeb48819d2013-09-14 16:15:25 -07001725// LIR offset assignment.
1726// TODO: consolidate w/ Arm assembly mechanism.
1727int X86Mir2Lir::AssignInsnOffsets() {
1728 LIR* lir;
1729 int offset = 0;
1730
1731 for (lir = first_lir_insn_; lir != NULL; lir = NEXT_LIR(lir)) {
1732 lir->offset = offset;
buzbee409fe942013-10-11 10:49:56 -07001733 if (LIKELY(!IsPseudoLirOp(lir->opcode))) {
buzbeeb48819d2013-09-14 16:15:25 -07001734 if (!lir->flags.is_nop) {
1735 offset += lir->flags.size;
1736 }
1737 } else if (UNLIKELY(lir->opcode == kPseudoPseudoAlign4)) {
1738 if (offset & 0x2) {
1739 offset += 2;
1740 lir->operands[0] = 1;
1741 } else {
1742 lir->operands[0] = 0;
1743 }
1744 }
1745 /* Pseudo opcodes don't consume space */
1746 }
1747 return offset;
1748}
1749
1750/*
1751 * Walk the compilation unit and assign offsets to instructions
1752 * and literals and compute the total size of the compiled unit.
1753 * TODO: consolidate w/ Arm assembly mechanism.
1754 */
1755void X86Mir2Lir::AssignOffsets() {
1756 int offset = AssignInsnOffsets();
1757
Mark Mendelld65c51a2014-04-29 16:55:20 -04001758 if (const_vectors_ != nullptr) {
1759 /* assign offsets to vector literals */
1760
1761 // First, get offset to 12 mod 16 to align to 16 byte boundary.
1762 // This will ensure that the vector is 16 byte aligned, as the procedure is
1763 // always aligned at at 4 mod 16.
1764 int align_size = (16-4) - (offset & 0xF);
1765 if (align_size < 0) {
1766 align_size += 16;
1767 }
1768
1769 offset += align_size;
1770
1771 // Now assign each literal the right offset.
1772 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
1773 p->offset = offset;
1774 offset += 16;
1775 }
1776 }
1777
buzbeeb48819d2013-09-14 16:15:25 -07001778 /* Const values have to be word aligned */
Andreas Gampe66018822014-05-05 20:47:19 -07001779 offset = RoundUp(offset, 4);
buzbeeb48819d2013-09-14 16:15:25 -07001780
1781 /* Set up offsets for literals */
1782 data_offset_ = offset;
1783
1784 offset = AssignLiteralOffset(offset);
1785
1786 offset = AssignSwitchTablesOffset(offset);
1787
1788 offset = AssignFillArrayDataOffset(offset);
1789
1790 total_size_ = offset;
1791}
1792
1793/*
1794 * Go over each instruction in the list and calculate the offset from the top
1795 * before sending them off to the assembler. If out-of-range branch distance is
1796 * seen rearrange the instructions a bit to correct it.
1797 * TODO: consolidate w/ Arm assembly mechanism.
1798 */
1799void X86Mir2Lir::AssembleLIR() {
buzbeea61f4952013-08-23 14:27:06 -07001800 cu_->NewTimingSplit("Assemble");
Mark Mendell55d0eac2014-02-06 11:02:52 -08001801
1802 // We will remove the method address if we never ended up using it
1803 if (store_method_addr_ && !store_method_addr_used_) {
1804 setup_method_address_[0]->flags.is_nop = true;
1805 setup_method_address_[1]->flags.is_nop = true;
1806 }
1807
buzbeeb48819d2013-09-14 16:15:25 -07001808 AssignOffsets();
1809 int assembler_retries = 0;
1810 /*
1811 * Assemble here. Note that we generate code with optimistic assumptions
1812 * and if found now to work, we'll have to redo the sequence and retry.
1813 */
1814
1815 while (true) {
1816 AssemblerStatus res = AssembleInstructions(0);
1817 if (res == kSuccess) {
1818 break;
1819 } else {
1820 assembler_retries++;
1821 if (assembler_retries > MAX_ASSEMBLER_RETRIES) {
1822 CodegenDump();
1823 LOG(FATAL) << "Assembler error - too many retries";
1824 }
1825 // Redo offsets and try again
1826 AssignOffsets();
1827 code_buffer_.clear();
1828 }
1829 }
1830
1831 // Install literals
1832 InstallLiteralPools();
1833
1834 // Install switch tables
1835 InstallSwitchTables();
1836
1837 // Install fill array data
1838 InstallFillArrayData();
1839
1840 // Create the mapping table and native offset to reference map.
buzbeea61f4952013-08-23 14:27:06 -07001841 cu_->NewTimingSplit("PcMappingTable");
buzbeeb48819d2013-09-14 16:15:25 -07001842 CreateMappingTables();
1843
buzbeea61f4952013-08-23 14:27:06 -07001844 cu_->NewTimingSplit("GcMap");
buzbeeb48819d2013-09-14 16:15:25 -07001845 CreateNativeGcMap();
1846}
1847
Brian Carlstrom7940e442013-07-12 13:46:57 -07001848} // namespace art