blob: c47711cab3ef8b89e22bd5bf6abe72456ab202ed [file] [log] [blame]
buzbeee88dfbf2012-03-05 11:19:57 -08001/*
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 "../../Dalvik.h"
18#include "../../CompilerInternals.h"
19#include "X86LIR.h"
20#include "Codegen.h"
buzbeee88dfbf2012-03-05 11:19:57 -080021
22namespace art {
23
24#define MAX_ASSEMBLER_RETRIES 50
25
buzbeea7678db2012-03-05 15:35:46 -080026X86EncodingMap EncodingMap[kX86Last] = {
Ian Rogersb5d09b22012-03-06 22:14:17 -080027 { kX8632BitData, kData, IS_UNARY_OP, { 0, 0, 0x00, 0, 0, 0, 0, 4 }, "data", "0x!0d" },
Ian Rogers7caad772012-03-30 01:07:54 -070028 { kX86Bkpt, kNullary, NO_OPERAND | IS_BRANCH, { 0, 0, 0xCC, 0, 0, 0, 0, 0 }, "int 3", "" },
Ian Rogersb5d09b22012-03-06 22:14:17 -080029 { kX86Nop, kNop, IS_UNARY_OP, { 0, 0, 0x90, 0, 0, 0, 0, 0 }, "nop", "" },
30
jeffhaoe2962482012-06-28 11:29:57 -070031#define ENCODING_MAP(opname, mem_use, reg_def, uses_ccodes, \
Ian Rogersb5d09b22012-03-06 22:14:17 -080032 rm8_r8, rm32_r32, \
33 r8_rm8, r32_rm32, \
34 ax8_i8, ax32_i32, \
35 rm8_i8, rm8_i8_modrm, \
36 rm32_i32, rm32_i32_modrm, \
37 rm32_i8, rm32_i8_modrm) \
jeffhaoe2962482012-06-28 11:29:57 -070038{ kX86 ## opname ## 8MR, kMemReg, mem_use | IS_TERTIARY_OP | REG_USE02 | SETS_CCODES | uses_ccodes, { 0, 0, rm8_r8, 0, 0, 0, 0, 0 }, #opname "8MR", "[!0r+!1d],!2r" }, \
39{ kX86 ## opname ## 8AR, kArrayReg, mem_use | IS_QUIN_OP | REG_USE014 | SETS_CCODES | uses_ccodes, { 0, 0, rm8_r8, 0, 0, 0, 0, 0 }, #opname "8AR", "[!0r+!1r<<!2d+!3d],!4r" }, \
40{ kX86 ## opname ## 8TR, kThreadReg, mem_use | IS_BINARY_OP | REG_USE1 | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0, rm8_r8, 0, 0, 0, 0, 0 }, #opname "8TR", "fs:[!0d],!1r" }, \
41{ kX86 ## opname ## 8RR, kRegReg, IS_BINARY_OP | reg_def | REG_USE01 | SETS_CCODES | uses_ccodes, { 0, 0, r8_rm8, 0, 0, 0, 0, 0 }, #opname "8RR", "!0r,!1r" }, \
42{ kX86 ## opname ## 8RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | reg_def | REG_USE01 | SETS_CCODES | uses_ccodes, { 0, 0, r8_rm8, 0, 0, 0, 0, 0 }, #opname "8RM", "!0r,[!1r+!2d]" }, \
43{ kX86 ## opname ## 8RA, kRegArray, IS_LOAD | IS_QUIN_OP | reg_def | REG_USE012 | SETS_CCODES | uses_ccodes, { 0, 0, r8_rm8, 0, 0, 0, 0, 0 }, #opname "8RA", "!0r,[!1r+!2r<<!3d+!4d]" }, \
44{ kX86 ## opname ## 8RT, kRegThread, IS_LOAD | IS_BINARY_OP | reg_def | REG_USE0 | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0, r8_rm8, 0, 0, 0, 0, 0 }, #opname "8RT", "!0r,fs:[!1d]" }, \
45{ kX86 ## opname ## 8RI, kRegImm, IS_BINARY_OP | reg_def | REG_USE0 | SETS_CCODES | uses_ccodes, { 0, 0, rm8_i8, 0, 0, rm8_i8_modrm, ax8_i8, 1 }, #opname "8RI", "!0r,!1d" }, \
46{ kX86 ## opname ## 8MI, kMemImm, mem_use | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES | uses_ccodes, { 0, 0, rm8_i8, 0, 0, rm8_i8_modrm, 0, 1 }, #opname "8MI", "[!0r+!1d],!2d" }, \
47{ kX86 ## opname ## 8AI, kArrayImm, mem_use | IS_QUIN_OP | REG_USE01 | SETS_CCODES | uses_ccodes, { 0, 0, rm8_i8, 0, 0, rm8_i8_modrm, 0, 1 }, #opname "8AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
48{ kX86 ## opname ## 8TI, kThreadImm, mem_use | IS_BINARY_OP | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0, rm8_i8, 0, 0, rm8_i8_modrm, 0, 1 }, #opname "8TI", "fs:[!0d],!1d" }, \
Ian Rogersb5d09b22012-03-06 22:14:17 -080049 \
jeffhaoe2962482012-06-28 11:29:57 -070050{ kX86 ## opname ## 16MR, kMemReg, mem_use | IS_TERTIARY_OP | REG_USE02 | SETS_CCODES | uses_ccodes, { 0x66, 0, rm32_r32, 0, 0, 0, 0, 0 }, #opname "16MR", "[!0r+!1d],!2r" }, \
51{ kX86 ## opname ## 16AR, kArrayReg, mem_use | IS_QUIN_OP | REG_USE014 | SETS_CCODES | uses_ccodes, { 0x66, 0, rm32_r32, 0, 0, 0, 0, 0 }, #opname "16AR", "[!0r+!1r<<!2d+!3d],!4r" }, \
52{ kX86 ## opname ## 16TR, kThreadReg, mem_use | IS_BINARY_OP | REG_USE1 | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0x66, rm32_r32, 0, 0, 0, 0, 0 }, #opname "16TR", "fs:[!0d],!1r" }, \
53{ kX86 ## opname ## 16RR, kRegReg, IS_BINARY_OP | reg_def | REG_USE01 | SETS_CCODES | uses_ccodes, { 0x66, 0, r32_rm32, 0, 0, 0, 0, 0 }, #opname "16RR", "!0r,!1r" }, \
54{ kX86 ## opname ## 16RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | reg_def | REG_USE01 | SETS_CCODES | uses_ccodes, { 0x66, 0, r32_rm32, 0, 0, 0, 0, 0 }, #opname "16RM", "!0r,[!1r+!2d]" }, \
55{ kX86 ## opname ## 16RA, kRegArray, IS_LOAD | IS_QUIN_OP | reg_def | REG_USE012 | SETS_CCODES | uses_ccodes, { 0x66, 0, r32_rm32, 0, 0, 0, 0, 0 }, #opname "16RA", "!0r,[!1r+!2r<<!3d+!4d]" }, \
56{ kX86 ## opname ## 16RT, kRegThread, IS_LOAD | IS_BINARY_OP | reg_def | REG_USE0 | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0x66, r32_rm32, 0, 0, 0, 0, 0 }, #opname "16RT", "!0r,fs:[!1d]" }, \
57{ kX86 ## opname ## 16RI, kRegImm, IS_BINARY_OP | reg_def | REG_USE0 | SETS_CCODES | uses_ccodes, { 0x66, 0, rm32_i32, 0, 0, rm32_i32_modrm, ax32_i32, 2 }, #opname "16RI", "!0r,!1d" }, \
58{ kX86 ## opname ## 16MI, kMemImm, mem_use | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES | uses_ccodes, { 0x66, 0, rm32_i32, 0, 0, rm32_i32_modrm, 0, 2 }, #opname "16MI", "[!0r+!1d],!2d" }, \
59{ kX86 ## opname ## 16AI, kArrayImm, mem_use | IS_QUIN_OP | REG_USE01 | SETS_CCODES | uses_ccodes, { 0x66, 0, rm32_i32, 0, 0, rm32_i32_modrm, 0, 2 }, #opname "16AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
60{ kX86 ## opname ## 16TI, kThreadImm, mem_use | IS_BINARY_OP | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0x66, rm32_i32, 0, 0, rm32_i32_modrm, 0, 2 }, #opname "16TI", "fs:[!0d],!1d" }, \
61{ kX86 ## opname ## 16RI8, kRegImm, IS_BINARY_OP | reg_def | REG_USE0 | SETS_CCODES | uses_ccodes, { 0x66, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "16RI8", "!0r,!1d" }, \
62{ kX86 ## opname ## 16MI8, kMemImm, mem_use | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES | uses_ccodes, { 0x66, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "16MI8", "[!0r+!1d],!2d" }, \
63{ kX86 ## opname ## 16AI8, kArrayImm, mem_use | IS_QUIN_OP | REG_USE01 | SETS_CCODES | uses_ccodes, { 0x66, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "16AI8", "[!0r+!1r<<!2d+!3d],!4d" }, \
64{ kX86 ## opname ## 16TI8, kThreadImm, mem_use | IS_BINARY_OP | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0x66, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "16TI8", "fs:[!0d],!1d" }, \
Ian Rogersb5d09b22012-03-06 22:14:17 -080065 \
jeffhaoe2962482012-06-28 11:29:57 -070066{ kX86 ## opname ## 32MR, kMemReg, mem_use | IS_TERTIARY_OP | REG_USE02 | SETS_CCODES | uses_ccodes, { 0, 0, rm32_r32, 0, 0, 0, 0, 0 }, #opname "32MR", "[!0r+!1d],!2r" }, \
67{ kX86 ## opname ## 32AR, kArrayReg, mem_use | IS_QUIN_OP | REG_USE014 | SETS_CCODES | uses_ccodes, { 0, 0, rm32_r32, 0, 0, 0, 0, 0 }, #opname "32AR", "[!0r+!1r<<!2d+!3d],!4r" }, \
68{ kX86 ## opname ## 32TR, kThreadReg, mem_use | IS_BINARY_OP | REG_USE1 | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0, rm32_r32, 0, 0, 0, 0, 0 }, #opname "32TR", "fs:[!0d],!1r" }, \
69{ kX86 ## opname ## 32RR, kRegReg, IS_BINARY_OP | reg_def | REG_USE01 | SETS_CCODES | uses_ccodes, { 0, 0, r32_rm32, 0, 0, 0, 0, 0 }, #opname "32RR", "!0r,!1r" }, \
70{ kX86 ## opname ## 32RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | reg_def | REG_USE01 | SETS_CCODES | uses_ccodes, { 0, 0, r32_rm32, 0, 0, 0, 0, 0 }, #opname "32RM", "!0r,[!1r+!2d]" }, \
71{ kX86 ## opname ## 32RA, kRegArray, IS_LOAD | IS_QUIN_OP | reg_def | REG_USE012 | SETS_CCODES | uses_ccodes, { 0, 0, r32_rm32, 0, 0, 0, 0, 0 }, #opname "32RA", "!0r,[!1r+!2r<<!3d+!4d]" }, \
72{ kX86 ## opname ## 32RT, kRegThread, IS_LOAD | IS_BINARY_OP | reg_def | REG_USE0 | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0, r32_rm32, 0, 0, 0, 0, 0 }, #opname "32RT", "!0r,fs:[!1d]" }, \
73{ kX86 ## opname ## 32RI, kRegImm, IS_BINARY_OP | reg_def | REG_USE0 | SETS_CCODES | uses_ccodes, { 0, 0, rm32_i32, 0, 0, rm32_i32_modrm, ax32_i32, 4 }, #opname "32RI", "!0r,!1d" }, \
74{ kX86 ## opname ## 32MI, kMemImm, mem_use | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES | uses_ccodes, { 0, 0, rm32_i32, 0, 0, rm32_i32_modrm, 0, 4 }, #opname "32MI", "[!0r+!1d],!2d" }, \
75{ kX86 ## opname ## 32AI, kArrayImm, mem_use | IS_QUIN_OP | REG_USE01 | SETS_CCODES | uses_ccodes, { 0, 0, rm32_i32, 0, 0, rm32_i32_modrm, 0, 4 }, #opname "32AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
76{ kX86 ## opname ## 32TI, kThreadImm, mem_use | IS_BINARY_OP | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0, rm32_i32, 0, 0, rm32_i32_modrm, 0, 4 }, #opname "32TI", "fs:[!0d],!1d" }, \
77{ kX86 ## opname ## 32RI8, kRegImm, IS_BINARY_OP | reg_def | REG_USE0 | SETS_CCODES | uses_ccodes, { 0, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "32RI8", "!0r,!1d" }, \
78{ kX86 ## opname ## 32MI8, kMemImm, mem_use | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES | uses_ccodes, { 0, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "32MI8", "[!0r+!1d],!2d" }, \
79{ kX86 ## opname ## 32AI8, kArrayImm, mem_use | IS_QUIN_OP | REG_USE01 | SETS_CCODES | uses_ccodes, { 0, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "32AI8", "[!0r+!1r<<!2d+!3d],!4d" }, \
80{ kX86 ## opname ## 32TI8, kThreadImm, mem_use | IS_BINARY_OP | SETS_CCODES | uses_ccodes, { THREAD_PREFIX, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "32TI8", "fs:[!0d],!1d" }
Ian Rogersb5d09b22012-03-06 22:14:17 -080081
jeffhaoe2962482012-06-28 11:29:57 -070082ENCODING_MAP(Add, IS_LOAD | IS_STORE, REG_DEF0, 0,
Ian Rogers96ab4202012-03-05 19:51:02 -080083 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */,
84 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */,
85 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */,
86 0x80, 0x0 /* RegMem8/imm8 */,
87 0x81, 0x0 /* RegMem32/imm32 */, 0x83, 0x0 /* RegMem32/imm8 */),
jeffhaoe2962482012-06-28 11:29:57 -070088ENCODING_MAP(Or, IS_LOAD | IS_STORE, REG_DEF0, 0,
Ian Rogers96ab4202012-03-05 19:51:02 -080089 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */,
90 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */,
91 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */,
92 0x80, 0x1 /* RegMem8/imm8 */,
93 0x81, 0x1 /* RegMem32/imm32 */, 0x83, 0x1 /* RegMem32/imm8 */),
jeffhaoe2962482012-06-28 11:29:57 -070094ENCODING_MAP(Adc, IS_LOAD | IS_STORE, REG_DEF0, USES_CCODES,
Ian Rogers96ab4202012-03-05 19:51:02 -080095 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */,
96 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */,
97 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */,
98 0x80, 0x2 /* RegMem8/imm8 */,
99 0x81, 0x2 /* RegMem32/imm32 */, 0x83, 0x2 /* RegMem32/imm8 */),
jeffhaoe2962482012-06-28 11:29:57 -0700100ENCODING_MAP(Sbb, IS_LOAD | IS_STORE, REG_DEF0, USES_CCODES,
Ian Rogers96ab4202012-03-05 19:51:02 -0800101 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */,
102 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */,
103 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */,
104 0x80, 0x3 /* RegMem8/imm8 */,
105 0x81, 0x3 /* RegMem32/imm32 */, 0x83, 0x3 /* RegMem32/imm8 */),
jeffhaoe2962482012-06-28 11:29:57 -0700106ENCODING_MAP(And, IS_LOAD | IS_STORE, REG_DEF0, 0,
Ian Rogers96ab4202012-03-05 19:51:02 -0800107 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */,
108 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */,
109 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */,
110 0x80, 0x4 /* RegMem8/imm8 */,
111 0x81, 0x4 /* RegMem32/imm32 */, 0x83, 0x4 /* RegMem32/imm8 */),
jeffhaoe2962482012-06-28 11:29:57 -0700112ENCODING_MAP(Sub, IS_LOAD | IS_STORE, REG_DEF0, 0,
Ian Rogers96ab4202012-03-05 19:51:02 -0800113 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */,
114 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */,
115 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */,
116 0x80, 0x5 /* RegMem8/imm8 */,
117 0x81, 0x5 /* RegMem32/imm32 */, 0x83, 0x5 /* RegMem32/imm8 */),
jeffhaoe2962482012-06-28 11:29:57 -0700118ENCODING_MAP(Xor, IS_LOAD | IS_STORE, REG_DEF0, 0,
Ian Rogers96ab4202012-03-05 19:51:02 -0800119 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */,
120 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */,
121 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */,
122 0x80, 0x6 /* RegMem8/imm8 */,
123 0x81, 0x6 /* RegMem32/imm32 */, 0x83, 0x6 /* RegMem32/imm8 */),
jeffhaoe2962482012-06-28 11:29:57 -0700124ENCODING_MAP(Cmp, IS_LOAD, 0, 0,
Ian Rogers96ab4202012-03-05 19:51:02 -0800125 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */,
126 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */,
127 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */,
128 0x80, 0x7 /* RegMem8/imm8 */,
Ian Rogersde797832012-03-06 10:18:10 -0800129 0x81, 0x7 /* RegMem32/imm32 */, 0x83, 0x7 /* RegMem32/imm8 */),
Ian Rogersb5d09b22012-03-06 22:14:17 -0800130#undef ENCODING_MAP
131
jeffhaoe2962482012-06-28 11:29:57 -0700132 { kX86Imul16RRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { 0x66, 0, 0x69, 0, 0, 0, 0, 2 }, "Imul16RRI", "!0r,!1r,!2d" },
133 { kX86Imul16RMI, kRegMemImm, IS_LOAD | IS_QUAD_OP | REG_DEF0_USE1 | SETS_CCODES, { 0x66, 0, 0x69, 0, 0, 0, 0, 2 }, "Imul16RMI", "!0r,[!1r+!2d],!3d" },
134 { kX86Imul16RAI, kRegArrayImm, IS_LOAD | IS_SEXTUPLE_OP | REG_DEF0_USE12 | SETS_CCODES, { 0x66, 0, 0x69, 0, 0, 0, 0, 2 }, "Imul16RAI", "!0r,[!1r+!2r<<!3d+!4d],!5d" },
Ian Rogersb5d09b22012-03-06 22:14:17 -0800135
jeffhaoe2962482012-06-28 11:29:57 -0700136 { kX86Imul32RRI, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { 0, 0, 0x69, 0, 0, 0, 0, 4 }, "Imul32RRI", "!0r,!1r,!2d" },
137 { kX86Imul32RMI, kRegMemImm, IS_LOAD | IS_QUAD_OP | REG_DEF0_USE1 | SETS_CCODES, { 0, 0, 0x69, 0, 0, 0, 0, 4 }, "Imul32RMI", "!0r,[!1r+!2d],!3d" },
138 { kX86Imul32RAI, kRegArrayImm, IS_LOAD | IS_SEXTUPLE_OP | REG_DEF0_USE12 | SETS_CCODES, { 0, 0, 0x69, 0, 0, 0, 0, 4 }, "Imul32RAI", "!0r,[!1r+!2r<<!3d+!4d],!5d" },
139 { kX86Imul32RRI8, kRegRegImm, IS_TERTIARY_OP | REG_DEF0_USE1 | SETS_CCODES, { 0, 0, 0x6B, 0, 0, 0, 0, 1 }, "Imul32RRI8", "!0r,!1r,!2d" },
140 { kX86Imul32RMI8, kRegMemImm, IS_LOAD | IS_QUAD_OP | REG_DEF0_USE1 | SETS_CCODES, { 0, 0, 0x6B, 0, 0, 0, 0, 1 }, "Imul32RMI8", "!0r,[!1r+!2d],!3d" },
141 { kX86Imul32RAI8, kRegArrayImm, IS_LOAD | IS_SEXTUPLE_OP | REG_DEF0_USE12 | SETS_CCODES, { 0, 0, 0x6B, 0, 0, 0, 0, 1 }, "Imul32RAI8", "!0r,[!1r+!2r<<!3d+!4d],!5d" },
Ian Rogersb5d09b22012-03-06 22:14:17 -0800142
jeffhaoe2962482012-06-28 11:29:57 -0700143 { kX86Mov8MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0, 0, 0x88, 0, 0, 0, 0, 0 }, "Mov8MR", "[!0r+!1d],!2r" },
144 { kX86Mov8AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0, 0, 0x88, 0, 0, 0, 0, 0 }, "Mov8AR", "[!0r+!1r<<!2d+!3d],!4r" },
145 { kX86Mov8TR, kThreadReg, IS_STORE | IS_BINARY_OP | REG_USE1, { THREAD_PREFIX, 0, 0x88, 0, 0, 0, 0, 0 }, "Mov8TR", "fs:[!0d],!1r" },
146 { kX86Mov8RR, kRegReg, IS_BINARY_OP | REG_DEF0_USE1, { 0, 0, 0x8A, 0, 0, 0, 0, 0 }, "Mov8RR", "!0r,!1r" },
147 { kX86Mov8RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { 0, 0, 0x8A, 0, 0, 0, 0, 0 }, "Mov8RM", "!0r,[!1r+!2d]" },
148 { kX86Mov8RA, kRegArray, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8A, 0, 0, 0, 0, 0 }, "Mov8RA", "!0r,[!1r+!2r<<!3d+!4d]" },
149 { kX86Mov8RT, kRegThread, IS_LOAD | IS_BINARY_OP | REG_DEF0, { THREAD_PREFIX, 0, 0x8A, 0, 0, 0, 0, 0 }, "Mov8RT", "!0r,fs:[!1d]" },
150 { kX86Mov8RI, kMovRegImm, IS_BINARY_OP | REG_DEF0, { 0, 0, 0xB0, 0, 0, 0, 0, 1 }, "Mov8RI", "!0r,!1d" },
151 { kX86Mov8MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { 0, 0, 0xC6, 0, 0, 0, 0, 1 }, "Mov8MI", "[!0r+!1d],!2d" },
152 { kX86Mov8AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { 0, 0, 0xC6, 0, 0, 0, 0, 1 }, "Mov8AI", "[!0r+!1r<<!2d+!3d],!4d" },
153 { kX86Mov8TI, kThreadImm, IS_STORE | IS_BINARY_OP, { THREAD_PREFIX, 0, 0xC6, 0, 0, 0, 0, 1 }, "Mov8TI", "fs:[!0d],!1d" },
Ian Rogersb5d09b22012-03-06 22:14:17 -0800154
jeffhaoe2962482012-06-28 11:29:57 -0700155 { kX86Mov16MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov16MR", "[!0r+!1d],!2r" },
156 { kX86Mov16AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x66, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov16AR", "[!0r+!1r<<!2d+!3d],!4r" },
157 { kX86Mov16TR, kThreadReg, IS_STORE | IS_BINARY_OP | REG_USE1, { THREAD_PREFIX, 0x66, 0x89, 0, 0, 0, 0, 0 }, "Mov16TR", "fs:[!0d],!1r" },
158 { kX86Mov16RR, kRegReg, IS_BINARY_OP | REG_DEF0_USE1, { 0x66, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov16RR", "!0r,!1r" },
159 { kX86Mov16RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { 0x66, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov16RM", "!0r,[!1r+!2d]" },
160 { kX86Mov16RA, kRegArray, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0x66, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov16RA", "!0r,[!1r+!2r<<!3d+!4d]" },
161 { kX86Mov16RT, kRegThread, IS_LOAD | IS_BINARY_OP | REG_DEF0, { THREAD_PREFIX, 0x66, 0x8B, 0, 0, 0, 0, 0 }, "Mov16RT", "!0r,fs:[!1d]" },
162 { kX86Mov16RI, kMovRegImm, IS_BINARY_OP | REG_DEF0, { 0x66, 0, 0xB8, 0, 0, 0, 0, 2 }, "Mov16RI", "!0r,!1d" },
163 { kX86Mov16MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { 0x66, 0, 0xC7, 0, 0, 0, 0, 2 }, "Mov16MI", "[!0r+!1d],!2d" },
164 { kX86Mov16AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { 0x66, 0, 0xC7, 0, 0, 0, 0, 2 }, "Mov16AI", "[!0r+!1r<<!2d+!3d],!4d" },
165 { kX86Mov16TI, kThreadImm, IS_STORE | IS_BINARY_OP, { THREAD_PREFIX, 0x66, 0xC7, 0, 0, 0, 0, 2 }, "Mov16TI", "fs:[!0d],!1d" },
Ian Rogersb5d09b22012-03-06 22:14:17 -0800166
jeffhaoe2962482012-06-28 11:29:57 -0700167 { kX86Mov32MR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov32MR", "[!0r+!1d],!2r" },
168 { kX86Mov32AR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov32AR", "[!0r+!1r<<!2d+!3d],!4r" },
169 { kX86Mov32TR, kThreadReg, IS_STORE | IS_BINARY_OP | REG_USE1, { THREAD_PREFIX, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov32TR", "fs:[!0d],!1r" },
170 { kX86Mov32RR, kRegReg, IS_BINARY_OP | REG_DEF0_USE1, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov32RR", "!0r,!1r" },
171 { kX86Mov32RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | REG_DEF0_USE1, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov32RM", "!0r,[!1r+!2d]" },
172 { kX86Mov32RA, kRegArray, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov32RA", "!0r,[!1r+!2r<<!3d+!4d]" },
173 { kX86Mov32RT, kRegThread, IS_LOAD | IS_BINARY_OP | REG_DEF0, { THREAD_PREFIX, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov32RT", "!0r,fs:[!1d]" },
174 { kX86Mov32RI, kMovRegImm, IS_BINARY_OP | REG_DEF0, { 0, 0, 0xB8, 0, 0, 0, 0, 4 }, "Mov32RI", "!0r,!1d" },
175 { kX86Mov32MI, kMemImm, IS_STORE | IS_TERTIARY_OP | REG_USE0, { 0, 0, 0xC7, 0, 0, 0, 0, 4 }, "Mov32MI", "[!0r+!1d],!2d" },
176 { kX86Mov32AI, kArrayImm, IS_STORE | IS_QUIN_OP | REG_USE01, { 0, 0, 0xC7, 0, 0, 0, 0, 4 }, "Mov32AI", "[!0r+!1r<<!2d+!3d],!4d" },
177 { kX86Mov32TI, kThreadImm, IS_STORE | IS_BINARY_OP, { THREAD_PREFIX, 0, 0xC7, 0, 0, 0, 0, 4 }, "Mov32TI", "fs:[!0d],!1d" },
Ian Rogersb5d09b22012-03-06 22:14:17 -0800178
jeffhaoe2962482012-06-28 11:29:57 -0700179 { kX86Lea32RA, kRegArray, IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8D, 0, 0, 0, 0, 0 }, "Lea32RA", "!0r,[!1r+!2r<<!3d+!4d]" },
Ian Rogersb5d09b22012-03-06 22:14:17 -0800180
181#define SHIFT_ENCODING_MAP(opname, modrm_opcode) \
jeffhaoe2962482012-06-28 11:29:57 -0700182{ kX86 ## opname ## 8RI, kShiftRegImm, IS_BINARY_OP | REG_DEF0_USE0 | SETS_CCODES, { 0, 0, 0xC0, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "8RI", "!0r,!1d" }, \
183{ kX86 ## opname ## 8MI, kShiftMemImm, IS_LOAD | IS_STORE | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xC0, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "8MI", "[!0r+!1d],!2d" }, \
184{ kX86 ## opname ## 8AI, kShiftArrayImm, IS_LOAD | IS_STORE | IS_QUIN_OP | REG_USE01 | SETS_CCODES, { 0, 0, 0xC0, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "8AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
185{ kX86 ## opname ## 8RC, kShiftRegCl, IS_BINARY_OP | REG_DEF0_USE0 | REG_USEC | SETS_CCODES, { 0, 0, 0xD2, 0, 0, modrm_opcode, 0, 1 }, #opname "8RC", "!0r,cl" }, \
186{ kX86 ## opname ## 8MC, kShiftMemCl, IS_LOAD | IS_STORE | IS_TERTIARY_OP | REG_USE0 | REG_USEC | SETS_CCODES, { 0, 0, 0xD2, 0, 0, modrm_opcode, 0, 1 }, #opname "8MC", "[!0r+!1d],cl" }, \
187{ kX86 ## opname ## 8AC, kShiftArrayCl, IS_LOAD | IS_STORE | IS_QUIN_OP | REG_USE01 | REG_USEC | SETS_CCODES, { 0, 0, 0xD2, 0, 0, modrm_opcode, 0, 1 }, #opname "8AC", "[!0r+!1r<<!2d+!3d],cl" }, \
Ian Rogersb5d09b22012-03-06 22:14:17 -0800188 \
jeffhaoe2962482012-06-28 11:29:57 -0700189{ kX86 ## opname ## 16RI, kShiftRegImm, IS_BINARY_OP | REG_DEF0_USE0 | SETS_CCODES, { 0x66, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "16RI", "!0r,!1d" }, \
190{ kX86 ## opname ## 16MI, kShiftMemImm, IS_LOAD | IS_STORE | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0x66, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "16MI", "[!0r+!1d],!2d" }, \
191{ kX86 ## opname ## 16AI, kShiftArrayImm, IS_LOAD | IS_STORE | IS_QUIN_OP | REG_USE01 | SETS_CCODES, { 0x66, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "16AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
192{ kX86 ## opname ## 16RC, kShiftRegCl, IS_BINARY_OP | REG_DEF0_USE0 | REG_USEC | SETS_CCODES, { 0x66, 0, 0xD3, 0, 0, modrm_opcode, 0, 1 }, #opname "16RC", "!0r,cl" }, \
193{ kX86 ## opname ## 16MC, kShiftMemCl, IS_LOAD | IS_STORE | IS_TERTIARY_OP | REG_USE0 | REG_USEC | SETS_CCODES, { 0x66, 0, 0xD3, 0, 0, modrm_opcode, 0, 1 }, #opname "16MC", "[!0r+!1d],cl" }, \
194{ kX86 ## opname ## 16AC, kShiftArrayCl, IS_LOAD | IS_STORE | IS_QUIN_OP | REG_USE01 | REG_USEC | SETS_CCODES, { 0x66, 0, 0xD3, 0, 0, modrm_opcode, 0, 1 }, #opname "16AC", "[!0r+!1r<<!2d+!3d],cl" }, \
Ian Rogersb5d09b22012-03-06 22:14:17 -0800195 \
jeffhaoe2962482012-06-28 11:29:57 -0700196{ kX86 ## opname ## 32RI, kShiftRegImm, IS_BINARY_OP | REG_DEF0_USE0 | SETS_CCODES, { 0, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "32RI", "!0r,!1d" }, \
197{ kX86 ## opname ## 32MI, kShiftMemImm, IS_LOAD | IS_STORE | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "32MI", "[!0r+!1d],!2d" }, \
198{ kX86 ## opname ## 32AI, kShiftArrayImm, IS_LOAD | IS_STORE | IS_QUIN_OP | REG_USE01 | SETS_CCODES, { 0, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "32AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
199{ kX86 ## opname ## 32RC, kShiftRegCl, IS_BINARY_OP | REG_DEF0_USE0 | REG_USEC | SETS_CCODES, { 0, 0, 0xD3, 0, 0, modrm_opcode, 0, 0 }, #opname "32RC", "!0r,cl" }, \
200{ kX86 ## opname ## 32MC, kShiftMemCl, IS_LOAD | IS_STORE | IS_TERTIARY_OP | REG_USE0 | REG_USEC | SETS_CCODES, { 0, 0, 0xD3, 0, 0, modrm_opcode, 0, 0 }, #opname "32MC", "[!0r+!1d],cl" }, \
201{ kX86 ## opname ## 32AC, kShiftArrayCl, IS_LOAD | IS_STORE | IS_QUIN_OP | REG_USE01 | REG_USEC | SETS_CCODES, { 0, 0, 0xD3, 0, 0, modrm_opcode, 0, 0 }, #opname "32AC", "[!0r+!1r<<!2d+!3d],cl" }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800202
203 SHIFT_ENCODING_MAP(Rol, 0x0),
204 SHIFT_ENCODING_MAP(Ror, 0x1),
205 SHIFT_ENCODING_MAP(Rcl, 0x2),
206 SHIFT_ENCODING_MAP(Rcr, 0x3),
207 SHIFT_ENCODING_MAP(Sal, 0x4),
Ian Rogers7caad772012-03-30 01:07:54 -0700208 SHIFT_ENCODING_MAP(Shr, 0x5),
Ian Rogersb5d09b22012-03-06 22:14:17 -0800209 SHIFT_ENCODING_MAP(Sar, 0x7),
210#undef SHIFT_ENCODING_MAP
211
jeffhao77ae36b2012-08-07 14:18:16 -0700212 { kX86Cmc, kNullary, NO_OPERAND, { 0, 0, 0xF5, 0, 0, 0, 0, 0}, "Cmc", "" },
213
jeffhaoe2962482012-06-28 11:29:57 -0700214 { kX86Test8RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF6, 0, 0, 0, 0, 1}, "Test8RI", "!0r,!1d" },
215 { kX86Test8MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF6, 0, 0, 0, 0, 1}, "Test8MI", "[!0r+!1d],!2d" },
216 { kX86Test8AI, kArrayImm, IS_LOAD | IS_QUIN_OP | REG_USE01 | SETS_CCODES, { 0, 0, 0xF6, 0, 0, 0, 0, 1}, "Test8AI", "[!0r+!1r<<!2d+!3d],!4d" },
217 { kX86Test16RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0x66, 0, 0xF7, 0, 0, 0, 0, 2}, "Test16RI", "!0r,!1d" },
218 { kX86Test16MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0x66, 0, 0xF7, 0, 0, 0, 0, 2}, "Test16MI", "[!0r+!1d],!2d" },
219 { kX86Test16AI, kArrayImm, IS_LOAD | IS_QUIN_OP | REG_USE01 | SETS_CCODES, { 0x66, 0, 0xF7, 0, 0, 0, 0, 2}, "Test16AI", "[!0r+!1r<<!2d+!3d],!4d" },
220 { kX86Test32RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF7, 0, 0, 0, 0, 4}, "Test32RI", "!0r,!1d" },
221 { kX86Test32MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF7, 0, 0, 0, 0, 4}, "Test32MI", "[!0r+!1d],!2d" },
222 { kX86Test32AI, kArrayImm, IS_LOAD | IS_QUIN_OP | REG_USE01 | SETS_CCODES, { 0, 0, 0xF7, 0, 0, 0, 0, 4}, "Test32AI", "[!0r+!1r<<!2d+!3d],!4d" },
Ian Rogers2e9f7ed2012-09-26 11:30:43 -0700223 { kX86Test32RR, kRegReg, IS_BINARY_OP | REG_USE01 | SETS_CCODES, { 0, 0, 0x85, 0, 0, 0, 0, 0}, "Test32RR", "!0r,!1r" },
jeffhaoe2962482012-06-28 11:29:57 -0700224
225#define UNARY_ENCODING_MAP(opname, modrm, is_store, sets_ccodes, \
Ian Rogersb5d09b22012-03-06 22:14:17 -0800226 reg, reg_kind, reg_flags, \
227 mem, mem_kind, mem_flags, \
jeffhaoe2962482012-06-28 11:29:57 -0700228 arr, arr_kind, arr_flags, imm, \
229 b_flags, hw_flags, w_flags, \
230 b_format, hw_format, w_format) \
231{ kX86 ## opname ## 8 ## reg, reg_kind, reg_flags | b_flags | sets_ccodes, { 0, 0, 0xF6, 0, 0, modrm, 0, imm << 0}, #opname "8" #reg, #b_format "!0r" }, \
232{ kX86 ## opname ## 8 ## mem, mem_kind, IS_LOAD | is_store | mem_flags | b_flags | sets_ccodes, { 0, 0, 0xF6, 0, 0, modrm, 0, imm << 0}, #opname "8" #mem, #b_format "[!0r+!1d]" }, \
233{ kX86 ## opname ## 8 ## arr, arr_kind, IS_LOAD | is_store | arr_flags | b_flags | sets_ccodes, { 0, 0, 0xF6, 0, 0, modrm, 0, imm << 0}, #opname "8" #arr, #b_format "[!0r+!1r<<!2d+!3d]" }, \
234{ kX86 ## opname ## 16 ## reg, reg_kind, reg_flags | hw_flags | sets_ccodes, { 0x66, 0, 0xF7, 0, 0, modrm, 0, imm << 1}, #opname "16" #reg, #hw_format "!0r" }, \
235{ kX86 ## opname ## 16 ## mem, mem_kind, IS_LOAD | is_store | mem_flags | hw_flags | sets_ccodes, { 0x66, 0, 0xF7, 0, 0, modrm, 0, imm << 1}, #opname "16" #mem, #hw_format "[!0r+!1d]" }, \
236{ kX86 ## opname ## 16 ## arr, arr_kind, IS_LOAD | is_store | arr_flags | hw_flags | sets_ccodes, { 0x66, 0, 0xF7, 0, 0, modrm, 0, imm << 1}, #opname "16" #arr, #hw_format "[!0r+!1r<<!2d+!3d]" }, \
237{ kX86 ## opname ## 32 ## reg, reg_kind, reg_flags | w_flags | sets_ccodes, { 0, 0, 0xF7, 0, 0, modrm, 0, imm << 2}, #opname "32" #reg, #w_format "!0r" }, \
238{ kX86 ## opname ## 32 ## mem, mem_kind, IS_LOAD | is_store | mem_flags | w_flags | sets_ccodes, { 0, 0, 0xF7, 0, 0, modrm, 0, imm << 2}, #opname "32" #mem, #w_format "[!0r+!1d]" }, \
239{ kX86 ## opname ## 32 ## arr, arr_kind, IS_LOAD | is_store | arr_flags | w_flags | sets_ccodes, { 0, 0, 0xF7, 0, 0, modrm, 0, imm << 2}, #opname "32" #arr, #w_format "[!0r+!1r<<!2d+!3d]" }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800240
jeffhaoe2962482012-06-28 11:29:57 -0700241 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, "", "", ""),
242 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, "", "", ""),
243
244 UNARY_ENCODING_MAP(Mul, 0x4, 0, SETS_CCODES, DaR, kRegRegReg, IS_UNARY_OP | REG_USE0, DaM, kRegRegMem, IS_BINARY_OP | REG_USE0, DaA, kRegRegArray, IS_QUAD_OP | REG_USE01, 0, REG_DEFA_USEA, REG_DEFAD_USEA, REG_DEFAD_USEA, "ax,al,", "dx:ax,ax,", "edx:eax,eax,"),
245 UNARY_ENCODING_MAP(Imul, 0x5, 0, SETS_CCODES, DaR, kRegRegReg, IS_UNARY_OP | REG_USE0, DaM, kRegRegMem, IS_BINARY_OP | REG_USE0, DaA, kRegRegArray, IS_QUAD_OP | REG_USE01, 0, REG_DEFA_USEA, REG_DEFAD_USEA, REG_DEFAD_USEA, "ax,al,", "dx:ax,ax,", "edx:eax,eax,"),
246 UNARY_ENCODING_MAP(Divmod, 0x6, 0, SETS_CCODES, DaR, kRegRegReg, IS_UNARY_OP | REG_USE0, DaM, kRegRegMem, IS_BINARY_OP | REG_USE0, DaA, kRegRegArray, IS_QUAD_OP | REG_USE01, 0, REG_DEFA_USEA, REG_DEFAD_USEAD, REG_DEFAD_USEAD, "ah:al,ax,", "dx:ax,dx:ax,", "edx:eax,edx:eax,"),
247 UNARY_ENCODING_MAP(Idivmod, 0x7, 0, SETS_CCODES, DaR, kRegRegReg, IS_UNARY_OP | REG_USE0, DaM, kRegRegMem, IS_BINARY_OP | REG_USE0, DaA, kRegRegArray, IS_QUAD_OP | REG_USE01, 0, REG_DEFA_USEA, REG_DEFAD_USEAD, REG_DEFAD_USEAD, "ah:al,ax,", "dx:ax,dx:ax,", "edx:eax,edx:eax,"),
Ian Rogersb5d09b22012-03-06 22:14:17 -0800248#undef UNARY_ENCODING_MAP
249
jeffhaoe2962482012-06-28 11:29:57 -0700250#define EXT_0F_ENCODING_MAP(opname, prefix, opcode, reg_def) \
251{ kX86 ## opname ## RR, kRegReg, IS_BINARY_OP | reg_def | REG_USE01, { prefix, 0, 0x0F, opcode, 0, 0, 0, 0 }, #opname "RR", "!0r,!1r" }, \
252{ kX86 ## opname ## RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | reg_def | REG_USE01, { prefix, 0, 0x0F, opcode, 0, 0, 0, 0 }, #opname "RM", "!0r,[!1r+!2d]" }, \
253{ kX86 ## opname ## RA, kRegArray, IS_LOAD | IS_QUIN_OP | reg_def | REG_USE012, { prefix, 0, 0x0F, opcode, 0, 0, 0, 0 }, #opname "RA", "!0r,[!1r+!2r<<!3d+!4d]" }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800254
jeffhaoe2962482012-06-28 11:29:57 -0700255 EXT_0F_ENCODING_MAP(Movsd, 0xF2, 0x10, REG_DEF0),
256 { kX86MovsdMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0xF2, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovsdMR", "[!0r+!1d],!2r" },
257 { kX86MovsdAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0xF2, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovsdAR", "[!0r+!1r<<!2d+!3d],!4r" },
Ian Rogersb5d09b22012-03-06 22:14:17 -0800258
jeffhaoe2962482012-06-28 11:29:57 -0700259 EXT_0F_ENCODING_MAP(Movss, 0xF3, 0x10, REG_DEF0),
260 { kX86MovssMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0xF3, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovssMR", "[!0r+!1d],!2r" },
261 { kX86MovssAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0xF3, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovssAR", "[!0r+!1r<<!2d+!3d],!4r" },
Ian Rogersb5d09b22012-03-06 22:14:17 -0800262
jeffhaoe2962482012-06-28 11:29:57 -0700263 EXT_0F_ENCODING_MAP(Cvtsi2sd, 0xF2, 0x2A, REG_DEF0),
264 EXT_0F_ENCODING_MAP(Cvtsi2ss, 0xF3, 0x2A, REG_DEF0),
265 EXT_0F_ENCODING_MAP(Cvttsd2si, 0xF2, 0x2C, REG_DEF0),
266 EXT_0F_ENCODING_MAP(Cvttss2si, 0xF3, 0x2C, REG_DEF0),
267 EXT_0F_ENCODING_MAP(Cvtsd2si, 0xF2, 0x2D, REG_DEF0),
268 EXT_0F_ENCODING_MAP(Cvtss2si, 0xF3, 0x2D, REG_DEF0),
269 EXT_0F_ENCODING_MAP(Ucomisd, 0x66, 0x2E, SETS_CCODES),
270 EXT_0F_ENCODING_MAP(Ucomiss, 0x00, 0x2E, SETS_CCODES),
271 EXT_0F_ENCODING_MAP(Comisd, 0x66, 0x2F, SETS_CCODES),
272 EXT_0F_ENCODING_MAP(Comiss, 0x00, 0x2F, SETS_CCODES),
273 EXT_0F_ENCODING_MAP(Orps, 0x00, 0x56, REG_DEF0),
274 EXT_0F_ENCODING_MAP(Xorps, 0x00, 0x57, REG_DEF0),
275 EXT_0F_ENCODING_MAP(Addsd, 0xF2, 0x58, REG_DEF0),
276 EXT_0F_ENCODING_MAP(Addss, 0xF3, 0x58, REG_DEF0),
277 EXT_0F_ENCODING_MAP(Mulsd, 0xF2, 0x59, REG_DEF0),
278 EXT_0F_ENCODING_MAP(Mulss, 0xF3, 0x59, REG_DEF0),
279 EXT_0F_ENCODING_MAP(Cvtsd2ss, 0xF2, 0x5A, REG_DEF0),
280 EXT_0F_ENCODING_MAP(Cvtss2sd, 0xF3, 0x5A, REG_DEF0),
281 EXT_0F_ENCODING_MAP(Subsd, 0xF2, 0x5C, REG_DEF0),
282 EXT_0F_ENCODING_MAP(Subss, 0xF3, 0x5C, REG_DEF0),
283 EXT_0F_ENCODING_MAP(Divsd, 0xF2, 0x5E, REG_DEF0),
284 EXT_0F_ENCODING_MAP(Divss, 0xF3, 0x5E, REG_DEF0),
Ian Rogersb5d09b22012-03-06 22:14:17 -0800285
jeffhaofdffdf82012-07-11 16:08:43 -0700286 { kX86PsrlqRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x73, 0, 2, 0, 1 }, "PsrlqRI", "!0r,!1d" },
jeffhaoe2962482012-06-28 11:29:57 -0700287 { kX86PsllqRI, kRegImm, IS_BINARY_OP | REG_DEF0_USE0, { 0x66, 0, 0x0F, 0x73, 0, 6, 0, 1 }, "PsllqRI", "!0r,!1d" },
Ian Rogersb41b33b2012-03-20 14:22:54 -0700288
jeffhaoe2962482012-06-28 11:29:57 -0700289 EXT_0F_ENCODING_MAP(Movdxr, 0x66, 0x6E, REG_DEF0),
jeffhaofdffdf82012-07-11 16:08:43 -0700290 { kX86MovdrxRR, kRegRegStore, IS_BINARY_OP | REG_DEF0 | REG_USE01, { 0x66, 0, 0x0F, 0x7E, 0, 0, 0, 0 }, "MovdrxRR", "!0r,!1r" },
291 { kX86MovdrxMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, 0, 0x0F, 0x7E, 0, 0, 0, 0 }, "MovdrxMR", "[!0r+!1d],!2r" },
292 { kX86MovdrxAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014, { 0x66, 0, 0x0F, 0x7E, 0, 0, 0, 0 }, "MovdrxAR", "[!0r+!1r<<!2d+!3d],!4r" },
Ian Rogersb5d09b22012-03-06 22:14:17 -0800293
jeffhaoe2962482012-06-28 11:29:57 -0700294 { kX86Set8R, kRegCond, IS_BINARY_OP | REG_DEF0 | USES_CCODES, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0 }, "Set8R", "!1c !0r" },
295 { kX86Set8M, kMemCond, IS_STORE | IS_TERTIARY_OP | REG_USE0 | USES_CCODES, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0 }, "Set8M", "!2c [!0r+!1d]" },
296 { kX86Set8A, kArrayCond, IS_STORE | IS_QUIN_OP | REG_USE01 | USES_CCODES, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0 }, "Set8A", "!4c [!0r+!1r<<!2d+!3d]" },
Ian Rogersb5d09b22012-03-06 22:14:17 -0800297
Ian Rogersc6f3bb82012-03-21 20:40:33 -0700298 // TODO: load/store?
299 // Encode the modrm opcode as an extra opcode byte to avoid computation during assembly.
300 { kX86Mfence, kReg, NO_OPERAND, { 0, 0, 0x0F, 0xAE, 0, 6, 0, 0 }, "Mfence", "" },
301
jeffhaoe2962482012-06-28 11:29:57 -0700302 EXT_0F_ENCODING_MAP(Imul16, 0x66, 0xAF, REG_DEF0 | SETS_CCODES),
303 EXT_0F_ENCODING_MAP(Imul32, 0x00, 0xAF, REG_DEF0 | SETS_CCODES),
jeffhao83025762012-08-02 11:08:56 -0700304
305 { kX86CmpxchgRR, kRegRegStore, IS_BINARY_OP | REG_DEF0 | REG_USE01 | REG_DEFA_USEA | SETS_CCODES, { 0, 0, 0x0F, 0xB1, 0, 0, 0, 0 }, "Cmpxchg", "!0r,!1r" },
306 { kX86CmpxchgMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02 | REG_DEFA_USEA | SETS_CCODES, { 0, 0, 0x0F, 0xB1, 0, 0, 0, 0 }, "Cmpxchg", "[!0r+!1d],!2r" },
307 { kX86CmpxchgAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014 | REG_DEFA_USEA | SETS_CCODES, { 0, 0, 0x0F, 0xB1, 0, 0, 0, 0 }, "Cmpxchg", "[!0r+!1r<<!2d+!3d],!4r" },
308 { kX86LockCmpxchgRR, kRegRegStore, IS_BINARY_OP | REG_DEF0 | REG_USE01 | REG_DEFA_USEA | SETS_CCODES, { 0xF0, 0, 0x0F, 0xB1, 0, 0, 0, 0 }, "Lock Cmpxchg", "!0r,!1r" },
309 { kX86LockCmpxchgMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02 | REG_DEFA_USEA | SETS_CCODES, { 0xF0, 0, 0x0F, 0xB1, 0, 0, 0, 0 }, "Lock Cmpxchg", "[!0r+!1d],!2r" },
310 { kX86LockCmpxchgAR, kArrayReg, IS_STORE | IS_QUIN_OP | REG_USE014 | REG_DEFA_USEA | SETS_CCODES, { 0xF0, 0, 0x0F, 0xB1, 0, 0, 0, 0 }, "Lock Cmpxchg", "[!0r+!1r<<!2d+!3d],!4r" },
311
jeffhaoe2962482012-06-28 11:29:57 -0700312 EXT_0F_ENCODING_MAP(Movzx8, 0x00, 0xB6, REG_DEF0),
313 EXT_0F_ENCODING_MAP(Movzx16, 0x00, 0xB7, REG_DEF0),
314 EXT_0F_ENCODING_MAP(Movsx8, 0x00, 0xBE, REG_DEF0),
315 EXT_0F_ENCODING_MAP(Movsx16, 0x00, 0xBF, REG_DEF0),
Ian Rogersb5d09b22012-03-06 22:14:17 -0800316#undef EXT_0F_ENCODING_MAP
317
jeffhaoe2962482012-06-28 11:29:57 -0700318 { kX86Jcc8, kJcc, IS_BINARY_OP | IS_BRANCH | NEEDS_FIXUP | USES_CCODES, { 0, 0, 0x70, 0, 0, 0, 0, 0 }, "Jcc8", "!1c !0t" },
319 { kX86Jcc32, kJcc, IS_BINARY_OP | IS_BRANCH | NEEDS_FIXUP | USES_CCODES, { 0, 0, 0x0F, 0x80, 0, 0, 0, 0 }, "Jcc32", "!1c !0t" },
320 { kX86Jmp8, kJmp, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0xEB, 0, 0, 0, 0, 0 }, "Jmp8", "!0t" },
321 { kX86Jmp32, kJmp, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0xE9, 0, 0, 0, 0, 0 }, "Jmp32", "!0t" },
322 { kX86JmpR, kJmp, IS_UNARY_OP | IS_BRANCH | REG_USE0, { 0, 0, 0xFF, 0, 0, 4, 0, 0 }, "JmpR", "!0r" },
323 { kX86CallR, kCall, IS_UNARY_OP | IS_BRANCH | REG_USE0, { 0, 0, 0xE8, 0, 0, 0, 0, 0 }, "CallR", "!0r" },
324 { kX86CallM, kCall, IS_BINARY_OP | IS_BRANCH | IS_LOAD | REG_USE0, { 0, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallM", "[!0r+!1d]" },
325 { kX86CallA, kCall, IS_QUAD_OP | IS_BRANCH | IS_LOAD | REG_USE01, { 0, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallA", "[!0r+!1r<<!2d+!3d]" },
326 { kX86CallT, kCall, IS_UNARY_OP | IS_BRANCH | IS_LOAD, { THREAD_PREFIX, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallT", "fs:[!0d]" },
327 { kX86Ret, kNullary,NO_OPERAND | IS_BRANCH, { 0, 0, 0xC3, 0, 0, 0, 0, 0 }, "Ret", "" },
Ian Rogers7caad772012-03-30 01:07:54 -0700328
jeffhaoe2962482012-06-28 11:29:57 -0700329 { kX86StartOfMethod, kMacro, IS_UNARY_OP | SETS_CCODES, { 0, 0, 0, 0, 0, 0, 0, 0 }, "StartOfMethod", "!0r" },
330 { kX86PcRelLoadRA, kPcRel, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "PcRelLoadRA", "!0r,[!1r+!2r<<!3d+!4p]" },
331 { kX86PcRelAdr, kPcRel, IS_LOAD | IS_BINARY_OP | REG_DEF0, { 0, 0, 0xB8, 0, 0, 0, 0, 4 }, "PcRelAdr", "!0r,!1d" },
buzbeee88dfbf2012-03-05 11:19:57 -0800332};
333
Ian Rogersb5d09b22012-03-06 22:14:17 -0800334static size_t computeSize(X86EncodingMap* entry, int displacement, bool has_sib) {
335 size_t size = 0;
336 if (entry->skeleton.prefix1 > 0) {
337 ++size;
338 if (entry->skeleton.prefix2 > 0) {
339 ++size;
Ian Rogersde797832012-03-06 10:18:10 -0800340 }
Ian Rogersde797832012-03-06 10:18:10 -0800341 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800342 ++size; // opcode
343 if (entry->skeleton.opcode == 0x0F) {
344 ++size;
345 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
346 ++size;
347 }
348 }
349 ++size; // modrm
350 if (has_sib) {
351 ++size;
352 }
353 if (displacement != 0) {
354 if (entry->opcode != kX86Lea32RA) {
Ian Rogers7caad772012-03-30 01:07:54 -0700355 DCHECK_NE(entry->flags & (IS_LOAD | IS_STORE), 0) << entry->name;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800356 }
357 size += IS_SIMM8(displacement) ? 1 : 4;
358 }
359 size += entry->skeleton.immediate_bytes;
360 return size;
361}
362
363int oatGetInsnSize(LIR* lir) {
364 X86EncodingMap* entry = &EncodingMap[lir->opcode];
365 switch (entry->kind) {
366 case kData:
367 return 4; // 4 bytes of data
368 case kNop:
369 return lir->operands[0]; // length of nop is sole operand
370 case kNullary:
371 return 1; // 1 byte of opcode
372 case kReg: // lir operands - 0: reg
373 return computeSize(entry, 0, false);
374 case kMem: { // lir operands - 0: base, 1: disp
375 int base = lir->operands[0];
jeffhao703f2cd2012-07-13 17:25:52 -0700376 int disp = lir->operands[1];
377 // SP requires a special extra SIB byte. BP requires explicit disp,
378 // so add a byte for disp 0 which would normally be omitted.
379 return computeSize(entry, disp, false) + ((base == rSP) || (base == rBP && disp == 0) ? 1 : 0);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800380 }
381 case kArray: // lir operands - 0: base, 1: index, 2: scale, 3: disp
382 return computeSize(entry, lir->operands[3], true);
383 case kMemReg: { // lir operands - 0: base, 1: disp, 2: reg
384 int base = lir->operands[0];
jeffhao703f2cd2012-07-13 17:25:52 -0700385 int disp = lir->operands[1];
386 // SP requires a special extra SIB byte. BP requires explicit disp,
387 // so add a byte for disp 0 which would normally be omitted.
388 return computeSize(entry, disp, false) + ((base == rSP) || (base == rBP && disp == 0) ? 1 : 0);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800389 }
390 case kArrayReg: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
391 return computeSize(entry, lir->operands[3], true);
392 case kThreadReg: // lir operands - 0: disp, 1: reg
393 return computeSize(entry, lir->operands[0], false);
394 case kRegReg:
395 return computeSize(entry, 0, false);
jeffhaofdffdf82012-07-11 16:08:43 -0700396 case kRegRegStore:
397 return computeSize(entry, 0, false);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800398 case kRegMem: { // lir operands - 0: reg, 1: base, 2: disp
399 int base = lir->operands[1];
jeffhao703f2cd2012-07-13 17:25:52 -0700400 int disp = lir->operands[2];
401 // SP requires a special extra SIB byte. BP requires explicit disp,
402 // so add a byte for disp 0 which would normally be omitted.
403 return computeSize(entry, disp, false) + ((base == rSP) || (base == rBP && disp == 0) ? 1 : 0);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800404 }
jeffhao703f2cd2012-07-13 17:25:52 -0700405 case kRegArray: { // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
406 int base = lir->operands[1];
407 int disp = lir->operands[4];
408 // BP requires explicit disp, so add a byte for disp 0 which would normally be omitted.
409 return computeSize(entry, disp, true) + ((base == rBP && disp == 0) ? 1 : 0);
410 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800411 case kRegThread: // lir operands - 0: reg, 1: disp
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700412 return computeSize(entry, 0x12345678, false); // displacement size is always 32bit
Ian Rogersb5d09b22012-03-06 22:14:17 -0800413 case kRegImm: { // lir operands - 0: reg, 1: immediate
Ian Rogersb41b33b2012-03-20 14:22:54 -0700414 size_t size = computeSize(entry, 0, false);
415 if (entry->skeleton.ax_opcode == 0) {
416 return size;
417 } else {
418 // AX opcodes don't require the modrm byte.
419 int reg = lir->operands[0];
420 return size - (reg == rAX ? 1 : 0);
421 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800422 }
423 case kMemImm: // lir operands - 0: base, 1: disp, 2: immediate
424 CHECK_NE(lir->operands[0], static_cast<int>(rSP)); // TODO: add extra SIB byte
425 return computeSize(entry, lir->operands[1], false);
426 case kArrayImm: // lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
427 return computeSize(entry, lir->operands[3], true);
428 case kThreadImm: // lir operands - 0: disp, 1: imm
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700429 return computeSize(entry, 0x12345678, false); // displacement size is always 32bit
Ian Rogersb5d09b22012-03-06 22:14:17 -0800430 case kRegRegImm: // lir operands - 0: reg, 1: reg, 2: imm
431 return computeSize(entry, 0, false);
432 case kRegMemImm: // lir operands - 0: reg, 1: base, 2: disp, 3: imm
433 CHECK_NE(lir->operands[1], static_cast<int>(rSP)); // TODO: add extra SIB byte
434 return computeSize(entry, lir->operands[2], false);
435 case kRegArrayImm: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp, 5: imm
436 return computeSize(entry, lir->operands[4], true);
437 case kMovRegImm: // lir operands - 0: reg, 1: immediate
438 return 1 + entry->skeleton.immediate_bytes;
439 case kShiftRegImm: // lir operands - 0: reg, 1: immediate
440 // Shift by immediate one has a shorter opcode.
441 return computeSize(entry, 0, false) - (lir->operands[1] == 1 ? 1 : 0);
442 case kShiftMemImm: // lir operands - 0: base, 1: disp, 2: immediate
443 CHECK_NE(lir->operands[0], static_cast<int>(rSP)); // TODO: add extra SIB byte
444 // Shift by immediate one has a shorter opcode.
445 return computeSize(entry, lir->operands[1], false) - (lir->operands[2] == 1 ? 1 : 0);
446 case kShiftArrayImm: // lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
447 // Shift by immediate one has a shorter opcode.
448 return computeSize(entry, lir->operands[3], true) - (lir->operands[4] == 1 ? 1 : 0);
449 case kShiftRegCl:
450 return computeSize(entry, 0, false);
451 case kShiftMemCl: // lir operands - 0: base, 1: disp, 2: cl
452 CHECK_NE(lir->operands[0], static_cast<int>(rSP)); // TODO: add extra SIB byte
453 return computeSize(entry, lir->operands[1], false);
454 case kShiftArrayCl: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
455 return computeSize(entry, lir->operands[3], true);
456 case kRegCond: // lir operands - 0: reg, 1: cond
457 return computeSize(entry, 0, false);
458 case kMemCond: // lir operands - 0: base, 1: disp, 2: cond
459 CHECK_NE(lir->operands[0], static_cast<int>(rSP)); // TODO: add extra SIB byte
460 return computeSize(entry, lir->operands[1], false);
461 case kArrayCond: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: cond
462 return computeSize(entry, lir->operands[3], true);
Ian Rogersb41b33b2012-03-20 14:22:54 -0700463 case kJcc:
464 if (lir->opcode == kX86Jcc8) {
465 return 2; // opcode + rel8
466 } else {
467 DCHECK(lir->opcode == kX86Jcc32);
468 return 6; // 2 byte opcode + rel32
469 }
470 case kJmp:
471 if (lir->opcode == kX86Jmp8) {
472 return 2; // opcode + rel8
Ian Rogers7caad772012-03-30 01:07:54 -0700473 } else if (lir->opcode == kX86Jmp32) {
Ian Rogersb41b33b2012-03-20 14:22:54 -0700474 return 5; // opcode + rel32
Ian Rogers7caad772012-03-30 01:07:54 -0700475 } else {
476 DCHECK(lir->opcode == kX86JmpR);
477 return 2; // opcode + modrm
Ian Rogersb41b33b2012-03-20 14:22:54 -0700478 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800479 case kCall:
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700480 switch (lir->opcode) {
Ian Rogersb5d09b22012-03-06 22:14:17 -0800481 case kX86CallR: return 2; // opcode modrm
482 case kX86CallM: // lir operands - 0: base, 1: disp
483 return computeSize(entry, lir->operands[1], false);
484 case kX86CallA: // lir operands - 0: base, 1: index, 2: scale, 3: disp
485 return computeSize(entry, lir->operands[3], true);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700486 case kX86CallT: // lir operands - 0: disp
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700487 return computeSize(entry, 0x12345678, false); // displacement size is always 32bit
Ian Rogersb5d09b22012-03-06 22:14:17 -0800488 default:
489 break;
490 }
491 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700492 case kPcRel:
493 if (entry->opcode == kX86PcRelLoadRA) {
494 // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
495 return computeSize(entry, 0x12345678, true);
496 } else {
497 DCHECK(entry->opcode == kX86PcRelAdr);
498 return 5; // opcode with reg + 4 byte immediate
499 }
500 case kMacro:
501 DCHECK_EQ(lir->opcode, static_cast<int>(kX86StartOfMethod));
502 return 5 /* call opcode + 4 byte displacement */ + 1 /* pop reg */ +
503 computeSize(&EncodingMap[kX86Sub32RI], 0, false) -
504 (lir->operands[0] == rAX ? 1 : 0); // shorter ax encoding
Ian Rogersb5d09b22012-03-06 22:14:17 -0800505 default:
506 break;
507 }
508 UNIMPLEMENTED(FATAL) << "Unimplemented size encoding for: " << entry->name;
Ian Rogersde797832012-03-06 10:18:10 -0800509 return 0;
510}
buzbeee88dfbf2012-03-05 11:19:57 -0800511
jeffhao703f2cd2012-07-13 17:25:52 -0700512static uint8_t modrmForDisp(int base, int disp) {
513 // BP requires an explicit disp, so do not omit it in the 0 case
514 if (disp == 0 && base != rBP) {
Ian Rogersb5d09b22012-03-06 22:14:17 -0800515 return 0;
516 } else if (IS_SIMM8(disp)) {
517 return 1;
518 } else {
519 return 2;
520 }
521}
522
jeffhao703f2cd2012-07-13 17:25:52 -0700523static void emitDisp(CompilationUnit* cUnit, int base, int disp) {
524 // BP requires an explicit disp, so do not omit it in the 0 case
525 if (disp == 0 && base != rBP) {
Ian Rogersb5d09b22012-03-06 22:14:17 -0800526 return;
527 } else if (IS_SIMM8(disp)) {
528 cUnit->codeBuffer.push_back(disp & 0xFF);
529 } else {
530 cUnit->codeBuffer.push_back(disp & 0xFF);
531 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
532 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
533 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
534 }
535}
536
537static void emitOpReg(CompilationUnit* cUnit, const X86EncodingMap* entry, uint8_t reg) {
538 if (entry->skeleton.prefix1 != 0) {
539 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
540 if (entry->skeleton.prefix2 != 0) {
541 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
542 }
543 } else {
544 DCHECK_EQ(0, entry->skeleton.prefix2);
545 }
546 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
547 if (entry->skeleton.opcode == 0x0F) {
548 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
549 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
550 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
551 } else {
552 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
553 }
554 } else {
555 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
556 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
557 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700558 if (FPREG(reg)) {
559 reg = reg & FP_REG_MASK;
560 }
jeffhao703f2cd2012-07-13 17:25:52 -0700561 if (reg >= 4) {
562 DCHECK(strchr(entry->name, '8') == NULL) << entry->name << " " << (int) reg
563 << " in " << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
564 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800565 DCHECK_LT(reg, 8);
566 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
567 cUnit->codeBuffer.push_back(modrm);
568 DCHECK_EQ(0, entry->skeleton.ax_opcode);
569 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
570}
571
572static void emitOpMem(CompilationUnit* cUnit, const X86EncodingMap* entry, uint8_t base, int disp) {
573 if (entry->skeleton.prefix1 != 0) {
574 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
575 if (entry->skeleton.prefix2 != 0) {
576 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
577 }
578 } else {
579 DCHECK_EQ(0, entry->skeleton.prefix2);
580 }
581 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
582 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
583 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
584 DCHECK_LT(entry->skeleton.modrm_opcode, 8);
585 DCHECK_LT(base, 8);
jeffhao703f2cd2012-07-13 17:25:52 -0700586 uint8_t modrm = (modrmForDisp(base, disp) << 6) | (entry->skeleton.modrm_opcode << 3) | base;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800587 cUnit->codeBuffer.push_back(modrm);
jeffhao703f2cd2012-07-13 17:25:52 -0700588 emitDisp(cUnit, base, disp);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800589 DCHECK_EQ(0, entry->skeleton.ax_opcode);
590 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
591}
592
593static void emitMemReg(CompilationUnit* cUnit, const X86EncodingMap* entry,
594 uint8_t base, int disp, uint8_t reg) {
595 if (entry->skeleton.prefix1 != 0) {
596 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
597 if (entry->skeleton.prefix2 != 0) {
598 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
599 }
600 } else {
601 DCHECK_EQ(0, entry->skeleton.prefix2);
602 }
603 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
604 if (entry->skeleton.opcode == 0x0F) {
605 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
606 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
607 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
608 } else {
609 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
610 }
611 } else {
612 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
613 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
614 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700615 if (FPREG(reg)) {
616 reg = reg & FP_REG_MASK;
617 }
jeffhao703f2cd2012-07-13 17:25:52 -0700618 if (reg >= 4) {
619 DCHECK(strchr(entry->name, '8') == NULL) << entry->name << " " << (int) reg
620 << " in " << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
621 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800622 DCHECK_LT(reg, 8);
623 DCHECK_LT(base, 8);
jeffhao703f2cd2012-07-13 17:25:52 -0700624 uint8_t modrm = (modrmForDisp(base, disp) << 6) | (reg << 3) | base;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800625 cUnit->codeBuffer.push_back(modrm);
626 if (base == rSP) {
627 // Special SIB for SP base
628 cUnit->codeBuffer.push_back(0 << 6 | (rSP << 3) | rSP);
629 }
jeffhao703f2cd2012-07-13 17:25:52 -0700630 emitDisp(cUnit, base, disp);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800631 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
632 DCHECK_EQ(0, entry->skeleton.ax_opcode);
633 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
634}
635
636static void emitRegMem(CompilationUnit* cUnit, const X86EncodingMap* entry,
637 uint8_t reg, uint8_t base, int disp) {
638 // Opcode will flip operands.
639 emitMemReg(cUnit, entry, base, disp, reg);
640}
641
642static void emitRegArray(CompilationUnit* cUnit, const X86EncodingMap* entry, uint8_t reg,
643 uint8_t base, uint8_t index, int scale, int disp) {
644 if (entry->skeleton.prefix1 != 0) {
645 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
646 if (entry->skeleton.prefix2 != 0) {
647 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
648 }
649 } else {
650 DCHECK_EQ(0, entry->skeleton.prefix2);
651 }
652 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
653 if (entry->skeleton.opcode == 0x0F) {
654 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
655 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
656 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
657 } else {
658 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
659 }
660 } else {
661 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
662 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
663 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700664 if (FPREG(reg)) {
665 reg = reg & FP_REG_MASK;
666 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800667 DCHECK_LT(reg, 8);
jeffhao703f2cd2012-07-13 17:25:52 -0700668 uint8_t modrm = (modrmForDisp(base, disp) << 6) | (reg << 3) | rSP;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800669 cUnit->codeBuffer.push_back(modrm);
670 DCHECK_LT(scale, 4);
671 DCHECK_LT(index, 8);
672 DCHECK_LT(base, 8);
673 uint8_t sib = (scale << 6) | (index << 3) | base;
674 cUnit->codeBuffer.push_back(sib);
jeffhao703f2cd2012-07-13 17:25:52 -0700675 emitDisp(cUnit, base, disp);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800676 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
677 DCHECK_EQ(0, entry->skeleton.ax_opcode);
678 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
679}
680
Ian Rogersb41b33b2012-03-20 14:22:54 -0700681static void emitArrayReg(CompilationUnit* cUnit, const X86EncodingMap* entry,
682 uint8_t base, uint8_t index, int scale, int disp, uint8_t reg) {
683 // Opcode will flip operands.
684 emitRegArray(cUnit, entry, reg, base, index, scale, disp);
685}
686
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700687static void emitRegThread(CompilationUnit* cUnit, const X86EncodingMap* entry,
688 uint8_t reg, int disp) {
689 DCHECK_NE(entry->skeleton.prefix1, 0);
690 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
691 if (entry->skeleton.prefix2 != 0) {
692 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
693 }
694 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
695 if (entry->skeleton.opcode == 0x0F) {
696 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
697 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
698 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
699 } else {
700 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
701 }
702 } else {
703 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
704 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
705 }
706 if (FPREG(reg)) {
707 reg = reg & FP_REG_MASK;
708 }
jeffhao703f2cd2012-07-13 17:25:52 -0700709 if (reg >= 4) {
710 DCHECK(strchr(entry->name, '8') == NULL) << entry->name << " " << (int) reg
711 << " in " << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
712 }
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700713 DCHECK_LT(reg, 8);
714 uint8_t modrm = (0 << 6) | (reg << 3) | rBP;
715 cUnit->codeBuffer.push_back(modrm);
716 cUnit->codeBuffer.push_back(disp & 0xFF);
717 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
718 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
719 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
720 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
721 DCHECK_EQ(0, entry->skeleton.ax_opcode);
722 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
723}
724
Ian Rogersb5d09b22012-03-06 22:14:17 -0800725static void emitRegReg(CompilationUnit* cUnit, const X86EncodingMap* entry,
726 uint8_t reg1, uint8_t reg2) {
727 if (entry->skeleton.prefix1 != 0) {
728 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
729 if (entry->skeleton.prefix2 != 0) {
730 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
731 }
732 } else {
733 DCHECK_EQ(0, entry->skeleton.prefix2);
734 }
735 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
736 if (entry->skeleton.opcode == 0x0F) {
737 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
738 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
739 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
740 } else {
741 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
742 }
743 } else {
744 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
745 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
746 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700747 if (FPREG(reg1)) {
748 reg1 = reg1 & FP_REG_MASK;
749 }
750 if (FPREG(reg2)) {
751 reg2 = reg2 & FP_REG_MASK;
752 }
753 DCHECK_LT(reg1, 8);
754 DCHECK_LT(reg2, 8);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800755 uint8_t modrm = (3 << 6) | (reg1 << 3) | reg2;
756 cUnit->codeBuffer.push_back(modrm);
757 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
758 DCHECK_EQ(0, entry->skeleton.ax_opcode);
759 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
760}
761
Elliott Hughes225ae522012-04-16 20:21:45 -0700762static void emitRegRegImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
763 uint8_t reg1, uint8_t reg2, int32_t imm) {
764 if (entry->skeleton.prefix1 != 0) {
765 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
766 if (entry->skeleton.prefix2 != 0) {
767 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
768 }
769 } else {
770 DCHECK_EQ(0, entry->skeleton.prefix2);
771 }
772 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
773 if (entry->skeleton.opcode == 0x0F) {
774 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
775 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
776 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
777 } else {
778 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
779 }
780 } else {
781 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
782 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
783 }
784 if (FPREG(reg1)) {
785 reg1 = reg1 & FP_REG_MASK;
786 }
787 if (FPREG(reg2)) {
788 reg2 = reg2 & FP_REG_MASK;
789 }
790 DCHECK_LT(reg1, 8);
791 DCHECK_LT(reg2, 8);
792 uint8_t modrm = (3 << 6) | (reg1 << 3) | reg2;
793 cUnit->codeBuffer.push_back(modrm);
794 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
795 DCHECK_EQ(0, entry->skeleton.ax_opcode);
796 switch (entry->skeleton.immediate_bytes) {
797 case 1:
798 DCHECK(IS_SIMM8(imm));
799 cUnit->codeBuffer.push_back(imm & 0xFF);
800 break;
801 case 2:
802 DCHECK(IS_SIMM16(imm));
803 cUnit->codeBuffer.push_back(imm & 0xFF);
804 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
805 break;
806 case 4:
807 cUnit->codeBuffer.push_back(imm & 0xFF);
808 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
809 cUnit->codeBuffer.push_back((imm >> 16) & 0xFF);
810 cUnit->codeBuffer.push_back((imm >> 24) & 0xFF);
811 break;
812 default:
813 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
814 << ") for instruction: " << entry->name;
815 break;
816 }
817}
818
Ian Rogersb5d09b22012-03-06 22:14:17 -0800819static void emitRegImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
820 uint8_t reg, int imm) {
821 if (entry->skeleton.prefix1 != 0) {
822 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
823 if (entry->skeleton.prefix2 != 0) {
824 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
825 }
826 } else {
827 DCHECK_EQ(0, entry->skeleton.prefix2);
828 }
829 if (reg == rAX && entry->skeleton.ax_opcode != 0) {
830 cUnit->codeBuffer.push_back(entry->skeleton.ax_opcode);
831 } else {
832 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
833 if (entry->skeleton.opcode == 0x0F) {
834 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
835 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
836 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
837 } else {
838 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
839 }
840 } else {
841 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
842 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
843 }
jeffhaofdffdf82012-07-11 16:08:43 -0700844 if (FPREG(reg)) {
845 reg = reg & FP_REG_MASK;
846 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800847 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
848 cUnit->codeBuffer.push_back(modrm);
849 }
850 switch (entry->skeleton.immediate_bytes) {
851 case 1:
852 DCHECK(IS_SIMM8(imm));
853 cUnit->codeBuffer.push_back(imm & 0xFF);
854 break;
855 case 2:
856 DCHECK(IS_SIMM16(imm));
857 cUnit->codeBuffer.push_back(imm & 0xFF);
858 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
859 break;
860 case 4:
861 cUnit->codeBuffer.push_back(imm & 0xFF);
862 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
863 cUnit->codeBuffer.push_back((imm >> 16) & 0xFF);
864 cUnit->codeBuffer.push_back((imm >> 24) & 0xFF);
865 break;
866 default:
867 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
868 << ") for instruction: " << entry->name;
869 break;
870 }
871}
872
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700873static void emitThreadImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
874 int disp, int imm) {
875 if (entry->skeleton.prefix1 != 0) {
876 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
877 if (entry->skeleton.prefix2 != 0) {
878 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
879 }
880 } else {
881 DCHECK_EQ(0, entry->skeleton.prefix2);
882 }
883 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
884 if (entry->skeleton.opcode == 0x0F) {
885 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
886 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
887 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
888 } else {
889 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
890 }
891 } else {
892 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
893 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
894 }
895 uint8_t modrm = (0 << 6) | (entry->skeleton.modrm_opcode << 3) | rBP;
896 cUnit->codeBuffer.push_back(modrm);
897 cUnit->codeBuffer.push_back(disp & 0xFF);
898 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
899 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
900 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
901 switch (entry->skeleton.immediate_bytes) {
902 case 1:
903 DCHECK(IS_SIMM8(imm));
904 cUnit->codeBuffer.push_back(imm & 0xFF);
905 break;
906 case 2:
907 DCHECK(IS_SIMM16(imm));
908 cUnit->codeBuffer.push_back(imm & 0xFF);
909 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
910 break;
911 case 4:
912 cUnit->codeBuffer.push_back(imm & 0xFF);
913 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
914 cUnit->codeBuffer.push_back((imm >> 16) & 0xFF);
915 cUnit->codeBuffer.push_back((imm >> 24) & 0xFF);
916 break;
917 default:
918 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
919 << ") for instruction: " << entry->name;
920 break;
921 }
922 DCHECK_EQ(entry->skeleton.ax_opcode, 0);
923}
924
925static void emitMovRegImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
926 uint8_t reg, int imm) {
927 DCHECK_LT(reg, 8);
928 cUnit->codeBuffer.push_back(0xB8 + reg);
929 cUnit->codeBuffer.push_back(imm & 0xFF);
930 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
931 cUnit->codeBuffer.push_back((imm >> 16) & 0xFF);
932 cUnit->codeBuffer.push_back((imm >> 24) & 0xFF);
933}
934
Ian Rogersb41b33b2012-03-20 14:22:54 -0700935static void emitShiftRegImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
Ian Rogers7caad772012-03-30 01:07:54 -0700936 uint8_t reg, int imm) {
Ian Rogersb41b33b2012-03-20 14:22:54 -0700937 if (entry->skeleton.prefix1 != 0) {
938 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
939 if (entry->skeleton.prefix2 != 0) {
940 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
941 }
942 } else {
943 DCHECK_EQ(0, entry->skeleton.prefix2);
944 }
945 if (imm != 1) {
946 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
947 } else {
948 // Shorter encoding for 1 bit shift
949 cUnit->codeBuffer.push_back(entry->skeleton.ax_opcode);
950 }
951 if (entry->skeleton.opcode == 0x0F) {
952 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
953 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
954 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
955 } else {
956 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
957 }
958 } else {
959 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
960 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
961 }
jeffhao703f2cd2012-07-13 17:25:52 -0700962 if (reg >= 4) {
963 DCHECK(strchr(entry->name, '8') == NULL) << entry->name << " " << (int) reg
964 << " in " << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
965 }
Ian Rogersb41b33b2012-03-20 14:22:54 -0700966 DCHECK_LT(reg, 8);
Ian Rogers7caad772012-03-30 01:07:54 -0700967 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
Ian Rogersb41b33b2012-03-20 14:22:54 -0700968 cUnit->codeBuffer.push_back(modrm);
969 if (imm != 1) {
970 DCHECK_EQ(entry->skeleton.immediate_bytes, 1);
971 DCHECK(IS_SIMM8(imm));
972 cUnit->codeBuffer.push_back(imm & 0xFF);
973 }
974}
975
Ian Rogers7caad772012-03-30 01:07:54 -0700976static void emitShiftRegCl(CompilationUnit* cUnit, const X86EncodingMap* entry,
977 uint8_t reg, uint8_t cl) {
978 DCHECK_EQ(cl, static_cast<uint8_t>(rCX));
979 if (entry->skeleton.prefix1 != 0) {
980 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
981 if (entry->skeleton.prefix2 != 0) {
982 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
983 }
984 } else {
985 DCHECK_EQ(0, entry->skeleton.prefix2);
986 }
987 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
988 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
989 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
990 DCHECK_LT(reg, 8);
991 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
992 cUnit->codeBuffer.push_back(modrm);
993 DCHECK_EQ(0, entry->skeleton.ax_opcode);
994 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
995}
996
997static void emitRegCond(CompilationUnit* cUnit, const X86EncodingMap* entry,
998 uint8_t reg, uint8_t condition) {
999 if (entry->skeleton.prefix1 != 0) {
1000 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
1001 if (entry->skeleton.prefix2 != 0) {
1002 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
1003 }
1004 } else {
1005 DCHECK_EQ(0, entry->skeleton.prefix2);
1006 }
1007 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1008 DCHECK_EQ(0x0F, entry->skeleton.opcode);
1009 cUnit->codeBuffer.push_back(0x0F);
1010 DCHECK_EQ(0x90, entry->skeleton.extra_opcode1);
1011 cUnit->codeBuffer.push_back(0x90 | condition);
1012 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1013 DCHECK_LT(reg, 8);
1014 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
1015 cUnit->codeBuffer.push_back(modrm);
1016 DCHECK_EQ(entry->skeleton.immediate_bytes, 0);
1017}
1018
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001019static void emitJmp(CompilationUnit* cUnit, const X86EncodingMap* entry, int rel) {
Ian Rogersb41b33b2012-03-20 14:22:54 -07001020 if (entry->opcode == kX86Jmp8) {
1021 DCHECK(IS_SIMM8(rel));
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001022 cUnit->codeBuffer.push_back(0xEB);
1023 cUnit->codeBuffer.push_back(rel & 0xFF);
Ian Rogers7caad772012-03-30 01:07:54 -07001024 } else if (entry->opcode == kX86Jmp32) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001025 cUnit->codeBuffer.push_back(0xE9);
1026 cUnit->codeBuffer.push_back(rel & 0xFF);
1027 cUnit->codeBuffer.push_back((rel >> 8) & 0xFF);
1028 cUnit->codeBuffer.push_back((rel >> 16) & 0xFF);
1029 cUnit->codeBuffer.push_back((rel >> 24) & 0xFF);
Ian Rogers7caad772012-03-30 01:07:54 -07001030 } else {
1031 DCHECK(entry->opcode == kX86JmpR);
1032 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
1033 uint8_t reg = static_cast<uint8_t>(rel);
1034 DCHECK_LT(reg, 8);
1035 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
1036 cUnit->codeBuffer.push_back(modrm);
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001037 }
1038}
1039
1040static void emitJcc(CompilationUnit* cUnit, const X86EncodingMap* entry,
1041 int rel, uint8_t cc) {
1042 DCHECK_LT(cc, 16);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001043 if (entry->opcode == kX86Jcc8) {
1044 DCHECK(IS_SIMM8(rel));
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001045 cUnit->codeBuffer.push_back(0x70 | cc);
1046 cUnit->codeBuffer.push_back(rel & 0xFF);
1047 } else {
Ian Rogersb41b33b2012-03-20 14:22:54 -07001048 DCHECK(entry->opcode == kX86Jcc32);
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001049 cUnit->codeBuffer.push_back(0x0F);
1050 cUnit->codeBuffer.push_back(0x80 | cc);
1051 cUnit->codeBuffer.push_back(rel & 0xFF);
1052 cUnit->codeBuffer.push_back((rel >> 8) & 0xFF);
1053 cUnit->codeBuffer.push_back((rel >> 16) & 0xFF);
1054 cUnit->codeBuffer.push_back((rel >> 24) & 0xFF);
1055 }
1056}
1057
1058static void emitCallMem(CompilationUnit* cUnit, const X86EncodingMap* entry,
1059 uint8_t base, int disp) {
1060 if (entry->skeleton.prefix1 != 0) {
1061 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
1062 if (entry->skeleton.prefix2 != 0) {
1063 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
1064 }
1065 } else {
1066 DCHECK_EQ(0, entry->skeleton.prefix2);
1067 }
1068 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
1069 if (entry->skeleton.opcode == 0x0F) {
1070 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
1071 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
1072 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
1073 } else {
1074 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1075 }
1076 } else {
1077 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1078 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1079 }
jeffhao703f2cd2012-07-13 17:25:52 -07001080 uint8_t modrm = (modrmForDisp(base, disp) << 6) | (entry->skeleton.modrm_opcode << 3) | base;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001081 cUnit->codeBuffer.push_back(modrm);
1082 if (base == rSP) {
1083 // Special SIB for SP base
1084 cUnit->codeBuffer.push_back(0 << 6 | (rSP << 3) | rSP);
1085 }
jeffhao703f2cd2012-07-13 17:25:52 -07001086 emitDisp(cUnit, base, disp);
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001087 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1088 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1089}
1090
1091static void emitCallThread(CompilationUnit* cUnit, const X86EncodingMap* entry, int disp) {
1092 DCHECK_NE(entry->skeleton.prefix1, 0);
1093 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
1094 if (entry->skeleton.prefix2 != 0) {
1095 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
1096 }
1097 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
1098 if (entry->skeleton.opcode == 0x0F) {
1099 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
1100 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
1101 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
1102 } else {
1103 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1104 }
1105 } else {
1106 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1107 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1108 }
1109 uint8_t modrm = (0 << 6) | (entry->skeleton.modrm_opcode << 3) | rBP;
1110 cUnit->codeBuffer.push_back(modrm);
1111 cUnit->codeBuffer.push_back(disp & 0xFF);
1112 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
1113 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
1114 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
1115 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1116 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1117}
1118
Ian Rogers7caad772012-03-30 01:07:54 -07001119static void emitPcRel(CompilationUnit* cUnit, const X86EncodingMap* entry, uint8_t reg,
1120 int base_or_table, uint8_t index, int scale, int table_or_disp) {
1121 int disp;
1122 if (entry->opcode == kX86PcRelLoadRA) {
1123 SwitchTable *tabRec = (SwitchTable*)table_or_disp;
1124 disp = tabRec->offset;
1125 } else {
1126 DCHECK(entry->opcode == kX86PcRelAdr);
1127 FillArrayData *tabRec = (FillArrayData *)base_or_table;
1128 disp = tabRec->offset;
1129 }
1130 if (entry->skeleton.prefix1 != 0) {
1131 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
1132 if (entry->skeleton.prefix2 != 0) {
1133 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
1134 }
1135 } else {
1136 DCHECK_EQ(0, entry->skeleton.prefix2);
1137 }
1138 if (FPREG(reg)) {
1139 reg = reg & FP_REG_MASK;
1140 }
1141 DCHECK_LT(reg, 8);
1142 if (entry->opcode == kX86PcRelLoadRA) {
1143 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
1144 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1145 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1146 uint8_t modrm = (2 << 6) | (reg << 3) | rSP;
1147 cUnit->codeBuffer.push_back(modrm);
1148 DCHECK_LT(scale, 4);
1149 DCHECK_LT(index, 8);
1150 DCHECK_LT(base_or_table, 8);
1151 uint8_t base = static_cast<uint8_t>(base_or_table);
1152 uint8_t sib = (scale << 6) | (index << 3) | base;
1153 cUnit->codeBuffer.push_back(sib);
1154 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1155 } else {
1156 cUnit->codeBuffer.push_back(entry->skeleton.opcode + reg);
1157 }
1158 cUnit->codeBuffer.push_back(disp & 0xFF);
1159 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
1160 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
1161 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
1162 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1163 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1164}
1165
1166static void emitMacro(CompilationUnit* cUnit, const X86EncodingMap* entry,
1167 uint8_t reg, int offset) {
1168 DCHECK(entry->opcode == kX86StartOfMethod) << entry->name;
1169 cUnit->codeBuffer.push_back(0xE8); // call +0
1170 cUnit->codeBuffer.push_back(0);
1171 cUnit->codeBuffer.push_back(0);
1172 cUnit->codeBuffer.push_back(0);
1173 cUnit->codeBuffer.push_back(0);
1174
1175 DCHECK_LT(reg, 8);
1176 cUnit->codeBuffer.push_back(0x58 + reg); // pop reg
1177
1178 emitRegImm(cUnit, &EncodingMap[kX86Sub32RI], reg, offset + 5 /* size of call +0 */);
1179}
1180
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001181void emitUnimplemented(CompilationUnit* cUnit, const X86EncodingMap* entry, LIR* lir) {
Elliott Hughes225ae522012-04-16 20:21:45 -07001182 UNIMPLEMENTED(WARNING) << "encoding kind for " << entry->name << " " << buildInsnString(entry->fmt, lir, 0);
Ian Rogers141b0c72012-03-15 18:18:52 -07001183 for (int i = 0; i < oatGetInsnSize(lir); ++i) {
1184 cUnit->codeBuffer.push_back(0xCC); // push breakpoint instruction - int 3
1185 }
1186}
1187
buzbeee88dfbf2012-03-05 11:19:57 -08001188/*
1189 * Assemble the LIR into binary instruction format. Note that we may
1190 * discover that pc-relative displacements may not fit the selected
1191 * instruction. In those cases we will try to substitute a new code
1192 * sequence or request that the trace be shortened and retried.
1193 */
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001194AssemblerStatus oatAssembleInstructions(CompilationUnit *cUnit, intptr_t startAddr) {
Ian Rogersb5d09b22012-03-06 22:14:17 -08001195 LIR *lir;
1196 AssemblerStatus res = kSuccess; // Assume success
buzbeee88dfbf2012-03-05 11:19:57 -08001197
Ian Rogers141d6222012-04-05 12:23:06 -07001198 const bool kVerbosePcFixup = false;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001199 for (lir = (LIR *) cUnit->firstLIRInsn; lir; lir = NEXT_LIR(lir)) {
1200 if (lir->opcode < 0) {
1201 continue;
buzbeee88dfbf2012-03-05 11:19:57 -08001202 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001203
Ian Rogersb5d09b22012-03-06 22:14:17 -08001204 if (lir->flags.isNop) {
1205 continue;
1206 }
1207
1208 if (lir->flags.pcRelFixup) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001209 switch (lir->opcode) {
Ian Rogersb41b33b2012-03-20 14:22:54 -07001210 case kX86Jcc8: {
1211 LIR *targetLIR = lir->target;
1212 DCHECK(targetLIR != NULL);
1213 int delta = 0;
1214 intptr_t pc;
1215 if (IS_SIMM8(lir->operands[0])) {
1216 pc = lir->offset + 2 /* opcode + rel8 */;
1217 } else {
1218 pc = lir->offset + 6 /* 2 byte opcode + rel32 */;
1219 }
1220 intptr_t target = targetLIR->offset;
1221 delta = target - pc;
1222 if (IS_SIMM8(delta) != IS_SIMM8(lir->operands[0])) {
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001223 if (kVerbosePcFixup) {
1224 LOG(INFO) << "Retry for JCC growth at " << lir->offset
1225 << " delta: " << delta << " old delta: " << lir->operands[0];
1226 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001227 lir->opcode = kX86Jcc32;
buzbeeb046e162012-10-30 15:48:42 -07001228 oatSetupResourceMasks(cUnit, lir);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001229 res = kRetryAll;
1230 }
Ian Rogers7caad772012-03-30 01:07:54 -07001231 if (kVerbosePcFixup) {
1232 LOG(INFO) << "Source:";
1233 oatDumpLIRInsn(cUnit, lir, 0);
1234 LOG(INFO) << "Target:";
1235 oatDumpLIRInsn(cUnit, targetLIR, 0);
1236 LOG(INFO) << "Delta " << delta;
1237 }
1238 lir->operands[0] = delta;
1239 break;
1240 }
1241 case kX86Jcc32: {
1242 LIR *targetLIR = lir->target;
1243 DCHECK(targetLIR != NULL);
1244 intptr_t pc = lir->offset + 6 /* 2 byte opcode + rel32 */;
1245 intptr_t target = targetLIR->offset;
1246 int delta = target - pc;
1247 if (kVerbosePcFixup) {
1248 LOG(INFO) << "Source:";
1249 oatDumpLIRInsn(cUnit, lir, 0);
1250 LOG(INFO) << "Target:";
1251 oatDumpLIRInsn(cUnit, targetLIR, 0);
1252 LOG(INFO) << "Delta " << delta;
1253 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001254 lir->operands[0] = delta;
1255 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001256 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001257 case kX86Jmp8: {
1258 LIR *targetLIR = lir->target;
1259 DCHECK(targetLIR != NULL);
1260 int delta = 0;
1261 intptr_t pc;
1262 if (IS_SIMM8(lir->operands[0])) {
1263 pc = lir->offset + 2 /* opcode + rel8 */;
1264 } else {
1265 pc = lir->offset + 5 /* opcode + rel32 */;
1266 }
1267 intptr_t target = targetLIR->offset;
1268 delta = target - pc;
jeffhaoe2962482012-06-28 11:29:57 -07001269 if (!(cUnit->disableOpt & (1 << kSafeOptimizations)) && delta == 0) {
Ian Rogersb41b33b2012-03-20 14:22:54 -07001270 // Useless branch
1271 lir->flags.isNop = true;
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001272 if (kVerbosePcFixup) {
1273 LOG(INFO) << "Retry for useless branch at " << lir->offset;
1274 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001275 res = kRetryAll;
1276 } else if (IS_SIMM8(delta) != IS_SIMM8(lir->operands[0])) {
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001277 if (kVerbosePcFixup) {
1278 LOG(INFO) << "Retry for JMP growth at " << lir->offset;
1279 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001280 lir->opcode = kX86Jmp32;
buzbeeb046e162012-10-30 15:48:42 -07001281 oatSetupResourceMasks(cUnit, lir);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001282 res = kRetryAll;
1283 }
1284 lir->operands[0] = delta;
1285 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001286 }
Ian Rogers7caad772012-03-30 01:07:54 -07001287 case kX86Jmp32: {
1288 LIR *targetLIR = lir->target;
1289 DCHECK(targetLIR != NULL);
1290 intptr_t pc = lir->offset + 5 /* opcode + rel32 */;
1291 intptr_t target = targetLIR->offset;
1292 int delta = target - pc;
1293 lir->operands[0] = delta;
1294 break;
1295 }
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001296 default:
1297 break;
1298 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001299 }
1300
1301 /*
1302 * If one of the pc-relative instructions expanded we'll have
1303 * to make another pass. Don't bother to fully assemble the
1304 * instruction.
1305 */
1306 if (res != kSuccess) {
1307 continue;
1308 }
Ian Rogers7caad772012-03-30 01:07:54 -07001309 CHECK_EQ(static_cast<size_t>(lir->offset), cUnit->codeBuffer.size());
Ian Rogersb5d09b22012-03-06 22:14:17 -08001310 const X86EncodingMap *entry = &EncodingMap[lir->opcode];
Ian Rogers141b0c72012-03-15 18:18:52 -07001311 size_t starting_cbuf_size = cUnit->codeBuffer.size();
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001312 switch (entry->kind) {
Ian Rogersb5d09b22012-03-06 22:14:17 -08001313 case kData: // 4 bytes of data
1314 cUnit->codeBuffer.push_back(lir->operands[0]);
1315 break;
1316 case kNullary: // 1 byte of opcode
1317 DCHECK_EQ(0, entry->skeleton.prefix1);
1318 DCHECK_EQ(0, entry->skeleton.prefix2);
1319 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001320 if (entry->skeleton.extra_opcode1 != 0) {
1321 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
1322 if (entry->skeleton.extra_opcode2 != 0) {
1323 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
1324 }
1325 } else {
1326 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1327 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001328 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1329 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1330 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1331 break;
1332 case kReg: // lir operands - 0: reg
1333 emitOpReg(cUnit, entry, lir->operands[0]);
1334 break;
1335 case kMem: // lir operands - 0: base, 1: disp
1336 emitOpMem(cUnit, entry, lir->operands[0], lir->operands[1]);
1337 break;
1338 case kMemReg: // lir operands - 0: base, 1: disp, 2: reg
1339 emitMemReg(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1340 break;
Ian Rogersb41b33b2012-03-20 14:22:54 -07001341 case kArrayReg: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
1342 emitArrayReg(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2],
1343 lir->operands[3], lir->operands[4]);
1344 break;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001345 case kRegMem: // lir operands - 0: reg, 1: base, 2: disp
1346 emitRegMem(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1347 break;
1348 case kRegArray: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
1349 emitRegArray(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2],
1350 lir->operands[3], lir->operands[4]);
1351 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001352 case kRegThread: // lir operands - 0: reg, 1: disp
1353 emitRegThread(cUnit, entry, lir->operands[0], lir->operands[1]);
1354 break;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001355 case kRegReg: // lir operands - 0: reg1, 1: reg2
1356 emitRegReg(cUnit, entry, lir->operands[0], lir->operands[1]);
1357 break;
jeffhaofdffdf82012-07-11 16:08:43 -07001358 case kRegRegStore: // lir operands - 0: reg2, 1: reg1
1359 emitRegReg(cUnit, entry, lir->operands[1], lir->operands[0]);
1360 break;
Elliott Hughes225ae522012-04-16 20:21:45 -07001361 case kRegRegImm:
1362 emitRegRegImm(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1363 break;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001364 case kRegImm: // lir operands - 0: reg, 1: immediate
1365 emitRegImm(cUnit, entry, lir->operands[0], lir->operands[1]);
1366 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001367 case kThreadImm: // lir operands - 0: disp, 1: immediate
1368 emitThreadImm(cUnit, entry, lir->operands[0], lir->operands[1]);
1369 break;
1370 case kMovRegImm: // lir operands - 0: reg, 1: immediate
1371 emitMovRegImm(cUnit, entry, lir->operands[0], lir->operands[1]);
1372 break;
Ian Rogersb41b33b2012-03-20 14:22:54 -07001373 case kShiftRegImm: // lir operands - 0: reg, 1: immediate
1374 emitShiftRegImm(cUnit, entry, lir->operands[0], lir->operands[1]);
1375 break;
Ian Rogers7caad772012-03-30 01:07:54 -07001376 case kShiftRegCl: // lir operands - 0: reg, 1: cl
1377 emitShiftRegCl(cUnit, entry, lir->operands[0], lir->operands[1]);
1378 break;
1379 case kRegCond: // lir operands - 0: reg, 1: condition
1380 emitRegCond(cUnit, entry, lir->operands[0], lir->operands[1]);
1381 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001382 case kJmp: // lir operands - 0: rel
1383 emitJmp(cUnit, entry, lir->operands[0]);
1384 break;
1385 case kJcc: // lir operands - 0: rel, 1: CC, target assigned
1386 emitJcc(cUnit, entry, lir->operands[0], lir->operands[1]);
1387 break;
1388 case kCall:
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001389 switch (entry->opcode) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001390 case kX86CallM: // lir operands - 0: base, 1: disp
1391 emitCallMem(cUnit, entry, lir->operands[0], lir->operands[1]);
1392 break;
1393 case kX86CallT: // lir operands - 0: disp
1394 emitCallThread(cUnit, entry, lir->operands[0]);
1395 break;
1396 default:
1397 emitUnimplemented(cUnit, entry, lir);
1398 break;
1399 }
1400 break;
Ian Rogers7caad772012-03-30 01:07:54 -07001401 case kPcRel: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
1402 emitPcRel(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2],
1403 lir->operands[3], lir->operands[4]);
1404 break;
1405 case kMacro:
1406 emitMacro(cUnit, entry, lir->operands[0], lir->offset);
1407 break;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001408 default:
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001409 emitUnimplemented(cUnit, entry, lir);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001410 break;
1411 }
Ian Rogers7caad772012-03-30 01:07:54 -07001412 CHECK_EQ(static_cast<size_t>(oatGetInsnSize(lir)),
1413 cUnit->codeBuffer.size() - starting_cbuf_size)
1414 << "Instruction size mismatch for entry: " << EncodingMap[lir->opcode].name;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001415 }
1416 return res;
buzbeee88dfbf2012-03-05 11:19:57 -08001417}
1418
buzbeee88dfbf2012-03-05 11:19:57 -08001419/*
1420 * Target-dependent offset assignment.
1421 * independent.
1422 */
1423int oatAssignInsnOffsets(CompilationUnit* cUnit)
1424{
1425 LIR* x86LIR;
1426 int offset = 0;
1427
1428 for (x86LIR = (LIR *) cUnit->firstLIRInsn;
1429 x86LIR;
1430 x86LIR = NEXT_LIR(x86LIR)) {
1431 x86LIR->offset = offset;
1432 if (x86LIR->opcode >= 0) {
1433 if (!x86LIR->flags.isNop) {
1434 offset += x86LIR->flags.size;
1435 }
1436 } else if (x86LIR->opcode == kPseudoPseudoAlign4) {
1437 if (offset & 0x2) {
1438 offset += 2;
1439 x86LIR->operands[0] = 1;
1440 } else {
1441 x86LIR->operands[0] = 0;
1442 }
1443 }
1444 /* Pseudo opcodes don't consume space */
1445 }
1446
1447 return offset;
1448}
1449
1450} // namespace art