blob: 9e0e713e0cdf0838342f2d799cf8ce9093b6c08c [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
Ian Rogersb3ab25b2012-03-19 01:12:01 -070031#define ENCODING_MAP(opname, is_store, \
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) \
Ian Rogersb3ab25b2012-03-19 01:12:01 -070038{ kX86 ## opname ## 8MR, kMemReg, is_store | IS_TERTIARY_OP | SETS_CCODES, { 0, 0, rm8_r8, 0, 0, 0, 0, 0 }, #opname "8MR", "[!0r+!1d],!2r" }, \
39{ kX86 ## opname ## 8AR, kArrayReg, is_store | IS_QUIN_OP | SETS_CCODES, { 0, 0, rm8_r8, 0, 0, 0, 0, 0 }, #opname "8AR", "[!0r+!1r<<!2d+!3d],!4r" }, \
40{ kX86 ## opname ## 8TR, kThreadReg,is_store | IS_BINARY_OP | SETS_CCODES, { THREAD_PREFIX, 0, rm8_r8, 0, 0, 0, 0, 0 }, #opname "8TR", "fs:[!0d],!1r" }, \
Ian Rogersb5d09b22012-03-06 22:14:17 -080041{ kX86 ## opname ## 8RR, kRegReg, IS_BINARY_OP | SETS_CCODES, { 0, 0, r8_rm8, 0, 0, 0, 0, 0 }, #opname "8RR", "!0r,!1r" }, \
42{ kX86 ## opname ## 8RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | SETS_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 | SETS_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 | SETS_CCODES, { THREAD_PREFIX, 0, r8_rm8, 0, 0, 0, 0, 0 }, #opname "8RT", "!0r,fs:[!1d]" }, \
45{ kX86 ## opname ## 8RI, kRegImm, IS_BINARY_OP | SETS_CCODES, { 0, 0, rm8_i8, 0, 0, rm8_i8_modrm, ax8_i8, 1 }, #opname "8RI", "!0r,!1d" }, \
Ian Rogersb3ab25b2012-03-19 01:12:01 -070046{ kX86 ## opname ## 8MI, kMemImm, is_store | IS_TERTIARY_OP | SETS_CCODES, { 0, 0, rm8_i8, 0, 0, rm8_i8_modrm, 0, 1 }, #opname "8MI", "[!0r+!1d],!2r" }, \
47{ kX86 ## opname ## 8AI, kArrayImm, is_store | IS_QUIN_OP | SETS_CCODES, { 0, 0, rm8_i8, 0, 0, rm8_i8_modrm, 0, 1 }, #opname "8AI", "[!0r+!1r<<!2d+!3d],!4r" }, \
48{ kX86 ## opname ## 8TI, kThreadImm,is_store | IS_BINARY_OP | SETS_CCODES, { THREAD_PREFIX, 0, rm8_i8, 0, 0, rm8_i8_modrm, 0, 1 }, #opname "8TI", "fs:[!0d],!1r" }, \
Ian Rogersb5d09b22012-03-06 22:14:17 -080049 \
Ian Rogersb3ab25b2012-03-19 01:12:01 -070050{ kX86 ## opname ## 16MR, kMemReg, is_store | IS_TERTIARY_OP | SETS_CCODES, { 0x66, 0, rm32_r32, 0, 0, 0, 0, 0 }, #opname "16MR", "[!0r+!1d],!2r" }, \
51{ kX86 ## opname ## 16AR, kArrayReg, is_store | IS_QUIN_OP | SETS_CCODES, { 0x66, 0, rm32_r32, 0, 0, 0, 0, 0 }, #opname "16AR", "[!0r+!1r<<!2d+!3d],!4r" }, \
52{ kX86 ## opname ## 16TR, kThreadReg,is_store | IS_BINARY_OP | SETS_CCODES, { THREAD_PREFIX, 0x66, rm32_r32, 0, 0, 0, 0, 0 }, #opname "16TR", "fs:[!0d],!1r" }, \
Ian Rogersb5d09b22012-03-06 22:14:17 -080053{ kX86 ## opname ## 16RR, kRegReg, IS_BINARY_OP | SETS_CCODES, { 0x66, 0, r32_rm32, 0, 0, 0, 0, 0 }, #opname "16RR", "!0r,!1r" }, \
54{ kX86 ## opname ## 16RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | SETS_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 | SETS_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 | SETS_CCODES, { THREAD_PREFIX, 0x66, r32_rm32, 0, 0, 0, 0, 0 }, #opname "16RT", "!0r,fs:[!1d]" }, \
57{ kX86 ## opname ## 16RI, kRegImm, IS_BINARY_OP | SETS_CCODES, { 0x66, 0, rm32_i32, 0, 0, rm32_i32_modrm, ax32_i32, 2 }, #opname "16RI", "!0r,!1d" }, \
Ian Rogersb3ab25b2012-03-19 01:12:01 -070058{ kX86 ## opname ## 16MI, kMemImm, is_store | IS_TERTIARY_OP | SETS_CCODES, { 0x66, 0, rm32_i32, 0, 0, rm32_i32_modrm, 0, 2 }, #opname "16MI", "[!0r+!1d],!2d" }, \
59{ kX86 ## opname ## 16AI, kArrayImm, is_store | IS_QUIN_OP | SETS_CCODES, { 0x66, 0, rm32_i32, 0, 0, rm32_i32_modrm, 0, 2 }, #opname "16AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
60{ kX86 ## opname ## 16TI, kThreadImm,is_store | IS_BINARY_OP | SETS_CCODES, { THREAD_PREFIX, 0x66, rm32_i32, 0, 0, rm32_i32_modrm, 0, 2 }, #opname "16TI", "fs:[!0d],!1d" }, \
Ian Rogersb5d09b22012-03-06 22:14:17 -080061{ kX86 ## opname ## 16RI8, kRegImm, IS_BINARY_OP | SETS_CCODES, { 0x66, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "16RI8", "!0r,!1d" }, \
Ian Rogersb3ab25b2012-03-19 01:12:01 -070062{ kX86 ## opname ## 16MI8, kMemImm, is_store | IS_TERTIARY_OP | SETS_CCODES, { 0x66, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "16MI8", "[!0r+!1d],!2d" }, \
63{ kX86 ## opname ## 16AI8, kArrayImm, is_store | IS_QUIN_OP | SETS_CCODES, { 0x66, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "16AI8", "[!0r+!1r<<!2d+!3d],!4d" }, \
64{ kX86 ## opname ## 16TI8, kThreadImm,is_store | IS_BINARY_OP | SETS_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 \
Ian Rogersb3ab25b2012-03-19 01:12:01 -070066{ kX86 ## opname ## 32MR, kMemReg, is_store | IS_TERTIARY_OP | SETS_CCODES, { 0, 0, rm32_r32, 0, 0, 0, 0, 0 }, #opname "32MR", "[!0r+!1d],!2r" }, \
67{ kX86 ## opname ## 32AR, kArrayReg, is_store | IS_QUIN_OP | SETS_CCODES, { 0, 0, rm32_r32, 0, 0, 0, 0, 0 }, #opname "32AR", "[!0r+!1r<<!2d+!3d],!4r" }, \
68{ kX86 ## opname ## 32TR, kThreadReg,is_store | IS_BINARY_OP | SETS_CCODES, { THREAD_PREFIX, 0, rm32_r32, 0, 0, 0, 0, 0 }, #opname "32TR", "fs:[!0d],!1r" }, \
Ian Rogersb5d09b22012-03-06 22:14:17 -080069{ kX86 ## opname ## 32RR, kRegReg, IS_BINARY_OP | SETS_CCODES, { 0, 0, r32_rm32, 0, 0, 0, 0, 0 }, #opname "32RR", "!0r,!1r" }, \
70{ kX86 ## opname ## 32RM, kRegMem, IS_LOAD | IS_TERTIARY_OP | SETS_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 | SETS_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 | SETS_CCODES, { THREAD_PREFIX, 0, r32_rm32, 0, 0, 0, 0, 0 }, #opname "32RT", "!0r,fs:[!1d]" }, \
73{ kX86 ## opname ## 32RI, kRegImm, IS_BINARY_OP | SETS_CCODES, { 0, 0, rm32_i32, 0, 0, rm32_i32_modrm, ax32_i32, 4 }, #opname "32RI", "!0r,!1d" }, \
Ian Rogersb3ab25b2012-03-19 01:12:01 -070074{ kX86 ## opname ## 32MI, kMemImm, is_store | IS_TERTIARY_OP | SETS_CCODES, { 0, 0, rm32_i32, 0, 0, rm32_i32_modrm, 0, 4 }, #opname "32MI", "[!0r+!1d],!2r" }, \
75{ kX86 ## opname ## 32AI, kArrayImm, is_store | IS_QUIN_OP | SETS_CCODES, { 0, 0, rm32_i32, 0, 0, rm32_i32_modrm, 0, 4 }, #opname "32AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
76{ kX86 ## opname ## 32TI, kThreadImm,is_store | IS_BINARY_OP | SETS_CCODES, { THREAD_PREFIX, 0, rm32_i32, 0, 0, rm32_i32_modrm, 0, 4 }, #opname "32TI", "fs:[!0d],!1d" }, \
Ian Rogersb5d09b22012-03-06 22:14:17 -080077{ kX86 ## opname ## 32RI8, kRegImm, IS_BINARY_OP | SETS_CCODES, { 0, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "32RI8", "!0r,!1d" }, \
Ian Rogersb3ab25b2012-03-19 01:12:01 -070078{ kX86 ## opname ## 32MI8, kMemImm, is_store | IS_TERTIARY_OP | SETS_CCODES, { 0, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "32MI8", "[!0r+!1d],!2d" }, \
79{ kX86 ## opname ## 32AI8, kArrayImm, is_store | IS_QUIN_OP | SETS_CCODES, { 0, 0, rm32_i8, 0, 0, rm32_i8_modrm, 0, 1 }, #opname "32AI8", "[!0r+!1r<<!2d+!3d],!4d" }, \
80{ kX86 ## opname ## 32TI8, kThreadImm,is_store | IS_BINARY_OP | SETS_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
Ian Rogersb3ab25b2012-03-19 01:12:01 -070082ENCODING_MAP(Add, IS_STORE,
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 */),
Ian Rogersb3ab25b2012-03-19 01:12:01 -070088ENCODING_MAP(Or, IS_STORE,
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 */),
Ian Rogersb3ab25b2012-03-19 01:12:01 -070094ENCODING_MAP(Adc, IS_STORE,
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 */),
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700100ENCODING_MAP(Sbb, IS_STORE,
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 */),
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700106ENCODING_MAP(And, IS_STORE,
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 */),
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700112ENCODING_MAP(Sub, IS_STORE,
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 */),
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700118ENCODING_MAP(Xor, IS_STORE,
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 */),
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700124ENCODING_MAP(Cmp, IS_LOAD,
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
Elliott Hughes225ae522012-04-16 20:21:45 -0700132 { kX86Imul16RRI, kRegRegImm, IS_TERTIARY_OP | SETS_CCODES, { 0x66, 0, 0x69, 0, 0, 0, 0, 2 }, "Imul16RRI", "!0r,!1r,!2d" },
133 { kX86Imul16RMI, kRegMemImm, IS_LOAD | IS_QUAD_OP | SETS_CCODES, { 0x66, 0, 0x69, 0, 0, 0, 0, 2 }, "Imul16RMI", "!0r,[!1r+!2d],!3d" },
134 { kX86Imul16RAI, kRegArrayImm, IS_LOAD | IS_SEXTUPLE_OP | 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
jeffhao44679082012-04-25 16:22:59 -0700136 { kX86Imul32RRI, kRegRegImm, IS_TERTIARY_OP | SETS_CCODES, { 0, 0, 0x69, 0, 0, 0, 0, 4 }, "Imul32RRI", "!0r,!1r,!2d" },
137 { kX86Imul32RMI, kRegMemImm, IS_LOAD | IS_QUAD_OP | SETS_CCODES, { 0, 0, 0x69, 0, 0, 0, 0, 4 }, "Imul32RMI", "!0r,[!1r+!2d],!3d" },
138 { kX86Imul32RAI, kRegArrayImm, IS_LOAD | IS_SEXTUPLE_OP | SETS_CCODES, { 0, 0, 0x69, 0, 0, 0, 0, 4 }, "Imul32RAI", "!0r,[!1r+!2r<<!3d+!4d],!5d" },
Elliott Hughes225ae522012-04-16 20:21:45 -0700139 { kX86Imul32RRI8, kRegRegImm, IS_TERTIARY_OP | SETS_CCODES, { 0, 0, 0x6B, 0, 0, 0, 0, 1 }, "Imul32RRI8", "!0r,!1r,!2d" },
140 { kX86Imul32RMI8, kRegMemImm, IS_LOAD | IS_QUAD_OP | SETS_CCODES, { 0, 0, 0x6B, 0, 0, 0, 0, 1 }, "Imul32RMI8", "!0r,[!1r+!2d],!3d" },
141 { kX86Imul32RAI8, kRegArrayImm, IS_LOAD | IS_SEXTUPLE_OP | 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
143 { kX86Mov8MR, kMemReg, IS_STORE | IS_TERTIARY_OP, { 0, 0, 0x88, 0, 0, 0, 0, 0 }, "Mov8MR", "[!0r+!1d],!2r" },
144 { kX86Mov8AR, kArrayReg, IS_STORE | IS_QUIN_OP, { 0, 0, 0x88, 0, 0, 0, 0, 0 }, "Mov8AR", "[!0r+!1r<<!2d+!3d],!4r" },
145 { kX86Mov8TR, kThreadReg, IS_STORE | IS_BINARY_OP, { THREAD_PREFIX, 0, 0x88, 0, 0, 0, 0, 0 }, "Mov8TR", "fs:[!0d],!1r" },
146 { kX86Mov8RR, kRegReg, IS_BINARY_OP, { 0, 0, 0x8A, 0, 0, 0, 0, 0 }, "Mov8RR", "!0r,!1r" },
147 { kX86Mov8RM, kRegMem, IS_LOAD | IS_TERTIARY_OP, { 0, 0, 0x8A, 0, 0, 0, 0, 0 }, "Mov8RM", "!0r,[!1r+!2d]" },
148 { kX86Mov8RA, kRegArray, IS_LOAD | IS_QUIN_OP, { 0, 0, 0x8A, 0, 0, 0, 0, 0 }, "Mov8RA", "!0r,[!1r+!2r<<!3d+!4d]" },
149 { kX86Mov8RT, kRegThread, IS_LOAD | IS_BINARY_OP, { THREAD_PREFIX, 0, 0x8A, 0, 0, 0, 0, 0 }, "Mov8RT", "!0r,fs:[!1d]" },
150 { kX86Mov8RI, kMovRegImm, IS_BINARY_OP, { 0, 0, 0xB0, 0, 0, 0, 0, 1 }, "Mov8RI", "!0r,!1d" },
151 { kX86Mov8MI, kMemImm, IS_STORE | IS_TERTIARY_OP, { 0, 0, 0xC6, 0, 0, 0, 0, 1 }, "Mov8MI", "[!0r+!1d],!2r" },
152 { kX86Mov8AI, kArrayImm, IS_STORE | IS_QUIN_OP, { 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" },
154
155 { kX86Mov16MR, kMemReg, IS_STORE | IS_TERTIARY_OP, { 0x66, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov16MR", "[!0r+!1d],!2r" },
156 { kX86Mov16AR, kArrayReg, IS_STORE | IS_QUIN_OP, { 0x66, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov16AR", "[!0r+!1r<<!2d+!3d],!4r" },
157 { kX86Mov16TR, kThreadReg, IS_STORE | IS_BINARY_OP, { THREAD_PREFIX, 0x66, 0x89, 0, 0, 0, 0, 0 }, "Mov16TR", "fs:[!0d],!1r" },
158 { kX86Mov16RR, kRegReg, IS_BINARY_OP, { 0x66, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov16RR", "!0r,!1r" },
159 { kX86Mov16RM, kRegMem, IS_LOAD | IS_TERTIARY_OP, { 0x66, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov16RM", "!0r,[!1r+!2d]" },
160 { kX86Mov16RA, kRegArray, IS_LOAD | IS_QUIN_OP, { 0x66, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov16RA", "!0r,[!1r+!2r<<!3d+!4d]" },
161 { kX86Mov16RT, kRegThread, IS_LOAD | IS_BINARY_OP, { THREAD_PREFIX, 0x66, 0x8B, 0, 0, 0, 0, 0 }, "Mov16RT", "!0r,fs:[!1d]" },
162 { kX86Mov16RI, kMovRegImm, IS_BINARY_OP, { 0x66, 0, 0xB8, 0, 0, 0, 0, 2 }, "Mov16RI", "!0r,!1d" },
163 { kX86Mov16MI, kMemImm, IS_STORE | IS_TERTIARY_OP, { 0x66, 0, 0xC7, 0, 0, 0, 0, 2 }, "Mov16MI", "[!0r+!1d],!2r" },
164 { kX86Mov16AI, kArrayImm, IS_STORE | IS_QUIN_OP, { 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" },
166
167 { kX86Mov32MR, kMemReg, IS_STORE | IS_TERTIARY_OP, { 0, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov32MR", "[!0r+!1d],!2r" },
168 { kX86Mov32AR, kArrayReg, IS_STORE | IS_QUIN_OP, { 0, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov32AR", "[!0r+!1r<<!2d+!3d],!4r" },
169 { kX86Mov32TR, kThreadReg, IS_STORE | IS_BINARY_OP, { THREAD_PREFIX, 0, 0x89, 0, 0, 0, 0, 0 }, "Mov32TR", "fs:[!0d],!1r" },
170 { kX86Mov32RR, kRegReg, IS_BINARY_OP, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov32RR", "!0r,!1r" },
171 { kX86Mov32RM, kRegMem, IS_LOAD | IS_TERTIARY_OP, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov32RM", "!0r,[!1r+!2d]" },
172 { kX86Mov32RA, kRegArray, IS_LOAD | IS_QUIN_OP, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov32RA", "!0r,[!1r+!2r<<!3d+!4d]" },
173 { kX86Mov32RT, kRegThread, IS_LOAD | IS_BINARY_OP, { THREAD_PREFIX, 0, 0x8B, 0, 0, 0, 0, 0 }, "Mov32RT", "!0r,fs:[!1d]" },
174 { kX86Mov32RI, kMovRegImm, IS_BINARY_OP, { 0, 0, 0xB8, 0, 0, 0, 0, 4 }, "Mov32RI", "!0r,!1d" },
175 { kX86Mov32MI, kMemImm, IS_STORE | IS_TERTIARY_OP, { 0, 0, 0xC7, 0, 0, 0, 0, 4 }, "Mov32MI", "[!0r+!1d],!2r" },
176 { kX86Mov32AI, kArrayImm, IS_STORE | IS_QUIN_OP, { 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" },
178
179 { kX86Lea32RA, kRegArray, IS_QUIN_OP, { 0, 0, 0x8D, 0, 0, 0, 0, 0 }, "Lea32RA", "!0r,[!1r+!2r<<!3d+!4d]" },
180
181#define SHIFT_ENCODING_MAP(opname, modrm_opcode) \
182{ kX86 ## opname ## 8RI, kShiftRegImm, IS_BINARY_OP | SETS_CCODES, { 0, 0, 0xC0, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "8RI", "!0r,!1d" }, \
183{ kX86 ## opname ## 8MI, kShiftMemImm, IS_TERTIARY_OP | SETS_CCODES, { 0, 0, 0xC0, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "8MI", "[!0r+!1d],!2r" }, \
184{ kX86 ## opname ## 8AI, kShiftArrayImm, IS_QUIN_OP | 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 | SETS_CCODES, { 0, 0, 0xD2, 0, 0, modrm_opcode, 0, 1 }, #opname "8RC", "" }, \
186{ kX86 ## opname ## 8MC, kShiftMemCl, IS_TERTIARY_OP | SETS_CCODES, { 0, 0, 0xD2, 0, 0, modrm_opcode, 0, 1 }, #opname "8MC", "" }, \
187{ kX86 ## opname ## 8AC, kShiftArrayCl, IS_QUIN_OP | SETS_CCODES, { 0, 0, 0xD2, 0, 0, modrm_opcode, 0, 1 }, #opname "8AC", "" }, \
188 \
189{ kX86 ## opname ## 16RI, kShiftRegImm, IS_BINARY_OP | SETS_CCODES, { 0x66, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "16RI", "!0r,!1d" }, \
190{ kX86 ## opname ## 16MI, kShiftMemImm, IS_TERTIARY_OP | SETS_CCODES, { 0x66, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "16MI", "[!0r+!1d],!2r" }, \
191{ kX86 ## opname ## 16AI, kShiftArrayImm, IS_QUIN_OP | 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 | SETS_CCODES, { 0x66, 0, 0xD3, 0, 0, modrm_opcode, 0, 1 }, #opname "16RC", "" }, \
193{ kX86 ## opname ## 16MC, kShiftMemCl, IS_TERTIARY_OP | SETS_CCODES, { 0x66, 0, 0xD3, 0, 0, modrm_opcode, 0, 1 }, #opname "16MC", "" }, \
194{ kX86 ## opname ## 16AC, kShiftArrayCl, IS_QUIN_OP | SETS_CCODES, { 0x66, 0, 0xD3, 0, 0, modrm_opcode, 0, 1 }, #opname "16AC", "" }, \
195 \
196{ kX86 ## opname ## 32RI, kShiftRegImm, IS_BINARY_OP | SETS_CCODES, { 0, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "32RI", "!0r,!1d" }, \
197{ kX86 ## opname ## 32MI, kShiftMemImm, IS_TERTIARY_OP | SETS_CCODES, { 0, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "32MI", "[!0r+!1d],!2r" }, \
198{ kX86 ## opname ## 32AI, kShiftArrayImm, IS_QUIN_OP | SETS_CCODES, { 0, 0, 0xC1, 0, 0, modrm_opcode, 0xD1, 1 }, #opname "32AI", "[!0r+!1r<<!2d+!3d],!4d" }, \
Ian Rogers7caad772012-03-30 01:07:54 -0700199{ kX86 ## opname ## 32RC, kShiftRegCl, IS_BINARY_OP | SETS_CCODES, { 0, 0, 0xD3, 0, 0, modrm_opcode, 0, 0 }, #opname "32RC", "" }, \
200{ kX86 ## opname ## 32MC, kShiftMemCl, IS_TERTIARY_OP | SETS_CCODES, { 0, 0, 0xD3, 0, 0, modrm_opcode, 0, 0 }, #opname "32MC", "" }, \
201{ kX86 ## opname ## 32AC, kShiftArrayCl, IS_QUIN_OP | SETS_CCODES, { 0, 0, 0xD3, 0, 0, modrm_opcode, 0, 0 }, #opname "32AC", "" }
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
212#define UNARY_ENCODING_MAP(opname, modrm, \
213 reg, reg_kind, reg_flags, \
214 mem, mem_kind, mem_flags, \
215 arr, arr_kind, arr_flags, imm) \
216{ kX86 ## opname ## 8 ## reg, reg_kind, reg_flags, { 0, 0, 0xF6, 0, 0, modrm, 0, imm << 0}, #opname "8" #reg, "" }, \
217{ kX86 ## opname ## 8 ## mem, mem_kind, IS_LOAD | mem_flags, { 0, 0, 0xF6, 0, 0, modrm, 0, imm << 0}, #opname "8" #mem, "" }, \
218{ kX86 ## opname ## 8 ## arr, arr_kind, IS_LOAD | arr_flags, { 0, 0, 0xF6, 0, 0, modrm, 0, imm << 0}, #opname "8" #arr, "" }, \
219{ kX86 ## opname ## 16 ## reg, reg_kind, reg_flags, { 0x66, 0, 0xF7, 0, 0, modrm, 0, imm << 1}, #opname "16" #reg, "" }, \
220{ kX86 ## opname ## 16 ## mem, mem_kind, IS_LOAD | mem_flags, { 0x66, 0, 0xF7, 0, 0, modrm, 0, imm << 1}, #opname "16" #mem, "" }, \
221{ kX86 ## opname ## 16 ## arr, arr_kind, IS_LOAD | arr_flags, { 0x66, 0, 0xF7, 0, 0, modrm, 0, imm << 1}, #opname "16" #arr, "" }, \
222{ kX86 ## opname ## 32 ## reg, reg_kind, reg_flags, { 0, 0, 0xF7, 0, 0, modrm, 0, imm << 2}, #opname "32" #reg, "" }, \
223{ kX86 ## opname ## 32 ## mem, mem_kind, IS_LOAD | mem_flags, { 0, 0, 0xF7, 0, 0, modrm, 0, imm << 2}, #opname "32" #mem, "" }, \
224{ kX86 ## opname ## 32 ## arr, arr_kind, IS_LOAD | arr_flags, { 0, 0, 0xF7, 0, 0, modrm, 0, imm << 2}, #opname "32" #arr, "" }
225
226 UNARY_ENCODING_MAP(Test, 0x0, RI, kRegImm, IS_BINARY_OP, MI, kMemImm, IS_TERTIARY_OP, AI, kArrayImm, IS_QUIN_OP, 1),
227 UNARY_ENCODING_MAP(Not, 0x2, R, kReg, IS_UNARY_OP, M, kMem, IS_BINARY_OP, A, kArray, IS_QUAD_OP, 0),
228 UNARY_ENCODING_MAP(Neg, 0x3, R, kReg, IS_UNARY_OP, M, kMem, IS_BINARY_OP, A, kArray, IS_QUAD_OP, 0),
229 UNARY_ENCODING_MAP(Mul, 0x4, DaR, kRegRegReg, IS_TERTIARY_OP, DaM, kRegRegMem, IS_QUAD_OP, DaA, kRegRegArray, IS_SEXTUPLE_OP, 0),
230 UNARY_ENCODING_MAP(Imul, 0x5, DaR, kRegRegReg, IS_TERTIARY_OP, DaM, kRegRegMem, IS_QUAD_OP, DaA, kRegRegArray, IS_SEXTUPLE_OP, 0),
231 UNARY_ENCODING_MAP(Divmod, 0x6, DaR, kRegRegReg, IS_TERTIARY_OP, DaM, kRegRegMem, IS_QUAD_OP, DaA, kRegRegArray, IS_SEXTUPLE_OP, 0),
232 UNARY_ENCODING_MAP(Idivmod, 0x7, DaR, kRegRegReg, IS_TERTIARY_OP, DaM, kRegRegMem, IS_QUAD_OP, DaA, kRegRegArray, IS_SEXTUPLE_OP, 0),
233#undef UNARY_ENCODING_MAP
234
235#define EXT_0F_ENCODING_MAP(opname, prefix, opcode) \
236{ kX86 ## opname ## RR, kRegReg, IS_BINARY_OP, { prefix, 0, 0x0F, opcode, 0, 0, 0, 0 }, #opname "RR", "!0r,!1r" }, \
237{ kX86 ## opname ## RM, kRegMem, IS_LOAD | IS_TERTIARY_OP, { prefix, 0, 0x0F, opcode, 0, 0, 0, 0 }, #opname "RM", "!0r,[!1r+!2d]" }, \
238{ kX86 ## opname ## RA, kRegArray, IS_LOAD | IS_QUIN_OP, { prefix, 0, 0x0F, opcode, 0, 0, 0, 0 }, #opname "RA", "!0r,[!1r+!2r<<!3d+!4d]" }
239
240 EXT_0F_ENCODING_MAP(Movsd, 0xF2, 0x10),
241 { kX86MovsdMR, kMemReg, IS_STORE | IS_TERTIARY_OP, { 0xF2, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovsdMR", "[!0r+!1d],!2r" },
242 { kX86MovsdAR, kArrayReg, IS_STORE | IS_QUIN_OP, { 0xF2, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovsdAR", "[!0r+!1r<<!2d+!3d],!4r" },
243
244 EXT_0F_ENCODING_MAP(Movss, 0xF3, 0x10),
245 { kX86MovssMR, kMemReg, IS_STORE | IS_TERTIARY_OP, { 0xF3, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovssMR", "[!0r+!1d],!2r" },
246 { kX86MovssAR, kArrayReg, IS_STORE | IS_QUIN_OP, { 0xF3, 0, 0x0F, 0x11, 0, 0, 0, 0 }, "MovssAR", "[!0r+!1r<<!2d+!3d],!4r" },
247
248 EXT_0F_ENCODING_MAP(Cvtsi2sd, 0xF2, 0x2A),
249 EXT_0F_ENCODING_MAP(Cvtsi2ss, 0xF3, 0x2A),
250 EXT_0F_ENCODING_MAP(Cvttsd2si, 0xF2, 0x2C),
251 EXT_0F_ENCODING_MAP(Cvttss2si, 0xF3, 0x2C),
252 EXT_0F_ENCODING_MAP(Cvtsd2si, 0xF2, 0x2D),
253 EXT_0F_ENCODING_MAP(Cvtss2si, 0xF3, 0x2D),
254 EXT_0F_ENCODING_MAP(Ucomisd, 0x66, 0x2E),
255 EXT_0F_ENCODING_MAP(Ucomiss, 0x00, 0x2E),
256 EXT_0F_ENCODING_MAP(Comisd, 0x66, 0x2F),
257 EXT_0F_ENCODING_MAP(Comiss, 0x00, 0x2F),
Ian Rogersb41b33b2012-03-20 14:22:54 -0700258 EXT_0F_ENCODING_MAP(Orps, 0x00, 0x56),
259 EXT_0F_ENCODING_MAP(Xorps, 0x00, 0x57),
Ian Rogersb5d09b22012-03-06 22:14:17 -0800260 EXT_0F_ENCODING_MAP(Addsd, 0xF2, 0x58),
261 EXT_0F_ENCODING_MAP(Addss, 0xF3, 0x58),
262 EXT_0F_ENCODING_MAP(Mulsd, 0xF2, 0x59),
263 EXT_0F_ENCODING_MAP(Mulss, 0xF3, 0x59),
jeffhao292188d2012-05-17 15:45:04 -0700264 EXT_0F_ENCODING_MAP(Cvtsd2ss, 0xF2, 0x5A),
265 EXT_0F_ENCODING_MAP(Cvtss2sd, 0xF3, 0x5A),
Ian Rogersb5d09b22012-03-06 22:14:17 -0800266 EXT_0F_ENCODING_MAP(Subsd, 0xF2, 0x5C),
267 EXT_0F_ENCODING_MAP(Subss, 0xF3, 0x5C),
268 EXT_0F_ENCODING_MAP(Divsd, 0xF2, 0x5E),
269 EXT_0F_ENCODING_MAP(Divss, 0xF3, 0x5E),
270
jeffhao6a4a7e12012-05-07 13:28:15 -0700271 { kX86PsllqRI, kRegImm, IS_BINARY_OP, { 0x66, 0, 0x0F, 0x73, 0, 6, 0, 1 }, "PsllqRI", "!0r, !1d" },
Ian Rogersb41b33b2012-03-20 14:22:54 -0700272
Ian Rogersb5d09b22012-03-06 22:14:17 -0800273 EXT_0F_ENCODING_MAP(Movdxr, 0x66, 0x6E),
274 EXT_0F_ENCODING_MAP(Movdrx, 0x66, 0x7E),
275
276 { kX86Set8R, kRegCond, IS_BINARY_OP, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0 }, "Set8R", "!1c !0r" },
277 { kX86Set8M, kMemCond, IS_STORE | IS_TERTIARY_OP, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0 }, "Set8M", "!2c [!0r+!1d]" },
278 { kX86Set8A, kArrayCond, IS_STORE | IS_QUIN_OP, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0 }, "Set8A", "!4c [!0r+!1r<<!2d+!3d]" },
279
Ian Rogersc6f3bb82012-03-21 20:40:33 -0700280 // TODO: load/store?
281 // Encode the modrm opcode as an extra opcode byte to avoid computation during assembly.
282 { kX86Mfence, kReg, NO_OPERAND, { 0, 0, 0x0F, 0xAE, 0, 6, 0, 0 }, "Mfence", "" },
283
Ian Rogersb5d09b22012-03-06 22:14:17 -0800284 EXT_0F_ENCODING_MAP(Imul16, 0x66, 0xAF),
285 EXT_0F_ENCODING_MAP(Imul32, 0x00, 0xAF),
286 EXT_0F_ENCODING_MAP(Movzx8, 0x00, 0xB6),
287 EXT_0F_ENCODING_MAP(Movzx16, 0x00, 0xB7),
288 EXT_0F_ENCODING_MAP(Movsx8, 0x00, 0xBE),
289 EXT_0F_ENCODING_MAP(Movsx16, 0x00, 0xBF),
290#undef EXT_0F_ENCODING_MAP
291
Ian Rogersb41b33b2012-03-20 14:22:54 -0700292 { kX86Jcc8, kJcc, IS_BINARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0x70, 0, 0, 0, 0, 0 }, "Jcc8", "!1c !0t" },
293 { kX86Jcc32, kJcc, IS_BINARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0x0F, 0x80, 0, 0, 0, 0 }, "Jcc32", "!1c !0t" },
294 { kX86Jmp8, kJmp, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0xEB, 0, 0, 0, 0, 0 }, "Jmp8", "!0t" },
295 { kX86Jmp32, kJmp, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0xE9, 0, 0, 0, 0, 0 }, "Jmp32", "!0t" },
Ian Rogers7caad772012-03-30 01:07:54 -0700296 { kX86JmpR, kJmp, IS_UNARY_OP | IS_BRANCH, { 0, 0, 0xFF, 0, 0, 4, 0, 0 }, "JmpR", "!0r" },
297 { kX86CallR, kCall, IS_UNARY_OP | IS_BRANCH, { 0, 0, 0xE8, 0, 0, 0, 0, 0 }, "CallR", "!0r" },
298 { kX86CallM, kCall, IS_BINARY_OP | IS_BRANCH | IS_LOAD, { 0, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallM", "[!0r+!1d]" },
299 { kX86CallA, kCall, IS_QUAD_OP | IS_BRANCH | IS_LOAD, { 0, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallA", "[!0r+!1r<<!2d+!3d]" },
300 { kX86CallT, kCall, IS_UNARY_OP | IS_BRANCH | IS_LOAD, { THREAD_PREFIX, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallT", "fs:[!0d]" },
301 { kX86Ret, kNullary,NO_OPERAND | IS_BRANCH, { 0, 0, 0xC3, 0, 0, 0, 0, 0 }, "Ret", "" },
302
303 { kX86StartOfMethod, kMacro, IS_UNARY_OP | SETS_CCODES, { 0,0,0,0,0,0,0,0 }, "StartOfMethod", "!0r" },
304 { kX86PcRelLoadRA, kPcRel, IS_LOAD | IS_QUIN_OP, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "PcRelLoadRA", "!0r,[!1r+!2r<<!3d+!4p]" },
305 { kX86PcRelAdr, kPcRel, IS_LOAD | IS_BINARY_OP, { 0, 0, 0xB8, 0, 0, 0, 0, 4 }, "PcRelAdr", "!0r,!1d" },
buzbeee88dfbf2012-03-05 11:19:57 -0800306};
307
Ian Rogersb5d09b22012-03-06 22:14:17 -0800308static size_t computeSize(X86EncodingMap* entry, int displacement, bool has_sib) {
309 size_t size = 0;
310 if (entry->skeleton.prefix1 > 0) {
311 ++size;
312 if (entry->skeleton.prefix2 > 0) {
313 ++size;
Ian Rogersde797832012-03-06 10:18:10 -0800314 }
Ian Rogersde797832012-03-06 10:18:10 -0800315 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800316 ++size; // opcode
317 if (entry->skeleton.opcode == 0x0F) {
318 ++size;
319 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
320 ++size;
321 }
322 }
323 ++size; // modrm
324 if (has_sib) {
325 ++size;
326 }
327 if (displacement != 0) {
328 if (entry->opcode != kX86Lea32RA) {
Ian Rogers7caad772012-03-30 01:07:54 -0700329 DCHECK_NE(entry->flags & (IS_LOAD | IS_STORE), 0) << entry->name;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800330 }
331 size += IS_SIMM8(displacement) ? 1 : 4;
332 }
333 size += entry->skeleton.immediate_bytes;
334 return size;
335}
336
337int oatGetInsnSize(LIR* lir) {
338 X86EncodingMap* entry = &EncodingMap[lir->opcode];
339 switch (entry->kind) {
340 case kData:
341 return 4; // 4 bytes of data
342 case kNop:
343 return lir->operands[0]; // length of nop is sole operand
344 case kNullary:
345 return 1; // 1 byte of opcode
346 case kReg: // lir operands - 0: reg
347 return computeSize(entry, 0, false);
348 case kMem: { // lir operands - 0: base, 1: disp
349 int base = lir->operands[0];
350 // SP requires a special extra SIB byte
351 return computeSize(entry, lir->operands[1], false) + (base == rSP ? 1 : 0);
352 }
353 case kArray: // lir operands - 0: base, 1: index, 2: scale, 3: disp
354 return computeSize(entry, lir->operands[3], true);
355 case kMemReg: { // lir operands - 0: base, 1: disp, 2: reg
356 int base = lir->operands[0];
357 // SP requires a special extra SIB byte
358 return computeSize(entry, lir->operands[1], false) + (base == rSP ? 1 : 0);
359 }
360 case kArrayReg: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
361 return computeSize(entry, lir->operands[3], true);
362 case kThreadReg: // lir operands - 0: disp, 1: reg
363 return computeSize(entry, lir->operands[0], false);
364 case kRegReg:
365 return computeSize(entry, 0, false);
366 case kRegMem: { // lir operands - 0: reg, 1: base, 2: disp
367 int base = lir->operands[1];
368 return computeSize(entry, lir->operands[2], false) + (base == rSP ? 1 : 0);
369 }
370 case kRegArray: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
371 return computeSize(entry, lir->operands[4], true);
372 case kRegThread: // lir operands - 0: reg, 1: disp
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700373 return computeSize(entry, 0x12345678, false); // displacement size is always 32bit
Ian Rogersb5d09b22012-03-06 22:14:17 -0800374 case kRegImm: { // lir operands - 0: reg, 1: immediate
Ian Rogersb41b33b2012-03-20 14:22:54 -0700375 size_t size = computeSize(entry, 0, false);
376 if (entry->skeleton.ax_opcode == 0) {
377 return size;
378 } else {
379 // AX opcodes don't require the modrm byte.
380 int reg = lir->operands[0];
381 return size - (reg == rAX ? 1 : 0);
382 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800383 }
384 case kMemImm: // lir operands - 0: base, 1: disp, 2: immediate
385 CHECK_NE(lir->operands[0], static_cast<int>(rSP)); // TODO: add extra SIB byte
386 return computeSize(entry, lir->operands[1], false);
387 case kArrayImm: // lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
388 return computeSize(entry, lir->operands[3], true);
389 case kThreadImm: // lir operands - 0: disp, 1: imm
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700390 return computeSize(entry, 0x12345678, false); // displacement size is always 32bit
Ian Rogersb5d09b22012-03-06 22:14:17 -0800391 case kRegRegImm: // lir operands - 0: reg, 1: reg, 2: imm
392 return computeSize(entry, 0, false);
393 case kRegMemImm: // lir operands - 0: reg, 1: base, 2: disp, 3: imm
394 CHECK_NE(lir->operands[1], static_cast<int>(rSP)); // TODO: add extra SIB byte
395 return computeSize(entry, lir->operands[2], false);
396 case kRegArrayImm: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp, 5: imm
397 return computeSize(entry, lir->operands[4], true);
398 case kMovRegImm: // lir operands - 0: reg, 1: immediate
399 return 1 + entry->skeleton.immediate_bytes;
400 case kShiftRegImm: // lir operands - 0: reg, 1: immediate
401 // Shift by immediate one has a shorter opcode.
402 return computeSize(entry, 0, false) - (lir->operands[1] == 1 ? 1 : 0);
403 case kShiftMemImm: // lir operands - 0: base, 1: disp, 2: immediate
404 CHECK_NE(lir->operands[0], static_cast<int>(rSP)); // TODO: add extra SIB byte
405 // Shift by immediate one has a shorter opcode.
406 return computeSize(entry, lir->operands[1], false) - (lir->operands[2] == 1 ? 1 : 0);
407 case kShiftArrayImm: // lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
408 // Shift by immediate one has a shorter opcode.
409 return computeSize(entry, lir->operands[3], true) - (lir->operands[4] == 1 ? 1 : 0);
410 case kShiftRegCl:
411 return computeSize(entry, 0, false);
412 case kShiftMemCl: // lir operands - 0: base, 1: disp, 2: cl
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 kShiftArrayCl: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
416 return computeSize(entry, lir->operands[3], true);
417 case kRegCond: // lir operands - 0: reg, 1: cond
418 return computeSize(entry, 0, false);
419 case kMemCond: // lir operands - 0: base, 1: disp, 2: cond
420 CHECK_NE(lir->operands[0], static_cast<int>(rSP)); // TODO: add extra SIB byte
421 return computeSize(entry, lir->operands[1], false);
422 case kArrayCond: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: cond
423 return computeSize(entry, lir->operands[3], true);
Ian Rogersb41b33b2012-03-20 14:22:54 -0700424 case kJcc:
425 if (lir->opcode == kX86Jcc8) {
426 return 2; // opcode + rel8
427 } else {
428 DCHECK(lir->opcode == kX86Jcc32);
429 return 6; // 2 byte opcode + rel32
430 }
431 case kJmp:
432 if (lir->opcode == kX86Jmp8) {
433 return 2; // opcode + rel8
Ian Rogers7caad772012-03-30 01:07:54 -0700434 } else if (lir->opcode == kX86Jmp32) {
Ian Rogersb41b33b2012-03-20 14:22:54 -0700435 return 5; // opcode + rel32
Ian Rogers7caad772012-03-30 01:07:54 -0700436 } else {
437 DCHECK(lir->opcode == kX86JmpR);
438 return 2; // opcode + modrm
Ian Rogersb41b33b2012-03-20 14:22:54 -0700439 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800440 case kCall:
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700441 switch (lir->opcode) {
Ian Rogersb5d09b22012-03-06 22:14:17 -0800442 case kX86CallR: return 2; // opcode modrm
443 case kX86CallM: // lir operands - 0: base, 1: disp
444 return computeSize(entry, lir->operands[1], false);
445 case kX86CallA: // lir operands - 0: base, 1: index, 2: scale, 3: disp
446 return computeSize(entry, lir->operands[3], true);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700447 case kX86CallT: // lir operands - 0: disp
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700448 return computeSize(entry, 0x12345678, false); // displacement size is always 32bit
Ian Rogersb5d09b22012-03-06 22:14:17 -0800449 default:
450 break;
451 }
452 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700453 case kPcRel:
454 if (entry->opcode == kX86PcRelLoadRA) {
455 // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
456 return computeSize(entry, 0x12345678, true);
457 } else {
458 DCHECK(entry->opcode == kX86PcRelAdr);
459 return 5; // opcode with reg + 4 byte immediate
460 }
461 case kMacro:
462 DCHECK_EQ(lir->opcode, static_cast<int>(kX86StartOfMethod));
463 return 5 /* call opcode + 4 byte displacement */ + 1 /* pop reg */ +
464 computeSize(&EncodingMap[kX86Sub32RI], 0, false) -
465 (lir->operands[0] == rAX ? 1 : 0); // shorter ax encoding
Ian Rogersb5d09b22012-03-06 22:14:17 -0800466 default:
467 break;
468 }
469 UNIMPLEMENTED(FATAL) << "Unimplemented size encoding for: " << entry->name;
Ian Rogersde797832012-03-06 10:18:10 -0800470 return 0;
471}
buzbeee88dfbf2012-03-05 11:19:57 -0800472
Ian Rogersb5d09b22012-03-06 22:14:17 -0800473static uint8_t modrmForDisp(int disp) {
474 if (disp == 0) {
475 return 0;
476 } else if (IS_SIMM8(disp)) {
477 return 1;
478 } else {
479 return 2;
480 }
481}
482
483static void emitDisp(CompilationUnit* cUnit, int disp) {
484 if (disp == 0) {
485 return;
486 } else if (IS_SIMM8(disp)) {
487 cUnit->codeBuffer.push_back(disp & 0xFF);
488 } else {
489 cUnit->codeBuffer.push_back(disp & 0xFF);
490 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
491 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
492 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
493 }
494}
495
496static void emitOpReg(CompilationUnit* cUnit, const X86EncodingMap* entry, uint8_t reg) {
497 if (entry->skeleton.prefix1 != 0) {
498 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
499 if (entry->skeleton.prefix2 != 0) {
500 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
501 }
502 } else {
503 DCHECK_EQ(0, entry->skeleton.prefix2);
504 }
505 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
506 if (entry->skeleton.opcode == 0x0F) {
507 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
508 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
509 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
510 } else {
511 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
512 }
513 } else {
514 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
515 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
516 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700517 if (FPREG(reg)) {
518 reg = reg & FP_REG_MASK;
519 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800520 DCHECK_LT(reg, 8);
521 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
522 cUnit->codeBuffer.push_back(modrm);
523 DCHECK_EQ(0, entry->skeleton.ax_opcode);
524 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
525}
526
527static void emitOpMem(CompilationUnit* cUnit, const X86EncodingMap* entry, uint8_t base, int disp) {
528 if (entry->skeleton.prefix1 != 0) {
529 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
530 if (entry->skeleton.prefix2 != 0) {
531 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
532 }
533 } else {
534 DCHECK_EQ(0, entry->skeleton.prefix2);
535 }
536 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
537 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
538 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
539 DCHECK_LT(entry->skeleton.modrm_opcode, 8);
540 DCHECK_LT(base, 8);
541 uint8_t modrm = (modrmForDisp(disp) << 6) | (entry->skeleton.modrm_opcode << 3) | base;
542 cUnit->codeBuffer.push_back(modrm);
543 emitDisp(cUnit, disp);
544 DCHECK_EQ(0, entry->skeleton.ax_opcode);
545 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
546}
547
548static void emitMemReg(CompilationUnit* cUnit, const X86EncodingMap* entry,
549 uint8_t base, int disp, uint8_t reg) {
550 if (entry->skeleton.prefix1 != 0) {
551 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
552 if (entry->skeleton.prefix2 != 0) {
553 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
554 }
555 } else {
556 DCHECK_EQ(0, entry->skeleton.prefix2);
557 }
558 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
559 if (entry->skeleton.opcode == 0x0F) {
560 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
561 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
562 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
563 } else {
564 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
565 }
566 } else {
567 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
568 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
569 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700570 if (FPREG(reg)) {
571 reg = reg & FP_REG_MASK;
572 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800573 DCHECK_LT(reg, 8);
574 DCHECK_LT(base, 8);
575 uint8_t modrm = (modrmForDisp(disp) << 6) | (reg << 3) | base;
576 cUnit->codeBuffer.push_back(modrm);
577 if (base == rSP) {
578 // Special SIB for SP base
579 cUnit->codeBuffer.push_back(0 << 6 | (rSP << 3) | rSP);
580 }
581 emitDisp(cUnit, disp);
582 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
583 DCHECK_EQ(0, entry->skeleton.ax_opcode);
584 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
585}
586
587static void emitRegMem(CompilationUnit* cUnit, const X86EncodingMap* entry,
588 uint8_t reg, uint8_t base, int disp) {
589 // Opcode will flip operands.
590 emitMemReg(cUnit, entry, base, disp, reg);
591}
592
593static void emitRegArray(CompilationUnit* cUnit, const X86EncodingMap* entry, uint8_t reg,
594 uint8_t base, uint8_t index, int scale, int disp) {
595 if (entry->skeleton.prefix1 != 0) {
596 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
597 if (entry->skeleton.prefix2 != 0) {
598 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
599 }
600 } else {
601 DCHECK_EQ(0, entry->skeleton.prefix2);
602 }
603 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
604 if (entry->skeleton.opcode == 0x0F) {
605 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
606 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
607 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
608 } else {
609 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
610 }
611 } else {
612 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
613 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
614 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700615 if (FPREG(reg)) {
616 reg = reg & FP_REG_MASK;
617 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800618 DCHECK_LT(reg, 8);
619 uint8_t modrm = (modrmForDisp(disp) << 6) | (reg << 3) | rSP;
620 cUnit->codeBuffer.push_back(modrm);
621 DCHECK_LT(scale, 4);
622 DCHECK_LT(index, 8);
623 DCHECK_LT(base, 8);
624 uint8_t sib = (scale << 6) | (index << 3) | base;
625 cUnit->codeBuffer.push_back(sib);
626 emitDisp(cUnit, disp);
627 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
628 DCHECK_EQ(0, entry->skeleton.ax_opcode);
629 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
630}
631
Ian Rogersb41b33b2012-03-20 14:22:54 -0700632static void emitArrayReg(CompilationUnit* cUnit, const X86EncodingMap* entry,
633 uint8_t base, uint8_t index, int scale, int disp, uint8_t reg) {
634 // Opcode will flip operands.
635 emitRegArray(cUnit, entry, reg, base, index, scale, disp);
636}
637
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700638static void emitRegThread(CompilationUnit* cUnit, const X86EncodingMap* entry,
639 uint8_t reg, int disp) {
640 DCHECK_NE(entry->skeleton.prefix1, 0);
641 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
642 if (entry->skeleton.prefix2 != 0) {
643 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
644 }
645 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
646 if (entry->skeleton.opcode == 0x0F) {
647 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
648 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
649 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
650 } else {
651 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
652 }
653 } else {
654 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
655 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
656 }
657 if (FPREG(reg)) {
658 reg = reg & FP_REG_MASK;
659 }
660 DCHECK_LT(reg, 8);
661 uint8_t modrm = (0 << 6) | (reg << 3) | rBP;
662 cUnit->codeBuffer.push_back(modrm);
663 cUnit->codeBuffer.push_back(disp & 0xFF);
664 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
665 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
666 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
667 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
668 DCHECK_EQ(0, entry->skeleton.ax_opcode);
669 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
670}
671
Ian Rogersb5d09b22012-03-06 22:14:17 -0800672static void emitRegReg(CompilationUnit* cUnit, const X86EncodingMap* entry,
673 uint8_t reg1, uint8_t reg2) {
674 if (entry->skeleton.prefix1 != 0) {
675 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
676 if (entry->skeleton.prefix2 != 0) {
677 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
678 }
679 } else {
680 DCHECK_EQ(0, entry->skeleton.prefix2);
681 }
682 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
683 if (entry->skeleton.opcode == 0x0F) {
684 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
685 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
686 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
687 } else {
688 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
689 }
690 } else {
691 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
692 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
693 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700694 if (FPREG(reg1)) {
695 reg1 = reg1 & FP_REG_MASK;
696 }
697 if (FPREG(reg2)) {
698 reg2 = reg2 & FP_REG_MASK;
699 }
700 DCHECK_LT(reg1, 8);
701 DCHECK_LT(reg2, 8);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800702 uint8_t modrm = (3 << 6) | (reg1 << 3) | reg2;
703 cUnit->codeBuffer.push_back(modrm);
704 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
705 DCHECK_EQ(0, entry->skeleton.ax_opcode);
706 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
707}
708
Elliott Hughes225ae522012-04-16 20:21:45 -0700709static void emitRegRegImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
710 uint8_t reg1, uint8_t reg2, int32_t imm) {
711 if (entry->skeleton.prefix1 != 0) {
712 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
713 if (entry->skeleton.prefix2 != 0) {
714 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
715 }
716 } else {
717 DCHECK_EQ(0, entry->skeleton.prefix2);
718 }
719 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
720 if (entry->skeleton.opcode == 0x0F) {
721 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
722 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
723 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
724 } else {
725 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
726 }
727 } else {
728 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
729 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
730 }
731 if (FPREG(reg1)) {
732 reg1 = reg1 & FP_REG_MASK;
733 }
734 if (FPREG(reg2)) {
735 reg2 = reg2 & FP_REG_MASK;
736 }
737 DCHECK_LT(reg1, 8);
738 DCHECK_LT(reg2, 8);
739 uint8_t modrm = (3 << 6) | (reg1 << 3) | reg2;
740 cUnit->codeBuffer.push_back(modrm);
741 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
742 DCHECK_EQ(0, entry->skeleton.ax_opcode);
743 switch (entry->skeleton.immediate_bytes) {
744 case 1:
745 DCHECK(IS_SIMM8(imm));
746 cUnit->codeBuffer.push_back(imm & 0xFF);
747 break;
748 case 2:
749 DCHECK(IS_SIMM16(imm));
750 cUnit->codeBuffer.push_back(imm & 0xFF);
751 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
752 break;
753 case 4:
754 cUnit->codeBuffer.push_back(imm & 0xFF);
755 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
756 cUnit->codeBuffer.push_back((imm >> 16) & 0xFF);
757 cUnit->codeBuffer.push_back((imm >> 24) & 0xFF);
758 break;
759 default:
760 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
761 << ") for instruction: " << entry->name;
762 break;
763 }
764}
765
Ian Rogersb5d09b22012-03-06 22:14:17 -0800766static void emitRegImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
767 uint8_t reg, int imm) {
768 if (entry->skeleton.prefix1 != 0) {
769 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
770 if (entry->skeleton.prefix2 != 0) {
771 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
772 }
773 } else {
774 DCHECK_EQ(0, entry->skeleton.prefix2);
775 }
776 if (reg == rAX && entry->skeleton.ax_opcode != 0) {
777 cUnit->codeBuffer.push_back(entry->skeleton.ax_opcode);
778 } else {
779 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
780 if (entry->skeleton.opcode == 0x0F) {
781 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
782 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
783 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
784 } else {
785 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
786 }
787 } else {
788 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
789 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
790 }
791 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
792 cUnit->codeBuffer.push_back(modrm);
793 }
794 switch (entry->skeleton.immediate_bytes) {
795 case 1:
796 DCHECK(IS_SIMM8(imm));
797 cUnit->codeBuffer.push_back(imm & 0xFF);
798 break;
799 case 2:
800 DCHECK(IS_SIMM16(imm));
801 cUnit->codeBuffer.push_back(imm & 0xFF);
802 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
803 break;
804 case 4:
805 cUnit->codeBuffer.push_back(imm & 0xFF);
806 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
807 cUnit->codeBuffer.push_back((imm >> 16) & 0xFF);
808 cUnit->codeBuffer.push_back((imm >> 24) & 0xFF);
809 break;
810 default:
811 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
812 << ") for instruction: " << entry->name;
813 break;
814 }
815}
816
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700817static void emitThreadImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
818 int disp, int imm) {
819 if (entry->skeleton.prefix1 != 0) {
820 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
821 if (entry->skeleton.prefix2 != 0) {
822 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
823 }
824 } else {
825 DCHECK_EQ(0, entry->skeleton.prefix2);
826 }
827 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
828 if (entry->skeleton.opcode == 0x0F) {
829 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
830 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
831 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
832 } else {
833 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
834 }
835 } else {
836 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
837 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
838 }
839 uint8_t modrm = (0 << 6) | (entry->skeleton.modrm_opcode << 3) | rBP;
840 cUnit->codeBuffer.push_back(modrm);
841 cUnit->codeBuffer.push_back(disp & 0xFF);
842 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
843 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
844 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
845 switch (entry->skeleton.immediate_bytes) {
846 case 1:
847 DCHECK(IS_SIMM8(imm));
848 cUnit->codeBuffer.push_back(imm & 0xFF);
849 break;
850 case 2:
851 DCHECK(IS_SIMM16(imm));
852 cUnit->codeBuffer.push_back(imm & 0xFF);
853 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
854 break;
855 case 4:
856 cUnit->codeBuffer.push_back(imm & 0xFF);
857 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
858 cUnit->codeBuffer.push_back((imm >> 16) & 0xFF);
859 cUnit->codeBuffer.push_back((imm >> 24) & 0xFF);
860 break;
861 default:
862 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
863 << ") for instruction: " << entry->name;
864 break;
865 }
866 DCHECK_EQ(entry->skeleton.ax_opcode, 0);
867}
868
869static void emitMovRegImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
870 uint8_t reg, int imm) {
871 DCHECK_LT(reg, 8);
872 cUnit->codeBuffer.push_back(0xB8 + reg);
873 cUnit->codeBuffer.push_back(imm & 0xFF);
874 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
875 cUnit->codeBuffer.push_back((imm >> 16) & 0xFF);
876 cUnit->codeBuffer.push_back((imm >> 24) & 0xFF);
877}
878
Ian Rogersb41b33b2012-03-20 14:22:54 -0700879static void emitShiftRegImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
Ian Rogers7caad772012-03-30 01:07:54 -0700880 uint8_t reg, int imm) {
Ian Rogersb41b33b2012-03-20 14:22:54 -0700881 if (entry->skeleton.prefix1 != 0) {
882 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
883 if (entry->skeleton.prefix2 != 0) {
884 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
885 }
886 } else {
887 DCHECK_EQ(0, entry->skeleton.prefix2);
888 }
889 if (imm != 1) {
890 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
891 } else {
892 // Shorter encoding for 1 bit shift
893 cUnit->codeBuffer.push_back(entry->skeleton.ax_opcode);
894 }
895 if (entry->skeleton.opcode == 0x0F) {
896 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
897 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
898 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
899 } else {
900 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
901 }
902 } else {
903 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
904 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
905 }
906 DCHECK_LT(reg, 8);
Ian Rogers7caad772012-03-30 01:07:54 -0700907 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
Ian Rogersb41b33b2012-03-20 14:22:54 -0700908 cUnit->codeBuffer.push_back(modrm);
909 if (imm != 1) {
910 DCHECK_EQ(entry->skeleton.immediate_bytes, 1);
911 DCHECK(IS_SIMM8(imm));
912 cUnit->codeBuffer.push_back(imm & 0xFF);
913 }
914}
915
Ian Rogers7caad772012-03-30 01:07:54 -0700916static void emitShiftRegCl(CompilationUnit* cUnit, const X86EncodingMap* entry,
917 uint8_t reg, uint8_t cl) {
918 DCHECK_EQ(cl, static_cast<uint8_t>(rCX));
919 if (entry->skeleton.prefix1 != 0) {
920 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
921 if (entry->skeleton.prefix2 != 0) {
922 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
923 }
924 } else {
925 DCHECK_EQ(0, entry->skeleton.prefix2);
926 }
927 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
928 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
929 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
930 DCHECK_LT(reg, 8);
931 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
932 cUnit->codeBuffer.push_back(modrm);
933 DCHECK_EQ(0, entry->skeleton.ax_opcode);
934 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
935}
936
937static void emitRegCond(CompilationUnit* cUnit, const X86EncodingMap* entry,
938 uint8_t reg, uint8_t condition) {
939 if (entry->skeleton.prefix1 != 0) {
940 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
941 if (entry->skeleton.prefix2 != 0) {
942 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
943 }
944 } else {
945 DCHECK_EQ(0, entry->skeleton.prefix2);
946 }
947 DCHECK_EQ(0, entry->skeleton.ax_opcode);
948 DCHECK_EQ(0x0F, entry->skeleton.opcode);
949 cUnit->codeBuffer.push_back(0x0F);
950 DCHECK_EQ(0x90, entry->skeleton.extra_opcode1);
951 cUnit->codeBuffer.push_back(0x90 | condition);
952 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
953 DCHECK_LT(reg, 8);
954 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
955 cUnit->codeBuffer.push_back(modrm);
956 DCHECK_EQ(entry->skeleton.immediate_bytes, 0);
957}
958
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700959static void emitJmp(CompilationUnit* cUnit, const X86EncodingMap* entry, int rel) {
Ian Rogersb41b33b2012-03-20 14:22:54 -0700960 if (entry->opcode == kX86Jmp8) {
961 DCHECK(IS_SIMM8(rel));
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700962 cUnit->codeBuffer.push_back(0xEB);
963 cUnit->codeBuffer.push_back(rel & 0xFF);
Ian Rogers7caad772012-03-30 01:07:54 -0700964 } else if (entry->opcode == kX86Jmp32) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700965 cUnit->codeBuffer.push_back(0xE9);
966 cUnit->codeBuffer.push_back(rel & 0xFF);
967 cUnit->codeBuffer.push_back((rel >> 8) & 0xFF);
968 cUnit->codeBuffer.push_back((rel >> 16) & 0xFF);
969 cUnit->codeBuffer.push_back((rel >> 24) & 0xFF);
Ian Rogers7caad772012-03-30 01:07:54 -0700970 } else {
971 DCHECK(entry->opcode == kX86JmpR);
972 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
973 uint8_t reg = static_cast<uint8_t>(rel);
974 DCHECK_LT(reg, 8);
975 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
976 cUnit->codeBuffer.push_back(modrm);
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700977 }
978}
979
980static void emitJcc(CompilationUnit* cUnit, const X86EncodingMap* entry,
981 int rel, uint8_t cc) {
982 DCHECK_LT(cc, 16);
Ian Rogersb41b33b2012-03-20 14:22:54 -0700983 if (entry->opcode == kX86Jcc8) {
984 DCHECK(IS_SIMM8(rel));
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700985 cUnit->codeBuffer.push_back(0x70 | cc);
986 cUnit->codeBuffer.push_back(rel & 0xFF);
987 } else {
Ian Rogersb41b33b2012-03-20 14:22:54 -0700988 DCHECK(entry->opcode == kX86Jcc32);
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700989 cUnit->codeBuffer.push_back(0x0F);
990 cUnit->codeBuffer.push_back(0x80 | cc);
991 cUnit->codeBuffer.push_back(rel & 0xFF);
992 cUnit->codeBuffer.push_back((rel >> 8) & 0xFF);
993 cUnit->codeBuffer.push_back((rel >> 16) & 0xFF);
994 cUnit->codeBuffer.push_back((rel >> 24) & 0xFF);
995 }
996}
997
998static void emitCallMem(CompilationUnit* cUnit, const X86EncodingMap* entry,
999 uint8_t base, int disp) {
1000 if (entry->skeleton.prefix1 != 0) {
1001 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
1002 if (entry->skeleton.prefix2 != 0) {
1003 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
1004 }
1005 } else {
1006 DCHECK_EQ(0, entry->skeleton.prefix2);
1007 }
1008 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
1009 if (entry->skeleton.opcode == 0x0F) {
1010 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
1011 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
1012 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
1013 } else {
1014 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1015 }
1016 } else {
1017 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1018 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1019 }
1020 uint8_t modrm = (modrmForDisp(disp) << 6) | (entry->skeleton.modrm_opcode << 3) | base;
1021 cUnit->codeBuffer.push_back(modrm);
1022 if (base == rSP) {
1023 // Special SIB for SP base
1024 cUnit->codeBuffer.push_back(0 << 6 | (rSP << 3) | rSP);
1025 }
1026 emitDisp(cUnit, disp);
1027 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1028 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1029}
1030
1031static void emitCallThread(CompilationUnit* cUnit, const X86EncodingMap* entry, int disp) {
1032 DCHECK_NE(entry->skeleton.prefix1, 0);
1033 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
1034 if (entry->skeleton.prefix2 != 0) {
1035 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
1036 }
1037 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
1038 if (entry->skeleton.opcode == 0x0F) {
1039 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
1040 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
1041 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
1042 } else {
1043 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1044 }
1045 } else {
1046 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1047 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1048 }
1049 uint8_t modrm = (0 << 6) | (entry->skeleton.modrm_opcode << 3) | rBP;
1050 cUnit->codeBuffer.push_back(modrm);
1051 cUnit->codeBuffer.push_back(disp & 0xFF);
1052 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
1053 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
1054 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
1055 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1056 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1057}
1058
Ian Rogers7caad772012-03-30 01:07:54 -07001059static void emitPcRel(CompilationUnit* cUnit, const X86EncodingMap* entry, uint8_t reg,
1060 int base_or_table, uint8_t index, int scale, int table_or_disp) {
1061 int disp;
1062 if (entry->opcode == kX86PcRelLoadRA) {
1063 SwitchTable *tabRec = (SwitchTable*)table_or_disp;
1064 disp = tabRec->offset;
1065 } else {
1066 DCHECK(entry->opcode == kX86PcRelAdr);
1067 FillArrayData *tabRec = (FillArrayData *)base_or_table;
1068 disp = tabRec->offset;
1069 }
1070 if (entry->skeleton.prefix1 != 0) {
1071 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
1072 if (entry->skeleton.prefix2 != 0) {
1073 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
1074 }
1075 } else {
1076 DCHECK_EQ(0, entry->skeleton.prefix2);
1077 }
1078 if (FPREG(reg)) {
1079 reg = reg & FP_REG_MASK;
1080 }
1081 DCHECK_LT(reg, 8);
1082 if (entry->opcode == kX86PcRelLoadRA) {
1083 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
1084 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1085 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1086 uint8_t modrm = (2 << 6) | (reg << 3) | rSP;
1087 cUnit->codeBuffer.push_back(modrm);
1088 DCHECK_LT(scale, 4);
1089 DCHECK_LT(index, 8);
1090 DCHECK_LT(base_or_table, 8);
1091 uint8_t base = static_cast<uint8_t>(base_or_table);
1092 uint8_t sib = (scale << 6) | (index << 3) | base;
1093 cUnit->codeBuffer.push_back(sib);
1094 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1095 } else {
1096 cUnit->codeBuffer.push_back(entry->skeleton.opcode + reg);
1097 }
1098 cUnit->codeBuffer.push_back(disp & 0xFF);
1099 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
1100 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
1101 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
1102 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1103 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1104}
1105
1106static void emitMacro(CompilationUnit* cUnit, const X86EncodingMap* entry,
1107 uint8_t reg, int offset) {
1108 DCHECK(entry->opcode == kX86StartOfMethod) << entry->name;
1109 cUnit->codeBuffer.push_back(0xE8); // call +0
1110 cUnit->codeBuffer.push_back(0);
1111 cUnit->codeBuffer.push_back(0);
1112 cUnit->codeBuffer.push_back(0);
1113 cUnit->codeBuffer.push_back(0);
1114
1115 DCHECK_LT(reg, 8);
1116 cUnit->codeBuffer.push_back(0x58 + reg); // pop reg
1117
1118 emitRegImm(cUnit, &EncodingMap[kX86Sub32RI], reg, offset + 5 /* size of call +0 */);
1119}
1120
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001121void emitUnimplemented(CompilationUnit* cUnit, const X86EncodingMap* entry, LIR* lir) {
Elliott Hughes225ae522012-04-16 20:21:45 -07001122 UNIMPLEMENTED(WARNING) << "encoding kind for " << entry->name << " " << buildInsnString(entry->fmt, lir, 0);
Ian Rogers141b0c72012-03-15 18:18:52 -07001123 for (int i = 0; i < oatGetInsnSize(lir); ++i) {
1124 cUnit->codeBuffer.push_back(0xCC); // push breakpoint instruction - int 3
1125 }
1126}
1127
buzbeee88dfbf2012-03-05 11:19:57 -08001128/*
1129 * Assemble the LIR into binary instruction format. Note that we may
1130 * discover that pc-relative displacements may not fit the selected
1131 * instruction. In those cases we will try to substitute a new code
1132 * sequence or request that the trace be shortened and retried.
1133 */
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001134AssemblerStatus oatAssembleInstructions(CompilationUnit *cUnit, intptr_t startAddr) {
Ian Rogersb5d09b22012-03-06 22:14:17 -08001135 LIR *lir;
1136 AssemblerStatus res = kSuccess; // Assume success
buzbeee88dfbf2012-03-05 11:19:57 -08001137
Ian Rogers141d6222012-04-05 12:23:06 -07001138 const bool kVerbosePcFixup = false;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001139 for (lir = (LIR *) cUnit->firstLIRInsn; lir; lir = NEXT_LIR(lir)) {
1140 if (lir->opcode < 0) {
1141 continue;
buzbeee88dfbf2012-03-05 11:19:57 -08001142 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001143
Ian Rogersb5d09b22012-03-06 22:14:17 -08001144 if (lir->flags.isNop) {
1145 continue;
1146 }
1147
1148 if (lir->flags.pcRelFixup) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001149 switch (lir->opcode) {
Ian Rogersb41b33b2012-03-20 14:22:54 -07001150 case kX86Jcc8: {
1151 LIR *targetLIR = lir->target;
1152 DCHECK(targetLIR != NULL);
1153 int delta = 0;
1154 intptr_t pc;
1155 if (IS_SIMM8(lir->operands[0])) {
1156 pc = lir->offset + 2 /* opcode + rel8 */;
1157 } else {
1158 pc = lir->offset + 6 /* 2 byte opcode + rel32 */;
1159 }
1160 intptr_t target = targetLIR->offset;
1161 delta = target - pc;
1162 if (IS_SIMM8(delta) != IS_SIMM8(lir->operands[0])) {
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001163 if (kVerbosePcFixup) {
1164 LOG(INFO) << "Retry for JCC growth at " << lir->offset
1165 << " delta: " << delta << " old delta: " << lir->operands[0];
1166 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001167 lir->opcode = kX86Jcc32;
1168 oatSetupResourceMasks(lir);
1169 res = kRetryAll;
1170 }
Ian Rogers7caad772012-03-30 01:07:54 -07001171 if (kVerbosePcFixup) {
1172 LOG(INFO) << "Source:";
1173 oatDumpLIRInsn(cUnit, lir, 0);
1174 LOG(INFO) << "Target:";
1175 oatDumpLIRInsn(cUnit, targetLIR, 0);
1176 LOG(INFO) << "Delta " << delta;
1177 }
1178 lir->operands[0] = delta;
1179 break;
1180 }
1181 case kX86Jcc32: {
1182 LIR *targetLIR = lir->target;
1183 DCHECK(targetLIR != NULL);
1184 intptr_t pc = lir->offset + 6 /* 2 byte opcode + rel32 */;
1185 intptr_t target = targetLIR->offset;
1186 int delta = target - pc;
1187 if (kVerbosePcFixup) {
1188 LOG(INFO) << "Source:";
1189 oatDumpLIRInsn(cUnit, lir, 0);
1190 LOG(INFO) << "Target:";
1191 oatDumpLIRInsn(cUnit, targetLIR, 0);
1192 LOG(INFO) << "Delta " << delta;
1193 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001194 lir->operands[0] = delta;
1195 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001196 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001197 case kX86Jmp8: {
1198 LIR *targetLIR = lir->target;
1199 DCHECK(targetLIR != NULL);
1200 int delta = 0;
1201 intptr_t pc;
1202 if (IS_SIMM8(lir->operands[0])) {
1203 pc = lir->offset + 2 /* opcode + rel8 */;
1204 } else {
1205 pc = lir->offset + 5 /* opcode + rel32 */;
1206 }
1207 intptr_t target = targetLIR->offset;
1208 delta = target - pc;
1209 if (!(cUnit->disableOpt & (1 << kSafeOptimizations)) && lir->operands[0] == 0) {
1210 // Useless branch
1211 lir->flags.isNop = true;
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001212 if (kVerbosePcFixup) {
1213 LOG(INFO) << "Retry for useless branch at " << lir->offset;
1214 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001215 res = kRetryAll;
1216 } else if (IS_SIMM8(delta) != IS_SIMM8(lir->operands[0])) {
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001217 if (kVerbosePcFixup) {
1218 LOG(INFO) << "Retry for JMP growth at " << lir->offset;
1219 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001220 lir->opcode = kX86Jmp32;
1221 oatSetupResourceMasks(lir);
1222 res = kRetryAll;
1223 }
1224 lir->operands[0] = delta;
1225 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001226 }
Ian Rogers7caad772012-03-30 01:07:54 -07001227 case kX86Jmp32: {
1228 LIR *targetLIR = lir->target;
1229 DCHECK(targetLIR != NULL);
1230 intptr_t pc = lir->offset + 5 /* opcode + rel32 */;
1231 intptr_t target = targetLIR->offset;
1232 int delta = target - pc;
1233 lir->operands[0] = delta;
1234 break;
1235 }
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001236 default:
1237 break;
1238 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001239 }
1240
1241 /*
1242 * If one of the pc-relative instructions expanded we'll have
1243 * to make another pass. Don't bother to fully assemble the
1244 * instruction.
1245 */
1246 if (res != kSuccess) {
1247 continue;
1248 }
Ian Rogers7caad772012-03-30 01:07:54 -07001249 CHECK_EQ(static_cast<size_t>(lir->offset), cUnit->codeBuffer.size());
Ian Rogersb5d09b22012-03-06 22:14:17 -08001250 const X86EncodingMap *entry = &EncodingMap[lir->opcode];
Ian Rogers141b0c72012-03-15 18:18:52 -07001251 size_t starting_cbuf_size = cUnit->codeBuffer.size();
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001252 switch (entry->kind) {
Ian Rogersb5d09b22012-03-06 22:14:17 -08001253 case kData: // 4 bytes of data
1254 cUnit->codeBuffer.push_back(lir->operands[0]);
1255 break;
1256 case kNullary: // 1 byte of opcode
1257 DCHECK_EQ(0, entry->skeleton.prefix1);
1258 DCHECK_EQ(0, entry->skeleton.prefix2);
1259 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001260 if (entry->skeleton.extra_opcode1 != 0) {
1261 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
1262 if (entry->skeleton.extra_opcode2 != 0) {
1263 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
1264 }
1265 } else {
1266 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1267 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001268 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1269 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1270 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1271 break;
1272 case kReg: // lir operands - 0: reg
1273 emitOpReg(cUnit, entry, lir->operands[0]);
1274 break;
1275 case kMem: // lir operands - 0: base, 1: disp
1276 emitOpMem(cUnit, entry, lir->operands[0], lir->operands[1]);
1277 break;
1278 case kMemReg: // lir operands - 0: base, 1: disp, 2: reg
1279 emitMemReg(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1280 break;
Ian Rogersb41b33b2012-03-20 14:22:54 -07001281 case kArrayReg: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
1282 emitArrayReg(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2],
1283 lir->operands[3], lir->operands[4]);
1284 break;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001285 case kRegMem: // lir operands - 0: reg, 1: base, 2: disp
1286 emitRegMem(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1287 break;
1288 case kRegArray: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
1289 emitRegArray(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2],
1290 lir->operands[3], lir->operands[4]);
1291 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001292 case kRegThread: // lir operands - 0: reg, 1: disp
1293 emitRegThread(cUnit, entry, lir->operands[0], lir->operands[1]);
1294 break;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001295 case kRegReg: // lir operands - 0: reg1, 1: reg2
1296 emitRegReg(cUnit, entry, lir->operands[0], lir->operands[1]);
1297 break;
Elliott Hughes225ae522012-04-16 20:21:45 -07001298 case kRegRegImm:
1299 emitRegRegImm(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1300 break;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001301 case kRegImm: // lir operands - 0: reg, 1: immediate
1302 emitRegImm(cUnit, entry, lir->operands[0], lir->operands[1]);
1303 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001304 case kThreadImm: // lir operands - 0: disp, 1: immediate
1305 emitThreadImm(cUnit, entry, lir->operands[0], lir->operands[1]);
1306 break;
1307 case kMovRegImm: // lir operands - 0: reg, 1: immediate
1308 emitMovRegImm(cUnit, entry, lir->operands[0], lir->operands[1]);
1309 break;
Ian Rogersb41b33b2012-03-20 14:22:54 -07001310 case kShiftRegImm: // lir operands - 0: reg, 1: immediate
1311 emitShiftRegImm(cUnit, entry, lir->operands[0], lir->operands[1]);
1312 break;
Ian Rogers7caad772012-03-30 01:07:54 -07001313 case kShiftRegCl: // lir operands - 0: reg, 1: cl
1314 emitShiftRegCl(cUnit, entry, lir->operands[0], lir->operands[1]);
1315 break;
1316 case kRegCond: // lir operands - 0: reg, 1: condition
1317 emitRegCond(cUnit, entry, lir->operands[0], lir->operands[1]);
1318 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001319 case kJmp: // lir operands - 0: rel
1320 emitJmp(cUnit, entry, lir->operands[0]);
1321 break;
1322 case kJcc: // lir operands - 0: rel, 1: CC, target assigned
1323 emitJcc(cUnit, entry, lir->operands[0], lir->operands[1]);
1324 break;
1325 case kCall:
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001326 switch (entry->opcode) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001327 case kX86CallM: // lir operands - 0: base, 1: disp
1328 emitCallMem(cUnit, entry, lir->operands[0], lir->operands[1]);
1329 break;
1330 case kX86CallT: // lir operands - 0: disp
1331 emitCallThread(cUnit, entry, lir->operands[0]);
1332 break;
1333 default:
1334 emitUnimplemented(cUnit, entry, lir);
1335 break;
1336 }
1337 break;
Ian Rogers7caad772012-03-30 01:07:54 -07001338 case kPcRel: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
1339 emitPcRel(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2],
1340 lir->operands[3], lir->operands[4]);
1341 break;
1342 case kMacro:
1343 emitMacro(cUnit, entry, lir->operands[0], lir->offset);
1344 break;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001345 default:
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001346 emitUnimplemented(cUnit, entry, lir);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001347 break;
1348 }
Ian Rogers7caad772012-03-30 01:07:54 -07001349 CHECK_EQ(static_cast<size_t>(oatGetInsnSize(lir)),
1350 cUnit->codeBuffer.size() - starting_cbuf_size)
1351 << "Instruction size mismatch for entry: " << EncodingMap[lir->opcode].name;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001352 }
1353 return res;
buzbeee88dfbf2012-03-05 11:19:57 -08001354}
1355
buzbeee88dfbf2012-03-05 11:19:57 -08001356/*
1357 * Target-dependent offset assignment.
1358 * independent.
1359 */
1360int oatAssignInsnOffsets(CompilationUnit* cUnit)
1361{
1362 LIR* x86LIR;
1363 int offset = 0;
1364
1365 for (x86LIR = (LIR *) cUnit->firstLIRInsn;
1366 x86LIR;
1367 x86LIR = NEXT_LIR(x86LIR)) {
1368 x86LIR->offset = offset;
1369 if (x86LIR->opcode >= 0) {
1370 if (!x86LIR->flags.isNop) {
1371 offset += x86LIR->flags.size;
1372 }
1373 } else if (x86LIR->opcode == kPseudoPseudoAlign4) {
1374 if (offset & 0x2) {
1375 offset += 2;
1376 x86LIR->operands[0] = 1;
1377 } else {
1378 x86LIR->operands[0] = 0;
1379 }
1380 }
1381 /* Pseudo opcodes don't consume space */
1382 }
1383
1384 return offset;
1385}
1386
1387} // namespace art