blob: 12523aca76c1f913842637040b6fcbc9a2c170d1 [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"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080018
Vladimir Marko80afd022015-05-19 18:08:00 +010019#include "base/bit_utils.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080020#include "base/logging.h"
21#include "dex/compiler_ir.h"
22#include "dex/quick/mir_to_lir.h"
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -070023#include "oat.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010024#include "utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include "x86_lir.h"
26
27namespace art {
28
29#define MAX_ASSEMBLER_RETRIES 50
30
31const X86EncodingMap X86Mir2Lir::EncodingMap[kX86Last] = {
Ian Rogers0f9b9c52014-06-09 01:32:12 -070032 { kX8632BitData, kData, IS_UNARY_OP, { 0, 0, 0x00, 0, 0, 0, 0, 4, false }, "data", "0x!0d" },
33 { kX86Bkpt, kNullary, NO_OPERAND | IS_BRANCH, { 0, 0, 0xCC, 0, 0, 0, 0, 0, false }, "int 3", "" },
34 { kX86Nop, kNop, NO_OPERAND, { 0, 0, 0x90, 0, 0, 0, 0, 0, false }, "nop", "" },
Brian Carlstrom7940e442013-07-12 13:46:57 -070035
36#define ENCODING_MAP(opname, mem_use, reg_def, uses_ccodes, \
37 rm8_r8, rm32_r32, \
38 r8_rm8, r32_rm32, \
39 ax8_i8, ax32_i32, \
40 rm8_i8, rm8_i8_modrm, \
41 rm32_i32, rm32_i32_modrm, \
42 rm32_i8, rm32_i8_modrm) \
Ian Rogers0f9b9c52014-06-09 01:32:12 -070043{ 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 -040044{ 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 -070045{ 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" }, \
46{ 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" }, \
47{ 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]" }, \
48{ 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]" }, \
49{ 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]" }, \
50{ 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 -040051{ 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" }, \
52{ 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" }, \
53{ 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 -070054 \
Ian Rogers0f9b9c52014-06-09 01:32:12 -070055{ 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" }, \
56{ 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" }, \
57{ 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" }, \
58{ 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" }, \
59{ 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]" }, \
60{ 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]" }, \
61{ 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]" }, \
62{ 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" }, \
63{ 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" }, \
64{ 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" }, \
65{ 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" }, \
66{ 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" }, \
67{ 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" }, \
68{ 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" }, \
69{ 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 -070070 \
Ian Rogers0f9b9c52014-06-09 01:32:12 -070071{ 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" }, \
72{ 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" }, \
73{ 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" }, \
74{ 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" }, \
75{ 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]" }, \
76{ 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]" }, \
77{ 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]" }, \
78{ 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" }, \
79{ 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" }, \
80{ kX86 ## opname ## 32AI, kArrayImm, mem_use | IS_QUIN_OP | REG_USE01 | SETS_CCODES | uses_ccodes, { 0, 0, rm32_i32, 0, 0, rm32_i32_modrm, 0, 4, false }, #opname "32AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
81{ kX86 ## opname ## 32TI, kThreadImm, mem_use | IS_BINARY_OP | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0, rm32_i32, 0, 0, rm32_i32_modrm, 0, 4, false }, #opname "32TI", "fs:[!0d],!1d" }, \
82{ kX86 ## opname ## 32RI8, kRegImm, IS_BINARY_OP | reg_def | REG_USE0 | SETS_CCODES | uses_ccodes, { 0, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1, false }, #opname "32RI8", "!0r,!1d" }, \
83{ 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" }, \
84{ 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" }, \
85{ 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 +070086 \
Ian Rogers0f9b9c52014-06-09 01:32:12 -070087{ 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" }, \
88{ 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" }, \
89{ 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" }, \
90{ 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" }, \
91{ 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]" }, \
92{ 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]" }, \
93{ 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]" }, \
94{ 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" }, \
95{ 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" }, \
96{ 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" }, \
97{ 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" }, \
98{ 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" }, \
99{ 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" }, \
100{ 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" }, \
101{ 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 -0700102
103ENCODING_MAP(Add, IS_LOAD | IS_STORE, REG_DEF0, 0,
104 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */,
105 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */,
106 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */,
107 0x80, 0x0 /* RegMem8/imm8 */,
108 0x81, 0x0 /* RegMem32/imm32 */, 0x83, 0x0 /* RegMem32/imm8 */),
109ENCODING_MAP(Or, IS_LOAD | IS_STORE, REG_DEF0, 0,
110 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */,
111 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */,
112 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */,
113 0x80, 0x1 /* RegMem8/imm8 */,
114 0x81, 0x1 /* RegMem32/imm32 */, 0x83, 0x1 /* RegMem32/imm8 */),
115ENCODING_MAP(Adc, IS_LOAD | IS_STORE, REG_DEF0, USES_CCODES,
116 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */,
117 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */,
118 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */,
119 0x80, 0x2 /* RegMem8/imm8 */,
120 0x81, 0x2 /* RegMem32/imm32 */, 0x83, 0x2 /* RegMem32/imm8 */),
121ENCODING_MAP(Sbb, IS_LOAD | IS_STORE, REG_DEF0, USES_CCODES,
122 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */,
123 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */,
124 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */,
125 0x80, 0x3 /* RegMem8/imm8 */,
126 0x81, 0x3 /* RegMem32/imm32 */, 0x83, 0x3 /* RegMem32/imm8 */),
127ENCODING_MAP(And, IS_LOAD | IS_STORE, REG_DEF0, 0,
128 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */,
129 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */,
130 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */,
131 0x80, 0x4 /* RegMem8/imm8 */,
132 0x81, 0x4 /* RegMem32/imm32 */, 0x83, 0x4 /* RegMem32/imm8 */),
133ENCODING_MAP(Sub, IS_LOAD | IS_STORE, REG_DEF0, 0,
134 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */,
135 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */,
136 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */,
137 0x80, 0x5 /* RegMem8/imm8 */,
138 0x81, 0x5 /* RegMem32/imm32 */, 0x83, 0x5 /* RegMem32/imm8 */),
139ENCODING_MAP(Xor, IS_LOAD | IS_STORE, REG_DEF0, 0,
140 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */,
141 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */,
142 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */,
143 0x80, 0x6 /* RegMem8/imm8 */,
144 0x81, 0x6 /* RegMem32/imm32 */, 0x83, 0x6 /* RegMem32/imm8 */),
145ENCODING_MAP(Cmp, IS_LOAD, 0, 0,
146 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */,
147 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */,
148 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */,
149 0x80, 0x7 /* RegMem8/imm8 */,
150 0x81, 0x7 /* RegMem32/imm32 */, 0x83, 0x7 /* RegMem32/imm8 */),
151#undef ENCODING_MAP
152
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700153 { kX86Imul16RRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { 0x66, 0, 0x69, 0, 0, 0, 0, 2, false }, "Imul16RRI", "!0r,!1r,!2d" },
154 { 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" },
155 { 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 -0700156
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700157 { kX86Imul32RRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { 0, 0, 0x69, 0, 0, 0, 0, 4, false }, "Imul32RRI", "!0r,!1r,!2d" },
158 { 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" },
159 { 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" },
160 { kX86Imul32RRI8, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { 0, 0, 0x6B, 0, 0, 0, 0, 1, false }, "Imul32RRI8", "!0r,!1r,!2d" },
161 { 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" },
162 { 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 -0700163
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700164 { kX86Imul64RRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { REX_W, 0, 0x69, 0, 0, 0, 0, 4, false }, "Imul64RRI", "!0r,!1r,!2d" },
165 { 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" },
166 { 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" },
167 { kX86Imul64RRI8, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { REX_W, 0, 0x6B, 0, 0, 0, 0, 1, false }, "Imul64RRI8", "!0r,!1r,!2d" },
168 { 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" },
169 { 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 +0700170
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700171 { kX86Mov8MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0, 0, 0x88, 0, 0, 0, 0, 0, true }, "Mov8MR", "[!0r+!1d],!2r" },
172 { kX86Mov8AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0, 0, 0x88, 0, 0, 0, 0, 0, true }, "Mov8AR", "[!0r+!1r<<!2d+!3d],!4r" },
173 { kX86Mov8TR, kThreadReg, IS_STORE | IS_BINARY_OP | REG_USE1, { THREAD_PREFIX, 0, 0x88, 0, 0, 0, 0, 0, true }, "Mov8TR", "fs:[!0d],!1r" },
174 { kX86Mov8RR, kRegReg, IS_BINARY_OP | REG_DEF0_USE1, { 0, 0, 0x8A, 0, 0, 0, 0, 0, true }, "Mov8RR", "!0r,!1r" },
175 { kX86Mov8RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { 0, 0, 0x8A, 0, 0, 0, 0, 0, true }, "Mov8RM", "!0r,[!1r+!2d]" },
176 { 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]" },
177 { kX86Mov8RT, kRegThread, IS_LOAD | IS_BINARY_OP | REG_DEF0, { THREAD_PREFIX, 0, 0x8A, 0, 0, 0, 0, 0, true }, "Mov8RT", "!0r,fs:[!1d]" },
178 { 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 -0400179 { kX86Mov8MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { 0, 0, 0xC6, 0, 0, 0, 0, 1, false}, "Mov8MI", "[!0r+!1d],!2d" },
180 { kX86Mov8AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { 0, 0, 0xC6, 0, 0, 0, 0, 1, false}, "Mov8AI", "[!0r+!1r<<!2d+!3d],!4d" },
181 { 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 -0700182
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700183 { kX86Mov16MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, 0, 0x89, 0, 0, 0, 0, 0, false }, "Mov16MR", "[!0r+!1d],!2r" },
184 { kX86Mov16AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x66, 0, 0x89, 0, 0, 0, 0, 0, false }, "Mov16AR", "[!0r+!1r<<!2d+!3d],!4r" },
185 { kX86Mov16TR, kThreadReg, IS_STORE | IS_BINARY_OP | REG_USE1, { THREAD_PREFIX, 0x66, 0x89, 0, 0, 0, 0, 0, false }, "Mov16TR", "fs:[!0d],!1r" },
186 { kX86Mov16RR, kRegReg, IS_BINARY_OP | REG_DEF0_USE1, { 0x66, 0, 0x8B, 0, 0, 0, 0, 0, false }, "Mov16RR", "!0r,!1r" },
187 { kX86Mov16RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { 0x66, 0, 0x8B, 0, 0, 0, 0, 0, false }, "Mov16RM", "!0r,[!1r+!2d]" },
188 { 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]" },
189 { kX86Mov16RT, kRegThread, IS_LOAD | IS_BINARY_OP | REG_DEF0, { THREAD_PREFIX, 0x66, 0x8B, 0, 0, 0, 0, 0, false }, "Mov16RT", "!0r,fs:[!1d]" },
190 { kX86Mov16RI, kMovRegImm, IS_BINARY_OP | REG_DEF0, { 0x66, 0, 0xB8, 0, 0, 0, 0, 2, false }, "Mov16RI", "!0r,!1d" },
191 { kX86Mov16MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { 0x66, 0, 0xC7, 0, 0, 0, 0, 2, false }, "Mov16MI", "[!0r+!1d],!2d" },
192 { kX86Mov16AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { 0x66, 0, 0xC7, 0, 0, 0, 0, 2, false }, "Mov16AI", "[!0r+!1r<<!2d+!3d],!4d" },
193 { 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 -0700194
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700195 { kX86Mov32MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0, 0, 0x89, 0, 0, 0, 0, 0, false }, "Mov32MR", "[!0r+!1d],!2r" },
196 { 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 -0700197 { kX86Movnti32MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0, 0, 0x0F, 0xC3, 0, 0, 0, 0, false }, "Movnti32MR", "[!0r+!1d],!2r" },
198 { 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 -0700199 { 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 +0800200 { 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 -0700201 { kX86Mov32RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { 0, 0, 0x8B, 0, 0, 0, 0, 0, false }, "Mov32RM", "!0r,[!1r+!2d]" },
202 { 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]" },
203 { kX86Mov32RT, kRegThread, IS_LOAD | IS_BINARY_OP | REG_DEF0, { THREAD_PREFIX, 0, 0x8B, 0, 0, 0, 0, 0, false }, "Mov32RT", "!0r,fs:[!1d]" },
204 { kX86Mov32RI, kMovRegImm, IS_BINARY_OP | REG_DEF0, { 0, 0, 0xB8, 0, 0, 0, 0, 4, false }, "Mov32RI", "!0r,!1d" },
205 { kX86Mov32MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { 0, 0, 0xC7, 0, 0, 0, 0, 4, false }, "Mov32MI", "[!0r+!1d],!2d" },
206 { kX86Mov32AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { 0, 0, 0xC7, 0, 0, 0, 0, 4, false }, "Mov32AI", "[!0r+!1r<<!2d+!3d],!4d" },
207 { 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 -0700208
Haitao Fenga870bc52014-09-09 15:52:34 +0800209 { kX86Lea32RM, kRegMem, IS_TERTIARY_OP | REG_DEF0_USE1, { 0, 0, 0x8D, 0, 0, 0, 0, 0, false }, "Lea32RM", "!0r,[!1r+!2d]" },
210 { 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 -0800211
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700212 { kX86Mov64MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { REX_W, 0, 0x89, 0, 0, 0, 0, 0, false }, "Mov64MR", "[!0r+!1d],!2r" },
213 { 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 -0700214 { kX86Movnti64MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { REX_W, 0, 0x0F, 0xC3, 0, 0, 0, 0, false }, "Movnti64MR", "[!0r+!1d],!2r" },
215 { 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 -0700216 { 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 +0800217 { 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 -0700218 { kX86Mov64RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { REX_W, 0, 0x8B, 0, 0, 0, 0, 0, false }, "Mov64RM", "!0r,[!1r+!2d]" },
219 { 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]" },
220 { 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 -0400221 { kX86Mov64RI32, kRegImm, IS_BINARY_OP | REG_DEF0, { REX_W, 0, 0xC7, 0, 0, 0, 0, 4, false }, "Mov64RI32", "!0r,!1d" },
222 { 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 -0700223 { kX86Mov64MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { REX_W, 0, 0xC7, 0, 0, 0, 0, 4, false }, "Mov64MI", "[!0r+!1d],!2d" },
224 { 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" },
225 { 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 -0700226
Haitao Fenga870bc52014-09-09 15:52:34 +0800227 { kX86Lea64RM, kRegMem, IS_TERTIARY_OP | REG_DEF0_USE1, { REX_W, 0, 0x8D, 0, 0, 0, 0, 0, false }, "Lea64RM", "!0r,[!1r+!2d]" },
228 { 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 -0800229
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700230 { kX86Cmov32RRC, kRegRegCond, IS_TERTIARY_OP | REG_DEF0_USE01 | USES_CCODES, { 0, 0, 0x0F, 0x40, 0, 0, 0, 0, false }, "Cmovcc32RR", "!2c !0r,!1r" },
231 { 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 +0700232
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700233 { 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]" },
234 { 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 -0400235
Brian Carlstrom7940e442013-07-12 13:46:57 -0700236#define SHIFT_ENCODING_MAP(opname, modrm_opcode) \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700237{ 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" }, \
238{ 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" }, \
239{ 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" }, \
240{ 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" }, \
241{ 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" }, \
242{ 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 -0700243 \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700244{ 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" }, \
245{ 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" }, \
246{ 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" }, \
247{ 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" }, \
248{ 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" }, \
249{ 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 -0700250 \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700251{ 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" }, \
252{ 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" }, \
253{ 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" }, \
254{ 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" }, \
255{ 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" }, \
256{ 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 +0700257 \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700258{ 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" }, \
259{ 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" }, \
260{ 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" }, \
261{ 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" }, \
262{ 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" }, \
263{ 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 -0700264
265 SHIFT_ENCODING_MAP(Rol, 0x0),
266 SHIFT_ENCODING_MAP(Ror, 0x1),
267 SHIFT_ENCODING_MAP(Rcl, 0x2),
268 SHIFT_ENCODING_MAP(Rcr, 0x3),
269 SHIFT_ENCODING_MAP(Sal, 0x4),
270 SHIFT_ENCODING_MAP(Shr, 0x5),
271 SHIFT_ENCODING_MAP(Sar, 0x7),
272#undef SHIFT_ENCODING_MAP
273
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700274 { kX86Cmc, kNullary, NO_OPERAND, { 0, 0, 0xF5, 0, 0, 0, 0, 0, false }, "Cmc", "" },
275 { 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 -0400276 { 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 -0700277 { 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" },
278 { 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 -0400279 { 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 -0700280 { 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" },
281 { kX86Shld64RRI, kRegRegImmStore, IS_TERTIARY_OP | REG_DEF0_USE01 | SETS_CCODES, { REX_W, 0, 0x0F, 0xA4, 0, 0, 0, 1, false }, "Shld64RRI", "!0r,!1r,!2d" },
282 { 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" },
283 { kX86Shrd64RRI, kRegRegImmStore, IS_TERTIARY_OP | REG_DEF0_USE01 | SETS_CCODES, { REX_W, 0, 0x0F, 0xAC, 0, 0, 0, 1, false }, "Shrd64RRI", "!0r,!1r,!2d" },
284 { 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 -0700285
Dave Allison69dfe512014-07-11 17:11:58 +0000286 { kX86Test8RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF6, 0, 0, 0, 0, 1, true }, "Test8RI", "!0r,!1d" },
287 { kX86Test8MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF6, 0, 0, 0, 0, 1, true }, "Test8MI", "[!0r+!1d],!2d" },
288 { 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" },
289 { kX86Test16RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0x66, 0, 0xF7, 0, 0, 0, 0, 2, false }, "Test16RI", "!0r,!1d" },
290 { kX86Test16MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0x66, 0, 0xF7, 0, 0, 0, 0, 2, false }, "Test16MI", "[!0r+!1d],!2d" },
291 { 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" },
292 { kX86Test32RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF7, 0, 0, 0, 0, 4, false }, "Test32RI", "!0r,!1d" },
293 { kX86Test32MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF7, 0, 0, 0, 0, 4, false }, "Test32MI", "[!0r+!1d],!2d" },
294 { 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 -0700295 { kX86Test64RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { REX_W, 0, 0xF7, 0, 0, 0, 0, 4, false }, "Test64RI", "!0r,!1d" },
296 { 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" },
297 { 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 +0700298
Dave Allison69dfe512014-07-11 17:11:58 +0000299 { 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 -0700300 { 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 -0700301 { 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 -0700302
303#define UNARY_ENCODING_MAP(opname, modrm, is_store, sets_ccodes, \
304 reg, reg_kind, reg_flags, \
305 mem, mem_kind, mem_flags, \
306 arr, arr_kind, arr_flags, imm, \
307 b_flags, hw_flags, w_flags, \
308 b_format, hw_format, w_format) \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700309{ 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" }, \
310{ 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]" }, \
311{ 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]" }, \
312{ 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" }, \
313{ 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]" }, \
314{ 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]" }, \
315{ 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" }, \
316{ 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]" }, \
317{ 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]" }, \
318{ 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" }, \
319{ 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]" }, \
320{ 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 -0700321
322 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, "", "", ""),
323 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, "", "", ""),
324
Mark Mendell2bf31e62014-01-23 12:13:40 -0800325 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,"),
326 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,"),
327 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,"),
328 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 -0700329#undef UNARY_ENCODING_MAP
330
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700331 { kx86Cdq32Da, kRegOpcode, NO_OPERAND | REG_DEFAD_USEA, { 0, 0, 0x99, 0, 0, 0, 0, 0, false }, "Cdq", "" },
332 { kx86Cqo64Da, kRegOpcode, NO_OPERAND | REG_DEFAD_USEA, { REX_W, 0, 0x99, 0, 0, 0, 0, 0, false }, "Cqo", "" },
333 { 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 +0700334 { 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 -0700335 { 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" },
336 { 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 +0100337
Brian Carlstrom7940e442013-07-12 13:46:57 -0700338#define EXT_0F_ENCODING_MAP(opname, prefix, opcode, reg_def) \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700339{ kX86 ## opname ## RR, kRegReg, IS_BINARY_OP | reg_def | REG_USE1, { prefix, 0, 0x0F, opcode, 0, 0, 0, 0, false }, #opname "RR", "!0r,!1r" }, \
340{ 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]" }, \
341{ 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 -0700342
Chao-ying Fu021b60f2014-07-09 11:32:31 -0700343// This is a special encoding with r8_form on the second register only
344// for Movzx8 and Movsx8.
345#define EXT_0F_R8_FORM_ENCODING_MAP(opname, prefix, opcode, reg_def) \
346{ kX86 ## opname ## RR, kRegReg, IS_BINARY_OP | reg_def | REG_USE1, { prefix, 0, 0x0F, opcode, 0, 0, 0, 0, true }, #opname "RR", "!0r,!1r" }, \
347{ 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]" }, \
348{ 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]" }
349
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700350#define EXT_0F_REX_W_ENCODING_MAP(opname, prefix, opcode, reg_def) \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700351{ 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" }, \
352{ 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]" }, \
353{ 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 -0700354
Mark Mendellfe945782014-05-22 09:52:36 -0400355#define EXT_0F_ENCODING2_MAP(opname, prefix, opcode, opcode2, reg_def) \
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700356{ kX86 ## opname ## RR, kRegReg, IS_BINARY_OP | reg_def | REG_USE1, { prefix, 0, 0x0F, opcode, opcode2, 0, 0, 0, false }, #opname "RR", "!0r,!1r" }, \
357{ 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]" }, \
358{ 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 -0400359
Brian Carlstrom7940e442013-07-12 13:46:57 -0700360 EXT_0F_ENCODING_MAP(Movsd, 0xF2, 0x10, REG_DEF0),
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700361 { kX86MovsdMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0xF2, 0, 0x0F, 0x11, 0, 0, 0, 0, false }, "MovsdMR", "[!0r+!1d],!2r" },
362 { 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 -0700363
364 EXT_0F_ENCODING_MAP(Movss, 0xF3, 0x10, REG_DEF0),
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700365 { kX86MovssMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0xF3, 0, 0x0F, 0x11, 0, 0, 0, 0, false }, "MovssMR", "[!0r+!1d],!2r" },
366 { 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 -0700367
368 EXT_0F_ENCODING_MAP(Cvtsi2sd, 0xF2, 0x2A, REG_DEF0),
369 EXT_0F_ENCODING_MAP(Cvtsi2ss, 0xF3, 0x2A, REG_DEF0),
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700370 EXT_0F_REX_W_ENCODING_MAP(Cvtsqi2sd, 0xF2, 0x2A, REG_DEF0),
371 EXT_0F_REX_W_ENCODING_MAP(Cvtsqi2ss, 0xF3, 0x2A, REG_DEF0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700372 EXT_0F_ENCODING_MAP(Cvttsd2si, 0xF2, 0x2C, REG_DEF0),
373 EXT_0F_ENCODING_MAP(Cvttss2si, 0xF3, 0x2C, REG_DEF0),
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700374 EXT_0F_REX_W_ENCODING_MAP(Cvttsd2sqi, 0xF2, 0x2C, REG_DEF0),
375 EXT_0F_REX_W_ENCODING_MAP(Cvttss2sqi, 0xF3, 0x2C, REG_DEF0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700376 EXT_0F_ENCODING_MAP(Cvtsd2si, 0xF2, 0x2D, REG_DEF0),
377 EXT_0F_ENCODING_MAP(Cvtss2si, 0xF3, 0x2D, REG_DEF0),
Mark Mendell2637f2e2014-04-30 10:10:47 -0400378 EXT_0F_ENCODING_MAP(Ucomisd, 0x66, 0x2E, SETS_CCODES|REG_USE0),
379 EXT_0F_ENCODING_MAP(Ucomiss, 0x00, 0x2E, SETS_CCODES|REG_USE0),
380 EXT_0F_ENCODING_MAP(Comisd, 0x66, 0x2F, SETS_CCODES|REG_USE0),
381 EXT_0F_ENCODING_MAP(Comiss, 0x00, 0x2F, SETS_CCODES|REG_USE0),
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700382 EXT_0F_ENCODING_MAP(Orpd, 0x66, 0x56, REG_DEF0_USE0),
Mark Mendell2637f2e2014-04-30 10:10:47 -0400383 EXT_0F_ENCODING_MAP(Orps, 0x00, 0x56, REG_DEF0_USE0),
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700384 EXT_0F_ENCODING_MAP(Andpd, 0x66, 0x54, REG_DEF0_USE0),
385 EXT_0F_ENCODING_MAP(Andps, 0x00, 0x54, REG_DEF0_USE0),
386 EXT_0F_ENCODING_MAP(Xorpd, 0x66, 0x57, REG_DEF0_USE0),
Mark Mendell2637f2e2014-04-30 10:10:47 -0400387 EXT_0F_ENCODING_MAP(Xorps, 0x00, 0x57, REG_DEF0_USE0),
388 EXT_0F_ENCODING_MAP(Addsd, 0xF2, 0x58, REG_DEF0_USE0),
389 EXT_0F_ENCODING_MAP(Addss, 0xF3, 0x58, REG_DEF0_USE0),
390 EXT_0F_ENCODING_MAP(Mulsd, 0xF2, 0x59, REG_DEF0_USE0),
391 EXT_0F_ENCODING_MAP(Mulss, 0xF3, 0x59, REG_DEF0_USE0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700392 EXT_0F_ENCODING_MAP(Cvtsd2ss, 0xF2, 0x5A, REG_DEF0),
393 EXT_0F_ENCODING_MAP(Cvtss2sd, 0xF3, 0x5A, REG_DEF0),
Mark Mendell2637f2e2014-04-30 10:10:47 -0400394 EXT_0F_ENCODING_MAP(Subsd, 0xF2, 0x5C, REG_DEF0_USE0),
395 EXT_0F_ENCODING_MAP(Subss, 0xF3, 0x5C, REG_DEF0_USE0),
396 EXT_0F_ENCODING_MAP(Divsd, 0xF2, 0x5E, REG_DEF0_USE0),
397 EXT_0F_ENCODING_MAP(Divss, 0xF3, 0x5E, REG_DEF0_USE0),
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700398 EXT_0F_ENCODING_MAP(Punpcklbw, 0x66, 0x60, REG_DEF0_USE0),
399 EXT_0F_ENCODING_MAP(Punpcklwd, 0x66, 0x61, REG_DEF0_USE0),
Mark Mendell2637f2e2014-04-30 10:10:47 -0400400 EXT_0F_ENCODING_MAP(Punpckldq, 0x66, 0x62, REG_DEF0_USE0),
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700401 EXT_0F_ENCODING_MAP(Punpcklqdq, 0x66, 0x6C, REG_DEF0_USE0),
Mark Mendellfe945782014-05-22 09:52:36 -0400402 EXT_0F_ENCODING_MAP(Sqrtsd, 0xF2, 0x51, REG_DEF0_USE0),
403 EXT_0F_ENCODING2_MAP(Pmulld, 0x66, 0x38, 0x40, REG_DEF0_USE0),
404 EXT_0F_ENCODING_MAP(Pmullw, 0x66, 0xD5, REG_DEF0_USE0),
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700405 EXT_0F_ENCODING_MAP(Pmuludq, 0x66, 0xF4, REG_DEF0_USE0),
Mark Mendellfe945782014-05-22 09:52:36 -0400406 EXT_0F_ENCODING_MAP(Mulps, 0x00, 0x59, REG_DEF0_USE0),
407 EXT_0F_ENCODING_MAP(Mulpd, 0x66, 0x59, REG_DEF0_USE0),
408 EXT_0F_ENCODING_MAP(Paddb, 0x66, 0xFC, REG_DEF0_USE0),
409 EXT_0F_ENCODING_MAP(Paddw, 0x66, 0xFD, REG_DEF0_USE0),
410 EXT_0F_ENCODING_MAP(Paddd, 0x66, 0xFE, REG_DEF0_USE0),
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700411 EXT_0F_ENCODING_MAP(Paddq, 0x66, 0xD4, REG_DEF0_USE0),
412 EXT_0F_ENCODING_MAP(Psadbw, 0x66, 0xF6, REG_DEF0_USE0),
Mark Mendellfe945782014-05-22 09:52:36 -0400413 EXT_0F_ENCODING_MAP(Addps, 0x00, 0x58, REG_DEF0_USE0),
Chao-ying Fuc4013ea2015-04-22 10:51:21 -0700414 EXT_0F_ENCODING_MAP(Addpd, 0x66, 0x58, REG_DEF0_USE0),
Mark Mendellfe945782014-05-22 09:52:36 -0400415 EXT_0F_ENCODING_MAP(Psubb, 0x66, 0xF8, REG_DEF0_USE0),
416 EXT_0F_ENCODING_MAP(Psubw, 0x66, 0xF9, REG_DEF0_USE0),
417 EXT_0F_ENCODING_MAP(Psubd, 0x66, 0xFA, REG_DEF0_USE0),
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700418 EXT_0F_ENCODING_MAP(Psubq, 0x66, 0xFB, REG_DEF0_USE0),
Mark Mendellfe945782014-05-22 09:52:36 -0400419 EXT_0F_ENCODING_MAP(Subps, 0x00, 0x5C, REG_DEF0_USE0),
420 EXT_0F_ENCODING_MAP(Subpd, 0x66, 0x5C, REG_DEF0_USE0),
421 EXT_0F_ENCODING_MAP(Pand, 0x66, 0xDB, REG_DEF0_USE0),
422 EXT_0F_ENCODING_MAP(Por, 0x66, 0xEB, REG_DEF0_USE0),
423 EXT_0F_ENCODING_MAP(Pxor, 0x66, 0xEF, REG_DEF0_USE0),
424 EXT_0F_ENCODING2_MAP(Phaddw, 0x66, 0x38, 0x01, REG_DEF0_USE0),
425 EXT_0F_ENCODING2_MAP(Phaddd, 0x66, 0x38, 0x02, REG_DEF0_USE0),
Olivier Comefb0fecf2014-06-20 11:46:16 +0200426 EXT_0F_ENCODING_MAP(Haddpd, 0x66, 0x7C, REG_DEF0_USE0),
427 EXT_0F_ENCODING_MAP(Haddps, 0xF2, 0x7C, REG_DEF0_USE0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700428
Serguei Katkov35690632014-07-16 15:52:59 +0700429 { 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 -0700430 { 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 +0700431 { kX86PextrdRRI, kRegRegImmStore, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0x66, 0, 0x0F, 0x3A, 0x16, 0, 0, 1, false }, "PextdRRI", "!0r,!1r,!2d" },
Dmitry Petrochenkof18b92f2014-11-14 17:32:56 +0600432 { kX86PextrbMRI, kMemRegImm, IS_QUAD_OP | REG_USE02 | IS_STORE, { 0x66, 0, 0x0F, 0x3A, 0x16, 0, 0, 1, false }, "PextrbMRI", "[!0r+!1d],!2r,!3d" },
nikolay serdjuke0705f52015-04-27 17:52:57 +0600433 { kX86PextrwMRI, kMemRegImm, IS_QUAD_OP | REG_USE02 | IS_STORE, { 0x66, 0, 0x0F, 0x3A, 0x15, 0, 0, 1, false }, "PextrwMRI", "[!0r+!1d],!2r,!3d" },
Dmitry Petrochenkof18b92f2014-11-14 17:32:56 +0600434 { kX86PextrdMRI, kMemRegImm, IS_QUAD_OP | REG_USE02 | IS_STORE, { 0x66, 0, 0x0F, 0x3A, 0x16, 0, 0, 1, false }, "PextrdMRI", "[!0r+!1d],!2r,!3d" },
Mark Mendellfe945782014-05-22 09:52:36 -0400435
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700436 { kX86PshuflwRRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0 | REG_USE1, { 0xF2, 0, 0x0F, 0x70, 0, 0, 0, 1, false }, "PshuflwRRI", "!0r,!1r,!2d" },
437 { 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 -0400438
Dmitry Petrochenkof18b92f2014-11-14 17:32:56 +0600439 { kX86ShufpsRRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE0 | REG_USE1, { 0x00, 0, 0x0F, 0xC6, 0, 0, 0, 1, false }, "ShufpsRRI", "!0r,!1r,!2d" },
440 { kX86ShufpdRRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE0 | REG_USE1, { 0x66, 0, 0x0F, 0xC6, 0, 0, 0, 1, false }, "ShufpdRRI", "!0r,!1r,!2d" },
Olivier Comefb0fecf2014-06-20 11:46:16 +0200441
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700442 { kX86PsrawRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x71, 0, 4, 0, 1, false }, "PsrawRI", "!0r,!1d" },
443 { kX86PsradRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x72, 0, 4, 0, 1, false }, "PsradRI", "!0r,!1d" },
444 { kX86PsrlwRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x71, 0, 2, 0, 1, false }, "PsrlwRI", "!0r,!1d" },
445 { kX86PsrldRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x72, 0, 2, 0, 1, false }, "PsrldRI", "!0r,!1d" },
446 { 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 -0700447 { 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 -0700448 { kX86PsllwRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x71, 0, 6, 0, 1, false }, "PsllwRI", "!0r,!1d" },
449 { kX86PslldRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x72, 0, 6, 0, 1, false }, "PslldRI", "!0r,!1d" },
450 { 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 -0800451
Haitao Fenga870bc52014-09-09 15:52:34 +0800452 { kX86Fild32M, kMem, IS_LOAD | IS_BINARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xDB, 0x00, 0, 0, 0, 0, false }, "Fild32M", "[!0r,!1d]" },
453 { kX86Fild64M, kMem, IS_LOAD | IS_BINARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xDF, 0x00, 0, 5, 0, 0, false }, "Fild64M", "[!0r,!1d]" },
454 { kX86Fld32M, kMem, IS_LOAD | IS_BINARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xD9, 0x00, 0, 0, 0, 0, false }, "Fld32M", "[!0r,!1d]" },
455 { kX86Fld64M, kMem, IS_LOAD | IS_BINARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xDD, 0x00, 0, 0, 0, 0, false }, "Fld64M", "[!0r,!1d]" },
456 { kX86Fstp32M, kMem, IS_STORE | IS_BINARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xD9, 0x00, 0, 3, 0, 0, false }, "Fstps32M", "[!0r,!1d]" },
457 { kX86Fstp64M, kMem, IS_STORE | IS_BINARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xDD, 0x00, 0, 3, 0, 0, false }, "Fstpd64M", "[!0r,!1d]" },
458 { kX86Fst32M, kMem, IS_STORE | IS_BINARY_OP | REG_USE0 | USE_FP_STACK, { 0x0, 0, 0xD9, 0x00, 0, 2, 0, 0, false }, "Fsts32M", "[!0r,!1d]" },
459 { 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 +0700460 { kX86Fprem, kNullary, NO_OPERAND | USE_FP_STACK, { 0xD9, 0, 0xF8, 0, 0, 0, 0, 0, false }, "Fprem64", "" },
461 { kX86Fucompp, kNullary, NO_OPERAND | USE_FP_STACK, { 0xDA, 0, 0xE9, 0, 0, 0, 0, 0, false }, "Fucompp", "" },
Mark Mendell01a50d62014-07-06 12:24:40 -0400462 { 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 -0700463
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700464 EXT_0F_ENCODING_MAP(Movdqa, 0x66, 0x6F, REG_DEF0),
465 { kX86MovdqaMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, 0, 0x0F, 0x6F, 0, 0, 0, 0, false }, "MovdqaMR", "[!0r+!1d],!2r" },
466 { 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 -0400467
468
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800469 EXT_0F_ENCODING_MAP(Movups, 0x0, 0x10, REG_DEF0),
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700470 { kX86MovupsMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x0, 0, 0x0F, 0x11, 0, 0, 0, 0, false }, "MovupsMR", "[!0r+!1d],!2r" },
471 { 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 -0800472
473 EXT_0F_ENCODING_MAP(Movaps, 0x0, 0x28, REG_DEF0),
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700474 { kX86MovapsMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x0, 0, 0x0F, 0x29, 0, 0, 0, 0, false }, "MovapsMR", "[!0r+!1d],!2r" },
475 { 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 -0800476
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700477 { kX86MovlpsRM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0 | REG_USE01, { 0x0, 0, 0x0F, 0x12, 0, 0, 0, 0, false }, "MovlpsRM", "!0r,[!1r+!2d]" },
478 { 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]" },
479 { kX86MovlpsMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x0, 0, 0x0F, 0x13, 0, 0, 0, 0, false }, "MovlpsMR", "[!0r+!1d],!2r" },
480 { 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 -0800481
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700482 { kX86MovhpsRM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0 | REG_USE01, { 0x0, 0, 0x0F, 0x16, 0, 0, 0, 0, false }, "MovhpsRM", "!0r,[!1r+!2d]" },
483 { 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]" },
484 { kX86MovhpsMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x0, 0, 0x0F, 0x17, 0, 0, 0, 0, false }, "MovhpsMR", "[!0r+!1d],!2r" },
485 { 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 -0800486
Brian Carlstrom7940e442013-07-12 13:46:57 -0700487 EXT_0F_ENCODING_MAP(Movdxr, 0x66, 0x6E, REG_DEF0),
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700488 EXT_0F_REX_W_ENCODING_MAP(Movqxr, 0x66, 0x6E, REG_DEF0),
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700489 { kX86MovqrxRR, kRegRegStore, IS_BINARY_OP | REG_DEF0 | REG_USE1, { 0x66, REX_W, 0x0F, 0x7E, 0, 0, 0, 0, false }, "MovqrxRR", "!0r,!1r" },
490 { kX86MovqrxMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, REX_W, 0x0F, 0x7E, 0, 0, 0, 0, false }, "MovqrxMR", "[!0r+!1d],!2r" },
491 { 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 -0700492
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700493 { kX86MovdrxRR, kRegRegStore, IS_BINARY_OP | REG_DEF0 | REG_USE1, { 0x66, 0, 0x0F, 0x7E, 0, 0, 0, 0, false }, "MovdrxRR", "!0r,!1r" },
494 { kX86MovdrxMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, 0, 0x0F, 0x7E, 0, 0, 0, 0, false }, "MovdrxMR", "[!0r+!1d],!2r" },
495 { 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 -0700496
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700497 { kX86MovsxdRR, kRegReg, IS_BINARY_OP | REG_DEF0 | REG_USE1, { REX_W, 0, 0x63, 0, 0, 0, 0, 0, false }, "MovsxdRR", "!0r,!1r" },
498 { 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]" },
499 { 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 -0700500
Mark Mendell2bc47702014-07-31 14:36:54 -0400501 { 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 -0700502 { kX86Set8M, kMemCond, IS_STORE | IS_TERTIARY_OP | REG_USE0 | USES_CCODES, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0, false }, "Set8M", "!2c [!0r+!1d]" },
503 { 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 -0700504
505 // TODO: load/store?
506 // Encode the modrm opcode as an extra opcode byte to avoid computation during assembly.
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -0700507 { kX86Lfence, kReg, NO_OPERAND, { 0, 0, 0x0F, 0xAE, 0, 5, 0, 0, false }, "Lfence", "" },
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700508 { kX86Mfence, kReg, NO_OPERAND, { 0, 0, 0x0F, 0xAE, 0, 6, 0, 0, false }, "Mfence", "" },
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -0700509 { kX86Sfence, kReg, NO_OPERAND, { 0, 0, 0x0F, 0xAE, 0, 7, 0, 0, false }, "Sfence", "" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700510
Mark Mendell2637f2e2014-04-30 10:10:47 -0400511 EXT_0F_ENCODING_MAP(Imul16, 0x66, 0xAF, REG_USE0 | REG_DEF0 | SETS_CCODES),
512 EXT_0F_ENCODING_MAP(Imul32, 0x00, 0xAF, REG_USE0 | REG_DEF0 | SETS_CCODES),
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700513 EXT_0F_ENCODING_MAP(Imul64, REX_W, 0xAF, REG_USE0 | REG_DEF0 | SETS_CCODES),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700514
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700515 { 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" },
516 { 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" },
517 { 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" },
518 { 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" },
519 { 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 +0700520 { 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 -0700521 { 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]" },
522 { 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]" },
523 { 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 -0700524
Chao-ying Fu021b60f2014-07-09 11:32:31 -0700525 EXT_0F_R8_FORM_ENCODING_MAP(Movzx8, 0x00, 0xB6, REG_DEF0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700526 EXT_0F_ENCODING_MAP(Movzx16, 0x00, 0xB7, REG_DEF0),
Chao-ying Fu021b60f2014-07-09 11:32:31 -0700527 EXT_0F_R8_FORM_ENCODING_MAP(Movsx8, 0x00, 0xBE, REG_DEF0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700528 EXT_0F_ENCODING_MAP(Movsx16, 0x00, 0xBF, REG_DEF0),
Serguei Katkov94f3eb02014-06-24 13:23:17 +0700529 EXT_0F_ENCODING_MAP(Movzx8q, REX_W, 0xB6, REG_DEF0),
530 EXT_0F_ENCODING_MAP(Movzx16q, REX_W, 0xB7, REG_DEF0),
531 EXT_0F_ENCODING_MAP(Movsx8q, REX, 0xBE, REG_DEF0),
532 EXT_0F_ENCODING_MAP(Movsx16q, REX_W, 0xBF, REG_DEF0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700533#undef EXT_0F_ENCODING_MAP
534
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700535 { kX86Jcc8, kJcc, IS_BINARY_OP | IS_BRANCH | NEEDS_FIXUP | USES_CCODES, { 0, 0, 0x70, 0, 0, 0, 0, 0, false }, "Jcc8", "!1c !0t" },
536 { kX86Jcc32, kJcc, IS_BINARY_OP | IS_BRANCH | NEEDS_FIXUP | USES_CCODES, { 0, 0, 0x0F, 0x80, 0, 0, 0, 0, false }, "Jcc32", "!1c !0t" },
537 { kX86Jmp8, kJmp, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0xEB, 0, 0, 0, 0, 0, false }, "Jmp8", "!0t" },
538 { kX86Jmp32, kJmp, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0xE9, 0, 0, 0, 0, 0, false }, "Jmp32", "!0t" },
539 { kX86JmpR, kJmp, IS_UNARY_OP | IS_BRANCH | REG_USE0, { 0, 0, 0xFF, 0, 0, 4, 0, 0, false }, "JmpR", "!0r" },
540 { kX86Jecxz8, kJmp, NO_OPERAND | IS_BRANCH | NEEDS_FIXUP | REG_USEC, { 0, 0, 0xE3, 0, 0, 0, 0, 0, false }, "Jecxz", "!0t" },
541 { kX86JmpT, kJmp, IS_UNARY_OP | IS_BRANCH | IS_LOAD, { THREAD_PREFIX, 0, 0xFF, 0, 0, 4, 0, 0, false }, "JmpT", "fs:[!0d]" },
542 { kX86CallR, kCall, IS_UNARY_OP | IS_BRANCH | REG_USE0, { 0, 0, 0xE8, 0, 0, 0, 0, 0, false }, "CallR", "!0r" },
543 { kX86CallM, kCall, IS_BINARY_OP | IS_BRANCH | IS_LOAD | REG_USE0, { 0, 0, 0xFF, 0, 0, 2, 0, 0, false }, "CallM", "[!0r+!1d]" },
544 { 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]" },
545 { kX86CallT, kCall, IS_UNARY_OP | IS_BRANCH | IS_LOAD, { THREAD_PREFIX, 0, 0xFF, 0, 0, 2, 0, 0, false }, "CallT", "fs:[!0d]" },
546 { kX86CallI, kCall, IS_UNARY_OP | IS_BRANCH, { 0, 0, 0xE8, 0, 0, 0, 0, 4, false }, "CallI", "!0d" },
547 { kX86Ret, kNullary, NO_OPERAND | IS_BRANCH, { 0, 0, 0xC3, 0, 0, 0, 0, 0, false }, "Ret", "" },
Brian Carlstrom7940e442013-07-12 13:46:57 -0700548
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700549 { 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 +0800550 { 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 -0700551 { 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 -0700552};
553
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700554std::ostream& operator<<(std::ostream& os, const X86OpCode& rhs) {
555 os << X86Mir2Lir::EncodingMap[rhs].name;
556 return os;
557}
558
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700559static bool NeedsRex(int32_t raw_reg) {
Mark Mendell27dee8b2014-12-01 19:06:12 -0500560 return raw_reg != kRIPReg && RegStorage::RegNum(raw_reg) > 7;
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700561}
562
563static uint8_t LowRegisterBits(int32_t raw_reg) {
564 uint8_t low_reg = RegStorage::RegNum(raw_reg) & kRegNumMask32; // 3 bits
565 DCHECK_LT(low_reg, 8);
566 return low_reg;
567}
568
Ian Rogers5aa6e042014-06-13 16:38:24 -0700569static bool HasModrm(const X86EncodingMap* entry) {
570 switch (entry->kind) {
571 case kNullary: return false;
572 case kRegOpcode: return false;
573 default: return true;
574 }
575}
576
577static bool HasSib(const X86EncodingMap* entry) {
578 switch (entry->kind) {
579 case kArray: return true;
580 case kArrayReg: return true;
581 case kRegArray: return true;
582 case kArrayImm: return true;
583 case kRegArrayImm: return true;
584 case kShiftArrayImm: return true;
585 case kShiftArrayCl: return true;
586 case kArrayCond: return true;
587 case kCall:
588 switch (entry->opcode) {
589 case kX86CallA: return true;
590 default: return false;
591 }
Ian Rogers07140832014-09-30 15:43:59 -0700592 case kPcRel:
Ian Rogers5aa6e042014-06-13 16:38:24 -0700593 switch (entry->opcode) {
594 case kX86PcRelLoadRA: return true;
595 default: return false;
596 }
597 default: return false;
598 }
599}
600
601static bool ModrmIsRegReg(const X86EncodingMap* entry) {
602 switch (entry->kind) {
603 // There is no modrm for this kind of instruction, therefore the reg doesn't form part of the
604 // modrm:
605 case kNullary: return true;
606 case kRegOpcode: return true;
607 case kMovRegImm: return true;
608 // Regular modrm value of 3 cases, when there is one register the other register holds an
609 // opcode so the base register is special.
610 case kReg: return true;
611 case kRegReg: return true;
612 case kRegRegStore: return true;
613 case kRegImm: return true;
614 case kRegRegImm: return true;
615 case kRegRegImmStore: return true;
616 case kShiftRegImm: return true;
617 case kShiftRegCl: return true;
618 case kRegCond: return true;
619 case kRegRegCond: return true;
Yixin Shouf40f8902014-08-14 14:10:32 -0400620 case kShiftRegRegCl: return true;
Ian Rogers5aa6e042014-06-13 16:38:24 -0700621 case kJmp:
622 switch (entry->opcode) {
623 case kX86JmpR: return true;
624 default: return false;
625 }
626 case kCall:
627 switch (entry->opcode) {
628 case kX86CallR: return true;
629 default: return false;
630 }
631 default: return false;
632 }
633}
634
Chao-ying Fu021b60f2014-07-09 11:32:31 -0700635static bool IsByteSecondOperand(const X86EncodingMap* entry) {
636 return StartsWith(entry->name, "Movzx8") || StartsWith(entry->name, "Movsx8");
637}
638
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700639size_t X86Mir2Lir::ComputeSize(const X86EncodingMap* entry, int32_t raw_reg, int32_t raw_index,
Ian Rogers5aa6e042014-06-13 16:38:24 -0700640 int32_t raw_base, int32_t displacement) {
641 bool has_modrm = HasModrm(entry);
642 bool has_sib = HasSib(entry);
643 bool r8_form = entry->skeleton.r8_form;
644 bool modrm_is_reg_reg = ModrmIsRegReg(entry);
645 if (has_sib) {
646 DCHECK(!modrm_is_reg_reg);
647 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700648 size_t size = 0;
649 if (entry->skeleton.prefix1 > 0) {
650 ++size;
651 if (entry->skeleton.prefix2 > 0) {
652 ++size;
653 }
654 }
Elena Sayapinadd644502014-07-01 18:39:52 +0700655 if (cu_->target64 || kIsDebugBuild) {
Ian Rogers5aa6e042014-06-13 16:38:24 -0700656 bool registers_need_rex_prefix = NeedsRex(raw_reg) || NeedsRex(raw_index) || NeedsRex(raw_base);
657 if (r8_form) {
658 // Do we need an empty REX prefix to normalize byte registers?
Chao-ying Fu021b60f2014-07-09 11:32:31 -0700659 registers_need_rex_prefix = registers_need_rex_prefix ||
660 (RegStorage::RegNum(raw_reg) >= 4 && !IsByteSecondOperand(entry));
Ian Rogers5aa6e042014-06-13 16:38:24 -0700661 registers_need_rex_prefix = registers_need_rex_prefix ||
662 (modrm_is_reg_reg && (RegStorage::RegNum(raw_base) >= 4));
663 }
664 if (registers_need_rex_prefix) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700665 DCHECK(cu_->target64) << "Attempt to use a 64-bit only addressable register "
Ian Rogers5aa6e042014-06-13 16:38:24 -0700666 << RegStorage::RegNum(raw_reg) << " with instruction " << entry->name;
Serguei Katkov94f3eb02014-06-24 13:23:17 +0700667 if (entry->skeleton.prefix1 != REX_W && entry->skeleton.prefix2 != REX_W
668 && entry->skeleton.prefix1 != REX && entry->skeleton.prefix2 != REX) {
Ian Rogers5aa6e042014-06-13 16:38:24 -0700669 ++size; // rex
670 }
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700671 }
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700672 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700673 ++size; // opcode
674 if (entry->skeleton.opcode == 0x0F) {
675 ++size;
676 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
677 ++size;
678 }
679 }
Ian Rogers5aa6e042014-06-13 16:38:24 -0700680 if (has_modrm) {
681 ++size; // modrm
Brian Carlstrom7940e442013-07-12 13:46:57 -0700682 }
Ian Rogers5aa6e042014-06-13 16:38:24 -0700683 if (!modrm_is_reg_reg) {
Ian Rogersb28c1c02014-11-08 11:21:21 -0800684 if (has_sib || (LowRegisterBits(raw_base) == rs_rX86_SP_32.GetRegNum())
Elena Sayapinadd644502014-07-01 18:39:52 +0700685 || (cu_->target64 && entry->skeleton.prefix1 == THREAD_PREFIX)) {
Ian Rogers5aa6e042014-06-13 16:38:24 -0700686 // SP requires a SIB byte.
687 // GS access also needs a SIB byte for absolute adressing in 64-bit mode.
688 ++size;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700689 }
Ian Rogers5aa6e042014-06-13 16:38:24 -0700690 if (displacement != 0 || LowRegisterBits(raw_base) == rs_rBP.GetRegNum()) {
691 // BP requires an explicit displacement, even when it's 0.
Haitao Fenga870bc52014-09-09 15:52:34 +0800692 if (entry->opcode != kX86Lea32RA && entry->opcode != kX86Lea64RA &&
693 entry->opcode != kX86Lea32RM && entry->opcode != kX86Lea64RM) {
Ian Rogers5aa6e042014-06-13 16:38:24 -0700694 DCHECK_NE(entry->flags & (IS_LOAD | IS_STORE), UINT64_C(0)) << entry->name;
695 }
Mark Mendell27dee8b2014-12-01 19:06:12 -0500696 if (raw_base == kRIPReg) {
697 DCHECK(cu_->target64) <<
698 "Attempt to use a 64-bit RIP adressing with instruction " << entry->name;
699 size += 4;
700 } else {
701 size += IS_SIMM8(displacement) ? 1 : 4;
702 }
Ian Rogers5aa6e042014-06-13 16:38:24 -0700703 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700704 }
705 size += entry->skeleton.immediate_bytes;
706 return size;
707}
708
Ian Rogers5aa6e042014-06-13 16:38:24 -0700709size_t X86Mir2Lir::GetInsnSize(LIR* lir) {
buzbee409fe942013-10-11 10:49:56 -0700710 DCHECK(!IsPseudoLirOp(lir->opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700711 const X86EncodingMap* entry = &X86Mir2Lir::EncodingMap[lir->opcode];
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700712 DCHECK_EQ(entry->opcode, lir->opcode) << entry->name;
Ian Rogers5aa6e042014-06-13 16:38:24 -0700713
Brian Carlstrom7940e442013-07-12 13:46:57 -0700714 switch (entry->kind) {
715 case kData:
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700716 return 4; // 4 bytes of data.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700717 case kNop:
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700718 return lir->operands[0]; // Length of nop is sole operand.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700719 case kNullary:
Ian Rogers5aa6e042014-06-13 16:38:24 -0700720 return ComputeSize(entry, NO_REG, NO_REG, NO_REG, 0);
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100721 case kRegOpcode: // lir operands - 0: reg
Ian Rogers5aa6e042014-06-13 16:38:24 -0700722 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700723 case kReg: // lir operands - 0: reg
Ian Rogers5aa6e042014-06-13 16:38:24 -0700724 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700725 case kMem: // lir operands - 0: base, 1: disp
Ian Rogers5aa6e042014-06-13 16:38:24 -0700726 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], lir->operands[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700727 case kArray: // lir operands - 0: base, 1: index, 2: scale, 3: disp
Ian Rogers5aa6e042014-06-13 16:38:24 -0700728 return ComputeSize(entry, NO_REG, lir->operands[1], lir->operands[0], lir->operands[3]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700729 case kMemReg: // lir operands - 0: base, 1: disp, 2: reg
Ian Rogers5aa6e042014-06-13 16:38:24 -0700730 return ComputeSize(entry, lir->operands[2], NO_REG, lir->operands[0], lir->operands[1]);
Mark Mendell2637f2e2014-04-30 10:10:47 -0400731 case kMemRegImm: // lir operands - 0: base, 1: disp, 2: reg 3: immediate
Ian Rogers5aa6e042014-06-13 16:38:24 -0700732 return ComputeSize(entry, lir->operands[2], NO_REG, lir->operands[0], lir->operands[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700733 case kArrayReg: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700734 return ComputeSize(entry, lir->operands[4], lir->operands[1], lir->operands[0],
Ian Rogers5aa6e042014-06-13 16:38:24 -0700735 lir->operands[3]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700736 case kThreadReg: // lir operands - 0: disp, 1: reg
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700737 // Thread displacement size is always 32bit.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700738 return ComputeSize(entry, lir->operands[1], NO_REG, NO_REG, 0x12345678);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700739 case kRegReg: // lir operands - 0: reg1, 1: reg2
Ian Rogers5aa6e042014-06-13 16:38:24 -0700740 return ComputeSize(entry, lir->operands[0], NO_REG, lir->operands[1], 0);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700741 case kRegRegStore: // lir operands - 0: reg2, 1: reg1
Ian Rogers5aa6e042014-06-13 16:38:24 -0700742 return ComputeSize(entry, lir->operands[1], NO_REG, lir->operands[0], 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700743 case kRegMem: // lir operands - 0: reg, 1: base, 2: disp
Ian Rogers5aa6e042014-06-13 16:38:24 -0700744 return ComputeSize(entry, lir->operands[0], NO_REG, lir->operands[1], lir->operands[2]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700745 case kRegArray: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700746 return ComputeSize(entry, lir->operands[0], lir->operands[2], lir->operands[1],
Ian Rogers5aa6e042014-06-13 16:38:24 -0700747 lir->operands[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700748 case kRegThread: // lir operands - 0: reg, 1: disp
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700749 // Thread displacement size is always 32bit.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700750 return ComputeSize(entry, lir->operands[0], NO_REG, NO_REG, 0x12345678);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700751 case kRegImm: { // lir operands - 0: reg, 1: immediate
Ian Rogers5aa6e042014-06-13 16:38:24 -0700752 size_t size = ComputeSize(entry, lir->operands[0], NO_REG, NO_REG, 0);
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700753 // AX opcodes don't require the modrm byte.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700754 if (entry->skeleton.ax_opcode == 0) {
755 return size;
756 } else {
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700757 return size - (RegStorage::RegNum(lir->operands[0]) == rs_rAX.GetRegNum() ? 1 : 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700758 }
759 }
760 case kMemImm: // lir operands - 0: base, 1: disp, 2: immediate
Ian Rogers5aa6e042014-06-13 16:38:24 -0700761 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], lir->operands[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700762 case kArrayImm: // lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
Ian Rogers5aa6e042014-06-13 16:38:24 -0700763 return ComputeSize(entry, NO_REG, lir->operands[1], lir->operands[0], lir->operands[3]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700764 case kThreadImm: // lir operands - 0: disp, 1: imm
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700765 // Thread displacement size is always 32bit.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700766 return ComputeSize(entry, NO_REG, NO_REG, NO_REG, 0x12345678);
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700767 case kRegRegImm: // lir operands - 0: reg1, 1: reg2, 2: imm
768 // Note: RegRegImm form passes reg2 as index but encodes it using base.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700769 return ComputeSize(entry, lir->operands[0], lir->operands[1], NO_REG, 0);
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700770 case kRegRegImmStore: // lir operands - 0: reg2, 1: reg1, 2: imm
771 // Note: RegRegImmStore form passes reg1 as index but encodes it using base.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700772 return ComputeSize(entry, lir->operands[1], lir->operands[0], NO_REG, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700773 case kRegMemImm: // lir operands - 0: reg, 1: base, 2: disp, 3: imm
Ian Rogers5aa6e042014-06-13 16:38:24 -0700774 return ComputeSize(entry, lir->operands[0], NO_REG, lir->operands[1], lir->operands[2]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700775 case kRegArrayImm: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp, 5: imm
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700776 return ComputeSize(entry, lir->operands[0], lir->operands[2], lir->operands[1],
Ian Rogers5aa6e042014-06-13 16:38:24 -0700777 lir->operands[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700778 case kMovRegImm: // lir operands - 0: reg, 1: immediate
Yixin Shou5192cbb2014-07-01 13:48:17 -0400779 case kMovRegQuadImm:
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700780 return ((entry->skeleton.prefix1 != 0 || NeedsRex(lir->operands[0])) ? 1 : 0) + 1 +
781 entry->skeleton.immediate_bytes;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700782 case kShiftRegImm: // lir operands - 0: reg, 1: immediate
783 // Shift by immediate one has a shorter opcode.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700784 return ComputeSize(entry, lir->operands[0], NO_REG, NO_REG, 0) -
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700785 (lir->operands[1] == 1 ? 1 : 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700786 case kShiftMemImm: // lir operands - 0: base, 1: disp, 2: immediate
787 // Shift by immediate one has a shorter opcode.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700788 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], lir->operands[1]) -
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700789 (lir->operands[2] == 1 ? 1 : 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700790 case kShiftArrayImm: // lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
791 // Shift by immediate one has a shorter opcode.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700792 return ComputeSize(entry, NO_REG, lir->operands[1], lir->operands[0], lir->operands[3]) -
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700793 (lir->operands[4] == 1 ? 1 : 0);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700794 case kShiftRegCl: // lir operands - 0: reg, 1: cl
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700795 DCHECK_EQ(rs_rCX.GetRegNum(), RegStorage::RegNum(lir->operands[1]));
796 // Note: ShiftRegCl form passes reg as reg but encodes it using base.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700797 return ComputeSize(entry, lir->operands[0], NO_REG, NO_REG, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700798 case kShiftMemCl: // lir operands - 0: base, 1: disp, 2: cl
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700799 DCHECK_EQ(rs_rCX.GetRegNum(), RegStorage::RegNum(lir->operands[2]));
Ian Rogers5aa6e042014-06-13 16:38:24 -0700800 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], lir->operands[1]);
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700801 case kShiftArrayCl: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: cl
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700802 DCHECK_EQ(rs_rCX.GetRegNum(), RegStorage::RegNum(lir->operands[4]));
803 return ComputeSize(entry, lir->operands[4], lir->operands[1], lir->operands[0],
Ian Rogers5aa6e042014-06-13 16:38:24 -0700804 lir->operands[3]);
Yixin Shouf40f8902014-08-14 14:10:32 -0400805 case kShiftRegRegCl: // lir operands - 0: reg1, 1: reg2, 2: cl
806 DCHECK_EQ(rs_rCX.GetRegNum(), RegStorage::RegNum(lir->operands[2]));
807 return ComputeSize(entry, lir->operands[0], NO_REG, lir->operands[1], 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700808 case kRegCond: // lir operands - 0: reg, 1: cond
Ian Rogers5aa6e042014-06-13 16:38:24 -0700809 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700810 case kMemCond: // lir operands - 0: base, 1: disp, 2: cond
Ian Rogers5aa6e042014-06-13 16:38:24 -0700811 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], lir->operands[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700812 case kArrayCond: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: cond
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700813 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -0700814 return ComputeSize(entry, NO_REG, lir->operands[1], lir->operands[0], lir->operands[3]);
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700815 case kRegRegCond: // lir operands - 0: reg1, 1: reg2, 2: cond
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700816 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -0700817 return ComputeSize(entry, lir->operands[0], NO_REG, lir->operands[1], 0);
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700818 case kRegMemCond: // lir operands - 0: reg, 1: base, 2: disp, 3:cond
819 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -0700820 return ComputeSize(entry, lir->operands[0], NO_REG, lir->operands[1], lir->operands[2]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700821 case kJcc:
822 if (lir->opcode == kX86Jcc8) {
823 return 2; // opcode + rel8
824 } else {
825 DCHECK(lir->opcode == kX86Jcc32);
826 return 6; // 2 byte opcode + rel32
827 }
828 case kJmp:
Mark Mendell4028a6c2014-02-19 20:06:20 -0800829 if (lir->opcode == kX86Jmp8 || lir->opcode == kX86Jecxz8) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700830 return 2; // opcode + rel8
831 } else if (lir->opcode == kX86Jmp32) {
832 return 5; // opcode + rel32
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700833 } else if (lir->opcode == kX86JmpT) {
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700834 // Thread displacement size is always 32bit.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700835 return ComputeSize(entry, NO_REG, NO_REG, NO_REG, 0x12345678);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700836 } else {
837 DCHECK(lir->opcode == kX86JmpR);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700838 if (NeedsRex(lir->operands[0])) {
839 return 3; // REX.B + opcode + modrm
840 } else {
841 return 2; // opcode + modrm
842 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700843 }
844 case kCall:
845 switch (lir->opcode) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800846 case kX86CallI: return 5; // opcode 0:disp
Brian Carlstrom7940e442013-07-12 13:46:57 -0700847 case kX86CallR: return 2; // opcode modrm
848 case kX86CallM: // lir operands - 0: base, 1: disp
Ian Rogers5aa6e042014-06-13 16:38:24 -0700849 return ComputeSize(entry, NO_REG, NO_REG, lir->operands[0], lir->operands[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700850 case kX86CallA: // lir operands - 0: base, 1: index, 2: scale, 3: disp
Ian Rogers5aa6e042014-06-13 16:38:24 -0700851 return ComputeSize(entry, NO_REG, lir->operands[1], lir->operands[0], lir->operands[3]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700852 case kX86CallT: // lir operands - 0: disp
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700853 // Thread displacement size is always 32bit.
Ian Rogers5aa6e042014-06-13 16:38:24 -0700854 return ComputeSize(entry, NO_REG, NO_REG, NO_REG, 0x12345678);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700855 default:
856 break;
857 }
858 break;
859 case kPcRel:
860 if (entry->opcode == kX86PcRelLoadRA) {
861 // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700862 // Force the displacement size to 32bit, it will hold a computed offset later.
863 return ComputeSize(entry, lir->operands[0], lir->operands[2], lir->operands[1],
Ian Rogers5aa6e042014-06-13 16:38:24 -0700864 0x12345678);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700865 } else {
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700866 DCHECK_EQ(entry->opcode, kX86PcRelAdr);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700867 return 5; // opcode with reg + 4 byte immediate
Brian Carlstrom7940e442013-07-12 13:46:57 -0700868 }
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700869 case kUnimplemented:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700870 break;
871 }
872 UNIMPLEMENTED(FATAL) << "Unimplemented size encoding for: " << entry->name;
873 return 0;
874}
875
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700876static uint8_t ModrmForDisp(int base, int disp) {
877 // BP requires an explicit disp, so do not omit it in the 0 case
878 if (disp == 0 && RegStorage::RegNum(base) != rs_rBP.GetRegNum()) {
879 return 0;
880 } else if (IS_SIMM8(disp)) {
881 return 1;
882 } else {
883 return 2;
884 }
885}
886
887void X86Mir2Lir::CheckValidByteRegister(const X86EncodingMap* entry, int32_t raw_reg) {
888 if (kIsDebugBuild) {
889 // Sanity check r8_form is correctly specified.
890 if (entry->skeleton.r8_form) {
891 CHECK(strchr(entry->name, '8') != nullptr) << entry->name;
892 } else {
893 if (entry->skeleton.immediate_bytes != 1) { // Ignore ...I8 instructions.
Serguei Katkov1c557032014-06-23 13:23:38 +0700894 if (!StartsWith(entry->name, "Movzx8") && !StartsWith(entry->name, "Movsx8")
895 && !StartsWith(entry->name, "Movzx8q") && !StartsWith(entry->name, "Movsx8q")) {
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700896 CHECK(strchr(entry->name, '8') == nullptr) << entry->name;
897 }
898 }
899 }
900 if (RegStorage::RegNum(raw_reg) >= 4) {
901 // ah, bh, ch and dh are not valid registers in 32-bit.
Elena Sayapinadd644502014-07-01 18:39:52 +0700902 CHECK(cu_->target64 || !entry->skeleton.r8_form)
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700903 << "Invalid register " << static_cast<int>(RegStorage::RegNum(raw_reg))
904 << " for instruction " << entry->name << " in "
905 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
906 }
907 }
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700908}
909
910void X86Mir2Lir::EmitPrefix(const X86EncodingMap* entry,
Ian Rogers5aa6e042014-06-13 16:38:24 -0700911 int32_t raw_reg_r, int32_t raw_reg_x, int32_t raw_reg_b) {
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700912 // REX.WRXB
913 // W - 64-bit operand
914 // R - MODRM.reg
915 // X - SIB.index
916 // B - MODRM.rm/SIB.base
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700917 bool w = (entry->skeleton.prefix1 == REX_W) || (entry->skeleton.prefix2 == REX_W);
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700918 bool r = NeedsRex(raw_reg_r);
919 bool x = NeedsRex(raw_reg_x);
920 bool b = NeedsRex(raw_reg_b);
Ian Rogers5aa6e042014-06-13 16:38:24 -0700921 bool r8_form = entry->skeleton.r8_form;
922 bool modrm_is_reg_reg = ModrmIsRegReg(entry);
923
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700924 uint8_t rex = 0;
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700925 if (r8_form) {
926 // Do we need an empty REX prefix to normalize byte register addressing?
Chao-ying Fu021b60f2014-07-09 11:32:31 -0700927 if (RegStorage::RegNum(raw_reg_r) >= 4 && !IsByteSecondOperand(entry)) {
Razvan A Lupusoruae9f3e62014-09-23 14:54:32 -0700928 rex |= REX; // REX.0000
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700929 } else if (modrm_is_reg_reg && RegStorage::RegNum(raw_reg_b) >= 4) {
Razvan A Lupusoruae9f3e62014-09-23 14:54:32 -0700930 rex |= REX; // REX.0000
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700931 }
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700932 }
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700933 if (w) {
Razvan A Lupusoruae9f3e62014-09-23 14:54:32 -0700934 rex |= REX_W; // REX.W000
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700935 }
936 if (r) {
Razvan A Lupusoruae9f3e62014-09-23 14:54:32 -0700937 rex |= REX_R; // REX.0R00
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700938 }
939 if (x) {
Razvan A Lupusoruae9f3e62014-09-23 14:54:32 -0700940 rex |= REX_X; // REX.00X0
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700941 }
942 if (b) {
Razvan A Lupusoruae9f3e62014-09-23 14:54:32 -0700943 rex |= REX_B; // REX.000B
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700944 }
Vladimir Marko057c74a2013-12-03 15:20:45 +0000945 if (entry->skeleton.prefix1 != 0) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700946 if (cu_->target64 && entry->skeleton.prefix1 == THREAD_PREFIX) {
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700947 // 64 bit addresses by GS, not FS.
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700948 code_buffer_.push_back(THREAD_PREFIX_GS);
949 } else {
Serguei Katkov94f3eb02014-06-24 13:23:17 +0700950 if (entry->skeleton.prefix1 == REX_W || entry->skeleton.prefix1 == REX) {
951 DCHECK(cu_->target64);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700952 rex |= entry->skeleton.prefix1;
953 code_buffer_.push_back(rex);
954 rex = 0;
955 } else {
956 code_buffer_.push_back(entry->skeleton.prefix1);
957 }
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700958 }
Vladimir Marko057c74a2013-12-03 15:20:45 +0000959 if (entry->skeleton.prefix2 != 0) {
Serguei Katkov94f3eb02014-06-24 13:23:17 +0700960 if (entry->skeleton.prefix2 == REX_W || entry->skeleton.prefix1 == REX) {
961 DCHECK(cu_->target64);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700962 rex |= entry->skeleton.prefix2;
963 code_buffer_.push_back(rex);
964 rex = 0;
965 } else {
966 code_buffer_.push_back(entry->skeleton.prefix2);
967 }
Vladimir Marko057c74a2013-12-03 15:20:45 +0000968 }
969 } else {
970 DCHECK_EQ(0, entry->skeleton.prefix2);
971 }
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700972 if (rex != 0) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700973 DCHECK(cu_->target64);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700974 code_buffer_.push_back(rex);
975 }
Vladimir Marko057c74a2013-12-03 15:20:45 +0000976}
977
978void X86Mir2Lir::EmitOpcode(const X86EncodingMap* entry) {
979 code_buffer_.push_back(entry->skeleton.opcode);
980 if (entry->skeleton.opcode == 0x0F) {
981 code_buffer_.push_back(entry->skeleton.extra_opcode1);
982 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
983 code_buffer_.push_back(entry->skeleton.extra_opcode2);
984 } else {
985 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
986 }
987 } else {
988 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
989 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
990 }
991}
992
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +0700993void X86Mir2Lir::EmitPrefixAndOpcode(const X86EncodingMap* entry,
Ian Rogers5aa6e042014-06-13 16:38:24 -0700994 int32_t raw_reg_r, int32_t raw_reg_x, int32_t raw_reg_b) {
995 EmitPrefix(entry, raw_reg_r, raw_reg_x, raw_reg_b);
Vladimir Marko057c74a2013-12-03 15:20:45 +0000996 EmitOpcode(entry);
997}
998
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700999void X86Mir2Lir::EmitDisp(uint8_t base, int32_t disp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001000 // BP requires an explicit disp, so do not omit it in the 0 case
buzbee091cc402014-03-31 10:14:40 -07001001 if (disp == 0 && RegStorage::RegNum(base) != rs_rBP.GetRegNum()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001002 return;
1003 } else if (IS_SIMM8(disp)) {
1004 code_buffer_.push_back(disp & 0xFF);
1005 } else {
1006 code_buffer_.push_back(disp & 0xFF);
1007 code_buffer_.push_back((disp >> 8) & 0xFF);
1008 code_buffer_.push_back((disp >> 16) & 0xFF);
1009 code_buffer_.push_back((disp >> 24) & 0xFF);
1010 }
1011}
1012
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001013void X86Mir2Lir::EmitModrmThread(uint8_t reg_or_opcode) {
Elena Sayapinadd644502014-07-01 18:39:52 +07001014 if (cu_->target64) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001015 // Absolute adressing for GS access.
Ian Rogersb28c1c02014-11-08 11:21:21 -08001016 uint8_t modrm = (0 << 6) | (reg_or_opcode << 3) | rs_rX86_SP_32.GetRegNum();
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001017 code_buffer_.push_back(modrm);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001018 uint8_t sib = (0/*TIMES_1*/ << 6) | (rs_rX86_SP_32.GetRegNum() << 3) | rs_rBP.GetRegNum();
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001019 code_buffer_.push_back(sib);
1020 } else {
1021 uint8_t modrm = (0 << 6) | (reg_or_opcode << 3) | rs_rBP.GetRegNum();
1022 code_buffer_.push_back(modrm);
1023 }
1024}
1025
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001026void X86Mir2Lir::EmitModrmDisp(uint8_t reg_or_opcode, uint8_t base, int32_t disp) {
1027 DCHECK_LT(reg_or_opcode, 8);
Mark Mendell27dee8b2014-12-01 19:06:12 -05001028 if (base == kRIPReg) {
1029 // x86_64 RIP handling: always 32 bit displacement.
1030 uint8_t modrm = (0x0 << 6) | (reg_or_opcode << 3) | 0x5;
1031 code_buffer_.push_back(modrm);
1032 code_buffer_.push_back(disp & 0xFF);
1033 code_buffer_.push_back((disp >> 8) & 0xFF);
1034 code_buffer_.push_back((disp >> 16) & 0xFF);
1035 code_buffer_.push_back((disp >> 24) & 0xFF);
1036 } else {
1037 DCHECK_LT(base, 8);
1038 uint8_t modrm = (ModrmForDisp(base, disp) << 6) | (reg_or_opcode << 3) | base;
1039 code_buffer_.push_back(modrm);
1040 if (base == rs_rX86_SP_32.GetRegNum()) {
1041 // Special SIB for SP base
1042 code_buffer_.push_back(0 << 6 | rs_rX86_SP_32.GetRegNum() << 3 | rs_rX86_SP_32.GetRegNum());
1043 }
1044 EmitDisp(base, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001045 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001046}
1047
Vladimir Marko057c74a2013-12-03 15:20:45 +00001048void X86Mir2Lir::EmitModrmSibDisp(uint8_t reg_or_opcode, uint8_t base, uint8_t index,
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001049 int scale, int32_t disp) {
buzbee091cc402014-03-31 10:14:40 -07001050 DCHECK_LT(RegStorage::RegNum(reg_or_opcode), 8);
1051 uint8_t modrm = (ModrmForDisp(base, disp) << 6) | RegStorage::RegNum(reg_or_opcode) << 3 |
Ian Rogersb28c1c02014-11-08 11:21:21 -08001052 rs_rX86_SP_32.GetRegNum();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001053 code_buffer_.push_back(modrm);
1054 DCHECK_LT(scale, 4);
buzbee091cc402014-03-31 10:14:40 -07001055 DCHECK_LT(RegStorage::RegNum(index), 8);
1056 DCHECK_LT(RegStorage::RegNum(base), 8);
1057 uint8_t sib = (scale << 6) | (RegStorage::RegNum(index) << 3) | RegStorage::RegNum(base);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001058 code_buffer_.push_back(sib);
1059 EmitDisp(base, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001060}
1061
Dmitry Petrochenko96992e82014-05-20 04:03:46 +07001062void X86Mir2Lir::EmitImm(const X86EncodingMap* entry, int64_t imm) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001063 switch (entry->skeleton.immediate_bytes) {
1064 case 1:
1065 DCHECK(IS_SIMM8(imm));
1066 code_buffer_.push_back(imm & 0xFF);
1067 break;
1068 case 2:
1069 DCHECK(IS_SIMM16(imm));
1070 code_buffer_.push_back(imm & 0xFF);
1071 code_buffer_.push_back((imm >> 8) & 0xFF);
1072 break;
1073 case 4:
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001074 DCHECK(IS_SIMM32(imm));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001075 code_buffer_.push_back(imm & 0xFF);
1076 code_buffer_.push_back((imm >> 8) & 0xFF);
1077 code_buffer_.push_back((imm >> 16) & 0xFF);
1078 code_buffer_.push_back((imm >> 24) & 0xFF);
1079 break;
Dmitry Petrochenko96992e82014-05-20 04:03:46 +07001080 case 8:
1081 code_buffer_.push_back(imm & 0xFF);
1082 code_buffer_.push_back((imm >> 8) & 0xFF);
1083 code_buffer_.push_back((imm >> 16) & 0xFF);
1084 code_buffer_.push_back((imm >> 24) & 0xFF);
1085 code_buffer_.push_back((imm >> 32) & 0xFF);
1086 code_buffer_.push_back((imm >> 40) & 0xFF);
1087 code_buffer_.push_back((imm >> 48) & 0xFF);
1088 code_buffer_.push_back((imm >> 56) & 0xFF);
1089 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001090 default:
1091 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
1092 << ") for instruction: " << entry->name;
1093 break;
1094 }
1095}
1096
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001097void X86Mir2Lir::EmitNullary(const X86EncodingMap* entry) {
1098 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001099 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, NO_REG);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001100 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001101 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1102 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1103}
1104
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001105void X86Mir2Lir::EmitOpRegOpcode(const X86EncodingMap* entry, int32_t raw_reg) {
1106 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001107 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, raw_reg);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001108 // There's no 3-byte instruction with +rd
1109 DCHECK(entry->skeleton.opcode != 0x0F ||
1110 (entry->skeleton.extra_opcode1 != 0x38 && entry->skeleton.extra_opcode1 != 0x3A));
1111 DCHECK(!RegStorage::IsFloat(raw_reg));
1112 uint8_t low_reg = LowRegisterBits(raw_reg);
1113 code_buffer_.back() += low_reg;
1114 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1115 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1116}
1117
1118void X86Mir2Lir::EmitOpReg(const X86EncodingMap* entry, int32_t raw_reg) {
1119 CheckValidByteRegister(entry, raw_reg);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001120 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, raw_reg);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001121 uint8_t low_reg = LowRegisterBits(raw_reg);
1122 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | low_reg;
Vladimir Marko057c74a2013-12-03 15:20:45 +00001123 code_buffer_.push_back(modrm);
1124 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::EmitOpMem(const X86EncodingMap* entry, int32_t raw_base, int32_t disp) {
1129 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001130 EmitPrefix(entry, NO_REG, NO_REG, raw_base);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001131 code_buffer_.push_back(entry->skeleton.opcode);
1132 DCHECK_NE(0x0F, entry->skeleton.opcode);
1133 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1134 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001135 uint8_t low_base = LowRegisterBits(raw_base);
1136 EmitModrmDisp(entry->skeleton.modrm_opcode, low_base, disp);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001137 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1138 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1139}
1140
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001141void X86Mir2Lir::EmitOpArray(const X86EncodingMap* entry, int32_t raw_base, int32_t raw_index,
1142 int scale, int32_t disp) {
1143 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001144 EmitPrefixAndOpcode(entry, NO_REG, raw_index, raw_base);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001145 uint8_t low_index = LowRegisterBits(raw_index);
1146 uint8_t low_base = LowRegisterBits(raw_base);
1147 EmitModrmSibDisp(entry->skeleton.modrm_opcode, low_base, low_index, scale, disp);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001148 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1149 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1150}
1151
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001152void X86Mir2Lir::EmitMemReg(const X86EncodingMap* entry, int32_t raw_base, int32_t disp,
1153 int32_t raw_reg) {
1154 CheckValidByteRegister(entry, raw_reg);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001155 EmitPrefixAndOpcode(entry, raw_reg, NO_REG, raw_base);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001156 uint8_t low_reg = LowRegisterBits(raw_reg);
Mark Mendell27dee8b2014-12-01 19:06:12 -05001157 uint8_t low_base = (raw_base == kRIPReg) ? raw_base : LowRegisterBits(raw_base);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001158 EmitModrmDisp(low_reg, low_base, disp);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001159 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1160 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1161 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1162}
1163
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001164void X86Mir2Lir::EmitRegMem(const X86EncodingMap* entry, int32_t raw_reg, int32_t raw_base,
1165 int32_t disp) {
Vladimir Marko057c74a2013-12-03 15:20:45 +00001166 // Opcode will flip operands.
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001167 EmitMemReg(entry, raw_base, disp, raw_reg);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001168}
1169
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001170void X86Mir2Lir::EmitRegArray(const X86EncodingMap* entry, int32_t raw_reg, int32_t raw_base,
1171 int32_t raw_index, int scale, int32_t disp) {
1172 CheckValidByteRegister(entry, raw_reg);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001173 EmitPrefixAndOpcode(entry, raw_reg, raw_index, raw_base);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001174 uint8_t low_reg = LowRegisterBits(raw_reg);
1175 uint8_t low_index = LowRegisterBits(raw_index);
1176 uint8_t low_base = LowRegisterBits(raw_base);
1177 EmitModrmSibDisp(low_reg, low_base, low_index, scale, disp);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001178 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1179 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1180 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1181}
1182
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001183void X86Mir2Lir::EmitArrayReg(const X86EncodingMap* entry, int32_t raw_base, int32_t raw_index,
1184 int scale, int32_t disp, int32_t raw_reg) {
Vladimir Marko057c74a2013-12-03 15:20:45 +00001185 // Opcode will flip operands.
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001186 EmitRegArray(entry, raw_reg, raw_base, raw_index, scale, disp);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001187}
1188
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001189void X86Mir2Lir::EmitMemImm(const X86EncodingMap* entry, int32_t raw_base, int32_t disp,
1190 int32_t imm) {
1191 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001192 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, raw_base);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001193 uint8_t low_base = LowRegisterBits(raw_base);
1194 EmitModrmDisp(entry->skeleton.modrm_opcode, low_base, disp);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001195 DCHECK_EQ(0, entry->skeleton.ax_opcode);
Mark Mendell9ed42772014-05-07 17:26:12 -04001196 EmitImm(entry, imm);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001197}
1198
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001199void X86Mir2Lir::EmitArrayImm(const X86EncodingMap* entry,
1200 int32_t raw_base, int32_t raw_index, int scale, int32_t disp,
1201 int32_t imm) {
1202 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001203 EmitPrefixAndOpcode(entry, NO_REG, raw_index, raw_base);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001204 uint8_t low_index = LowRegisterBits(raw_index);
1205 uint8_t low_base = LowRegisterBits(raw_base);
1206 EmitModrmSibDisp(entry->skeleton.modrm_opcode, low_base, low_index, scale, disp);
1207 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1208 EmitImm(entry, imm);
1209}
1210
1211void X86Mir2Lir::EmitRegThread(const X86EncodingMap* entry, int32_t raw_reg, int32_t disp) {
1212 DCHECK_EQ(false, entry->skeleton.r8_form);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001213 DCHECK_NE(entry->skeleton.prefix1, 0);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001214 EmitPrefixAndOpcode(entry, raw_reg, NO_REG, NO_REG);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001215 uint8_t low_reg = LowRegisterBits(raw_reg);
1216 EmitModrmThread(low_reg);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001217 code_buffer_.push_back(disp & 0xFF);
1218 code_buffer_.push_back((disp >> 8) & 0xFF);
1219 code_buffer_.push_back((disp >> 16) & 0xFF);
1220 code_buffer_.push_back((disp >> 24) & 0xFF);
1221 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1222 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1223 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1224}
1225
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001226void X86Mir2Lir::EmitRegReg(const X86EncodingMap* entry, int32_t raw_reg1, int32_t raw_reg2) {
Chao-ying Fu021b60f2014-07-09 11:32:31 -07001227 if (!IsByteSecondOperand(entry)) {
1228 CheckValidByteRegister(entry, raw_reg1);
1229 }
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001230 CheckValidByteRegister(entry, raw_reg2);
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 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1239}
1240
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001241void X86Mir2Lir::EmitRegRegImm(const X86EncodingMap* entry, int32_t raw_reg1, int32_t raw_reg2,
1242 int32_t imm) {
1243 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001244 EmitPrefixAndOpcode(entry, raw_reg1, NO_REG, raw_reg2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001245 uint8_t low_reg1 = LowRegisterBits(raw_reg1);
1246 uint8_t low_reg2 = LowRegisterBits(raw_reg2);
1247 uint8_t modrm = (3 << 6) | (low_reg1 << 3) | low_reg2;
Vladimir Marko057c74a2013-12-03 15:20:45 +00001248 code_buffer_.push_back(modrm);
1249 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1250 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1251 EmitImm(entry, imm);
1252}
1253
Mark Mendell4708dcd2014-01-22 09:05:18 -08001254void X86Mir2Lir::EmitRegMemImm(const X86EncodingMap* entry,
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001255 int32_t raw_reg, int32_t raw_base, int disp, int32_t imm) {
1256 DCHECK(!RegStorage::IsFloat(raw_reg));
1257 CheckValidByteRegister(entry, raw_reg);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001258 EmitPrefixAndOpcode(entry, raw_reg, NO_REG, raw_base);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001259 uint8_t low_reg = LowRegisterBits(raw_reg);
1260 uint8_t low_base = LowRegisterBits(raw_base);
1261 EmitModrmDisp(low_reg, low_base, disp);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001262 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1263 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1264 EmitImm(entry, imm);
1265}
1266
Mark Mendell2637f2e2014-04-30 10:10:47 -04001267void X86Mir2Lir::EmitMemRegImm(const X86EncodingMap* entry,
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001268 int32_t raw_base, int32_t disp, int32_t raw_reg, int32_t imm) {
1269 // Opcode will flip operands.
1270 EmitRegMemImm(entry, raw_reg, raw_base, disp, imm);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001271}
1272
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001273void X86Mir2Lir::EmitRegImm(const X86EncodingMap* entry, int32_t raw_reg, int32_t imm) {
1274 CheckValidByteRegister(entry, raw_reg);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001275 EmitPrefix(entry, NO_REG, NO_REG, raw_reg);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001276 if (RegStorage::RegNum(raw_reg) == rs_rAX.GetRegNum() && entry->skeleton.ax_opcode != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001277 code_buffer_.push_back(entry->skeleton.ax_opcode);
1278 } else {
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001279 uint8_t low_reg = LowRegisterBits(raw_reg);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001280 EmitOpcode(entry);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001281 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | low_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001282 code_buffer_.push_back(modrm);
1283 }
Vladimir Marko057c74a2013-12-03 15:20:45 +00001284 EmitImm(entry, imm);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001285}
1286
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001287void X86Mir2Lir::EmitThreadImm(const X86EncodingMap* entry, int32_t disp, int32_t imm) {
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07001288 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001289 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, NO_REG);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001290 EmitModrmThread(entry->skeleton.modrm_opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001291 code_buffer_.push_back(disp & 0xFF);
1292 code_buffer_.push_back((disp >> 8) & 0xFF);
1293 code_buffer_.push_back((disp >> 16) & 0xFF);
1294 code_buffer_.push_back((disp >> 24) & 0xFF);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001295 EmitImm(entry, imm);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001296 DCHECK_EQ(entry->skeleton.ax_opcode, 0);
1297}
1298
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001299void X86Mir2Lir::EmitMovRegImm(const X86EncodingMap* entry, int32_t raw_reg, int64_t imm) {
1300 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001301 EmitPrefix(entry, NO_REG, NO_REG, raw_reg);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001302 uint8_t low_reg = LowRegisterBits(raw_reg);
1303 code_buffer_.push_back(0xB8 + low_reg);
Dmitry Petrochenko96992e82014-05-20 04:03:46 +07001304 switch (entry->skeleton.immediate_bytes) {
1305 case 4:
1306 code_buffer_.push_back(imm & 0xFF);
1307 code_buffer_.push_back((imm >> 8) & 0xFF);
1308 code_buffer_.push_back((imm >> 16) & 0xFF);
1309 code_buffer_.push_back((imm >> 24) & 0xFF);
1310 break;
1311 case 8:
1312 code_buffer_.push_back(imm & 0xFF);
1313 code_buffer_.push_back((imm >> 8) & 0xFF);
1314 code_buffer_.push_back((imm >> 16) & 0xFF);
1315 code_buffer_.push_back((imm >> 24) & 0xFF);
1316 code_buffer_.push_back((imm >> 32) & 0xFF);
1317 code_buffer_.push_back((imm >> 40) & 0xFF);
1318 code_buffer_.push_back((imm >> 48) & 0xFF);
1319 code_buffer_.push_back((imm >> 56) & 0xFF);
1320 break;
1321 default:
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001322 LOG(FATAL) << "Unsupported immediate size for EmitMovRegImm: "
1323 << static_cast<uint32_t>(entry->skeleton.immediate_bytes);
Dmitry Petrochenko96992e82014-05-20 04:03:46 +07001324 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001325}
1326
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001327void X86Mir2Lir::EmitShiftRegImm(const X86EncodingMap* entry, int32_t raw_reg, int32_t imm) {
1328 CheckValidByteRegister(entry, raw_reg);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001329 EmitPrefix(entry, NO_REG, NO_REG, raw_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001330 if (imm != 1) {
1331 code_buffer_.push_back(entry->skeleton.opcode);
1332 } else {
1333 // Shorter encoding for 1 bit shift
1334 code_buffer_.push_back(entry->skeleton.ax_opcode);
1335 }
Vladimir Marko057c74a2013-12-03 15:20:45 +00001336 DCHECK_NE(0x0F, entry->skeleton.opcode);
1337 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1338 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001339 uint8_t low_reg = LowRegisterBits(raw_reg);
1340 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | low_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001341 code_buffer_.push_back(modrm);
1342 if (imm != 1) {
1343 DCHECK_EQ(entry->skeleton.immediate_bytes, 1);
1344 DCHECK(IS_SIMM8(imm));
1345 code_buffer_.push_back(imm & 0xFF);
1346 }
1347}
1348
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001349void X86Mir2Lir::EmitShiftRegCl(const X86EncodingMap* entry, int32_t raw_reg, int32_t raw_cl) {
1350 CheckValidByteRegister(entry, raw_reg);
1351 DCHECK_EQ(rs_rCX.GetRegNum(), RegStorage::RegNum(raw_cl));
Ian Rogers5aa6e042014-06-13 16:38:24 -07001352 EmitPrefix(entry, NO_REG, NO_REG, raw_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001353 code_buffer_.push_back(entry->skeleton.opcode);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001354 DCHECK_NE(0x0F, entry->skeleton.opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001355 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1356 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001357 uint8_t low_reg = LowRegisterBits(raw_reg);
1358 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | low_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001359 code_buffer_.push_back(modrm);
1360 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1361 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1362}
1363
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001364void X86Mir2Lir::EmitShiftMemCl(const X86EncodingMap* entry, int32_t raw_base,
1365 int32_t displacement, int32_t raw_cl) {
1366 DCHECK_EQ(false, entry->skeleton.r8_form);
1367 DCHECK_EQ(rs_rCX.GetRegNum(), RegStorage::RegNum(raw_cl));
Ian Rogers5aa6e042014-06-13 16:38:24 -07001368 EmitPrefix(entry, NO_REG, NO_REG, raw_base);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001369 code_buffer_.push_back(entry->skeleton.opcode);
1370 DCHECK_NE(0x0F, entry->skeleton.opcode);
1371 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1372 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001373 uint8_t low_base = LowRegisterBits(raw_base);
1374 EmitModrmDisp(entry->skeleton.modrm_opcode, low_base, displacement);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001375 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1376 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1377}
1378
Yixin Shouf40f8902014-08-14 14:10:32 -04001379void X86Mir2Lir::EmitShiftRegRegCl(const X86EncodingMap* entry, int32_t raw_reg1, int32_t raw_reg2, int32_t raw_cl) {
1380 DCHECK_EQ(false, entry->skeleton.r8_form);
1381 DCHECK_EQ(rs_rCX.GetRegNum(), RegStorage::RegNum(raw_cl));
1382 EmitPrefixAndOpcode(entry, raw_reg1, NO_REG, raw_reg2);
1383 uint8_t low_reg1 = LowRegisterBits(raw_reg1);
1384 uint8_t low_reg2 = LowRegisterBits(raw_reg2);
1385 uint8_t modrm = (3 << 6) | (low_reg1 << 3) | low_reg2;
1386 code_buffer_.push_back(modrm);
1387 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1388 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1389 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1390}
1391
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001392void X86Mir2Lir::EmitShiftMemImm(const X86EncodingMap* entry, int32_t raw_base, int32_t disp,
1393 int32_t imm) {
1394 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001395 EmitPrefix(entry, NO_REG, NO_REG, raw_base);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001396 if (imm != 1) {
1397 code_buffer_.push_back(entry->skeleton.opcode);
1398 } else {
1399 // Shorter encoding for 1 bit shift
1400 code_buffer_.push_back(entry->skeleton.ax_opcode);
1401 }
1402 DCHECK_NE(0x0F, entry->skeleton.opcode);
1403 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1404 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001405 uint8_t low_base = LowRegisterBits(raw_base);
1406 EmitModrmDisp(entry->skeleton.modrm_opcode, low_base, disp);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001407 if (imm != 1) {
1408 DCHECK_EQ(entry->skeleton.immediate_bytes, 1);
1409 DCHECK(IS_SIMM8(imm));
1410 code_buffer_.push_back(imm & 0xFF);
1411 }
1412}
1413
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001414void X86Mir2Lir::EmitRegCond(const X86EncodingMap* entry, int32_t raw_reg, int32_t cc) {
1415 CheckValidByteRegister(entry, raw_reg);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001416 EmitPrefix(entry, NO_REG, NO_REG, raw_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001417 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1418 DCHECK_EQ(0x0F, entry->skeleton.opcode);
1419 code_buffer_.push_back(0x0F);
1420 DCHECK_EQ(0x90, entry->skeleton.extra_opcode1);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001421 DCHECK_GE(cc, 0);
1422 DCHECK_LT(cc, 16);
1423 code_buffer_.push_back(0x90 | cc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001424 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001425 uint8_t low_reg = LowRegisterBits(raw_reg);
1426 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | low_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001427 code_buffer_.push_back(modrm);
1428 DCHECK_EQ(entry->skeleton.immediate_bytes, 0);
1429}
1430
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001431void X86Mir2Lir::EmitMemCond(const X86EncodingMap* entry, int32_t raw_base, int32_t disp,
1432 int32_t cc) {
1433 DCHECK_EQ(false, entry->skeleton.r8_form);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001434 if (entry->skeleton.prefix1 != 0) {
1435 code_buffer_.push_back(entry->skeleton.prefix1);
1436 if (entry->skeleton.prefix2 != 0) {
1437 code_buffer_.push_back(entry->skeleton.prefix2);
1438 }
1439 } else {
1440 DCHECK_EQ(0, entry->skeleton.prefix2);
1441 }
1442 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1443 DCHECK_EQ(0x0F, entry->skeleton.opcode);
1444 code_buffer_.push_back(0x0F);
1445 DCHECK_EQ(0x90, entry->skeleton.extra_opcode1);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001446 DCHECK_GE(cc, 0);
1447 DCHECK_LT(cc, 16);
1448 code_buffer_.push_back(0x90 | cc);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001449 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001450 uint8_t low_base = LowRegisterBits(raw_base);
1451 EmitModrmDisp(entry->skeleton.modrm_opcode, low_base, disp);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001452 DCHECK_EQ(entry->skeleton.immediate_bytes, 0);
1453}
1454
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001455void X86Mir2Lir::EmitRegRegCond(const X86EncodingMap* entry, int32_t raw_reg1, int32_t raw_reg2,
1456 int32_t cc) {
1457 // Generate prefix and opcode without the condition.
1458 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001459 EmitPrefixAndOpcode(entry, raw_reg1, NO_REG, raw_reg2);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001460
1461 // Now add the condition. The last byte of opcode is the one that receives it.
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001462 DCHECK_GE(cc, 0);
1463 DCHECK_LT(cc, 16);
1464 code_buffer_.back() += cc;
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001465
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001466 // Not expecting to have to encode immediate or do anything special for ModR/M since there are
1467 // two registers.
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001468 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1469 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1470
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001471 // For register to register encoding, the mod is 3.
1472 const uint8_t mod = (3 << 6);
1473
1474 // Encode the ModR/M byte now.
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001475 uint8_t low_reg1 = LowRegisterBits(raw_reg1);
1476 uint8_t low_reg2 = LowRegisterBits(raw_reg2);
1477 const uint8_t modrm = mod | (low_reg1 << 3) | low_reg2;
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001478 code_buffer_.push_back(modrm);
1479}
1480
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001481void X86Mir2Lir::EmitRegMemCond(const X86EncodingMap* entry, int32_t raw_reg1, int32_t raw_base,
1482 int32_t disp, int32_t cc) {
1483 // Generate prefix and opcode without the condition.
1484 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001485 EmitPrefixAndOpcode(entry, raw_reg1, NO_REG, raw_base);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001486
1487 // Now add the condition. The last byte of opcode is the one that receives it.
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001488 DCHECK_GE(cc, 0);
1489 DCHECK_LT(cc, 16);
1490 code_buffer_.back() += cc;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001491
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001492 // Not expecting to have to encode immediate or do anything special for ModR/M since there are
1493 // two registers.
Mark Mendell2637f2e2014-04-30 10:10:47 -04001494 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1495 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1496
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001497 uint8_t low_reg1 = LowRegisterBits(raw_reg1);
1498 uint8_t low_base = LowRegisterBits(raw_base);
1499 EmitModrmDisp(low_reg1, low_base, disp);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001500}
1501
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001502void X86Mir2Lir::EmitJmp(const X86EncodingMap* entry, int32_t rel) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001503 if (entry->opcode == kX86Jmp8) {
1504 DCHECK(IS_SIMM8(rel));
1505 code_buffer_.push_back(0xEB);
1506 code_buffer_.push_back(rel & 0xFF);
1507 } else if (entry->opcode == kX86Jmp32) {
1508 code_buffer_.push_back(0xE9);
1509 code_buffer_.push_back(rel & 0xFF);
1510 code_buffer_.push_back((rel >> 8) & 0xFF);
1511 code_buffer_.push_back((rel >> 16) & 0xFF);
1512 code_buffer_.push_back((rel >> 24) & 0xFF);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001513 } else if (entry->opcode == kX86Jecxz8) {
1514 DCHECK(IS_SIMM8(rel));
1515 code_buffer_.push_back(0xE3);
1516 code_buffer_.push_back(rel & 0xFF);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001517 } else {
1518 DCHECK(entry->opcode == kX86JmpR);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001519 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001520 EmitPrefix(entry, NO_REG, NO_REG, rel);
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001521 code_buffer_.push_back(entry->skeleton.opcode);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001522 uint8_t low_reg = LowRegisterBits(rel);
1523 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | low_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001524 code_buffer_.push_back(modrm);
1525 }
1526}
1527
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001528void X86Mir2Lir::EmitJcc(const X86EncodingMap* entry, int32_t rel, int32_t cc) {
1529 DCHECK_GE(cc, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001530 DCHECK_LT(cc, 16);
1531 if (entry->opcode == kX86Jcc8) {
1532 DCHECK(IS_SIMM8(rel));
1533 code_buffer_.push_back(0x70 | cc);
1534 code_buffer_.push_back(rel & 0xFF);
1535 } else {
1536 DCHECK(entry->opcode == kX86Jcc32);
1537 code_buffer_.push_back(0x0F);
1538 code_buffer_.push_back(0x80 | cc);
1539 code_buffer_.push_back(rel & 0xFF);
1540 code_buffer_.push_back((rel >> 8) & 0xFF);
1541 code_buffer_.push_back((rel >> 16) & 0xFF);
1542 code_buffer_.push_back((rel >> 24) & 0xFF);
1543 }
1544}
1545
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001546void X86Mir2Lir::EmitCallMem(const X86EncodingMap* entry, int32_t raw_base, int32_t disp) {
1547 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001548 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, raw_base);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001549 uint8_t low_base = LowRegisterBits(raw_base);
1550 EmitModrmDisp(entry->skeleton.modrm_opcode, low_base, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001551 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1552 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1553}
1554
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001555void X86Mir2Lir::EmitCallImmediate(const X86EncodingMap* entry, int32_t disp) {
1556 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001557 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, NO_REG);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001558 DCHECK_EQ(4, entry->skeleton.immediate_bytes);
1559 code_buffer_.push_back(disp & 0xFF);
1560 code_buffer_.push_back((disp >> 8) & 0xFF);
1561 code_buffer_.push_back((disp >> 16) & 0xFF);
1562 code_buffer_.push_back((disp >> 24) & 0xFF);
1563 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1564}
1565
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001566void X86Mir2Lir::EmitCallThread(const X86EncodingMap* entry, int32_t disp) {
1567 DCHECK_EQ(false, entry->skeleton.r8_form);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001568 DCHECK_NE(entry->skeleton.prefix1, 0);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001569 EmitPrefixAndOpcode(entry, NO_REG, NO_REG, NO_REG);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07001570 EmitModrmThread(entry->skeleton.modrm_opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001571 code_buffer_.push_back(disp & 0xFF);
1572 code_buffer_.push_back((disp >> 8) & 0xFF);
1573 code_buffer_.push_back((disp >> 16) & 0xFF);
1574 code_buffer_.push_back((disp >> 24) & 0xFF);
1575 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1576 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1577}
1578
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001579void X86Mir2Lir::EmitPcRel(const X86EncodingMap* entry, int32_t raw_reg, int32_t raw_base_or_table,
1580 int32_t raw_index, int scale, int32_t table_or_disp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001581 int disp;
1582 if (entry->opcode == kX86PcRelLoadRA) {
Vladimir Marko1961b602015-04-08 20:51:48 +01001583 const SwitchTable* tab_rec = UnwrapPointer<SwitchTable>(table_or_disp);
1584 disp = tab_rec->offset - tab_rec->anchor->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001585 } else {
1586 DCHECK(entry->opcode == kX86PcRelAdr);
Vladimir Markof6737f72015-03-23 17:05:14 +00001587 const EmbeddedData* tab_rec = UnwrapPointer<EmbeddedData>(raw_base_or_table);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001588 disp = tab_rec->offset;
1589 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001590 if (entry->opcode == kX86PcRelLoadRA) {
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001591 DCHECK_EQ(false, entry->skeleton.r8_form);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001592 EmitPrefix(entry, raw_reg, raw_index, raw_base_or_table);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001593 code_buffer_.push_back(entry->skeleton.opcode);
Vladimir Marko057c74a2013-12-03 15:20:45 +00001594 DCHECK_NE(0x0F, entry->skeleton.opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001595 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1596 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001597 uint8_t low_reg = LowRegisterBits(raw_reg);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001598 uint8_t modrm = (2 << 6) | (low_reg << 3) | rs_rX86_SP_32.GetRegNum();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001599 code_buffer_.push_back(modrm);
1600 DCHECK_LT(scale, 4);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001601 uint8_t low_base_or_table = LowRegisterBits(raw_base_or_table);
1602 uint8_t low_index = LowRegisterBits(raw_index);
1603 uint8_t sib = (scale << 6) | (low_index << 3) | low_base_or_table;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001604 code_buffer_.push_back(sib);
1605 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1606 } else {
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001607 uint8_t low_reg = LowRegisterBits(raw_reg);
1608 code_buffer_.push_back(entry->skeleton.opcode + low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001609 }
1610 code_buffer_.push_back(disp & 0xFF);
1611 code_buffer_.push_back((disp >> 8) & 0xFF);
1612 code_buffer_.push_back((disp >> 16) & 0xFF);
1613 code_buffer_.push_back((disp >> 24) & 0xFF);
1614 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1615 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1616}
1617
Brian Carlstrom7940e442013-07-12 13:46:57 -07001618void X86Mir2Lir::EmitUnimplemented(const X86EncodingMap* entry, LIR* lir) {
1619 UNIMPLEMENTED(WARNING) << "encoding kind for " << entry->name << " "
1620 << BuildInsnString(entry->fmt, lir, 0);
Ian Rogers5aa6e042014-06-13 16:38:24 -07001621 for (size_t i = 0; i < GetInsnSize(lir); ++i) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001622 code_buffer_.push_back(0xCC); // push breakpoint instruction - int 3
1623 }
1624}
1625
1626/*
1627 * Assemble the LIR into binary instruction format. Note that we may
1628 * discover that pc-relative displacements may not fit the selected
1629 * instruction. In those cases we will try to substitute a new code
1630 * sequence or request that the trace be shortened and retried.
1631 */
Chao-ying Fuc4013ea2015-04-22 10:51:21 -07001632AssemblerStatus X86Mir2Lir::AssembleInstructions(LIR* first_lir_insn, CodeOffset start_addr) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001633 UNUSED(start_addr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001634 LIR *lir;
1635 AssemblerStatus res = kSuccess; // Assume success
1636
1637 const bool kVerbosePcFixup = false;
Chao-ying Fuc4013ea2015-04-22 10:51:21 -07001638 for (lir = first_lir_insn; lir != nullptr; lir = NEXT_LIR(lir)) {
buzbee409fe942013-10-11 10:49:56 -07001639 if (IsPseudoLirOp(lir->opcode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001640 continue;
1641 }
1642
1643 if (lir->flags.is_nop) {
1644 continue;
1645 }
1646
buzbeeb48819d2013-09-14 16:15:25 -07001647 if (lir->flags.fixup != kFixupNone) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001648 switch (lir->opcode) {
1649 case kX86Jcc8: {
1650 LIR *target_lir = lir->target;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001651 DCHECK(target_lir != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001652 int delta = 0;
buzbee0d829482013-10-11 15:24:55 -07001653 CodeOffset pc;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001654 if (IS_SIMM8(lir->operands[0])) {
1655 pc = lir->offset + 2 /* opcode + rel8 */;
1656 } else {
1657 pc = lir->offset + 6 /* 2 byte opcode + rel32 */;
1658 }
buzbee0d829482013-10-11 15:24:55 -07001659 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001660 delta = target - pc;
1661 if (IS_SIMM8(delta) != IS_SIMM8(lir->operands[0])) {
1662 if (kVerbosePcFixup) {
1663 LOG(INFO) << "Retry for JCC growth at " << lir->offset
1664 << " delta: " << delta << " old delta: " << lir->operands[0];
1665 }
1666 lir->opcode = kX86Jcc32;
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001667 lir->flags.size = GetInsnSize(lir);
1668 DCHECK(lir->u.m.def_mask->Equals(kEncodeAll));
1669 DCHECK(lir->u.m.use_mask->Equals(kEncodeAll));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001670 res = kRetryAll;
1671 }
1672 if (kVerbosePcFixup) {
1673 LOG(INFO) << "Source:";
1674 DumpLIRInsn(lir, 0);
1675 LOG(INFO) << "Target:";
1676 DumpLIRInsn(target_lir, 0);
1677 LOG(INFO) << "Delta " << delta;
1678 }
1679 lir->operands[0] = delta;
1680 break;
1681 }
1682 case kX86Jcc32: {
1683 LIR *target_lir = lir->target;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001684 DCHECK(target_lir != nullptr);
buzbee0d829482013-10-11 15:24:55 -07001685 CodeOffset pc = lir->offset + 6 /* 2 byte opcode + rel32 */;
1686 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001687 int delta = target - pc;
1688 if (kVerbosePcFixup) {
1689 LOG(INFO) << "Source:";
1690 DumpLIRInsn(lir, 0);
1691 LOG(INFO) << "Target:";
1692 DumpLIRInsn(target_lir, 0);
1693 LOG(INFO) << "Delta " << delta;
1694 }
1695 lir->operands[0] = delta;
1696 break;
1697 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001698 case kX86Jecxz8: {
1699 LIR *target_lir = lir->target;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001700 DCHECK(target_lir != nullptr);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001701 CodeOffset pc;
1702 pc = lir->offset + 2; // opcode + rel8
1703 CodeOffset target = target_lir->offset;
1704 int delta = target - pc;
1705 lir->operands[0] = delta;
1706 DCHECK(IS_SIMM8(delta));
1707 break;
1708 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001709 case kX86Jmp8: {
1710 LIR *target_lir = lir->target;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001711 DCHECK(target_lir != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001712 int delta = 0;
buzbee0d829482013-10-11 15:24:55 -07001713 CodeOffset pc;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001714 if (IS_SIMM8(lir->operands[0])) {
1715 pc = lir->offset + 2 /* opcode + rel8 */;
1716 } else {
1717 pc = lir->offset + 5 /* opcode + rel32 */;
1718 }
buzbee0d829482013-10-11 15:24:55 -07001719 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001720 delta = target - pc;
1721 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && delta == 0) {
1722 // Useless branch
buzbee252254b2013-09-08 16:20:53 -07001723 NopLIR(lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001724 if (kVerbosePcFixup) {
1725 LOG(INFO) << "Retry for useless branch at " << lir->offset;
1726 }
1727 res = kRetryAll;
1728 } else if (IS_SIMM8(delta) != IS_SIMM8(lir->operands[0])) {
1729 if (kVerbosePcFixup) {
1730 LOG(INFO) << "Retry for JMP growth at " << lir->offset;
1731 }
1732 lir->opcode = kX86Jmp32;
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001733 lir->flags.size = GetInsnSize(lir);
1734 DCHECK(lir->u.m.def_mask->Equals(kEncodeAll));
1735 DCHECK(lir->u.m.use_mask->Equals(kEncodeAll));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001736 res = kRetryAll;
1737 }
1738 lir->operands[0] = delta;
1739 break;
1740 }
1741 case kX86Jmp32: {
1742 LIR *target_lir = lir->target;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001743 DCHECK(target_lir != nullptr);
buzbee0d829482013-10-11 15:24:55 -07001744 CodeOffset pc = lir->offset + 5 /* opcode + rel32 */;
1745 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001746 int delta = target - pc;
1747 lir->operands[0] = delta;
1748 break;
1749 }
1750 default:
Mark Mendell67c39c42014-01-31 17:28:00 -08001751 if (lir->flags.fixup == kFixupLoad) {
1752 LIR *target_lir = lir->target;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001753 DCHECK(target_lir != nullptr);
Mark Mendell67c39c42014-01-31 17:28:00 -08001754 CodeOffset target = target_lir->offset;
Mark Mendell27dee8b2014-12-01 19:06:12 -05001755 // Handle 64 bit RIP addressing.
1756 if (lir->operands[1] == kRIPReg) {
1757 // Offset is relative to next instruction.
1758 lir->operands[2] = target - (lir->offset + lir->flags.size);
1759 } else {
Vladimir Marko1961b602015-04-08 20:51:48 +01001760 const LIR* anchor = UnwrapPointer<LIR>(lir->operands[4]);
1761 lir->operands[2] = target - anchor->offset;
Mark Mendell27dee8b2014-12-01 19:06:12 -05001762 int newSize = GetInsnSize(lir);
1763 if (newSize != lir->flags.size) {
1764 lir->flags.size = newSize;
1765 res = kRetryAll;
1766 }
Mark Mendell67c39c42014-01-31 17:28:00 -08001767 }
Mark Mendell27dee8b2014-12-01 19:06:12 -05001768 } else if (lir->flags.fixup == kFixupSwitchTable) {
1769 DCHECK(cu_->target64);
1770 DCHECK_EQ(lir->opcode, kX86Lea64RM) << "Unknown instruction: " << X86Mir2Lir::EncodingMap[lir->opcode].name;
1771 DCHECK_EQ(lir->operands[1], static_cast<int>(kRIPReg));
1772 // Grab the target offset from the saved data.
Vladimir Markof6737f72015-03-23 17:05:14 +00001773 const EmbeddedData* tab_rec = UnwrapPointer<Mir2Lir::EmbeddedData>(lir->operands[4]);
Mark Mendell27dee8b2014-12-01 19:06:12 -05001774 CodeOffset target = tab_rec->offset;
1775 // Handle 64 bit RIP addressing.
1776 // Offset is relative to next instruction.
1777 lir->operands[2] = target - (lir->offset + lir->flags.size);
Mark Mendell67c39c42014-01-31 17:28:00 -08001778 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001779 break;
1780 }
1781 }
1782
1783 /*
1784 * If one of the pc-relative instructions expanded we'll have
1785 * to make another pass. Don't bother to fully assemble the
1786 * instruction.
1787 */
1788 if (res != kSuccess) {
1789 continue;
1790 }
1791 CHECK_EQ(static_cast<size_t>(lir->offset), code_buffer_.size());
1792 const X86EncodingMap *entry = &X86Mir2Lir::EncodingMap[lir->opcode];
1793 size_t starting_cbuf_size = code_buffer_.size();
1794 switch (entry->kind) {
1795 case kData: // 4 bytes of data
1796 code_buffer_.push_back(lir->operands[0]);
1797 break;
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001798 case kNullary: // 1 byte of opcode and possible prefixes.
1799 EmitNullary(entry);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001800 break;
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001801 case kRegOpcode: // lir operands - 0: reg
1802 EmitOpRegOpcode(entry, lir->operands[0]);
1803 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001804 case kReg: // lir operands - 0: reg
1805 EmitOpReg(entry, lir->operands[0]);
1806 break;
1807 case kMem: // lir operands - 0: base, 1: disp
1808 EmitOpMem(entry, lir->operands[0], lir->operands[1]);
1809 break;
Vladimir Marko057c74a2013-12-03 15:20:45 +00001810 case kArray: // lir operands - 0: base, 1: index, 2: scale, 3: disp
1811 EmitOpArray(entry, lir->operands[0], lir->operands[1], lir->operands[2], lir->operands[3]);
1812 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001813 case kMemReg: // lir operands - 0: base, 1: disp, 2: reg
1814 EmitMemReg(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1815 break;
Mark Mendell343adb52013-12-18 06:02:17 -08001816 case kMemImm: // lir operands - 0: base, 1: disp, 2: immediate
1817 EmitMemImm(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1818 break;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001819 case kArrayImm: // lir operands - 0: base, 1: index, 2: disp, 3:scale, 4:immediate
1820 EmitArrayImm(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1821 lir->operands[3], lir->operands[4]);
1822 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001823 case kArrayReg: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
1824 EmitArrayReg(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1825 lir->operands[3], lir->operands[4]);
1826 break;
1827 case kRegMem: // lir operands - 0: reg, 1: base, 2: disp
1828 EmitRegMem(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1829 break;
1830 case kRegArray: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
1831 EmitRegArray(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1832 lir->operands[3], lir->operands[4]);
1833 break;
1834 case kRegThread: // lir operands - 0: reg, 1: disp
1835 EmitRegThread(entry, lir->operands[0], lir->operands[1]);
1836 break;
1837 case kRegReg: // lir operands - 0: reg1, 1: reg2
1838 EmitRegReg(entry, lir->operands[0], lir->operands[1]);
1839 break;
1840 case kRegRegStore: // lir operands - 0: reg2, 1: reg1
1841 EmitRegReg(entry, lir->operands[1], lir->operands[0]);
1842 break;
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001843 case kMemRegImm: // lir operands - 0: base, 1: disp, 2: reg 3: immediate
Mark Mendell2637f2e2014-04-30 10:10:47 -04001844 EmitMemRegImm(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1845 lir->operands[3]);
1846 break;
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001847 case kRegRegImm: // lir operands - 0: reg1, 1: reg2, 2: imm
Brian Carlstrom7940e442013-07-12 13:46:57 -07001848 EmitRegRegImm(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1849 break;
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001850 case kRegRegImmStore: // lir operands - 0: reg2, 1: reg1, 2: imm
1851 EmitRegRegImm(entry, lir->operands[1], lir->operands[0], lir->operands[2]);
1852 break;
1853 case kRegMemImm: // lir operands - 0: reg, 1: base, 2: disp, 3: imm
Mark Mendell4708dcd2014-01-22 09:05:18 -08001854 EmitRegMemImm(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1855 lir->operands[3]);
1856 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001857 case kRegImm: // lir operands - 0: reg, 1: immediate
1858 EmitRegImm(entry, lir->operands[0], lir->operands[1]);
1859 break;
1860 case kThreadImm: // lir operands - 0: disp, 1: immediate
1861 EmitThreadImm(entry, lir->operands[0], lir->operands[1]);
1862 break;
1863 case kMovRegImm: // lir operands - 0: reg, 1: immediate
1864 EmitMovRegImm(entry, lir->operands[0], lir->operands[1]);
1865 break;
Yixin Shou5192cbb2014-07-01 13:48:17 -04001866 case kMovRegQuadImm: {
1867 int64_t value = static_cast<int64_t>(static_cast<int64_t>(lir->operands[1]) << 32 |
1868 static_cast<uint32_t>(lir->operands[2]));
1869 EmitMovRegImm(entry, lir->operands[0], value);
1870 }
1871 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001872 case kShiftRegImm: // lir operands - 0: reg, 1: immediate
1873 EmitShiftRegImm(entry, lir->operands[0], lir->operands[1]);
1874 break;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001875 case kShiftMemImm: // lir operands - 0: base, 1: disp, 2:immediate
1876 EmitShiftMemImm(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1877 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001878 case kShiftRegCl: // lir operands - 0: reg, 1: cl
Brian Carlstrom7940e442013-07-12 13:46:57 -07001879 EmitShiftRegCl(entry, lir->operands[0], lir->operands[1]);
1880 break;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001881 case kShiftMemCl: // lir operands - 0: base, 1:displacement, 2: cl
1882 EmitShiftMemCl(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1883 break;
Yixin Shouf40f8902014-08-14 14:10:32 -04001884 case kShiftRegRegCl: // lir operands - 0: reg1, 1: reg2, 2: cl
1885 EmitShiftRegRegCl(entry, lir->operands[1], lir->operands[0], lir->operands[2]);
1886 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001887 case kRegCond: // lir operands - 0: reg, 1: condition
1888 EmitRegCond(entry, lir->operands[0], lir->operands[1]);
1889 break;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001890 case kMemCond: // lir operands - 0: base, 1: displacement, 2: condition
1891 EmitMemCond(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1892 break;
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001893 case kRegRegCond: // lir operands - 0: reg, 1: reg, 2: condition
1894 EmitRegRegCond(entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1895 break;
Mark Mendell2637f2e2014-04-30 10:10:47 -04001896 case kRegMemCond: // lir operands - 0: reg, 1: reg, displacement, 3: condition
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +07001897 EmitRegMemCond(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1898 lir->operands[3]);
Mark Mendell2637f2e2014-04-30 10:10:47 -04001899 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001900 case kJmp: // lir operands - 0: rel
Brian Carlstrom60d7a652014-03-13 18:10:08 -07001901 if (entry->opcode == kX86JmpT) {
1902 // This works since the instruction format for jmp and call is basically the same and
1903 // EmitCallThread loads opcode info.
1904 EmitCallThread(entry, lir->operands[0]);
1905 } else {
1906 EmitJmp(entry, lir->operands[0]);
1907 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001908 break;
1909 case kJcc: // lir operands - 0: rel, 1: CC, target assigned
1910 EmitJcc(entry, lir->operands[0], lir->operands[1]);
1911 break;
1912 case kCall:
1913 switch (entry->opcode) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001914 case kX86CallI: // lir operands - 0: disp
1915 EmitCallImmediate(entry, lir->operands[0]);
1916 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001917 case kX86CallM: // lir operands - 0: base, 1: disp
1918 EmitCallMem(entry, lir->operands[0], lir->operands[1]);
1919 break;
1920 case kX86CallT: // lir operands - 0: disp
1921 EmitCallThread(entry, lir->operands[0]);
1922 break;
1923 default:
1924 EmitUnimplemented(entry, lir);
1925 break;
1926 }
1927 break;
1928 case kPcRel: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
1929 EmitPcRel(entry, lir->operands[0], lir->operands[1], lir->operands[2],
1930 lir->operands[3], lir->operands[4]);
1931 break;
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001932 case kNop: // TODO: these instruction kinds are missing implementations.
1933 case kThreadReg:
1934 case kRegArrayImm:
1935 case kShiftArrayImm:
1936 case kShiftArrayCl:
1937 case kArrayCond:
1938 case kUnimplemented:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001939 EmitUnimplemented(entry, lir);
1940 break;
1941 }
Ian Rogers5aa6e042014-06-13 16:38:24 -07001942 DCHECK_EQ(lir->flags.size, GetInsnSize(lir));
1943 CHECK_EQ(lir->flags.size, code_buffer_.size() - starting_cbuf_size)
Brian Carlstrom7940e442013-07-12 13:46:57 -07001944 << "Instruction size mismatch for entry: " << X86Mir2Lir::EncodingMap[lir->opcode].name;
1945 }
1946 return res;
1947}
1948
buzbeeb48819d2013-09-14 16:15:25 -07001949// LIR offset assignment.
1950// TODO: consolidate w/ Arm assembly mechanism.
1951int X86Mir2Lir::AssignInsnOffsets() {
1952 LIR* lir;
1953 int offset = 0;
1954
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001955 for (lir = first_lir_insn_; lir != nullptr; lir = NEXT_LIR(lir)) {
buzbeeb48819d2013-09-14 16:15:25 -07001956 lir->offset = offset;
buzbee409fe942013-10-11 10:49:56 -07001957 if (LIKELY(!IsPseudoLirOp(lir->opcode))) {
buzbeeb48819d2013-09-14 16:15:25 -07001958 if (!lir->flags.is_nop) {
1959 offset += lir->flags.size;
1960 }
1961 } else if (UNLIKELY(lir->opcode == kPseudoPseudoAlign4)) {
1962 if (offset & 0x2) {
1963 offset += 2;
1964 lir->operands[0] = 1;
1965 } else {
1966 lir->operands[0] = 0;
1967 }
1968 }
1969 /* Pseudo opcodes don't consume space */
1970 }
1971 return offset;
1972}
1973
1974/*
1975 * Walk the compilation unit and assign offsets to instructions
1976 * and literals and compute the total size of the compiled unit.
1977 * TODO: consolidate w/ Arm assembly mechanism.
1978 */
1979void X86Mir2Lir::AssignOffsets() {
1980 int offset = AssignInsnOffsets();
1981
Mark Mendelld65c51a2014-04-29 16:55:20 -04001982 if (const_vectors_ != nullptr) {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001983 // Vector literals must be 16-byte aligned. The header that is placed
1984 // in the code section causes misalignment so we take it into account.
1985 // Otherwise, we are sure that for x86 method is aligned to 16.
1986 DCHECK_EQ(GetInstructionSetAlignment(cu_->instruction_set), 16u);
1987 uint32_t bytes_to_fill = (0x10 - ((offset + sizeof(OatQuickMethodHeader)) & 0xF)) & 0xF;
1988 offset += bytes_to_fill;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001989
1990 // Now assign each literal the right offset.
1991 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
1992 p->offset = offset;
1993 offset += 16;
1994 }
1995 }
1996
buzbeeb48819d2013-09-14 16:15:25 -07001997 /* Const values have to be word aligned */
Andreas Gampe66018822014-05-05 20:47:19 -07001998 offset = RoundUp(offset, 4);
buzbeeb48819d2013-09-14 16:15:25 -07001999
2000 /* Set up offsets for literals */
2001 data_offset_ = offset;
2002
2003 offset = AssignLiteralOffset(offset);
2004
2005 offset = AssignSwitchTablesOffset(offset);
2006
2007 offset = AssignFillArrayDataOffset(offset);
2008
2009 total_size_ = offset;
2010}
2011
2012/*
2013 * Go over each instruction in the list and calculate the offset from the top
2014 * before sending them off to the assembler. If out-of-range branch distance is
2015 * seen rearrange the instructions a bit to correct it.
2016 * TODO: consolidate w/ Arm assembly mechanism.
2017 */
2018void X86Mir2Lir::AssembleLIR() {
buzbeea61f4952013-08-23 14:27:06 -07002019 cu_->NewTimingSplit("Assemble");
Mark Mendell55d0eac2014-02-06 11:02:52 -08002020
2021 // We will remove the method address if we never ended up using it
Vladimir Marko1961b602015-04-08 20:51:48 +01002022 if (pc_rel_base_reg_.Valid() && !pc_rel_base_reg_used_) {
2023 if (kIsDebugBuild) {
2024 LOG(WARNING) << "PC-relative addressing base promoted but unused in "
2025 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
2026 }
2027 setup_pc_rel_base_reg_->flags.is_nop = true;
2028 NEXT_LIR(setup_pc_rel_base_reg_)->flags.is_nop = true;
Mark Mendell55d0eac2014-02-06 11:02:52 -08002029 }
2030
buzbeeb48819d2013-09-14 16:15:25 -07002031 AssignOffsets();
2032 int assembler_retries = 0;
2033 /*
2034 * Assemble here. Note that we generate code with optimistic assumptions
2035 * and if found now to work, we'll have to redo the sequence and retry.
2036 */
2037
2038 while (true) {
Chao-ying Fuc4013ea2015-04-22 10:51:21 -07002039 AssemblerStatus res = AssembleInstructions(first_lir_insn_, 0);
buzbeeb48819d2013-09-14 16:15:25 -07002040 if (res == kSuccess) {
2041 break;
2042 } else {
2043 assembler_retries++;
2044 if (assembler_retries > MAX_ASSEMBLER_RETRIES) {
2045 CodegenDump();
2046 LOG(FATAL) << "Assembler error - too many retries";
2047 }
2048 // Redo offsets and try again
2049 AssignOffsets();
2050 code_buffer_.clear();
2051 }
2052 }
2053
2054 // Install literals
2055 InstallLiteralPools();
2056
2057 // Install switch tables
2058 InstallSwitchTables();
2059
2060 // Install fill array data
2061 InstallFillArrayData();
2062
2063 // Create the mapping table and native offset to reference map.
buzbeea61f4952013-08-23 14:27:06 -07002064 cu_->NewTimingSplit("PcMappingTable");
buzbeeb48819d2013-09-14 16:15:25 -07002065 CreateMappingTables();
2066
buzbeea61f4952013-08-23 14:27:06 -07002067 cu_->NewTimingSplit("GcMap");
buzbeeb48819d2013-09-14 16:15:25 -07002068 CreateNativeGcMap();
2069}
2070
Brian Carlstrom7940e442013-07-12 13:46:57 -07002071} // namespace art