blob: 3f54798b7e319b23f69a62ab83c90713bd7fe4a2 [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] = {
Ian Rogers0f9b9c52014-06-09 01:32:12 -070026 { kX8632BitData, kData, IS_UNARY_OP, { 0, 0, 0x00, 0, 0, 0, 0, 4, false }, "data", "0x!0d" },
27 { kX86Bkpt, kNullary, NO_OPERAND | IS_BRANCH, { 0, 0, 0xCC, 0, 0, 0, 0, 0, false }, "int 3", "" },
28 { kX86Nop, kNop, NO_OPERAND, { 0, 0, 0x90, 0, 0, 0, 0, 0, false }, "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) \
Ian Rogers0f9b9c52014-06-09 01:32:12 -070037{ kX86 ## opname ## 8MR, kMemReg, mem_use | IS_TERTIARY_OP | REG_USE02 | SETS_CCODES | uses_ccodes, { 0, 0, rm8_r8, 0, 0, 0, 0, 0, true }, #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, true}, #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, true }, #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, true }, #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, true }, #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, true }, #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, true }, #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, true }, #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, true }, #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, true }, #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, true }, #opname "8TI", "fs:[!0d],!1d" }, \
Brian Carlstrom7940e442013-07-12 13:46:57 -070048 \
Ian Rogers0f9b9c52014-06-09 01:32:12 -070049{ kX86 ## opname ## 16MR, kMemReg, mem_use | IS_TERTIARY_OP | REG_USE02 | SETS_CCODES | uses_ccodes, { 0x66, 0, rm32_r32, 0, 0, 0, 0, 0, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #opname "16TI8", "fs:[!0d],!1d" }, \
Brian Carlstrom7940e442013-07-12 13:46:57 -070064 \
Ian Rogers0f9b9c52014-06-09 01:32:12 -070065{ kX86 ## opname ## 32MR, kMemReg, mem_use | IS_TERTIARY_OP | REG_USE02 | SETS_CCODES | uses_ccodes, { 0, 0, rm32_r32, 0, 0, 0, 0, 0, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #opname "32AI8", "[!0r+!1r<<!2d+!3d],!4d" }, \
79{ kX86 ## opname ## 32TI8, kThreadImm, mem_use | IS_BINARY_OP | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1, false }, #opname "32TI8", "fs:[!0d],!1d" }, \
Dmitry Petrochenko96992e82014-05-20 04:03:46 +070080 \
Ian Rogers0f9b9c52014-06-09 01:32:12 -070081{ 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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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, false }, #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
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700147 { kX86Imul16RRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { 0x66, 0, 0x69, 0, 0, 0, 0, 2, false }, "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, false }, "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, false }, "Imul16RAI", "!0r,[!1r+!2r<<!3d+!4d],!5d" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700150
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700151 { kX86Imul32RRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { 0, 0, 0x69, 0, 0, 0, 0, 4, false }, "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, false }, "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, false }, "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, false }, "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, false }, "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, false }, "Imul32RAI8", "!0r,[!1r+!2r<<!3d+!4d],!5d" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700158 { kX86Imul64RRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { REX_W, 0, 0x69, 0, 0, 0, 0, 4, false }, "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, 4, false }, "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, 4, false }, "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, false }, "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, false }, "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, false }, "Imul64RAI8", "!0r,[!1r+!2r<<!3d+!4d],!5d" },
Dmitry Petrochenko96992e82014-05-20 04:03:46 +0700164
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700165 { kX86Mov8MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0, 0, 0x88, 0, 0, 0, 0, 0, true }, "Mov8MR", "[!0r+!1d],!2r" },
166 { kX86Mov8AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0, 0, 0x88, 0, 0, 0, 0, 0, true }, "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, true }, "Mov8TR", "fs:[!0d],!1r" },
168 { kX86Mov8RR, kRegReg, IS_BINARY_OP | REG_DEF0_USE1, { 0, 0, 0x8A, 0, 0, 0, 0, 0, true }, "Mov8RR", "!0r,!1r" },
169 { kX86Mov8RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { 0, 0, 0x8A, 0, 0, 0, 0, 0, true }, "Mov8RM", "!0r,[!1r+!2d]" },
170 { kX86Mov8RA, kRegArray, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8A, 0, 0, 0, 0, 0, true }, "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, true }, "Mov8RT", "!0r,fs:[!1d]" },
172 { kX86Mov8RI, kMovRegImm, IS_BINARY_OP | REG_DEF0, { 0, 0, 0xB0, 0, 0, 0, 0, 1, true }, "Mov8RI", "!0r,!1d" },
173 { kX86Mov8MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { 0, 0, 0xC6, 0, 0, 0, 0, 1, true }, "Mov8MI", "[!0r+!1d],!2d" },
174 { kX86Mov8AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { 0, 0, 0xC6, 0, 0, 0, 0, 1, true }, "Mov8AI", "[!0r+!1r<<!2d+!3d],!4d" },
175 { kX86Mov8TI, kThreadImm, IS_STORE | IS_BINARY_OP, { THREAD_PREFIX, 0, 0xC6, 0, 0, 0, 0, 1, true }, "Mov8TI", "fs:[!0d],!1d" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700176
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700177 { kX86Mov16MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, 0, 0x89, 0, 0, 0, 0, 0, false }, "Mov16MR", "[!0r+!1d],!2r" },
178 { kX86Mov16AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x66, 0, 0x89, 0, 0, 0, 0, 0, false }, "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, false }, "Mov16TR", "fs:[!0d],!1r" },
180 { kX86Mov16RR, kRegReg, IS_BINARY_OP | REG_DEF0_USE1, { 0x66, 0, 0x8B, 0, 0, 0, 0, 0, false }, "Mov16RR", "!0r,!1r" },
181 { kX86Mov16RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { 0x66, 0, 0x8B, 0, 0, 0, 0, 0, false }, "Mov16RM", "!0r,[!1r+!2d]" },
182 { kX86Mov16RA, kRegArray, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0x66, 0, 0x8B, 0, 0, 0, 0, 0, false }, "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, false }, "Mov16RT", "!0r,fs:[!1d]" },
184 { kX86Mov16RI, kMovRegImm, IS_BINARY_OP | REG_DEF0, { 0x66, 0, 0xB8, 0, 0, 0, 0, 2, false }, "Mov16RI", "!0r,!1d" },
185 { kX86Mov16MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { 0x66, 0, 0xC7, 0, 0, 0, 0, 2, false }, "Mov16MI", "[!0r+!1d],!2d" },
186 { kX86Mov16AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { 0x66, 0, 0xC7, 0, 0, 0, 0, 2, false }, "Mov16AI", "[!0r+!1r<<!2d+!3d],!4d" },
187 { kX86Mov16TI, kThreadImm, IS_STORE | IS_BINARY_OP, { THREAD_PREFIX, 0x66, 0xC7, 0, 0, 0, 0, 2, false }, "Mov16TI", "fs:[!0d],!1d" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700189 { kX86Mov32MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0, 0, 0x89, 0, 0, 0, 0, 0, false }, "Mov32MR", "[!0r+!1d],!2r" },
190 { kX86Mov32AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0, 0, 0x89, 0, 0, 0, 0, 0, false }, "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, false }, "Mov32TR", "fs:[!0d],!1r" },
192 { kX86Mov32RR, kRegReg, IS_BINARY_OP | REG_DEF0_USE1, { 0, 0, 0x8B, 0, 0, 0, 0, 0, false }, "Mov32RR", "!0r,!1r" },
193 { kX86Mov32RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { 0, 0, 0x8B, 0, 0, 0, 0, 0, false }, "Mov32RM", "!0r,[!1r+!2d]" },
194 { kX86Mov32RA, kRegArray, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8B, 0, 0, 0, 0, 0, false }, "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, false }, "Mov32RT", "!0r,fs:[!1d]" },
196 { kX86Mov32RI, kMovRegImm, IS_BINARY_OP | REG_DEF0, { 0, 0, 0xB8, 0, 0, 0, 0, 4, false }, "Mov32RI", "!0r,!1d" },
197 { kX86Mov32MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { 0, 0, 0xC7, 0, 0, 0, 0, 4, false }, "Mov32MI", "[!0r+!1d],!2d" },
198 { kX86Mov32AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { 0, 0, 0xC7, 0, 0, 0, 0, 4, false }, "Mov32AI", "[!0r+!1r<<!2d+!3d],!4d" },
199 { kX86Mov32TI, kThreadImm, IS_STORE | IS_BINARY_OP, { THREAD_PREFIX, 0, 0xC7, 0, 0, 0, 0, 4, false }, "Mov32TI", "fs:[!0d],!1d" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700201 { kX86Lea32RM, kRegMem, IS_TERTIARY_OP | IS_LOAD | REG_DEF0_USE1, { 0, 0, 0x8D, 0, 0, 0, 0, 0, false }, "Lea32RM", "!0r,[!1r+!2d]" },
202 { kX86Lea32RA, kRegArray, IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8D, 0, 0, 0, 0, 0, false }, "Lea32RA", "!0r,[!1r+!2r<<!3d+!4d]" },
Mark Mendell4028a6c2014-02-19 20:06:20 -0800203
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700204 { kX86Mov64MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { REX_W, 0, 0x89, 0, 0, 0, 0, 0, false }, "Mov64MR", "[!0r+!1d],!2r" },
205 { kX86Mov64AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { REX_W, 0, 0x89, 0, 0, 0, 0, 0, false }, "Mov64AR", "[!0r+!1r<<!2d+!3d],!4r" },
206 { kX86Mov64TR, kThreadReg, IS_STORE | IS_BINARY_OP | REG_USE1, { THREAD_PREFIX, REX_W, 0x89, 0, 0, 0, 0, 0, false }, "Mov64TR", "fs:[!0d],!1r" },
207 { kX86Mov64RR, kRegReg, IS_BINARY_OP | REG_DEF0_USE1, { REX_W, 0, 0x8B, 0, 0, 0, 0, 0, false }, "Mov64RR", "!0r,!1r" },
208 { kX86Mov64RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { REX_W, 0, 0x8B, 0, 0, 0, 0, 0, false }, "Mov64RM", "!0r,[!1r+!2d]" },
209 { kX86Mov64RA, kRegArray, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { REX_W, 0, 0x8B, 0, 0, 0, 0, 0, false }, "Mov64RA", "!0r,[!1r+!2r<<!3d+!4d]" },
210 { kX86Mov64RT, kRegThread, IS_LOAD | IS_BINARY_OP | REG_DEF0, { THREAD_PREFIX, REX_W, 0x8B, 0, 0, 0, 0, 0, false }, "Mov64RT", "!0r,fs:[!1d]" },
211 { kX86Mov64RI, kMovRegImm, IS_BINARY_OP | REG_DEF0, { REX_W, 0, 0xB8, 0, 0, 0, 0, 8, false }, "Mov64RI", "!0r,!1d" },
212 { kX86Mov64MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { REX_W, 0, 0xC7, 0, 0, 0, 0, 4, false }, "Mov64MI", "[!0r+!1d],!2d" },
213 { kX86Mov64AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { REX_W, 0, 0xC7, 0, 0, 0, 0, 4, false }, "Mov64AI", "[!0r+!1r<<!2d+!3d],!4d" },
214 { kX86Mov64TI, kThreadImm, IS_STORE | IS_BINARY_OP, { THREAD_PREFIX, REX_W, 0xC7, 0, 0, 0, 0, 4, false }, "Mov64TI", "fs:[!0d],!1d" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700215
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700216 { kX86Lea64RM, kRegMem, IS_TERTIARY_OP | IS_LOAD | REG_DEF0_USE1, { REX_W, 0, 0x8D, 0, 0, 0, 0, 0, false }, "Lea64RM", "!0r,[!1r+!2d]" },
217 { kX86Lea64RA, kRegArray, IS_QUIN_OP | REG_DEF0_USE12, { REX_W, 0, 0x8D, 0, 0, 0, 0, 0, false }, "Lea64RA", "!0r,[!1r+!2r<<!3d+!4d]" },
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800218
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700219 { kX86Cmov32RRC, kRegRegCond, IS_TERTIARY_OP | REG_DEF0_USE01 | USES_CCODES, { 0, 0, 0x0F, 0x40, 0, 0, 0, 0, false }, "Cmovcc32RR", "!2c !0r,!1r" },
220 { kX86Cmov64RRC, kRegRegCond, IS_TERTIARY_OP | REG_DEF0_USE01 | USES_CCODES, { REX_W, 0, 0x0F, 0x40, 0, 0, 0, 0, false }, "Cmovcc64RR", "!2c !0r,!1r" },
Dmitry Petrochenko96992e82014-05-20 04:03:46 +0700221
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700222 { kX86Cmov32RMC, kRegMemCond, IS_QUAD_OP | IS_LOAD | REG_DEF0_USE01 | USES_CCODES, { 0, 0, 0x0F, 0x40, 0, 0, 0, 0, false }, "Cmovcc32RM", "!3c !0r,[!1r+!2d]" },
223 { kX86Cmov64RMC, kRegMemCond, IS_QUAD_OP | IS_LOAD | REG_DEF0_USE01 | USES_CCODES, { REX_W, 0, 0x0F, 0x40, 0, 0, 0, 0, false }, "Cmovcc64RM", "!3c !0r,[!1r+!2d]" },
Mark Mendell2637f2e2014-04-30 10:10:47 -0400224
Brian Carlstrom7940e442013-07-12 13:46:57 -0700225#define SHIFT_ENCODING_MAP(opname, modrm_opcode) \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700226{ kX86 ## opname ## 8RI, kShiftRegImm, IS_BINARY_OP | REG_DEF0_USE0 | SETS_CCODES, { 0, 0, 0xC0, 0, 0, modrm_opcode, 0xD1, 1, true }, #opname "8RI", "!0r,!1d" }, \
227{ kX86 ## opname ## 8MI, kShiftMemImm, IS_LOAD | IS_STORE | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xC0, 0, 0, modrm_opcode, 0xD1, 1, true }, #opname "8MI", "[!0r+!1d],!2d" }, \
228{ kX86 ## opname ## 8AI, kShiftArrayImm, IS_LOAD | IS_STORE | IS_QUIN_OP | REG_USE01 | SETS_CCODES, { 0, 0, 0xC0, 0, 0, modrm_opcode, 0xD1, 1, true }, #opname "8AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
229{ kX86 ## opname ## 8RC, kShiftRegCl, IS_BINARY_OP | REG_DEF0_USE0 | REG_USEC | SETS_CCODES, { 0, 0, 0xD2, 0, 0, modrm_opcode, 0, 1, true }, #opname "8RC", "!0r,cl" }, \
230{ 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, true }, #opname "8MC", "[!0r+!1d],cl" }, \
231{ 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, true }, #opname "8AC", "[!0r+!1r<<!2d+!3d],cl" }, \
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232 \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700233{ kX86 ## opname ## 16RI, kShiftRegImm, IS_BINARY_OP | REG_DEF0_USE0 | SETS_CCODES, { 0x66, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1, false }, #opname "16RI", "!0r,!1d" }, \
234{ kX86 ## opname ## 16MI, kShiftMemImm, IS_LOAD | IS_STORE | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0x66, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1, false }, #opname "16MI", "[!0r+!1d],!2d" }, \
235{ kX86 ## opname ## 16AI, kShiftArrayImm, IS_LOAD | IS_STORE | IS_QUIN_OP | REG_USE01 | SETS_CCODES, { 0x66, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1, false }, #opname "16AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
236{ kX86 ## opname ## 16RC, kShiftRegCl, IS_BINARY_OP | REG_DEF0_USE0 | REG_USEC | SETS_CCODES, { 0x66, 0, 0xD3, 0, 0, modrm_opcode, 0, 1, false }, #opname "16RC", "!0r,cl" }, \
237{ 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, false }, #opname "16MC", "[!0r+!1d],cl" }, \
238{ 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, false }, #opname "16AC", "[!0r+!1r<<!2d+!3d],cl" }, \
Brian Carlstrom7940e442013-07-12 13:46:57 -0700239 \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700240{ kX86 ## opname ## 32RI, kShiftRegImm, IS_BINARY_OP | REG_DEF0_USE0 | SETS_CCODES, { 0, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1, false }, #opname "32RI", "!0r,!1d" }, \
241{ kX86 ## opname ## 32MI, kShiftMemImm, IS_LOAD | IS_STORE | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1, false }, #opname "32MI", "[!0r+!1d],!2d" }, \
242{ kX86 ## opname ## 32AI, kShiftArrayImm, IS_LOAD | IS_STORE | IS_QUIN_OP | REG_USE01 | SETS_CCODES, { 0, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1, false }, #opname "32AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
243{ kX86 ## opname ## 32RC, kShiftRegCl, IS_BINARY_OP | REG_DEF0_USE0 | REG_USEC | SETS_CCODES, { 0, 0, 0xD3, 0, 0, modrm_opcode, 0, 0, false }, #opname "32RC", "!0r,cl" }, \
244{ 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, false }, #opname "32MC", "[!0r+!1d],cl" }, \
245{ 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, false }, #opname "32AC", "[!0r+!1r<<!2d+!3d],cl" }, \
Dmitry Petrochenko96992e82014-05-20 04:03:46 +0700246 \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700247{ kX86 ## opname ## 64RI, kShiftRegImm, IS_BINARY_OP | REG_DEF0_USE0 | SETS_CCODES, { REX_W, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1, false }, #opname "64RI", "!0r,!1d" }, \
248{ 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, false }, #opname "64MI", "[!0r+!1d],!2d" }, \
249{ 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, false }, #opname "64AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
250{ kX86 ## opname ## 64RC, kShiftRegCl, IS_BINARY_OP | REG_DEF0_USE0 | REG_USEC | SETS_CCODES, { REX_W, 0, 0xD3, 0, 0, modrm_opcode, 0, 0, false }, #opname "64RC", "!0r,cl" }, \
251{ 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, false }, #opname "64MC", "[!0r+!1d],cl" }, \
252{ 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, false }, #opname "64AC", "[!0r+!1r<<!2d+!3d],cl" }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253
254 SHIFT_ENCODING_MAP(Rol, 0x0),
255 SHIFT_ENCODING_MAP(Ror, 0x1),
256 SHIFT_ENCODING_MAP(Rcl, 0x2),
257 SHIFT_ENCODING_MAP(Rcr, 0x3),
258 SHIFT_ENCODING_MAP(Sal, 0x4),
259 SHIFT_ENCODING_MAP(Shr, 0x5),
260 SHIFT_ENCODING_MAP(Sar, 0x7),
261#undef SHIFT_ENCODING_MAP
262
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700263 { kX86Cmc, kNullary, NO_OPERAND, { 0, 0, 0xF5, 0, 0, 0, 0, 0, false }, "Cmc", "" },
264 { kX86Shld32RRI, kRegRegImmStore, IS_TERTIARY_OP | REG_DEF0_USE01 | SETS_CCODES, { 0, 0, 0x0F, 0xA4, 0, 0, 0, 1, false }, "Shld32RRI", "!0r,!1r,!2d" },
265 { kX86Shld32MRI, kMemRegImm, IS_QUAD_OP | REG_USE02 | IS_LOAD | IS_STORE | SETS_CCODES, { 0, 0, 0x0F, 0xA4, 0, 0, 0, 1, false }, "Shld32MRI", "[!0r+!1d],!2r,!3d" },
266 { kX86Shrd32RRI, kRegRegImmStore, IS_TERTIARY_OP | REG_DEF0_USE01 | SETS_CCODES, { 0, 0, 0x0F, 0xAC, 0, 0, 0, 1, false }, "Shrd32RRI", "!0r,!1r,!2d" },
267 { kX86Shrd32MRI, kMemRegImm, IS_QUAD_OP | REG_USE02 | IS_LOAD | IS_STORE | SETS_CCODES, { 0, 0, 0x0F, 0xAC, 0, 0, 0, 1, false }, "Shrd32MRI", "[!0r+!1d],!2r,!3d" },
268 { kX86Shld64RRI, kRegRegImmStore, IS_TERTIARY_OP | REG_DEF0_USE01 | SETS_CCODES, { REX_W, 0, 0x0F, 0xA4, 0, 0, 0, 1, false }, "Shld64RRI", "!0r,!1r,!2d" },
269 { kX86Shld64MRI, kMemRegImm, IS_QUAD_OP | REG_USE02 | IS_LOAD | IS_STORE | SETS_CCODES, { REX_W, 0, 0x0F, 0xA4, 0, 0, 0, 1, false }, "Shld64MRI", "[!0r+!1d],!2r,!3d" },
270 { kX86Shrd64RRI, kRegRegImmStore, IS_TERTIARY_OP | REG_DEF0_USE01 | SETS_CCODES, { REX_W, 0, 0x0F, 0xAC, 0, 0, 0, 1, false }, "Shrd64RRI", "!0r,!1r,!2d" },
271 { kX86Shrd64MRI, kMemRegImm, IS_QUAD_OP | REG_USE02 | IS_LOAD | IS_STORE | SETS_CCODES, { REX_W, 0, 0x0F, 0xAC, 0, 0, 0, 1, false }, "Shrd64MRI", "[!0r+!1d],!2r,!3d" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700273 { kX86Test8RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF6, 0, 0, 0, 0, 1, true }, "Test8RI", "!0r,!1d" },
274 { kX86Test8MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF6, 0, 0, 0, 0, 1, true }, "Test8MI", "[!0r+!1d],!2d" },
275 { kX86Test8AI, kArrayImm, IS_LOAD | IS_QUIN_OP | REG_USE01 | SETS_CCODES, { 0, 0, 0xF6, 0, 0, 0, 0, 1, true }, "Test8AI", "[!0r+!1r<<!2d+!3d],!4d" },
276 { kX86Test16RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0x66, 0, 0xF7, 0, 0, 0, 0, 2, false }, "Test16RI", "!0r,!1d" },
277 { kX86Test16MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0x66, 0, 0xF7, 0, 0, 0, 0, 2, false }, "Test16MI", "[!0r+!1d],!2d" },
278 { kX86Test16AI, kArrayImm, IS_LOAD | IS_QUIN_OP | REG_USE01 | SETS_CCODES, { 0x66, 0, 0xF7, 0, 0, 0, 0, 2, false }, "Test16AI", "[!0r+!1r<<!2d+!3d],!4d" },
279 { kX86Test32RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF7, 0, 0, 0, 0, 4, false }, "Test32RI", "!0r,!1d" },
280 { kX86Test32MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF7, 0, 0, 0, 0, 4, false }, "Test32MI", "[!0r+!1d],!2d" },
281 { kX86Test32AI, kArrayImm, IS_LOAD | IS_QUIN_OP | REG_USE01 | SETS_CCODES, { 0, 0, 0xF7, 0, 0, 0, 0, 4, false }, "Test32AI", "[!0r+!1r<<!2d+!3d],!4d" },
282 { kX86Test64RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { REX_W, 0, 0xF7, 0, 0, 0, 0, 4, false }, "Test64RI", "!0r,!1d" },
283 { kX86Test64MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { REX_W, 0, 0xF7, 0, 0, 0, 0, 4, false }, "Test64MI", "[!0r+!1d],!2d" },
284 { kX86Test64AI, kArrayImm, IS_LOAD | IS_QUIN_OP | REG_USE01 | SETS_CCODES, { REX_W, 0, 0xF7, 0, 0, 0, 0, 4, false }, "Test64AI", "[!0r+!1r<<!2d+!3d],!4d" },
Dmitry Petrochenko96992e82014-05-20 04:03:46 +0700285
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700286 { kX86Test32RR, kRegReg, IS_BINARY_OP | REG_USE01 | SETS_CCODES, { 0, 0, 0x85, 0, 0, 0, 0, 0, false }, "Test32RR", "!0r,!1r" },
287 { kX86Test64RR, kRegReg, IS_BINARY_OP | REG_USE01 | SETS_CCODES, { REX_W, 0, 0x85, 0, 0, 0, 0, 0, false }, "Test64RR", "!0r,!1r" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288
289#define UNARY_ENCODING_MAP(opname, modrm, is_store, sets_ccodes, \
290 reg, reg_kind, reg_flags, \
291 mem, mem_kind, mem_flags, \
292 arr, arr_kind, arr_flags, imm, \
293 b_flags, hw_flags, w_flags, \
294 b_format, hw_format, w_format) \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700295{ kX86 ## opname ## 8 ## reg, reg_kind, reg_flags | b_flags | sets_ccodes, { 0, 0, 0xF6, 0, 0, modrm, 0, imm << 0, true }, #opname "8" #reg, b_format "!0r" }, \
296{ 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, true }, #opname "8" #mem, b_format "[!0r+!1d]" }, \
297{ 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, true }, #opname "8" #arr, b_format "[!0r+!1r<<!2d+!3d]" }, \
298{ kX86 ## opname ## 16 ## reg, reg_kind, reg_flags | hw_flags | sets_ccodes, { 0x66, 0, 0xF7, 0, 0, modrm, 0, imm << 1, false }, #opname "16" #reg, hw_format "!0r" }, \
299{ 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, false }, #opname "16" #mem, hw_format "[!0r+!1d]" }, \
300{ 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, false }, #opname "16" #arr, hw_format "[!0r+!1r<<!2d+!3d]" }, \
301{ kX86 ## opname ## 32 ## reg, reg_kind, reg_flags | w_flags | sets_ccodes, { 0, 0, 0xF7, 0, 0, modrm, 0, imm << 2, false }, #opname "32" #reg, w_format "!0r" }, \
302{ 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, false }, #opname "32" #mem, w_format "[!0r+!1d]" }, \
303{ 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, false }, #opname "32" #arr, w_format "[!0r+!1r<<!2d+!3d]" }, \
304{ kX86 ## opname ## 64 ## reg, reg_kind, reg_flags | w_flags | sets_ccodes, { REX_W, 0, 0xF7, 0, 0, modrm, 0, imm << 2, false }, #opname "64" #reg, w_format "!0r" }, \
305{ 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, false }, #opname "64" #mem, w_format "[!0r+!1d]" }, \
306{ 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, false }, #opname "64" #arr, w_format "[!0r+!1r<<!2d+!3d]" }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700307
308 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, "", "", ""),
309 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, "", "", ""),
310
Mark Mendell2bf31e62014-01-23 12:13:40 -0800311 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,"),
312 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,"),
313 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,"),
314 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 -0700315#undef UNARY_ENCODING_MAP
316
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700317 { kx86Cdq32Da, kRegOpcode, NO_OPERAND | REG_DEFAD_USEA, { 0, 0, 0x99, 0, 0, 0, 0, 0, false }, "Cdq", "" },
318 { kx86Cqo64Da, kRegOpcode, NO_OPERAND | REG_DEFAD_USEA, { REX_W, 0, 0x99, 0, 0, 0, 0, 0, false }, "Cqo", "" },
319 { kX86Bswap32R, kRegOpcode, IS_UNARY_OP | REG_DEF0_USE0, { 0, 0, 0x0F, 0xC8, 0, 0, 0, 0, false }, "Bswap32R", "!0r" },
320 { kX86Push32R, kRegOpcode, IS_UNARY_OP | REG_USE0 | REG_USE_SP | REG_DEF_SP | IS_STORE, { 0, 0, 0x50, 0, 0, 0, 0, 0, false }, "Push32R", "!0r" },
321 { kX86Pop32R, kRegOpcode, IS_UNARY_OP | REG_DEF0 | REG_USE_SP | REG_DEF_SP | IS_LOAD, { 0, 0, 0x58, 0, 0, 0, 0, 0, false }, "Pop32R", "!0r" },
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100322
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323#define EXT_0F_ENCODING_MAP(opname, prefix, opcode, reg_def) \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700324{ kX86 ## opname ## RR, kRegReg, IS_BINARY_OP | reg_def | REG_USE1, { prefix, 0, 0x0F, opcode, 0, 0, 0, 0, false }, #opname "RR", "!0r,!1r" }, \
325{ kX86 ## opname ## RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | reg_def | REG_USE1, { prefix, 0, 0x0F, opcode, 0, 0, 0, 0, false }, #opname "RM", "!0r,[!1r+!2d]" }, \
326{ kX86 ## opname ## RA, kRegArray, IS_LOAD | IS_QUIN_OP | reg_def | REG_USE12, { prefix, 0, 0x0F, opcode, 0, 0, 0, 0, false }, #opname "RA", "!0r,[!1r+!2r<<!3d+!4d]" }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700327
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700328#define EXT_0F_REX_W_ENCODING_MAP(opname, prefix, opcode, reg_def) \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700329{ kX86 ## opname ## RR, kRegReg, IS_BINARY_OP | reg_def | REG_USE1, { prefix, REX_W, 0x0F, opcode, 0, 0, 0, 0, false }, #opname "RR", "!0r,!1r" }, \
330{ kX86 ## opname ## RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | reg_def | REG_USE1, { prefix, REX_W, 0x0F, opcode, 0, 0, 0, 0, false }, #opname "RM", "!0r,[!1r+!2d]" }, \
331{ kX86 ## opname ## RA, kRegArray, IS_LOAD | IS_QUIN_OP | reg_def | REG_USE12, { prefix, REX_W, 0x0F, opcode, 0, 0, 0, 0, false }, #opname "RA", "!0r,[!1r+!2r<<!3d+!4d]" }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700332
Mark Mendellfe945782014-05-22 09:52:36 -0400333#define EXT_0F_ENCODING2_MAP(opname, prefix, opcode, opcode2, reg_def) \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700334{ kX86 ## opname ## RR, kRegReg, IS_BINARY_OP | reg_def | REG_USE1, { prefix, 0, 0x0F, opcode, opcode2, 0, 0, 0, false }, #opname "RR", "!0r,!1r" }, \
335{ kX86 ## opname ## RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | reg_def | REG_USE1, { prefix, 0, 0x0F, opcode, opcode2, 0, 0, 0, false }, #opname "RM", "!0r,[!1r+!2d]" }, \
336{ kX86 ## opname ## RA, kRegArray, IS_LOAD | IS_QUIN_OP | reg_def | REG_USE12, { prefix, 0, 0x0F, opcode, opcode2, 0, 0, 0, false }, #opname "RA", "!0r,[!1r+!2r<<!3d+!4d]" }
Mark Mendellfe945782014-05-22 09:52:36 -0400337
Brian Carlstrom7940e442013-07-12 13:46:57 -0700338 EXT_0F_ENCODING_MAP(Movsd, 0xF2, 0x10, REG_DEF0),
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700339 { kX86MovsdMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0xF2, 0, 0x0F, 0x11, 0, 0, 0, 0, false }, "MovsdMR", "[!0r+!1d],!2r" },
340 { kX86MovsdAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0xF2, 0, 0x0F, 0x11, 0, 0, 0, 0, false }, "MovsdAR", "[!0r+!1r<<!2d+!3d],!4r" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341
342 EXT_0F_ENCODING_MAP(Movss, 0xF3, 0x10, REG_DEF0),
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700343 { kX86MovssMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0xF3, 0, 0x0F, 0x11, 0, 0, 0, 0, false }, "MovssMR", "[!0r+!1d],!2r" },
344 { kX86MovssAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0xF3, 0, 0x0F, 0x11, 0, 0, 0, 0, false }, "MovssAR", "[!0r+!1r<<!2d+!3d],!4r" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700345
346 EXT_0F_ENCODING_MAP(Cvtsi2sd, 0xF2, 0x2A, REG_DEF0),
347 EXT_0F_ENCODING_MAP(Cvtsi2ss, 0xF3, 0x2A, REG_DEF0),
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700348 EXT_0F_REX_W_ENCODING_MAP(Cvtsqi2sd, 0xF2, 0x2A, REG_DEF0),
349 EXT_0F_REX_W_ENCODING_MAP(Cvtsqi2ss, 0xF3, 0x2A, REG_DEF0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700350 EXT_0F_ENCODING_MAP(Cvttsd2si, 0xF2, 0x2C, REG_DEF0),
351 EXT_0F_ENCODING_MAP(Cvttss2si, 0xF3, 0x2C, REG_DEF0),
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700352 EXT_0F_REX_W_ENCODING_MAP(Cvttsd2sqi, 0xF2, 0x2C, REG_DEF0),
353 EXT_0F_REX_W_ENCODING_MAP(Cvttss2sqi, 0xF3, 0x2C, REG_DEF0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700354 EXT_0F_ENCODING_MAP(Cvtsd2si, 0xF2, 0x2D, REG_DEF0),
355 EXT_0F_ENCODING_MAP(Cvtss2si, 0xF3, 0x2D, REG_DEF0),
Mark Mendell2637f2e2014-04-30 10:10:47 -0400356 EXT_0F_ENCODING_MAP(Ucomisd, 0x66, 0x2E, SETS_CCODES|REG_USE0),
357 EXT_0F_ENCODING_MAP(Ucomiss, 0x00, 0x2E, SETS_CCODES|REG_USE0),
358 EXT_0F_ENCODING_MAP(Comisd, 0x66, 0x2F, SETS_CCODES|REG_USE0),
359 EXT_0F_ENCODING_MAP(Comiss, 0x00, 0x2F, SETS_CCODES|REG_USE0),
360 EXT_0F_ENCODING_MAP(Orps, 0x00, 0x56, REG_DEF0_USE0),
361 EXT_0F_ENCODING_MAP(Xorps, 0x00, 0x57, REG_DEF0_USE0),
362 EXT_0F_ENCODING_MAP(Addsd, 0xF2, 0x58, REG_DEF0_USE0),
363 EXT_0F_ENCODING_MAP(Addss, 0xF3, 0x58, REG_DEF0_USE0),
364 EXT_0F_ENCODING_MAP(Mulsd, 0xF2, 0x59, REG_DEF0_USE0),
365 EXT_0F_ENCODING_MAP(Mulss, 0xF3, 0x59, REG_DEF0_USE0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700366 EXT_0F_ENCODING_MAP(Cvtsd2ss, 0xF2, 0x5A, REG_DEF0),
367 EXT_0F_ENCODING_MAP(Cvtss2sd, 0xF3, 0x5A, REG_DEF0),
Mark Mendell2637f2e2014-04-30 10:10:47 -0400368 EXT_0F_ENCODING_MAP(Subsd, 0xF2, 0x5C, REG_DEF0_USE0),
369 EXT_0F_ENCODING_MAP(Subss, 0xF3, 0x5C, REG_DEF0_USE0),
370 EXT_0F_ENCODING_MAP(Divsd, 0xF2, 0x5E, REG_DEF0_USE0),
371 EXT_0F_ENCODING_MAP(Divss, 0xF3, 0x5E, REG_DEF0_USE0),
372 EXT_0F_ENCODING_MAP(Punpckldq, 0x66, 0x62, REG_DEF0_USE0),
Mark Mendellfe945782014-05-22 09:52:36 -0400373 EXT_0F_ENCODING_MAP(Sqrtsd, 0xF2, 0x51, REG_DEF0_USE0),
374 EXT_0F_ENCODING2_MAP(Pmulld, 0x66, 0x38, 0x40, REG_DEF0_USE0),
375 EXT_0F_ENCODING_MAP(Pmullw, 0x66, 0xD5, REG_DEF0_USE0),
376 EXT_0F_ENCODING_MAP(Mulps, 0x00, 0x59, REG_DEF0_USE0),
377 EXT_0F_ENCODING_MAP(Mulpd, 0x66, 0x59, REG_DEF0_USE0),
378 EXT_0F_ENCODING_MAP(Paddb, 0x66, 0xFC, REG_DEF0_USE0),
379 EXT_0F_ENCODING_MAP(Paddw, 0x66, 0xFD, REG_DEF0_USE0),
380 EXT_0F_ENCODING_MAP(Paddd, 0x66, 0xFE, REG_DEF0_USE0),
381 EXT_0F_ENCODING_MAP(Addps, 0x00, 0x58, REG_DEF0_USE0),
382 EXT_0F_ENCODING_MAP(Addpd, 0xF2, 0x58, REG_DEF0_USE0),
383 EXT_0F_ENCODING_MAP(Psubb, 0x66, 0xF8, REG_DEF0_USE0),
384 EXT_0F_ENCODING_MAP(Psubw, 0x66, 0xF9, REG_DEF0_USE0),
385 EXT_0F_ENCODING_MAP(Psubd, 0x66, 0xFA, REG_DEF0_USE0),
386 EXT_0F_ENCODING_MAP(Subps, 0x00, 0x5C, REG_DEF0_USE0),
387 EXT_0F_ENCODING_MAP(Subpd, 0x66, 0x5C, REG_DEF0_USE0),
388 EXT_0F_ENCODING_MAP(Pand, 0x66, 0xDB, REG_DEF0_USE0),
389 EXT_0F_ENCODING_MAP(Por, 0x66, 0xEB, REG_DEF0_USE0),
390 EXT_0F_ENCODING_MAP(Pxor, 0x66, 0xEF, REG_DEF0_USE0),
391 EXT_0F_ENCODING2_MAP(Phaddw, 0x66, 0x38, 0x01, REG_DEF0_USE0),
392 EXT_0F_ENCODING2_MAP(Phaddd, 0x66, 0x38, 0x02, REG_DEF0_USE0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700393
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700394 { kX86PextrbRRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0x66, 0, 0x0F, 0x3A, 0x14, 0, 0, 1, false }, "PextbRRI", "!0r,!1r,!2d" },
395 { kX86PextrwRRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0x66, 0, 0x0F, 0xC5, 0x00, 0, 0, 1, false }, "PextwRRI", "!0r,!1r,!2d" },
396 { kX86PextrdRRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0x66, 0, 0x0F, 0x3A, 0x16, 0, 0, 1, false }, "PextdRRI", "!0r,!1r,!2d" },
Mark Mendellfe945782014-05-22 09:52:36 -0400397
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700398 { kX86PshuflwRRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0xF2, 0, 0x0F, 0x70, 0, 0, 0, 1, false }, "PshuflwRRI", "!0r,!1r,!2d" },
399 { kX86PshufdRRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0x66, 0, 0x0F, 0x70, 0, 0, 0, 1, false }, "PshuffRRI", "!0r,!1r,!2d" },
Mark Mendellfe945782014-05-22 09:52:36 -0400400
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700401 { kX86PsrawRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x71, 0, 4, 0, 1, false }, "PsrawRI", "!0r,!1d" },
402 { kX86PsradRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x72, 0, 4, 0, 1, false }, "PsradRI", "!0r,!1d" },
403 { kX86PsrlwRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x71, 0, 2, 0, 1, false }, "PsrlwRI", "!0r,!1d" },
404 { kX86PsrldRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x72, 0, 2, 0, 1, false }, "PsrldRI", "!0r,!1d" },
405 { kX86PsrlqRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x73, 0, 2, 0, 1, false }, "PsrlqRI", "!0r,!1d" },
406 { kX86PsllwRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x71, 0, 6, 0, 1, false }, "PsllwRI", "!0r,!1d" },
407 { kX86PslldRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x72, 0, 6, 0, 1, false }, "PslldRI", "!0r,!1d" },
408 { kX86PsllqRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x73, 0, 6, 0, 1, false }, "PsllqRI", "!0r,!1d" },
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800409
Alexei Zavjalovbd3682e2014-06-12 03:08:01 +0700410 { kX86Fild32M, kMem, IS_LOAD | IS_UNARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xDB, 0x00, 0, 0, 0, 0, false }, "Fild32M", "[!0r,!1d]" },
411 { kX86Fild64M, kMem, IS_LOAD | IS_UNARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xDF, 0x00, 0, 5, 0, 0, false }, "Fild64M", "[!0r,!1d]" },
412 { kX86Fld32M, kMem, IS_LOAD | IS_UNARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xD9, 0x00, 0, 0, 0, 0, false }, "Fld32M", "[!0r,!1d]" },
413 { kX86Fld64M, kMem, IS_LOAD | IS_UNARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xDD, 0x00, 0, 0, 0, 0, false }, "Fld64M", "[!0r,!1d]" },
414 { kX86Fstp32M, kMem, IS_STORE | IS_UNARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xD9, 0x00, 0, 3, 0, 0, false }, "Fstps32M", "[!0r,!1d]" },
415 { kX86Fstp64M, kMem, IS_STORE | IS_UNARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xDD, 0x00, 0, 3, 0, 0, false }, "Fstpd64M", "[!0r,!1d]" },
416 { kX86Fst32M, kMem, IS_STORE | IS_UNARY_OP | REG_USE0, { 0x0, 0, 0xD9, 0x00, 0, 2, 0, 0, false }, "Fsts32M", "[!0r,!1d]" },
417 { kX86Fst64M, kMem, IS_STORE | IS_UNARY_OP | REG_USE0, { 0x0, 0, 0xDD, 0x00, 0, 2, 0, 0, false }, "Fstd64M", "[!0r,!1d]" },
418 { kX86Fprem, kNullary, NO_OPERAND | USE_FP_STACK, { 0xD9, 0, 0xF8, 0, 0, 0, 0, 0, false }, "Fprem64", "" },
419 { kX86Fucompp, kNullary, NO_OPERAND | USE_FP_STACK, { 0xDA, 0, 0xE9, 0, 0, 0, 0, 0, false }, "Fucompp", "" },
420 { kX86Fstsw16R, kNullary, NO_OPERAND, { 0x9B, 0xDF, 0xE0, 0, 0, 0, 0, 0, false }, "Fstsw16R", "ax" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700421
Mark Mendelld65c51a2014-04-29 16:55:20 -0400422 EXT_0F_ENCODING_MAP(Mova128, 0x66, 0x6F, REG_DEF0),
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700423 { kX86Mova128MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, 0, 0x0F, 0x6F, 0, 0, 0, 0, false }, "Mova128MR", "[!0r+!1d],!2r" },
424 { kX86Mova128AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x66, 0, 0x0F, 0x6F, 0, 0, 0, 0, false }, "Mova128AR", "[!0r+!1r<<!2d+!3d],!4r" },
Mark Mendelld65c51a2014-04-29 16:55:20 -0400425
426
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800427 EXT_0F_ENCODING_MAP(Movups, 0x0, 0x10, REG_DEF0),
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700428 { kX86MovupsMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x0, 0, 0x0F, 0x11, 0, 0, 0, 0, false }, "MovupsMR", "[!0r+!1d],!2r" },
429 { kX86MovupsAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x0, 0, 0x0F, 0x11, 0, 0, 0, 0, false }, "MovupsAR", "[!0r+!1r<<!2d+!3d],!4r" },
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800430
431 EXT_0F_ENCODING_MAP(Movaps, 0x0, 0x28, REG_DEF0),
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700432 { kX86MovapsMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x0, 0, 0x0F, 0x29, 0, 0, 0, 0, false }, "MovapsMR", "[!0r+!1d],!2r" },
433 { kX86MovapsAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x0, 0, 0x0F, 0x29, 0, 0, 0, 0, false }, "MovapsAR", "[!0r+!1r<<!2d+!3d],!4r" },
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800434
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700435 { kX86MovlpsRM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0 | REG_USE01, { 0x0, 0, 0x0F, 0x12, 0, 0, 0, 0, false }, "MovlpsRM", "!0r,[!1r+!2d]" },
436 { kX86MovlpsRA, kRegArray, IS_LOAD | IS_QUIN_OP | REG_DEF0 | REG_USE012, { 0x0, 0, 0x0F, 0x12, 0, 0, 0, 0, false }, "MovlpsRA", "!0r,[!1r+!2r<<!3d+!4d]" },
437 { kX86MovlpsMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x0, 0, 0x0F, 0x13, 0, 0, 0, 0, false }, "MovlpsMR", "[!0r+!1d],!2r" },
438 { kX86MovlpsAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x0, 0, 0x0F, 0x13, 0, 0, 0, 0, false }, "MovlpsAR", "[!0r+!1r<<!2d+!3d],!4r" },
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800439
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700440 { kX86MovhpsRM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0 | REG_USE01, { 0x0, 0, 0x0F, 0x16, 0, 0, 0, 0, false }, "MovhpsRM", "!0r,[!1r+!2d]" },
441 { kX86MovhpsRA, kRegArray, IS_LOAD | IS_QUIN_OP | REG_DEF0 | REG_USE012, { 0x0, 0, 0x0F, 0x16, 0, 0, 0, 0, false }, "MovhpsRA", "!0r,[!1r+!2r<<!3d+!4d]" },
442 { kX86MovhpsMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x0, 0, 0x0F, 0x17, 0, 0, 0, 0, false }, "MovhpsMR", "[!0r+!1d],!2r" },
443 { kX86MovhpsAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x0, 0, 0x0F, 0x17, 0, 0, 0, 0, false }, "MovhpsAR", "[!0r+!1r<<!2d+!3d],!4r" },
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800444
Brian Carlstrom7940e442013-07-12 13:46:57 -0700445 EXT_0F_ENCODING_MAP(Movdxr, 0x66, 0x6E, REG_DEF0),
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700446 EXT_0F_REX_W_ENCODING_MAP(Movqxr, 0x66, 0x6E, REG_DEF0),
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700447 { kX86MovqrxRR, kRegRegStore, IS_BINARY_OP | REG_DEF0 | REG_USE1, { 0x66, REX_W, 0x0F, 0x7E, 0, 0, 0, 0, false }, "MovqrxRR", "!0r,!1r" },
448 { kX86MovqrxMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, REX_W, 0x0F, 0x7E, 0, 0, 0, 0, false }, "MovqrxMR", "[!0r+!1d],!2r" },
449 { kX86MovqrxAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x66, REX_W, 0x0F, 0x7E, 0, 0, 0, 0, false }, "MovqrxAR", "[!0r+!1r<<!2d+!3d],!4r" },
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700450
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700451 { kX86MovdrxRR, kRegRegStore, IS_BINARY_OP | REG_DEF0 | REG_USE1, { 0x66, 0, 0x0F, 0x7E, 0, 0, 0, 0, false }, "MovdrxRR", "!0r,!1r" },
452 { kX86MovdrxMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, 0, 0x0F, 0x7E, 0, 0, 0, 0, false }, "MovdrxMR", "[!0r+!1d],!2r" },
453 { kX86MovdrxAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x66, 0, 0x0F, 0x7E, 0, 0, 0, 0, false }, "MovdrxAR", "[!0r+!1r<<!2d+!3d],!4r" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700455 { kX86MovsxdRR, kRegReg, IS_BINARY_OP | REG_DEF0 | REG_USE1, { REX_W, 0, 0x63, 0, 0, 0, 0, 0, false }, "MovsxdRR", "!0r,!1r" },
456 { kX86MovsxdRM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { REX_W, 0, 0x63, 0, 0, 0, 0, 0, false }, "MovsxdRM", "!0r,[!1r+!2d]" },
457 { kX86MovsxdRA, kRegArray, IS_LOAD | IS_QUIN_OP | REG_DEF0 | REG_USE12, { REX_W, 0, 0x63, 0, 0, 0, 0, 0, false }, "MovsxdRA", "!0r,[!1r+!2r<<!3d+!4d]" },
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700458
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700459 { kX86Set8R, kRegCond, IS_BINARY_OP | REG_DEF0 | USES_CCODES, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0, true }, "Set8R", "!1c !0r" },
460 { kX86Set8M, kMemCond, IS_STORE | IS_TERTIARY_OP | REG_USE0 | USES_CCODES, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0, false }, "Set8M", "!2c [!0r+!1d]" },
461 { kX86Set8A, kArrayCond, IS_STORE | IS_QUIN_OP | REG_USE01 | USES_CCODES, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0, false }, "Set8A", "!4c [!0r+!1r<<!2d+!3d]" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700462
463 // TODO: load/store?
464 // Encode the modrm opcode as an extra opcode byte to avoid computation during assembly.
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700465 { kX86Mfence, kReg, NO_OPERAND, { 0, 0, 0x0F, 0xAE, 0, 6, 0, 0, false }, "Mfence", "" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700466
Mark Mendell2637f2e2014-04-30 10:10:47 -0400467 EXT_0F_ENCODING_MAP(Imul16, 0x66, 0xAF, REG_USE0 | REG_DEF0 | SETS_CCODES),
468 EXT_0F_ENCODING_MAP(Imul32, 0x00, 0xAF, REG_USE0 | REG_DEF0 | SETS_CCODES),
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700469 EXT_0F_ENCODING_MAP(Imul64, REX_W, 0xAF, REG_USE0 | REG_DEF0 | SETS_CCODES),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700470
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700471 { kX86CmpxchgRR, kRegRegStore, IS_BINARY_OP | REG_DEF0 | REG_USE01 | REG_DEFA_USEA | SETS_CCODES, { 0, 0, 0x0F, 0xB1, 0, 0, 0, 0, false }, "Cmpxchg", "!0r,!1r" },
472 { kX86CmpxchgMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02 | REG_DEFA_USEA | SETS_CCODES, { 0, 0, 0x0F, 0xB1, 0, 0, 0, 0, false }, "Cmpxchg", "[!0r+!1d],!2r" },
473 { kX86CmpxchgAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014 | REG_DEFA_USEA | SETS_CCODES, { 0, 0, 0x0F, 0xB1, 0, 0, 0, 0, false }, "Cmpxchg", "[!0r+!1r<<!2d+!3d],!4r" },
474 { kX86LockCmpxchgMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02 | REG_DEFA_USEA | SETS_CCODES, { 0xF0, 0, 0x0F, 0xB1, 0, 0, 0, 0, false }, "Lock Cmpxchg", "[!0r+!1d],!2r" },
475 { kX86LockCmpxchgAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014 | REG_DEFA_USEA | SETS_CCODES, { 0xF0, 0, 0x0F, 0xB1, 0, 0, 0, 0, false }, "Lock Cmpxchg", "[!0r+!1r<<!2d+!3d],!4r" },
476 { kX86LockCmpxchg64M, 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, false }, "Lock Cmpxchg8b", "[!0r+!1d]" },
477 { kX86LockCmpxchg64A, 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, false }, "Lock Cmpxchg8b", "[!0r+!1r<<!2d+!3d]" },
478 { kX86XchgMR, kMemReg, IS_STORE | IS_LOAD | IS_TERTIARY_OP | REG_DEF2 | REG_USE02, { 0, 0, 0x87, 0, 0, 0, 0, 0, false }, "Xchg", "[!0r+!1d],!2r" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700479
480 EXT_0F_ENCODING_MAP(Movzx8, 0x00, 0xB6, REG_DEF0),
481 EXT_0F_ENCODING_MAP(Movzx16, 0x00, 0xB7, REG_DEF0),
482 EXT_0F_ENCODING_MAP(Movsx8, 0x00, 0xBE, REG_DEF0),
483 EXT_0F_ENCODING_MAP(Movsx16, 0x00, 0xBF, REG_DEF0),
484#undef EXT_0F_ENCODING_MAP
485
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700486 { kX86Jcc8, kJcc, IS_BINARY_OP | IS_BRANCH | NEEDS_FIXUP | USES_CCODES, { 0, 0, 0x70, 0, 0, 0, 0, 0, false }, "Jcc8", "!1c !0t" },
487 { kX86Jcc32, kJcc, IS_BINARY_OP | IS_BRANCH | NEEDS_FIXUP | USES_CCODES, { 0, 0, 0x0F, 0x80, 0, 0, 0, 0, false }, "Jcc32", "!1c !0t" },
488 { kX86Jmp8, kJmp, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0xEB, 0, 0, 0, 0, 0, false }, "Jmp8", "!0t" },
489 { kX86Jmp32, kJmp, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0xE9, 0, 0, 0, 0, 0, false }, "Jmp32", "!0t" },
490 { kX86JmpR, kJmp, IS_UNARY_OP | IS_BRANCH | REG_USE0, { 0, 0, 0xFF, 0, 0, 4, 0, 0, false }, "JmpR", "!0r" },
491 { kX86Jecxz8, kJmp, NO_OPERAND | IS_BRANCH | NEEDS_FIXUP | REG_USEC, { 0, 0, 0xE3, 0, 0, 0, 0, 0, false }, "Jecxz", "!0t" },
492 { kX86JmpT, kJmp, IS_UNARY_OP | IS_BRANCH | IS_LOAD, { THREAD_PREFIX, 0, 0xFF, 0, 0, 4, 0, 0, false }, "JmpT", "fs:[!0d]" },
493 { kX86CallR, kCall, IS_UNARY_OP | IS_BRANCH | REG_USE0, { 0, 0, 0xE8, 0, 0, 0, 0, 0, false }, "CallR", "!0r" },
494 { kX86CallM, kCall, IS_BINARY_OP | IS_BRANCH | IS_LOAD | REG_USE0, { 0, 0, 0xFF, 0, 0, 2, 0, 0, false }, "CallM", "[!0r+!1d]" },
495 { kX86CallA, kCall, IS_QUAD_OP | IS_BRANCH | IS_LOAD | REG_USE01, { 0, 0, 0xFF, 0, 0, 2, 0, 0, false }, "CallA", "[!0r+!1r<<!2d+!3d]" },
496 { kX86CallT, kCall, IS_UNARY_OP | IS_BRANCH | IS_LOAD, { THREAD_PREFIX, 0, 0xFF, 0, 0, 2, 0, 0, false }, "CallT", "fs:[!0d]" },
497 { kX86CallI, kCall, IS_UNARY_OP | IS_BRANCH, { 0, 0, 0xE8, 0, 0, 0, 0, 4, false }, "CallI", "!0d" },
498 { kX86Ret, kNullary, NO_OPERAND | IS_BRANCH, { 0, 0, 0xC3, 0, 0, 0, 0, 0, false }, "Ret", "" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700500 { kX86StartOfMethod, kMacro, IS_UNARY_OP | SETS_CCODES, { 0, 0, 0, 0, 0, 0, 0, 0, false }, "StartOfMethod", "!0r" },
501 { kX86PcRelLoadRA, kPcRel, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8B, 0, 0, 0, 0, 0, false }, "PcRelLoadRA", "!0r,[!1r+!2r<<!3d+!4p]" },
502 { kX86PcRelAdr, kPcRel, IS_LOAD | IS_BINARY_OP | REG_DEF0, { 0, 0, 0xB8, 0, 0, 0, 0, 4, false }, "PcRelAdr", "!0r,!1d" },
503 { kX86RepneScasw, kNullary, NO_OPERAND | REG_USEA | REG_USEC | SETS_CCODES, { 0x66, 0xF2, 0xAF, 0, 0, 0, 0, 0, false }, "RepNE ScasW", "" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700504};
505
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700506static bool NeedsRex(int32_t raw_reg) {
507 return RegStorage::RegNum(raw_reg) > 7;
508}
509
510static uint8_t LowRegisterBits(int32_t raw_reg) {
511 uint8_t low_reg = RegStorage::RegNum(raw_reg) & kRegNumMask32; // 3 bits
512 DCHECK_LT(low_reg, 8);
513 return low_reg;
514}
515
Ian Rogers5aa6e042014-06-13 16:38:24 -0700516static bool HasModrm(const X86EncodingMap* entry) {
517 switch (entry->kind) {
518 case kNullary: return false;
519 case kRegOpcode: return false;
520 default: return true;
521 }
522}
523
524static bool HasSib(const X86EncodingMap* entry) {
525 switch (entry->kind) {
526 case kArray: return true;
527 case kArrayReg: return true;
528 case kRegArray: return true;
529 case kArrayImm: return true;
530 case kRegArrayImm: return true;
531 case kShiftArrayImm: return true;
532 case kShiftArrayCl: return true;
533 case kArrayCond: return true;
534 case kCall:
535 switch (entry->opcode) {
536 case kX86CallA: return true;
537 default: return false;
538 }
539 case kPcRel: return true;
540 switch (entry->opcode) {
541 case kX86PcRelLoadRA: return true;
542 default: return false;
543 }
544 default: return false;
545 }
546}
547
548static bool ModrmIsRegReg(const X86EncodingMap* entry) {
549 switch (entry->kind) {
550 // There is no modrm for this kind of instruction, therefore the reg doesn't form part of the
551 // modrm:
552 case kNullary: return true;
553 case kRegOpcode: return true;
554 case kMovRegImm: return true;
555 // Regular modrm value of 3 cases, when there is one register the other register holds an
556 // opcode so the base register is special.
557 case kReg: return true;
558 case kRegReg: return true;
559 case kRegRegStore: return true;
560 case kRegImm: return true;
561 case kRegRegImm: return true;
562 case kRegRegImmStore: return true;
563 case kShiftRegImm: return true;
564 case kShiftRegCl: return true;
565 case kRegCond: return true;
566 case kRegRegCond: return true;
567 case kJmp:
568 switch (entry->opcode) {
569 case kX86JmpR: return true;
570 default: return false;
571 }
572 case kCall:
573 switch (entry->opcode) {
574 case kX86CallR: return true;
575 default: return false;
576 }
577 default: return false;
578 }
579}
580
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700581size_t X86Mir2Lir::ComputeSize(const X86EncodingMap* entry, int32_t raw_reg, int32_t raw_index,
Ian Rogers5aa6e042014-06-13 16:38:24 -0700582 int32_t raw_base, int32_t displacement) {
583 bool has_modrm = HasModrm(entry);
584 bool has_sib = HasSib(entry);
585 bool r8_form = entry->skeleton.r8_form;
586 bool modrm_is_reg_reg = ModrmIsRegReg(entry);
587 if (has_sib) {
588 DCHECK(!modrm_is_reg_reg);
589 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700590 size_t size = 0;
591 if (entry->skeleton.prefix1 > 0) {
592 ++size;
593 if (entry->skeleton.prefix2 > 0) {
594 ++size;
595 }
596 }
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700597 if (Gen64Bit() || kIsDebugBuild) {
Ian Rogers5aa6e042014-06-13 16:38:24 -0700598 bool registers_need_rex_prefix = NeedsRex(raw_reg) || NeedsRex(raw_index) || NeedsRex(raw_base);
599 if (r8_form) {
600 // Do we need an empty REX prefix to normalize byte registers?
601 registers_need_rex_prefix = registers_need_rex_prefix || (RegStorage::RegNum(raw_reg) >= 4);
602 registers_need_rex_prefix = registers_need_rex_prefix ||
603 (modrm_is_reg_reg && (RegStorage::RegNum(raw_base) >= 4));
604 }
605 if (registers_need_rex_prefix) {
606 DCHECK(Gen64Bit()) << "Attempt to use a 64-bit only addressable register "
607 << RegStorage::RegNum(raw_reg) << " with instruction " << entry->name;
608 if (entry->skeleton.prefix1 != REX_W && entry->skeleton.prefix2 != REX_W) {
609 ++size; // rex
610 }
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700611 }
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700612 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700613 ++size; // opcode
614 if (entry->skeleton.opcode == 0x0F) {
615 ++size;
616 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
617 ++size;
618 }
619 }
Ian Rogers5aa6e042014-06-13 16:38:24 -0700620 if (has_modrm) {
621 ++size; // modrm
Brian Carlstrom7940e442013-07-12 13:46:57 -0700622 }
Ian Rogers5aa6e042014-06-13 16:38:24 -0700623 if (!modrm_is_reg_reg) {
624 if (has_sib || LowRegisterBits(raw_base) == rs_rX86_SP.GetRegNum()
625 || (Gen64Bit() && entry->skeleton.prefix1 == THREAD_PREFIX)) {
626 // SP requires a SIB byte.
627 // GS access also needs a SIB byte for absolute adressing in 64-bit mode.
628 ++size;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700629 }
Ian Rogers5aa6e042014-06-13 16:38:24 -0700630 if (displacement != 0 || LowRegisterBits(raw_base) == rs_rBP.GetRegNum()) {
631 // BP requires an explicit displacement, even when it's 0.
632 if (entry->opcode != kX86Lea32RA && entry->opcode != kX86Lea64RA) {
633 DCHECK_NE(entry->flags & (IS_LOAD | IS_STORE), UINT64_C(0)) << entry->name;
634 }
635 size += IS_SIMM8(displacement) ? 1 : 4;
636 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637 }
638 size += entry->skeleton.immediate_bytes;
639 return size;
640}
641
Ian Rogers5aa6e042014-06-13 16:38:24 -0700642size_t X86Mir2Lir::GetInsnSize(LIR* lir) {
buzbee409fe942013-10-11 10:49:56 -0700643 DCHECK(!IsPseudoLirOp(lir->opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700644 const X86EncodingMap* entry = &X86Mir2Lir::EncodingMap[lir->opcode];
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700645 DCHECK_EQ(entry->opcode, lir->opcode) << entry->name;
Ian Rogers5aa6e042014-06-13 16:38:24 -0700646
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647 switch (entry->kind) {
648 case kData:
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700649 return 4; // 4 bytes of data.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650 case kNop:
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700651 return lir->operands[0]; // Length of nop is sole operand.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700652 case kNullary:
Ian Rogers5aa6e042014-06-13 16:38:24 -0700653 return ComputeSize(entry, NO_REG, NO_REG, NO_REG, 0);
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100654 case kRegOpcode: // lir operands - 0: reg
Ian Rogers5aa6e042014-06-13 16:38:24 -0700655 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700656 case kReg: // lir operands - 0: reg
Ian Rogers5aa6e042014-06-13 16:38:24 -0700657 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700658 case kMem: // lir operands - 0: base, 1: disp
Ian Rogers5aa6e042014-06-13 16:38:24 -0700659 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], lir->operands[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700660 case kArray: // lir operands - 0: base, 1: index, 2: scale, 3: disp
Ian Rogers5aa6e042014-06-13 16:38:24 -0700661 return ComputeSize(entry, NO_REG, lir->operands[1], lir->operands[0], lir->operands[3]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700662 case kMemReg: // lir operands - 0: base, 1: disp, 2: reg
Ian Rogers5aa6e042014-06-13 16:38:24 -0700663 return ComputeSize(entry, lir->operands[2], NO_REG, lir->operands[0], lir->operands[1]);
Mark Mendell2637f2e2014-04-30 10:10:47 -0400664 case kMemRegImm: // lir operands - 0: base, 1: disp, 2: reg 3: immediate
Ian Rogers5aa6e042014-06-13 16:38:24 -0700665 return ComputeSize(entry, lir->operands[2], NO_REG, lir->operands[0], lir->operands[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700666 case kArrayReg: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700667 return ComputeSize(entry, lir->operands[4], lir->operands[1], lir->operands[0],
Ian Rogers5aa6e042014-06-13 16:38:24 -0700668 lir->operands[3]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669 case kThreadReg: // lir operands - 0: disp, 1: reg
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700670 // Thread displacement size is always 32bit.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700671 return ComputeSize(entry, lir->operands[1], NO_REG, NO_REG, 0x12345678);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700672 case kRegReg: // lir operands - 0: reg1, 1: reg2
Ian Rogers5aa6e042014-06-13 16:38:24 -0700673 return ComputeSize(entry, lir->operands[0], NO_REG, lir->operands[1], 0);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700674 case kRegRegStore: // lir operands - 0: reg2, 1: reg1
Ian Rogers5aa6e042014-06-13 16:38:24 -0700675 return ComputeSize(entry, lir->operands[1], NO_REG, lir->operands[0], 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700676 case kRegMem: // lir operands - 0: reg, 1: base, 2: disp
Ian Rogers5aa6e042014-06-13 16:38:24 -0700677 return ComputeSize(entry, lir->operands[0], NO_REG, lir->operands[1], lir->operands[2]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700678 case kRegArray: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700679 return ComputeSize(entry, lir->operands[0], lir->operands[2], lir->operands[1],
Ian Rogers5aa6e042014-06-13 16:38:24 -0700680 lir->operands[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700681 case kRegThread: // lir operands - 0: reg, 1: disp
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700682 // Thread displacement size is always 32bit.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700683 return ComputeSize(entry, lir->operands[0], NO_REG, NO_REG, 0x12345678);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700684 case kRegImm: { // lir operands - 0: reg, 1: immediate
Ian Rogers5aa6e042014-06-13 16:38:24 -0700685 size_t size = ComputeSize(entry, lir->operands[0], NO_REG, NO_REG, 0);
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700686 // AX opcodes don't require the modrm byte.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700687 if (entry->skeleton.ax_opcode == 0) {
688 return size;
689 } else {
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700690 return size - (RegStorage::RegNum(lir->operands[0]) == rs_rAX.GetRegNum() ? 1 : 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700691 }
692 }
693 case kMemImm: // lir operands - 0: base, 1: disp, 2: immediate
Ian Rogers5aa6e042014-06-13 16:38:24 -0700694 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], lir->operands[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700695 case kArrayImm: // lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
Ian Rogers5aa6e042014-06-13 16:38:24 -0700696 return ComputeSize(entry, NO_REG, lir->operands[1], lir->operands[0], lir->operands[3]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697 case kThreadImm: // lir operands - 0: disp, 1: imm
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700698 // Thread displacement size is always 32bit.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700699 return ComputeSize(entry, NO_REG, NO_REG, NO_REG, 0x12345678);
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700700 case kRegRegImm: // lir operands - 0: reg1, 1: reg2, 2: imm
701 // Note: RegRegImm form passes reg2 as index but encodes it using base.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700702 return ComputeSize(entry, lir->operands[0], lir->operands[1], NO_REG, 0);
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700703 case kRegRegImmStore: // lir operands - 0: reg2, 1: reg1, 2: imm
704 // Note: RegRegImmStore form passes reg1 as index but encodes it using base.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700705 return ComputeSize(entry, lir->operands[1], lir->operands[0], NO_REG, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700706 case kRegMemImm: // lir operands - 0: reg, 1: base, 2: disp, 3: imm
Ian Rogers5aa6e042014-06-13 16:38:24 -0700707 return ComputeSize(entry, lir->operands[0], NO_REG, lir->operands[1], lir->operands[2]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700708 case kRegArrayImm: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp, 5: imm
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700709 return ComputeSize(entry, lir->operands[0], lir->operands[2], lir->operands[1],
Ian Rogers5aa6e042014-06-13 16:38:24 -0700710 lir->operands[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700711 case kMovRegImm: // lir operands - 0: reg, 1: immediate
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700712 return ((entry->skeleton.prefix1 != 0 || NeedsRex(lir->operands[0])) ? 1 : 0) + 1 +
713 entry->skeleton.immediate_bytes;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700714 case kShiftRegImm: // lir operands - 0: reg, 1: immediate
715 // Shift by immediate one has a shorter opcode.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700716 return ComputeSize(entry, lir->operands[0], NO_REG, NO_REG, 0) -
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700717 (lir->operands[1] == 1 ? 1 : 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700718 case kShiftMemImm: // lir operands - 0: base, 1: disp, 2: immediate
719 // Shift by immediate one has a shorter opcode.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700720 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], lir->operands[1]) -
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700721 (lir->operands[2] == 1 ? 1 : 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700722 case kShiftArrayImm: // lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
723 // Shift by immediate one has a shorter opcode.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700724 return ComputeSize(entry, NO_REG, lir->operands[1], lir->operands[0], lir->operands[3]) -
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700725 (lir->operands[4] == 1 ? 1 : 0);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700726 case kShiftRegCl: // lir operands - 0: reg, 1: cl
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700727 DCHECK_EQ(rs_rCX.GetRegNum(), RegStorage::RegNum(lir->operands[1]));
728 // Note: ShiftRegCl form passes reg as reg but encodes it using base.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700729 return ComputeSize(entry, lir->operands[0], NO_REG, NO_REG, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700730 case kShiftMemCl: // lir operands - 0: base, 1: disp, 2: cl
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700731 DCHECK_EQ(rs_rCX.GetRegNum(), RegStorage::RegNum(lir->operands[2]));
Ian Rogers5aa6e042014-06-13 16:38:24 -0700732 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], lir->operands[1]);
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700733 case kShiftArrayCl: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: cl
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700734 DCHECK_EQ(rs_rCX.GetRegNum(), RegStorage::RegNum(lir->operands[4]));
735 return ComputeSize(entry, lir->operands[4], lir->operands[1], lir->operands[0],
Ian Rogers5aa6e042014-06-13 16:38:24 -0700736 lir->operands[3]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700737 case kRegCond: // lir operands - 0: reg, 1: cond
Ian Rogers5aa6e042014-06-13 16:38:24 -0700738 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700739 case kMemCond: // lir operands - 0: base, 1: disp, 2: cond
Ian Rogers5aa6e042014-06-13 16:38:24 -0700740 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], lir->operands[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700741 case kArrayCond: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: cond
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700742 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -0700743 return ComputeSize(entry, NO_REG, lir->operands[1], lir->operands[0], lir->operands[3]);
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700744 case kRegRegCond: // lir operands - 0: reg1, 1: reg2, 2: cond
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700745 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -0700746 return ComputeSize(entry, lir->operands[0], NO_REG, lir->operands[1], 0);
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700747 case kRegMemCond: // lir operands - 0: reg, 1: base, 2: disp, 3:cond
748 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -0700749 return ComputeSize(entry, lir->operands[0], NO_REG, lir->operands[1], lir->operands[2]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700750 case kJcc:
751 if (lir->opcode == kX86Jcc8) {
752 return 2; // opcode + rel8
753 } else {
754 DCHECK(lir->opcode == kX86Jcc32);
755 return 6; // 2 byte opcode + rel32
756 }
757 case kJmp:
Mark Mendell4028a6c2014-02-19 20:06:20 -0800758 if (lir->opcode == kX86Jmp8 || lir->opcode == kX86Jecxz8) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700759 return 2; // opcode + rel8
760 } else if (lir->opcode == kX86Jmp32) {
761 return 5; // opcode + rel32
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700762 } else if (lir->opcode == kX86JmpT) {
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700763 // Thread displacement size is always 32bit.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700764 return ComputeSize(entry, NO_REG, NO_REG, NO_REG, 0x12345678);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700765 } else {
766 DCHECK(lir->opcode == kX86JmpR);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700767 if (NeedsRex(lir->operands[0])) {
768 return 3; // REX.B + opcode + modrm
769 } else {
770 return 2; // opcode + modrm
771 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700772 }
773 case kCall:
774 switch (lir->opcode) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800775 case kX86CallI: return 5; // opcode 0:disp
Brian Carlstrom7940e442013-07-12 13:46:57 -0700776 case kX86CallR: return 2; // opcode modrm
777 case kX86CallM: // lir operands - 0: base, 1: disp
Ian Rogers5aa6e042014-06-13 16:38:24 -0700778 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], lir->operands[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700779 case kX86CallA: // lir operands - 0: base, 1: index, 2: scale, 3: disp
Ian Rogers5aa6e042014-06-13 16:38:24 -0700780 return ComputeSize(entry, NO_REG, lir->operands[1], lir->operands[0], lir->operands[3]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700781 case kX86CallT: // lir operands - 0: disp
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700782 // Thread displacement size is always 32bit.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700783 return ComputeSize(entry, NO_REG, NO_REG, NO_REG, 0x12345678);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700784 default:
785 break;
786 }
787 break;
788 case kPcRel:
789 if (entry->opcode == kX86PcRelLoadRA) {
790 // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700791 // Force the displacement size to 32bit, it will hold a computed offset later.
792 return ComputeSize(entry, lir->operands[0], lir->operands[2], lir->operands[1],
Ian Rogers5aa6e042014-06-13 16:38:24 -0700793 0x12345678);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700794 } else {
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700795 DCHECK_EQ(entry->opcode, kX86PcRelAdr);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700796 return 5; // opcode with reg + 4 byte immediate
Brian Carlstrom7940e442013-07-12 13:46:57 -0700797 }
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700798 case kMacro: // lir operands - 0: reg
Brian Carlstrom7940e442013-07-12 13:46:57 -0700799 DCHECK_EQ(lir->opcode, static_cast<int>(kX86StartOfMethod));
800 return 5 /* call opcode + 4 byte displacement */ + 1 /* pop reg */ +
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700801 ComputeSize(&X86Mir2Lir::EncodingMap[Gen64Bit() ? kX86Sub64RI : kX86Sub32RI],
Ian Rogers5aa6e042014-06-13 16:38:24 -0700802 lir->operands[0], NO_REG, NO_REG, 0) -
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700803 // Shorter ax encoding.
804 (RegStorage::RegNum(lir->operands[0]) == rs_rAX.GetRegNum() ? 1 : 0);
805 case kUnimplemented:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700806 break;
807 }
808 UNIMPLEMENTED(FATAL) << "Unimplemented size encoding for: " << entry->name;
809 return 0;
810}
811
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700812static uint8_t ModrmForDisp(int base, int disp) {
813 // BP requires an explicit disp, so do not omit it in the 0 case
814 if (disp == 0 && RegStorage::RegNum(base) != rs_rBP.GetRegNum()) {
815 return 0;
816 } else if (IS_SIMM8(disp)) {
817 return 1;
818 } else {
819 return 2;
820 }
821}
822
823void X86Mir2Lir::CheckValidByteRegister(const X86EncodingMap* entry, int32_t raw_reg) {
824 if (kIsDebugBuild) {
825 // Sanity check r8_form is correctly specified.
826 if (entry->skeleton.r8_form) {
827 CHECK(strchr(entry->name, '8') != nullptr) << entry->name;
828 } else {
829 if (entry->skeleton.immediate_bytes != 1) { // Ignore ...I8 instructions.
830 if (!StartsWith(entry->name, "Movzx8") && !StartsWith(entry->name, "Movsx8")) {
831 CHECK(strchr(entry->name, '8') == nullptr) << entry->name;
832 }
833 }
834 }
835 if (RegStorage::RegNum(raw_reg) >= 4) {
836 // ah, bh, ch and dh are not valid registers in 32-bit.
837 CHECK(Gen64Bit() || !entry->skeleton.r8_form)
838 << "Invalid register " << static_cast<int>(RegStorage::RegNum(raw_reg))
839 << " for instruction " << entry->name << " in "
840 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
841 }
842 }
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700843}
844
845void X86Mir2Lir::EmitPrefix(const X86EncodingMap* entry,
Ian Rogers5aa6e042014-06-13 16:38:24 -0700846 int32_t raw_reg_r, int32_t raw_reg_x, int32_t raw_reg_b) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700847 // REX.WRXB
848 // W - 64-bit operand
849 // R - MODRM.reg
850 // X - SIB.index
851 // B - MODRM.rm/SIB.base
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700852 bool w = (entry->skeleton.prefix1 == REX_W) || (entry->skeleton.prefix2 == REX_W);
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700853 bool r = NeedsRex(raw_reg_r);
854 bool x = NeedsRex(raw_reg_x);
855 bool b = NeedsRex(raw_reg_b);
Ian Rogers5aa6e042014-06-13 16:38:24 -0700856 bool r8_form = entry->skeleton.r8_form;
857 bool modrm_is_reg_reg = ModrmIsRegReg(entry);
858
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700859 uint8_t rex = 0;
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700860 if (r8_form) {
861 // Do we need an empty REX prefix to normalize byte register addressing?
862 if (RegStorage::RegNum(raw_reg_r) >= 4) {
863 rex |= 0x40; // REX.0000
864 } else if (modrm_is_reg_reg && RegStorage::RegNum(raw_reg_b) >= 4) {
865 rex |= 0x40; // REX.0000
866 }
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700867 }
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700868 if (w) {
869 rex |= 0x48; // REX.W000
870 }
871 if (r) {
872 rex |= 0x44; // REX.0R00
873 }
874 if (x) {
875 rex |= 0x42; // REX.00X0
876 }
877 if (b) {
878 rex |= 0x41; // REX.000B
879 }
Vladimir Marko057c74a2013-12-03 15:20:45 +0000880 if (entry->skeleton.prefix1 != 0) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700881 if (Gen64Bit() && entry->skeleton.prefix1 == THREAD_PREFIX) {
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700882 // 64 bit addresses by GS, not FS.
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700883 code_buffer_.push_back(THREAD_PREFIX_GS);
884 } else {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700885 if (entry->skeleton.prefix1 == REX_W) {
886 rex |= entry->skeleton.prefix1;
887 code_buffer_.push_back(rex);
888 rex = 0;
889 } else {
890 code_buffer_.push_back(entry->skeleton.prefix1);
891 }
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700892 }
Vladimir Marko057c74a2013-12-03 15:20:45 +0000893 if (entry->skeleton.prefix2 != 0) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700894 if (entry->skeleton.prefix2 == REX_W) {
895 rex |= entry->skeleton.prefix2;
896 code_buffer_.push_back(rex);
897 rex = 0;
898 } else {
899 code_buffer_.push_back(entry->skeleton.prefix2);
900 }
Vladimir Marko057c74a2013-12-03 15:20:45 +0000901 }
902 } else {
903 DCHECK_EQ(0, entry->skeleton.prefix2);
904 }
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700905 if (rex != 0) {
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700906 DCHECK(Gen64Bit());
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700907 code_buffer_.push_back(rex);
908 }
Vladimir Marko057c74a2013-12-03 15:20:45 +0000909}
910
911void X86Mir2Lir::EmitOpcode(const X86EncodingMap* entry) {
912 code_buffer_.push_back(entry->skeleton.opcode);
913 if (entry->skeleton.opcode == 0x0F) {
914 code_buffer_.push_back(entry->skeleton.extra_opcode1);
915 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
916 code_buffer_.push_back(entry->skeleton.extra_opcode2);
917 } else {
918 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
919 }
920 } else {
921 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
922 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
923 }
924}
925
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700926void X86Mir2Lir::EmitPrefixAndOpcode(const X86EncodingMap* entry,
Ian Rogers5aa6e042014-06-13 16:38:24 -0700927 int32_t raw_reg_r, int32_t raw_reg_x, int32_t raw_reg_b) {
928 EmitPrefix(entry, raw_reg_r, raw_reg_x, raw_reg_b);
Vladimir Marko057c74a2013-12-03 15:20:45 +0000929 EmitOpcode(entry);
930}
931
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700932void X86Mir2Lir::EmitDisp(uint8_t base, int32_t disp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700933 // BP requires an explicit disp, so do not omit it in the 0 case
buzbee091cc402014-03-31 10:14:40 -0700934 if (disp == 0 && RegStorage::RegNum(base) != rs_rBP.GetRegNum()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700935 return;
936 } else if (IS_SIMM8(disp)) {
937 code_buffer_.push_back(disp & 0xFF);
938 } else {
939 code_buffer_.push_back(disp & 0xFF);
940 code_buffer_.push_back((disp >> 8) & 0xFF);
941 code_buffer_.push_back((disp >> 16) & 0xFF);
942 code_buffer_.push_back((disp >> 24) & 0xFF);
943 }
944}
945
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700946void X86Mir2Lir::EmitModrmThread(uint8_t reg_or_opcode) {
947 if (Gen64Bit()) {
948 // Absolute adressing for GS access.
949 uint8_t modrm = (0 << 6) | (reg_or_opcode << 3) | rs_rX86_SP.GetRegNum();
950 code_buffer_.push_back(modrm);
951 uint8_t sib = (0/*TIMES_1*/ << 6) | (rs_rX86_SP.GetRegNum() << 3) | rs_rBP.GetRegNum();
952 code_buffer_.push_back(sib);
953 } else {
954 uint8_t modrm = (0 << 6) | (reg_or_opcode << 3) | rs_rBP.GetRegNum();
955 code_buffer_.push_back(modrm);
956 }
957}
958
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700959void X86Mir2Lir::EmitModrmDisp(uint8_t reg_or_opcode, uint8_t base, int32_t disp) {
960 DCHECK_LT(reg_or_opcode, 8);
961 DCHECK_LT(base, 8);
962 uint8_t modrm = (ModrmForDisp(base, disp) << 6) | (reg_or_opcode << 3) | base;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700963 code_buffer_.push_back(modrm);
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700964 if (base == rs_rX86_SP.GetRegNum()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700965 // Special SIB for SP base
buzbee091cc402014-03-31 10:14:40 -0700966 code_buffer_.push_back(0 << 6 | rs_rX86_SP.GetRegNum() << 3 | rs_rX86_SP.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700967 }
968 EmitDisp(base, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700969}
970
Vladimir Marko057c74a2013-12-03 15:20:45 +0000971void X86Mir2Lir::EmitModrmSibDisp(uint8_t reg_or_opcode, uint8_t base, uint8_t index,
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700972 int scale, int32_t disp) {
buzbee091cc402014-03-31 10:14:40 -0700973 DCHECK_LT(RegStorage::RegNum(reg_or_opcode), 8);
974 uint8_t modrm = (ModrmForDisp(base, disp) << 6) | RegStorage::RegNum(reg_or_opcode) << 3 |
975 rs_rX86_SP.GetRegNum();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700976 code_buffer_.push_back(modrm);
977 DCHECK_LT(scale, 4);
buzbee091cc402014-03-31 10:14:40 -0700978 DCHECK_LT(RegStorage::RegNum(index), 8);
979 DCHECK_LT(RegStorage::RegNum(base), 8);
980 uint8_t sib = (scale << 6) | (RegStorage::RegNum(index) << 3) | RegStorage::RegNum(base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700981 code_buffer_.push_back(sib);
982 EmitDisp(base, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700983}
984
Dmitry Petrochenko96992e82014-05-20 04:03:46 +0700985void X86Mir2Lir::EmitImm(const X86EncodingMap* entry, int64_t imm) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700986 switch (entry->skeleton.immediate_bytes) {
987 case 1:
988 DCHECK(IS_SIMM8(imm));
989 code_buffer_.push_back(imm & 0xFF);
990 break;
991 case 2:
992 DCHECK(IS_SIMM16(imm));
993 code_buffer_.push_back(imm & 0xFF);
994 code_buffer_.push_back((imm >> 8) & 0xFF);
995 break;
996 case 4:
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700997 DCHECK(IS_SIMM32(imm));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700998 code_buffer_.push_back(imm & 0xFF);
999 code_buffer_.push_back((imm >> 8) & 0xFF);
1000 code_buffer_.push_back((imm >> 16) & 0xFF);
1001 code_buffer_.push_back((imm >> 24) & 0xFF);
1002 break;
Dmitry Petrochenko96992e82014-05-20 04:03:46 +07001003 case 8:
1004 code_buffer_.push_back(imm & 0xFF);
1005 code_buffer_.push_back((imm >> 8) & 0xFF);
1006 code_buffer_.push_back((imm >> 16) & 0xFF);
1007 code_buffer_.push_back((imm >> 24) & 0xFF);
1008 code_buffer_.push_back((imm >> 32) & 0xFF);
1009 code_buffer_.push_back((imm >> 40) & 0xFF);
1010 code_buffer_.push_back((imm >> 48) & 0xFF);
1011 code_buffer_.push_back((imm >> 56) & 0xFF);
1012 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 default:
1014 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
1015 << ") for instruction: " << entry->name;
1016 break;
1017 }
1018}
1019
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001020void X86Mir2Lir::EmitNullary(const X86EncodingMap* entry) {
1021 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001022 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, NO_REG);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001023 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001024 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1025 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1026}
1027
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001028void X86Mir2Lir::EmitOpRegOpcode(const X86EncodingMap* entry, int32_t raw_reg) {
1029 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001030 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, raw_reg);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001031 // There's no 3-byte instruction with +rd
1032 DCHECK(entry->skeleton.opcode != 0x0F ||
1033 (entry->skeleton.extra_opcode1 != 0x38 && entry->skeleton.extra_opcode1 != 0x3A));
1034 DCHECK(!RegStorage::IsFloat(raw_reg));
1035 uint8_t low_reg = LowRegisterBits(raw_reg);
1036 code_buffer_.back() += low_reg;
1037 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1038 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1039}
1040
1041void X86Mir2Lir::EmitOpReg(const X86EncodingMap* entry, int32_t raw_reg) {
1042 CheckValidByteRegister(entry, raw_reg);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001043 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, raw_reg);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001044 uint8_t low_reg = LowRegisterBits(raw_reg);
1045 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | low_reg;
Vladimir Marko057c74a2013-12-03 15:20:45 +00001046 code_buffer_.push_back(modrm);
1047 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1048 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1049}
1050
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001051void X86Mir2Lir::EmitOpMem(const X86EncodingMap* entry, int32_t raw_base, int32_t disp) {
1052 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001053 EmitPrefix(entry, NO_REG, NO_REG, raw_base);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001054 code_buffer_.push_back(entry->skeleton.opcode);
1055 DCHECK_NE(0x0F, entry->skeleton.opcode);
1056 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1057 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001058 uint8_t low_base = LowRegisterBits(raw_base);
1059 EmitModrmDisp(entry->skeleton.modrm_opcode, low_base, disp);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001060 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1061 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1062}
1063
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001064void X86Mir2Lir::EmitOpArray(const X86EncodingMap* entry, int32_t raw_base, int32_t raw_index,
1065 int scale, int32_t disp) {
1066 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001067 EmitPrefixAndOpcode(entry, NO_REG, raw_index, raw_base);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001068 uint8_t low_index = LowRegisterBits(raw_index);
1069 uint8_t low_base = LowRegisterBits(raw_base);
1070 EmitModrmSibDisp(entry->skeleton.modrm_opcode, low_base, low_index, scale, disp);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001071 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1072 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1073}
1074
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001075void X86Mir2Lir::EmitMemReg(const X86EncodingMap* entry, int32_t raw_base, int32_t disp,
1076 int32_t raw_reg) {
1077 CheckValidByteRegister(entry, raw_reg);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001078 EmitPrefixAndOpcode(entry, raw_reg, NO_REG, raw_base);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001079 uint8_t low_reg = LowRegisterBits(raw_reg);
1080 uint8_t low_base = LowRegisterBits(raw_base);
1081 EmitModrmDisp(low_reg, low_base, disp);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001082 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1083 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1084 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1085}
1086
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001087void X86Mir2Lir::EmitRegMem(const X86EncodingMap* entry, int32_t raw_reg, int32_t raw_base,
1088 int32_t disp) {
Vladimir Marko057c74a2013-12-03 15:20:45 +00001089 // Opcode will flip operands.
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001090 EmitMemReg(entry, raw_base, disp, raw_reg);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001091}
1092
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001093void X86Mir2Lir::EmitRegArray(const X86EncodingMap* entry, int32_t raw_reg, int32_t raw_base,
1094 int32_t raw_index, int scale, int32_t disp) {
1095 CheckValidByteRegister(entry, raw_reg);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001096 EmitPrefixAndOpcode(entry, raw_reg, raw_index, raw_base);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001097 uint8_t low_reg = LowRegisterBits(raw_reg);
1098 uint8_t low_index = LowRegisterBits(raw_index);
1099 uint8_t low_base = LowRegisterBits(raw_base);
1100 EmitModrmSibDisp(low_reg, low_base, low_index, scale, disp);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001101 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1102 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1103 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1104}
1105
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001106void X86Mir2Lir::EmitArrayReg(const X86EncodingMap* entry, int32_t raw_base, int32_t raw_index,
1107 int scale, int32_t disp, int32_t raw_reg) {
Vladimir Marko057c74a2013-12-03 15:20:45 +00001108 // Opcode will flip operands.
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001109 EmitRegArray(entry, raw_reg, raw_base, raw_index, scale, disp);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001110}
1111
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001112void X86Mir2Lir::EmitMemImm(const X86EncodingMap* entry, int32_t raw_base, int32_t disp,
1113 int32_t imm) {
1114 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001115 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, raw_base);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001116 uint8_t low_base = LowRegisterBits(raw_base);
1117 EmitModrmDisp(entry->skeleton.modrm_opcode, low_base, disp);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001118 DCHECK_EQ(0, entry->skeleton.ax_opcode);
Mark Mendell9ed42772014-05-07 17:26:12 -04001119 EmitImm(entry, imm);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001120}
1121
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001122void X86Mir2Lir::EmitArrayImm(const X86EncodingMap* entry,
1123 int32_t raw_base, int32_t raw_index, int scale, int32_t disp,
1124 int32_t imm) {
1125 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001126 EmitPrefixAndOpcode(entry, NO_REG, raw_index, raw_base);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001127 uint8_t low_index = LowRegisterBits(raw_index);
1128 uint8_t low_base = LowRegisterBits(raw_base);
1129 EmitModrmSibDisp(entry->skeleton.modrm_opcode, low_base, low_index, scale, disp);
1130 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1131 EmitImm(entry, imm);
1132}
1133
1134void X86Mir2Lir::EmitRegThread(const X86EncodingMap* entry, int32_t raw_reg, int32_t disp) {
1135 DCHECK_EQ(false, entry->skeleton.r8_form);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001136 DCHECK_NE(entry->skeleton.prefix1, 0);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001137 EmitPrefixAndOpcode(entry, raw_reg, NO_REG, NO_REG);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001138 uint8_t low_reg = LowRegisterBits(raw_reg);
1139 EmitModrmThread(low_reg);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001140 code_buffer_.push_back(disp & 0xFF);
1141 code_buffer_.push_back((disp >> 8) & 0xFF);
1142 code_buffer_.push_back((disp >> 16) & 0xFF);
1143 code_buffer_.push_back((disp >> 24) & 0xFF);
1144 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1145 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1146 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1147}
1148
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001149void X86Mir2Lir::EmitRegReg(const X86EncodingMap* entry, int32_t raw_reg1, int32_t raw_reg2) {
1150 CheckValidByteRegister(entry, raw_reg1);
1151 CheckValidByteRegister(entry, raw_reg2);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001152 EmitPrefixAndOpcode(entry, raw_reg1, NO_REG, raw_reg2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001153 uint8_t low_reg1 = LowRegisterBits(raw_reg1);
1154 uint8_t low_reg2 = LowRegisterBits(raw_reg2);
1155 uint8_t modrm = (3 << 6) | (low_reg1 << 3) | low_reg2;
Vladimir Marko057c74a2013-12-03 15:20:45 +00001156 code_buffer_.push_back(modrm);
1157 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1158 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1159 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1160}
1161
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001162void X86Mir2Lir::EmitRegRegImm(const X86EncodingMap* entry, int32_t raw_reg1, int32_t raw_reg2,
1163 int32_t imm) {
1164 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001165 EmitPrefixAndOpcode(entry, raw_reg1, NO_REG, raw_reg2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001166 uint8_t low_reg1 = LowRegisterBits(raw_reg1);
1167 uint8_t low_reg2 = LowRegisterBits(raw_reg2);
1168 uint8_t modrm = (3 << 6) | (low_reg1 << 3) | low_reg2;
Vladimir Marko057c74a2013-12-03 15:20:45 +00001169 code_buffer_.push_back(modrm);
1170 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1171 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1172 EmitImm(entry, imm);
1173}
1174
Mark Mendell4708dcd2014-01-22 09:05:18 -08001175void X86Mir2Lir::EmitRegMemImm(const X86EncodingMap* entry,
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001176 int32_t raw_reg, int32_t raw_base, int disp, int32_t imm) {
1177 DCHECK(!RegStorage::IsFloat(raw_reg));
1178 CheckValidByteRegister(entry, raw_reg);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001179 EmitPrefixAndOpcode(entry, raw_reg, NO_REG, raw_base);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001180 uint8_t low_reg = LowRegisterBits(raw_reg);
1181 uint8_t low_base = LowRegisterBits(raw_base);
1182 EmitModrmDisp(low_reg, low_base, disp);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001183 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1184 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1185 EmitImm(entry, imm);
1186}
1187
Mark Mendell2637f2e2014-04-30 10:10:47 -04001188void X86Mir2Lir::EmitMemRegImm(const X86EncodingMap* entry,
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001189 int32_t raw_base, int32_t disp, int32_t raw_reg, int32_t imm) {
1190 // Opcode will flip operands.
1191 EmitRegMemImm(entry, raw_reg, raw_base, disp, imm);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001192}
1193
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001194void X86Mir2Lir::EmitRegImm(const X86EncodingMap* entry, int32_t raw_reg, int32_t imm) {
1195 CheckValidByteRegister(entry, raw_reg);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001196 EmitPrefix(entry, NO_REG, NO_REG, raw_reg);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001197 if (RegStorage::RegNum(raw_reg) == rs_rAX.GetRegNum() && entry->skeleton.ax_opcode != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 code_buffer_.push_back(entry->skeleton.ax_opcode);
1199 } else {
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001200 uint8_t low_reg = LowRegisterBits(raw_reg);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001201 EmitOpcode(entry);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001202 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | low_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001203 code_buffer_.push_back(modrm);
1204 }
Vladimir Marko057c74a2013-12-03 15:20:45 +00001205 EmitImm(entry, imm);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001206}
1207
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001208void X86Mir2Lir::EmitThreadImm(const X86EncodingMap* entry, int32_t disp, int32_t imm) {
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07001209 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001210 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, NO_REG);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001211 EmitModrmThread(entry->skeleton.modrm_opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001212 code_buffer_.push_back(disp & 0xFF);
1213 code_buffer_.push_back((disp >> 8) & 0xFF);
1214 code_buffer_.push_back((disp >> 16) & 0xFF);
1215 code_buffer_.push_back((disp >> 24) & 0xFF);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001216 EmitImm(entry, imm);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001217 DCHECK_EQ(entry->skeleton.ax_opcode, 0);
1218}
1219
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001220void X86Mir2Lir::EmitMovRegImm(const X86EncodingMap* entry, int32_t raw_reg, int64_t imm) {
1221 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001222 EmitPrefix(entry, NO_REG, NO_REG, raw_reg);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001223 uint8_t low_reg = LowRegisterBits(raw_reg);
1224 code_buffer_.push_back(0xB8 + low_reg);
Dmitry Petrochenko96992e82014-05-20 04:03:46 +07001225 switch (entry->skeleton.immediate_bytes) {
1226 case 4:
1227 code_buffer_.push_back(imm & 0xFF);
1228 code_buffer_.push_back((imm >> 8) & 0xFF);
1229 code_buffer_.push_back((imm >> 16) & 0xFF);
1230 code_buffer_.push_back((imm >> 24) & 0xFF);
1231 break;
1232 case 8:
1233 code_buffer_.push_back(imm & 0xFF);
1234 code_buffer_.push_back((imm >> 8) & 0xFF);
1235 code_buffer_.push_back((imm >> 16) & 0xFF);
1236 code_buffer_.push_back((imm >> 24) & 0xFF);
1237 code_buffer_.push_back((imm >> 32) & 0xFF);
1238 code_buffer_.push_back((imm >> 40) & 0xFF);
1239 code_buffer_.push_back((imm >> 48) & 0xFF);
1240 code_buffer_.push_back((imm >> 56) & 0xFF);
1241 break;
1242 default:
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001243 LOG(FATAL) << "Unsupported immediate size for EmitMovRegImm: "
1244 << static_cast<uint32_t>(entry->skeleton.immediate_bytes);
Dmitry Petrochenko96992e82014-05-20 04:03:46 +07001245 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001246}
1247
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001248void X86Mir2Lir::EmitShiftRegImm(const X86EncodingMap* entry, int32_t raw_reg, int32_t imm) {
1249 CheckValidByteRegister(entry, raw_reg);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001250 EmitPrefix(entry, NO_REG, NO_REG, raw_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001251 if (imm != 1) {
1252 code_buffer_.push_back(entry->skeleton.opcode);
1253 } else {
1254 // Shorter encoding for 1 bit shift
1255 code_buffer_.push_back(entry->skeleton.ax_opcode);
1256 }
Vladimir Marko057c74a2013-12-03 15:20:45 +00001257 DCHECK_NE(0x0F, entry->skeleton.opcode);
1258 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1259 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001260 uint8_t low_reg = LowRegisterBits(raw_reg);
1261 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | low_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001262 code_buffer_.push_back(modrm);
1263 if (imm != 1) {
1264 DCHECK_EQ(entry->skeleton.immediate_bytes, 1);
1265 DCHECK(IS_SIMM8(imm));
1266 code_buffer_.push_back(imm & 0xFF);
1267 }
1268}
1269
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001270void X86Mir2Lir::EmitShiftRegCl(const X86EncodingMap* entry, int32_t raw_reg, int32_t raw_cl) {
1271 CheckValidByteRegister(entry, raw_reg);
1272 DCHECK_EQ(rs_rCX.GetRegNum(), RegStorage::RegNum(raw_cl));
Ian Rogers5aa6e042014-06-13 16:38:24 -07001273 EmitPrefix(entry, NO_REG, NO_REG, raw_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001274 code_buffer_.push_back(entry->skeleton.opcode);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001275 DCHECK_NE(0x0F, entry->skeleton.opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001276 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1277 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001278 uint8_t low_reg = LowRegisterBits(raw_reg);
1279 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | low_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001280 code_buffer_.push_back(modrm);
1281 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1282 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1283}
1284
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001285void X86Mir2Lir::EmitShiftMemCl(const X86EncodingMap* entry, int32_t raw_base,
1286 int32_t displacement, int32_t raw_cl) {
1287 DCHECK_EQ(false, entry->skeleton.r8_form);
1288 DCHECK_EQ(rs_rCX.GetRegNum(), RegStorage::RegNum(raw_cl));
Ian Rogers5aa6e042014-06-13 16:38:24 -07001289 EmitPrefix(entry, NO_REG, NO_REG, raw_base);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001290 code_buffer_.push_back(entry->skeleton.opcode);
1291 DCHECK_NE(0x0F, entry->skeleton.opcode);
1292 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1293 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001294 uint8_t low_base = LowRegisterBits(raw_base);
1295 EmitModrmDisp(entry->skeleton.modrm_opcode, low_base, displacement);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001296 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1297 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1298}
1299
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001300void X86Mir2Lir::EmitShiftMemImm(const X86EncodingMap* entry, int32_t raw_base, int32_t disp,
1301 int32_t imm) {
1302 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001303 EmitPrefix(entry, NO_REG, NO_REG, raw_base);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001304 if (imm != 1) {
1305 code_buffer_.push_back(entry->skeleton.opcode);
1306 } else {
1307 // Shorter encoding for 1 bit shift
1308 code_buffer_.push_back(entry->skeleton.ax_opcode);
1309 }
1310 DCHECK_NE(0x0F, entry->skeleton.opcode);
1311 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1312 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001313 uint8_t low_base = LowRegisterBits(raw_base);
1314 EmitModrmDisp(entry->skeleton.modrm_opcode, low_base, disp);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001315 if (imm != 1) {
1316 DCHECK_EQ(entry->skeleton.immediate_bytes, 1);
1317 DCHECK(IS_SIMM8(imm));
1318 code_buffer_.push_back(imm & 0xFF);
1319 }
1320}
1321
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001322void X86Mir2Lir::EmitRegCond(const X86EncodingMap* entry, int32_t raw_reg, int32_t cc) {
1323 CheckValidByteRegister(entry, raw_reg);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001324 EmitPrefix(entry, NO_REG, NO_REG, raw_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001325 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1326 DCHECK_EQ(0x0F, entry->skeleton.opcode);
1327 code_buffer_.push_back(0x0F);
1328 DCHECK_EQ(0x90, entry->skeleton.extra_opcode1);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001329 DCHECK_GE(cc, 0);
1330 DCHECK_LT(cc, 16);
1331 code_buffer_.push_back(0x90 | cc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001332 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001333 uint8_t low_reg = LowRegisterBits(raw_reg);
1334 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | low_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001335 code_buffer_.push_back(modrm);
1336 DCHECK_EQ(entry->skeleton.immediate_bytes, 0);
1337}
1338
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001339void X86Mir2Lir::EmitMemCond(const X86EncodingMap* entry, int32_t raw_base, int32_t disp,
1340 int32_t cc) {
1341 DCHECK_EQ(false, entry->skeleton.r8_form);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001342 if (entry->skeleton.prefix1 != 0) {
1343 code_buffer_.push_back(entry->skeleton.prefix1);
1344 if (entry->skeleton.prefix2 != 0) {
1345 code_buffer_.push_back(entry->skeleton.prefix2);
1346 }
1347 } else {
1348 DCHECK_EQ(0, entry->skeleton.prefix2);
1349 }
1350 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1351 DCHECK_EQ(0x0F, entry->skeleton.opcode);
1352 code_buffer_.push_back(0x0F);
1353 DCHECK_EQ(0x90, entry->skeleton.extra_opcode1);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001354 DCHECK_GE(cc, 0);
1355 DCHECK_LT(cc, 16);
1356 code_buffer_.push_back(0x90 | cc);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001357 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001358 uint8_t low_base = LowRegisterBits(raw_base);
1359 EmitModrmDisp(entry->skeleton.modrm_opcode, low_base, disp);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001360 DCHECK_EQ(entry->skeleton.immediate_bytes, 0);
1361}
1362
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001363void X86Mir2Lir::EmitRegRegCond(const X86EncodingMap* entry, int32_t raw_reg1, int32_t raw_reg2,
1364 int32_t cc) {
1365 // Generate prefix and opcode without the condition.
1366 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001367 EmitPrefixAndOpcode(entry, raw_reg1, NO_REG, raw_reg2);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001368
1369 // Now add the condition. The last byte of opcode is the one that receives it.
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001370 DCHECK_GE(cc, 0);
1371 DCHECK_LT(cc, 16);
1372 code_buffer_.back() += cc;
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001373
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001374 // Not expecting to have to encode immediate or do anything special for ModR/M since there are
1375 // two registers.
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001376 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1377 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1378
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001379 // For register to register encoding, the mod is 3.
1380 const uint8_t mod = (3 << 6);
1381
1382 // Encode the ModR/M byte now.
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001383 uint8_t low_reg1 = LowRegisterBits(raw_reg1);
1384 uint8_t low_reg2 = LowRegisterBits(raw_reg2);
1385 const uint8_t modrm = mod | (low_reg1 << 3) | low_reg2;
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001386 code_buffer_.push_back(modrm);
1387}
1388
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001389void X86Mir2Lir::EmitRegMemCond(const X86EncodingMap* entry, int32_t raw_reg1, int32_t raw_base,
1390 int32_t disp, int32_t cc) {
1391 // Generate prefix and opcode without the condition.
1392 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001393 EmitPrefixAndOpcode(entry, raw_reg1, NO_REG, raw_base);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001394
1395 // Now add the condition. The last byte of opcode is the one that receives it.
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001396 DCHECK_GE(cc, 0);
1397 DCHECK_LT(cc, 16);
1398 code_buffer_.back() += cc;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001399
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001400 // Not expecting to have to encode immediate or do anything special for ModR/M since there are
1401 // two registers.
Mark Mendell2637f2e2014-04-30 10:10:47 -04001402 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1403 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1404
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001405 uint8_t low_reg1 = LowRegisterBits(raw_reg1);
1406 uint8_t low_base = LowRegisterBits(raw_base);
1407 EmitModrmDisp(low_reg1, low_base, disp);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001408}
1409
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001410void X86Mir2Lir::EmitJmp(const X86EncodingMap* entry, int32_t rel) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001411 if (entry->opcode == kX86Jmp8) {
1412 DCHECK(IS_SIMM8(rel));
1413 code_buffer_.push_back(0xEB);
1414 code_buffer_.push_back(rel & 0xFF);
1415 } else if (entry->opcode == kX86Jmp32) {
1416 code_buffer_.push_back(0xE9);
1417 code_buffer_.push_back(rel & 0xFF);
1418 code_buffer_.push_back((rel >> 8) & 0xFF);
1419 code_buffer_.push_back((rel >> 16) & 0xFF);
1420 code_buffer_.push_back((rel >> 24) & 0xFF);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001421 } else if (entry->opcode == kX86Jecxz8) {
1422 DCHECK(IS_SIMM8(rel));
1423 code_buffer_.push_back(0xE3);
1424 code_buffer_.push_back(rel & 0xFF);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001425 } else {
1426 DCHECK(entry->opcode == kX86JmpR);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001427 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001428 EmitPrefix(entry, NO_REG, NO_REG, rel);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001429 code_buffer_.push_back(entry->skeleton.opcode);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001430 uint8_t low_reg = LowRegisterBits(rel);
1431 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | low_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001432 code_buffer_.push_back(modrm);
1433 }
1434}
1435
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001436void X86Mir2Lir::EmitJcc(const X86EncodingMap* entry, int32_t rel, int32_t cc) {
1437 DCHECK_GE(cc, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001438 DCHECK_LT(cc, 16);
1439 if (entry->opcode == kX86Jcc8) {
1440 DCHECK(IS_SIMM8(rel));
1441 code_buffer_.push_back(0x70 | cc);
1442 code_buffer_.push_back(rel & 0xFF);
1443 } else {
1444 DCHECK(entry->opcode == kX86Jcc32);
1445 code_buffer_.push_back(0x0F);
1446 code_buffer_.push_back(0x80 | cc);
1447 code_buffer_.push_back(rel & 0xFF);
1448 code_buffer_.push_back((rel >> 8) & 0xFF);
1449 code_buffer_.push_back((rel >> 16) & 0xFF);
1450 code_buffer_.push_back((rel >> 24) & 0xFF);
1451 }
1452}
1453
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001454void X86Mir2Lir::EmitCallMem(const X86EncodingMap* entry, int32_t raw_base, int32_t disp) {
1455 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001456 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, raw_base);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001457 uint8_t low_base = LowRegisterBits(raw_base);
1458 EmitModrmDisp(entry->skeleton.modrm_opcode, low_base, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001459 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1460 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1461}
1462
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001463void X86Mir2Lir::EmitCallImmediate(const X86EncodingMap* entry, int32_t disp) {
1464 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001465 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, NO_REG);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001466 DCHECK_EQ(4, entry->skeleton.immediate_bytes);
1467 code_buffer_.push_back(disp & 0xFF);
1468 code_buffer_.push_back((disp >> 8) & 0xFF);
1469 code_buffer_.push_back((disp >> 16) & 0xFF);
1470 code_buffer_.push_back((disp >> 24) & 0xFF);
1471 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1472}
1473
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001474void X86Mir2Lir::EmitCallThread(const X86EncodingMap* entry, int32_t disp) {
1475 DCHECK_EQ(false, entry->skeleton.r8_form);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001476 DCHECK_NE(entry->skeleton.prefix1, 0);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001477 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, NO_REG);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001478 EmitModrmThread(entry->skeleton.modrm_opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001479 code_buffer_.push_back(disp & 0xFF);
1480 code_buffer_.push_back((disp >> 8) & 0xFF);
1481 code_buffer_.push_back((disp >> 16) & 0xFF);
1482 code_buffer_.push_back((disp >> 24) & 0xFF);
1483 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1484 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1485}
1486
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001487void X86Mir2Lir::EmitPcRel(const X86EncodingMap* entry, int32_t raw_reg, int32_t raw_base_or_table,
1488 int32_t raw_index, int scale, int32_t table_or_disp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001489 int disp;
1490 if (entry->opcode == kX86PcRelLoadRA) {
buzbee0d829482013-10-11 15:24:55 -07001491 Mir2Lir::EmbeddedData *tab_rec =
1492 reinterpret_cast<Mir2Lir::EmbeddedData*>(UnwrapPointer(table_or_disp));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001493 disp = tab_rec->offset;
1494 } else {
1495 DCHECK(entry->opcode == kX86PcRelAdr);
buzbee0d829482013-10-11 15:24:55 -07001496 Mir2Lir::EmbeddedData *tab_rec =
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001497 reinterpret_cast<Mir2Lir::EmbeddedData*>(UnwrapPointer(raw_base_or_table));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001498 disp = tab_rec->offset;
1499 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001500 if (entry->opcode == kX86PcRelLoadRA) {
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001501 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001502 EmitPrefix(entry, raw_reg, raw_index, raw_base_or_table);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001503 code_buffer_.push_back(entry->skeleton.opcode);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001504 DCHECK_NE(0x0F, entry->skeleton.opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001505 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1506 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001507 uint8_t low_reg = LowRegisterBits(raw_reg);
1508 uint8_t modrm = (2 << 6) | (low_reg << 3) | rs_rX86_SP.GetRegNum();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001509 code_buffer_.push_back(modrm);
1510 DCHECK_LT(scale, 4);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001511 uint8_t low_base_or_table = LowRegisterBits(raw_base_or_table);
1512 uint8_t low_index = LowRegisterBits(raw_index);
1513 uint8_t sib = (scale << 6) | (low_index << 3) | low_base_or_table;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001514 code_buffer_.push_back(sib);
1515 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1516 } else {
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001517 uint8_t low_reg = LowRegisterBits(raw_reg);
1518 code_buffer_.push_back(entry->skeleton.opcode + low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001519 }
1520 code_buffer_.push_back(disp & 0xFF);
1521 code_buffer_.push_back((disp >> 8) & 0xFF);
1522 code_buffer_.push_back((disp >> 16) & 0xFF);
1523 code_buffer_.push_back((disp >> 24) & 0xFF);
1524 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1525 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1526}
1527
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001528void X86Mir2Lir::EmitMacro(const X86EncodingMap* entry, int32_t raw_reg, int32_t offset) {
1529 DCHECK_EQ(entry->opcode, kX86StartOfMethod) << entry->name;
1530 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001531 EmitPrefix(entry, raw_reg, NO_REG, NO_REG);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001532 code_buffer_.push_back(0xE8); // call +0
1533 code_buffer_.push_back(0);
1534 code_buffer_.push_back(0);
1535 code_buffer_.push_back(0);
1536 code_buffer_.push_back(0);
1537
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001538 uint8_t low_reg = LowRegisterBits(raw_reg);
1539 code_buffer_.push_back(0x58 + low_reg); // pop reg
Brian Carlstrom7940e442013-07-12 13:46:57 -07001540
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07001541 EmitRegImm(&X86Mir2Lir::EncodingMap[Gen64Bit() ? kX86Sub64RI : kX86Sub32RI],
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001542 raw_reg, offset + 5 /* size of call +0 */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001543}
1544
1545void X86Mir2Lir::EmitUnimplemented(const X86EncodingMap* entry, LIR* lir) {
1546 UNIMPLEMENTED(WARNING) << "encoding kind for " << entry->name << " "
1547 << BuildInsnString(entry->fmt, lir, 0);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001548 for (size_t i = 0; i < GetInsnSize(lir); ++i) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001549 code_buffer_.push_back(0xCC); // push breakpoint instruction - int 3
1550 }
1551}
1552
1553/*
1554 * Assemble the LIR into binary instruction format. Note that we may
1555 * discover that pc-relative displacements may not fit the selected
1556 * instruction. In those cases we will try to substitute a new code
1557 * sequence or request that the trace be shortened and retried.
1558 */
buzbee0d829482013-10-11 15:24:55 -07001559AssemblerStatus X86Mir2Lir::AssembleInstructions(CodeOffset start_addr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001560 LIR *lir;
1561 AssemblerStatus res = kSuccess; // Assume success
1562
1563 const bool kVerbosePcFixup = false;
1564 for (lir = first_lir_insn_; lir != NULL; lir = NEXT_LIR(lir)) {
buzbee409fe942013-10-11 10:49:56 -07001565 if (IsPseudoLirOp(lir->opcode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001566 continue;
1567 }
1568
1569 if (lir->flags.is_nop) {
1570 continue;
1571 }
1572
buzbeeb48819d2013-09-14 16:15:25 -07001573 if (lir->flags.fixup != kFixupNone) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001574 switch (lir->opcode) {
1575 case kX86Jcc8: {
1576 LIR *target_lir = lir->target;
1577 DCHECK(target_lir != NULL);
1578 int delta = 0;
buzbee0d829482013-10-11 15:24:55 -07001579 CodeOffset pc;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001580 if (IS_SIMM8(lir->operands[0])) {
1581 pc = lir->offset + 2 /* opcode + rel8 */;
1582 } else {
1583 pc = lir->offset + 6 /* 2 byte opcode + rel32 */;
1584 }
buzbee0d829482013-10-11 15:24:55 -07001585 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001586 delta = target - pc;
1587 if (IS_SIMM8(delta) != IS_SIMM8(lir->operands[0])) {
1588 if (kVerbosePcFixup) {
1589 LOG(INFO) << "Retry for JCC growth at " << lir->offset
1590 << " delta: " << delta << " old delta: " << lir->operands[0];
1591 }
1592 lir->opcode = kX86Jcc32;
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001593 lir->flags.size = GetInsnSize(lir);
1594 DCHECK(lir->u.m.def_mask->Equals(kEncodeAll));
1595 DCHECK(lir->u.m.use_mask->Equals(kEncodeAll));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001596 res = kRetryAll;
1597 }
1598 if (kVerbosePcFixup) {
1599 LOG(INFO) << "Source:";
1600 DumpLIRInsn(lir, 0);
1601 LOG(INFO) << "Target:";
1602 DumpLIRInsn(target_lir, 0);
1603 LOG(INFO) << "Delta " << delta;
1604 }
1605 lir->operands[0] = delta;
1606 break;
1607 }
1608 case kX86Jcc32: {
1609 LIR *target_lir = lir->target;
1610 DCHECK(target_lir != NULL);
buzbee0d829482013-10-11 15:24:55 -07001611 CodeOffset pc = lir->offset + 6 /* 2 byte opcode + rel32 */;
1612 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001613 int delta = target - pc;
1614 if (kVerbosePcFixup) {
1615 LOG(INFO) << "Source:";
1616 DumpLIRInsn(lir, 0);
1617 LOG(INFO) << "Target:";
1618 DumpLIRInsn(target_lir, 0);
1619 LOG(INFO) << "Delta " << delta;
1620 }
1621 lir->operands[0] = delta;
1622 break;
1623 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001624 case kX86Jecxz8: {
1625 LIR *target_lir = lir->target;
1626 DCHECK(target_lir != NULL);
1627 CodeOffset pc;
1628 pc = lir->offset + 2; // opcode + rel8
1629 CodeOffset target = target_lir->offset;
1630 int delta = target - pc;
1631 lir->operands[0] = delta;
1632 DCHECK(IS_SIMM8(delta));
1633 break;
1634 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001635 case kX86Jmp8: {
1636 LIR *target_lir = lir->target;
1637 DCHECK(target_lir != NULL);
1638 int delta = 0;
buzbee0d829482013-10-11 15:24:55 -07001639 CodeOffset pc;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001640 if (IS_SIMM8(lir->operands[0])) {
1641 pc = lir->offset + 2 /* opcode + rel8 */;
1642 } else {
1643 pc = lir->offset + 5 /* opcode + rel32 */;
1644 }
buzbee0d829482013-10-11 15:24:55 -07001645 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001646 delta = target - pc;
1647 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && delta == 0) {
1648 // Useless branch
buzbee252254b2013-09-08 16:20:53 -07001649 NopLIR(lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001650 if (kVerbosePcFixup) {
1651 LOG(INFO) << "Retry for useless branch at " << lir->offset;
1652 }
1653 res = kRetryAll;
1654 } else if (IS_SIMM8(delta) != IS_SIMM8(lir->operands[0])) {
1655 if (kVerbosePcFixup) {
1656 LOG(INFO) << "Retry for JMP growth at " << lir->offset;
1657 }
1658 lir->opcode = kX86Jmp32;
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001659 lir->flags.size = GetInsnSize(lir);
1660 DCHECK(lir->u.m.def_mask->Equals(kEncodeAll));
1661 DCHECK(lir->u.m.use_mask->Equals(kEncodeAll));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001662 res = kRetryAll;
1663 }
1664 lir->operands[0] = delta;
1665 break;
1666 }
1667 case kX86Jmp32: {
1668 LIR *target_lir = lir->target;
1669 DCHECK(target_lir != NULL);
buzbee0d829482013-10-11 15:24:55 -07001670 CodeOffset pc = lir->offset + 5 /* opcode + rel32 */;
1671 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001672 int delta = target - pc;
1673 lir->operands[0] = delta;
1674 break;
1675 }
1676 default:
Mark Mendell67c39c42014-01-31 17:28:00 -08001677 if (lir->flags.fixup == kFixupLoad) {
1678 LIR *target_lir = lir->target;
1679 DCHECK(target_lir != NULL);
1680 CodeOffset target = target_lir->offset;
1681 lir->operands[2] = target;
1682 int newSize = GetInsnSize(lir);
1683 if (newSize != lir->flags.size) {
1684 lir->flags.size = newSize;
1685 res = kRetryAll;
1686 }
1687 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001688 break;
1689 }
1690 }
1691
1692 /*
1693 * If one of the pc-relative instructions expanded we'll have
1694 * to make another pass. Don't bother to fully assemble the
1695 * instruction.
1696 */
1697 if (res != kSuccess) {
1698 continue;
1699 }
1700 CHECK_EQ(static_cast<size_t>(lir->offset), code_buffer_.size());
1701 const X86EncodingMap *entry = &X86Mir2Lir::EncodingMap[lir->opcode];
1702 size_t starting_cbuf_size = code_buffer_.size();
1703 switch (entry->kind) {
1704 case kData: // 4 bytes of data
1705 code_buffer_.push_back(lir->operands[0]);
1706 break;
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001707 case kNullary: // 1 byte of opcode and possible prefixes.
1708 EmitNullary(entry);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001709 break;
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001710 case kRegOpcode: // lir operands - 0: reg
1711 EmitOpRegOpcode(entry, lir->operands[0]);
1712 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001713 case kReg: // lir operands - 0: reg
1714 EmitOpReg(entry, lir->operands[0]);
1715 break;
1716 case kMem: // lir operands - 0: base, 1: disp
1717 EmitOpMem(entry, lir->operands[0], lir->operands[1]);
1718 break;
Vladimir Marko057c74a2013-12-03 15:20:45 +00001719 case kArray: // lir operands - 0: base, 1: index, 2: scale, 3: disp
1720 EmitOpArray(entry, lir->operands[0], lir->operands[1], lir->operands[2], lir->operands[3]);
1721 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001722 case kMemReg: // lir operands - 0: base, 1: disp, 2: reg
1723 EmitMemReg(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1724 break;
Mark Mendell343adb52013-12-18 06:02:17 -08001725 case kMemImm: // lir operands - 0: base, 1: disp, 2: immediate
1726 EmitMemImm(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1727 break;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001728 case kArrayImm: // lir operands - 0: base, 1: index, 2: disp, 3:scale, 4:immediate
1729 EmitArrayImm(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1730 lir->operands[3], lir->operands[4]);
1731 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001732 case kArrayReg: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
1733 EmitArrayReg(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1734 lir->operands[3], lir->operands[4]);
1735 break;
1736 case kRegMem: // lir operands - 0: reg, 1: base, 2: disp
1737 EmitRegMem(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1738 break;
1739 case kRegArray: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
1740 EmitRegArray(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1741 lir->operands[3], lir->operands[4]);
1742 break;
1743 case kRegThread: // lir operands - 0: reg, 1: disp
1744 EmitRegThread(entry, lir->operands[0], lir->operands[1]);
1745 break;
1746 case kRegReg: // lir operands - 0: reg1, 1: reg2
1747 EmitRegReg(entry, lir->operands[0], lir->operands[1]);
1748 break;
1749 case kRegRegStore: // lir operands - 0: reg2, 1: reg1
1750 EmitRegReg(entry, lir->operands[1], lir->operands[0]);
1751 break;
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001752 case kMemRegImm: // lir operands - 0: base, 1: disp, 2: reg 3: immediate
Mark Mendell2637f2e2014-04-30 10:10:47 -04001753 EmitMemRegImm(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1754 lir->operands[3]);
1755 break;
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001756 case kRegRegImm: // lir operands - 0: reg1, 1: reg2, 2: imm
Brian Carlstrom7940e442013-07-12 13:46:57 -07001757 EmitRegRegImm(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1758 break;
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001759 case kRegRegImmStore: // lir operands - 0: reg2, 1: reg1, 2: imm
1760 EmitRegRegImm(entry, lir->operands[1], lir->operands[0], lir->operands[2]);
1761 break;
1762 case kRegMemImm: // lir operands - 0: reg, 1: base, 2: disp, 3: imm
Mark Mendell4708dcd2014-01-22 09:05:18 -08001763 EmitRegMemImm(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1764 lir->operands[3]);
1765 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001766 case kRegImm: // lir operands - 0: reg, 1: immediate
1767 EmitRegImm(entry, lir->operands[0], lir->operands[1]);
1768 break;
1769 case kThreadImm: // lir operands - 0: disp, 1: immediate
1770 EmitThreadImm(entry, lir->operands[0], lir->operands[1]);
1771 break;
1772 case kMovRegImm: // lir operands - 0: reg, 1: immediate
1773 EmitMovRegImm(entry, lir->operands[0], lir->operands[1]);
1774 break;
1775 case kShiftRegImm: // lir operands - 0: reg, 1: immediate
1776 EmitShiftRegImm(entry, lir->operands[0], lir->operands[1]);
1777 break;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001778 case kShiftMemImm: // lir operands - 0: base, 1: disp, 2:immediate
1779 EmitShiftMemImm(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1780 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001781 case kShiftRegCl: // lir operands - 0: reg, 1: cl
Brian Carlstrom7940e442013-07-12 13:46:57 -07001782 EmitShiftRegCl(entry, lir->operands[0], lir->operands[1]);
1783 break;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001784 case kShiftMemCl: // lir operands - 0: base, 1:displacement, 2: cl
1785 EmitShiftMemCl(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1786 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001787 case kRegCond: // lir operands - 0: reg, 1: condition
1788 EmitRegCond(entry, lir->operands[0], lir->operands[1]);
1789 break;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001790 case kMemCond: // lir operands - 0: base, 1: displacement, 2: condition
1791 EmitMemCond(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1792 break;
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001793 case kRegRegCond: // lir operands - 0: reg, 1: reg, 2: condition
1794 EmitRegRegCond(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1795 break;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001796 case kRegMemCond: // lir operands - 0: reg, 1: reg, displacement, 3: condition
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001797 EmitRegMemCond(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1798 lir->operands[3]);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001799 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001800 case kJmp: // lir operands - 0: rel
Brian Carlstrom60d7a652014-03-13 18:10:08 -07001801 if (entry->opcode == kX86JmpT) {
1802 // This works since the instruction format for jmp and call is basically the same and
1803 // EmitCallThread loads opcode info.
1804 EmitCallThread(entry, lir->operands[0]);
1805 } else {
1806 EmitJmp(entry, lir->operands[0]);
1807 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001808 break;
1809 case kJcc: // lir operands - 0: rel, 1: CC, target assigned
1810 EmitJcc(entry, lir->operands[0], lir->operands[1]);
1811 break;
1812 case kCall:
1813 switch (entry->opcode) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001814 case kX86CallI: // lir operands - 0: disp
1815 EmitCallImmediate(entry, lir->operands[0]);
1816 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001817 case kX86CallM: // lir operands - 0: base, 1: disp
1818 EmitCallMem(entry, lir->operands[0], lir->operands[1]);
1819 break;
1820 case kX86CallT: // lir operands - 0: disp
1821 EmitCallThread(entry, lir->operands[0]);
1822 break;
1823 default:
1824 EmitUnimplemented(entry, lir);
1825 break;
1826 }
1827 break;
1828 case kPcRel: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
1829 EmitPcRel(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1830 lir->operands[3], lir->operands[4]);
1831 break;
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001832 case kMacro: // lir operands - 0: reg
Brian Carlstrom7940e442013-07-12 13:46:57 -07001833 EmitMacro(entry, lir->operands[0], lir->offset);
1834 break;
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001835 case kNop: // TODO: these instruction kinds are missing implementations.
1836 case kThreadReg:
1837 case kRegArrayImm:
1838 case kShiftArrayImm:
1839 case kShiftArrayCl:
1840 case kArrayCond:
1841 case kUnimplemented:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001842 EmitUnimplemented(entry, lir);
1843 break;
1844 }
Ian Rogers5aa6e042014-06-13 16:38:24 -07001845 DCHECK_EQ(lir->flags.size, GetInsnSize(lir));
1846 CHECK_EQ(lir->flags.size, code_buffer_.size() - starting_cbuf_size)
Brian Carlstrom7940e442013-07-12 13:46:57 -07001847 << "Instruction size mismatch for entry: " << X86Mir2Lir::EncodingMap[lir->opcode].name;
1848 }
1849 return res;
1850}
1851
buzbeeb48819d2013-09-14 16:15:25 -07001852// LIR offset assignment.
1853// TODO: consolidate w/ Arm assembly mechanism.
1854int X86Mir2Lir::AssignInsnOffsets() {
1855 LIR* lir;
1856 int offset = 0;
1857
1858 for (lir = first_lir_insn_; lir != NULL; lir = NEXT_LIR(lir)) {
1859 lir->offset = offset;
buzbee409fe942013-10-11 10:49:56 -07001860 if (LIKELY(!IsPseudoLirOp(lir->opcode))) {
buzbeeb48819d2013-09-14 16:15:25 -07001861 if (!lir->flags.is_nop) {
1862 offset += lir->flags.size;
1863 }
1864 } else if (UNLIKELY(lir->opcode == kPseudoPseudoAlign4)) {
1865 if (offset & 0x2) {
1866 offset += 2;
1867 lir->operands[0] = 1;
1868 } else {
1869 lir->operands[0] = 0;
1870 }
1871 }
1872 /* Pseudo opcodes don't consume space */
1873 }
1874 return offset;
1875}
1876
1877/*
1878 * Walk the compilation unit and assign offsets to instructions
1879 * and literals and compute the total size of the compiled unit.
1880 * TODO: consolidate w/ Arm assembly mechanism.
1881 */
1882void X86Mir2Lir::AssignOffsets() {
1883 int offset = AssignInsnOffsets();
1884
Mark Mendelld65c51a2014-04-29 16:55:20 -04001885 if (const_vectors_ != nullptr) {
1886 /* assign offsets to vector literals */
1887
1888 // First, get offset to 12 mod 16 to align to 16 byte boundary.
1889 // This will ensure that the vector is 16 byte aligned, as the procedure is
1890 // always aligned at at 4 mod 16.
1891 int align_size = (16-4) - (offset & 0xF);
1892 if (align_size < 0) {
1893 align_size += 16;
1894 }
1895
1896 offset += align_size;
1897
1898 // Now assign each literal the right offset.
1899 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
1900 p->offset = offset;
1901 offset += 16;
1902 }
1903 }
1904
buzbeeb48819d2013-09-14 16:15:25 -07001905 /* Const values have to be word aligned */
Andreas Gampe66018822014-05-05 20:47:19 -07001906 offset = RoundUp(offset, 4);
buzbeeb48819d2013-09-14 16:15:25 -07001907
1908 /* Set up offsets for literals */
1909 data_offset_ = offset;
1910
1911 offset = AssignLiteralOffset(offset);
1912
1913 offset = AssignSwitchTablesOffset(offset);
1914
1915 offset = AssignFillArrayDataOffset(offset);
1916
1917 total_size_ = offset;
1918}
1919
1920/*
1921 * Go over each instruction in the list and calculate the offset from the top
1922 * before sending them off to the assembler. If out-of-range branch distance is
1923 * seen rearrange the instructions a bit to correct it.
1924 * TODO: consolidate w/ Arm assembly mechanism.
1925 */
1926void X86Mir2Lir::AssembleLIR() {
buzbeea61f4952013-08-23 14:27:06 -07001927 cu_->NewTimingSplit("Assemble");
Mark Mendell55d0eac2014-02-06 11:02:52 -08001928
1929 // We will remove the method address if we never ended up using it
1930 if (store_method_addr_ && !store_method_addr_used_) {
1931 setup_method_address_[0]->flags.is_nop = true;
1932 setup_method_address_[1]->flags.is_nop = true;
1933 }
1934
buzbeeb48819d2013-09-14 16:15:25 -07001935 AssignOffsets();
1936 int assembler_retries = 0;
1937 /*
1938 * Assemble here. Note that we generate code with optimistic assumptions
1939 * and if found now to work, we'll have to redo the sequence and retry.
1940 */
1941
1942 while (true) {
1943 AssemblerStatus res = AssembleInstructions(0);
1944 if (res == kSuccess) {
1945 break;
1946 } else {
1947 assembler_retries++;
1948 if (assembler_retries > MAX_ASSEMBLER_RETRIES) {
1949 CodegenDump();
1950 LOG(FATAL) << "Assembler error - too many retries";
1951 }
1952 // Redo offsets and try again
1953 AssignOffsets();
1954 code_buffer_.clear();
1955 }
1956 }
1957
1958 // Install literals
1959 InstallLiteralPools();
1960
1961 // Install switch tables
1962 InstallSwitchTables();
1963
1964 // Install fill array data
1965 InstallFillArrayData();
1966
1967 // Create the mapping table and native offset to reference map.
buzbeea61f4952013-08-23 14:27:06 -07001968 cu_->NewTimingSplit("PcMappingTable");
buzbeeb48819d2013-09-14 16:15:25 -07001969 CreateMappingTables();
1970
buzbeea61f4952013-08-23 14:27:06 -07001971 cu_->NewTimingSplit("GcMap");
buzbeeb48819d2013-09-14 16:15:25 -07001972 CreateNativeGcMap();
1973}
1974
Brian Carlstrom7940e442013-07-12 13:46:57 -07001975} // namespace art