blob: 3933b21a26c36f1561baa55c1ef512f802e320bd [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"
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -070019#include "oat.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070020#include "x86_lir.h"
21
22namespace art {
23
24#define MAX_ASSEMBLER_RETRIES 50
25
26const X86EncodingMap X86Mir2Lir::EncodingMap[kX86Last] = {
Ian Rogers0f9b9c52014-06-09 01:32:12 -070027 { kX8632BitData, kData, IS_UNARY_OP, { 0, 0, 0x00, 0, 0, 0, 0, 4, false }, "data", "0x!0d" },
28 { kX86Bkpt, kNullary, NO_OPERAND | IS_BRANCH, { 0, 0, 0xCC, 0, 0, 0, 0, 0, false }, "int 3", "" },
29 { kX86Nop, kNop, NO_OPERAND, { 0, 0, 0x90, 0, 0, 0, 0, 0, false }, "nop", "" },
Brian Carlstrom7940e442013-07-12 13:46:57 -070030
31#define ENCODING_MAP(opname, mem_use, reg_def, uses_ccodes, \
32 rm8_r8, rm32_r32, \
33 r8_rm8, r32_rm32, \
34 ax8_i8, ax32_i32, \
35 rm8_i8, rm8_i8_modrm, \
36 rm32_i32, rm32_i32_modrm, \
37 rm32_i8, rm32_i8_modrm) \
Ian Rogers0f9b9c52014-06-09 01:32:12 -070038{ 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" }, \
Mark Mendell2bc47702014-07-31 14:36:54 -040039{ 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" }, \
Ian Rogers0f9b9c52014-06-09 01:32:12 -070040{ 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" }, \
41{ 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" }, \
42{ 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]" }, \
43{ 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]" }, \
44{ 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]" }, \
45{ 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" }, \
Mark Mendellfd0c2372014-07-31 13:20:21 -040046{ 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, false}, #opname "8MI", "[!0r+!1d],!2d" }, \
47{ 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, false}, #opname "8AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
48{ 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, false}, #opname "8TI", "fs:[!0d],!1d" }, \
Brian Carlstrom7940e442013-07-12 13:46:57 -070049 \
Ian Rogers0f9b9c52014-06-09 01:32:12 -070050{ 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" }, \
51{ 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" }, \
52{ 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" }, \
53{ 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" }, \
54{ 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]" }, \
55{ 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]" }, \
56{ 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]" }, \
57{ 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" }, \
58{ 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" }, \
59{ 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" }, \
60{ 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" }, \
61{ 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" }, \
62{ 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" }, \
63{ 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" }, \
64{ 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 -070065 \
Ian Rogers0f9b9c52014-06-09 01:32:12 -070066{ 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" }, \
67{ 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" }, \
68{ 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" }, \
69{ 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" }, \
70{ 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]" }, \
71{ 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]" }, \
72{ 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]" }, \
73{ 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" }, \
74{ 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" }, \
75{ 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" }, \
76{ 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" }, \
77{ 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" }, \
78{ 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" }, \
79{ 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" }, \
80{ 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 +070081 \
Ian Rogers0f9b9c52014-06-09 01:32:12 -070082{ 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" }, \
83{ 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" }, \
84{ 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" }, \
85{ 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" }, \
86{ 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]" }, \
87{ 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]" }, \
88{ 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]" }, \
89{ 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" }, \
90{ 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" }, \
91{ 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" }, \
92{ 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" }, \
93{ 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" }, \
94{ 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" }, \
95{ 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" }, \
96{ 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 -070097
98ENCODING_MAP(Add, IS_LOAD | IS_STORE, REG_DEF0, 0,
99 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */,
100 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */,
101 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */,
102 0x80, 0x0 /* RegMem8/imm8 */,
103 0x81, 0x0 /* RegMem32/imm32 */, 0x83, 0x0 /* RegMem32/imm8 */),
104ENCODING_MAP(Or, IS_LOAD | IS_STORE, REG_DEF0, 0,
105 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */,
106 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */,
107 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */,
108 0x80, 0x1 /* RegMem8/imm8 */,
109 0x81, 0x1 /* RegMem32/imm32 */, 0x83, 0x1 /* RegMem32/imm8 */),
110ENCODING_MAP(Adc, IS_LOAD | IS_STORE, REG_DEF0, USES_CCODES,
111 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */,
112 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */,
113 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */,
114 0x80, 0x2 /* RegMem8/imm8 */,
115 0x81, 0x2 /* RegMem32/imm32 */, 0x83, 0x2 /* RegMem32/imm8 */),
116ENCODING_MAP(Sbb, IS_LOAD | IS_STORE, REG_DEF0, USES_CCODES,
117 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */,
118 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */,
119 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */,
120 0x80, 0x3 /* RegMem8/imm8 */,
121 0x81, 0x3 /* RegMem32/imm32 */, 0x83, 0x3 /* RegMem32/imm8 */),
122ENCODING_MAP(And, IS_LOAD | IS_STORE, REG_DEF0, 0,
123 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */,
124 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */,
125 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */,
126 0x80, 0x4 /* RegMem8/imm8 */,
127 0x81, 0x4 /* RegMem32/imm32 */, 0x83, 0x4 /* RegMem32/imm8 */),
128ENCODING_MAP(Sub, IS_LOAD | IS_STORE, REG_DEF0, 0,
129 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */,
130 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */,
131 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */,
132 0x80, 0x5 /* RegMem8/imm8 */,
133 0x81, 0x5 /* RegMem32/imm32 */, 0x83, 0x5 /* RegMem32/imm8 */),
134ENCODING_MAP(Xor, IS_LOAD | IS_STORE, REG_DEF0, 0,
135 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */,
136 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */,
137 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */,
138 0x80, 0x6 /* RegMem8/imm8 */,
139 0x81, 0x6 /* RegMem32/imm32 */, 0x83, 0x6 /* RegMem32/imm8 */),
140ENCODING_MAP(Cmp, IS_LOAD, 0, 0,
141 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */,
142 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */,
143 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */,
144 0x80, 0x7 /* RegMem8/imm8 */,
145 0x81, 0x7 /* RegMem32/imm32 */, 0x83, 0x7 /* RegMem32/imm8 */),
146#undef ENCODING_MAP
147
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700148 { kX86Imul16RRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { 0x66, 0, 0x69, 0, 0, 0, 0, 2, false }, "Imul16RRI", "!0r,!1r,!2d" },
149 { 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" },
150 { 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 -0700151
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700152 { kX86Imul32RRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { 0, 0, 0x69, 0, 0, 0, 0, 4, false }, "Imul32RRI", "!0r,!1r,!2d" },
153 { 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" },
154 { 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" },
155 { kX86Imul32RRI8, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { 0, 0, 0x6B, 0, 0, 0, 0, 1, false }, "Imul32RRI8", "!0r,!1r,!2d" },
156 { 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" },
157 { 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 -0700158
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700159 { kX86Imul64RRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { REX_W, 0, 0x69, 0, 0, 0, 0, 4, false }, "Imul64RRI", "!0r,!1r,!2d" },
160 { 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" },
161 { 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" },
162 { kX86Imul64RRI8, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { REX_W, 0, 0x6B, 0, 0, 0, 0, 1, false }, "Imul64RRI8", "!0r,!1r,!2d" },
163 { 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" },
164 { 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 +0700165
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700166 { kX86Mov8MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0, 0, 0x88, 0, 0, 0, 0, 0, true }, "Mov8MR", "[!0r+!1d],!2r" },
167 { kX86Mov8AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0, 0, 0x88, 0, 0, 0, 0, 0, true }, "Mov8AR", "[!0r+!1r<<!2d+!3d],!4r" },
168 { kX86Mov8TR, kThreadReg, IS_STORE | IS_BINARY_OP | REG_USE1, { THREAD_PREFIX, 0, 0x88, 0, 0, 0, 0, 0, true }, "Mov8TR", "fs:[!0d],!1r" },
169 { kX86Mov8RR, kRegReg, IS_BINARY_OP | REG_DEF0_USE1, { 0, 0, 0x8A, 0, 0, 0, 0, 0, true }, "Mov8RR", "!0r,!1r" },
170 { kX86Mov8RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { 0, 0, 0x8A, 0, 0, 0, 0, 0, true }, "Mov8RM", "!0r,[!1r+!2d]" },
171 { 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]" },
172 { kX86Mov8RT, kRegThread, IS_LOAD | IS_BINARY_OP | REG_DEF0, { THREAD_PREFIX, 0, 0x8A, 0, 0, 0, 0, 0, true }, "Mov8RT", "!0r,fs:[!1d]" },
173 { kX86Mov8RI, kMovRegImm, IS_BINARY_OP | REG_DEF0, { 0, 0, 0xB0, 0, 0, 0, 0, 1, true }, "Mov8RI", "!0r,!1d" },
Mark Mendellfd0c2372014-07-31 13:20:21 -0400174 { kX86Mov8MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { 0, 0, 0xC6, 0, 0, 0, 0, 1, false}, "Mov8MI", "[!0r+!1d],!2d" },
175 { kX86Mov8AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { 0, 0, 0xC6, 0, 0, 0, 0, 1, false}, "Mov8AI", "[!0r+!1r<<!2d+!3d],!4d" },
176 { kX86Mov8TI, kThreadImm, IS_STORE | IS_BINARY_OP, { THREAD_PREFIX, 0, 0xC6, 0, 0, 0, 0, 1, false}, "Mov8TI", "fs:[!0d],!1d" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700177
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700178 { kX86Mov16MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, 0, 0x89, 0, 0, 0, 0, 0, false }, "Mov16MR", "[!0r+!1d],!2r" },
179 { kX86Mov16AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x66, 0, 0x89, 0, 0, 0, 0, 0, false }, "Mov16AR", "[!0r+!1r<<!2d+!3d],!4r" },
180 { kX86Mov16TR, kThreadReg, IS_STORE | IS_BINARY_OP | REG_USE1, { THREAD_PREFIX, 0x66, 0x89, 0, 0, 0, 0, 0, false }, "Mov16TR", "fs:[!0d],!1r" },
181 { kX86Mov16RR, kRegReg, IS_BINARY_OP | REG_DEF0_USE1, { 0x66, 0, 0x8B, 0, 0, 0, 0, 0, false }, "Mov16RR", "!0r,!1r" },
182 { kX86Mov16RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { 0x66, 0, 0x8B, 0, 0, 0, 0, 0, false }, "Mov16RM", "!0r,[!1r+!2d]" },
183 { 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]" },
184 { kX86Mov16RT, kRegThread, IS_LOAD | IS_BINARY_OP | REG_DEF0, { THREAD_PREFIX, 0x66, 0x8B, 0, 0, 0, 0, 0, false }, "Mov16RT", "!0r,fs:[!1d]" },
185 { kX86Mov16RI, kMovRegImm, IS_BINARY_OP | REG_DEF0, { 0x66, 0, 0xB8, 0, 0, 0, 0, 2, false }, "Mov16RI", "!0r,!1d" },
186 { kX86Mov16MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { 0x66, 0, 0xC7, 0, 0, 0, 0, 2, false }, "Mov16MI", "[!0r+!1d],!2d" },
187 { kX86Mov16AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { 0x66, 0, 0xC7, 0, 0, 0, 0, 2, false }, "Mov16AI", "[!0r+!1r<<!2d+!3d],!4d" },
188 { 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 -0700189
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700190 { kX86Mov32MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0, 0, 0x89, 0, 0, 0, 0, 0, false }, "Mov32MR", "[!0r+!1d],!2r" },
191 { kX86Mov32AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0, 0, 0x89, 0, 0, 0, 0, 0, false }, "Mov32AR", "[!0r+!1r<<!2d+!3d],!4r" },
Razvan A Lupusoruae9f3e62014-09-23 14:54:32 -0700192 { kX86Movnti32MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0, 0, 0x0F, 0xC3, 0, 0, 0, 0, false }, "Movnti32MR", "[!0r+!1d],!2r" },
193 { kX86Movnti32AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0, 0, 0x0F, 0xC3, 0, 0, 0, 0, false }, "Movnti32AR", "[!0r+!1r<<!2d+!3d],!4r" },
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700194 { kX86Mov32TR, kThreadReg, IS_STORE | IS_BINARY_OP | REG_USE1, { THREAD_PREFIX, 0, 0x89, 0, 0, 0, 0, 0, false }, "Mov32TR", "fs:[!0d],!1r" },
Haitao Fenga870bc52014-09-09 15:52:34 +0800195 { kX86Mov32RR, kRegReg, IS_MOVE | IS_BINARY_OP | REG_DEF0_USE1, { 0, 0, 0x8B, 0, 0, 0, 0, 0, false }, "Mov32RR", "!0r,!1r" },
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700196 { kX86Mov32RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { 0, 0, 0x8B, 0, 0, 0, 0, 0, false }, "Mov32RM", "!0r,[!1r+!2d]" },
197 { 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]" },
198 { kX86Mov32RT, kRegThread, IS_LOAD | IS_BINARY_OP | REG_DEF0, { THREAD_PREFIX, 0, 0x8B, 0, 0, 0, 0, 0, false }, "Mov32RT", "!0r,fs:[!1d]" },
199 { kX86Mov32RI, kMovRegImm, IS_BINARY_OP | REG_DEF0, { 0, 0, 0xB8, 0, 0, 0, 0, 4, false }, "Mov32RI", "!0r,!1d" },
200 { kX86Mov32MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { 0, 0, 0xC7, 0, 0, 0, 0, 4, false }, "Mov32MI", "[!0r+!1d],!2d" },
201 { kX86Mov32AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { 0, 0, 0xC7, 0, 0, 0, 0, 4, false }, "Mov32AI", "[!0r+!1r<<!2d+!3d],!4d" },
202 { 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 -0700203
Haitao Fenga870bc52014-09-09 15:52:34 +0800204 { kX86Lea32RM, kRegMem, IS_TERTIARY_OP | REG_DEF0_USE1, { 0, 0, 0x8D, 0, 0, 0, 0, 0, false }, "Lea32RM", "!0r,[!1r+!2d]" },
205 { 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 -0800206
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700207 { kX86Mov64MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { REX_W, 0, 0x89, 0, 0, 0, 0, 0, false }, "Mov64MR", "[!0r+!1d],!2r" },
208 { 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" },
Razvan A Lupusoruae9f3e62014-09-23 14:54:32 -0700209 { kX86Movnti64MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { REX_W, 0, 0x0F, 0xC3, 0, 0, 0, 0, false }, "Movnti64MR", "[!0r+!1d],!2r" },
210 { kX86Movnti64AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { REX_W, 0, 0x0F, 0xC3, 0, 0, 0, 0, false }, "Movnti64AR", "[!0r+!1r<<!2d+!3d],!4r" },
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700211 { kX86Mov64TR, kThreadReg, IS_STORE | IS_BINARY_OP | REG_USE1, { THREAD_PREFIX, REX_W, 0x89, 0, 0, 0, 0, 0, false }, "Mov64TR", "fs:[!0d],!1r" },
Haitao Fenga870bc52014-09-09 15:52:34 +0800212 { kX86Mov64RR, kRegReg, IS_MOVE | IS_BINARY_OP | REG_DEF0_USE1, { REX_W, 0, 0x8B, 0, 0, 0, 0, 0, false }, "Mov64RR", "!0r,!1r" },
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700213 { kX86Mov64RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { REX_W, 0, 0x8B, 0, 0, 0, 0, 0, false }, "Mov64RM", "!0r,[!1r+!2d]" },
214 { 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]" },
215 { kX86Mov64RT, kRegThread, IS_LOAD | IS_BINARY_OP | REG_DEF0, { THREAD_PREFIX, REX_W, 0x8B, 0, 0, 0, 0, 0, false }, "Mov64RT", "!0r,fs:[!1d]" },
Yixin Shou5192cbb2014-07-01 13:48:17 -0400216 { kX86Mov64RI32, kRegImm, IS_BINARY_OP | REG_DEF0, { REX_W, 0, 0xC7, 0, 0, 0, 0, 4, false }, "Mov64RI32", "!0r,!1d" },
217 { kX86Mov64RI64, kMovRegQuadImm, IS_TERTIARY_OP | REG_DEF0, { REX_W, 0, 0xB8, 0, 0, 0, 0, 8, false }, "Mov64RI64", "!0r,!1q" },
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700218 { kX86Mov64MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { REX_W, 0, 0xC7, 0, 0, 0, 0, 4, false }, "Mov64MI", "[!0r+!1d],!2d" },
219 { 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" },
220 { 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 -0700221
Haitao Fenga870bc52014-09-09 15:52:34 +0800222 { kX86Lea64RM, kRegMem, IS_TERTIARY_OP | REG_DEF0_USE1, { REX_W, 0, 0x8D, 0, 0, 0, 0, 0, false }, "Lea64RM", "!0r,[!1r+!2d]" },
223 { 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 -0800224
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700225 { kX86Cmov32RRC, kRegRegCond, IS_TERTIARY_OP | REG_DEF0_USE01 | USES_CCODES, { 0, 0, 0x0F, 0x40, 0, 0, 0, 0, false }, "Cmovcc32RR", "!2c !0r,!1r" },
226 { 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 +0700227
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700228 { 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]" },
229 { 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 -0400230
Brian Carlstrom7940e442013-07-12 13:46:57 -0700231#define SHIFT_ENCODING_MAP(opname, modrm_opcode) \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700232{ 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" }, \
233{ 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" }, \
234{ 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" }, \
235{ 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" }, \
236{ 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" }, \
237{ 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 -0700238 \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700239{ 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" }, \
240{ 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" }, \
241{ 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" }, \
242{ 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" }, \
243{ 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" }, \
244{ 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 -0700245 \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700246{ 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" }, \
247{ 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" }, \
248{ 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" }, \
249{ 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" }, \
250{ 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" }, \
251{ 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 +0700252 \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700253{ 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" }, \
254{ 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" }, \
255{ 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" }, \
256{ 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" }, \
257{ 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" }, \
258{ 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 -0700259
260 SHIFT_ENCODING_MAP(Rol, 0x0),
261 SHIFT_ENCODING_MAP(Ror, 0x1),
262 SHIFT_ENCODING_MAP(Rcl, 0x2),
263 SHIFT_ENCODING_MAP(Rcr, 0x3),
264 SHIFT_ENCODING_MAP(Sal, 0x4),
265 SHIFT_ENCODING_MAP(Shr, 0x5),
266 SHIFT_ENCODING_MAP(Sar, 0x7),
267#undef SHIFT_ENCODING_MAP
268
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700269 { kX86Cmc, kNullary, NO_OPERAND, { 0, 0, 0xF5, 0, 0, 0, 0, 0, false }, "Cmc", "" },
270 { kX86Shld32RRI, kRegRegImmStore, IS_TERTIARY_OP | REG_DEF0_USE01 | SETS_CCODES, { 0, 0, 0x0F, 0xA4, 0, 0, 0, 1, false }, "Shld32RRI", "!0r,!1r,!2d" },
Yixin Shouf40f8902014-08-14 14:10:32 -0400271 { kX86Shld32RRC, kShiftRegRegCl, IS_TERTIARY_OP | REG_DEF0_USE01 | REG_USEC | SETS_CCODES, { 0, 0, 0x0F, 0xA5, 0, 0, 0, 0, false }, "Shld32RRC", "!0r,!1r,cl" },
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700272 { 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" },
273 { kX86Shrd32RRI, kRegRegImmStore, IS_TERTIARY_OP | REG_DEF0_USE01 | SETS_CCODES, { 0, 0, 0x0F, 0xAC, 0, 0, 0, 1, false }, "Shrd32RRI", "!0r,!1r,!2d" },
Yixin Shouf40f8902014-08-14 14:10:32 -0400274 { kX86Shrd32RRC, kShiftRegRegCl, IS_TERTIARY_OP | REG_DEF0_USE01 | REG_USEC | SETS_CCODES, { 0, 0, 0x0F, 0xAD, 0, 0, 0, 0, false }, "Shrd32RRC", "!0r,!1r,cl" },
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700275 { 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" },
276 { kX86Shld64RRI, kRegRegImmStore, IS_TERTIARY_OP | REG_DEF0_USE01 | SETS_CCODES, { REX_W, 0, 0x0F, 0xA4, 0, 0, 0, 1, false }, "Shld64RRI", "!0r,!1r,!2d" },
277 { 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" },
278 { kX86Shrd64RRI, kRegRegImmStore, IS_TERTIARY_OP | REG_DEF0_USE01 | SETS_CCODES, { REX_W, 0, 0x0F, 0xAC, 0, 0, 0, 1, false }, "Shrd64RRI", "!0r,!1r,!2d" },
279 { 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 -0700280
Dave Allison69dfe512014-07-11 17:11:58 +0000281 { kX86Test8RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF6, 0, 0, 0, 0, 1, true }, "Test8RI", "!0r,!1d" },
282 { kX86Test8MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF6, 0, 0, 0, 0, 1, true }, "Test8MI", "[!0r+!1d],!2d" },
283 { 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" },
284 { kX86Test16RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0x66, 0, 0xF7, 0, 0, 0, 0, 2, false }, "Test16RI", "!0r,!1d" },
285 { kX86Test16MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0x66, 0, 0xF7, 0, 0, 0, 0, 2, false }, "Test16MI", "[!0r+!1d],!2d" },
286 { 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" },
287 { kX86Test32RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF7, 0, 0, 0, 0, 4, false }, "Test32RI", "!0r,!1d" },
288 { kX86Test32MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF7, 0, 0, 0, 0, 4, false }, "Test32MI", "[!0r+!1d],!2d" },
289 { 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" },
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700290 { kX86Test64RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { REX_W, 0, 0xF7, 0, 0, 0, 0, 4, false }, "Test64RI", "!0r,!1d" },
291 { 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" },
292 { 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 +0700293
Dave Allison69dfe512014-07-11 17:11:58 +0000294 { kX86Test32RR, kRegReg, IS_BINARY_OP | REG_USE01 | SETS_CCODES, { 0, 0, 0x85, 0, 0, 0, 0, 0, false }, "Test32RR", "!0r,!1r" },
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700295 { kX86Test64RR, kRegReg, IS_BINARY_OP | REG_USE01 | SETS_CCODES, { REX_W, 0, 0x85, 0, 0, 0, 0, 0, false }, "Test64RR", "!0r,!1r" },
Chao-ying Fucf818412014-07-24 12:08:28 -0700296 { kX86Test32RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_USE01 | SETS_CCODES, { 0, 0, 0x85, 0, 0, 0, 0, 0, false }, "Test32RM", "!0r,[!1r+!2d]" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297
298#define UNARY_ENCODING_MAP(opname, modrm, is_store, sets_ccodes, \
299 reg, reg_kind, reg_flags, \
300 mem, mem_kind, mem_flags, \
301 arr, arr_kind, arr_flags, imm, \
302 b_flags, hw_flags, w_flags, \
303 b_format, hw_format, w_format) \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700304{ 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" }, \
305{ 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]" }, \
306{ 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]" }, \
307{ 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" }, \
308{ 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]" }, \
309{ 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]" }, \
310{ 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" }, \
311{ 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]" }, \
312{ 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]" }, \
313{ 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" }, \
314{ 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]" }, \
315{ 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 -0700316
317 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, "", "", ""),
318 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, "", "", ""),
319
Mark Mendell2bf31e62014-01-23 12:13:40 -0800320 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,"),
321 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,"),
322 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,"),
323 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 -0700324#undef UNARY_ENCODING_MAP
325
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700326 { kx86Cdq32Da, kRegOpcode, NO_OPERAND | REG_DEFAD_USEA, { 0, 0, 0x99, 0, 0, 0, 0, 0, false }, "Cdq", "" },
327 { kx86Cqo64Da, kRegOpcode, NO_OPERAND | REG_DEFAD_USEA, { REX_W, 0, 0x99, 0, 0, 0, 0, 0, false }, "Cqo", "" },
328 { kX86Bswap32R, kRegOpcode, IS_UNARY_OP | REG_DEF0_USE0, { 0, 0, 0x0F, 0xC8, 0, 0, 0, 0, false }, "Bswap32R", "!0r" },
nikolay serdjukc5e4ce12014-06-10 17:07:10 +0700329 { kX86Bswap64R, kRegOpcode, IS_UNARY_OP | REG_DEF0_USE0, { REX_W, 0, 0x0F, 0xC8, 0, 0, 0, 0, false }, "Bswap64R", "!0r" },
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700330 { 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" },
331 { 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 +0100332
Brian Carlstrom7940e442013-07-12 13:46:57 -0700333#define EXT_0F_ENCODING_MAP(opname, prefix, opcode, 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, 0, 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, 0, 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, 0, 0, 0, 0, false }, #opname "RA", "!0r,[!1r+!2r<<!3d+!4d]" }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700337
Chao-ying Fu021b60f2014-07-09 11:32:31 -0700338// This is a special encoding with r8_form on the second register only
339// for Movzx8 and Movsx8.
340#define EXT_0F_R8_FORM_ENCODING_MAP(opname, prefix, opcode, reg_def) \
341{ kX86 ## opname ## RR, kRegReg, IS_BINARY_OP | reg_def | REG_USE1, { prefix, 0, 0x0F, opcode, 0, 0, 0, 0, true }, #opname "RR", "!0r,!1r" }, \
342{ 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]" }, \
343{ 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]" }
344
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700345#define EXT_0F_REX_W_ENCODING_MAP(opname, prefix, opcode, reg_def) \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700346{ 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" }, \
347{ 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]" }, \
348{ 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 -0700349
Mark Mendellfe945782014-05-22 09:52:36 -0400350#define EXT_0F_ENCODING2_MAP(opname, prefix, opcode, opcode2, reg_def) \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700351{ kX86 ## opname ## RR, kRegReg, IS_BINARY_OP | reg_def | REG_USE1, { prefix, 0, 0x0F, opcode, opcode2, 0, 0, 0, false }, #opname "RR", "!0r,!1r" }, \
352{ 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]" }, \
353{ 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 -0400354
Brian Carlstrom7940e442013-07-12 13:46:57 -0700355 EXT_0F_ENCODING_MAP(Movsd, 0xF2, 0x10, REG_DEF0),
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700356 { kX86MovsdMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0xF2, 0, 0x0F, 0x11, 0, 0, 0, 0, false }, "MovsdMR", "[!0r+!1d],!2r" },
357 { 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 -0700358
359 EXT_0F_ENCODING_MAP(Movss, 0xF3, 0x10, REG_DEF0),
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700360 { kX86MovssMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0xF3, 0, 0x0F, 0x11, 0, 0, 0, 0, false }, "MovssMR", "[!0r+!1d],!2r" },
361 { 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 -0700362
363 EXT_0F_ENCODING_MAP(Cvtsi2sd, 0xF2, 0x2A, REG_DEF0),
364 EXT_0F_ENCODING_MAP(Cvtsi2ss, 0xF3, 0x2A, REG_DEF0),
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700365 EXT_0F_REX_W_ENCODING_MAP(Cvtsqi2sd, 0xF2, 0x2A, REG_DEF0),
366 EXT_0F_REX_W_ENCODING_MAP(Cvtsqi2ss, 0xF3, 0x2A, REG_DEF0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700367 EXT_0F_ENCODING_MAP(Cvttsd2si, 0xF2, 0x2C, REG_DEF0),
368 EXT_0F_ENCODING_MAP(Cvttss2si, 0xF3, 0x2C, REG_DEF0),
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700369 EXT_0F_REX_W_ENCODING_MAP(Cvttsd2sqi, 0xF2, 0x2C, REG_DEF0),
370 EXT_0F_REX_W_ENCODING_MAP(Cvttss2sqi, 0xF3, 0x2C, REG_DEF0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 EXT_0F_ENCODING_MAP(Cvtsd2si, 0xF2, 0x2D, REG_DEF0),
372 EXT_0F_ENCODING_MAP(Cvtss2si, 0xF3, 0x2D, REG_DEF0),
Mark Mendell2637f2e2014-04-30 10:10:47 -0400373 EXT_0F_ENCODING_MAP(Ucomisd, 0x66, 0x2E, SETS_CCODES|REG_USE0),
374 EXT_0F_ENCODING_MAP(Ucomiss, 0x00, 0x2E, SETS_CCODES|REG_USE0),
375 EXT_0F_ENCODING_MAP(Comisd, 0x66, 0x2F, SETS_CCODES|REG_USE0),
376 EXT_0F_ENCODING_MAP(Comiss, 0x00, 0x2F, SETS_CCODES|REG_USE0),
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700377 EXT_0F_ENCODING_MAP(Orpd, 0x66, 0x56, REG_DEF0_USE0),
Mark Mendell2637f2e2014-04-30 10:10:47 -0400378 EXT_0F_ENCODING_MAP(Orps, 0x00, 0x56, REG_DEF0_USE0),
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700379 EXT_0F_ENCODING_MAP(Andpd, 0x66, 0x54, REG_DEF0_USE0),
380 EXT_0F_ENCODING_MAP(Andps, 0x00, 0x54, REG_DEF0_USE0),
381 EXT_0F_ENCODING_MAP(Xorpd, 0x66, 0x57, REG_DEF0_USE0),
Mark Mendell2637f2e2014-04-30 10:10:47 -0400382 EXT_0F_ENCODING_MAP(Xorps, 0x00, 0x57, REG_DEF0_USE0),
383 EXT_0F_ENCODING_MAP(Addsd, 0xF2, 0x58, REG_DEF0_USE0),
384 EXT_0F_ENCODING_MAP(Addss, 0xF3, 0x58, REG_DEF0_USE0),
385 EXT_0F_ENCODING_MAP(Mulsd, 0xF2, 0x59, REG_DEF0_USE0),
386 EXT_0F_ENCODING_MAP(Mulss, 0xF3, 0x59, REG_DEF0_USE0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 EXT_0F_ENCODING_MAP(Cvtsd2ss, 0xF2, 0x5A, REG_DEF0),
388 EXT_0F_ENCODING_MAP(Cvtss2sd, 0xF3, 0x5A, REG_DEF0),
Mark Mendell2637f2e2014-04-30 10:10:47 -0400389 EXT_0F_ENCODING_MAP(Subsd, 0xF2, 0x5C, REG_DEF0_USE0),
390 EXT_0F_ENCODING_MAP(Subss, 0xF3, 0x5C, REG_DEF0_USE0),
391 EXT_0F_ENCODING_MAP(Divsd, 0xF2, 0x5E, REG_DEF0_USE0),
392 EXT_0F_ENCODING_MAP(Divss, 0xF3, 0x5E, REG_DEF0_USE0),
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700393 EXT_0F_ENCODING_MAP(Punpcklbw, 0x66, 0x60, REG_DEF0_USE0),
394 EXT_0F_ENCODING_MAP(Punpcklwd, 0x66, 0x61, REG_DEF0_USE0),
Mark Mendell2637f2e2014-04-30 10:10:47 -0400395 EXT_0F_ENCODING_MAP(Punpckldq, 0x66, 0x62, REG_DEF0_USE0),
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700396 EXT_0F_ENCODING_MAP(Punpcklqdq, 0x66, 0x6C, REG_DEF0_USE0),
Mark Mendellfe945782014-05-22 09:52:36 -0400397 EXT_0F_ENCODING_MAP(Sqrtsd, 0xF2, 0x51, REG_DEF0_USE0),
398 EXT_0F_ENCODING2_MAP(Pmulld, 0x66, 0x38, 0x40, REG_DEF0_USE0),
399 EXT_0F_ENCODING_MAP(Pmullw, 0x66, 0xD5, REG_DEF0_USE0),
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700400 EXT_0F_ENCODING_MAP(Pmuludq, 0x66, 0xF4, REG_DEF0_USE0),
Mark Mendellfe945782014-05-22 09:52:36 -0400401 EXT_0F_ENCODING_MAP(Mulps, 0x00, 0x59, REG_DEF0_USE0),
402 EXT_0F_ENCODING_MAP(Mulpd, 0x66, 0x59, REG_DEF0_USE0),
403 EXT_0F_ENCODING_MAP(Paddb, 0x66, 0xFC, REG_DEF0_USE0),
404 EXT_0F_ENCODING_MAP(Paddw, 0x66, 0xFD, REG_DEF0_USE0),
405 EXT_0F_ENCODING_MAP(Paddd, 0x66, 0xFE, REG_DEF0_USE0),
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700406 EXT_0F_ENCODING_MAP(Paddq, 0x66, 0xD4, REG_DEF0_USE0),
407 EXT_0F_ENCODING_MAP(Psadbw, 0x66, 0xF6, REG_DEF0_USE0),
Mark Mendellfe945782014-05-22 09:52:36 -0400408 EXT_0F_ENCODING_MAP(Addps, 0x00, 0x58, REG_DEF0_USE0),
409 EXT_0F_ENCODING_MAP(Addpd, 0xF2, 0x58, REG_DEF0_USE0),
410 EXT_0F_ENCODING_MAP(Psubb, 0x66, 0xF8, REG_DEF0_USE0),
411 EXT_0F_ENCODING_MAP(Psubw, 0x66, 0xF9, REG_DEF0_USE0),
412 EXT_0F_ENCODING_MAP(Psubd, 0x66, 0xFA, REG_DEF0_USE0),
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700413 EXT_0F_ENCODING_MAP(Psubq, 0x66, 0xFB, REG_DEF0_USE0),
Mark Mendellfe945782014-05-22 09:52:36 -0400414 EXT_0F_ENCODING_MAP(Subps, 0x00, 0x5C, REG_DEF0_USE0),
415 EXT_0F_ENCODING_MAP(Subpd, 0x66, 0x5C, REG_DEF0_USE0),
416 EXT_0F_ENCODING_MAP(Pand, 0x66, 0xDB, REG_DEF0_USE0),
417 EXT_0F_ENCODING_MAP(Por, 0x66, 0xEB, REG_DEF0_USE0),
418 EXT_0F_ENCODING_MAP(Pxor, 0x66, 0xEF, REG_DEF0_USE0),
419 EXT_0F_ENCODING2_MAP(Phaddw, 0x66, 0x38, 0x01, REG_DEF0_USE0),
420 EXT_0F_ENCODING2_MAP(Phaddd, 0x66, 0x38, 0x02, REG_DEF0_USE0),
Olivier Comefb0fecf2014-06-20 11:46:16 +0200421 EXT_0F_ENCODING_MAP(Haddpd, 0x66, 0x7C, REG_DEF0_USE0),
422 EXT_0F_ENCODING_MAP(Haddps, 0xF2, 0x7C, REG_DEF0_USE0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700423
Serguei Katkov35690632014-07-16 15:52:59 +0700424 { kX86PextrbRRI, kRegRegImmStore, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0x66, 0, 0x0F, 0x3A, 0x14, 0, 0, 1, false }, "PextbRRI", "!0r,!1r,!2d" },
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700425 { kX86PextrwRRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0x66, 0, 0x0F, 0xC5, 0x00, 0, 0, 1, false }, "PextwRRI", "!0r,!1r,!2d" },
Serguei Katkov35690632014-07-16 15:52:59 +0700426 { kX86PextrdRRI, kRegRegImmStore, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0x66, 0, 0x0F, 0x3A, 0x16, 0, 0, 1, false }, "PextdRRI", "!0r,!1r,!2d" },
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700427 { kX86PextrbMRI, kMemRegImm, IS_QUAD_OP | REG_USE02 | IS_STORE, { 0x66, 0, 0x0F, 0x3A, 0x16, 0, 0, 1, false }, "kX86PextrbMRI", "[!0r+!1d],!2r,!3d" },
428 { kX86PextrwMRI, kMemRegImm, IS_QUAD_OP | REG_USE02 | IS_STORE, { 0x66, 0, 0x0F, 0x3A, 0x16, 0, 0, 1, false }, "kX86PextrwMRI", "[!0r+!1d],!2r,!3d" },
429 { kX86PextrdMRI, kMemRegImm, IS_QUAD_OP | REG_USE02 | IS_STORE, { 0x66, 0, 0x0F, 0x3A, 0x16, 0, 0, 1, false }, "kX86PextrdMRI", "[!0r+!1d],!2r,!3d" },
Mark Mendellfe945782014-05-22 09:52:36 -0400430
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700431 { kX86PshuflwRRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0xF2, 0, 0x0F, 0x70, 0, 0, 0, 1, false }, "PshuflwRRI", "!0r,!1r,!2d" },
432 { 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 -0400433
Olivier Comefb0fecf2014-06-20 11:46:16 +0200434 { kX86ShufpsRRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0x00, 0, 0x0F, 0xC6, 0, 0, 0, 1, false }, "kX86ShufpsRRI", "!0r,!1r,!2d" },
435 { kX86ShufpdRRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0x66, 0, 0x0F, 0xC6, 0, 0, 0, 1, false }, "kX86ShufpdRRI", "!0r,!1r,!2d" },
436
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700437 { kX86PsrawRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x71, 0, 4, 0, 1, false }, "PsrawRI", "!0r,!1d" },
438 { kX86PsradRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x72, 0, 4, 0, 1, false }, "PsradRI", "!0r,!1d" },
439 { kX86PsrlwRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x71, 0, 2, 0, 1, false }, "PsrlwRI", "!0r,!1d" },
440 { kX86PsrldRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x72, 0, 2, 0, 1, false }, "PsrldRI", "!0r,!1d" },
441 { kX86PsrlqRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x73, 0, 2, 0, 1, false }, "PsrlqRI", "!0r,!1d" },
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700442 { kX86PsrldqRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x73, 0, 3, 0, 1, false }, "PsrldqRI", "!0r,!1d" },
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700443 { kX86PsllwRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x71, 0, 6, 0, 1, false }, "PsllwRI", "!0r,!1d" },
444 { kX86PslldRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x72, 0, 6, 0, 1, false }, "PslldRI", "!0r,!1d" },
445 { 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 -0800446
Haitao Fenga870bc52014-09-09 15:52:34 +0800447 { kX86Fild32M, kMem, IS_LOAD | IS_BINARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xDB, 0x00, 0, 0, 0, 0, false }, "Fild32M", "[!0r,!1d]" },
448 { kX86Fild64M, kMem, IS_LOAD | IS_BINARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xDF, 0x00, 0, 5, 0, 0, false }, "Fild64M", "[!0r,!1d]" },
449 { kX86Fld32M, kMem, IS_LOAD | IS_BINARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xD9, 0x00, 0, 0, 0, 0, false }, "Fld32M", "[!0r,!1d]" },
450 { kX86Fld64M, kMem, IS_LOAD | IS_BINARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xDD, 0x00, 0, 0, 0, 0, false }, "Fld64M", "[!0r,!1d]" },
451 { kX86Fstp32M, kMem, IS_STORE | IS_BINARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xD9, 0x00, 0, 3, 0, 0, false }, "Fstps32M", "[!0r,!1d]" },
452 { kX86Fstp64M, kMem, IS_STORE | IS_BINARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xDD, 0x00, 0, 3, 0, 0, false }, "Fstpd64M", "[!0r,!1d]" },
453 { kX86Fst32M, kMem, IS_STORE | IS_BINARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xD9, 0x00, 0, 2, 0, 0, false }, "Fsts32M", "[!0r,!1d]" },
454 { kX86Fst64M, kMem, IS_STORE | IS_BINARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xDD, 0x00, 0, 2, 0, 0, false }, "Fstd64M", "[!0r,!1d]" },
Alexei Zavjalovbd3682e2014-06-12 03:08:01 +0700455 { kX86Fprem, kNullary, NO_OPERAND | USE_FP_STACK, { 0xD9, 0, 0xF8, 0, 0, 0, 0, 0, false }, "Fprem64", "" },
456 { kX86Fucompp, kNullary, NO_OPERAND | USE_FP_STACK, { 0xDA, 0, 0xE9, 0, 0, 0, 0, 0, false }, "Fucompp", "" },
Mark Mendell01a50d62014-07-06 12:24:40 -0400457 { kX86Fstsw16R, kNullary, NO_OPERAND | REG_DEFA | USE_FP_STACK, { 0x9B, 0xDF, 0xE0, 0, 0, 0, 0, 0, false }, "Fstsw16R", "ax" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700458
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700459 EXT_0F_ENCODING_MAP(Movdqa, 0x66, 0x6F, REG_DEF0),
460 { kX86MovdqaMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, 0, 0x0F, 0x6F, 0, 0, 0, 0, false }, "MovdqaMR", "[!0r+!1d],!2r" },
461 { kX86MovdqaAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x66, 0, 0x0F, 0x6F, 0, 0, 0, 0, false }, "MovdqaAR", "[!0r+!1r<<!2d+!3d],!4r" },
Mark Mendelld65c51a2014-04-29 16:55:20 -0400462
463
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800464 EXT_0F_ENCODING_MAP(Movups, 0x0, 0x10, REG_DEF0),
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700465 { kX86MovupsMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x0, 0, 0x0F, 0x11, 0, 0, 0, 0, false }, "MovupsMR", "[!0r+!1d],!2r" },
466 { 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 -0800467
468 EXT_0F_ENCODING_MAP(Movaps, 0x0, 0x28, REG_DEF0),
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700469 { kX86MovapsMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x0, 0, 0x0F, 0x29, 0, 0, 0, 0, false }, "MovapsMR", "[!0r+!1d],!2r" },
470 { 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 -0800471
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700472 { kX86MovlpsRM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0 | REG_USE01, { 0x0, 0, 0x0F, 0x12, 0, 0, 0, 0, false }, "MovlpsRM", "!0r,[!1r+!2d]" },
473 { 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]" },
474 { kX86MovlpsMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x0, 0, 0x0F, 0x13, 0, 0, 0, 0, false }, "MovlpsMR", "[!0r+!1d],!2r" },
475 { 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 -0800476
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700477 { kX86MovhpsRM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0 | REG_USE01, { 0x0, 0, 0x0F, 0x16, 0, 0, 0, 0, false }, "MovhpsRM", "!0r,[!1r+!2d]" },
478 { 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]" },
479 { kX86MovhpsMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x0, 0, 0x0F, 0x17, 0, 0, 0, 0, false }, "MovhpsMR", "[!0r+!1d],!2r" },
480 { 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 -0800481
Brian Carlstrom7940e442013-07-12 13:46:57 -0700482 EXT_0F_ENCODING_MAP(Movdxr, 0x66, 0x6E, REG_DEF0),
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700483 EXT_0F_REX_W_ENCODING_MAP(Movqxr, 0x66, 0x6E, REG_DEF0),
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700484 { kX86MovqrxRR, kRegRegStore, IS_BINARY_OP | REG_DEF0 | REG_USE1, { 0x66, REX_W, 0x0F, 0x7E, 0, 0, 0, 0, false }, "MovqrxRR", "!0r,!1r" },
485 { kX86MovqrxMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, REX_W, 0x0F, 0x7E, 0, 0, 0, 0, false }, "MovqrxMR", "[!0r+!1d],!2r" },
486 { 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 -0700487
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700488 { kX86MovdrxRR, kRegRegStore, IS_BINARY_OP | REG_DEF0 | REG_USE1, { 0x66, 0, 0x0F, 0x7E, 0, 0, 0, 0, false }, "MovdrxRR", "!0r,!1r" },
489 { kX86MovdrxMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, 0, 0x0F, 0x7E, 0, 0, 0, 0, false }, "MovdrxMR", "[!0r+!1d],!2r" },
490 { 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 -0700491
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700492 { kX86MovsxdRR, kRegReg, IS_BINARY_OP | REG_DEF0 | REG_USE1, { REX_W, 0, 0x63, 0, 0, 0, 0, 0, false }, "MovsxdRR", "!0r,!1r" },
493 { 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]" },
494 { 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 -0700495
Mark Mendell2bc47702014-07-31 14:36:54 -0400496 { kX86Set8R, kRegCond, IS_BINARY_OP | REG_DEF0 | REG_USE0 | USES_CCODES, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0, true }, "Set8R", "!1c !0r" },
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700497 { kX86Set8M, kMemCond, IS_STORE | IS_TERTIARY_OP | REG_USE0 | USES_CCODES, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0, false }, "Set8M", "!2c [!0r+!1d]" },
498 { 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 -0700499
500 // TODO: load/store?
501 // Encode the modrm opcode as an extra opcode byte to avoid computation during assembly.
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -0700502 { kX86Lfence, kReg, NO_OPERAND, { 0, 0, 0x0F, 0xAE, 0, 5, 0, 0, false }, "Lfence", "" },
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700503 { kX86Mfence, kReg, NO_OPERAND, { 0, 0, 0x0F, 0xAE, 0, 6, 0, 0, false }, "Mfence", "" },
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -0700504 { kX86Sfence, kReg, NO_OPERAND, { 0, 0, 0x0F, 0xAE, 0, 7, 0, 0, false }, "Sfence", "" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505
Mark Mendell2637f2e2014-04-30 10:10:47 -0400506 EXT_0F_ENCODING_MAP(Imul16, 0x66, 0xAF, REG_USE0 | REG_DEF0 | SETS_CCODES),
507 EXT_0F_ENCODING_MAP(Imul32, 0x00, 0xAF, REG_USE0 | REG_DEF0 | SETS_CCODES),
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700508 EXT_0F_ENCODING_MAP(Imul64, REX_W, 0xAF, REG_USE0 | REG_DEF0 | SETS_CCODES),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700510 { 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" },
511 { 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" },
512 { 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" },
513 { 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" },
514 { 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" },
nikolay serdjukc5e4ce12014-06-10 17:07:10 +0700515 { kX86LockCmpxchg64AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014 | REG_DEFA_USEA | SETS_CCODES, { 0xF0, REX_W, 0x0F, 0xB1, 0, 0, 0, 0, false }, "Lock Cmpxchg", "[!0r+!1r<<!2d+!3d],!4r" },
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700516 { 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]" },
517 { 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]" },
518 { 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 -0700519
Chao-ying Fu021b60f2014-07-09 11:32:31 -0700520 EXT_0F_R8_FORM_ENCODING_MAP(Movzx8, 0x00, 0xB6, REG_DEF0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700521 EXT_0F_ENCODING_MAP(Movzx16, 0x00, 0xB7, REG_DEF0),
Chao-ying Fu021b60f2014-07-09 11:32:31 -0700522 EXT_0F_R8_FORM_ENCODING_MAP(Movsx8, 0x00, 0xBE, REG_DEF0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700523 EXT_0F_ENCODING_MAP(Movsx16, 0x00, 0xBF, REG_DEF0),
Serguei Katkov94f3eb02014-06-24 13:23:17 +0700524 EXT_0F_ENCODING_MAP(Movzx8q, REX_W, 0xB6, REG_DEF0),
525 EXT_0F_ENCODING_MAP(Movzx16q, REX_W, 0xB7, REG_DEF0),
526 EXT_0F_ENCODING_MAP(Movsx8q, REX, 0xBE, REG_DEF0),
527 EXT_0F_ENCODING_MAP(Movsx16q, REX_W, 0xBF, REG_DEF0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700528#undef EXT_0F_ENCODING_MAP
529
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700530 { kX86Jcc8, kJcc, IS_BINARY_OP | IS_BRANCH | NEEDS_FIXUP | USES_CCODES, { 0, 0, 0x70, 0, 0, 0, 0, 0, false }, "Jcc8", "!1c !0t" },
531 { kX86Jcc32, kJcc, IS_BINARY_OP | IS_BRANCH | NEEDS_FIXUP | USES_CCODES, { 0, 0, 0x0F, 0x80, 0, 0, 0, 0, false }, "Jcc32", "!1c !0t" },
532 { kX86Jmp8, kJmp, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0xEB, 0, 0, 0, 0, 0, false }, "Jmp8", "!0t" },
533 { kX86Jmp32, kJmp, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0xE9, 0, 0, 0, 0, 0, false }, "Jmp32", "!0t" },
534 { kX86JmpR, kJmp, IS_UNARY_OP | IS_BRANCH | REG_USE0, { 0, 0, 0xFF, 0, 0, 4, 0, 0, false }, "JmpR", "!0r" },
535 { kX86Jecxz8, kJmp, NO_OPERAND | IS_BRANCH | NEEDS_FIXUP | REG_USEC, { 0, 0, 0xE3, 0, 0, 0, 0, 0, false }, "Jecxz", "!0t" },
536 { kX86JmpT, kJmp, IS_UNARY_OP | IS_BRANCH | IS_LOAD, { THREAD_PREFIX, 0, 0xFF, 0, 0, 4, 0, 0, false }, "JmpT", "fs:[!0d]" },
537 { kX86CallR, kCall, IS_UNARY_OP | IS_BRANCH | REG_USE0, { 0, 0, 0xE8, 0, 0, 0, 0, 0, false }, "CallR", "!0r" },
538 { kX86CallM, kCall, IS_BINARY_OP | IS_BRANCH | IS_LOAD | REG_USE0, { 0, 0, 0xFF, 0, 0, 2, 0, 0, false }, "CallM", "[!0r+!1d]" },
539 { 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]" },
540 { kX86CallT, kCall, IS_UNARY_OP | IS_BRANCH | IS_LOAD, { THREAD_PREFIX, 0, 0xFF, 0, 0, 2, 0, 0, false }, "CallT", "fs:[!0d]" },
541 { kX86CallI, kCall, IS_UNARY_OP | IS_BRANCH, { 0, 0, 0xE8, 0, 0, 0, 0, 4, false }, "CallI", "!0d" },
542 { kX86Ret, kNullary, NO_OPERAND | IS_BRANCH, { 0, 0, 0xC3, 0, 0, 0, 0, 0, false }, "Ret", "" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700543
Mark Mendell5f70c792014-10-29 16:17:11 -0400544 { kX86StartOfMethod, kMacro, IS_UNARY_OP | REG_DEF0 | SETS_CCODES, { 0, 0, 0, 0, 0, 0, 0, 0, false }, "StartOfMethod", "!0r" },
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700545 { 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]" },
Haitao Fenge70f1792014-08-09 08:31:02 +0800546 { kX86PcRelAdr, kPcRel, IS_LOAD | IS_BINARY_OP | REG_DEF0, { 0, 0, 0xB8, 0, 0, 0, 0, 4, false }, "PcRelAdr", "!0r,!1p" },
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700547 { 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 -0700548};
549
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700550std::ostream& operator<<(std::ostream& os, const X86OpCode& rhs) {
551 os << X86Mir2Lir::EncodingMap[rhs].name;
552 return os;
553}
554
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700555static bool NeedsRex(int32_t raw_reg) {
556 return RegStorage::RegNum(raw_reg) > 7;
557}
558
559static uint8_t LowRegisterBits(int32_t raw_reg) {
560 uint8_t low_reg = RegStorage::RegNum(raw_reg) & kRegNumMask32; // 3 bits
561 DCHECK_LT(low_reg, 8);
562 return low_reg;
563}
564
Ian Rogers5aa6e042014-06-13 16:38:24 -0700565static bool HasModrm(const X86EncodingMap* entry) {
566 switch (entry->kind) {
567 case kNullary: return false;
568 case kRegOpcode: return false;
569 default: return true;
570 }
571}
572
573static bool HasSib(const X86EncodingMap* entry) {
574 switch (entry->kind) {
575 case kArray: return true;
576 case kArrayReg: return true;
577 case kRegArray: return true;
578 case kArrayImm: return true;
579 case kRegArrayImm: return true;
580 case kShiftArrayImm: return true;
581 case kShiftArrayCl: return true;
582 case kArrayCond: return true;
583 case kCall:
584 switch (entry->opcode) {
585 case kX86CallA: return true;
586 default: return false;
587 }
Ian Rogers07140832014-09-30 15:43:59 -0700588 case kPcRel:
Ian Rogers5aa6e042014-06-13 16:38:24 -0700589 switch (entry->opcode) {
590 case kX86PcRelLoadRA: return true;
591 default: return false;
592 }
593 default: return false;
594 }
595}
596
597static bool ModrmIsRegReg(const X86EncodingMap* entry) {
598 switch (entry->kind) {
599 // There is no modrm for this kind of instruction, therefore the reg doesn't form part of the
600 // modrm:
601 case kNullary: return true;
602 case kRegOpcode: return true;
603 case kMovRegImm: return true;
604 // Regular modrm value of 3 cases, when there is one register the other register holds an
605 // opcode so the base register is special.
606 case kReg: return true;
607 case kRegReg: return true;
608 case kRegRegStore: return true;
609 case kRegImm: return true;
610 case kRegRegImm: return true;
611 case kRegRegImmStore: return true;
612 case kShiftRegImm: return true;
613 case kShiftRegCl: return true;
614 case kRegCond: return true;
615 case kRegRegCond: return true;
Yixin Shouf40f8902014-08-14 14:10:32 -0400616 case kShiftRegRegCl: return true;
Ian Rogers5aa6e042014-06-13 16:38:24 -0700617 case kJmp:
618 switch (entry->opcode) {
619 case kX86JmpR: return true;
620 default: return false;
621 }
622 case kCall:
623 switch (entry->opcode) {
624 case kX86CallR: return true;
625 default: return false;
626 }
627 default: return false;
628 }
629}
630
Chao-ying Fu021b60f2014-07-09 11:32:31 -0700631static bool IsByteSecondOperand(const X86EncodingMap* entry) {
632 return StartsWith(entry->name, "Movzx8") || StartsWith(entry->name, "Movsx8");
633}
634
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700635size_t X86Mir2Lir::ComputeSize(const X86EncodingMap* entry, int32_t raw_reg, int32_t raw_index,
Ian Rogers5aa6e042014-06-13 16:38:24 -0700636 int32_t raw_base, int32_t displacement) {
637 bool has_modrm = HasModrm(entry);
638 bool has_sib = HasSib(entry);
639 bool r8_form = entry->skeleton.r8_form;
640 bool modrm_is_reg_reg = ModrmIsRegReg(entry);
641 if (has_sib) {
642 DCHECK(!modrm_is_reg_reg);
643 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700644 size_t size = 0;
645 if (entry->skeleton.prefix1 > 0) {
646 ++size;
647 if (entry->skeleton.prefix2 > 0) {
648 ++size;
649 }
650 }
Elena Sayapinadd644502014-07-01 18:39:52 +0700651 if (cu_->target64 || kIsDebugBuild) {
Ian Rogers5aa6e042014-06-13 16:38:24 -0700652 bool registers_need_rex_prefix = NeedsRex(raw_reg) || NeedsRex(raw_index) || NeedsRex(raw_base);
653 if (r8_form) {
654 // Do we need an empty REX prefix to normalize byte registers?
Chao-ying Fu021b60f2014-07-09 11:32:31 -0700655 registers_need_rex_prefix = registers_need_rex_prefix ||
656 (RegStorage::RegNum(raw_reg) >= 4 && !IsByteSecondOperand(entry));
Ian Rogers5aa6e042014-06-13 16:38:24 -0700657 registers_need_rex_prefix = registers_need_rex_prefix ||
658 (modrm_is_reg_reg && (RegStorage::RegNum(raw_base) >= 4));
659 }
660 if (registers_need_rex_prefix) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700661 DCHECK(cu_->target64) << "Attempt to use a 64-bit only addressable register "
Ian Rogers5aa6e042014-06-13 16:38:24 -0700662 << RegStorage::RegNum(raw_reg) << " with instruction " << entry->name;
Serguei Katkov94f3eb02014-06-24 13:23:17 +0700663 if (entry->skeleton.prefix1 != REX_W && entry->skeleton.prefix2 != REX_W
664 && entry->skeleton.prefix1 != REX && entry->skeleton.prefix2 != REX) {
Ian Rogers5aa6e042014-06-13 16:38:24 -0700665 ++size; // rex
666 }
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700667 }
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700668 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669 ++size; // opcode
670 if (entry->skeleton.opcode == 0x0F) {
671 ++size;
672 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
673 ++size;
674 }
675 }
Ian Rogers5aa6e042014-06-13 16:38:24 -0700676 if (has_modrm) {
677 ++size; // modrm
Brian Carlstrom7940e442013-07-12 13:46:57 -0700678 }
Ian Rogers5aa6e042014-06-13 16:38:24 -0700679 if (!modrm_is_reg_reg) {
Ian Rogersb28c1c02014-11-08 11:21:21 -0800680 if (has_sib || (LowRegisterBits(raw_base) == rs_rX86_SP_32.GetRegNum())
Elena Sayapinadd644502014-07-01 18:39:52 +0700681 || (cu_->target64 && entry->skeleton.prefix1 == THREAD_PREFIX)) {
Ian Rogers5aa6e042014-06-13 16:38:24 -0700682 // SP requires a SIB byte.
683 // GS access also needs a SIB byte for absolute adressing in 64-bit mode.
684 ++size;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700685 }
Ian Rogers5aa6e042014-06-13 16:38:24 -0700686 if (displacement != 0 || LowRegisterBits(raw_base) == rs_rBP.GetRegNum()) {
687 // BP requires an explicit displacement, even when it's 0.
Haitao Fenga870bc52014-09-09 15:52:34 +0800688 if (entry->opcode != kX86Lea32RA && entry->opcode != kX86Lea64RA &&
689 entry->opcode != kX86Lea32RM && entry->opcode != kX86Lea64RM) {
Ian Rogers5aa6e042014-06-13 16:38:24 -0700690 DCHECK_NE(entry->flags & (IS_LOAD | IS_STORE), UINT64_C(0)) << entry->name;
691 }
692 size += IS_SIMM8(displacement) ? 1 : 4;
693 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700694 }
695 size += entry->skeleton.immediate_bytes;
696 return size;
697}
698
Ian Rogers5aa6e042014-06-13 16:38:24 -0700699size_t X86Mir2Lir::GetInsnSize(LIR* lir) {
buzbee409fe942013-10-11 10:49:56 -0700700 DCHECK(!IsPseudoLirOp(lir->opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700701 const X86EncodingMap* entry = &X86Mir2Lir::EncodingMap[lir->opcode];
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700702 DCHECK_EQ(entry->opcode, lir->opcode) << entry->name;
Ian Rogers5aa6e042014-06-13 16:38:24 -0700703
Brian Carlstrom7940e442013-07-12 13:46:57 -0700704 switch (entry->kind) {
705 case kData:
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700706 return 4; // 4 bytes of data.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700707 case kNop:
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700708 return lir->operands[0]; // Length of nop is sole operand.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700709 case kNullary:
Ian Rogers5aa6e042014-06-13 16:38:24 -0700710 return ComputeSize(entry, NO_REG, NO_REG, NO_REG, 0);
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100711 case kRegOpcode: // lir operands - 0: reg
Ian Rogers5aa6e042014-06-13 16:38:24 -0700712 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700713 case kReg: // lir operands - 0: reg
Ian Rogers5aa6e042014-06-13 16:38:24 -0700714 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700715 case kMem: // lir operands - 0: base, 1: disp
Ian Rogers5aa6e042014-06-13 16:38:24 -0700716 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], lir->operands[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700717 case kArray: // lir operands - 0: base, 1: index, 2: scale, 3: disp
Ian Rogers5aa6e042014-06-13 16:38:24 -0700718 return ComputeSize(entry, NO_REG, lir->operands[1], lir->operands[0], lir->operands[3]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700719 case kMemReg: // lir operands - 0: base, 1: disp, 2: reg
Ian Rogers5aa6e042014-06-13 16:38:24 -0700720 return ComputeSize(entry, lir->operands[2], NO_REG, lir->operands[0], lir->operands[1]);
Mark Mendell2637f2e2014-04-30 10:10:47 -0400721 case kMemRegImm: // lir operands - 0: base, 1: disp, 2: reg 3: immediate
Ian Rogers5aa6e042014-06-13 16:38:24 -0700722 return ComputeSize(entry, lir->operands[2], NO_REG, lir->operands[0], lir->operands[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700723 case kArrayReg: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700724 return ComputeSize(entry, lir->operands[4], lir->operands[1], lir->operands[0],
Ian Rogers5aa6e042014-06-13 16:38:24 -0700725 lir->operands[3]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700726 case kThreadReg: // lir operands - 0: disp, 1: reg
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700727 // Thread displacement size is always 32bit.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700728 return ComputeSize(entry, lir->operands[1], NO_REG, NO_REG, 0x12345678);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700729 case kRegReg: // lir operands - 0: reg1, 1: reg2
Ian Rogers5aa6e042014-06-13 16:38:24 -0700730 return ComputeSize(entry, lir->operands[0], NO_REG, lir->operands[1], 0);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700731 case kRegRegStore: // lir operands - 0: reg2, 1: reg1
Ian Rogers5aa6e042014-06-13 16:38:24 -0700732 return ComputeSize(entry, lir->operands[1], NO_REG, lir->operands[0], 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700733 case kRegMem: // lir operands - 0: reg, 1: base, 2: disp
Ian Rogers5aa6e042014-06-13 16:38:24 -0700734 return ComputeSize(entry, lir->operands[0], NO_REG, lir->operands[1], lir->operands[2]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700735 case kRegArray: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700736 return ComputeSize(entry, lir->operands[0], lir->operands[2], lir->operands[1],
Ian Rogers5aa6e042014-06-13 16:38:24 -0700737 lir->operands[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700738 case kRegThread: // lir operands - 0: reg, 1: disp
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700739 // Thread displacement size is always 32bit.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700740 return ComputeSize(entry, lir->operands[0], NO_REG, NO_REG, 0x12345678);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700741 case kRegImm: { // lir operands - 0: reg, 1: immediate
Ian Rogers5aa6e042014-06-13 16:38:24 -0700742 size_t size = ComputeSize(entry, lir->operands[0], NO_REG, NO_REG, 0);
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700743 // AX opcodes don't require the modrm byte.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700744 if (entry->skeleton.ax_opcode == 0) {
745 return size;
746 } else {
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700747 return size - (RegStorage::RegNum(lir->operands[0]) == rs_rAX.GetRegNum() ? 1 : 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700748 }
749 }
750 case kMemImm: // lir operands - 0: base, 1: disp, 2: immediate
Ian Rogers5aa6e042014-06-13 16:38:24 -0700751 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], lir->operands[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700752 case kArrayImm: // lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
Ian Rogers5aa6e042014-06-13 16:38:24 -0700753 return ComputeSize(entry, NO_REG, lir->operands[1], lir->operands[0], lir->operands[3]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700754 case kThreadImm: // lir operands - 0: disp, 1: imm
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700755 // Thread displacement size is always 32bit.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700756 return ComputeSize(entry, NO_REG, NO_REG, NO_REG, 0x12345678);
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700757 case kRegRegImm: // lir operands - 0: reg1, 1: reg2, 2: imm
758 // Note: RegRegImm form passes reg2 as index but encodes it using base.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700759 return ComputeSize(entry, lir->operands[0], lir->operands[1], NO_REG, 0);
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700760 case kRegRegImmStore: // lir operands - 0: reg2, 1: reg1, 2: imm
761 // Note: RegRegImmStore form passes reg1 as index but encodes it using base.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700762 return ComputeSize(entry, lir->operands[1], lir->operands[0], NO_REG, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700763 case kRegMemImm: // lir operands - 0: reg, 1: base, 2: disp, 3: imm
Ian Rogers5aa6e042014-06-13 16:38:24 -0700764 return ComputeSize(entry, lir->operands[0], NO_REG, lir->operands[1], lir->operands[2]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700765 case kRegArrayImm: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp, 5: imm
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700766 return ComputeSize(entry, lir->operands[0], lir->operands[2], lir->operands[1],
Ian Rogers5aa6e042014-06-13 16:38:24 -0700767 lir->operands[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700768 case kMovRegImm: // lir operands - 0: reg, 1: immediate
Yixin Shou5192cbb2014-07-01 13:48:17 -0400769 case kMovRegQuadImm:
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700770 return ((entry->skeleton.prefix1 != 0 || NeedsRex(lir->operands[0])) ? 1 : 0) + 1 +
771 entry->skeleton.immediate_bytes;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700772 case kShiftRegImm: // lir operands - 0: reg, 1: immediate
773 // Shift by immediate one has a shorter opcode.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700774 return ComputeSize(entry, lir->operands[0], NO_REG, NO_REG, 0) -
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700775 (lir->operands[1] == 1 ? 1 : 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700776 case kShiftMemImm: // lir operands - 0: base, 1: disp, 2: immediate
777 // Shift by immediate one has a shorter opcode.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700778 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], lir->operands[1]) -
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700779 (lir->operands[2] == 1 ? 1 : 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700780 case kShiftArrayImm: // lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
781 // Shift by immediate one has a shorter opcode.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700782 return ComputeSize(entry, NO_REG, lir->operands[1], lir->operands[0], lir->operands[3]) -
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700783 (lir->operands[4] == 1 ? 1 : 0);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700784 case kShiftRegCl: // lir operands - 0: reg, 1: cl
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700785 DCHECK_EQ(rs_rCX.GetRegNum(), RegStorage::RegNum(lir->operands[1]));
786 // Note: ShiftRegCl form passes reg as reg but encodes it using base.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700787 return ComputeSize(entry, lir->operands[0], NO_REG, NO_REG, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700788 case kShiftMemCl: // lir operands - 0: base, 1: disp, 2: cl
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700789 DCHECK_EQ(rs_rCX.GetRegNum(), RegStorage::RegNum(lir->operands[2]));
Ian Rogers5aa6e042014-06-13 16:38:24 -0700790 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], lir->operands[1]);
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700791 case kShiftArrayCl: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: cl
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700792 DCHECK_EQ(rs_rCX.GetRegNum(), RegStorage::RegNum(lir->operands[4]));
793 return ComputeSize(entry, lir->operands[4], lir->operands[1], lir->operands[0],
Ian Rogers5aa6e042014-06-13 16:38:24 -0700794 lir->operands[3]);
Yixin Shouf40f8902014-08-14 14:10:32 -0400795 case kShiftRegRegCl: // lir operands - 0: reg1, 1: reg2, 2: cl
796 DCHECK_EQ(rs_rCX.GetRegNum(), RegStorage::RegNum(lir->operands[2]));
797 return ComputeSize(entry, lir->operands[0], NO_REG, lir->operands[1], 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700798 case kRegCond: // lir operands - 0: reg, 1: cond
Ian Rogers5aa6e042014-06-13 16:38:24 -0700799 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700800 case kMemCond: // lir operands - 0: base, 1: disp, 2: cond
Ian Rogers5aa6e042014-06-13 16:38:24 -0700801 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], lir->operands[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700802 case kArrayCond: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: cond
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700803 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -0700804 return ComputeSize(entry, NO_REG, lir->operands[1], lir->operands[0], lir->operands[3]);
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700805 case kRegRegCond: // lir operands - 0: reg1, 1: reg2, 2: cond
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700806 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -0700807 return ComputeSize(entry, lir->operands[0], NO_REG, lir->operands[1], 0);
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700808 case kRegMemCond: // lir operands - 0: reg, 1: base, 2: disp, 3:cond
809 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -0700810 return ComputeSize(entry, lir->operands[0], NO_REG, lir->operands[1], lir->operands[2]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700811 case kJcc:
812 if (lir->opcode == kX86Jcc8) {
813 return 2; // opcode + rel8
814 } else {
815 DCHECK(lir->opcode == kX86Jcc32);
816 return 6; // 2 byte opcode + rel32
817 }
818 case kJmp:
Mark Mendell4028a6c2014-02-19 20:06:20 -0800819 if (lir->opcode == kX86Jmp8 || lir->opcode == kX86Jecxz8) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700820 return 2; // opcode + rel8
821 } else if (lir->opcode == kX86Jmp32) {
822 return 5; // opcode + rel32
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700823 } else if (lir->opcode == kX86JmpT) {
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700824 // Thread displacement size is always 32bit.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700825 return ComputeSize(entry, NO_REG, NO_REG, NO_REG, 0x12345678);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700826 } else {
827 DCHECK(lir->opcode == kX86JmpR);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700828 if (NeedsRex(lir->operands[0])) {
829 return 3; // REX.B + opcode + modrm
830 } else {
831 return 2; // opcode + modrm
832 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700833 }
834 case kCall:
835 switch (lir->opcode) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800836 case kX86CallI: return 5; // opcode 0:disp
Brian Carlstrom7940e442013-07-12 13:46:57 -0700837 case kX86CallR: return 2; // opcode modrm
838 case kX86CallM: // lir operands - 0: base, 1: disp
Ian Rogers5aa6e042014-06-13 16:38:24 -0700839 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], lir->operands[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700840 case kX86CallA: // lir operands - 0: base, 1: index, 2: scale, 3: disp
Ian Rogers5aa6e042014-06-13 16:38:24 -0700841 return ComputeSize(entry, NO_REG, lir->operands[1], lir->operands[0], lir->operands[3]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700842 case kX86CallT: // lir operands - 0: disp
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700843 // Thread displacement size is always 32bit.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700844 return ComputeSize(entry, NO_REG, NO_REG, NO_REG, 0x12345678);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700845 default:
846 break;
847 }
848 break;
849 case kPcRel:
850 if (entry->opcode == kX86PcRelLoadRA) {
851 // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700852 // Force the displacement size to 32bit, it will hold a computed offset later.
853 return ComputeSize(entry, lir->operands[0], lir->operands[2], lir->operands[1],
Ian Rogers5aa6e042014-06-13 16:38:24 -0700854 0x12345678);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700855 } else {
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700856 DCHECK_EQ(entry->opcode, kX86PcRelAdr);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700857 return 5; // opcode with reg + 4 byte immediate
Brian Carlstrom7940e442013-07-12 13:46:57 -0700858 }
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700859 case kMacro: // lir operands - 0: reg
Brian Carlstrom7940e442013-07-12 13:46:57 -0700860 DCHECK_EQ(lir->opcode, static_cast<int>(kX86StartOfMethod));
861 return 5 /* call opcode + 4 byte displacement */ + 1 /* pop reg */ +
Elena Sayapinadd644502014-07-01 18:39:52 +0700862 ComputeSize(&X86Mir2Lir::EncodingMap[cu_->target64 ? kX86Sub64RI : kX86Sub32RI],
Ian Rogers5aa6e042014-06-13 16:38:24 -0700863 lir->operands[0], NO_REG, NO_REG, 0) -
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700864 // Shorter ax encoding.
865 (RegStorage::RegNum(lir->operands[0]) == rs_rAX.GetRegNum() ? 1 : 0);
866 case kUnimplemented:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700867 break;
868 }
869 UNIMPLEMENTED(FATAL) << "Unimplemented size encoding for: " << entry->name;
870 return 0;
871}
872
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700873static uint8_t ModrmForDisp(int base, int disp) {
874 // BP requires an explicit disp, so do not omit it in the 0 case
875 if (disp == 0 && RegStorage::RegNum(base) != rs_rBP.GetRegNum()) {
876 return 0;
877 } else if (IS_SIMM8(disp)) {
878 return 1;
879 } else {
880 return 2;
881 }
882}
883
884void X86Mir2Lir::CheckValidByteRegister(const X86EncodingMap* entry, int32_t raw_reg) {
885 if (kIsDebugBuild) {
886 // Sanity check r8_form is correctly specified.
887 if (entry->skeleton.r8_form) {
888 CHECK(strchr(entry->name, '8') != nullptr) << entry->name;
889 } else {
890 if (entry->skeleton.immediate_bytes != 1) { // Ignore ...I8 instructions.
Serguei Katkov1c557032014-06-23 13:23:38 +0700891 if (!StartsWith(entry->name, "Movzx8") && !StartsWith(entry->name, "Movsx8")
892 && !StartsWith(entry->name, "Movzx8q") && !StartsWith(entry->name, "Movsx8q")) {
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700893 CHECK(strchr(entry->name, '8') == nullptr) << entry->name;
894 }
895 }
896 }
897 if (RegStorage::RegNum(raw_reg) >= 4) {
898 // ah, bh, ch and dh are not valid registers in 32-bit.
Elena Sayapinadd644502014-07-01 18:39:52 +0700899 CHECK(cu_->target64 || !entry->skeleton.r8_form)
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700900 << "Invalid register " << static_cast<int>(RegStorage::RegNum(raw_reg))
901 << " for instruction " << entry->name << " in "
902 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
903 }
904 }
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700905}
906
907void X86Mir2Lir::EmitPrefix(const X86EncodingMap* entry,
Ian Rogers5aa6e042014-06-13 16:38:24 -0700908 int32_t raw_reg_r, int32_t raw_reg_x, int32_t raw_reg_b) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700909 // REX.WRXB
910 // W - 64-bit operand
911 // R - MODRM.reg
912 // X - SIB.index
913 // B - MODRM.rm/SIB.base
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700914 bool w = (entry->skeleton.prefix1 == REX_W) || (entry->skeleton.prefix2 == REX_W);
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700915 bool r = NeedsRex(raw_reg_r);
916 bool x = NeedsRex(raw_reg_x);
917 bool b = NeedsRex(raw_reg_b);
Ian Rogers5aa6e042014-06-13 16:38:24 -0700918 bool r8_form = entry->skeleton.r8_form;
919 bool modrm_is_reg_reg = ModrmIsRegReg(entry);
920
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700921 uint8_t rex = 0;
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700922 if (r8_form) {
923 // Do we need an empty REX prefix to normalize byte register addressing?
Chao-ying Fu021b60f2014-07-09 11:32:31 -0700924 if (RegStorage::RegNum(raw_reg_r) >= 4 && !IsByteSecondOperand(entry)) {
Razvan A Lupusoruae9f3e62014-09-23 14:54:32 -0700925 rex |= REX; // REX.0000
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700926 } else if (modrm_is_reg_reg && RegStorage::RegNum(raw_reg_b) >= 4) {
Razvan A Lupusoruae9f3e62014-09-23 14:54:32 -0700927 rex |= REX; // REX.0000
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700928 }
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700929 }
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700930 if (w) {
Razvan A Lupusoruae9f3e62014-09-23 14:54:32 -0700931 rex |= REX_W; // REX.W000
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700932 }
933 if (r) {
Razvan A Lupusoruae9f3e62014-09-23 14:54:32 -0700934 rex |= REX_R; // REX.0R00
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700935 }
936 if (x) {
Razvan A Lupusoruae9f3e62014-09-23 14:54:32 -0700937 rex |= REX_X; // REX.00X0
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700938 }
939 if (b) {
Razvan A Lupusoruae9f3e62014-09-23 14:54:32 -0700940 rex |= REX_B; // REX.000B
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700941 }
Vladimir Marko057c74a2013-12-03 15:20:45 +0000942 if (entry->skeleton.prefix1 != 0) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700943 if (cu_->target64 && entry->skeleton.prefix1 == THREAD_PREFIX) {
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700944 // 64 bit addresses by GS, not FS.
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700945 code_buffer_.push_back(THREAD_PREFIX_GS);
946 } else {
Serguei Katkov94f3eb02014-06-24 13:23:17 +0700947 if (entry->skeleton.prefix1 == REX_W || entry->skeleton.prefix1 == REX) {
948 DCHECK(cu_->target64);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700949 rex |= entry->skeleton.prefix1;
950 code_buffer_.push_back(rex);
951 rex = 0;
952 } else {
953 code_buffer_.push_back(entry->skeleton.prefix1);
954 }
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700955 }
Vladimir Marko057c74a2013-12-03 15:20:45 +0000956 if (entry->skeleton.prefix2 != 0) {
Serguei Katkov94f3eb02014-06-24 13:23:17 +0700957 if (entry->skeleton.prefix2 == REX_W || entry->skeleton.prefix1 == REX) {
958 DCHECK(cu_->target64);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700959 rex |= entry->skeleton.prefix2;
960 code_buffer_.push_back(rex);
961 rex = 0;
962 } else {
963 code_buffer_.push_back(entry->skeleton.prefix2);
964 }
Vladimir Marko057c74a2013-12-03 15:20:45 +0000965 }
966 } else {
967 DCHECK_EQ(0, entry->skeleton.prefix2);
968 }
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700969 if (rex != 0) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700970 DCHECK(cu_->target64);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700971 code_buffer_.push_back(rex);
972 }
Vladimir Marko057c74a2013-12-03 15:20:45 +0000973}
974
975void X86Mir2Lir::EmitOpcode(const X86EncodingMap* entry) {
976 code_buffer_.push_back(entry->skeleton.opcode);
977 if (entry->skeleton.opcode == 0x0F) {
978 code_buffer_.push_back(entry->skeleton.extra_opcode1);
979 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
980 code_buffer_.push_back(entry->skeleton.extra_opcode2);
981 } else {
982 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
983 }
984 } else {
985 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
986 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
987 }
988}
989
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700990void X86Mir2Lir::EmitPrefixAndOpcode(const X86EncodingMap* entry,
Ian Rogers5aa6e042014-06-13 16:38:24 -0700991 int32_t raw_reg_r, int32_t raw_reg_x, int32_t raw_reg_b) {
992 EmitPrefix(entry, raw_reg_r, raw_reg_x, raw_reg_b);
Vladimir Marko057c74a2013-12-03 15:20:45 +0000993 EmitOpcode(entry);
994}
995
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700996void X86Mir2Lir::EmitDisp(uint8_t base, int32_t disp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700997 // BP requires an explicit disp, so do not omit it in the 0 case
buzbee091cc402014-03-31 10:14:40 -0700998 if (disp == 0 && RegStorage::RegNum(base) != rs_rBP.GetRegNum()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700999 return;
1000 } else if (IS_SIMM8(disp)) {
1001 code_buffer_.push_back(disp & 0xFF);
1002 } else {
1003 code_buffer_.push_back(disp & 0xFF);
1004 code_buffer_.push_back((disp >> 8) & 0xFF);
1005 code_buffer_.push_back((disp >> 16) & 0xFF);
1006 code_buffer_.push_back((disp >> 24) & 0xFF);
1007 }
1008}
1009
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001010void X86Mir2Lir::EmitModrmThread(uint8_t reg_or_opcode) {
Elena Sayapinadd644502014-07-01 18:39:52 +07001011 if (cu_->target64) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001012 // Absolute adressing for GS access.
Ian Rogersb28c1c02014-11-08 11:21:21 -08001013 uint8_t modrm = (0 << 6) | (reg_or_opcode << 3) | rs_rX86_SP_32.GetRegNum();
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001014 code_buffer_.push_back(modrm);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001015 uint8_t sib = (0/*TIMES_1*/ << 6) | (rs_rX86_SP_32.GetRegNum() << 3) | rs_rBP.GetRegNum();
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001016 code_buffer_.push_back(sib);
1017 } else {
1018 uint8_t modrm = (0 << 6) | (reg_or_opcode << 3) | rs_rBP.GetRegNum();
1019 code_buffer_.push_back(modrm);
1020 }
1021}
1022
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001023void X86Mir2Lir::EmitModrmDisp(uint8_t reg_or_opcode, uint8_t base, int32_t disp) {
1024 DCHECK_LT(reg_or_opcode, 8);
1025 DCHECK_LT(base, 8);
1026 uint8_t modrm = (ModrmForDisp(base, disp) << 6) | (reg_or_opcode << 3) | base;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001027 code_buffer_.push_back(modrm);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001028 if (base == rs_rX86_SP_32.GetRegNum()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001029 // Special SIB for SP base
Ian Rogersb28c1c02014-11-08 11:21:21 -08001030 code_buffer_.push_back(0 << 6 | rs_rX86_SP_32.GetRegNum() << 3 | rs_rX86_SP_32.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001031 }
1032 EmitDisp(base, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001033}
1034
Vladimir Marko057c74a2013-12-03 15:20:45 +00001035void X86Mir2Lir::EmitModrmSibDisp(uint8_t reg_or_opcode, uint8_t base, uint8_t index,
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001036 int scale, int32_t disp) {
buzbee091cc402014-03-31 10:14:40 -07001037 DCHECK_LT(RegStorage::RegNum(reg_or_opcode), 8);
1038 uint8_t modrm = (ModrmForDisp(base, disp) << 6) | RegStorage::RegNum(reg_or_opcode) << 3 |
Ian Rogersb28c1c02014-11-08 11:21:21 -08001039 rs_rX86_SP_32.GetRegNum();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001040 code_buffer_.push_back(modrm);
1041 DCHECK_LT(scale, 4);
buzbee091cc402014-03-31 10:14:40 -07001042 DCHECK_LT(RegStorage::RegNum(index), 8);
1043 DCHECK_LT(RegStorage::RegNum(base), 8);
1044 uint8_t sib = (scale << 6) | (RegStorage::RegNum(index) << 3) | RegStorage::RegNum(base);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001045 code_buffer_.push_back(sib);
1046 EmitDisp(base, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001047}
1048
Dmitry Petrochenko96992e82014-05-20 04:03:46 +07001049void X86Mir2Lir::EmitImm(const X86EncodingMap* entry, int64_t imm) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001050 switch (entry->skeleton.immediate_bytes) {
1051 case 1:
1052 DCHECK(IS_SIMM8(imm));
1053 code_buffer_.push_back(imm & 0xFF);
1054 break;
1055 case 2:
1056 DCHECK(IS_SIMM16(imm));
1057 code_buffer_.push_back(imm & 0xFF);
1058 code_buffer_.push_back((imm >> 8) & 0xFF);
1059 break;
1060 case 4:
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001061 DCHECK(IS_SIMM32(imm));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001062 code_buffer_.push_back(imm & 0xFF);
1063 code_buffer_.push_back((imm >> 8) & 0xFF);
1064 code_buffer_.push_back((imm >> 16) & 0xFF);
1065 code_buffer_.push_back((imm >> 24) & 0xFF);
1066 break;
Dmitry Petrochenko96992e82014-05-20 04:03:46 +07001067 case 8:
1068 code_buffer_.push_back(imm & 0xFF);
1069 code_buffer_.push_back((imm >> 8) & 0xFF);
1070 code_buffer_.push_back((imm >> 16) & 0xFF);
1071 code_buffer_.push_back((imm >> 24) & 0xFF);
1072 code_buffer_.push_back((imm >> 32) & 0xFF);
1073 code_buffer_.push_back((imm >> 40) & 0xFF);
1074 code_buffer_.push_back((imm >> 48) & 0xFF);
1075 code_buffer_.push_back((imm >> 56) & 0xFF);
1076 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001077 default:
1078 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
1079 << ") for instruction: " << entry->name;
1080 break;
1081 }
1082}
1083
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001084void X86Mir2Lir::EmitNullary(const X86EncodingMap* entry) {
1085 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001086 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, NO_REG);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001087 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001088 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1089 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1090}
1091
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001092void X86Mir2Lir::EmitOpRegOpcode(const X86EncodingMap* entry, int32_t raw_reg) {
1093 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001094 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, raw_reg);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001095 // There's no 3-byte instruction with +rd
1096 DCHECK(entry->skeleton.opcode != 0x0F ||
1097 (entry->skeleton.extra_opcode1 != 0x38 && entry->skeleton.extra_opcode1 != 0x3A));
1098 DCHECK(!RegStorage::IsFloat(raw_reg));
1099 uint8_t low_reg = LowRegisterBits(raw_reg);
1100 code_buffer_.back() += low_reg;
1101 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1102 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1103}
1104
1105void X86Mir2Lir::EmitOpReg(const X86EncodingMap* entry, int32_t raw_reg) {
1106 CheckValidByteRegister(entry, raw_reg);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001107 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, raw_reg);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001108 uint8_t low_reg = LowRegisterBits(raw_reg);
1109 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | low_reg;
Vladimir Marko057c74a2013-12-03 15:20:45 +00001110 code_buffer_.push_back(modrm);
1111 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1112 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1113}
1114
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001115void X86Mir2Lir::EmitOpMem(const X86EncodingMap* entry, int32_t raw_base, int32_t disp) {
1116 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001117 EmitPrefix(entry, NO_REG, NO_REG, raw_base);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001118 code_buffer_.push_back(entry->skeleton.opcode);
1119 DCHECK_NE(0x0F, entry->skeleton.opcode);
1120 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1121 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001122 uint8_t low_base = LowRegisterBits(raw_base);
1123 EmitModrmDisp(entry->skeleton.modrm_opcode, low_base, disp);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001124 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1125 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1126}
1127
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001128void X86Mir2Lir::EmitOpArray(const X86EncodingMap* entry, int32_t raw_base, int32_t raw_index,
1129 int scale, int32_t disp) {
1130 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001131 EmitPrefixAndOpcode(entry, NO_REG, raw_index, raw_base);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001132 uint8_t low_index = LowRegisterBits(raw_index);
1133 uint8_t low_base = LowRegisterBits(raw_base);
1134 EmitModrmSibDisp(entry->skeleton.modrm_opcode, low_base, low_index, scale, disp);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001135 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1136 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1137}
1138
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001139void X86Mir2Lir::EmitMemReg(const X86EncodingMap* entry, int32_t raw_base, int32_t disp,
1140 int32_t raw_reg) {
1141 CheckValidByteRegister(entry, raw_reg);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001142 EmitPrefixAndOpcode(entry, raw_reg, NO_REG, raw_base);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001143 uint8_t low_reg = LowRegisterBits(raw_reg);
1144 uint8_t low_base = LowRegisterBits(raw_base);
1145 EmitModrmDisp(low_reg, low_base, disp);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001146 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1147 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1148 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1149}
1150
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001151void X86Mir2Lir::EmitRegMem(const X86EncodingMap* entry, int32_t raw_reg, int32_t raw_base,
1152 int32_t disp) {
Vladimir Marko057c74a2013-12-03 15:20:45 +00001153 // Opcode will flip operands.
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001154 EmitMemReg(entry, raw_base, disp, raw_reg);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001155}
1156
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001157void X86Mir2Lir::EmitRegArray(const X86EncodingMap* entry, int32_t raw_reg, int32_t raw_base,
1158 int32_t raw_index, int scale, int32_t disp) {
1159 CheckValidByteRegister(entry, raw_reg);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001160 EmitPrefixAndOpcode(entry, raw_reg, raw_index, raw_base);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001161 uint8_t low_reg = LowRegisterBits(raw_reg);
1162 uint8_t low_index = LowRegisterBits(raw_index);
1163 uint8_t low_base = LowRegisterBits(raw_base);
1164 EmitModrmSibDisp(low_reg, low_base, low_index, scale, disp);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001165 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1166 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1167 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1168}
1169
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001170void X86Mir2Lir::EmitArrayReg(const X86EncodingMap* entry, int32_t raw_base, int32_t raw_index,
1171 int scale, int32_t disp, int32_t raw_reg) {
Vladimir Marko057c74a2013-12-03 15:20:45 +00001172 // Opcode will flip operands.
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001173 EmitRegArray(entry, raw_reg, raw_base, raw_index, scale, disp);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001174}
1175
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001176void X86Mir2Lir::EmitMemImm(const X86EncodingMap* entry, int32_t raw_base, int32_t disp,
1177 int32_t imm) {
1178 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001179 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, raw_base);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001180 uint8_t low_base = LowRegisterBits(raw_base);
1181 EmitModrmDisp(entry->skeleton.modrm_opcode, low_base, disp);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001182 DCHECK_EQ(0, entry->skeleton.ax_opcode);
Mark Mendell9ed42772014-05-07 17:26:12 -04001183 EmitImm(entry, imm);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001184}
1185
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001186void X86Mir2Lir::EmitArrayImm(const X86EncodingMap* entry,
1187 int32_t raw_base, int32_t raw_index, int scale, int32_t disp,
1188 int32_t imm) {
1189 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001190 EmitPrefixAndOpcode(entry, NO_REG, raw_index, raw_base);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001191 uint8_t low_index = LowRegisterBits(raw_index);
1192 uint8_t low_base = LowRegisterBits(raw_base);
1193 EmitModrmSibDisp(entry->skeleton.modrm_opcode, low_base, low_index, scale, disp);
1194 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1195 EmitImm(entry, imm);
1196}
1197
1198void X86Mir2Lir::EmitRegThread(const X86EncodingMap* entry, int32_t raw_reg, int32_t disp) {
1199 DCHECK_EQ(false, entry->skeleton.r8_form);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001200 DCHECK_NE(entry->skeleton.prefix1, 0);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001201 EmitPrefixAndOpcode(entry, raw_reg, NO_REG, NO_REG);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001202 uint8_t low_reg = LowRegisterBits(raw_reg);
1203 EmitModrmThread(low_reg);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001204 code_buffer_.push_back(disp & 0xFF);
1205 code_buffer_.push_back((disp >> 8) & 0xFF);
1206 code_buffer_.push_back((disp >> 16) & 0xFF);
1207 code_buffer_.push_back((disp >> 24) & 0xFF);
1208 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1209 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1210 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1211}
1212
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001213void X86Mir2Lir::EmitRegReg(const X86EncodingMap* entry, int32_t raw_reg1, int32_t raw_reg2) {
Chao-ying Fu021b60f2014-07-09 11:32:31 -07001214 if (!IsByteSecondOperand(entry)) {
1215 CheckValidByteRegister(entry, raw_reg1);
1216 }
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001217 CheckValidByteRegister(entry, raw_reg2);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001218 EmitPrefixAndOpcode(entry, raw_reg1, NO_REG, raw_reg2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001219 uint8_t low_reg1 = LowRegisterBits(raw_reg1);
1220 uint8_t low_reg2 = LowRegisterBits(raw_reg2);
1221 uint8_t modrm = (3 << 6) | (low_reg1 << 3) | low_reg2;
Vladimir Marko057c74a2013-12-03 15:20:45 +00001222 code_buffer_.push_back(modrm);
1223 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1224 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1225 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1226}
1227
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001228void X86Mir2Lir::EmitRegRegImm(const X86EncodingMap* entry, int32_t raw_reg1, int32_t raw_reg2,
1229 int32_t imm) {
1230 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001231 EmitPrefixAndOpcode(entry, raw_reg1, NO_REG, raw_reg2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001232 uint8_t low_reg1 = LowRegisterBits(raw_reg1);
1233 uint8_t low_reg2 = LowRegisterBits(raw_reg2);
1234 uint8_t modrm = (3 << 6) | (low_reg1 << 3) | low_reg2;
Vladimir Marko057c74a2013-12-03 15:20:45 +00001235 code_buffer_.push_back(modrm);
1236 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1237 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1238 EmitImm(entry, imm);
1239}
1240
Mark Mendell4708dcd2014-01-22 09:05:18 -08001241void X86Mir2Lir::EmitRegMemImm(const X86EncodingMap* entry,
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001242 int32_t raw_reg, int32_t raw_base, int disp, int32_t imm) {
1243 DCHECK(!RegStorage::IsFloat(raw_reg));
1244 CheckValidByteRegister(entry, raw_reg);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001245 EmitPrefixAndOpcode(entry, raw_reg, NO_REG, raw_base);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001246 uint8_t low_reg = LowRegisterBits(raw_reg);
1247 uint8_t low_base = LowRegisterBits(raw_base);
1248 EmitModrmDisp(low_reg, low_base, disp);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001249 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1250 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1251 EmitImm(entry, imm);
1252}
1253
Mark Mendell2637f2e2014-04-30 10:10:47 -04001254void X86Mir2Lir::EmitMemRegImm(const X86EncodingMap* entry,
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001255 int32_t raw_base, int32_t disp, int32_t raw_reg, int32_t imm) {
1256 // Opcode will flip operands.
1257 EmitRegMemImm(entry, raw_reg, raw_base, disp, imm);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001258}
1259
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001260void X86Mir2Lir::EmitRegImm(const X86EncodingMap* entry, int32_t raw_reg, int32_t imm) {
1261 CheckValidByteRegister(entry, raw_reg);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001262 EmitPrefix(entry, NO_REG, NO_REG, raw_reg);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001263 if (RegStorage::RegNum(raw_reg) == rs_rAX.GetRegNum() && entry->skeleton.ax_opcode != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001264 code_buffer_.push_back(entry->skeleton.ax_opcode);
1265 } else {
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001266 uint8_t low_reg = LowRegisterBits(raw_reg);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001267 EmitOpcode(entry);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001268 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | low_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001269 code_buffer_.push_back(modrm);
1270 }
Vladimir Marko057c74a2013-12-03 15:20:45 +00001271 EmitImm(entry, imm);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001272}
1273
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001274void X86Mir2Lir::EmitThreadImm(const X86EncodingMap* entry, int32_t disp, int32_t imm) {
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07001275 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001276 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, NO_REG);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001277 EmitModrmThread(entry->skeleton.modrm_opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001278 code_buffer_.push_back(disp & 0xFF);
1279 code_buffer_.push_back((disp >> 8) & 0xFF);
1280 code_buffer_.push_back((disp >> 16) & 0xFF);
1281 code_buffer_.push_back((disp >> 24) & 0xFF);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001282 EmitImm(entry, imm);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001283 DCHECK_EQ(entry->skeleton.ax_opcode, 0);
1284}
1285
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001286void X86Mir2Lir::EmitMovRegImm(const X86EncodingMap* entry, int32_t raw_reg, int64_t imm) {
1287 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001288 EmitPrefix(entry, NO_REG, NO_REG, raw_reg);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001289 uint8_t low_reg = LowRegisterBits(raw_reg);
1290 code_buffer_.push_back(0xB8 + low_reg);
Dmitry Petrochenko96992e82014-05-20 04:03:46 +07001291 switch (entry->skeleton.immediate_bytes) {
1292 case 4:
1293 code_buffer_.push_back(imm & 0xFF);
1294 code_buffer_.push_back((imm >> 8) & 0xFF);
1295 code_buffer_.push_back((imm >> 16) & 0xFF);
1296 code_buffer_.push_back((imm >> 24) & 0xFF);
1297 break;
1298 case 8:
1299 code_buffer_.push_back(imm & 0xFF);
1300 code_buffer_.push_back((imm >> 8) & 0xFF);
1301 code_buffer_.push_back((imm >> 16) & 0xFF);
1302 code_buffer_.push_back((imm >> 24) & 0xFF);
1303 code_buffer_.push_back((imm >> 32) & 0xFF);
1304 code_buffer_.push_back((imm >> 40) & 0xFF);
1305 code_buffer_.push_back((imm >> 48) & 0xFF);
1306 code_buffer_.push_back((imm >> 56) & 0xFF);
1307 break;
1308 default:
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001309 LOG(FATAL) << "Unsupported immediate size for EmitMovRegImm: "
1310 << static_cast<uint32_t>(entry->skeleton.immediate_bytes);
Dmitry Petrochenko96992e82014-05-20 04:03:46 +07001311 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001312}
1313
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001314void X86Mir2Lir::EmitShiftRegImm(const X86EncodingMap* entry, int32_t raw_reg, int32_t imm) {
1315 CheckValidByteRegister(entry, raw_reg);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001316 EmitPrefix(entry, NO_REG, NO_REG, raw_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001317 if (imm != 1) {
1318 code_buffer_.push_back(entry->skeleton.opcode);
1319 } else {
1320 // Shorter encoding for 1 bit shift
1321 code_buffer_.push_back(entry->skeleton.ax_opcode);
1322 }
Vladimir Marko057c74a2013-12-03 15:20:45 +00001323 DCHECK_NE(0x0F, entry->skeleton.opcode);
1324 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1325 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001326 uint8_t low_reg = LowRegisterBits(raw_reg);
1327 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | low_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001328 code_buffer_.push_back(modrm);
1329 if (imm != 1) {
1330 DCHECK_EQ(entry->skeleton.immediate_bytes, 1);
1331 DCHECK(IS_SIMM8(imm));
1332 code_buffer_.push_back(imm & 0xFF);
1333 }
1334}
1335
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001336void X86Mir2Lir::EmitShiftRegCl(const X86EncodingMap* entry, int32_t raw_reg, int32_t raw_cl) {
1337 CheckValidByteRegister(entry, raw_reg);
1338 DCHECK_EQ(rs_rCX.GetRegNum(), RegStorage::RegNum(raw_cl));
Ian Rogers5aa6e042014-06-13 16:38:24 -07001339 EmitPrefix(entry, NO_REG, NO_REG, raw_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001340 code_buffer_.push_back(entry->skeleton.opcode);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001341 DCHECK_NE(0x0F, entry->skeleton.opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001342 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1343 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001344 uint8_t low_reg = LowRegisterBits(raw_reg);
1345 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | low_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001346 code_buffer_.push_back(modrm);
1347 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1348 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1349}
1350
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001351void X86Mir2Lir::EmitShiftMemCl(const X86EncodingMap* entry, int32_t raw_base,
1352 int32_t displacement, int32_t raw_cl) {
1353 DCHECK_EQ(false, entry->skeleton.r8_form);
1354 DCHECK_EQ(rs_rCX.GetRegNum(), RegStorage::RegNum(raw_cl));
Ian Rogers5aa6e042014-06-13 16:38:24 -07001355 EmitPrefix(entry, NO_REG, NO_REG, raw_base);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001356 code_buffer_.push_back(entry->skeleton.opcode);
1357 DCHECK_NE(0x0F, entry->skeleton.opcode);
1358 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1359 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001360 uint8_t low_base = LowRegisterBits(raw_base);
1361 EmitModrmDisp(entry->skeleton.modrm_opcode, low_base, displacement);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001362 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1363 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1364}
1365
Yixin Shouf40f8902014-08-14 14:10:32 -04001366void X86Mir2Lir::EmitShiftRegRegCl(const X86EncodingMap* entry, int32_t raw_reg1, int32_t raw_reg2, int32_t raw_cl) {
1367 DCHECK_EQ(false, entry->skeleton.r8_form);
1368 DCHECK_EQ(rs_rCX.GetRegNum(), RegStorage::RegNum(raw_cl));
1369 EmitPrefixAndOpcode(entry, raw_reg1, NO_REG, raw_reg2);
1370 uint8_t low_reg1 = LowRegisterBits(raw_reg1);
1371 uint8_t low_reg2 = LowRegisterBits(raw_reg2);
1372 uint8_t modrm = (3 << 6) | (low_reg1 << 3) | low_reg2;
1373 code_buffer_.push_back(modrm);
1374 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1375 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1376 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1377}
1378
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001379void X86Mir2Lir::EmitShiftMemImm(const X86EncodingMap* entry, int32_t raw_base, int32_t disp,
1380 int32_t imm) {
1381 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001382 EmitPrefix(entry, NO_REG, NO_REG, raw_base);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001383 if (imm != 1) {
1384 code_buffer_.push_back(entry->skeleton.opcode);
1385 } else {
1386 // Shorter encoding for 1 bit shift
1387 code_buffer_.push_back(entry->skeleton.ax_opcode);
1388 }
1389 DCHECK_NE(0x0F, entry->skeleton.opcode);
1390 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1391 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001392 uint8_t low_base = LowRegisterBits(raw_base);
1393 EmitModrmDisp(entry->skeleton.modrm_opcode, low_base, disp);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001394 if (imm != 1) {
1395 DCHECK_EQ(entry->skeleton.immediate_bytes, 1);
1396 DCHECK(IS_SIMM8(imm));
1397 code_buffer_.push_back(imm & 0xFF);
1398 }
1399}
1400
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001401void X86Mir2Lir::EmitRegCond(const X86EncodingMap* entry, int32_t raw_reg, int32_t cc) {
1402 CheckValidByteRegister(entry, raw_reg);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001403 EmitPrefix(entry, NO_REG, NO_REG, raw_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001404 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1405 DCHECK_EQ(0x0F, entry->skeleton.opcode);
1406 code_buffer_.push_back(0x0F);
1407 DCHECK_EQ(0x90, entry->skeleton.extra_opcode1);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001408 DCHECK_GE(cc, 0);
1409 DCHECK_LT(cc, 16);
1410 code_buffer_.push_back(0x90 | cc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001411 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001412 uint8_t low_reg = LowRegisterBits(raw_reg);
1413 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | low_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001414 code_buffer_.push_back(modrm);
1415 DCHECK_EQ(entry->skeleton.immediate_bytes, 0);
1416}
1417
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001418void X86Mir2Lir::EmitMemCond(const X86EncodingMap* entry, int32_t raw_base, int32_t disp,
1419 int32_t cc) {
1420 DCHECK_EQ(false, entry->skeleton.r8_form);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001421 if (entry->skeleton.prefix1 != 0) {
1422 code_buffer_.push_back(entry->skeleton.prefix1);
1423 if (entry->skeleton.prefix2 != 0) {
1424 code_buffer_.push_back(entry->skeleton.prefix2);
1425 }
1426 } else {
1427 DCHECK_EQ(0, entry->skeleton.prefix2);
1428 }
1429 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1430 DCHECK_EQ(0x0F, entry->skeleton.opcode);
1431 code_buffer_.push_back(0x0F);
1432 DCHECK_EQ(0x90, entry->skeleton.extra_opcode1);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001433 DCHECK_GE(cc, 0);
1434 DCHECK_LT(cc, 16);
1435 code_buffer_.push_back(0x90 | cc);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001436 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001437 uint8_t low_base = LowRegisterBits(raw_base);
1438 EmitModrmDisp(entry->skeleton.modrm_opcode, low_base, disp);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001439 DCHECK_EQ(entry->skeleton.immediate_bytes, 0);
1440}
1441
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001442void X86Mir2Lir::EmitRegRegCond(const X86EncodingMap* entry, int32_t raw_reg1, int32_t raw_reg2,
1443 int32_t cc) {
1444 // Generate prefix and opcode without the condition.
1445 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001446 EmitPrefixAndOpcode(entry, raw_reg1, NO_REG, raw_reg2);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001447
1448 // Now add the condition. The last byte of opcode is the one that receives it.
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001449 DCHECK_GE(cc, 0);
1450 DCHECK_LT(cc, 16);
1451 code_buffer_.back() += cc;
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001452
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001453 // Not expecting to have to encode immediate or do anything special for ModR/M since there are
1454 // two registers.
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001455 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1456 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1457
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001458 // For register to register encoding, the mod is 3.
1459 const uint8_t mod = (3 << 6);
1460
1461 // Encode the ModR/M byte now.
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001462 uint8_t low_reg1 = LowRegisterBits(raw_reg1);
1463 uint8_t low_reg2 = LowRegisterBits(raw_reg2);
1464 const uint8_t modrm = mod | (low_reg1 << 3) | low_reg2;
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001465 code_buffer_.push_back(modrm);
1466}
1467
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001468void X86Mir2Lir::EmitRegMemCond(const X86EncodingMap* entry, int32_t raw_reg1, int32_t raw_base,
1469 int32_t disp, int32_t cc) {
1470 // Generate prefix and opcode without the condition.
1471 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001472 EmitPrefixAndOpcode(entry, raw_reg1, NO_REG, raw_base);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001473
1474 // Now add the condition. The last byte of opcode is the one that receives it.
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001475 DCHECK_GE(cc, 0);
1476 DCHECK_LT(cc, 16);
1477 code_buffer_.back() += cc;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001478
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001479 // Not expecting to have to encode immediate or do anything special for ModR/M since there are
1480 // two registers.
Mark Mendell2637f2e2014-04-30 10:10:47 -04001481 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1482 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1483
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001484 uint8_t low_reg1 = LowRegisterBits(raw_reg1);
1485 uint8_t low_base = LowRegisterBits(raw_base);
1486 EmitModrmDisp(low_reg1, low_base, disp);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001487}
1488
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001489void X86Mir2Lir::EmitJmp(const X86EncodingMap* entry, int32_t rel) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001490 if (entry->opcode == kX86Jmp8) {
1491 DCHECK(IS_SIMM8(rel));
1492 code_buffer_.push_back(0xEB);
1493 code_buffer_.push_back(rel & 0xFF);
1494 } else if (entry->opcode == kX86Jmp32) {
1495 code_buffer_.push_back(0xE9);
1496 code_buffer_.push_back(rel & 0xFF);
1497 code_buffer_.push_back((rel >> 8) & 0xFF);
1498 code_buffer_.push_back((rel >> 16) & 0xFF);
1499 code_buffer_.push_back((rel >> 24) & 0xFF);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001500 } else if (entry->opcode == kX86Jecxz8) {
1501 DCHECK(IS_SIMM8(rel));
1502 code_buffer_.push_back(0xE3);
1503 code_buffer_.push_back(rel & 0xFF);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001504 } else {
1505 DCHECK(entry->opcode == kX86JmpR);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001506 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001507 EmitPrefix(entry, NO_REG, NO_REG, rel);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001508 code_buffer_.push_back(entry->skeleton.opcode);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001509 uint8_t low_reg = LowRegisterBits(rel);
1510 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | low_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001511 code_buffer_.push_back(modrm);
1512 }
1513}
1514
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001515void X86Mir2Lir::EmitJcc(const X86EncodingMap* entry, int32_t rel, int32_t cc) {
1516 DCHECK_GE(cc, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001517 DCHECK_LT(cc, 16);
1518 if (entry->opcode == kX86Jcc8) {
1519 DCHECK(IS_SIMM8(rel));
1520 code_buffer_.push_back(0x70 | cc);
1521 code_buffer_.push_back(rel & 0xFF);
1522 } else {
1523 DCHECK(entry->opcode == kX86Jcc32);
1524 code_buffer_.push_back(0x0F);
1525 code_buffer_.push_back(0x80 | cc);
1526 code_buffer_.push_back(rel & 0xFF);
1527 code_buffer_.push_back((rel >> 8) & 0xFF);
1528 code_buffer_.push_back((rel >> 16) & 0xFF);
1529 code_buffer_.push_back((rel >> 24) & 0xFF);
1530 }
1531}
1532
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001533void X86Mir2Lir::EmitCallMem(const X86EncodingMap* entry, int32_t raw_base, int32_t disp) {
1534 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001535 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, raw_base);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001536 uint8_t low_base = LowRegisterBits(raw_base);
1537 EmitModrmDisp(entry->skeleton.modrm_opcode, low_base, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001538 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1539 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1540}
1541
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001542void X86Mir2Lir::EmitCallImmediate(const X86EncodingMap* entry, int32_t disp) {
1543 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001544 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, NO_REG);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001545 DCHECK_EQ(4, entry->skeleton.immediate_bytes);
1546 code_buffer_.push_back(disp & 0xFF);
1547 code_buffer_.push_back((disp >> 8) & 0xFF);
1548 code_buffer_.push_back((disp >> 16) & 0xFF);
1549 code_buffer_.push_back((disp >> 24) & 0xFF);
1550 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1551}
1552
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001553void X86Mir2Lir::EmitCallThread(const X86EncodingMap* entry, int32_t disp) {
1554 DCHECK_EQ(false, entry->skeleton.r8_form);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001555 DCHECK_NE(entry->skeleton.prefix1, 0);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001556 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, NO_REG);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001557 EmitModrmThread(entry->skeleton.modrm_opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001558 code_buffer_.push_back(disp & 0xFF);
1559 code_buffer_.push_back((disp >> 8) & 0xFF);
1560 code_buffer_.push_back((disp >> 16) & 0xFF);
1561 code_buffer_.push_back((disp >> 24) & 0xFF);
1562 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1563 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1564}
1565
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001566void X86Mir2Lir::EmitPcRel(const X86EncodingMap* entry, int32_t raw_reg, int32_t raw_base_or_table,
1567 int32_t raw_index, int scale, int32_t table_or_disp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001568 int disp;
1569 if (entry->opcode == kX86PcRelLoadRA) {
buzbee0d829482013-10-11 15:24:55 -07001570 Mir2Lir::EmbeddedData *tab_rec =
1571 reinterpret_cast<Mir2Lir::EmbeddedData*>(UnwrapPointer(table_or_disp));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001572 disp = tab_rec->offset;
1573 } else {
1574 DCHECK(entry->opcode == kX86PcRelAdr);
buzbee0d829482013-10-11 15:24:55 -07001575 Mir2Lir::EmbeddedData *tab_rec =
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001576 reinterpret_cast<Mir2Lir::EmbeddedData*>(UnwrapPointer(raw_base_or_table));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001577 disp = tab_rec->offset;
1578 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001579 if (entry->opcode == kX86PcRelLoadRA) {
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001580 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001581 EmitPrefix(entry, raw_reg, raw_index, raw_base_or_table);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001582 code_buffer_.push_back(entry->skeleton.opcode);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001583 DCHECK_NE(0x0F, entry->skeleton.opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001584 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1585 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001586 uint8_t low_reg = LowRegisterBits(raw_reg);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001587 uint8_t modrm = (2 << 6) | (low_reg << 3) | rs_rX86_SP_32.GetRegNum();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001588 code_buffer_.push_back(modrm);
1589 DCHECK_LT(scale, 4);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001590 uint8_t low_base_or_table = LowRegisterBits(raw_base_or_table);
1591 uint8_t low_index = LowRegisterBits(raw_index);
1592 uint8_t sib = (scale << 6) | (low_index << 3) | low_base_or_table;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001593 code_buffer_.push_back(sib);
1594 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1595 } else {
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001596 uint8_t low_reg = LowRegisterBits(raw_reg);
1597 code_buffer_.push_back(entry->skeleton.opcode + low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001598 }
1599 code_buffer_.push_back(disp & 0xFF);
1600 code_buffer_.push_back((disp >> 8) & 0xFF);
1601 code_buffer_.push_back((disp >> 16) & 0xFF);
1602 code_buffer_.push_back((disp >> 24) & 0xFF);
1603 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1604 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1605}
1606
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001607void X86Mir2Lir::EmitMacro(const X86EncodingMap* entry, int32_t raw_reg, int32_t offset) {
1608 DCHECK_EQ(entry->opcode, kX86StartOfMethod) << entry->name;
1609 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001610 EmitPrefix(entry, raw_reg, NO_REG, NO_REG);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001611 code_buffer_.push_back(0xE8); // call +0
1612 code_buffer_.push_back(0);
1613 code_buffer_.push_back(0);
1614 code_buffer_.push_back(0);
1615 code_buffer_.push_back(0);
1616
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001617 uint8_t low_reg = LowRegisterBits(raw_reg);
1618 code_buffer_.push_back(0x58 + low_reg); // pop reg
Brian Carlstrom7940e442013-07-12 13:46:57 -07001619
Elena Sayapinadd644502014-07-01 18:39:52 +07001620 EmitRegImm(&X86Mir2Lir::EncodingMap[cu_->target64 ? kX86Sub64RI : kX86Sub32RI],
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001621 raw_reg, offset + 5 /* size of call +0 */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001622}
1623
1624void X86Mir2Lir::EmitUnimplemented(const X86EncodingMap* entry, LIR* lir) {
1625 UNIMPLEMENTED(WARNING) << "encoding kind for " << entry->name << " "
1626 << BuildInsnString(entry->fmt, lir, 0);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001627 for (size_t i = 0; i < GetInsnSize(lir); ++i) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001628 code_buffer_.push_back(0xCC); // push breakpoint instruction - int 3
1629 }
1630}
1631
1632/*
1633 * Assemble the LIR into binary instruction format. Note that we may
1634 * discover that pc-relative displacements may not fit the selected
1635 * instruction. In those cases we will try to substitute a new code
1636 * sequence or request that the trace be shortened and retried.
1637 */
buzbee0d829482013-10-11 15:24:55 -07001638AssemblerStatus X86Mir2Lir::AssembleInstructions(CodeOffset start_addr) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001639 UNUSED(start_addr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001640 LIR *lir;
1641 AssemblerStatus res = kSuccess; // Assume success
1642
1643 const bool kVerbosePcFixup = false;
1644 for (lir = first_lir_insn_; lir != NULL; lir = NEXT_LIR(lir)) {
buzbee409fe942013-10-11 10:49:56 -07001645 if (IsPseudoLirOp(lir->opcode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001646 continue;
1647 }
1648
1649 if (lir->flags.is_nop) {
1650 continue;
1651 }
1652
buzbeeb48819d2013-09-14 16:15:25 -07001653 if (lir->flags.fixup != kFixupNone) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001654 switch (lir->opcode) {
1655 case kX86Jcc8: {
1656 LIR *target_lir = lir->target;
1657 DCHECK(target_lir != NULL);
1658 int delta = 0;
buzbee0d829482013-10-11 15:24:55 -07001659 CodeOffset pc;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001660 if (IS_SIMM8(lir->operands[0])) {
1661 pc = lir->offset + 2 /* opcode + rel8 */;
1662 } else {
1663 pc = lir->offset + 6 /* 2 byte opcode + rel32 */;
1664 }
buzbee0d829482013-10-11 15:24:55 -07001665 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001666 delta = target - pc;
1667 if (IS_SIMM8(delta) != IS_SIMM8(lir->operands[0])) {
1668 if (kVerbosePcFixup) {
1669 LOG(INFO) << "Retry for JCC growth at " << lir->offset
1670 << " delta: " << delta << " old delta: " << lir->operands[0];
1671 }
1672 lir->opcode = kX86Jcc32;
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001673 lir->flags.size = GetInsnSize(lir);
1674 DCHECK(lir->u.m.def_mask->Equals(kEncodeAll));
1675 DCHECK(lir->u.m.use_mask->Equals(kEncodeAll));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001676 res = kRetryAll;
1677 }
1678 if (kVerbosePcFixup) {
1679 LOG(INFO) << "Source:";
1680 DumpLIRInsn(lir, 0);
1681 LOG(INFO) << "Target:";
1682 DumpLIRInsn(target_lir, 0);
1683 LOG(INFO) << "Delta " << delta;
1684 }
1685 lir->operands[0] = delta;
1686 break;
1687 }
1688 case kX86Jcc32: {
1689 LIR *target_lir = lir->target;
1690 DCHECK(target_lir != NULL);
buzbee0d829482013-10-11 15:24:55 -07001691 CodeOffset pc = lir->offset + 6 /* 2 byte opcode + rel32 */;
1692 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001693 int delta = target - pc;
1694 if (kVerbosePcFixup) {
1695 LOG(INFO) << "Source:";
1696 DumpLIRInsn(lir, 0);
1697 LOG(INFO) << "Target:";
1698 DumpLIRInsn(target_lir, 0);
1699 LOG(INFO) << "Delta " << delta;
1700 }
1701 lir->operands[0] = delta;
1702 break;
1703 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001704 case kX86Jecxz8: {
1705 LIR *target_lir = lir->target;
1706 DCHECK(target_lir != NULL);
1707 CodeOffset pc;
1708 pc = lir->offset + 2; // opcode + rel8
1709 CodeOffset target = target_lir->offset;
1710 int delta = target - pc;
1711 lir->operands[0] = delta;
1712 DCHECK(IS_SIMM8(delta));
1713 break;
1714 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001715 case kX86Jmp8: {
1716 LIR *target_lir = lir->target;
1717 DCHECK(target_lir != NULL);
1718 int delta = 0;
buzbee0d829482013-10-11 15:24:55 -07001719 CodeOffset pc;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001720 if (IS_SIMM8(lir->operands[0])) {
1721 pc = lir->offset + 2 /* opcode + rel8 */;
1722 } else {
1723 pc = lir->offset + 5 /* opcode + rel32 */;
1724 }
buzbee0d829482013-10-11 15:24:55 -07001725 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001726 delta = target - pc;
1727 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && delta == 0) {
1728 // Useless branch
buzbee252254b2013-09-08 16:20:53 -07001729 NopLIR(lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001730 if (kVerbosePcFixup) {
1731 LOG(INFO) << "Retry for useless branch at " << lir->offset;
1732 }
1733 res = kRetryAll;
1734 } else if (IS_SIMM8(delta) != IS_SIMM8(lir->operands[0])) {
1735 if (kVerbosePcFixup) {
1736 LOG(INFO) << "Retry for JMP growth at " << lir->offset;
1737 }
1738 lir->opcode = kX86Jmp32;
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001739 lir->flags.size = GetInsnSize(lir);
1740 DCHECK(lir->u.m.def_mask->Equals(kEncodeAll));
1741 DCHECK(lir->u.m.use_mask->Equals(kEncodeAll));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001742 res = kRetryAll;
1743 }
1744 lir->operands[0] = delta;
1745 break;
1746 }
1747 case kX86Jmp32: {
1748 LIR *target_lir = lir->target;
1749 DCHECK(target_lir != NULL);
buzbee0d829482013-10-11 15:24:55 -07001750 CodeOffset pc = lir->offset + 5 /* opcode + rel32 */;
1751 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001752 int delta = target - pc;
1753 lir->operands[0] = delta;
1754 break;
1755 }
1756 default:
Mark Mendell67c39c42014-01-31 17:28:00 -08001757 if (lir->flags.fixup == kFixupLoad) {
1758 LIR *target_lir = lir->target;
1759 DCHECK(target_lir != NULL);
1760 CodeOffset target = target_lir->offset;
1761 lir->operands[2] = target;
1762 int newSize = GetInsnSize(lir);
1763 if (newSize != lir->flags.size) {
1764 lir->flags.size = newSize;
1765 res = kRetryAll;
1766 }
1767 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001768 break;
1769 }
1770 }
1771
1772 /*
1773 * If one of the pc-relative instructions expanded we'll have
1774 * to make another pass. Don't bother to fully assemble the
1775 * instruction.
1776 */
1777 if (res != kSuccess) {
1778 continue;
1779 }
1780 CHECK_EQ(static_cast<size_t>(lir->offset), code_buffer_.size());
1781 const X86EncodingMap *entry = &X86Mir2Lir::EncodingMap[lir->opcode];
1782 size_t starting_cbuf_size = code_buffer_.size();
1783 switch (entry->kind) {
1784 case kData: // 4 bytes of data
1785 code_buffer_.push_back(lir->operands[0]);
1786 break;
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001787 case kNullary: // 1 byte of opcode and possible prefixes.
1788 EmitNullary(entry);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001789 break;
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001790 case kRegOpcode: // lir operands - 0: reg
1791 EmitOpRegOpcode(entry, lir->operands[0]);
1792 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001793 case kReg: // lir operands - 0: reg
1794 EmitOpReg(entry, lir->operands[0]);
1795 break;
1796 case kMem: // lir operands - 0: base, 1: disp
1797 EmitOpMem(entry, lir->operands[0], lir->operands[1]);
1798 break;
Vladimir Marko057c74a2013-12-03 15:20:45 +00001799 case kArray: // lir operands - 0: base, 1: index, 2: scale, 3: disp
1800 EmitOpArray(entry, lir->operands[0], lir->operands[1], lir->operands[2], lir->operands[3]);
1801 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001802 case kMemReg: // lir operands - 0: base, 1: disp, 2: reg
1803 EmitMemReg(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1804 break;
Mark Mendell343adb52013-12-18 06:02:17 -08001805 case kMemImm: // lir operands - 0: base, 1: disp, 2: immediate
1806 EmitMemImm(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1807 break;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001808 case kArrayImm: // lir operands - 0: base, 1: index, 2: disp, 3:scale, 4:immediate
1809 EmitArrayImm(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1810 lir->operands[3], lir->operands[4]);
1811 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001812 case kArrayReg: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
1813 EmitArrayReg(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1814 lir->operands[3], lir->operands[4]);
1815 break;
1816 case kRegMem: // lir operands - 0: reg, 1: base, 2: disp
1817 EmitRegMem(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1818 break;
1819 case kRegArray: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
1820 EmitRegArray(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1821 lir->operands[3], lir->operands[4]);
1822 break;
1823 case kRegThread: // lir operands - 0: reg, 1: disp
1824 EmitRegThread(entry, lir->operands[0], lir->operands[1]);
1825 break;
1826 case kRegReg: // lir operands - 0: reg1, 1: reg2
1827 EmitRegReg(entry, lir->operands[0], lir->operands[1]);
1828 break;
1829 case kRegRegStore: // lir operands - 0: reg2, 1: reg1
1830 EmitRegReg(entry, lir->operands[1], lir->operands[0]);
1831 break;
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001832 case kMemRegImm: // lir operands - 0: base, 1: disp, 2: reg 3: immediate
Mark Mendell2637f2e2014-04-30 10:10:47 -04001833 EmitMemRegImm(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1834 lir->operands[3]);
1835 break;
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001836 case kRegRegImm: // lir operands - 0: reg1, 1: reg2, 2: imm
Brian Carlstrom7940e442013-07-12 13:46:57 -07001837 EmitRegRegImm(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1838 break;
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001839 case kRegRegImmStore: // lir operands - 0: reg2, 1: reg1, 2: imm
1840 EmitRegRegImm(entry, lir->operands[1], lir->operands[0], lir->operands[2]);
1841 break;
1842 case kRegMemImm: // lir operands - 0: reg, 1: base, 2: disp, 3: imm
Mark Mendell4708dcd2014-01-22 09:05:18 -08001843 EmitRegMemImm(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1844 lir->operands[3]);
1845 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001846 case kRegImm: // lir operands - 0: reg, 1: immediate
1847 EmitRegImm(entry, lir->operands[0], lir->operands[1]);
1848 break;
1849 case kThreadImm: // lir operands - 0: disp, 1: immediate
1850 EmitThreadImm(entry, lir->operands[0], lir->operands[1]);
1851 break;
1852 case kMovRegImm: // lir operands - 0: reg, 1: immediate
1853 EmitMovRegImm(entry, lir->operands[0], lir->operands[1]);
1854 break;
Yixin Shou5192cbb2014-07-01 13:48:17 -04001855 case kMovRegQuadImm: {
1856 int64_t value = static_cast<int64_t>(static_cast<int64_t>(lir->operands[1]) << 32 |
1857 static_cast<uint32_t>(lir->operands[2]));
1858 EmitMovRegImm(entry, lir->operands[0], value);
1859 }
1860 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001861 case kShiftRegImm: // lir operands - 0: reg, 1: immediate
1862 EmitShiftRegImm(entry, lir->operands[0], lir->operands[1]);
1863 break;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001864 case kShiftMemImm: // lir operands - 0: base, 1: disp, 2:immediate
1865 EmitShiftMemImm(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1866 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001867 case kShiftRegCl: // lir operands - 0: reg, 1: cl
Brian Carlstrom7940e442013-07-12 13:46:57 -07001868 EmitShiftRegCl(entry, lir->operands[0], lir->operands[1]);
1869 break;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001870 case kShiftMemCl: // lir operands - 0: base, 1:displacement, 2: cl
1871 EmitShiftMemCl(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1872 break;
Yixin Shouf40f8902014-08-14 14:10:32 -04001873 case kShiftRegRegCl: // lir operands - 0: reg1, 1: reg2, 2: cl
1874 EmitShiftRegRegCl(entry, lir->operands[1], lir->operands[0], lir->operands[2]);
1875 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001876 case kRegCond: // lir operands - 0: reg, 1: condition
1877 EmitRegCond(entry, lir->operands[0], lir->operands[1]);
1878 break;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001879 case kMemCond: // lir operands - 0: base, 1: displacement, 2: condition
1880 EmitMemCond(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1881 break;
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001882 case kRegRegCond: // lir operands - 0: reg, 1: reg, 2: condition
1883 EmitRegRegCond(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1884 break;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001885 case kRegMemCond: // lir operands - 0: reg, 1: reg, displacement, 3: condition
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001886 EmitRegMemCond(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1887 lir->operands[3]);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001888 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001889 case kJmp: // lir operands - 0: rel
Brian Carlstrom60d7a652014-03-13 18:10:08 -07001890 if (entry->opcode == kX86JmpT) {
1891 // This works since the instruction format for jmp and call is basically the same and
1892 // EmitCallThread loads opcode info.
1893 EmitCallThread(entry, lir->operands[0]);
1894 } else {
1895 EmitJmp(entry, lir->operands[0]);
1896 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001897 break;
1898 case kJcc: // lir operands - 0: rel, 1: CC, target assigned
1899 EmitJcc(entry, lir->operands[0], lir->operands[1]);
1900 break;
1901 case kCall:
1902 switch (entry->opcode) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001903 case kX86CallI: // lir operands - 0: disp
1904 EmitCallImmediate(entry, lir->operands[0]);
1905 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001906 case kX86CallM: // lir operands - 0: base, 1: disp
1907 EmitCallMem(entry, lir->operands[0], lir->operands[1]);
1908 break;
1909 case kX86CallT: // lir operands - 0: disp
1910 EmitCallThread(entry, lir->operands[0]);
1911 break;
1912 default:
1913 EmitUnimplemented(entry, lir);
1914 break;
1915 }
1916 break;
1917 case kPcRel: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
1918 EmitPcRel(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1919 lir->operands[3], lir->operands[4]);
1920 break;
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001921 case kMacro: // lir operands - 0: reg
Brian Carlstrom7940e442013-07-12 13:46:57 -07001922 EmitMacro(entry, lir->operands[0], lir->offset);
1923 break;
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001924 case kNop: // TODO: these instruction kinds are missing implementations.
1925 case kThreadReg:
1926 case kRegArrayImm:
1927 case kShiftArrayImm:
1928 case kShiftArrayCl:
1929 case kArrayCond:
1930 case kUnimplemented:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001931 EmitUnimplemented(entry, lir);
1932 break;
1933 }
Ian Rogers5aa6e042014-06-13 16:38:24 -07001934 DCHECK_EQ(lir->flags.size, GetInsnSize(lir));
1935 CHECK_EQ(lir->flags.size, code_buffer_.size() - starting_cbuf_size)
Brian Carlstrom7940e442013-07-12 13:46:57 -07001936 << "Instruction size mismatch for entry: " << X86Mir2Lir::EncodingMap[lir->opcode].name;
1937 }
1938 return res;
1939}
1940
buzbeeb48819d2013-09-14 16:15:25 -07001941// LIR offset assignment.
1942// TODO: consolidate w/ Arm assembly mechanism.
1943int X86Mir2Lir::AssignInsnOffsets() {
1944 LIR* lir;
1945 int offset = 0;
1946
1947 for (lir = first_lir_insn_; lir != NULL; lir = NEXT_LIR(lir)) {
1948 lir->offset = offset;
buzbee409fe942013-10-11 10:49:56 -07001949 if (LIKELY(!IsPseudoLirOp(lir->opcode))) {
buzbeeb48819d2013-09-14 16:15:25 -07001950 if (!lir->flags.is_nop) {
1951 offset += lir->flags.size;
1952 }
1953 } else if (UNLIKELY(lir->opcode == kPseudoPseudoAlign4)) {
1954 if (offset & 0x2) {
1955 offset += 2;
1956 lir->operands[0] = 1;
1957 } else {
1958 lir->operands[0] = 0;
1959 }
1960 }
1961 /* Pseudo opcodes don't consume space */
1962 }
1963 return offset;
1964}
1965
1966/*
1967 * Walk the compilation unit and assign offsets to instructions
1968 * and literals and compute the total size of the compiled unit.
1969 * TODO: consolidate w/ Arm assembly mechanism.
1970 */
1971void X86Mir2Lir::AssignOffsets() {
1972 int offset = AssignInsnOffsets();
1973
Mark Mendelld65c51a2014-04-29 16:55:20 -04001974 if (const_vectors_ != nullptr) {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001975 // Vector literals must be 16-byte aligned. The header that is placed
1976 // in the code section causes misalignment so we take it into account.
1977 // Otherwise, we are sure that for x86 method is aligned to 16.
1978 DCHECK_EQ(GetInstructionSetAlignment(cu_->instruction_set), 16u);
1979 uint32_t bytes_to_fill = (0x10 - ((offset + sizeof(OatQuickMethodHeader)) & 0xF)) & 0xF;
1980 offset += bytes_to_fill;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001981
1982 // Now assign each literal the right offset.
1983 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
1984 p->offset = offset;
1985 offset += 16;
1986 }
1987 }
1988
buzbeeb48819d2013-09-14 16:15:25 -07001989 /* Const values have to be word aligned */
Andreas Gampe66018822014-05-05 20:47:19 -07001990 offset = RoundUp(offset, 4);
buzbeeb48819d2013-09-14 16:15:25 -07001991
1992 /* Set up offsets for literals */
1993 data_offset_ = offset;
1994
1995 offset = AssignLiteralOffset(offset);
1996
1997 offset = AssignSwitchTablesOffset(offset);
1998
1999 offset = AssignFillArrayDataOffset(offset);
2000
2001 total_size_ = offset;
2002}
2003
2004/*
2005 * Go over each instruction in the list and calculate the offset from the top
2006 * before sending them off to the assembler. If out-of-range branch distance is
2007 * seen rearrange the instructions a bit to correct it.
2008 * TODO: consolidate w/ Arm assembly mechanism.
2009 */
2010void X86Mir2Lir::AssembleLIR() {
buzbeea61f4952013-08-23 14:27:06 -07002011 cu_->NewTimingSplit("Assemble");
Mark Mendell55d0eac2014-02-06 11:02:52 -08002012
2013 // We will remove the method address if we never ended up using it
2014 if (store_method_addr_ && !store_method_addr_used_) {
2015 setup_method_address_[0]->flags.is_nop = true;
2016 setup_method_address_[1]->flags.is_nop = true;
2017 }
2018
buzbeeb48819d2013-09-14 16:15:25 -07002019 AssignOffsets();
2020 int assembler_retries = 0;
2021 /*
2022 * Assemble here. Note that we generate code with optimistic assumptions
2023 * and if found now to work, we'll have to redo the sequence and retry.
2024 */
2025
2026 while (true) {
2027 AssemblerStatus res = AssembleInstructions(0);
2028 if (res == kSuccess) {
2029 break;
2030 } else {
2031 assembler_retries++;
2032 if (assembler_retries > MAX_ASSEMBLER_RETRIES) {
2033 CodegenDump();
2034 LOG(FATAL) << "Assembler error - too many retries";
2035 }
2036 // Redo offsets and try again
2037 AssignOffsets();
2038 code_buffer_.clear();
2039 }
2040 }
2041
2042 // Install literals
2043 InstallLiteralPools();
2044
2045 // Install switch tables
2046 InstallSwitchTables();
2047
2048 // Install fill array data
2049 InstallFillArrayData();
2050
2051 // Create the mapping table and native offset to reference map.
buzbeea61f4952013-08-23 14:27:06 -07002052 cu_->NewTimingSplit("PcMappingTable");
buzbeeb48819d2013-09-14 16:15:25 -07002053 CreateMappingTables();
2054
buzbeea61f4952013-08-23 14:27:06 -07002055 cu_->NewTimingSplit("GcMap");
buzbeeb48819d2013-09-14 16:15:25 -07002056 CreateNativeGcMap();
2057}
2058
Brian Carlstrom7940e442013-07-12 13:46:57 -07002059} // namespace art