blob: 7bd5c522de3ccb9ee8fc45ac8a53e5660d66f170 [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
jeffhaoe2962482012-06-28 11:29:57 -0700283 { 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 -0700284
jeffhaoe2962482012-06-28 11:29:57 -0700285 EXT_0F_ENCODING_MAP(Movdxr, 0x66, 0x6E, REG_DEF0),
286 EXT_0F_ENCODING_MAP(Movdrx, 0x66, 0x7E, REG_DEF0),
Ian Rogersb5d09b22012-03-06 22:14:17 -0800287
jeffhaoe2962482012-06-28 11:29:57 -0700288 { kX86Set8R, kRegCond, IS_BINARY_OP | REG_DEF0 | USES_CCODES, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0 }, "Set8R", "!1c !0r" },
289 { kX86Set8M, kMemCond, IS_STORE | IS_TERTIARY_OP | REG_USE0 | USES_CCODES, { 0, 0, 0x0F, 0x90, 0, 0, 0, 0 }, "Set8M", "!2c [!0r+!1d]" },
290 { 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 -0800291
Ian Rogersc6f3bb82012-03-21 20:40:33 -0700292 // TODO: load/store?
293 // Encode the modrm opcode as an extra opcode byte to avoid computation during assembly.
294 { kX86Mfence, kReg, NO_OPERAND, { 0, 0, 0x0F, 0xAE, 0, 6, 0, 0 }, "Mfence", "" },
295
jeffhaoe2962482012-06-28 11:29:57 -0700296 EXT_0F_ENCODING_MAP(Imul16, 0x66, 0xAF, REG_DEF0 | SETS_CCODES),
297 EXT_0F_ENCODING_MAP(Imul32, 0x00, 0xAF, REG_DEF0 | SETS_CCODES),
298 EXT_0F_ENCODING_MAP(Movzx8, 0x00, 0xB6, REG_DEF0),
299 EXT_0F_ENCODING_MAP(Movzx16, 0x00, 0xB7, REG_DEF0),
300 EXT_0F_ENCODING_MAP(Movsx8, 0x00, 0xBE, REG_DEF0),
301 EXT_0F_ENCODING_MAP(Movsx16, 0x00, 0xBF, REG_DEF0),
Ian Rogersb5d09b22012-03-06 22:14:17 -0800302#undef EXT_0F_ENCODING_MAP
303
jeffhaoe2962482012-06-28 11:29:57 -0700304 { kX86Jcc8, kJcc, IS_BINARY_OP | IS_BRANCH | NEEDS_FIXUP | USES_CCODES, { 0, 0, 0x70, 0, 0, 0, 0, 0 }, "Jcc8", "!1c !0t" },
305 { kX86Jcc32, kJcc, IS_BINARY_OP | IS_BRANCH | NEEDS_FIXUP | USES_CCODES, { 0, 0, 0x0F, 0x80, 0, 0, 0, 0 }, "Jcc32", "!1c !0t" },
306 { kX86Jmp8, kJmp, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0xEB, 0, 0, 0, 0, 0 }, "Jmp8", "!0t" },
307 { kX86Jmp32, kJmp, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP, { 0, 0, 0xE9, 0, 0, 0, 0, 0 }, "Jmp32", "!0t" },
308 { kX86JmpR, kJmp, IS_UNARY_OP | IS_BRANCH | REG_USE0, { 0, 0, 0xFF, 0, 0, 4, 0, 0 }, "JmpR", "!0r" },
309 { kX86CallR, kCall, IS_UNARY_OP | IS_BRANCH | REG_USE0, { 0, 0, 0xE8, 0, 0, 0, 0, 0 }, "CallR", "!0r" },
310 { kX86CallM, kCall, IS_BINARY_OP | IS_BRANCH | IS_LOAD | REG_USE0, { 0, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallM", "[!0r+!1d]" },
311 { kX86CallA, kCall, IS_QUAD_OP | IS_BRANCH | IS_LOAD | REG_USE01, { 0, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallA", "[!0r+!1r<<!2d+!3d]" },
312 { kX86CallT, kCall, IS_UNARY_OP | IS_BRANCH | IS_LOAD, { THREAD_PREFIX, 0, 0xFF, 0, 0, 2, 0, 0 }, "CallT", "fs:[!0d]" },
313 { kX86Ret, kNullary,NO_OPERAND | IS_BRANCH, { 0, 0, 0xC3, 0, 0, 0, 0, 0 }, "Ret", "" },
Ian Rogers7caad772012-03-30 01:07:54 -0700314
jeffhaoe2962482012-06-28 11:29:57 -0700315 { kX86StartOfMethod, kMacro, IS_UNARY_OP | SETS_CCODES, { 0, 0, 0, 0, 0, 0, 0, 0 }, "StartOfMethod", "!0r" },
316 { kX86PcRelLoadRA, kPcRel, IS_LOAD | IS_QUIN_OP | REG_DEF0_USE12, { 0, 0, 0x8B, 0, 0, 0, 0, 0 }, "PcRelLoadRA", "!0r,[!1r+!2r<<!3d+!4p]" },
317 { 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 -0800318};
319
Ian Rogersb5d09b22012-03-06 22:14:17 -0800320static size_t computeSize(X86EncodingMap* entry, int displacement, bool has_sib) {
321 size_t size = 0;
322 if (entry->skeleton.prefix1 > 0) {
323 ++size;
324 if (entry->skeleton.prefix2 > 0) {
325 ++size;
Ian Rogersde797832012-03-06 10:18:10 -0800326 }
Ian Rogersde797832012-03-06 10:18:10 -0800327 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800328 ++size; // opcode
329 if (entry->skeleton.opcode == 0x0F) {
330 ++size;
331 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode1 == 0x3A) {
332 ++size;
333 }
334 }
335 ++size; // modrm
336 if (has_sib) {
337 ++size;
338 }
339 if (displacement != 0) {
340 if (entry->opcode != kX86Lea32RA) {
Ian Rogers7caad772012-03-30 01:07:54 -0700341 DCHECK_NE(entry->flags & (IS_LOAD | IS_STORE), 0) << entry->name;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800342 }
343 size += IS_SIMM8(displacement) ? 1 : 4;
344 }
345 size += entry->skeleton.immediate_bytes;
346 return size;
347}
348
349int oatGetInsnSize(LIR* lir) {
350 X86EncodingMap* entry = &EncodingMap[lir->opcode];
351 switch (entry->kind) {
352 case kData:
353 return 4; // 4 bytes of data
354 case kNop:
355 return lir->operands[0]; // length of nop is sole operand
356 case kNullary:
357 return 1; // 1 byte of opcode
358 case kReg: // lir operands - 0: reg
359 return computeSize(entry, 0, false);
360 case kMem: { // lir operands - 0: base, 1: disp
361 int base = lir->operands[0];
362 // SP requires a special extra SIB byte
363 return computeSize(entry, lir->operands[1], false) + (base == rSP ? 1 : 0);
364 }
365 case kArray: // lir operands - 0: base, 1: index, 2: scale, 3: disp
366 return computeSize(entry, lir->operands[3], true);
367 case kMemReg: { // lir operands - 0: base, 1: disp, 2: reg
368 int base = lir->operands[0];
369 // SP requires a special extra SIB byte
370 return computeSize(entry, lir->operands[1], false) + (base == rSP ? 1 : 0);
371 }
372 case kArrayReg: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
373 return computeSize(entry, lir->operands[3], true);
374 case kThreadReg: // lir operands - 0: disp, 1: reg
375 return computeSize(entry, lir->operands[0], false);
376 case kRegReg:
377 return computeSize(entry, 0, false);
378 case kRegMem: { // lir operands - 0: reg, 1: base, 2: disp
379 int base = lir->operands[1];
380 return computeSize(entry, lir->operands[2], false) + (base == rSP ? 1 : 0);
381 }
382 case kRegArray: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
383 return computeSize(entry, lir->operands[4], true);
384 case kRegThread: // lir operands - 0: reg, 1: disp
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700385 return computeSize(entry, 0x12345678, false); // displacement size is always 32bit
Ian Rogersb5d09b22012-03-06 22:14:17 -0800386 case kRegImm: { // lir operands - 0: reg, 1: immediate
Ian Rogersb41b33b2012-03-20 14:22:54 -0700387 size_t size = computeSize(entry, 0, false);
388 if (entry->skeleton.ax_opcode == 0) {
389 return size;
390 } else {
391 // AX opcodes don't require the modrm byte.
392 int reg = lir->operands[0];
393 return size - (reg == rAX ? 1 : 0);
394 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800395 }
396 case kMemImm: // lir operands - 0: base, 1: disp, 2: immediate
397 CHECK_NE(lir->operands[0], static_cast<int>(rSP)); // TODO: add extra SIB byte
398 return computeSize(entry, lir->operands[1], false);
399 case kArrayImm: // lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
400 return computeSize(entry, lir->operands[3], true);
401 case kThreadImm: // lir operands - 0: disp, 1: imm
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700402 return computeSize(entry, 0x12345678, false); // displacement size is always 32bit
Ian Rogersb5d09b22012-03-06 22:14:17 -0800403 case kRegRegImm: // lir operands - 0: reg, 1: reg, 2: imm
404 return computeSize(entry, 0, false);
405 case kRegMemImm: // lir operands - 0: reg, 1: base, 2: disp, 3: imm
406 CHECK_NE(lir->operands[1], static_cast<int>(rSP)); // TODO: add extra SIB byte
407 return computeSize(entry, lir->operands[2], false);
408 case kRegArrayImm: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp, 5: imm
409 return computeSize(entry, lir->operands[4], true);
410 case kMovRegImm: // lir operands - 0: reg, 1: immediate
411 return 1 + entry->skeleton.immediate_bytes;
412 case kShiftRegImm: // lir operands - 0: reg, 1: immediate
413 // Shift by immediate one has a shorter opcode.
414 return computeSize(entry, 0, false) - (lir->operands[1] == 1 ? 1 : 0);
415 case kShiftMemImm: // lir operands - 0: base, 1: disp, 2: immediate
416 CHECK_NE(lir->operands[0], static_cast<int>(rSP)); // TODO: add extra SIB byte
417 // Shift by immediate one has a shorter opcode.
418 return computeSize(entry, lir->operands[1], false) - (lir->operands[2] == 1 ? 1 : 0);
419 case kShiftArrayImm: // lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
420 // Shift by immediate one has a shorter opcode.
421 return computeSize(entry, lir->operands[3], true) - (lir->operands[4] == 1 ? 1 : 0);
422 case kShiftRegCl:
423 return computeSize(entry, 0, false);
424 case kShiftMemCl: // lir operands - 0: base, 1: disp, 2: cl
425 CHECK_NE(lir->operands[0], static_cast<int>(rSP)); // TODO: add extra SIB byte
426 return computeSize(entry, lir->operands[1], false);
427 case kShiftArrayCl: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
428 return computeSize(entry, lir->operands[3], true);
429 case kRegCond: // lir operands - 0: reg, 1: cond
430 return computeSize(entry, 0, false);
431 case kMemCond: // lir operands - 0: base, 1: disp, 2: cond
432 CHECK_NE(lir->operands[0], static_cast<int>(rSP)); // TODO: add extra SIB byte
433 return computeSize(entry, lir->operands[1], false);
434 case kArrayCond: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: cond
435 return computeSize(entry, lir->operands[3], true);
Ian Rogersb41b33b2012-03-20 14:22:54 -0700436 case kJcc:
437 if (lir->opcode == kX86Jcc8) {
438 return 2; // opcode + rel8
439 } else {
440 DCHECK(lir->opcode == kX86Jcc32);
441 return 6; // 2 byte opcode + rel32
442 }
443 case kJmp:
444 if (lir->opcode == kX86Jmp8) {
445 return 2; // opcode + rel8
Ian Rogers7caad772012-03-30 01:07:54 -0700446 } else if (lir->opcode == kX86Jmp32) {
Ian Rogersb41b33b2012-03-20 14:22:54 -0700447 return 5; // opcode + rel32
Ian Rogers7caad772012-03-30 01:07:54 -0700448 } else {
449 DCHECK(lir->opcode == kX86JmpR);
450 return 2; // opcode + modrm
Ian Rogersb41b33b2012-03-20 14:22:54 -0700451 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800452 case kCall:
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700453 switch (lir->opcode) {
Ian Rogersb5d09b22012-03-06 22:14:17 -0800454 case kX86CallR: return 2; // opcode modrm
455 case kX86CallM: // lir operands - 0: base, 1: disp
456 return computeSize(entry, lir->operands[1], false);
457 case kX86CallA: // lir operands - 0: base, 1: index, 2: scale, 3: disp
458 return computeSize(entry, lir->operands[3], true);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700459 case kX86CallT: // lir operands - 0: disp
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700460 return computeSize(entry, 0x12345678, false); // displacement size is always 32bit
Ian Rogersb5d09b22012-03-06 22:14:17 -0800461 default:
462 break;
463 }
464 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700465 case kPcRel:
466 if (entry->opcode == kX86PcRelLoadRA) {
467 // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
468 return computeSize(entry, 0x12345678, true);
469 } else {
470 DCHECK(entry->opcode == kX86PcRelAdr);
471 return 5; // opcode with reg + 4 byte immediate
472 }
473 case kMacro:
474 DCHECK_EQ(lir->opcode, static_cast<int>(kX86StartOfMethod));
475 return 5 /* call opcode + 4 byte displacement */ + 1 /* pop reg */ +
476 computeSize(&EncodingMap[kX86Sub32RI], 0, false) -
477 (lir->operands[0] == rAX ? 1 : 0); // shorter ax encoding
Ian Rogersb5d09b22012-03-06 22:14:17 -0800478 default:
479 break;
480 }
481 UNIMPLEMENTED(FATAL) << "Unimplemented size encoding for: " << entry->name;
Ian Rogersde797832012-03-06 10:18:10 -0800482 return 0;
483}
buzbeee88dfbf2012-03-05 11:19:57 -0800484
Ian Rogersb5d09b22012-03-06 22:14:17 -0800485static uint8_t modrmForDisp(int disp) {
486 if (disp == 0) {
487 return 0;
488 } else if (IS_SIMM8(disp)) {
489 return 1;
490 } else {
491 return 2;
492 }
493}
494
495static void emitDisp(CompilationUnit* cUnit, int disp) {
496 if (disp == 0) {
497 return;
498 } else if (IS_SIMM8(disp)) {
499 cUnit->codeBuffer.push_back(disp & 0xFF);
500 } else {
501 cUnit->codeBuffer.push_back(disp & 0xFF);
502 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
503 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
504 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
505 }
506}
507
508static void emitOpReg(CompilationUnit* cUnit, const X86EncodingMap* entry, uint8_t reg) {
509 if (entry->skeleton.prefix1 != 0) {
510 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
511 if (entry->skeleton.prefix2 != 0) {
512 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
513 }
514 } else {
515 DCHECK_EQ(0, entry->skeleton.prefix2);
516 }
517 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
518 if (entry->skeleton.opcode == 0x0F) {
519 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
520 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
521 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
522 } else {
523 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
524 }
525 } else {
526 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
527 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
528 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700529 if (FPREG(reg)) {
530 reg = reg & FP_REG_MASK;
531 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800532 DCHECK_LT(reg, 8);
533 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
534 cUnit->codeBuffer.push_back(modrm);
535 DCHECK_EQ(0, entry->skeleton.ax_opcode);
536 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
537}
538
539static void emitOpMem(CompilationUnit* cUnit, const X86EncodingMap* entry, uint8_t base, int disp) {
540 if (entry->skeleton.prefix1 != 0) {
541 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
542 if (entry->skeleton.prefix2 != 0) {
543 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
544 }
545 } else {
546 DCHECK_EQ(0, entry->skeleton.prefix2);
547 }
548 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
549 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
550 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
551 DCHECK_LT(entry->skeleton.modrm_opcode, 8);
552 DCHECK_LT(base, 8);
553 uint8_t modrm = (modrmForDisp(disp) << 6) | (entry->skeleton.modrm_opcode << 3) | base;
554 cUnit->codeBuffer.push_back(modrm);
555 emitDisp(cUnit, disp);
556 DCHECK_EQ(0, entry->skeleton.ax_opcode);
557 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
558}
559
560static void emitMemReg(CompilationUnit* cUnit, const X86EncodingMap* entry,
561 uint8_t base, int disp, uint8_t reg) {
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 if (entry->skeleton.opcode == 0x0F) {
572 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
573 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
574 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
575 } else {
576 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
577 }
578 } else {
579 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
580 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
581 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700582 if (FPREG(reg)) {
583 reg = reg & FP_REG_MASK;
584 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800585 DCHECK_LT(reg, 8);
586 DCHECK_LT(base, 8);
587 uint8_t modrm = (modrmForDisp(disp) << 6) | (reg << 3) | base;
588 cUnit->codeBuffer.push_back(modrm);
589 if (base == rSP) {
590 // Special SIB for SP base
591 cUnit->codeBuffer.push_back(0 << 6 | (rSP << 3) | rSP);
592 }
593 emitDisp(cUnit, disp);
594 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
595 DCHECK_EQ(0, entry->skeleton.ax_opcode);
596 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
597}
598
599static void emitRegMem(CompilationUnit* cUnit, const X86EncodingMap* entry,
600 uint8_t reg, uint8_t base, int disp) {
601 // Opcode will flip operands.
602 emitMemReg(cUnit, entry, base, disp, reg);
603}
604
605static void emitRegArray(CompilationUnit* cUnit, const X86EncodingMap* entry, uint8_t reg,
606 uint8_t base, uint8_t index, int scale, int disp) {
607 if (entry->skeleton.prefix1 != 0) {
608 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
609 if (entry->skeleton.prefix2 != 0) {
610 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
611 }
612 } else {
613 DCHECK_EQ(0, entry->skeleton.prefix2);
614 }
615 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
616 if (entry->skeleton.opcode == 0x0F) {
617 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
618 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
619 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
620 } else {
621 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
622 }
623 } else {
624 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
625 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
626 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700627 if (FPREG(reg)) {
628 reg = reg & FP_REG_MASK;
629 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800630 DCHECK_LT(reg, 8);
631 uint8_t modrm = (modrmForDisp(disp) << 6) | (reg << 3) | rSP;
632 cUnit->codeBuffer.push_back(modrm);
633 DCHECK_LT(scale, 4);
634 DCHECK_LT(index, 8);
635 DCHECK_LT(base, 8);
636 uint8_t sib = (scale << 6) | (index << 3) | base;
637 cUnit->codeBuffer.push_back(sib);
638 emitDisp(cUnit, disp);
639 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
640 DCHECK_EQ(0, entry->skeleton.ax_opcode);
641 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
642}
643
Ian Rogersb41b33b2012-03-20 14:22:54 -0700644static void emitArrayReg(CompilationUnit* cUnit, const X86EncodingMap* entry,
645 uint8_t base, uint8_t index, int scale, int disp, uint8_t reg) {
646 // Opcode will flip operands.
647 emitRegArray(cUnit, entry, reg, base, index, scale, disp);
648}
649
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700650static void emitRegThread(CompilationUnit* cUnit, const X86EncodingMap* entry,
651 uint8_t reg, int disp) {
652 DCHECK_NE(entry->skeleton.prefix1, 0);
653 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
654 if (entry->skeleton.prefix2 != 0) {
655 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
656 }
657 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
658 if (entry->skeleton.opcode == 0x0F) {
659 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
660 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
661 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
662 } else {
663 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
664 }
665 } else {
666 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
667 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
668 }
669 if (FPREG(reg)) {
670 reg = reg & FP_REG_MASK;
671 }
672 DCHECK_LT(reg, 8);
673 uint8_t modrm = (0 << 6) | (reg << 3) | rBP;
674 cUnit->codeBuffer.push_back(modrm);
675 cUnit->codeBuffer.push_back(disp & 0xFF);
676 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
677 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
678 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
679 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
680 DCHECK_EQ(0, entry->skeleton.ax_opcode);
681 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
682}
683
Ian Rogersb5d09b22012-03-06 22:14:17 -0800684static void emitRegReg(CompilationUnit* cUnit, const X86EncodingMap* entry,
685 uint8_t reg1, uint8_t reg2) {
686 if (entry->skeleton.prefix1 != 0) {
687 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
688 if (entry->skeleton.prefix2 != 0) {
689 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
690 }
691 } else {
692 DCHECK_EQ(0, entry->skeleton.prefix2);
693 }
694 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
695 if (entry->skeleton.opcode == 0x0F) {
696 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
697 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
698 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
699 } else {
700 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
701 }
702 } else {
703 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
704 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
705 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700706 if (FPREG(reg1)) {
707 reg1 = reg1 & FP_REG_MASK;
708 }
709 if (FPREG(reg2)) {
710 reg2 = reg2 & FP_REG_MASK;
711 }
712 DCHECK_LT(reg1, 8);
713 DCHECK_LT(reg2, 8);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800714 uint8_t modrm = (3 << 6) | (reg1 << 3) | reg2;
715 cUnit->codeBuffer.push_back(modrm);
716 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
717 DCHECK_EQ(0, entry->skeleton.ax_opcode);
718 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
719}
720
Elliott Hughes225ae522012-04-16 20:21:45 -0700721static void emitRegRegImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
722 uint8_t reg1, uint8_t reg2, int32_t imm) {
723 if (entry->skeleton.prefix1 != 0) {
724 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
725 if (entry->skeleton.prefix2 != 0) {
726 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
727 }
728 } else {
729 DCHECK_EQ(0, entry->skeleton.prefix2);
730 }
731 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
732 if (entry->skeleton.opcode == 0x0F) {
733 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
734 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
735 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
736 } else {
737 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
738 }
739 } else {
740 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
741 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
742 }
743 if (FPREG(reg1)) {
744 reg1 = reg1 & FP_REG_MASK;
745 }
746 if (FPREG(reg2)) {
747 reg2 = reg2 & FP_REG_MASK;
748 }
749 DCHECK_LT(reg1, 8);
750 DCHECK_LT(reg2, 8);
751 uint8_t modrm = (3 << 6) | (reg1 << 3) | reg2;
752 cUnit->codeBuffer.push_back(modrm);
753 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
754 DCHECK_EQ(0, entry->skeleton.ax_opcode);
755 switch (entry->skeleton.immediate_bytes) {
756 case 1:
757 DCHECK(IS_SIMM8(imm));
758 cUnit->codeBuffer.push_back(imm & 0xFF);
759 break;
760 case 2:
761 DCHECK(IS_SIMM16(imm));
762 cUnit->codeBuffer.push_back(imm & 0xFF);
763 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
764 break;
765 case 4:
766 cUnit->codeBuffer.push_back(imm & 0xFF);
767 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
768 cUnit->codeBuffer.push_back((imm >> 16) & 0xFF);
769 cUnit->codeBuffer.push_back((imm >> 24) & 0xFF);
770 break;
771 default:
772 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
773 << ") for instruction: " << entry->name;
774 break;
775 }
776}
777
Ian Rogersb5d09b22012-03-06 22:14:17 -0800778static void emitRegImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
779 uint8_t reg, int imm) {
780 if (entry->skeleton.prefix1 != 0) {
781 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
782 if (entry->skeleton.prefix2 != 0) {
783 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
784 }
785 } else {
786 DCHECK_EQ(0, entry->skeleton.prefix2);
787 }
788 if (reg == rAX && entry->skeleton.ax_opcode != 0) {
789 cUnit->codeBuffer.push_back(entry->skeleton.ax_opcode);
790 } else {
791 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
792 if (entry->skeleton.opcode == 0x0F) {
793 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
794 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
795 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
796 } else {
797 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
798 }
799 } else {
800 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
801 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
802 }
803 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
804 cUnit->codeBuffer.push_back(modrm);
805 }
806 switch (entry->skeleton.immediate_bytes) {
807 case 1:
808 DCHECK(IS_SIMM8(imm));
809 cUnit->codeBuffer.push_back(imm & 0xFF);
810 break;
811 case 2:
812 DCHECK(IS_SIMM16(imm));
813 cUnit->codeBuffer.push_back(imm & 0xFF);
814 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
815 break;
816 case 4:
817 cUnit->codeBuffer.push_back(imm & 0xFF);
818 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
819 cUnit->codeBuffer.push_back((imm >> 16) & 0xFF);
820 cUnit->codeBuffer.push_back((imm >> 24) & 0xFF);
821 break;
822 default:
823 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
824 << ") for instruction: " << entry->name;
825 break;
826 }
827}
828
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700829static void emitThreadImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
830 int disp, int imm) {
831 if (entry->skeleton.prefix1 != 0) {
832 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
833 if (entry->skeleton.prefix2 != 0) {
834 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
835 }
836 } else {
837 DCHECK_EQ(0, entry->skeleton.prefix2);
838 }
839 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
840 if (entry->skeleton.opcode == 0x0F) {
841 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
842 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
843 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
844 } else {
845 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
846 }
847 } else {
848 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
849 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
850 }
851 uint8_t modrm = (0 << 6) | (entry->skeleton.modrm_opcode << 3) | rBP;
852 cUnit->codeBuffer.push_back(modrm);
853 cUnit->codeBuffer.push_back(disp & 0xFF);
854 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
855 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
856 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
857 switch (entry->skeleton.immediate_bytes) {
858 case 1:
859 DCHECK(IS_SIMM8(imm));
860 cUnit->codeBuffer.push_back(imm & 0xFF);
861 break;
862 case 2:
863 DCHECK(IS_SIMM16(imm));
864 cUnit->codeBuffer.push_back(imm & 0xFF);
865 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
866 break;
867 case 4:
868 cUnit->codeBuffer.push_back(imm & 0xFF);
869 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
870 cUnit->codeBuffer.push_back((imm >> 16) & 0xFF);
871 cUnit->codeBuffer.push_back((imm >> 24) & 0xFF);
872 break;
873 default:
874 LOG(FATAL) << "Unexpected immediate bytes (" << entry->skeleton.immediate_bytes
875 << ") for instruction: " << entry->name;
876 break;
877 }
878 DCHECK_EQ(entry->skeleton.ax_opcode, 0);
879}
880
881static void emitMovRegImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
882 uint8_t reg, int imm) {
883 DCHECK_LT(reg, 8);
884 cUnit->codeBuffer.push_back(0xB8 + reg);
885 cUnit->codeBuffer.push_back(imm & 0xFF);
886 cUnit->codeBuffer.push_back((imm >> 8) & 0xFF);
887 cUnit->codeBuffer.push_back((imm >> 16) & 0xFF);
888 cUnit->codeBuffer.push_back((imm >> 24) & 0xFF);
889}
890
Ian Rogersb41b33b2012-03-20 14:22:54 -0700891static void emitShiftRegImm(CompilationUnit* cUnit, const X86EncodingMap* entry,
Ian Rogers7caad772012-03-30 01:07:54 -0700892 uint8_t reg, int imm) {
Ian Rogersb41b33b2012-03-20 14:22:54 -0700893 if (entry->skeleton.prefix1 != 0) {
894 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
895 if (entry->skeleton.prefix2 != 0) {
896 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
897 }
898 } else {
899 DCHECK_EQ(0, entry->skeleton.prefix2);
900 }
901 if (imm != 1) {
902 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
903 } else {
904 // Shorter encoding for 1 bit shift
905 cUnit->codeBuffer.push_back(entry->skeleton.ax_opcode);
906 }
907 if (entry->skeleton.opcode == 0x0F) {
908 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
909 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
910 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
911 } else {
912 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
913 }
914 } else {
915 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
916 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
917 }
918 DCHECK_LT(reg, 8);
Ian Rogers7caad772012-03-30 01:07:54 -0700919 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
Ian Rogersb41b33b2012-03-20 14:22:54 -0700920 cUnit->codeBuffer.push_back(modrm);
921 if (imm != 1) {
922 DCHECK_EQ(entry->skeleton.immediate_bytes, 1);
923 DCHECK(IS_SIMM8(imm));
924 cUnit->codeBuffer.push_back(imm & 0xFF);
925 }
926}
927
Ian Rogers7caad772012-03-30 01:07:54 -0700928static void emitShiftRegCl(CompilationUnit* cUnit, const X86EncodingMap* entry,
929 uint8_t reg, uint8_t cl) {
930 DCHECK_EQ(cl, static_cast<uint8_t>(rCX));
931 if (entry->skeleton.prefix1 != 0) {
932 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
933 if (entry->skeleton.prefix2 != 0) {
934 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
935 }
936 } else {
937 DCHECK_EQ(0, entry->skeleton.prefix2);
938 }
939 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
940 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
941 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
942 DCHECK_LT(reg, 8);
943 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
944 cUnit->codeBuffer.push_back(modrm);
945 DCHECK_EQ(0, entry->skeleton.ax_opcode);
946 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
947}
948
949static void emitRegCond(CompilationUnit* cUnit, const X86EncodingMap* entry,
950 uint8_t reg, uint8_t condition) {
951 if (entry->skeleton.prefix1 != 0) {
952 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
953 if (entry->skeleton.prefix2 != 0) {
954 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
955 }
956 } else {
957 DCHECK_EQ(0, entry->skeleton.prefix2);
958 }
959 DCHECK_EQ(0, entry->skeleton.ax_opcode);
960 DCHECK_EQ(0x0F, entry->skeleton.opcode);
961 cUnit->codeBuffer.push_back(0x0F);
962 DCHECK_EQ(0x90, entry->skeleton.extra_opcode1);
963 cUnit->codeBuffer.push_back(0x90 | condition);
964 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
965 DCHECK_LT(reg, 8);
966 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
967 cUnit->codeBuffer.push_back(modrm);
968 DCHECK_EQ(entry->skeleton.immediate_bytes, 0);
969}
970
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700971static void emitJmp(CompilationUnit* cUnit, const X86EncodingMap* entry, int rel) {
Ian Rogersb41b33b2012-03-20 14:22:54 -0700972 if (entry->opcode == kX86Jmp8) {
973 DCHECK(IS_SIMM8(rel));
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700974 cUnit->codeBuffer.push_back(0xEB);
975 cUnit->codeBuffer.push_back(rel & 0xFF);
Ian Rogers7caad772012-03-30 01:07:54 -0700976 } else if (entry->opcode == kX86Jmp32) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700977 cUnit->codeBuffer.push_back(0xE9);
978 cUnit->codeBuffer.push_back(rel & 0xFF);
979 cUnit->codeBuffer.push_back((rel >> 8) & 0xFF);
980 cUnit->codeBuffer.push_back((rel >> 16) & 0xFF);
981 cUnit->codeBuffer.push_back((rel >> 24) & 0xFF);
Ian Rogers7caad772012-03-30 01:07:54 -0700982 } else {
983 DCHECK(entry->opcode == kX86JmpR);
984 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
985 uint8_t reg = static_cast<uint8_t>(rel);
986 DCHECK_LT(reg, 8);
987 uint8_t modrm = (3 << 6) | (entry->skeleton.modrm_opcode << 3) | reg;
988 cUnit->codeBuffer.push_back(modrm);
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700989 }
990}
991
992static void emitJcc(CompilationUnit* cUnit, const X86EncodingMap* entry,
993 int rel, uint8_t cc) {
994 DCHECK_LT(cc, 16);
Ian Rogersb41b33b2012-03-20 14:22:54 -0700995 if (entry->opcode == kX86Jcc8) {
996 DCHECK(IS_SIMM8(rel));
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700997 cUnit->codeBuffer.push_back(0x70 | cc);
998 cUnit->codeBuffer.push_back(rel & 0xFF);
999 } else {
Ian Rogersb41b33b2012-03-20 14:22:54 -07001000 DCHECK(entry->opcode == kX86Jcc32);
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001001 cUnit->codeBuffer.push_back(0x0F);
1002 cUnit->codeBuffer.push_back(0x80 | cc);
1003 cUnit->codeBuffer.push_back(rel & 0xFF);
1004 cUnit->codeBuffer.push_back((rel >> 8) & 0xFF);
1005 cUnit->codeBuffer.push_back((rel >> 16) & 0xFF);
1006 cUnit->codeBuffer.push_back((rel >> 24) & 0xFF);
1007 }
1008}
1009
1010static void emitCallMem(CompilationUnit* cUnit, const X86EncodingMap* entry,
1011 uint8_t base, int disp) {
1012 if (entry->skeleton.prefix1 != 0) {
1013 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
1014 if (entry->skeleton.prefix2 != 0) {
1015 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
1016 }
1017 } else {
1018 DCHECK_EQ(0, entry->skeleton.prefix2);
1019 }
1020 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
1021 if (entry->skeleton.opcode == 0x0F) {
1022 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
1023 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
1024 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
1025 } else {
1026 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1027 }
1028 } else {
1029 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1030 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1031 }
1032 uint8_t modrm = (modrmForDisp(disp) << 6) | (entry->skeleton.modrm_opcode << 3) | base;
1033 cUnit->codeBuffer.push_back(modrm);
1034 if (base == rSP) {
1035 // Special SIB for SP base
1036 cUnit->codeBuffer.push_back(0 << 6 | (rSP << 3) | rSP);
1037 }
1038 emitDisp(cUnit, disp);
1039 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1040 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1041}
1042
1043static void emitCallThread(CompilationUnit* cUnit, const X86EncodingMap* entry, int disp) {
1044 DCHECK_NE(entry->skeleton.prefix1, 0);
1045 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
1046 if (entry->skeleton.prefix2 != 0) {
1047 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
1048 }
1049 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
1050 if (entry->skeleton.opcode == 0x0F) {
1051 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
1052 if (entry->skeleton.extra_opcode1 == 0x38 || entry->skeleton.extra_opcode2 == 0x3A) {
1053 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
1054 } else {
1055 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1056 }
1057 } else {
1058 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1059 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1060 }
1061 uint8_t modrm = (0 << 6) | (entry->skeleton.modrm_opcode << 3) | rBP;
1062 cUnit->codeBuffer.push_back(modrm);
1063 cUnit->codeBuffer.push_back(disp & 0xFF);
1064 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
1065 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
1066 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
1067 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1068 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1069}
1070
Ian Rogers7caad772012-03-30 01:07:54 -07001071static void emitPcRel(CompilationUnit* cUnit, const X86EncodingMap* entry, uint8_t reg,
1072 int base_or_table, uint8_t index, int scale, int table_or_disp) {
1073 int disp;
1074 if (entry->opcode == kX86PcRelLoadRA) {
1075 SwitchTable *tabRec = (SwitchTable*)table_or_disp;
1076 disp = tabRec->offset;
1077 } else {
1078 DCHECK(entry->opcode == kX86PcRelAdr);
1079 FillArrayData *tabRec = (FillArrayData *)base_or_table;
1080 disp = tabRec->offset;
1081 }
1082 if (entry->skeleton.prefix1 != 0) {
1083 cUnit->codeBuffer.push_back(entry->skeleton.prefix1);
1084 if (entry->skeleton.prefix2 != 0) {
1085 cUnit->codeBuffer.push_back(entry->skeleton.prefix2);
1086 }
1087 } else {
1088 DCHECK_EQ(0, entry->skeleton.prefix2);
1089 }
1090 if (FPREG(reg)) {
1091 reg = reg & FP_REG_MASK;
1092 }
1093 DCHECK_LT(reg, 8);
1094 if (entry->opcode == kX86PcRelLoadRA) {
1095 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
1096 DCHECK_EQ(0, entry->skeleton.extra_opcode1);
1097 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1098 uint8_t modrm = (2 << 6) | (reg << 3) | rSP;
1099 cUnit->codeBuffer.push_back(modrm);
1100 DCHECK_LT(scale, 4);
1101 DCHECK_LT(index, 8);
1102 DCHECK_LT(base_or_table, 8);
1103 uint8_t base = static_cast<uint8_t>(base_or_table);
1104 uint8_t sib = (scale << 6) | (index << 3) | base;
1105 cUnit->codeBuffer.push_back(sib);
1106 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1107 } else {
1108 cUnit->codeBuffer.push_back(entry->skeleton.opcode + reg);
1109 }
1110 cUnit->codeBuffer.push_back(disp & 0xFF);
1111 cUnit->codeBuffer.push_back((disp >> 8) & 0xFF);
1112 cUnit->codeBuffer.push_back((disp >> 16) & 0xFF);
1113 cUnit->codeBuffer.push_back((disp >> 24) & 0xFF);
1114 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1115 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1116}
1117
1118static void emitMacro(CompilationUnit* cUnit, const X86EncodingMap* entry,
1119 uint8_t reg, int offset) {
1120 DCHECK(entry->opcode == kX86StartOfMethod) << entry->name;
1121 cUnit->codeBuffer.push_back(0xE8); // call +0
1122 cUnit->codeBuffer.push_back(0);
1123 cUnit->codeBuffer.push_back(0);
1124 cUnit->codeBuffer.push_back(0);
1125 cUnit->codeBuffer.push_back(0);
1126
1127 DCHECK_LT(reg, 8);
1128 cUnit->codeBuffer.push_back(0x58 + reg); // pop reg
1129
1130 emitRegImm(cUnit, &EncodingMap[kX86Sub32RI], reg, offset + 5 /* size of call +0 */);
1131}
1132
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001133void emitUnimplemented(CompilationUnit* cUnit, const X86EncodingMap* entry, LIR* lir) {
Elliott Hughes225ae522012-04-16 20:21:45 -07001134 UNIMPLEMENTED(WARNING) << "encoding kind for " << entry->name << " " << buildInsnString(entry->fmt, lir, 0);
Ian Rogers141b0c72012-03-15 18:18:52 -07001135 for (int i = 0; i < oatGetInsnSize(lir); ++i) {
1136 cUnit->codeBuffer.push_back(0xCC); // push breakpoint instruction - int 3
1137 }
1138}
1139
buzbeee88dfbf2012-03-05 11:19:57 -08001140/*
1141 * Assemble the LIR into binary instruction format. Note that we may
1142 * discover that pc-relative displacements may not fit the selected
1143 * instruction. In those cases we will try to substitute a new code
1144 * sequence or request that the trace be shortened and retried.
1145 */
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001146AssemblerStatus oatAssembleInstructions(CompilationUnit *cUnit, intptr_t startAddr) {
Ian Rogersb5d09b22012-03-06 22:14:17 -08001147 LIR *lir;
1148 AssemblerStatus res = kSuccess; // Assume success
buzbeee88dfbf2012-03-05 11:19:57 -08001149
Ian Rogers141d6222012-04-05 12:23:06 -07001150 const bool kVerbosePcFixup = false;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001151 for (lir = (LIR *) cUnit->firstLIRInsn; lir; lir = NEXT_LIR(lir)) {
1152 if (lir->opcode < 0) {
1153 continue;
buzbeee88dfbf2012-03-05 11:19:57 -08001154 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001155
Ian Rogersb5d09b22012-03-06 22:14:17 -08001156 if (lir->flags.isNop) {
1157 continue;
1158 }
1159
1160 if (lir->flags.pcRelFixup) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001161 switch (lir->opcode) {
Ian Rogersb41b33b2012-03-20 14:22:54 -07001162 case kX86Jcc8: {
1163 LIR *targetLIR = lir->target;
1164 DCHECK(targetLIR != NULL);
1165 int delta = 0;
1166 intptr_t pc;
1167 if (IS_SIMM8(lir->operands[0])) {
1168 pc = lir->offset + 2 /* opcode + rel8 */;
1169 } else {
1170 pc = lir->offset + 6 /* 2 byte opcode + rel32 */;
1171 }
1172 intptr_t target = targetLIR->offset;
1173 delta = target - pc;
1174 if (IS_SIMM8(delta) != IS_SIMM8(lir->operands[0])) {
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001175 if (kVerbosePcFixup) {
1176 LOG(INFO) << "Retry for JCC growth at " << lir->offset
1177 << " delta: " << delta << " old delta: " << lir->operands[0];
1178 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001179 lir->opcode = kX86Jcc32;
1180 oatSetupResourceMasks(lir);
1181 res = kRetryAll;
1182 }
Ian Rogers7caad772012-03-30 01:07:54 -07001183 if (kVerbosePcFixup) {
1184 LOG(INFO) << "Source:";
1185 oatDumpLIRInsn(cUnit, lir, 0);
1186 LOG(INFO) << "Target:";
1187 oatDumpLIRInsn(cUnit, targetLIR, 0);
1188 LOG(INFO) << "Delta " << delta;
1189 }
1190 lir->operands[0] = delta;
1191 break;
1192 }
1193 case kX86Jcc32: {
1194 LIR *targetLIR = lir->target;
1195 DCHECK(targetLIR != NULL);
1196 intptr_t pc = lir->offset + 6 /* 2 byte opcode + rel32 */;
1197 intptr_t target = targetLIR->offset;
1198 int delta = target - pc;
1199 if (kVerbosePcFixup) {
1200 LOG(INFO) << "Source:";
1201 oatDumpLIRInsn(cUnit, lir, 0);
1202 LOG(INFO) << "Target:";
1203 oatDumpLIRInsn(cUnit, targetLIR, 0);
1204 LOG(INFO) << "Delta " << delta;
1205 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001206 lir->operands[0] = delta;
1207 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001208 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001209 case kX86Jmp8: {
1210 LIR *targetLIR = lir->target;
1211 DCHECK(targetLIR != NULL);
1212 int delta = 0;
1213 intptr_t pc;
1214 if (IS_SIMM8(lir->operands[0])) {
1215 pc = lir->offset + 2 /* opcode + rel8 */;
1216 } else {
1217 pc = lir->offset + 5 /* opcode + rel32 */;
1218 }
1219 intptr_t target = targetLIR->offset;
1220 delta = target - pc;
jeffhaoe2962482012-06-28 11:29:57 -07001221 if (!(cUnit->disableOpt & (1 << kSafeOptimizations)) && delta == 0) {
Ian Rogersb41b33b2012-03-20 14:22:54 -07001222 // Useless branch
1223 lir->flags.isNop = true;
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001224 if (kVerbosePcFixup) {
1225 LOG(INFO) << "Retry for useless branch at " << lir->offset;
1226 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001227 res = kRetryAll;
1228 } else if (IS_SIMM8(delta) != IS_SIMM8(lir->operands[0])) {
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001229 if (kVerbosePcFixup) {
1230 LOG(INFO) << "Retry for JMP growth at " << lir->offset;
1231 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001232 lir->opcode = kX86Jmp32;
1233 oatSetupResourceMasks(lir);
1234 res = kRetryAll;
1235 }
1236 lir->operands[0] = delta;
1237 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001238 }
Ian Rogers7caad772012-03-30 01:07:54 -07001239 case kX86Jmp32: {
1240 LIR *targetLIR = lir->target;
1241 DCHECK(targetLIR != NULL);
1242 intptr_t pc = lir->offset + 5 /* opcode + rel32 */;
1243 intptr_t target = targetLIR->offset;
1244 int delta = target - pc;
1245 lir->operands[0] = delta;
1246 break;
1247 }
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001248 default:
1249 break;
1250 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001251 }
1252
1253 /*
1254 * If one of the pc-relative instructions expanded we'll have
1255 * to make another pass. Don't bother to fully assemble the
1256 * instruction.
1257 */
1258 if (res != kSuccess) {
1259 continue;
1260 }
Ian Rogers7caad772012-03-30 01:07:54 -07001261 CHECK_EQ(static_cast<size_t>(lir->offset), cUnit->codeBuffer.size());
Ian Rogersb5d09b22012-03-06 22:14:17 -08001262 const X86EncodingMap *entry = &EncodingMap[lir->opcode];
Ian Rogers141b0c72012-03-15 18:18:52 -07001263 size_t starting_cbuf_size = cUnit->codeBuffer.size();
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001264 switch (entry->kind) {
Ian Rogersb5d09b22012-03-06 22:14:17 -08001265 case kData: // 4 bytes of data
1266 cUnit->codeBuffer.push_back(lir->operands[0]);
1267 break;
1268 case kNullary: // 1 byte of opcode
1269 DCHECK_EQ(0, entry->skeleton.prefix1);
1270 DCHECK_EQ(0, entry->skeleton.prefix2);
1271 cUnit->codeBuffer.push_back(entry->skeleton.opcode);
Ian Rogersc6f3bb82012-03-21 20:40:33 -07001272 if (entry->skeleton.extra_opcode1 != 0) {
1273 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode1);
1274 if (entry->skeleton.extra_opcode2 != 0) {
1275 cUnit->codeBuffer.push_back(entry->skeleton.extra_opcode2);
1276 }
1277 } else {
1278 DCHECK_EQ(0, entry->skeleton.extra_opcode2);
1279 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001280 DCHECK_EQ(0, entry->skeleton.modrm_opcode);
1281 DCHECK_EQ(0, entry->skeleton.ax_opcode);
1282 DCHECK_EQ(0, entry->skeleton.immediate_bytes);
1283 break;
1284 case kReg: // lir operands - 0: reg
1285 emitOpReg(cUnit, entry, lir->operands[0]);
1286 break;
1287 case kMem: // lir operands - 0: base, 1: disp
1288 emitOpMem(cUnit, entry, lir->operands[0], lir->operands[1]);
1289 break;
1290 case kMemReg: // lir operands - 0: base, 1: disp, 2: reg
1291 emitMemReg(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1292 break;
Ian Rogersb41b33b2012-03-20 14:22:54 -07001293 case kArrayReg: // lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
1294 emitArrayReg(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2],
1295 lir->operands[3], lir->operands[4]);
1296 break;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001297 case kRegMem: // lir operands - 0: reg, 1: base, 2: disp
1298 emitRegMem(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1299 break;
1300 case kRegArray: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
1301 emitRegArray(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2],
1302 lir->operands[3], lir->operands[4]);
1303 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001304 case kRegThread: // lir operands - 0: reg, 1: disp
1305 emitRegThread(cUnit, entry, lir->operands[0], lir->operands[1]);
1306 break;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001307 case kRegReg: // lir operands - 0: reg1, 1: reg2
1308 emitRegReg(cUnit, entry, lir->operands[0], lir->operands[1]);
1309 break;
Elliott Hughes225ae522012-04-16 20:21:45 -07001310 case kRegRegImm:
1311 emitRegRegImm(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2]);
1312 break;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001313 case kRegImm: // lir operands - 0: reg, 1: immediate
1314 emitRegImm(cUnit, entry, lir->operands[0], lir->operands[1]);
1315 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001316 case kThreadImm: // lir operands - 0: disp, 1: immediate
1317 emitThreadImm(cUnit, entry, lir->operands[0], lir->operands[1]);
1318 break;
1319 case kMovRegImm: // lir operands - 0: reg, 1: immediate
1320 emitMovRegImm(cUnit, entry, lir->operands[0], lir->operands[1]);
1321 break;
Ian Rogersb41b33b2012-03-20 14:22:54 -07001322 case kShiftRegImm: // lir operands - 0: reg, 1: immediate
1323 emitShiftRegImm(cUnit, entry, lir->operands[0], lir->operands[1]);
1324 break;
Ian Rogers7caad772012-03-30 01:07:54 -07001325 case kShiftRegCl: // lir operands - 0: reg, 1: cl
1326 emitShiftRegCl(cUnit, entry, lir->operands[0], lir->operands[1]);
1327 break;
1328 case kRegCond: // lir operands - 0: reg, 1: condition
1329 emitRegCond(cUnit, entry, lir->operands[0], lir->operands[1]);
1330 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001331 case kJmp: // lir operands - 0: rel
1332 emitJmp(cUnit, entry, lir->operands[0]);
1333 break;
1334 case kJcc: // lir operands - 0: rel, 1: CC, target assigned
1335 emitJcc(cUnit, entry, lir->operands[0], lir->operands[1]);
1336 break;
1337 case kCall:
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001338 switch (entry->opcode) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -07001339 case kX86CallM: // lir operands - 0: base, 1: disp
1340 emitCallMem(cUnit, entry, lir->operands[0], lir->operands[1]);
1341 break;
1342 case kX86CallT: // lir operands - 0: disp
1343 emitCallThread(cUnit, entry, lir->operands[0]);
1344 break;
1345 default:
1346 emitUnimplemented(cUnit, entry, lir);
1347 break;
1348 }
1349 break;
Ian Rogers7caad772012-03-30 01:07:54 -07001350 case kPcRel: // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
1351 emitPcRel(cUnit, entry, lir->operands[0], lir->operands[1], lir->operands[2],
1352 lir->operands[3], lir->operands[4]);
1353 break;
1354 case kMacro:
1355 emitMacro(cUnit, entry, lir->operands[0], lir->offset);
1356 break;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001357 default:
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001358 emitUnimplemented(cUnit, entry, lir);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001359 break;
1360 }
Ian Rogers7caad772012-03-30 01:07:54 -07001361 CHECK_EQ(static_cast<size_t>(oatGetInsnSize(lir)),
1362 cUnit->codeBuffer.size() - starting_cbuf_size)
1363 << "Instruction size mismatch for entry: " << EncodingMap[lir->opcode].name;
Ian Rogersb5d09b22012-03-06 22:14:17 -08001364 }
1365 return res;
buzbeee88dfbf2012-03-05 11:19:57 -08001366}
1367
buzbeee88dfbf2012-03-05 11:19:57 -08001368/*
1369 * Target-dependent offset assignment.
1370 * independent.
1371 */
1372int oatAssignInsnOffsets(CompilationUnit* cUnit)
1373{
1374 LIR* x86LIR;
1375 int offset = 0;
1376
1377 for (x86LIR = (LIR *) cUnit->firstLIRInsn;
1378 x86LIR;
1379 x86LIR = NEXT_LIR(x86LIR)) {
1380 x86LIR->offset = offset;
1381 if (x86LIR->opcode >= 0) {
1382 if (!x86LIR->flags.isNop) {
1383 offset += x86LIR->flags.size;
1384 }
1385 } else if (x86LIR->opcode == kPseudoPseudoAlign4) {
1386 if (offset & 0x2) {
1387 offset += 2;
1388 x86LIR->operands[0] = 1;
1389 } else {
1390 x86LIR->operands[0] = 0;
1391 }
1392 }
1393 /* Pseudo opcodes don't consume space */
1394 }
1395
1396 return offset;
1397}
1398
1399} // namespace art