blob: 0c5d3cf48142b5f8a2662631643ced07cc5b6ccc [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
jeffhaoe2962482012-06-28 11:29:57 -0700212 { kX86Test8RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF6, 0, 0, 0, 0, 1}, "Test8RI", "!0r,!1d" },
213 { kX86Test8MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF6, 0, 0, 0, 0, 1}, "Test8MI", "[!0r+!1d],!2d" },
214 { 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" },
215 { kX86Test16RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0x66, 0, 0xF7, 0, 0, 0, 0, 2}, "Test16RI", "!0r,!1d" },
216 { kX86Test16MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0x66, 0, 0xF7, 0, 0, 0, 0, 2}, "Test16MI", "[!0r+!1d],!2d" },
217 { 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" },
218 { kX86Test32RI, kRegImm, IS_BINARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF7, 0, 0, 0, 0, 4}, "Test32RI", "!0r,!1d" },
219 { kX86Test32MI, kMemImm, IS_LOAD | IS_TERTIARY_OP | REG_USE0 | SETS_CCODES, { 0, 0, 0xF7, 0, 0, 0, 0, 4}, "Test32MI", "[!0r+!1d],!2d" },
220 { 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" },
221
222#define UNARY_ENCODING_MAP(opname, modrm, is_store, sets_ccodes, \
Ian Rogersb5d09b22012-03-06 22:14:17 -0800223 reg, reg_kind, reg_flags, \
224 mem, mem_kind, mem_flags, \
jeffhaoe2962482012-06-28 11:29:57 -0700225 arr, arr_kind, arr_flags, imm, \
226 b_flags, hw_flags, w_flags, \
227 b_format, hw_format, w_format) \
228{ 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" }, \
229{ 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]" }, \
230{ 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]" }, \
231{ 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" }, \
232{ 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]" }, \
233{ 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]" }, \
234{ 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" }, \
235{ 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]" }, \
236{ 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 -0800237
jeffhaoe2962482012-06-28 11:29:57 -0700238 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, "", "", ""),
239 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, "", "", ""),
240
241 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,"),
242 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,"),
243 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,"),
244 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 -0800245#undef UNARY_ENCODING_MAP
246
jeffhaoe2962482012-06-28 11:29:57 -0700247#define EXT_0F_ENCODING_MAP(opname, prefix, opcode, reg_def) \
248{ kX86 ## opname ## RR, kRegReg, IS_BINARY_OP | reg_def | REG_USE01, { prefix, 0, 0x0F, opcode, 0, 0, 0, 0 }, #opname "RR", "!0r,!1r" }, \
249{ 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]" }, \
250{ 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 -0800251
jeffhaoe2962482012-06-28 11:29:57 -0700252 EXT_0F_ENCODING_MAP(Movsd, 0xF2, 0x10, REG_DEF0),
253 { kX86MovsdMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0xF2, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovsdMR", "[!0r+!1d],!2r" },
254 { 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 -0800255
jeffhaoe2962482012-06-28 11:29:57 -0700256 EXT_0F_ENCODING_MAP(Movss, 0xF3, 0x10, REG_DEF0),
257 { kX86MovssMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0xF3, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovssMR", "[!0r+!1d],!2r" },
258 { 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 -0800259
jeffhaoe2962482012-06-28 11:29:57 -0700260 EXT_0F_ENCODING_MAP(Cvtsi2sd, 0xF2, 0x2A, REG_DEF0),
261 EXT_0F_ENCODING_MAP(Cvtsi2ss, 0xF3, 0x2A, REG_DEF0),
262 EXT_0F_ENCODING_MAP(Cvttsd2si, 0xF2, 0x2C, REG_DEF0),
263 EXT_0F_ENCODING_MAP(Cvttss2si, 0xF3, 0x2C, REG_DEF0),
264 EXT_0F_ENCODING_MAP(Cvtsd2si, 0xF2, 0x2D, REG_DEF0),
265 EXT_0F_ENCODING_MAP(Cvtss2si, 0xF3, 0x2D, REG_DEF0),
266 EXT_0F_ENCODING_MAP(Ucomisd, 0x66, 0x2E, SETS_CCODES),
267 EXT_0F_ENCODING_MAP(Ucomiss, 0x00, 0x2E, SETS_CCODES),
268 EXT_0F_ENCODING_MAP(Comisd, 0x66, 0x2F, SETS_CCODES),
269 EXT_0F_ENCODING_MAP(Comiss, 0x00, 0x2F, SETS_CCODES),
270 EXT_0F_ENCODING_MAP(Orps, 0x00, 0x56, REG_DEF0),
271 EXT_0F_ENCODING_MAP(Xorps, 0x00, 0x57, REG_DEF0),
272 EXT_0F_ENCODING_MAP(Addsd, 0xF2, 0x58, REG_DEF0),
273 EXT_0F_ENCODING_MAP(Addss, 0xF3, 0x58, REG_DEF0),
274 EXT_0F_ENCODING_MAP(Mulsd, 0xF2, 0x59, REG_DEF0),
275 EXT_0F_ENCODING_MAP(Mulss, 0xF3, 0x59, REG_DEF0),
276 EXT_0F_ENCODING_MAP(Cvtsd2ss, 0xF2, 0x5A, REG_DEF0),
277 EXT_0F_ENCODING_MAP(Cvtss2sd, 0xF3, 0x5A, REG_DEF0),
278 EXT_0F_ENCODING_MAP(Subsd, 0xF2, 0x5C, REG_DEF0),
279 EXT_0F_ENCODING_MAP(Subss, 0xF3, 0x5C, REG_DEF0),
280 EXT_0F_ENCODING_MAP(Divsd, 0xF2, 0x5E, REG_DEF0),
281 EXT_0F_ENCODING_MAP(Divss, 0xF3, 0x5E, REG_DEF0),
Ian Rogersb5d09b22012-03-06 22:14:17 -0800282
jeffhaofdffdf82012-07-11 16:08:43 -0700283 { 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 -0700284 { 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 -0700285
jeffhaoe2962482012-06-28 11:29:57 -0700286 EXT_0F_ENCODING_MAP(Movdxr, 0x66, 0x6E, REG_DEF0),
jeffhaofdffdf82012-07-11 16:08:43 -0700287 { kX86MovdrxRR, kRegRegStore, IS_BINARY_OP | REG_DEF0 | REG_USE01, { 0x66, 0, 0x0F, 0x7E, 0, 0, 0, 0 }, "MovdrxRR", "!0r,!1r" },
288 { kX86MovdrxMR, kMemReg, IS_STORE | IS_TERTIARY_OP | REG_USE02, { 0x66, 0, 0x0F, 0x7E, 0, 0, 0, 0 }, "MovdrxMR", "[!0r+!1d],!2r" },
289 { 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 -0800290
jeffhaoe2962482012-06-28 11:29:57 -0700291 { kX86Set8R, kRegCond, IS_BINARY_OP | REG_DEF0 | USES_CCODES, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0 }, "Set8R", "!1c !0r" },
292 { kX86Set8M, kMemCond, IS_STORE | IS_TERTIARY_OP | REG_USE0 | USES_CCODES, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0 }, "Set8M", "!2c [!0r+!1d]" },
293 { 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 -0800294
Ian Rogersc6f3bb82012-03-21 20:40:33 -0700295 // TODO: load/store?
296 // Encode the modrm opcode as an extra opcode byte to avoid computation during assembly.
297 { kX86Mfence, kReg, NO_OPERAND, { 0, 0, 0x0F, 0xAE, 0, 6, 0, 0 }, "Mfence", "" },
298
jeffhaoe2962482012-06-28 11:29:57 -0700299 EXT_0F_ENCODING_MAP(Imul16, 0x66, 0xAF, REG_DEF0 | SETS_CCODES),
300 EXT_0F_ENCODING_MAP(Imul32, 0x00, 0xAF, REG_DEF0 | SETS_CCODES),
jeffhao83025762012-08-02 11:08:56 -0700301
302 { 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" },
303 { 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" },
304 { 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" },
305 { 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" },
306 { 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" },
307 { 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" },
308
jeffhaoe2962482012-06-28 11:29:57 -0700309 EXT_0F_ENCODING_MAP(Movzx8, 0x00, 0xB6, REG_DEF0),
310 EXT_0F_ENCODING_MAP(Movzx16, 0x00, 0xB7, REG_DEF0),
311 EXT_0F_ENCODING_MAP(Movsx8, 0x00, 0xBE, REG_DEF0),
312 EXT_0F_ENCODING_MAP(Movsx16, 0x00, 0xBF, REG_DEF0),
Ian Rogersb5d09b22012-03-06 22:14:17 -0800313#undef EXT_0F_ENCODING_MAP
314
jeffhaoe2962482012-06-28 11:29:57 -0700315 { kX86Jcc8, kJcc, IS_BINARY_OP | IS_BRANCH | NEEDS_FIXUP | USES_CCODES, { 0, 0, 0x70, 0, 0, 0, 0, 0 }, "Jcc8", "!1c !0t" },
316 { kX86Jcc32, kJcc, IS_BINARY_OP | IS_BRANCH | NEEDS_FIXUP | USES_CCODES, { 0, 0, 0x0F, 0x80, 0, 0, 0, 0 }, "Jcc32", "!1c !0t" },
317 { kX86Jmp8, kJmp, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0xEB, 0, 0, 0, 0, 0 }, "Jmp8", "!0t" },
318 { kX86Jmp32, kJmp, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0xE9, 0, 0, 0, 0, 0 }, "Jmp32", "!0t" },
319 { kX86JmpR, kJmp, IS_UNARY_OP | IS_BRANCH | REG_USE0, { 0, 0, 0xFF, 0, 0, 4, 0, 0 }, "JmpR", "!0r" },
320 { kX86CallR, kCall, IS_UNARY_OP | IS_BRANCH | REG_USE0, { 0, 0, 0xE8, 0, 0, 0, 0, 0 }, "CallR", "!0r" },
321 { kX86CallM, kCall, IS_BINARY_OP | IS_BRANCH | IS_LOAD | REG_USE0, { 0, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallM", "[!0r+!1d]" },
322 { kX86CallA, kCall, IS_QUAD_OP | IS_BRANCH | IS_LOAD | REG_USE01, { 0, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallA", "[!0r+!1r<<!2d+!3d]" },
323 { kX86CallT, kCall, IS_UNARY_OP | IS_BRANCH | IS_LOAD, { THREAD_PREFIX, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallT", "fs:[!0d]" },
324 { kX86Ret, kNullary,NO_OPERAND | IS_BRANCH, { 0, 0, 0xC3, 0, 0, 0, 0, 0 }, "Ret", "" },
Ian Rogers7caad772012-03-30 01:07:54 -0700325
jeffhaoe2962482012-06-28 11:29:57 -0700326 { kX86StartOfMethod, kMacro, IS_UNARY_OP | SETS_CCODES, { 0, 0, 0, 0, 0, 0, 0, 0 }, "StartOfMethod", "!0r" },
327 { kX86PcRelLoadRA, kPcRel, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "PcRelLoadRA", "!0r,[!1r+!2r<<!3d+!4p]" },
328 { 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 -0800329};
330
Ian Rogersb5d09b22012-03-06 22:14:17 -0800331static size_t computeSize(X86EncodingMap* entry, int displacement, bool has_sib) {
332 size_t size = 0;
333 if (entry->skeleton.prefix1 > 0) {
334 ++size;
335 if (entry->skeleton.prefix2 > 0) {
336 ++size;
Ian Rogersde797832012-03-06 10:18:10 -0800337 }
Ian Rogersde797832012-03-06 10:18:10 -0800338 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800339 ++size; // opcode
340 if (entry->skeleton.opcode == 0x0F) {
341 ++size;
342 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
343 ++size;
344 }
345 }
346 ++size; // modrm
347 if (has_sib) {
348 ++size;
349 }
350 if (displacement != 0) {
351 if (entry->opcode != kX86Lea32RA) {
Ian Rogers7caad772012-03-30 01:07:54 -0700352 DCHECK_NE(entry->flags & (IS_LOAD | IS_STORE), 0) << entry->name;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800353 }
354 size += IS_SIMM8(displacement) ? 1 : 4;
355 }
356 size += entry->skeleton.immediate_bytes;
357 return size;
358}
359
360int oatGetInsnSize(LIR* lir) {
361 X86EncodingMap* entry = &EncodingMap[lir->opcode];
362 switch (entry->kind) {
363 case kData:
364 return 4; // 4 bytes of data
365 case kNop:
366 return lir->operands[0]; // length of nop is sole operand
367 case kNullary:
368 return 1; // 1 byte of opcode
369 case kReg: // lir operands - 0: reg
370 return computeSize(entry, 0, false);
371 case kMem: { // lir operands - 0: base, 1: disp
372 int base = lir->operands[0];
jeffhao703f2cd2012-07-13 17:25:52 -0700373 int disp = lir->operands[1];
374 // SP requires a special extra SIB byte. BP requires explicit disp,
375 // so add a byte for disp 0 which would normally be omitted.
376 return computeSize(entry, disp, false) + ((base == rSP) || (base == rBP && disp == 0) ? 1 : 0);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800377 }
378 case kArray: // lir operands - 0: base, 1: index, 2: scale, 3: disp
379 return computeSize(entry, lir->operands[3], true);
380 case kMemReg: { // lir operands - 0: base, 1: disp, 2: reg
381 int base = lir->operands[0];
jeffhao703f2cd2012-07-13 17:25:52 -0700382 int disp = lir->operands[1];
383 // SP requires a special extra SIB byte. BP requires explicit disp,
384 // so add a byte for disp 0 which would normally be omitted.
385 return computeSize(entry, disp, false) + ((base == rSP) || (base == rBP && disp == 0) ? 1 : 0);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800386 }
387 case kArrayReg: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
388 return computeSize(entry, lir->operands[3], true);
389 case kThreadReg: // lir operands - 0: disp, 1: reg
390 return computeSize(entry, lir->operands[0], false);
391 case kRegReg:
392 return computeSize(entry, 0, false);
jeffhaofdffdf82012-07-11 16:08:43 -0700393 case kRegRegStore:
394 return computeSize(entry, 0, false);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800395 case kRegMem: { // lir operands - 0: reg, 1: base, 2: disp
396 int base = lir->operands[1];
jeffhao703f2cd2012-07-13 17:25:52 -0700397 int disp = lir->operands[2];
398 // SP requires a special extra SIB byte. BP requires explicit disp,
399 // so add a byte for disp 0 which would normally be omitted.
400 return computeSize(entry, disp, false) + ((base == rSP) || (base == rBP && disp == 0) ? 1 : 0);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800401 }
jeffhao703f2cd2012-07-13 17:25:52 -0700402 case kRegArray: { // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
403 int base = lir->operands[1];
404 int disp = lir->operands[4];
405 // BP requires explicit disp, so add a byte for disp 0 which would normally be omitted.
406 return computeSize(entry, disp, true) + ((base == rBP && disp == 0) ? 1 : 0);
407 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800408 case kRegThread: // lir operands - 0: reg, 1: disp
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700409 return computeSize(entry, 0x12345678, false); // displacement size is always 32bit
Ian Rogersb5d09b22012-03-06 22:14:17 -0800410 case kRegImm: { // lir operands - 0: reg, 1: immediate
Ian Rogersb41b33b2012-03-20 14:22:54 -0700411 size_t size = computeSize(entry, 0, false);
412 if (entry->skeleton.ax_opcode == 0) {
413 return size;
414 } else {
415 // AX opcodes don't require the modrm byte.
416 int reg = lir->operands[0];
417 return size - (reg == rAX ? 1 : 0);
418 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800419 }
420 case kMemImm: // lir operands - 0: base, 1: disp, 2: immediate
421 CHECK_NE(lir->operands[0], static_cast<int>(rSP)); // TODO: add extra SIB byte
422 return computeSize(entry, lir->operands[1], false);
423 case kArrayImm: // lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
424 return computeSize(entry, lir->operands[3], true);
425 case kThreadImm: // lir operands - 0: disp, 1: imm
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700426 return computeSize(entry, 0x12345678, false); // displacement size is always 32bit
Ian Rogersb5d09b22012-03-06 22:14:17 -0800427 case kRegRegImm: // lir operands - 0: reg, 1: reg, 2: imm
428 return computeSize(entry, 0, false);
429 case kRegMemImm: // lir operands - 0: reg, 1: base, 2: disp, 3: imm
430 CHECK_NE(lir->operands[1], static_cast<int>(rSP)); // TODO: add extra SIB byte
431 return computeSize(entry, lir->operands[2], false);
432 case kRegArrayImm: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp, 5: imm
433 return computeSize(entry, lir->operands[4], true);
434 case kMovRegImm: // lir operands - 0: reg, 1: immediate
435 return 1 + entry->skeleton.immediate_bytes;
436 case kShiftRegImm: // lir operands - 0: reg, 1: immediate
437 // Shift by immediate one has a shorter opcode.
438 return computeSize(entry, 0, false) - (lir->operands[1] == 1 ? 1 : 0);
439 case kShiftMemImm: // lir operands - 0: base, 1: disp, 2: immediate
440 CHECK_NE(lir->operands[0], static_cast<int>(rSP)); // TODO: add extra SIB byte
441 // Shift by immediate one has a shorter opcode.
442 return computeSize(entry, lir->operands[1], false) - (lir->operands[2] == 1 ? 1 : 0);
443 case kShiftArrayImm: // lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
444 // Shift by immediate one has a shorter opcode.
445 return computeSize(entry, lir->operands[3], true) - (lir->operands[4] == 1 ? 1 : 0);
446 case kShiftRegCl:
447 return computeSize(entry, 0, false);
448 case kShiftMemCl: // lir operands - 0: base, 1: disp, 2: cl
449 CHECK_NE(lir->operands[0], static_cast<int>(rSP)); // TODO: add extra SIB byte
450 return computeSize(entry, lir->operands[1], false);
451 case kShiftArrayCl: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
452 return computeSize(entry, lir->operands[3], true);
453 case kRegCond: // lir operands - 0: reg, 1: cond
454 return computeSize(entry, 0, false);
455 case kMemCond: // lir operands - 0: base, 1: disp, 2: cond
456 CHECK_NE(lir->operands[0], static_cast<int>(rSP)); // TODO: add extra SIB byte
457 return computeSize(entry, lir->operands[1], false);
458 case kArrayCond: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: cond
459 return computeSize(entry, lir->operands[3], true);
Ian Rogersb41b33b2012-03-20 14:22:54 -0700460 case kJcc:
461 if (lir->opcode == kX86Jcc8) {
462 return 2; // opcode + rel8
463 } else {
464 DCHECK(lir->opcode == kX86Jcc32);
465 return 6; // 2 byte opcode + rel32
466 }
467 case kJmp:
468 if (lir->opcode == kX86Jmp8) {
469 return 2; // opcode + rel8
Ian Rogers7caad772012-03-30 01:07:54 -0700470 } else if (lir->opcode == kX86Jmp32) {
Ian Rogersb41b33b2012-03-20 14:22:54 -0700471 return 5; // opcode + rel32
Ian Rogers7caad772012-03-30 01:07:54 -0700472 } else {
473 DCHECK(lir->opcode == kX86JmpR);
474 return 2; // opcode + modrm
Ian Rogersb41b33b2012-03-20 14:22:54 -0700475 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800476 case kCall:
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700477 switch (lir->opcode) {
Ian Rogersb5d09b22012-03-06 22:14:17 -0800478 case kX86CallR: return 2; // opcode modrm
479 case kX86CallM: // lir operands - 0: base, 1: disp
480 return computeSize(entry, lir->operands[1], false);
481 case kX86CallA: // lir operands - 0: base, 1: index, 2: scale, 3: disp
482 return computeSize(entry, lir->operands[3], true);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700483 case kX86CallT: // lir operands - 0: disp
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700484 return computeSize(entry, 0x12345678, false); // displacement size is always 32bit
Ian Rogersb5d09b22012-03-06 22:14:17 -0800485 default:
486 break;
487 }
488 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700489 case kPcRel:
490 if (entry->opcode == kX86PcRelLoadRA) {
491 // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
492 return computeSize(entry, 0x12345678, true);
493 } else {
494 DCHECK(entry->opcode == kX86PcRelAdr);
495 return 5; // opcode with reg + 4 byte immediate
496 }
497 case kMacro:
498 DCHECK_EQ(lir->opcode, static_cast<int>(kX86StartOfMethod));
499 return 5 /* call opcode + 4 byte displacement */ + 1 /* pop reg */ +
500 computeSize(&EncodingMap[kX86Sub32RI], 0, false) -
501 (lir->operands[0] == rAX ? 1 : 0); // shorter ax encoding
Ian Rogersb5d09b22012-03-06 22:14:17 -0800502 default:
503 break;
504 }
505 UNIMPLEMENTED(FATAL) << "Unimplemented size encoding for: " << entry->name;
Ian Rogersde797832012-03-06 10:18:10 -0800506 return 0;
507}
buzbeee88dfbf2012-03-05 11:19:57 -0800508
jeffhao703f2cd2012-07-13 17:25:52 -0700509static uint8_t modrmForDisp(int base, int disp) {
510 // BP requires an explicit disp, so do not omit it in the 0 case
511 if (disp == 0 && base != rBP) {
Ian Rogersb5d09b22012-03-06 22:14:17 -0800512 return 0;
513 } else if (IS_SIMM8(disp)) {
514 return 1;
515 } else {
516 return 2;
517 }
518}
519
jeffhao703f2cd2012-07-13 17:25:52 -0700520static void emitDisp(CompilationUnit* cUnit, int base, int disp) {
521 // BP requires an explicit disp, so do not omit it in the 0 case
522 if (disp == 0 && base != rBP) {
Ian Rogersb5d09b22012-03-06 22:14:17 -0800523 return;
524 } else if (IS_SIMM8(disp)) {
525 cUnit->codeBuffer.push_back(disp & 0xFF);
526 } else {
527 cUnit->codeBuffer.push_back(disp & 0xFF);
528 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
529 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
530 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
531 }
532}
533
534static void emitOpReg(CompilationUnit* cUnit, const X86EncodingMap* entry, uint8_t reg) {
535 if (entry->skeleton.prefix1 != 0) {
536 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
537 if (entry->skeleton.prefix2 != 0) {
538 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
539 }
540 } else {
541 DCHECK_EQ(0, entry->skeleton.prefix2);
542 }
543 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
544 if (entry->skeleton.opcode == 0x0F) {
545 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
546 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
547 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
548 } else {
549 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
550 }
551 } else {
552 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
553 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
554 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700555 if (FPREG(reg)) {
556 reg = reg & FP_REG_MASK;
557 }
jeffhao703f2cd2012-07-13 17:25:52 -0700558 if (reg >= 4) {
559 DCHECK(strchr(entry->name, '8') == NULL) << entry->name << " " << (int) reg
560 << " in " << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
561 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800562 DCHECK_LT(reg, 8);
563 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
564 cUnit->codeBuffer.push_back(modrm);
565 DCHECK_EQ(0, entry->skeleton.ax_opcode);
566 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
567}
568
569static void emitOpMem(CompilationUnit* cUnit, const X86EncodingMap* entry, uint8_t base, int disp) {
570 if (entry->skeleton.prefix1 != 0) {
571 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
572 if (entry->skeleton.prefix2 != 0) {
573 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
574 }
575 } else {
576 DCHECK_EQ(0, entry->skeleton.prefix2);
577 }
578 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
579 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
580 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
581 DCHECK_LT(entry->skeleton.modrm_opcode, 8);
582 DCHECK_LT(base, 8);
jeffhao703f2cd2012-07-13 17:25:52 -0700583 uint8_t modrm = (modrmForDisp(base, disp) << 6) | (entry->skeleton.modrm_opcode << 3) | base;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800584 cUnit->codeBuffer.push_back(modrm);
jeffhao703f2cd2012-07-13 17:25:52 -0700585 emitDisp(cUnit, base, disp);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800586 DCHECK_EQ(0, entry->skeleton.ax_opcode);
587 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
588}
589
590static void emitMemReg(CompilationUnit* cUnit, const X86EncodingMap* entry,
591 uint8_t base, int disp, uint8_t reg) {
592 if (entry->skeleton.prefix1 != 0) {
593 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
594 if (entry->skeleton.prefix2 != 0) {
595 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
596 }
597 } else {
598 DCHECK_EQ(0, entry->skeleton.prefix2);
599 }
600 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
601 if (entry->skeleton.opcode == 0x0F) {
602 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
603 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
604 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
605 } else {
606 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
607 }
608 } else {
609 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
610 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
611 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700612 if (FPREG(reg)) {
613 reg = reg & FP_REG_MASK;
614 }
jeffhao703f2cd2012-07-13 17:25:52 -0700615 if (reg >= 4) {
616 DCHECK(strchr(entry->name, '8') == NULL) << entry->name << " " << (int) reg
617 << " in " << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
618 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800619 DCHECK_LT(reg, 8);
620 DCHECK_LT(base, 8);
jeffhao703f2cd2012-07-13 17:25:52 -0700621 uint8_t modrm = (modrmForDisp(base, disp) << 6) | (reg << 3) | base;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800622 cUnit->codeBuffer.push_back(modrm);
623 if (base == rSP) {
624 // Special SIB for SP base
625 cUnit->codeBuffer.push_back(0 << 6 | (rSP << 3) | rSP);
626 }
jeffhao703f2cd2012-07-13 17:25:52 -0700627 emitDisp(cUnit, base, disp);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800628 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
629 DCHECK_EQ(0, entry->skeleton.ax_opcode);
630 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
631}
632
633static void emitRegMem(CompilationUnit* cUnit, const X86EncodingMap* entry,
634 uint8_t reg, uint8_t base, int disp) {
635 // Opcode will flip operands.
636 emitMemReg(cUnit, entry, base, disp, reg);
637}
638
639static void emitRegArray(CompilationUnit* cUnit, const X86EncodingMap* entry, uint8_t reg,
640 uint8_t base, uint8_t index, int scale, int disp) {
641 if (entry->skeleton.prefix1 != 0) {
642 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
643 if (entry->skeleton.prefix2 != 0) {
644 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
645 }
646 } else {
647 DCHECK_EQ(0, entry->skeleton.prefix2);
648 }
649 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
650 if (entry->skeleton.opcode == 0x0F) {
651 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
652 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
653 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
654 } else {
655 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
656 }
657 } else {
658 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
659 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
660 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700661 if (FPREG(reg)) {
662 reg = reg & FP_REG_MASK;
663 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800664 DCHECK_LT(reg, 8);
jeffhao703f2cd2012-07-13 17:25:52 -0700665 uint8_t modrm = (modrmForDisp(base, disp) << 6) | (reg << 3) | rSP;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800666 cUnit->codeBuffer.push_back(modrm);
667 DCHECK_LT(scale, 4);
668 DCHECK_LT(index, 8);
669 DCHECK_LT(base, 8);
670 uint8_t sib = (scale << 6) | (index << 3) | base;
671 cUnit->codeBuffer.push_back(sib);
jeffhao703f2cd2012-07-13 17:25:52 -0700672 emitDisp(cUnit, base, disp);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800673 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
674 DCHECK_EQ(0, entry->skeleton.ax_opcode);
675 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
676}
677
Ian Rogersb41b33b2012-03-20 14:22:54 -0700678static void emitArrayReg(CompilationUnit* cUnit, const X86EncodingMap* entry,
679 uint8_t base, uint8_t index, int scale, int disp, uint8_t reg) {
680 // Opcode will flip operands.
681 emitRegArray(cUnit, entry, reg, base, index, scale, disp);
682}
683
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700684static void emitRegThread(CompilationUnit* cUnit, const X86EncodingMap* entry,
685 uint8_t reg, int disp) {
686 DCHECK_NE(entry->skeleton.prefix1, 0);
687 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
688 if (entry->skeleton.prefix2 != 0) {
689 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
690 }
691 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
692 if (entry->skeleton.opcode == 0x0F) {
693 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
694 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
695 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
696 } else {
697 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
698 }
699 } else {
700 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
701 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
702 }
703 if (FPREG(reg)) {
704 reg = reg & FP_REG_MASK;
705 }
jeffhao703f2cd2012-07-13 17:25:52 -0700706 if (reg >= 4) {
707 DCHECK(strchr(entry->name, '8') == NULL) << entry->name << " " << (int) reg
708 << " in " << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
709 }
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700710 DCHECK_LT(reg, 8);
711 uint8_t modrm = (0 << 6) | (reg << 3) | rBP;
712 cUnit->codeBuffer.push_back(modrm);
713 cUnit->codeBuffer.push_back(disp & 0xFF);
714 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
715 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
716 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
717 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
718 DCHECK_EQ(0, entry->skeleton.ax_opcode);
719 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
720}
721
Ian Rogersb5d09b22012-03-06 22:14:17 -0800722static void emitRegReg(CompilationUnit* cUnit, const X86EncodingMap* entry,
723 uint8_t reg1, uint8_t reg2) {
724 if (entry->skeleton.prefix1 != 0) {
725 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
726 if (entry->skeleton.prefix2 != 0) {
727 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
728 }
729 } else {
730 DCHECK_EQ(0, entry->skeleton.prefix2);
731 }
732 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
733 if (entry->skeleton.opcode == 0x0F) {
734 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
735 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
736 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
737 } else {
738 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
739 }
740 } else {
741 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
742 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
743 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700744 if (FPREG(reg1)) {
745 reg1 = reg1 & FP_REG_MASK;
746 }
747 if (FPREG(reg2)) {
748 reg2 = reg2 & FP_REG_MASK;
749 }
750 DCHECK_LT(reg1, 8);
751 DCHECK_LT(reg2, 8);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800752 uint8_t modrm = (3 << 6) | (reg1 << 3) | reg2;
753 cUnit->codeBuffer.push_back(modrm);
754 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
755 DCHECK_EQ(0, entry->skeleton.ax_opcode);
756 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
757}
758
Elliott Hughes225ae522012-04-16 20:21:45 -0700759static void emitRegRegImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
760 uint8_t reg1, uint8_t reg2, int32_t imm) {
761 if (entry->skeleton.prefix1 != 0) {
762 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
763 if (entry->skeleton.prefix2 != 0) {
764 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
765 }
766 } else {
767 DCHECK_EQ(0, entry->skeleton.prefix2);
768 }
769 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
770 if (entry->skeleton.opcode == 0x0F) {
771 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
772 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
773 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
774 } else {
775 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
776 }
777 } else {
778 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
779 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
780 }
781 if (FPREG(reg1)) {
782 reg1 = reg1 & FP_REG_MASK;
783 }
784 if (FPREG(reg2)) {
785 reg2 = reg2 & FP_REG_MASK;
786 }
787 DCHECK_LT(reg1, 8);
788 DCHECK_LT(reg2, 8);
789 uint8_t modrm = (3 << 6) | (reg1 << 3) | reg2;
790 cUnit->codeBuffer.push_back(modrm);
791 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
792 DCHECK_EQ(0, entry->skeleton.ax_opcode);
793 switch (entry->skeleton.immediate_bytes) {
794 case 1:
795 DCHECK(IS_SIMM8(imm));
796 cUnit->codeBuffer.push_back(imm & 0xFF);
797 break;
798 case 2:
799 DCHECK(IS_SIMM16(imm));
800 cUnit->codeBuffer.push_back(imm & 0xFF);
801 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
802 break;
803 case 4:
804 cUnit->codeBuffer.push_back(imm & 0xFF);
805 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
806 cUnit->codeBuffer.push_back((imm >> 16) & 0xFF);
807 cUnit->codeBuffer.push_back((imm >> 24) & 0xFF);
808 break;
809 default:
810 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
811 << ") for instruction: " << entry->name;
812 break;
813 }
814}
815
Ian Rogersb5d09b22012-03-06 22:14:17 -0800816static void emitRegImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
817 uint8_t reg, int imm) {
818 if (entry->skeleton.prefix1 != 0) {
819 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
820 if (entry->skeleton.prefix2 != 0) {
821 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
822 }
823 } else {
824 DCHECK_EQ(0, entry->skeleton.prefix2);
825 }
826 if (reg == rAX && entry->skeleton.ax_opcode != 0) {
827 cUnit->codeBuffer.push_back(entry->skeleton.ax_opcode);
828 } else {
829 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
830 if (entry->skeleton.opcode == 0x0F) {
831 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
832 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
833 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
834 } else {
835 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
836 }
837 } else {
838 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
839 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
840 }
jeffhaofdffdf82012-07-11 16:08:43 -0700841 if (FPREG(reg)) {
842 reg = reg & FP_REG_MASK;
843 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800844 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
845 cUnit->codeBuffer.push_back(modrm);
846 }
847 switch (entry->skeleton.immediate_bytes) {
848 case 1:
849 DCHECK(IS_SIMM8(imm));
850 cUnit->codeBuffer.push_back(imm & 0xFF);
851 break;
852 case 2:
853 DCHECK(IS_SIMM16(imm));
854 cUnit->codeBuffer.push_back(imm & 0xFF);
855 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
856 break;
857 case 4:
858 cUnit->codeBuffer.push_back(imm & 0xFF);
859 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
860 cUnit->codeBuffer.push_back((imm >> 16) & 0xFF);
861 cUnit->codeBuffer.push_back((imm >> 24) & 0xFF);
862 break;
863 default:
864 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
865 << ") for instruction: " << entry->name;
866 break;
867 }
868}
869
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700870static void emitThreadImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
871 int disp, int imm) {
872 if (entry->skeleton.prefix1 != 0) {
873 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
874 if (entry->skeleton.prefix2 != 0) {
875 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
876 }
877 } else {
878 DCHECK_EQ(0, entry->skeleton.prefix2);
879 }
880 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
881 if (entry->skeleton.opcode == 0x0F) {
882 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
883 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
884 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
885 } else {
886 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
887 }
888 } else {
889 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
890 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
891 }
892 uint8_t modrm = (0 << 6) | (entry->skeleton.modrm_opcode << 3) | rBP;
893 cUnit->codeBuffer.push_back(modrm);
894 cUnit->codeBuffer.push_back(disp & 0xFF);
895 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
896 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
897 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
898 switch (entry->skeleton.immediate_bytes) {
899 case 1:
900 DCHECK(IS_SIMM8(imm));
901 cUnit->codeBuffer.push_back(imm & 0xFF);
902 break;
903 case 2:
904 DCHECK(IS_SIMM16(imm));
905 cUnit->codeBuffer.push_back(imm & 0xFF);
906 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
907 break;
908 case 4:
909 cUnit->codeBuffer.push_back(imm & 0xFF);
910 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
911 cUnit->codeBuffer.push_back((imm >> 16) & 0xFF);
912 cUnit->codeBuffer.push_back((imm >> 24) & 0xFF);
913 break;
914 default:
915 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
916 << ") for instruction: " << entry->name;
917 break;
918 }
919 DCHECK_EQ(entry->skeleton.ax_opcode, 0);
920}
921
922static void emitMovRegImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
923 uint8_t reg, int imm) {
924 DCHECK_LT(reg, 8);
925 cUnit->codeBuffer.push_back(0xB8 + reg);
926 cUnit->codeBuffer.push_back(imm & 0xFF);
927 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
928 cUnit->codeBuffer.push_back((imm >> 16) & 0xFF);
929 cUnit->codeBuffer.push_back((imm >> 24) & 0xFF);
930}
931
Ian Rogersb41b33b2012-03-20 14:22:54 -0700932static void emitShiftRegImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
Ian Rogers7caad772012-03-30 01:07:54 -0700933 uint8_t reg, int imm) {
Ian Rogersb41b33b2012-03-20 14:22:54 -0700934 if (entry->skeleton.prefix1 != 0) {
935 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
936 if (entry->skeleton.prefix2 != 0) {
937 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
938 }
939 } else {
940 DCHECK_EQ(0, entry->skeleton.prefix2);
941 }
942 if (imm != 1) {
943 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
944 } else {
945 // Shorter encoding for 1 bit shift
946 cUnit->codeBuffer.push_back(entry->skeleton.ax_opcode);
947 }
948 if (entry->skeleton.opcode == 0x0F) {
949 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
950 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
951 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
952 } else {
953 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
954 }
955 } else {
956 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
957 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
958 }
jeffhao703f2cd2012-07-13 17:25:52 -0700959 if (reg >= 4) {
960 DCHECK(strchr(entry->name, '8') == NULL) << entry->name << " " << (int) reg
961 << " in " << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
962 }
Ian Rogersb41b33b2012-03-20 14:22:54 -0700963 DCHECK_LT(reg, 8);
Ian Rogers7caad772012-03-30 01:07:54 -0700964 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
Ian Rogersb41b33b2012-03-20 14:22:54 -0700965 cUnit->codeBuffer.push_back(modrm);
966 if (imm != 1) {
967 DCHECK_EQ(entry->skeleton.immediate_bytes, 1);
968 DCHECK(IS_SIMM8(imm));
969 cUnit->codeBuffer.push_back(imm & 0xFF);
970 }
971}
972
Ian Rogers7caad772012-03-30 01:07:54 -0700973static void emitShiftRegCl(CompilationUnit* cUnit, const X86EncodingMap* entry,
974 uint8_t reg, uint8_t cl) {
975 DCHECK_EQ(cl, static_cast<uint8_t>(rCX));
976 if (entry->skeleton.prefix1 != 0) {
977 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
978 if (entry->skeleton.prefix2 != 0) {
979 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
980 }
981 } else {
982 DCHECK_EQ(0, entry->skeleton.prefix2);
983 }
984 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
985 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
986 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
987 DCHECK_LT(reg, 8);
988 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
989 cUnit->codeBuffer.push_back(modrm);
990 DCHECK_EQ(0, entry->skeleton.ax_opcode);
991 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
992}
993
994static void emitRegCond(CompilationUnit* cUnit, const X86EncodingMap* entry,
995 uint8_t reg, uint8_t condition) {
996 if (entry->skeleton.prefix1 != 0) {
997 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
998 if (entry->skeleton.prefix2 != 0) {
999 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
1000 }
1001 } else {
1002 DCHECK_EQ(0, entry->skeleton.prefix2);
1003 }
1004 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1005 DCHECK_EQ(0x0F, entry->skeleton.opcode);
1006 cUnit->codeBuffer.push_back(0x0F);
1007 DCHECK_EQ(0x90, entry->skeleton.extra_opcode1);
1008 cUnit->codeBuffer.push_back(0x90 | condition);
1009 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1010 DCHECK_LT(reg, 8);
1011 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
1012 cUnit->codeBuffer.push_back(modrm);
1013 DCHECK_EQ(entry->skeleton.immediate_bytes, 0);
1014}
1015
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001016static void emitJmp(CompilationUnit* cUnit, const X86EncodingMap* entry, int rel) {
Ian Rogersb41b33b2012-03-20 14:22:54 -07001017 if (entry->opcode == kX86Jmp8) {
1018 DCHECK(IS_SIMM8(rel));
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001019 cUnit->codeBuffer.push_back(0xEB);
1020 cUnit->codeBuffer.push_back(rel & 0xFF);
Ian Rogers7caad772012-03-30 01:07:54 -07001021 } else if (entry->opcode == kX86Jmp32) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001022 cUnit->codeBuffer.push_back(0xE9);
1023 cUnit->codeBuffer.push_back(rel & 0xFF);
1024 cUnit->codeBuffer.push_back((rel >> 8) & 0xFF);
1025 cUnit->codeBuffer.push_back((rel >> 16) & 0xFF);
1026 cUnit->codeBuffer.push_back((rel >> 24) & 0xFF);
Ian Rogers7caad772012-03-30 01:07:54 -07001027 } else {
1028 DCHECK(entry->opcode == kX86JmpR);
1029 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
1030 uint8_t reg = static_cast<uint8_t>(rel);
1031 DCHECK_LT(reg, 8);
1032 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
1033 cUnit->codeBuffer.push_back(modrm);
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001034 }
1035}
1036
1037static void emitJcc(CompilationUnit* cUnit, const X86EncodingMap* entry,
1038 int rel, uint8_t cc) {
1039 DCHECK_LT(cc, 16);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001040 if (entry->opcode == kX86Jcc8) {
1041 DCHECK(IS_SIMM8(rel));
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001042 cUnit->codeBuffer.push_back(0x70 | cc);
1043 cUnit->codeBuffer.push_back(rel & 0xFF);
1044 } else {
Ian Rogersb41b33b2012-03-20 14:22:54 -07001045 DCHECK(entry->opcode == kX86Jcc32);
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001046 cUnit->codeBuffer.push_back(0x0F);
1047 cUnit->codeBuffer.push_back(0x80 | cc);
1048 cUnit->codeBuffer.push_back(rel & 0xFF);
1049 cUnit->codeBuffer.push_back((rel >> 8) & 0xFF);
1050 cUnit->codeBuffer.push_back((rel >> 16) & 0xFF);
1051 cUnit->codeBuffer.push_back((rel >> 24) & 0xFF);
1052 }
1053}
1054
1055static void emitCallMem(CompilationUnit* cUnit, const X86EncodingMap* entry,
1056 uint8_t base, int disp) {
1057 if (entry->skeleton.prefix1 != 0) {
1058 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
1059 if (entry->skeleton.prefix2 != 0) {
1060 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
1061 }
1062 } else {
1063 DCHECK_EQ(0, entry->skeleton.prefix2);
1064 }
1065 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
1066 if (entry->skeleton.opcode == 0x0F) {
1067 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
1068 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
1069 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
1070 } else {
1071 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1072 }
1073 } else {
1074 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1075 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1076 }
jeffhao703f2cd2012-07-13 17:25:52 -07001077 uint8_t modrm = (modrmForDisp(base, disp) << 6) | (entry->skeleton.modrm_opcode << 3) | base;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001078 cUnit->codeBuffer.push_back(modrm);
1079 if (base == rSP) {
1080 // Special SIB for SP base
1081 cUnit->codeBuffer.push_back(0 << 6 | (rSP << 3) | rSP);
1082 }
jeffhao703f2cd2012-07-13 17:25:52 -07001083 emitDisp(cUnit, base, disp);
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001084 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1085 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1086}
1087
1088static void emitCallThread(CompilationUnit* cUnit, const X86EncodingMap* entry, int disp) {
1089 DCHECK_NE(entry->skeleton.prefix1, 0);
1090 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
1091 if (entry->skeleton.prefix2 != 0) {
1092 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
1093 }
1094 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
1095 if (entry->skeleton.opcode == 0x0F) {
1096 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
1097 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
1098 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
1099 } else {
1100 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1101 }
1102 } else {
1103 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1104 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1105 }
1106 uint8_t modrm = (0 << 6) | (entry->skeleton.modrm_opcode << 3) | rBP;
1107 cUnit->codeBuffer.push_back(modrm);
1108 cUnit->codeBuffer.push_back(disp & 0xFF);
1109 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
1110 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
1111 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
1112 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1113 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1114}
1115
Ian Rogers7caad772012-03-30 01:07:54 -07001116static void emitPcRel(CompilationUnit* cUnit, const X86EncodingMap* entry, uint8_t reg,
1117 int base_or_table, uint8_t index, int scale, int table_or_disp) {
1118 int disp;
1119 if (entry->opcode == kX86PcRelLoadRA) {
1120 SwitchTable *tabRec = (SwitchTable*)table_or_disp;
1121 disp = tabRec->offset;
1122 } else {
1123 DCHECK(entry->opcode == kX86PcRelAdr);
1124 FillArrayData *tabRec = (FillArrayData *)base_or_table;
1125 disp = tabRec->offset;
1126 }
1127 if (entry->skeleton.prefix1 != 0) {
1128 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
1129 if (entry->skeleton.prefix2 != 0) {
1130 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
1131 }
1132 } else {
1133 DCHECK_EQ(0, entry->skeleton.prefix2);
1134 }
1135 if (FPREG(reg)) {
1136 reg = reg & FP_REG_MASK;
1137 }
1138 DCHECK_LT(reg, 8);
1139 if (entry->opcode == kX86PcRelLoadRA) {
1140 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
1141 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1142 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1143 uint8_t modrm = (2 << 6) | (reg << 3) | rSP;
1144 cUnit->codeBuffer.push_back(modrm);
1145 DCHECK_LT(scale, 4);
1146 DCHECK_LT(index, 8);
1147 DCHECK_LT(base_or_table, 8);
1148 uint8_t base = static_cast<uint8_t>(base_or_table);
1149 uint8_t sib = (scale << 6) | (index << 3) | base;
1150 cUnit->codeBuffer.push_back(sib);
1151 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1152 } else {
1153 cUnit->codeBuffer.push_back(entry->skeleton.opcode + reg);
1154 }
1155 cUnit->codeBuffer.push_back(disp & 0xFF);
1156 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
1157 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
1158 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
1159 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1160 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1161}
1162
1163static void emitMacro(CompilationUnit* cUnit, const X86EncodingMap* entry,
1164 uint8_t reg, int offset) {
1165 DCHECK(entry->opcode == kX86StartOfMethod) << entry->name;
1166 cUnit->codeBuffer.push_back(0xE8); // call +0
1167 cUnit->codeBuffer.push_back(0);
1168 cUnit->codeBuffer.push_back(0);
1169 cUnit->codeBuffer.push_back(0);
1170 cUnit->codeBuffer.push_back(0);
1171
1172 DCHECK_LT(reg, 8);
1173 cUnit->codeBuffer.push_back(0x58 + reg); // pop reg
1174
1175 emitRegImm(cUnit, &EncodingMap[kX86Sub32RI], reg, offset + 5 /* size of call +0 */);
1176}
1177
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001178void emitUnimplemented(CompilationUnit* cUnit, const X86EncodingMap* entry, LIR* lir) {
Elliott Hughes225ae522012-04-16 20:21:45 -07001179 UNIMPLEMENTED(WARNING) << "encoding kind for " << entry->name << " " << buildInsnString(entry->fmt, lir, 0);
Ian Rogers141b0c72012-03-15 18:18:52 -07001180 for (int i = 0; i < oatGetInsnSize(lir); ++i) {
1181 cUnit->codeBuffer.push_back(0xCC); // push breakpoint instruction - int 3
1182 }
1183}
1184
buzbeee88dfbf2012-03-05 11:19:57 -08001185/*
1186 * Assemble the LIR into binary instruction format. Note that we may
1187 * discover that pc-relative displacements may not fit the selected
1188 * instruction. In those cases we will try to substitute a new code
1189 * sequence or request that the trace be shortened and retried.
1190 */
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001191AssemblerStatus oatAssembleInstructions(CompilationUnit *cUnit, intptr_t startAddr) {
Ian Rogersb5d09b22012-03-06 22:14:17 -08001192 LIR *lir;
1193 AssemblerStatus res = kSuccess; // Assume success
buzbeee88dfbf2012-03-05 11:19:57 -08001194
Ian Rogers141d6222012-04-05 12:23:06 -07001195 const bool kVerbosePcFixup = false;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001196 for (lir = (LIR *) cUnit->firstLIRInsn; lir; lir = NEXT_LIR(lir)) {
1197 if (lir->opcode < 0) {
1198 continue;
buzbeee88dfbf2012-03-05 11:19:57 -08001199 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001200
Ian Rogersb5d09b22012-03-06 22:14:17 -08001201 if (lir->flags.isNop) {
1202 continue;
1203 }
1204
1205 if (lir->flags.pcRelFixup) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001206 switch (lir->opcode) {
Ian Rogersb41b33b2012-03-20 14:22:54 -07001207 case kX86Jcc8: {
1208 LIR *targetLIR = lir->target;
1209 DCHECK(targetLIR != NULL);
1210 int delta = 0;
1211 intptr_t pc;
1212 if (IS_SIMM8(lir->operands[0])) {
1213 pc = lir->offset + 2 /* opcode + rel8 */;
1214 } else {
1215 pc = lir->offset + 6 /* 2 byte opcode + rel32 */;
1216 }
1217 intptr_t target = targetLIR->offset;
1218 delta = target - pc;
1219 if (IS_SIMM8(delta) != IS_SIMM8(lir->operands[0])) {
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001220 if (kVerbosePcFixup) {
1221 LOG(INFO) << "Retry for JCC growth at " << lir->offset
1222 << " delta: " << delta << " old delta: " << lir->operands[0];
1223 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001224 lir->opcode = kX86Jcc32;
1225 oatSetupResourceMasks(lir);
1226 res = kRetryAll;
1227 }
Ian Rogers7caad772012-03-30 01:07:54 -07001228 if (kVerbosePcFixup) {
1229 LOG(INFO) << "Source:";
1230 oatDumpLIRInsn(cUnit, lir, 0);
1231 LOG(INFO) << "Target:";
1232 oatDumpLIRInsn(cUnit, targetLIR, 0);
1233 LOG(INFO) << "Delta " << delta;
1234 }
1235 lir->operands[0] = delta;
1236 break;
1237 }
1238 case kX86Jcc32: {
1239 LIR *targetLIR = lir->target;
1240 DCHECK(targetLIR != NULL);
1241 intptr_t pc = lir->offset + 6 /* 2 byte opcode + rel32 */;
1242 intptr_t target = targetLIR->offset;
1243 int delta = target - pc;
1244 if (kVerbosePcFixup) {
1245 LOG(INFO) << "Source:";
1246 oatDumpLIRInsn(cUnit, lir, 0);
1247 LOG(INFO) << "Target:";
1248 oatDumpLIRInsn(cUnit, targetLIR, 0);
1249 LOG(INFO) << "Delta " << delta;
1250 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001251 lir->operands[0] = delta;
1252 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001253 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001254 case kX86Jmp8: {
1255 LIR *targetLIR = lir->target;
1256 DCHECK(targetLIR != NULL);
1257 int delta = 0;
1258 intptr_t pc;
1259 if (IS_SIMM8(lir->operands[0])) {
1260 pc = lir->offset + 2 /* opcode + rel8 */;
1261 } else {
1262 pc = lir->offset + 5 /* opcode + rel32 */;
1263 }
1264 intptr_t target = targetLIR->offset;
1265 delta = target - pc;
jeffhaoe2962482012-06-28 11:29:57 -07001266 if (!(cUnit->disableOpt & (1 << kSafeOptimizations)) && delta == 0) {
Ian Rogersb41b33b2012-03-20 14:22:54 -07001267 // Useless branch
1268 lir->flags.isNop = true;
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001269 if (kVerbosePcFixup) {
1270 LOG(INFO) << "Retry for useless branch at " << lir->offset;
1271 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001272 res = kRetryAll;
1273 } else if (IS_SIMM8(delta) != IS_SIMM8(lir->operands[0])) {
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001274 if (kVerbosePcFixup) {
1275 LOG(INFO) << "Retry for JMP growth at " << lir->offset;
1276 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001277 lir->opcode = kX86Jmp32;
1278 oatSetupResourceMasks(lir);
1279 res = kRetryAll;
1280 }
1281 lir->operands[0] = delta;
1282 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001283 }
Ian Rogers7caad772012-03-30 01:07:54 -07001284 case kX86Jmp32: {
1285 LIR *targetLIR = lir->target;
1286 DCHECK(targetLIR != NULL);
1287 intptr_t pc = lir->offset + 5 /* opcode + rel32 */;
1288 intptr_t target = targetLIR->offset;
1289 int delta = target - pc;
1290 lir->operands[0] = delta;
1291 break;
1292 }
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001293 default:
1294 break;
1295 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001296 }
1297
1298 /*
1299 * If one of the pc-relative instructions expanded we'll have
1300 * to make another pass. Don't bother to fully assemble the
1301 * instruction.
1302 */
1303 if (res != kSuccess) {
1304 continue;
1305 }
Ian Rogers7caad772012-03-30 01:07:54 -07001306 CHECK_EQ(static_cast<size_t>(lir->offset), cUnit->codeBuffer.size());
Ian Rogersb5d09b22012-03-06 22:14:17 -08001307 const X86EncodingMap *entry = &EncodingMap[lir->opcode];
Ian Rogers141b0c72012-03-15 18:18:52 -07001308 size_t starting_cbuf_size = cUnit->codeBuffer.size();
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001309 switch (entry->kind) {
Ian Rogersb5d09b22012-03-06 22:14:17 -08001310 case kData: // 4 bytes of data
1311 cUnit->codeBuffer.push_back(lir->operands[0]);
1312 break;
1313 case kNullary: // 1 byte of opcode
1314 DCHECK_EQ(0, entry->skeleton.prefix1);
1315 DCHECK_EQ(0, entry->skeleton.prefix2);
1316 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001317 if (entry->skeleton.extra_opcode1 != 0) {
1318 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
1319 if (entry->skeleton.extra_opcode2 != 0) {
1320 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
1321 }
1322 } else {
1323 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1324 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001325 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1326 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1327 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1328 break;
1329 case kReg: // lir operands - 0: reg
1330 emitOpReg(cUnit, entry, lir->operands[0]);
1331 break;
1332 case kMem: // lir operands - 0: base, 1: disp
1333 emitOpMem(cUnit, entry, lir->operands[0], lir->operands[1]);
1334 break;
1335 case kMemReg: // lir operands - 0: base, 1: disp, 2: reg
1336 emitMemReg(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1337 break;
Ian Rogersb41b33b2012-03-20 14:22:54 -07001338 case kArrayReg: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
1339 emitArrayReg(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2],
1340 lir->operands[3], lir->operands[4]);
1341 break;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001342 case kRegMem: // lir operands - 0: reg, 1: base, 2: disp
1343 emitRegMem(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1344 break;
1345 case kRegArray: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
1346 emitRegArray(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2],
1347 lir->operands[3], lir->operands[4]);
1348 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001349 case kRegThread: // lir operands - 0: reg, 1: disp
1350 emitRegThread(cUnit, entry, lir->operands[0], lir->operands[1]);
1351 break;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001352 case kRegReg: // lir operands - 0: reg1, 1: reg2
1353 emitRegReg(cUnit, entry, lir->operands[0], lir->operands[1]);
1354 break;
jeffhaofdffdf82012-07-11 16:08:43 -07001355 case kRegRegStore: // lir operands - 0: reg2, 1: reg1
1356 emitRegReg(cUnit, entry, lir->operands[1], lir->operands[0]);
1357 break;
Elliott Hughes225ae522012-04-16 20:21:45 -07001358 case kRegRegImm:
1359 emitRegRegImm(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1360 break;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001361 case kRegImm: // lir operands - 0: reg, 1: immediate
1362 emitRegImm(cUnit, entry, lir->operands[0], lir->operands[1]);
1363 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001364 case kThreadImm: // lir operands - 0: disp, 1: immediate
1365 emitThreadImm(cUnit, entry, lir->operands[0], lir->operands[1]);
1366 break;
1367 case kMovRegImm: // lir operands - 0: reg, 1: immediate
1368 emitMovRegImm(cUnit, entry, lir->operands[0], lir->operands[1]);
1369 break;
Ian Rogersb41b33b2012-03-20 14:22:54 -07001370 case kShiftRegImm: // lir operands - 0: reg, 1: immediate
1371 emitShiftRegImm(cUnit, entry, lir->operands[0], lir->operands[1]);
1372 break;
Ian Rogers7caad772012-03-30 01:07:54 -07001373 case kShiftRegCl: // lir operands - 0: reg, 1: cl
1374 emitShiftRegCl(cUnit, entry, lir->operands[0], lir->operands[1]);
1375 break;
1376 case kRegCond: // lir operands - 0: reg, 1: condition
1377 emitRegCond(cUnit, entry, lir->operands[0], lir->operands[1]);
1378 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001379 case kJmp: // lir operands - 0: rel
1380 emitJmp(cUnit, entry, lir->operands[0]);
1381 break;
1382 case kJcc: // lir operands - 0: rel, 1: CC, target assigned
1383 emitJcc(cUnit, entry, lir->operands[0], lir->operands[1]);
1384 break;
1385 case kCall:
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001386 switch (entry->opcode) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001387 case kX86CallM: // lir operands - 0: base, 1: disp
1388 emitCallMem(cUnit, entry, lir->operands[0], lir->operands[1]);
1389 break;
1390 case kX86CallT: // lir operands - 0: disp
1391 emitCallThread(cUnit, entry, lir->operands[0]);
1392 break;
1393 default:
1394 emitUnimplemented(cUnit, entry, lir);
1395 break;
1396 }
1397 break;
Ian Rogers7caad772012-03-30 01:07:54 -07001398 case kPcRel: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
1399 emitPcRel(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2],
1400 lir->operands[3], lir->operands[4]);
1401 break;
1402 case kMacro:
1403 emitMacro(cUnit, entry, lir->operands[0], lir->offset);
1404 break;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001405 default:
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001406 emitUnimplemented(cUnit, entry, lir);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001407 break;
1408 }
Ian Rogers7caad772012-03-30 01:07:54 -07001409 CHECK_EQ(static_cast<size_t>(oatGetInsnSize(lir)),
1410 cUnit->codeBuffer.size() - starting_cbuf_size)
1411 << "Instruction size mismatch for entry: " << EncodingMap[lir->opcode].name;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001412 }
1413 return res;
buzbeee88dfbf2012-03-05 11:19:57 -08001414}
1415
buzbeee88dfbf2012-03-05 11:19:57 -08001416/*
1417 * Target-dependent offset assignment.
1418 * independent.
1419 */
1420int oatAssignInsnOffsets(CompilationUnit* cUnit)
1421{
1422 LIR* x86LIR;
1423 int offset = 0;
1424
1425 for (x86LIR = (LIR *) cUnit->firstLIRInsn;
1426 x86LIR;
1427 x86LIR = NEXT_LIR(x86LIR)) {
1428 x86LIR->offset = offset;
1429 if (x86LIR->opcode >= 0) {
1430 if (!x86LIR->flags.isNop) {
1431 offset += x86LIR->flags.size;
1432 }
1433 } else if (x86LIR->opcode == kPseudoPseudoAlign4) {
1434 if (offset & 0x2) {
1435 offset += 2;
1436 x86LIR->operands[0] = 1;
1437 } else {
1438 x86LIR->operands[0] = 0;
1439 }
1440 }
1441 /* Pseudo opcodes don't consume space */
1442 }
1443
1444 return offset;
1445}
1446
1447} // namespace art