blob: a245660d7e53f323fe8b9cd169a29a9b8cd487cc [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),
301 EXT_0F_ENCODING_MAP(Movzx8, 0x00, 0xB6, REG_DEF0),
302 EXT_0F_ENCODING_MAP(Movzx16, 0x00, 0xB7, REG_DEF0),
303 EXT_0F_ENCODING_MAP(Movsx8, 0x00, 0xBE, REG_DEF0),
304 EXT_0F_ENCODING_MAP(Movsx16, 0x00, 0xBF, REG_DEF0),
Ian Rogersb5d09b22012-03-06 22:14:17 -0800305#undef EXT_0F_ENCODING_MAP
306
jeffhaoe2962482012-06-28 11:29:57 -0700307 { kX86Jcc8, kJcc, IS_BINARY_OP | IS_BRANCH | NEEDS_FIXUP | USES_CCODES, { 0, 0, 0x70, 0, 0, 0, 0, 0 }, "Jcc8", "!1c !0t" },
308 { kX86Jcc32, kJcc, IS_BINARY_OP | IS_BRANCH | NEEDS_FIXUP | USES_CCODES, { 0, 0, 0x0F, 0x80, 0, 0, 0, 0 }, "Jcc32", "!1c !0t" },
309 { kX86Jmp8, kJmp, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0xEB, 0, 0, 0, 0, 0 }, "Jmp8", "!0t" },
310 { kX86Jmp32, kJmp, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0xE9, 0, 0, 0, 0, 0 }, "Jmp32", "!0t" },
311 { kX86JmpR, kJmp, IS_UNARY_OP | IS_BRANCH | REG_USE0, { 0, 0, 0xFF, 0, 0, 4, 0, 0 }, "JmpR", "!0r" },
312 { kX86CallR, kCall, IS_UNARY_OP | IS_BRANCH | REG_USE0, { 0, 0, 0xE8, 0, 0, 0, 0, 0 }, "CallR", "!0r" },
313 { kX86CallM, kCall, IS_BINARY_OP | IS_BRANCH | IS_LOAD | REG_USE0, { 0, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallM", "[!0r+!1d]" },
314 { kX86CallA, kCall, IS_QUAD_OP | IS_BRANCH | IS_LOAD | REG_USE01, { 0, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallA", "[!0r+!1r<<!2d+!3d]" },
315 { kX86CallT, kCall, IS_UNARY_OP | IS_BRANCH | IS_LOAD, { THREAD_PREFIX, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallT", "fs:[!0d]" },
316 { kX86Ret, kNullary,NO_OPERAND | IS_BRANCH, { 0, 0, 0xC3, 0, 0, 0, 0, 0 }, "Ret", "" },
Ian Rogers7caad772012-03-30 01:07:54 -0700317
jeffhaoe2962482012-06-28 11:29:57 -0700318 { kX86StartOfMethod, kMacro, IS_UNARY_OP | SETS_CCODES, { 0, 0, 0, 0, 0, 0, 0, 0 }, "StartOfMethod", "!0r" },
319 { kX86PcRelLoadRA, kPcRel, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "PcRelLoadRA", "!0r,[!1r+!2r<<!3d+!4p]" },
320 { 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 -0800321};
322
Ian Rogersb5d09b22012-03-06 22:14:17 -0800323static size_t computeSize(X86EncodingMap* entry, int displacement, bool has_sib) {
324 size_t size = 0;
325 if (entry->skeleton.prefix1 > 0) {
326 ++size;
327 if (entry->skeleton.prefix2 > 0) {
328 ++size;
Ian Rogersde797832012-03-06 10:18:10 -0800329 }
Ian Rogersde797832012-03-06 10:18:10 -0800330 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800331 ++size; // opcode
332 if (entry->skeleton.opcode == 0x0F) {
333 ++size;
334 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
335 ++size;
336 }
337 }
338 ++size; // modrm
339 if (has_sib) {
340 ++size;
341 }
342 if (displacement != 0) {
343 if (entry->opcode != kX86Lea32RA) {
Ian Rogers7caad772012-03-30 01:07:54 -0700344 DCHECK_NE(entry->flags & (IS_LOAD | IS_STORE), 0) << entry->name;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800345 }
346 size += IS_SIMM8(displacement) ? 1 : 4;
347 }
348 size += entry->skeleton.immediate_bytes;
349 return size;
350}
351
352int oatGetInsnSize(LIR* lir) {
353 X86EncodingMap* entry = &EncodingMap[lir->opcode];
354 switch (entry->kind) {
355 case kData:
356 return 4; // 4 bytes of data
357 case kNop:
358 return lir->operands[0]; // length of nop is sole operand
359 case kNullary:
360 return 1; // 1 byte of opcode
361 case kReg: // lir operands - 0: reg
362 return computeSize(entry, 0, false);
363 case kMem: { // lir operands - 0: base, 1: disp
364 int base = lir->operands[0];
jeffhao703f2cd2012-07-13 17:25:52 -0700365 int disp = lir->operands[1];
366 // SP requires a special extra SIB byte. BP requires explicit disp,
367 // so add a byte for disp 0 which would normally be omitted.
368 return computeSize(entry, disp, false) + ((base == rSP) || (base == rBP && disp == 0) ? 1 : 0);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800369 }
370 case kArray: // lir operands - 0: base, 1: index, 2: scale, 3: disp
371 return computeSize(entry, lir->operands[3], true);
372 case kMemReg: { // lir operands - 0: base, 1: disp, 2: reg
373 int base = lir->operands[0];
jeffhao703f2cd2012-07-13 17:25:52 -0700374 int disp = lir->operands[1];
375 // SP requires a special extra SIB byte. BP requires explicit disp,
376 // so add a byte for disp 0 which would normally be omitted.
377 return computeSize(entry, disp, false) + ((base == rSP) || (base == rBP && disp == 0) ? 1 : 0);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800378 }
379 case kArrayReg: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
380 return computeSize(entry, lir->operands[3], true);
381 case kThreadReg: // lir operands - 0: disp, 1: reg
382 return computeSize(entry, lir->operands[0], false);
383 case kRegReg:
384 return computeSize(entry, 0, false);
jeffhaofdffdf82012-07-11 16:08:43 -0700385 case kRegRegStore:
386 return computeSize(entry, 0, false);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800387 case kRegMem: { // lir operands - 0: reg, 1: base, 2: disp
388 int base = lir->operands[1];
jeffhao703f2cd2012-07-13 17:25:52 -0700389 int disp = lir->operands[2];
390 // SP requires a special extra SIB byte. BP requires explicit disp,
391 // so add a byte for disp 0 which would normally be omitted.
392 return computeSize(entry, disp, false) + ((base == rSP) || (base == rBP && disp == 0) ? 1 : 0);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800393 }
jeffhao703f2cd2012-07-13 17:25:52 -0700394 case kRegArray: { // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
395 int base = lir->operands[1];
396 int disp = lir->operands[4];
397 // BP requires explicit disp, so add a byte for disp 0 which would normally be omitted.
398 return computeSize(entry, disp, true) + ((base == rBP && disp == 0) ? 1 : 0);
399 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800400 case kRegThread: // lir operands - 0: reg, 1: disp
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700401 return computeSize(entry, 0x12345678, false); // displacement size is always 32bit
Ian Rogersb5d09b22012-03-06 22:14:17 -0800402 case kRegImm: { // lir operands - 0: reg, 1: immediate
Ian Rogersb41b33b2012-03-20 14:22:54 -0700403 size_t size = computeSize(entry, 0, false);
404 if (entry->skeleton.ax_opcode == 0) {
405 return size;
406 } else {
407 // AX opcodes don't require the modrm byte.
408 int reg = lir->operands[0];
409 return size - (reg == rAX ? 1 : 0);
410 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800411 }
412 case kMemImm: // lir operands - 0: base, 1: disp, 2: immediate
413 CHECK_NE(lir->operands[0], static_cast<int>(rSP)); // TODO: add extra SIB byte
414 return computeSize(entry, lir->operands[1], false);
415 case kArrayImm: // lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
416 return computeSize(entry, lir->operands[3], true);
417 case kThreadImm: // lir operands - 0: disp, 1: imm
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700418 return computeSize(entry, 0x12345678, false); // displacement size is always 32bit
Ian Rogersb5d09b22012-03-06 22:14:17 -0800419 case kRegRegImm: // lir operands - 0: reg, 1: reg, 2: imm
420 return computeSize(entry, 0, false);
421 case kRegMemImm: // lir operands - 0: reg, 1: base, 2: disp, 3: imm
422 CHECK_NE(lir->operands[1], static_cast<int>(rSP)); // TODO: add extra SIB byte
423 return computeSize(entry, lir->operands[2], false);
424 case kRegArrayImm: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp, 5: imm
425 return computeSize(entry, lir->operands[4], true);
426 case kMovRegImm: // lir operands - 0: reg, 1: immediate
427 return 1 + entry->skeleton.immediate_bytes;
428 case kShiftRegImm: // lir operands - 0: reg, 1: immediate
429 // Shift by immediate one has a shorter opcode.
430 return computeSize(entry, 0, false) - (lir->operands[1] == 1 ? 1 : 0);
431 case kShiftMemImm: // lir operands - 0: base, 1: disp, 2: immediate
432 CHECK_NE(lir->operands[0], static_cast<int>(rSP)); // TODO: add extra SIB byte
433 // Shift by immediate one has a shorter opcode.
434 return computeSize(entry, lir->operands[1], false) - (lir->operands[2] == 1 ? 1 : 0);
435 case kShiftArrayImm: // lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
436 // Shift by immediate one has a shorter opcode.
437 return computeSize(entry, lir->operands[3], true) - (lir->operands[4] == 1 ? 1 : 0);
438 case kShiftRegCl:
439 return computeSize(entry, 0, false);
440 case kShiftMemCl: // lir operands - 0: base, 1: disp, 2: cl
441 CHECK_NE(lir->operands[0], static_cast<int>(rSP)); // TODO: add extra SIB byte
442 return computeSize(entry, lir->operands[1], false);
443 case kShiftArrayCl: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
444 return computeSize(entry, lir->operands[3], true);
445 case kRegCond: // lir operands - 0: reg, 1: cond
446 return computeSize(entry, 0, false);
447 case kMemCond: // lir operands - 0: base, 1: disp, 2: cond
448 CHECK_NE(lir->operands[0], static_cast<int>(rSP)); // TODO: add extra SIB byte
449 return computeSize(entry, lir->operands[1], false);
450 case kArrayCond: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: cond
451 return computeSize(entry, lir->operands[3], true);
Ian Rogersb41b33b2012-03-20 14:22:54 -0700452 case kJcc:
453 if (lir->opcode == kX86Jcc8) {
454 return 2; // opcode + rel8
455 } else {
456 DCHECK(lir->opcode == kX86Jcc32);
457 return 6; // 2 byte opcode + rel32
458 }
459 case kJmp:
460 if (lir->opcode == kX86Jmp8) {
461 return 2; // opcode + rel8
Ian Rogers7caad772012-03-30 01:07:54 -0700462 } else if (lir->opcode == kX86Jmp32) {
Ian Rogersb41b33b2012-03-20 14:22:54 -0700463 return 5; // opcode + rel32
Ian Rogers7caad772012-03-30 01:07:54 -0700464 } else {
465 DCHECK(lir->opcode == kX86JmpR);
466 return 2; // opcode + modrm
Ian Rogersb41b33b2012-03-20 14:22:54 -0700467 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800468 case kCall:
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700469 switch (lir->opcode) {
Ian Rogersb5d09b22012-03-06 22:14:17 -0800470 case kX86CallR: return 2; // opcode modrm
471 case kX86CallM: // lir operands - 0: base, 1: disp
472 return computeSize(entry, lir->operands[1], false);
473 case kX86CallA: // lir operands - 0: base, 1: index, 2: scale, 3: disp
474 return computeSize(entry, lir->operands[3], true);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700475 case kX86CallT: // lir operands - 0: disp
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700476 return computeSize(entry, 0x12345678, false); // displacement size is always 32bit
Ian Rogersb5d09b22012-03-06 22:14:17 -0800477 default:
478 break;
479 }
480 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700481 case kPcRel:
482 if (entry->opcode == kX86PcRelLoadRA) {
483 // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
484 return computeSize(entry, 0x12345678, true);
485 } else {
486 DCHECK(entry->opcode == kX86PcRelAdr);
487 return 5; // opcode with reg + 4 byte immediate
488 }
489 case kMacro:
490 DCHECK_EQ(lir->opcode, static_cast<int>(kX86StartOfMethod));
491 return 5 /* call opcode + 4 byte displacement */ + 1 /* pop reg */ +
492 computeSize(&EncodingMap[kX86Sub32RI], 0, false) -
493 (lir->operands[0] == rAX ? 1 : 0); // shorter ax encoding
Ian Rogersb5d09b22012-03-06 22:14:17 -0800494 default:
495 break;
496 }
497 UNIMPLEMENTED(FATAL) << "Unimplemented size encoding for: " << entry->name;
Ian Rogersde797832012-03-06 10:18:10 -0800498 return 0;
499}
buzbeee88dfbf2012-03-05 11:19:57 -0800500
jeffhao703f2cd2012-07-13 17:25:52 -0700501static uint8_t modrmForDisp(int base, int disp) {
502 // BP requires an explicit disp, so do not omit it in the 0 case
503 if (disp == 0 && base != rBP) {
Ian Rogersb5d09b22012-03-06 22:14:17 -0800504 return 0;
505 } else if (IS_SIMM8(disp)) {
506 return 1;
507 } else {
508 return 2;
509 }
510}
511
jeffhao703f2cd2012-07-13 17:25:52 -0700512static void emitDisp(CompilationUnit* cUnit, 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;
516 } else if (IS_SIMM8(disp)) {
517 cUnit->codeBuffer.push_back(disp & 0xFF);
518 } else {
519 cUnit->codeBuffer.push_back(disp & 0xFF);
520 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
521 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
522 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
523 }
524}
525
526static void emitOpReg(CompilationUnit* cUnit, const X86EncodingMap* entry, uint8_t reg) {
527 if (entry->skeleton.prefix1 != 0) {
528 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
529 if (entry->skeleton.prefix2 != 0) {
530 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
531 }
532 } else {
533 DCHECK_EQ(0, entry->skeleton.prefix2);
534 }
535 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
536 if (entry->skeleton.opcode == 0x0F) {
537 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
538 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
539 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
540 } else {
541 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
542 }
543 } else {
544 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
545 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
546 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700547 if (FPREG(reg)) {
548 reg = reg & FP_REG_MASK;
549 }
jeffhao703f2cd2012-07-13 17:25:52 -0700550 if (reg >= 4) {
551 DCHECK(strchr(entry->name, '8') == NULL) << entry->name << " " << (int) reg
552 << " in " << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
553 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800554 DCHECK_LT(reg, 8);
555 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
556 cUnit->codeBuffer.push_back(modrm);
557 DCHECK_EQ(0, entry->skeleton.ax_opcode);
558 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
559}
560
561static void emitOpMem(CompilationUnit* cUnit, const X86EncodingMap* entry, uint8_t base, int disp) {
562 if (entry->skeleton.prefix1 != 0) {
563 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
564 if (entry->skeleton.prefix2 != 0) {
565 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
566 }
567 } else {
568 DCHECK_EQ(0, entry->skeleton.prefix2);
569 }
570 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
571 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
572 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
573 DCHECK_LT(entry->skeleton.modrm_opcode, 8);
574 DCHECK_LT(base, 8);
jeffhao703f2cd2012-07-13 17:25:52 -0700575 uint8_t modrm = (modrmForDisp(base, disp) << 6) | (entry->skeleton.modrm_opcode << 3) | base;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800576 cUnit->codeBuffer.push_back(modrm);
jeffhao703f2cd2012-07-13 17:25:52 -0700577 emitDisp(cUnit, base, disp);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800578 DCHECK_EQ(0, entry->skeleton.ax_opcode);
579 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
580}
581
582static void emitMemReg(CompilationUnit* cUnit, const X86EncodingMap* entry,
583 uint8_t base, int disp, uint8_t reg) {
584 if (entry->skeleton.prefix1 != 0) {
585 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
586 if (entry->skeleton.prefix2 != 0) {
587 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
588 }
589 } else {
590 DCHECK_EQ(0, entry->skeleton.prefix2);
591 }
592 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
593 if (entry->skeleton.opcode == 0x0F) {
594 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
595 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
596 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
597 } else {
598 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
599 }
600 } else {
601 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
602 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
603 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700604 if (FPREG(reg)) {
605 reg = reg & FP_REG_MASK;
606 }
jeffhao703f2cd2012-07-13 17:25:52 -0700607 if (reg >= 4) {
608 DCHECK(strchr(entry->name, '8') == NULL) << entry->name << " " << (int) reg
609 << " in " << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
610 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800611 DCHECK_LT(reg, 8);
612 DCHECK_LT(base, 8);
jeffhao703f2cd2012-07-13 17:25:52 -0700613 uint8_t modrm = (modrmForDisp(base, disp) << 6) | (reg << 3) | base;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800614 cUnit->codeBuffer.push_back(modrm);
615 if (base == rSP) {
616 // Special SIB for SP base
617 cUnit->codeBuffer.push_back(0 << 6 | (rSP << 3) | rSP);
618 }
jeffhao703f2cd2012-07-13 17:25:52 -0700619 emitDisp(cUnit, base, disp);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800620 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
621 DCHECK_EQ(0, entry->skeleton.ax_opcode);
622 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
623}
624
625static void emitRegMem(CompilationUnit* cUnit, const X86EncodingMap* entry,
626 uint8_t reg, uint8_t base, int disp) {
627 // Opcode will flip operands.
628 emitMemReg(cUnit, entry, base, disp, reg);
629}
630
631static void emitRegArray(CompilationUnit* cUnit, const X86EncodingMap* entry, uint8_t reg,
632 uint8_t base, uint8_t index, int scale, int disp) {
633 if (entry->skeleton.prefix1 != 0) {
634 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
635 if (entry->skeleton.prefix2 != 0) {
636 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
637 }
638 } else {
639 DCHECK_EQ(0, entry->skeleton.prefix2);
640 }
641 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
642 if (entry->skeleton.opcode == 0x0F) {
643 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
644 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
645 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
646 } else {
647 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
648 }
649 } else {
650 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
651 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
652 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700653 if (FPREG(reg)) {
654 reg = reg & FP_REG_MASK;
655 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800656 DCHECK_LT(reg, 8);
jeffhao703f2cd2012-07-13 17:25:52 -0700657 uint8_t modrm = (modrmForDisp(base, disp) << 6) | (reg << 3) | rSP;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800658 cUnit->codeBuffer.push_back(modrm);
659 DCHECK_LT(scale, 4);
660 DCHECK_LT(index, 8);
661 DCHECK_LT(base, 8);
662 uint8_t sib = (scale << 6) | (index << 3) | base;
663 cUnit->codeBuffer.push_back(sib);
jeffhao703f2cd2012-07-13 17:25:52 -0700664 emitDisp(cUnit, base, disp);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800665 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
666 DCHECK_EQ(0, entry->skeleton.ax_opcode);
667 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
668}
669
Ian Rogersb41b33b2012-03-20 14:22:54 -0700670static void emitArrayReg(CompilationUnit* cUnit, const X86EncodingMap* entry,
671 uint8_t base, uint8_t index, int scale, int disp, uint8_t reg) {
672 // Opcode will flip operands.
673 emitRegArray(cUnit, entry, reg, base, index, scale, disp);
674}
675
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700676static void emitRegThread(CompilationUnit* cUnit, const X86EncodingMap* entry,
677 uint8_t reg, int disp) {
678 DCHECK_NE(entry->skeleton.prefix1, 0);
679 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
680 if (entry->skeleton.prefix2 != 0) {
681 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
682 }
683 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
684 if (entry->skeleton.opcode == 0x0F) {
685 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
686 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
687 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
688 } else {
689 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
690 }
691 } else {
692 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
693 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
694 }
695 if (FPREG(reg)) {
696 reg = reg & FP_REG_MASK;
697 }
jeffhao703f2cd2012-07-13 17:25:52 -0700698 if (reg >= 4) {
699 DCHECK(strchr(entry->name, '8') == NULL) << entry->name << " " << (int) reg
700 << " in " << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
701 }
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700702 DCHECK_LT(reg, 8);
703 uint8_t modrm = (0 << 6) | (reg << 3) | rBP;
704 cUnit->codeBuffer.push_back(modrm);
705 cUnit->codeBuffer.push_back(disp & 0xFF);
706 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
707 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
708 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
709 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
710 DCHECK_EQ(0, entry->skeleton.ax_opcode);
711 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
712}
713
Ian Rogersb5d09b22012-03-06 22:14:17 -0800714static void emitRegReg(CompilationUnit* cUnit, const X86EncodingMap* entry,
715 uint8_t reg1, uint8_t reg2) {
716 if (entry->skeleton.prefix1 != 0) {
717 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
718 if (entry->skeleton.prefix2 != 0) {
719 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
720 }
721 } else {
722 DCHECK_EQ(0, entry->skeleton.prefix2);
723 }
724 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
725 if (entry->skeleton.opcode == 0x0F) {
726 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
727 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
728 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
729 } else {
730 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
731 }
732 } else {
733 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
734 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
735 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700736 if (FPREG(reg1)) {
737 reg1 = reg1 & FP_REG_MASK;
738 }
739 if (FPREG(reg2)) {
740 reg2 = reg2 & FP_REG_MASK;
741 }
742 DCHECK_LT(reg1, 8);
743 DCHECK_LT(reg2, 8);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800744 uint8_t modrm = (3 << 6) | (reg1 << 3) | reg2;
745 cUnit->codeBuffer.push_back(modrm);
746 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
747 DCHECK_EQ(0, entry->skeleton.ax_opcode);
748 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
749}
750
Elliott Hughes225ae522012-04-16 20:21:45 -0700751static void emitRegRegImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
752 uint8_t reg1, uint8_t reg2, int32_t imm) {
753 if (entry->skeleton.prefix1 != 0) {
754 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
755 if (entry->skeleton.prefix2 != 0) {
756 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
757 }
758 } else {
759 DCHECK_EQ(0, entry->skeleton.prefix2);
760 }
761 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
762 if (entry->skeleton.opcode == 0x0F) {
763 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
764 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
765 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
766 } else {
767 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
768 }
769 } else {
770 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
771 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
772 }
773 if (FPREG(reg1)) {
774 reg1 = reg1 & FP_REG_MASK;
775 }
776 if (FPREG(reg2)) {
777 reg2 = reg2 & FP_REG_MASK;
778 }
779 DCHECK_LT(reg1, 8);
780 DCHECK_LT(reg2, 8);
781 uint8_t modrm = (3 << 6) | (reg1 << 3) | reg2;
782 cUnit->codeBuffer.push_back(modrm);
783 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
784 DCHECK_EQ(0, entry->skeleton.ax_opcode);
785 switch (entry->skeleton.immediate_bytes) {
786 case 1:
787 DCHECK(IS_SIMM8(imm));
788 cUnit->codeBuffer.push_back(imm & 0xFF);
789 break;
790 case 2:
791 DCHECK(IS_SIMM16(imm));
792 cUnit->codeBuffer.push_back(imm & 0xFF);
793 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
794 break;
795 case 4:
796 cUnit->codeBuffer.push_back(imm & 0xFF);
797 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
798 cUnit->codeBuffer.push_back((imm >> 16) & 0xFF);
799 cUnit->codeBuffer.push_back((imm >> 24) & 0xFF);
800 break;
801 default:
802 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
803 << ") for instruction: " << entry->name;
804 break;
805 }
806}
807
Ian Rogersb5d09b22012-03-06 22:14:17 -0800808static void emitRegImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
809 uint8_t reg, int imm) {
810 if (entry->skeleton.prefix1 != 0) {
811 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
812 if (entry->skeleton.prefix2 != 0) {
813 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
814 }
815 } else {
816 DCHECK_EQ(0, entry->skeleton.prefix2);
817 }
818 if (reg == rAX && entry->skeleton.ax_opcode != 0) {
819 cUnit->codeBuffer.push_back(entry->skeleton.ax_opcode);
820 } else {
821 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
822 if (entry->skeleton.opcode == 0x0F) {
823 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
824 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
825 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
826 } else {
827 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
828 }
829 } else {
830 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
831 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
832 }
jeffhaofdffdf82012-07-11 16:08:43 -0700833 if (FPREG(reg)) {
834 reg = reg & FP_REG_MASK;
835 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800836 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
837 cUnit->codeBuffer.push_back(modrm);
838 }
839 switch (entry->skeleton.immediate_bytes) {
840 case 1:
841 DCHECK(IS_SIMM8(imm));
842 cUnit->codeBuffer.push_back(imm & 0xFF);
843 break;
844 case 2:
845 DCHECK(IS_SIMM16(imm));
846 cUnit->codeBuffer.push_back(imm & 0xFF);
847 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
848 break;
849 case 4:
850 cUnit->codeBuffer.push_back(imm & 0xFF);
851 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
852 cUnit->codeBuffer.push_back((imm >> 16) & 0xFF);
853 cUnit->codeBuffer.push_back((imm >> 24) & 0xFF);
854 break;
855 default:
856 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
857 << ") for instruction: " << entry->name;
858 break;
859 }
860}
861
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700862static void emitThreadImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
863 int disp, int imm) {
864 if (entry->skeleton.prefix1 != 0) {
865 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
866 if (entry->skeleton.prefix2 != 0) {
867 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
868 }
869 } else {
870 DCHECK_EQ(0, entry->skeleton.prefix2);
871 }
872 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
873 if (entry->skeleton.opcode == 0x0F) {
874 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
875 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
876 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
877 } else {
878 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
879 }
880 } else {
881 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
882 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
883 }
884 uint8_t modrm = (0 << 6) | (entry->skeleton.modrm_opcode << 3) | rBP;
885 cUnit->codeBuffer.push_back(modrm);
886 cUnit->codeBuffer.push_back(disp & 0xFF);
887 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
888 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
889 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
890 switch (entry->skeleton.immediate_bytes) {
891 case 1:
892 DCHECK(IS_SIMM8(imm));
893 cUnit->codeBuffer.push_back(imm & 0xFF);
894 break;
895 case 2:
896 DCHECK(IS_SIMM16(imm));
897 cUnit->codeBuffer.push_back(imm & 0xFF);
898 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
899 break;
900 case 4:
901 cUnit->codeBuffer.push_back(imm & 0xFF);
902 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
903 cUnit->codeBuffer.push_back((imm >> 16) & 0xFF);
904 cUnit->codeBuffer.push_back((imm >> 24) & 0xFF);
905 break;
906 default:
907 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
908 << ") for instruction: " << entry->name;
909 break;
910 }
911 DCHECK_EQ(entry->skeleton.ax_opcode, 0);
912}
913
914static void emitMovRegImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
915 uint8_t reg, int imm) {
916 DCHECK_LT(reg, 8);
917 cUnit->codeBuffer.push_back(0xB8 + reg);
918 cUnit->codeBuffer.push_back(imm & 0xFF);
919 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
920 cUnit->codeBuffer.push_back((imm >> 16) & 0xFF);
921 cUnit->codeBuffer.push_back((imm >> 24) & 0xFF);
922}
923
Ian Rogersb41b33b2012-03-20 14:22:54 -0700924static void emitShiftRegImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
Ian Rogers7caad772012-03-30 01:07:54 -0700925 uint8_t reg, int imm) {
Ian Rogersb41b33b2012-03-20 14:22:54 -0700926 if (entry->skeleton.prefix1 != 0) {
927 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
928 if (entry->skeleton.prefix2 != 0) {
929 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
930 }
931 } else {
932 DCHECK_EQ(0, entry->skeleton.prefix2);
933 }
934 if (imm != 1) {
935 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
936 } else {
937 // Shorter encoding for 1 bit shift
938 cUnit->codeBuffer.push_back(entry->skeleton.ax_opcode);
939 }
940 if (entry->skeleton.opcode == 0x0F) {
941 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
942 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
943 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
944 } else {
945 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
946 }
947 } else {
948 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
949 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
950 }
jeffhao703f2cd2012-07-13 17:25:52 -0700951 if (reg >= 4) {
952 DCHECK(strchr(entry->name, '8') == NULL) << entry->name << " " << (int) reg
953 << " in " << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
954 }
Ian Rogersb41b33b2012-03-20 14:22:54 -0700955 DCHECK_LT(reg, 8);
Ian Rogers7caad772012-03-30 01:07:54 -0700956 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
Ian Rogersb41b33b2012-03-20 14:22:54 -0700957 cUnit->codeBuffer.push_back(modrm);
958 if (imm != 1) {
959 DCHECK_EQ(entry->skeleton.immediate_bytes, 1);
960 DCHECK(IS_SIMM8(imm));
961 cUnit->codeBuffer.push_back(imm & 0xFF);
962 }
963}
964
Ian Rogers7caad772012-03-30 01:07:54 -0700965static void emitShiftRegCl(CompilationUnit* cUnit, const X86EncodingMap* entry,
966 uint8_t reg, uint8_t cl) {
967 DCHECK_EQ(cl, static_cast<uint8_t>(rCX));
968 if (entry->skeleton.prefix1 != 0) {
969 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
970 if (entry->skeleton.prefix2 != 0) {
971 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
972 }
973 } else {
974 DCHECK_EQ(0, entry->skeleton.prefix2);
975 }
976 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
977 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
978 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
979 DCHECK_LT(reg, 8);
980 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
981 cUnit->codeBuffer.push_back(modrm);
982 DCHECK_EQ(0, entry->skeleton.ax_opcode);
983 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
984}
985
986static void emitRegCond(CompilationUnit* cUnit, const X86EncodingMap* entry,
987 uint8_t reg, uint8_t condition) {
988 if (entry->skeleton.prefix1 != 0) {
989 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
990 if (entry->skeleton.prefix2 != 0) {
991 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
992 }
993 } else {
994 DCHECK_EQ(0, entry->skeleton.prefix2);
995 }
996 DCHECK_EQ(0, entry->skeleton.ax_opcode);
997 DCHECK_EQ(0x0F, entry->skeleton.opcode);
998 cUnit->codeBuffer.push_back(0x0F);
999 DCHECK_EQ(0x90, entry->skeleton.extra_opcode1);
1000 cUnit->codeBuffer.push_back(0x90 | condition);
1001 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1002 DCHECK_LT(reg, 8);
1003 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
1004 cUnit->codeBuffer.push_back(modrm);
1005 DCHECK_EQ(entry->skeleton.immediate_bytes, 0);
1006}
1007
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001008static void emitJmp(CompilationUnit* cUnit, const X86EncodingMap* entry, int rel) {
Ian Rogersb41b33b2012-03-20 14:22:54 -07001009 if (entry->opcode == kX86Jmp8) {
1010 DCHECK(IS_SIMM8(rel));
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001011 cUnit->codeBuffer.push_back(0xEB);
1012 cUnit->codeBuffer.push_back(rel & 0xFF);
Ian Rogers7caad772012-03-30 01:07:54 -07001013 } else if (entry->opcode == kX86Jmp32) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001014 cUnit->codeBuffer.push_back(0xE9);
1015 cUnit->codeBuffer.push_back(rel & 0xFF);
1016 cUnit->codeBuffer.push_back((rel >> 8) & 0xFF);
1017 cUnit->codeBuffer.push_back((rel >> 16) & 0xFF);
1018 cUnit->codeBuffer.push_back((rel >> 24) & 0xFF);
Ian Rogers7caad772012-03-30 01:07:54 -07001019 } else {
1020 DCHECK(entry->opcode == kX86JmpR);
1021 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
1022 uint8_t reg = static_cast<uint8_t>(rel);
1023 DCHECK_LT(reg, 8);
1024 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
1025 cUnit->codeBuffer.push_back(modrm);
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001026 }
1027}
1028
1029static void emitJcc(CompilationUnit* cUnit, const X86EncodingMap* entry,
1030 int rel, uint8_t cc) {
1031 DCHECK_LT(cc, 16);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001032 if (entry->opcode == kX86Jcc8) {
1033 DCHECK(IS_SIMM8(rel));
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001034 cUnit->codeBuffer.push_back(0x70 | cc);
1035 cUnit->codeBuffer.push_back(rel & 0xFF);
1036 } else {
Ian Rogersb41b33b2012-03-20 14:22:54 -07001037 DCHECK(entry->opcode == kX86Jcc32);
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001038 cUnit->codeBuffer.push_back(0x0F);
1039 cUnit->codeBuffer.push_back(0x80 | cc);
1040 cUnit->codeBuffer.push_back(rel & 0xFF);
1041 cUnit->codeBuffer.push_back((rel >> 8) & 0xFF);
1042 cUnit->codeBuffer.push_back((rel >> 16) & 0xFF);
1043 cUnit->codeBuffer.push_back((rel >> 24) & 0xFF);
1044 }
1045}
1046
1047static void emitCallMem(CompilationUnit* cUnit, const X86EncodingMap* entry,
1048 uint8_t base, int disp) {
1049 if (entry->skeleton.prefix1 != 0) {
1050 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
1051 if (entry->skeleton.prefix2 != 0) {
1052 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
1053 }
1054 } else {
1055 DCHECK_EQ(0, entry->skeleton.prefix2);
1056 }
1057 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
1058 if (entry->skeleton.opcode == 0x0F) {
1059 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
1060 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
1061 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
1062 } else {
1063 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1064 }
1065 } else {
1066 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1067 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1068 }
jeffhao703f2cd2012-07-13 17:25:52 -07001069 uint8_t modrm = (modrmForDisp(base, disp) << 6) | (entry->skeleton.modrm_opcode << 3) | base;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001070 cUnit->codeBuffer.push_back(modrm);
1071 if (base == rSP) {
1072 // Special SIB for SP base
1073 cUnit->codeBuffer.push_back(0 << 6 | (rSP << 3) | rSP);
1074 }
jeffhao703f2cd2012-07-13 17:25:52 -07001075 emitDisp(cUnit, base, disp);
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001076 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1077 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1078}
1079
1080static void emitCallThread(CompilationUnit* cUnit, const X86EncodingMap* entry, int disp) {
1081 DCHECK_NE(entry->skeleton.prefix1, 0);
1082 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
1083 if (entry->skeleton.prefix2 != 0) {
1084 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
1085 }
1086 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
1087 if (entry->skeleton.opcode == 0x0F) {
1088 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
1089 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
1090 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
1091 } else {
1092 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1093 }
1094 } else {
1095 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1096 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1097 }
1098 uint8_t modrm = (0 << 6) | (entry->skeleton.modrm_opcode << 3) | rBP;
1099 cUnit->codeBuffer.push_back(modrm);
1100 cUnit->codeBuffer.push_back(disp & 0xFF);
1101 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
1102 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
1103 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
1104 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1105 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1106}
1107
Ian Rogers7caad772012-03-30 01:07:54 -07001108static void emitPcRel(CompilationUnit* cUnit, const X86EncodingMap* entry, uint8_t reg,
1109 int base_or_table, uint8_t index, int scale, int table_or_disp) {
1110 int disp;
1111 if (entry->opcode == kX86PcRelLoadRA) {
1112 SwitchTable *tabRec = (SwitchTable*)table_or_disp;
1113 disp = tabRec->offset;
1114 } else {
1115 DCHECK(entry->opcode == kX86PcRelAdr);
1116 FillArrayData *tabRec = (FillArrayData *)base_or_table;
1117 disp = tabRec->offset;
1118 }
1119 if (entry->skeleton.prefix1 != 0) {
1120 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
1121 if (entry->skeleton.prefix2 != 0) {
1122 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
1123 }
1124 } else {
1125 DCHECK_EQ(0, entry->skeleton.prefix2);
1126 }
1127 if (FPREG(reg)) {
1128 reg = reg & FP_REG_MASK;
1129 }
1130 DCHECK_LT(reg, 8);
1131 if (entry->opcode == kX86PcRelLoadRA) {
1132 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
1133 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1134 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1135 uint8_t modrm = (2 << 6) | (reg << 3) | rSP;
1136 cUnit->codeBuffer.push_back(modrm);
1137 DCHECK_LT(scale, 4);
1138 DCHECK_LT(index, 8);
1139 DCHECK_LT(base_or_table, 8);
1140 uint8_t base = static_cast<uint8_t>(base_or_table);
1141 uint8_t sib = (scale << 6) | (index << 3) | base;
1142 cUnit->codeBuffer.push_back(sib);
1143 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1144 } else {
1145 cUnit->codeBuffer.push_back(entry->skeleton.opcode + reg);
1146 }
1147 cUnit->codeBuffer.push_back(disp & 0xFF);
1148 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
1149 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
1150 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
1151 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1152 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1153}
1154
1155static void emitMacro(CompilationUnit* cUnit, const X86EncodingMap* entry,
1156 uint8_t reg, int offset) {
1157 DCHECK(entry->opcode == kX86StartOfMethod) << entry->name;
1158 cUnit->codeBuffer.push_back(0xE8); // call +0
1159 cUnit->codeBuffer.push_back(0);
1160 cUnit->codeBuffer.push_back(0);
1161 cUnit->codeBuffer.push_back(0);
1162 cUnit->codeBuffer.push_back(0);
1163
1164 DCHECK_LT(reg, 8);
1165 cUnit->codeBuffer.push_back(0x58 + reg); // pop reg
1166
1167 emitRegImm(cUnit, &EncodingMap[kX86Sub32RI], reg, offset + 5 /* size of call +0 */);
1168}
1169
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001170void emitUnimplemented(CompilationUnit* cUnit, const X86EncodingMap* entry, LIR* lir) {
Elliott Hughes225ae522012-04-16 20:21:45 -07001171 UNIMPLEMENTED(WARNING) << "encoding kind for " << entry->name << " " << buildInsnString(entry->fmt, lir, 0);
Ian Rogers141b0c72012-03-15 18:18:52 -07001172 for (int i = 0; i < oatGetInsnSize(lir); ++i) {
1173 cUnit->codeBuffer.push_back(0xCC); // push breakpoint instruction - int 3
1174 }
1175}
1176
buzbeee88dfbf2012-03-05 11:19:57 -08001177/*
1178 * Assemble the LIR into binary instruction format. Note that we may
1179 * discover that pc-relative displacements may not fit the selected
1180 * instruction. In those cases we will try to substitute a new code
1181 * sequence or request that the trace be shortened and retried.
1182 */
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001183AssemblerStatus oatAssembleInstructions(CompilationUnit *cUnit, intptr_t startAddr) {
Ian Rogersb5d09b22012-03-06 22:14:17 -08001184 LIR *lir;
1185 AssemblerStatus res = kSuccess; // Assume success
buzbeee88dfbf2012-03-05 11:19:57 -08001186
Ian Rogers141d6222012-04-05 12:23:06 -07001187 const bool kVerbosePcFixup = false;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001188 for (lir = (LIR *) cUnit->firstLIRInsn; lir; lir = NEXT_LIR(lir)) {
1189 if (lir->opcode < 0) {
1190 continue;
buzbeee88dfbf2012-03-05 11:19:57 -08001191 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001192
Ian Rogersb5d09b22012-03-06 22:14:17 -08001193 if (lir->flags.isNop) {
1194 continue;
1195 }
1196
1197 if (lir->flags.pcRelFixup) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001198 switch (lir->opcode) {
Ian Rogersb41b33b2012-03-20 14:22:54 -07001199 case kX86Jcc8: {
1200 LIR *targetLIR = lir->target;
1201 DCHECK(targetLIR != NULL);
1202 int delta = 0;
1203 intptr_t pc;
1204 if (IS_SIMM8(lir->operands[0])) {
1205 pc = lir->offset + 2 /* opcode + rel8 */;
1206 } else {
1207 pc = lir->offset + 6 /* 2 byte opcode + rel32 */;
1208 }
1209 intptr_t target = targetLIR->offset;
1210 delta = target - pc;
1211 if (IS_SIMM8(delta) != IS_SIMM8(lir->operands[0])) {
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001212 if (kVerbosePcFixup) {
1213 LOG(INFO) << "Retry for JCC growth at " << lir->offset
1214 << " delta: " << delta << " old delta: " << lir->operands[0];
1215 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001216 lir->opcode = kX86Jcc32;
1217 oatSetupResourceMasks(lir);
1218 res = kRetryAll;
1219 }
Ian Rogers7caad772012-03-30 01:07:54 -07001220 if (kVerbosePcFixup) {
1221 LOG(INFO) << "Source:";
1222 oatDumpLIRInsn(cUnit, lir, 0);
1223 LOG(INFO) << "Target:";
1224 oatDumpLIRInsn(cUnit, targetLIR, 0);
1225 LOG(INFO) << "Delta " << delta;
1226 }
1227 lir->operands[0] = delta;
1228 break;
1229 }
1230 case kX86Jcc32: {
1231 LIR *targetLIR = lir->target;
1232 DCHECK(targetLIR != NULL);
1233 intptr_t pc = lir->offset + 6 /* 2 byte opcode + rel32 */;
1234 intptr_t target = targetLIR->offset;
1235 int delta = target - pc;
1236 if (kVerbosePcFixup) {
1237 LOG(INFO) << "Source:";
1238 oatDumpLIRInsn(cUnit, lir, 0);
1239 LOG(INFO) << "Target:";
1240 oatDumpLIRInsn(cUnit, targetLIR, 0);
1241 LOG(INFO) << "Delta " << delta;
1242 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001243 lir->operands[0] = delta;
1244 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001245 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001246 case kX86Jmp8: {
1247 LIR *targetLIR = lir->target;
1248 DCHECK(targetLIR != NULL);
1249 int delta = 0;
1250 intptr_t pc;
1251 if (IS_SIMM8(lir->operands[0])) {
1252 pc = lir->offset + 2 /* opcode + rel8 */;
1253 } else {
1254 pc = lir->offset + 5 /* opcode + rel32 */;
1255 }
1256 intptr_t target = targetLIR->offset;
1257 delta = target - pc;
jeffhaoe2962482012-06-28 11:29:57 -07001258 if (!(cUnit->disableOpt & (1 << kSafeOptimizations)) && delta == 0) {
Ian Rogersb41b33b2012-03-20 14:22:54 -07001259 // Useless branch
1260 lir->flags.isNop = true;
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001261 if (kVerbosePcFixup) {
1262 LOG(INFO) << "Retry for useless branch at " << lir->offset;
1263 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001264 res = kRetryAll;
1265 } else if (IS_SIMM8(delta) != IS_SIMM8(lir->operands[0])) {
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001266 if (kVerbosePcFixup) {
1267 LOG(INFO) << "Retry for JMP growth at " << lir->offset;
1268 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001269 lir->opcode = kX86Jmp32;
1270 oatSetupResourceMasks(lir);
1271 res = kRetryAll;
1272 }
1273 lir->operands[0] = delta;
1274 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001275 }
Ian Rogers7caad772012-03-30 01:07:54 -07001276 case kX86Jmp32: {
1277 LIR *targetLIR = lir->target;
1278 DCHECK(targetLIR != NULL);
1279 intptr_t pc = lir->offset + 5 /* opcode + rel32 */;
1280 intptr_t target = targetLIR->offset;
1281 int delta = target - pc;
1282 lir->operands[0] = delta;
1283 break;
1284 }
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001285 default:
1286 break;
1287 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001288 }
1289
1290 /*
1291 * If one of the pc-relative instructions expanded we'll have
1292 * to make another pass. Don't bother to fully assemble the
1293 * instruction.
1294 */
1295 if (res != kSuccess) {
1296 continue;
1297 }
Ian Rogers7caad772012-03-30 01:07:54 -07001298 CHECK_EQ(static_cast<size_t>(lir->offset), cUnit->codeBuffer.size());
Ian Rogersb5d09b22012-03-06 22:14:17 -08001299 const X86EncodingMap *entry = &EncodingMap[lir->opcode];
Ian Rogers141b0c72012-03-15 18:18:52 -07001300 size_t starting_cbuf_size = cUnit->codeBuffer.size();
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001301 switch (entry->kind) {
Ian Rogersb5d09b22012-03-06 22:14:17 -08001302 case kData: // 4 bytes of data
1303 cUnit->codeBuffer.push_back(lir->operands[0]);
1304 break;
1305 case kNullary: // 1 byte of opcode
1306 DCHECK_EQ(0, entry->skeleton.prefix1);
1307 DCHECK_EQ(0, entry->skeleton.prefix2);
1308 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001309 if (entry->skeleton.extra_opcode1 != 0) {
1310 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
1311 if (entry->skeleton.extra_opcode2 != 0) {
1312 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
1313 }
1314 } else {
1315 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1316 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001317 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1318 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1319 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1320 break;
1321 case kReg: // lir operands - 0: reg
1322 emitOpReg(cUnit, entry, lir->operands[0]);
1323 break;
1324 case kMem: // lir operands - 0: base, 1: disp
1325 emitOpMem(cUnit, entry, lir->operands[0], lir->operands[1]);
1326 break;
1327 case kMemReg: // lir operands - 0: base, 1: disp, 2: reg
1328 emitMemReg(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1329 break;
Ian Rogersb41b33b2012-03-20 14:22:54 -07001330 case kArrayReg: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
1331 emitArrayReg(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2],
1332 lir->operands[3], lir->operands[4]);
1333 break;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001334 case kRegMem: // lir operands - 0: reg, 1: base, 2: disp
1335 emitRegMem(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1336 break;
1337 case kRegArray: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
1338 emitRegArray(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2],
1339 lir->operands[3], lir->operands[4]);
1340 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001341 case kRegThread: // lir operands - 0: reg, 1: disp
1342 emitRegThread(cUnit, entry, lir->operands[0], lir->operands[1]);
1343 break;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001344 case kRegReg: // lir operands - 0: reg1, 1: reg2
1345 emitRegReg(cUnit, entry, lir->operands[0], lir->operands[1]);
1346 break;
jeffhaofdffdf82012-07-11 16:08:43 -07001347 case kRegRegStore: // lir operands - 0: reg2, 1: reg1
1348 emitRegReg(cUnit, entry, lir->operands[1], lir->operands[0]);
1349 break;
Elliott Hughes225ae522012-04-16 20:21:45 -07001350 case kRegRegImm:
1351 emitRegRegImm(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1352 break;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001353 case kRegImm: // lir operands - 0: reg, 1: immediate
1354 emitRegImm(cUnit, entry, lir->operands[0], lir->operands[1]);
1355 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001356 case kThreadImm: // lir operands - 0: disp, 1: immediate
1357 emitThreadImm(cUnit, entry, lir->operands[0], lir->operands[1]);
1358 break;
1359 case kMovRegImm: // lir operands - 0: reg, 1: immediate
1360 emitMovRegImm(cUnit, entry, lir->operands[0], lir->operands[1]);
1361 break;
Ian Rogersb41b33b2012-03-20 14:22:54 -07001362 case kShiftRegImm: // lir operands - 0: reg, 1: immediate
1363 emitShiftRegImm(cUnit, entry, lir->operands[0], lir->operands[1]);
1364 break;
Ian Rogers7caad772012-03-30 01:07:54 -07001365 case kShiftRegCl: // lir operands - 0: reg, 1: cl
1366 emitShiftRegCl(cUnit, entry, lir->operands[0], lir->operands[1]);
1367 break;
1368 case kRegCond: // lir operands - 0: reg, 1: condition
1369 emitRegCond(cUnit, entry, lir->operands[0], lir->operands[1]);
1370 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001371 case kJmp: // lir operands - 0: rel
1372 emitJmp(cUnit, entry, lir->operands[0]);
1373 break;
1374 case kJcc: // lir operands - 0: rel, 1: CC, target assigned
1375 emitJcc(cUnit, entry, lir->operands[0], lir->operands[1]);
1376 break;
1377 case kCall:
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001378 switch (entry->opcode) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001379 case kX86CallM: // lir operands - 0: base, 1: disp
1380 emitCallMem(cUnit, entry, lir->operands[0], lir->operands[1]);
1381 break;
1382 case kX86CallT: // lir operands - 0: disp
1383 emitCallThread(cUnit, entry, lir->operands[0]);
1384 break;
1385 default:
1386 emitUnimplemented(cUnit, entry, lir);
1387 break;
1388 }
1389 break;
Ian Rogers7caad772012-03-30 01:07:54 -07001390 case kPcRel: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
1391 emitPcRel(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2],
1392 lir->operands[3], lir->operands[4]);
1393 break;
1394 case kMacro:
1395 emitMacro(cUnit, entry, lir->operands[0], lir->offset);
1396 break;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001397 default:
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001398 emitUnimplemented(cUnit, entry, lir);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001399 break;
1400 }
Ian Rogers7caad772012-03-30 01:07:54 -07001401 CHECK_EQ(static_cast<size_t>(oatGetInsnSize(lir)),
1402 cUnit->codeBuffer.size() - starting_cbuf_size)
1403 << "Instruction size mismatch for entry: " << EncodingMap[lir->opcode].name;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001404 }
1405 return res;
buzbeee88dfbf2012-03-05 11:19:57 -08001406}
1407
buzbeee88dfbf2012-03-05 11:19:57 -08001408/*
1409 * Target-dependent offset assignment.
1410 * independent.
1411 */
1412int oatAssignInsnOffsets(CompilationUnit* cUnit)
1413{
1414 LIR* x86LIR;
1415 int offset = 0;
1416
1417 for (x86LIR = (LIR *) cUnit->firstLIRInsn;
1418 x86LIR;
1419 x86LIR = NEXT_LIR(x86LIR)) {
1420 x86LIR->offset = offset;
1421 if (x86LIR->opcode >= 0) {
1422 if (!x86LIR->flags.isNop) {
1423 offset += x86LIR->flags.size;
1424 }
1425 } else if (x86LIR->opcode == kPseudoPseudoAlign4) {
1426 if (offset & 0x2) {
1427 offset += 2;
1428 x86LIR->operands[0] = 1;
1429 } else {
1430 x86LIR->operands[0] = 0;
1431 }
1432 }
1433 /* Pseudo opcodes don't consume space */
1434 }
1435
1436 return offset;
1437}
1438
1439} // namespace art