blob: 5f215ef6bf77565ec49fa54c84d5657bd501d041 [file] [log] [blame]
buzbeee3acd072012-02-25 17:03:10 -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 "MipsLIR.h"
20#include "Codegen.h"
21#include <sys/mman.h> /* for protection change */
22
23namespace art {
24
25#define MAX_ASSEMBLER_RETRIES 50
26
27/*
28 * opcode: MipsOpCode enum
29 * skeleton: pre-designated bit-pattern for this opcode
30 * k0: key to applying ds/de
31 * ds: dest start bit position
32 * de: dest end bit position
33 * k1: key to applying s1s/s1e
34 * s1s: src1 start bit position
35 * s1e: src1 end bit position
36 * k2: key to applying s2s/s2e
37 * s2s: src2 start bit position
38 * s2e: src2 end bit position
39 * operands: number of operands (for sanity check purposes)
40 * name: mnemonic name
41 * fmt: for pretty-printing
42 */
43#define ENCODING_MAP(opcode, skeleton, k0, ds, de, k1, s1s, s1e, k2, s2s, s2e, \
44 k3, k3s, k3e, flags, name, fmt, size) \
45 {skeleton, {{k0, ds, de}, {k1, s1s, s1e}, {k2, s2s, s2e}, \
46 {k3, k3s, k3e}}, opcode, flags, name, fmt, size}
47
48/* Instruction dump string format keys: !pf, where "!" is the start
49 * of the key, "p" is which numeric operand to use and "f" is the
50 * print format.
51 *
52 * [p]ositions:
53 * 0 -> operands[0] (dest)
54 * 1 -> operands[1] (src1)
55 * 2 -> operands[2] (src2)
56 * 3 -> operands[3] (extra)
57 *
58 * [f]ormats:
59 * h -> 4-digit hex
60 * d -> decimal
61 * E -> decimal*4
62 * F -> decimal*2
63 * c -> branch condition (beq, bne, etc.)
64 * t -> pc-relative target
65 * T -> pc-region target
66 * u -> 1st half of bl[x] target
67 * v -> 2nd half ob bl[x] target
68 * R -> register list
69 * s -> single precision floating point register
70 * S -> double precision floating point register
71 * m -> Thumb2 modified immediate
72 * n -> complimented Thumb2 modified immediate
73 * M -> Thumb2 16-bit zero-extended immediate
74 * b -> 4-digit binary
buzbee82488f52012-03-02 08:20:26 -080075 * N -> append a NOP
buzbeee3acd072012-02-25 17:03:10 -080076 *
77 * [!] escape. To insert "!", use "!!"
78 */
buzbee5de34942012-03-01 14:51:57 -080079/* NOTE: must be kept in sync with enum MipsOpcode from LIR.h */
buzbee82488f52012-03-02 08:20:26 -080080/*
81 * TUNING: We're currently punting on the branch delay slots. All branch
82 * instructions in this map are given a size of 8, which during assembly
83 * is expanded to include a nop. This scheme should be replaced with
84 * an assembler pass to fill those slots when possible.
85 */
buzbeee3acd072012-02-25 17:03:10 -080086MipsEncodingMap EncodingMap[kMipsLast] = {
87 ENCODING_MAP(kMips32BitData, 0x00000000,
88 kFmtBitBlt, 31, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
89 kFmtUnused, -1, -1, IS_UNARY_OP,
buzbee71ac9942012-03-01 17:23:10 -080090 "data", "0x!0h(!0d)", 4),
buzbeee3acd072012-02-25 17:03:10 -080091 ENCODING_MAP(kMipsAddiu, 0x24000000,
92 kFmtBitBlt, 20, 16, kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0,
93 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -080094 "addiu", "!0r,!1r,0x!2h(!2d)", 4),
buzbeee3acd072012-02-25 17:03:10 -080095 ENCODING_MAP(kMipsAddu, 0x00000021,
96 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16,
97 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
buzbee71ac9942012-03-01 17:23:10 -080098 "addu", "!0r,!1r,!2r", 4),
buzbeee3acd072012-02-25 17:03:10 -080099 ENCODING_MAP(kMipsAnd, 0x00000024,
100 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16,
101 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
buzbee71ac9942012-03-01 17:23:10 -0800102 "and", "!0r,!1r,!2r", 4),
buzbeee3acd072012-02-25 17:03:10 -0800103 ENCODING_MAP(kMipsAndi, 0x30000000,
104 kFmtBitBlt, 20, 16, kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0,
105 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800106 "andi", "!0r,!1r,0x!2h(!2d)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800107 ENCODING_MAP(kMipsB, 0x10000000,
108 kFmtBitBlt, 15, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
109 kFmtUnused, -1, -1, NO_OPERAND | IS_BRANCH,
buzbee82488f52012-03-02 08:20:26 -0800110 "b", "!0t!0N", 8),
buzbeee3acd072012-02-25 17:03:10 -0800111 ENCODING_MAP(kMipsBal, 0x04110000,
112 kFmtBitBlt, 15, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
113 kFmtUnused, -1, -1, NO_OPERAND | IS_BRANCH | REG_DEF_LR,
buzbee82488f52012-03-02 08:20:26 -0800114 "bal", "!0t!0N", 8),
buzbeee3acd072012-02-25 17:03:10 -0800115 ENCODING_MAP(kMipsBeq, 0x10000000,
116 kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0,
117 kFmtUnused, -1, -1, IS_BINARY_OP | IS_BRANCH | REG_USE01,
buzbee82488f52012-03-02 08:20:26 -0800118 "beq", "!0r,!1r,!2t!0N", 8),
buzbeee3acd072012-02-25 17:03:10 -0800119 ENCODING_MAP(kMipsBeqz, 0x10000000, /* same as beq above with t = $zero */
120 kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
121 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH | REG_USE0,
buzbee82488f52012-03-02 08:20:26 -0800122 "beqz", "!0r,!1t!0N", 8),
buzbeee3acd072012-02-25 17:03:10 -0800123 ENCODING_MAP(kMipsBgez, 0x04010000,
124 kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
125 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH | REG_USE0,
buzbee82488f52012-03-02 08:20:26 -0800126 "bgez", "!0r,!1t!0N", 8),
buzbeee3acd072012-02-25 17:03:10 -0800127 ENCODING_MAP(kMipsBgtz, 0x1C000000,
128 kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
129 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH | REG_USE0,
buzbee82488f52012-03-02 08:20:26 -0800130 "bgtz", "!0r,!1t!0N", 8),
buzbeee3acd072012-02-25 17:03:10 -0800131 ENCODING_MAP(kMipsBlez, 0x18000000,
132 kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
133 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH | REG_USE0,
buzbee82488f52012-03-02 08:20:26 -0800134 "blez", "!0r,!1t!0N", 8),
buzbeee3acd072012-02-25 17:03:10 -0800135 ENCODING_MAP(kMipsBltz, 0x04000000,
136 kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
137 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH | REG_USE0,
buzbee82488f52012-03-02 08:20:26 -0800138 "bltz", "!0r,!1t!0N", 8),
buzbeee3acd072012-02-25 17:03:10 -0800139 ENCODING_MAP(kMipsBnez, 0x14000000, /* same as bne below with t = $zero */
140 kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
141 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH | REG_USE0,
buzbee82488f52012-03-02 08:20:26 -0800142 "bnez", "!0r,!1t!0N", 8),
buzbeee3acd072012-02-25 17:03:10 -0800143 ENCODING_MAP(kMipsBne, 0x14000000,
144 kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0,
145 kFmtUnused, -1, -1, IS_BINARY_OP | IS_BRANCH | REG_USE01,
buzbee82488f52012-03-02 08:20:26 -0800146 "bne", "!0r,!1r,!2t!0N", 8),
buzbeee3acd072012-02-25 17:03:10 -0800147 ENCODING_MAP(kMipsDiv, 0x0000001a,
148 kFmtUnused, -1, -1, kFmtUnused, -1, -1, kFmtBitBlt, 25, 21,
149 kFmtBitBlt, 20, 16, IS_QUAD_OP | REG_DEF01 | REG_USE23,
buzbee71ac9942012-03-01 17:23:10 -0800150 "div", "!2r,!3r", 4),
buzbeee3acd072012-02-25 17:03:10 -0800151#if __mips_isa_rev>=2
152 ENCODING_MAP(kMipsExt, 0x7c000000,
153 kFmtBitBlt, 20, 16, kFmtBitBlt, 25, 21, kFmtBitBlt, 10, 6,
154 kFmtBitBlt, 15, 11, IS_QUAD_OP | REG_DEF0 | REG_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800155 "ext", "!0r,!1r,!2d,!3D", 4),
buzbeee3acd072012-02-25 17:03:10 -0800156#endif
157 ENCODING_MAP(kMipsJal, 0x0c000000,
158 kFmtBitBlt, 25, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
159 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH | REG_DEF_LR,
buzbee82488f52012-03-02 08:20:26 -0800160 "jal", "!0T(!0E)!0N", 8),
buzbeee3acd072012-02-25 17:03:10 -0800161 ENCODING_MAP(kMipsJalr, 0x00000009,
162 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtUnused, -1, -1,
163 kFmtUnused, -1, -1, IS_BINARY_OP | IS_BRANCH | REG_DEF0_USE1,
buzbee82488f52012-03-02 08:20:26 -0800164 "jalr", "!0r,!1r!0N", 8),
buzbeee3acd072012-02-25 17:03:10 -0800165 ENCODING_MAP(kMipsJr, 0x00000008,
166 kFmtBitBlt, 25, 21, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
167 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH | REG_USE0,
buzbee82488f52012-03-02 08:20:26 -0800168 "jr", "!0r!0N", 8),
buzbeee3acd072012-02-25 17:03:10 -0800169 ENCODING_MAP(kMipsLahi, 0x3C000000,
170 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
171 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0,
buzbee71ac9942012-03-01 17:23:10 -0800172 "lahi/lui", "!0r,0x!1h(!1d)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800173 ENCODING_MAP(kMipsLalo, 0x34000000,
174 kFmtBitBlt, 20, 16, kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0,
175 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800176 "lalo/ori", "!0r,!1r,0x!2h(!2d)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800177 ENCODING_MAP(kMipsLui, 0x3C000000,
178 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
179 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0,
buzbee71ac9942012-03-01 17:23:10 -0800180 "lui", "!0r,0x!1h(!1d)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800181 ENCODING_MAP(kMipsLb, 0x80000000,
182 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
183 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE2 | IS_LOAD,
buzbee71ac9942012-03-01 17:23:10 -0800184 "lb", "!0r,!1d(!2r)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800185 ENCODING_MAP(kMipsLbu, 0x90000000,
186 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
187 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE2 | IS_LOAD,
buzbee71ac9942012-03-01 17:23:10 -0800188 "lbu", "!0r,!1d(!2r)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800189 ENCODING_MAP(kMipsLh, 0x84000000,
190 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
191 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE2 | IS_LOAD,
buzbee71ac9942012-03-01 17:23:10 -0800192 "lh", "!0r,!1d(!2r)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800193 ENCODING_MAP(kMipsLhu, 0x94000000,
194 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
195 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE2 | IS_LOAD,
buzbee71ac9942012-03-01 17:23:10 -0800196 "lhu", "!0r,!1d(!2r)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800197 ENCODING_MAP(kMipsLw, 0x8C000000,
198 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
199 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE2 | IS_LOAD,
buzbee71ac9942012-03-01 17:23:10 -0800200 "lw", "!0r,!1d(!2r)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800201 ENCODING_MAP(kMipsMfhi, 0x00000010,
202 kFmtBitBlt, 15, 11, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
203 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800204 "mfhi", "!0r", 4),
buzbeee3acd072012-02-25 17:03:10 -0800205 ENCODING_MAP(kMipsMflo, 0x00000012,
206 kFmtBitBlt, 15, 11, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
207 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800208 "mflo", "!0r", 4),
buzbeee3acd072012-02-25 17:03:10 -0800209 ENCODING_MAP(kMipsMove, 0x00000025, /* or using zero reg */
210 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtUnused, -1, -1,
211 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800212 "move", "!0r,!1r", 4),
buzbeee3acd072012-02-25 17:03:10 -0800213 ENCODING_MAP(kMipsMovz, 0x0000000a,
214 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16,
215 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
buzbee71ac9942012-03-01 17:23:10 -0800216 "movz", "!0r,!1r,!2r", 4),
buzbeee3acd072012-02-25 17:03:10 -0800217 ENCODING_MAP(kMipsMul, 0x70000002,
218 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16,
219 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
buzbee71ac9942012-03-01 17:23:10 -0800220 "mul", "!0r,!1r,!2r", 4),
buzbeee3acd072012-02-25 17:03:10 -0800221 ENCODING_MAP(kMipsNop, 0x00000000,
222 kFmtUnused, -1, -1, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
223 kFmtUnused, -1, -1, NO_OPERAND,
buzbeec5159d52012-03-03 11:48:39 -0800224 "nop", ";", 4),
buzbeee3acd072012-02-25 17:03:10 -0800225 ENCODING_MAP(kMipsNor, 0x00000027, /* used for "not" too */
226 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16,
227 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
buzbee71ac9942012-03-01 17:23:10 -0800228 "nor", "!0r,!1r,!2r", 4),
buzbeee3acd072012-02-25 17:03:10 -0800229 ENCODING_MAP(kMipsOr, 0x00000025,
230 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16,
231 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
buzbee71ac9942012-03-01 17:23:10 -0800232 "or", "!0r,!1r,!2r", 4),
buzbeee3acd072012-02-25 17:03:10 -0800233 ENCODING_MAP(kMipsOri, 0x34000000,
234 kFmtBitBlt, 20, 16, kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0,
235 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800236 "ori", "!0r,!1r,0x!2h(!2d)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800237 ENCODING_MAP(kMipsPref, 0xCC000000,
238 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
239 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE2,
buzbee71ac9942012-03-01 17:23:10 -0800240 "pref", "!0d,!1d(!2r)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800241 ENCODING_MAP(kMipsSb, 0xA0000000,
242 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
243 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE02 | IS_STORE,
buzbee71ac9942012-03-01 17:23:10 -0800244 "sb", "!0r,!1d(!2r)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800245#if __mips_isa_rev>=2
246 ENCODING_MAP(kMipsSeb, 0x7c000420,
247 kFmtBitBlt, 15, 11, kFmtBitBlt, 20, 16, kFmtUnused, -1, -1,
248 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800249 "seb", "!0r,!1r", 4),
buzbeee3acd072012-02-25 17:03:10 -0800250 ENCODING_MAP(kMipsSeh, 0x7c000620,
251 kFmtBitBlt, 15, 11, kFmtBitBlt, 20, 16, kFmtUnused, -1, -1,
252 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800253 "seh", "!0r,!1r", 4),
buzbeee3acd072012-02-25 17:03:10 -0800254#endif
255 ENCODING_MAP(kMipsSh, 0xA4000000,
256 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
257 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE02 | IS_STORE,
buzbee71ac9942012-03-01 17:23:10 -0800258 "sh", "!0r,!1d(!2r)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800259 ENCODING_MAP(kMipsSll, 0x00000000,
260 kFmtBitBlt, 15, 11, kFmtBitBlt, 20, 16, kFmtBitBlt, 10, 6,
261 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800262 "sll", "!0r,!1r,0x!2h(!2d)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800263 ENCODING_MAP(kMipsSllv, 0x00000004,
264 kFmtBitBlt, 15, 11, kFmtBitBlt, 20, 16, kFmtBitBlt, 25, 21,
265 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
buzbee71ac9942012-03-01 17:23:10 -0800266 "sllv", "!0r,!1r,!2r", 4),
buzbeee3acd072012-02-25 17:03:10 -0800267 ENCODING_MAP(kMipsSlt, 0x0000002a,
268 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16,
269 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
buzbee71ac9942012-03-01 17:23:10 -0800270 "slt", "!0r,!1r,!2r", 4),
buzbeee3acd072012-02-25 17:03:10 -0800271 ENCODING_MAP(kMipsSlti, 0x28000000,
272 kFmtBitBlt, 20, 16, kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0,
273 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800274 "slti", "!0r,!1r,0x!2h(!2d)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800275 ENCODING_MAP(kMipsSltu, 0x0000002b,
276 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16,
277 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
buzbee71ac9942012-03-01 17:23:10 -0800278 "sltu", "!0r,!1r,!2r", 4),
buzbeee3acd072012-02-25 17:03:10 -0800279 ENCODING_MAP(kMipsSra, 0x00000003,
280 kFmtBitBlt, 15, 11, kFmtBitBlt, 20, 16, kFmtBitBlt, 10, 6,
281 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800282 "sra", "!0r,!1r,0x!2h(!2d)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800283 ENCODING_MAP(kMipsSrav, 0x00000007,
284 kFmtBitBlt, 15, 11, kFmtBitBlt, 20, 16, kFmtBitBlt, 25, 21,
285 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
buzbee71ac9942012-03-01 17:23:10 -0800286 "srav", "!0r,!1r,!2r", 4),
buzbeee3acd072012-02-25 17:03:10 -0800287 ENCODING_MAP(kMipsSrl, 0x00000002,
288 kFmtBitBlt, 15, 11, kFmtBitBlt, 20, 16, kFmtBitBlt, 10, 6,
289 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800290 "srl", "!0r,!1r,0x!2h(!2d)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800291 ENCODING_MAP(kMipsSrlv, 0x00000006,
292 kFmtBitBlt, 15, 11, kFmtBitBlt, 20, 16, kFmtBitBlt, 25, 21,
293 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
buzbee71ac9942012-03-01 17:23:10 -0800294 "srlv", "!0r,!1r,!2r", 4),
buzbeee3acd072012-02-25 17:03:10 -0800295 ENCODING_MAP(kMipsSubu, 0x00000023, /* used for "neg" too */
296 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16,
297 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
buzbee71ac9942012-03-01 17:23:10 -0800298 "subu", "!0r,!1r,!2r", 4),
buzbeee3acd072012-02-25 17:03:10 -0800299 ENCODING_MAP(kMipsSw, 0xAC000000,
300 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
301 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE02 | IS_STORE,
buzbee71ac9942012-03-01 17:23:10 -0800302 "sw", "!0r,!1d(!2r)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800303 ENCODING_MAP(kMipsXor, 0x00000026,
304 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16,
305 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
buzbee71ac9942012-03-01 17:23:10 -0800306 "xor", "!0r,!1r,!2r", 4),
buzbeee3acd072012-02-25 17:03:10 -0800307 ENCODING_MAP(kMipsXori, 0x38000000,
308 kFmtBitBlt, 20, 16, kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0,
309 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800310 "xori", "!0r,!1r,0x!2h(!2d)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800311#ifdef __mips_hard_float
312 ENCODING_MAP(kMipsFadds, 0x46000000,
313 kFmtSfp, 10, 6, kFmtSfp, 15, 11, kFmtSfp, 20, 16,
314 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
buzbee71ac9942012-03-01 17:23:10 -0800315 "add.s", "!0s,!1s,!2s", 4),
buzbeee3acd072012-02-25 17:03:10 -0800316 ENCODING_MAP(kMipsFsubs, 0x46000001,
317 kFmtSfp, 10, 6, kFmtSfp, 15, 11, kFmtSfp, 20, 16,
318 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
buzbee71ac9942012-03-01 17:23:10 -0800319 "sub.s", "!0s,!1s,!2s", 4),
buzbeee3acd072012-02-25 17:03:10 -0800320 ENCODING_MAP(kMipsFmuls, 0x46000002,
321 kFmtSfp, 10, 6, kFmtSfp, 15, 11, kFmtSfp, 20, 16,
322 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
buzbee71ac9942012-03-01 17:23:10 -0800323 "mul.s", "!0s,!1s,!2s", 4),
buzbeee3acd072012-02-25 17:03:10 -0800324 ENCODING_MAP(kMipsFdivs, 0x46000003,
325 kFmtSfp, 10, 6, kFmtSfp, 15, 11, kFmtSfp, 20, 16,
326 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
buzbee71ac9942012-03-01 17:23:10 -0800327 "div.s", "!0s,!1s,!2s", 4),
buzbeee3acd072012-02-25 17:03:10 -0800328 ENCODING_MAP(kMipsFaddd, 0x46200000,
329 kFmtDfp, 10, 6, kFmtDfp, 15, 11, kFmtDfp, 20, 16,
330 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
buzbee71ac9942012-03-01 17:23:10 -0800331 "add.d", "!0S,!1S,!2S", 4),
buzbeee3acd072012-02-25 17:03:10 -0800332 ENCODING_MAP(kMipsFsubd, 0x46200001,
333 kFmtDfp, 10, 6, kFmtDfp, 15, 11, kFmtDfp, 20, 16,
334 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
buzbee71ac9942012-03-01 17:23:10 -0800335 "sub.d", "!0S,!1S,!2S", 4),
buzbeee3acd072012-02-25 17:03:10 -0800336 ENCODING_MAP(kMipsFmuld, 0x46200002,
337 kFmtDfp, 10, 6, kFmtDfp, 15, 11, kFmtDfp, 20, 16,
338 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
buzbee71ac9942012-03-01 17:23:10 -0800339 "mul.d", "!0S,!1S,!2S", 4),
buzbeee3acd072012-02-25 17:03:10 -0800340 ENCODING_MAP(kMipsFdivd, 0x46200003,
341 kFmtDfp, 10, 6, kFmtDfp, 15, 11, kFmtDfp, 20, 16,
342 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
buzbee71ac9942012-03-01 17:23:10 -0800343 "div.d", "!0S,!1S,!2S", 4),
buzbeee3acd072012-02-25 17:03:10 -0800344 ENCODING_MAP(kMipsFcvtsd, 0x46200020,
345 kFmtSfp, 10, 6, kFmtDfp, 15, 11, kFmtUnused, -1, -1,
346 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800347 "cvt.s.d", "!0s,!1S", 4),
buzbeee3acd072012-02-25 17:03:10 -0800348 ENCODING_MAP(kMipsFcvtsw, 0x46800020,
349 kFmtSfp, 10, 6, kFmtSfp, 15, 11, kFmtUnused, -1, -1,
350 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800351 "cvt.s.w", "!0s,!1s", 4),
buzbeee3acd072012-02-25 17:03:10 -0800352 ENCODING_MAP(kMipsFcvtds, 0x46000021,
353 kFmtDfp, 10, 6, kFmtSfp, 15, 11, kFmtUnused, -1, -1,
354 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800355 "cvt.d.s", "!0S,!1s", 4),
buzbeee3acd072012-02-25 17:03:10 -0800356 ENCODING_MAP(kMipsFcvtdw, 0x46800021,
357 kFmtDfp, 10, 6, kFmtSfp, 15, 11, kFmtUnused, -1, -1,
358 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800359 "cvt.d.w", "!0S,!1s", 4),
buzbeee3acd072012-02-25 17:03:10 -0800360 ENCODING_MAP(kMipsFcvtws, 0x46000024,
361 kFmtSfp, 10, 6, kFmtSfp, 15, 11, kFmtUnused, -1, -1,
362 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800363 "cvt.w.s", "!0s,!1s", 4),
buzbeee3acd072012-02-25 17:03:10 -0800364 ENCODING_MAP(kMipsFcvtwd, 0x46200024,
365 kFmtSfp, 10, 6, kFmtDfp, 15, 11, kFmtUnused, -1, -1,
366 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800367 "cvt.w.d", "!0s,!1S", 4),
buzbeee3acd072012-02-25 17:03:10 -0800368 ENCODING_MAP(kMipsFmovs, 0x46000006,
369 kFmtSfp, 10, 6, kFmtSfp, 15, 11, kFmtUnused, -1, -1,
370 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800371 "mov.s", "!0s,!1s", 4),
buzbeee3acd072012-02-25 17:03:10 -0800372 ENCODING_MAP(kMipsFmovd, 0x46200006,
373 kFmtDfp, 10, 6, kFmtDfp, 15, 11, kFmtUnused, -1, -1,
374 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800375 "mov.d", "!0S,!1S", 4),
buzbeee3acd072012-02-25 17:03:10 -0800376 ENCODING_MAP(kMipsFlwc1, 0xC4000000,
377 kFmtSfp, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
378 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE2 | IS_LOAD,
buzbee71ac9942012-03-01 17:23:10 -0800379 "lwc1", "!0s,!1d(!2r)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800380 ENCODING_MAP(kMipsFldc1, 0xD4000000,
381 kFmtDfp, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
382 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE2 | IS_LOAD,
buzbee71ac9942012-03-01 17:23:10 -0800383 "ldc1", "!0S,!1d(!2r)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800384 ENCODING_MAP(kMipsFswc1, 0xE4000000,
385 kFmtSfp, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
386 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE02 | IS_STORE,
buzbee71ac9942012-03-01 17:23:10 -0800387 "swc1", "!0s,!1d(!2r)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800388 ENCODING_MAP(kMipsFsdc1, 0xF4000000,
389 kFmtDfp, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
390 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE02 | IS_STORE,
buzbee71ac9942012-03-01 17:23:10 -0800391 "sdc1", "!0S,!1d(!2r)", 4),
buzbeee3acd072012-02-25 17:03:10 -0800392 ENCODING_MAP(kMipsMfc1, 0x44000000,
393 kFmtBitBlt, 20, 16, kFmtSfp, 15, 11, kFmtUnused, -1, -1,
394 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
buzbee71ac9942012-03-01 17:23:10 -0800395 "mfc1", "!0r,!1s", 4),
buzbeee3acd072012-02-25 17:03:10 -0800396 ENCODING_MAP(kMipsMtc1, 0x44800000,
397 kFmtBitBlt, 20, 16, kFmtSfp, 15, 11, kFmtUnused, -1, -1,
398 kFmtUnused, -1, -1, IS_BINARY_OP | REG_USE0 | REG_DEF1,
buzbee71ac9942012-03-01 17:23:10 -0800399 "mtc1", "!0r,!1s", 4),
buzbeee3acd072012-02-25 17:03:10 -0800400#endif
buzbeec5159d52012-03-03 11:48:39 -0800401 ENCODING_MAP(kMipsDelta, 0x27e00000,
402 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtUnused, 15, 0,
403 kFmtUnused, -1, -1, IS_QUAD_OP | REG_DEF0 | REG_USE_LR,
404 "addiu", "!0r,r_ra,0x!1h(!1d)", 4),
405 ENCODING_MAP(kMipsDeltaHi, 0x3C000000,
406 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
407 kFmtUnused, -1, -1, IS_QUAD_OP | REG_DEF0,
408 "lui", "!0r,0x!1h(!1d)", 4),
409 ENCODING_MAP(kMipsDeltaLo, 0x34000000,
410 kFmtBlt5_2, 16, 21, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
411 kFmtUnused, -1, -1, IS_QUAD_OP | REG_DEF0_USE0,
412 "ori", "!0r,!0r,0x!1h(!1d)", 4),
413 ENCODING_MAP(kMipsCurrPC, 0x0c000000,
414 kFmtUnused, -1, -1, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
415 kFmtUnused, -1, -1, NO_OPERAND | IS_BRANCH | REG_DEF_LR,
416 "pc2ra", "; r_ra <- .+8", 4),
buzbeee3acd072012-02-25 17:03:10 -0800417 ENCODING_MAP(kMipsUndefined, 0x64000000,
418 kFmtUnused, -1, -1, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
419 kFmtUnused, -1, -1, NO_OPERAND,
buzbee71ac9942012-03-01 17:23:10 -0800420 "undefined", "", 4),
buzbeee3acd072012-02-25 17:03:10 -0800421};
422
423/*
424 * Assemble the LIR into binary instruction format. Note that we may
425 * discover that pc-relative displacements may not fit the selected
426 * instruction. In those cases we will try to substitute a new code
427 * sequence or request that the trace be shortened and retried.
428 */
429AssemblerStatus oatAssembleInstructions(CompilationUnit *cUnit,
430 intptr_t startAddr)
431{
buzbee5de34942012-03-01 14:51:57 -0800432 LIR *lir;
433 AssemblerStatus res = kSuccess; // Assume success
buzbeee3acd072012-02-25 17:03:10 -0800434
buzbee5de34942012-03-01 14:51:57 -0800435 for (lir = (LIR *) cUnit->firstLIRInsn; lir; lir = NEXT_LIR(lir)) {
buzbeee3acd072012-02-25 17:03:10 -0800436 if (lir->opcode < 0) {
437 continue;
438 }
439
440
441 if (lir->flags.isNop) {
442 continue;
443 }
444
buzbeec5159d52012-03-03 11:48:39 -0800445// TODO: check for lir->flags.pcRelFixup
446
447 if (lir->opcode == kMipsDelta) {
448 int offset1 = ((LIR*)lir->operands[2])->offset;
449 SwitchTable *tabRec = (SwitchTable*)lir->operands[3];
450 int offset2 = tabRec ? tabRec->offset : lir->target->offset;
451 int delta = offset2 - offset1;
452 if ((delta & 0xffff) == delta) {
453 // Fits
454 lir->operands[1] = delta;
455 } else {
456 // Doesn't fit - must expand to kMipsDelta[Hi|Lo] pair
457 LIR *newDeltaHi =
458 (LIR *)oatNew(cUnit, sizeof(LIR), true,
459 kAllocLIR);
460 newDeltaHi->dalvikOffset = lir->dalvikOffset;
461 newDeltaHi->target = lir->target;
462 newDeltaHi->opcode = kMipsDeltaHi;
463 newDeltaHi->operands[0] = lir->operands[0];
464 newDeltaHi->operands[2] = lir->operands[2];
465 newDeltaHi->operands[3] = lir->operands[3];
466 oatSetupResourceMasks(newDeltaHi);
467 oatInsertLIRBefore((LIR*)lir, (LIR*)newDeltaHi);
468 LIR *newDeltaLo =
469 (LIR *)oatNew(cUnit, sizeof(LIR), true,
470 kAllocLIR);
471 newDeltaLo->dalvikOffset = lir->dalvikOffset;
472 newDeltaLo->target = lir->target;
473 newDeltaLo->opcode = kMipsDeltaLo;
474 newDeltaLo->operands[0] = lir->operands[0];
475 newDeltaLo->operands[2] = lir->operands[2];
476 newDeltaLo->operands[3] = lir->operands[3];
477 oatSetupResourceMasks(newDeltaLo);
478 oatInsertLIRBefore((LIR*)lir, (LIR*)newDeltaLo);
479 lir->flags.isNop = true;
480 res = kRetryAll;
481 }
482 } else if (lir->opcode == kMipsDeltaLo) {
483 int offset1 = ((LIR*)lir->operands[2])->offset;
484 SwitchTable *tabRec = (SwitchTable*)lir->operands[3];
485 int offset2 = tabRec ? tabRec->offset : lir->target->offset;
486 int delta = offset2 - offset1;
487 lir->operands[1] = delta & 0xffff;
488 } else if (lir->opcode == kMipsDeltaHi) {
489 int offset1 = ((LIR*)lir->operands[2])->offset;
490 SwitchTable *tabRec = (SwitchTable*)lir->operands[3];
491 int offset2 = tabRec ? tabRec->offset : lir->target->offset;
492 int delta = offset2 - offset1;
493 lir->operands[1] = (delta >> 16) & 0xffff;
494 } else if (lir->opcode == kMipsB || lir->opcode == kMipsBal) {
buzbee5de34942012-03-01 14:51:57 -0800495 LIR *targetLIR = (LIR *) lir->target;
496 intptr_t pc = lir->offset + 4;
497 intptr_t target = targetLIR->offset;
buzbeee3acd072012-02-25 17:03:10 -0800498 int delta = target - pc;
499 if (delta & 0x3) {
500 LOG(FATAL) << "PC-rel offset not multiple of 4: " << delta;
501 }
502 if (delta > 131068 || delta < -131069) {
buzbee5de34942012-03-01 14:51:57 -0800503 UNIMPLEMENTED(FATAL) << "B out of range, need long sequence: " << delta;
buzbeee3acd072012-02-25 17:03:10 -0800504 }
505 lir->operands[0] = delta >> 2;
506 } else if (lir->opcode >= kMipsBeqz && lir->opcode <= kMipsBnez) {
buzbee5de34942012-03-01 14:51:57 -0800507 LIR *targetLIR = (LIR *) lir->target;
508 intptr_t pc = lir->offset + 4;
509 intptr_t target = targetLIR->offset;
buzbeee3acd072012-02-25 17:03:10 -0800510 int delta = target - pc;
511 if (delta & 0x3) {
512 LOG(FATAL) << "PC-rel offset not multiple of 4: " << delta;
513 }
514 if (delta > 131068 || delta < -131069) {
buzbee5de34942012-03-01 14:51:57 -0800515 UNIMPLEMENTED(FATAL) << "B[eq|ne]z needs long sequence: " << delta;
buzbeee3acd072012-02-25 17:03:10 -0800516 }
517 lir->operands[1] = delta >> 2;
518 } else if (lir->opcode == kMipsBeq || lir->opcode == kMipsBne) {
buzbee5de34942012-03-01 14:51:57 -0800519 LIR *targetLIR = (LIR *) lir->target;
520 intptr_t pc = lir->offset + 4;
521 intptr_t target = targetLIR->offset;
buzbeee3acd072012-02-25 17:03:10 -0800522 int delta = target - pc;
523 if (delta & 0x3) {
524 LOG(FATAL) << "PC-rel offset not multiple of 4: " << delta;
525 }
526 if (delta > 131068 || delta < -131069) {
buzbee5de34942012-03-01 14:51:57 -0800527 UNIMPLEMENTED(FATAL) << "B[eq|ne] needs long sequence: " << delta;
buzbeee3acd072012-02-25 17:03:10 -0800528 }
529 lir->operands[2] = delta >> 2;
530 } else if (lir->opcode == kMipsJal) {
buzbee5de34942012-03-01 14:51:57 -0800531 intptr_t curPC = (startAddr + lir->offset + 4) & ~3;
buzbeee3acd072012-02-25 17:03:10 -0800532 intptr_t target = lir->operands[0];
533 /* ensure PC-region branch can be used */
534 DCHECK_EQ((curPC & 0xF0000000), (target & 0xF0000000));
535 if (target & 0x3) {
536 LOG(FATAL) << "Jump target not multiple of 4: " << target;
537 }
538 lir->operands[0] = target >> 2;
539 } else if (lir->opcode == kMipsLahi) { /* load address hi (via lui) */
buzbee5de34942012-03-01 14:51:57 -0800540 LIR *targetLIR = (LIR *) lir->target;
541 intptr_t target = startAddr + targetLIR->offset;
buzbeee3acd072012-02-25 17:03:10 -0800542 lir->operands[1] = target >> 16;
543 } else if (lir->opcode == kMipsLalo) { /* load address lo (via ori) */
buzbee5de34942012-03-01 14:51:57 -0800544 LIR *targetLIR = (LIR *) lir->target;
545 intptr_t target = startAddr + targetLIR->offset;
buzbeee3acd072012-02-25 17:03:10 -0800546 lir->operands[2] = lir->operands[2] + target;
547 }
548
buzbee5de34942012-03-01 14:51:57 -0800549 /*
550 * If one of the pc-relative instructions expanded we'll have
551 * to make another pass. Don't bother to fully assemble the
552 * instruction.
553 */
554 if (res != kSuccess) {
555 continue;
556 }
557 const MipsEncodingMap *encoder = &EncodingMap[lir->opcode];
buzbeee3acd072012-02-25 17:03:10 -0800558 u4 bits = encoder->skeleton;
559 int i;
560 for (i = 0; i < 4; i++) {
561 u4 operand;
562 u4 value;
563 operand = lir->operands[i];
564 switch(encoder->fieldLoc[i].kind) {
565 case kFmtUnused:
566 break;
567 case kFmtBitBlt:
568 if (encoder->fieldLoc[i].start == 0 && encoder->fieldLoc[i].end == 31) {
569 value = operand;
570 } else {
571 value = (operand << encoder->fieldLoc[i].start) &
572 ((1 << (encoder->fieldLoc[i].end + 1)) - 1);
573 }
574 bits |= value;
575 break;
buzbeec5159d52012-03-03 11:48:39 -0800576 case kFmtBlt5_2:
577 value = (operand & 0x1f);
578 bits |= (value << encoder->fieldLoc[i].start);
579 bits |= (value << encoder->fieldLoc[i].end);
580 break;
buzbeee3acd072012-02-25 17:03:10 -0800581 case kFmtDfp: {
582 DCHECK(DOUBLEREG(operand));
buzbee5de34942012-03-01 14:51:57 -0800583 DCHECK((operand & 0x1) == 0);
buzbeee3acd072012-02-25 17:03:10 -0800584 value = ((operand & FP_REG_MASK) << encoder->fieldLoc[i].start) &
585 ((1 << (encoder->fieldLoc[i].end + 1)) - 1);
586 bits |= value;
587 break;
588 }
589 case kFmtSfp:
590 DCHECK(SINGLEREG(operand));
591 value = ((operand & FP_REG_MASK) << encoder->fieldLoc[i].start) &
592 ((1 << (encoder->fieldLoc[i].end + 1)) - 1);
593 bits |= value;
594 break;
595 default:
596 LOG(FATAL) << "Bad encoder format: "
buzbee5de34942012-03-01 14:51:57 -0800597 << (int)encoder->fieldLoc[i].kind;
buzbeee3acd072012-02-25 17:03:10 -0800598 }
599 }
buzbee5de34942012-03-01 14:51:57 -0800600 // FIXME: need multi-endian handling here
601 cUnit->codeBuffer.push_back((bits >> 16) & 0xffff);
602 cUnit->codeBuffer.push_back(bits & 0xffff);
buzbee82488f52012-03-02 08:20:26 -0800603 // TUNING: replace with proper delay slot handling
604 if (encoder->size == 8) {
605 const MipsEncodingMap *encoder = &EncodingMap[kMipsNop];
606 u4 bits = encoder->skeleton;
607 cUnit->codeBuffer.push_back((bits >> 16) & 0xffff);
608 cUnit->codeBuffer.push_back(bits & 0xffff);
609 }
buzbeee3acd072012-02-25 17:03:10 -0800610 }
buzbee5de34942012-03-01 14:51:57 -0800611 return res;
buzbeee3acd072012-02-25 17:03:10 -0800612}
613
614/*
615 * Target-dependent offset assignment.
616 * TODO: normalize usage of flags.size and make this target
617 * independent.
618 */
619int oatAssignInsnOffsets(CompilationUnit* cUnit)
620{
buzbee5de34942012-03-01 14:51:57 -0800621 LIR* mipsLIR;
buzbeee3acd072012-02-25 17:03:10 -0800622 int offset = 0;
623
buzbee5de34942012-03-01 14:51:57 -0800624 for (mipsLIR = (LIR *) cUnit->firstLIRInsn;
625 mipsLIR;
626 mipsLIR = NEXT_LIR(mipsLIR)) {
627 mipsLIR->offset = offset;
buzbeee3acd072012-02-25 17:03:10 -0800628 if (mipsLIR->opcode >= 0) {
629 if (!mipsLIR->flags.isNop) {
buzbee71ac9942012-03-01 17:23:10 -0800630 mipsLIR->flags.size = EncodingMap[mipsLIR->opcode].size;
buzbeee3acd072012-02-25 17:03:10 -0800631 offset += mipsLIR->flags.size;
632 }
buzbee31a4a6f2012-02-28 15:36:15 -0800633 } else if (mipsLIR->opcode == kPseudoPseudoAlign4) {
buzbeee3acd072012-02-25 17:03:10 -0800634 if (offset & 0x2) {
635 offset += 2;
636 mipsLIR->operands[0] = 1;
637 } else {
638 mipsLIR->operands[0] = 0;
639 }
640 }
641 /* Pseudo opcodes don't consume space */
642 }
643
644 return offset;
645}
646
647} // namespace art