blob: 5c98b10b5850105412e4ae445aca8158ae43b1ed [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
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 "codegen_mips.h"
Ian Rogersd582fa42014-11-05 23:46:43 -080018
Andreas Gampe0b9203e2015-01-22 20:39:27 -080019#include "base/logging.h"
20#include "dex/compiler_ir.h"
Andreas Gampea2e18ed2015-01-26 16:39:37 -080021#include "dex/quick/mir_to_lir-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include "mips_lir.h"
23
24namespace art {
25
26#define MAX_ASSEMBLER_RETRIES 50
27
28/*
29 * opcode: MipsOpCode enum
30 * skeleton: pre-designated bit-pattern for this opcode
31 * k0: key to applying ds/de
32 * ds: dest start bit position
33 * de: dest end bit position
34 * k1: key to applying s1s/s1e
35 * s1s: src1 start bit position
36 * s1e: src1 end bit position
37 * k2: key to applying s2s/s2e
38 * s2s: src2 start bit position
39 * s2e: src2 end bit position
40 * operands: number of operands (for sanity check purposes)
41 * name: mnemonic name
42 * fmt: for pretty-printing
43 */
44#define ENCODING_MAP(opcode, skeleton, k0, ds, de, k1, s1s, s1e, k2, s2s, s2e, \
45 k3, k3s, k3e, flags, name, fmt, size) \
46 {skeleton, {{k0, ds, de}, {k1, s1s, s1e}, {k2, s2s, s2e}, \
47 {k3, k3s, k3e}}, opcode, flags, name, fmt, size}
48
49/* Instruction dump string format keys: !pf, where "!" is the start
50 * of the key, "p" is which numeric operand to use and "f" is the
51 * print format.
52 *
53 * [p]ositions:
54 * 0 -> operands[0] (dest)
55 * 1 -> operands[1] (src1)
56 * 2 -> operands[2] (src2)
57 * 3 -> operands[3] (extra)
58 *
59 * [f]ormats:
60 * h -> 4-digit hex
61 * d -> decimal
62 * E -> decimal*4
63 * F -> decimal*2
64 * c -> branch condition (beq, bne, etc.)
65 * t -> pc-relative target
66 * T -> pc-region target
67 * u -> 1st half of bl[x] target
68 * v -> 2nd half ob bl[x] target
69 * R -> register list
70 * s -> single precision floating point register
71 * S -> double precision floating point register
72 * m -> Thumb2 modified immediate
73 * n -> complimented Thumb2 modified immediate
74 * M -> Thumb2 16-bit zero-extended immediate
75 * b -> 4-digit binary
76 * N -> append a NOP
77 *
78 * [!] escape. To insert "!", use "!!"
79 */
80/* NOTE: must be kept in sync with enum MipsOpcode from LIR.h */
81/*
82 * TUNING: We're currently punting on the branch delay slots. All branch
83 * instructions in this map are given a size of 8, which during assembly
84 * is expanded to include a nop. This scheme should be replaced with
85 * an assembler pass to fill those slots when possible.
86 */
87const MipsEncodingMap MipsMir2Lir::EncodingMap[kMipsLast] = {
88 ENCODING_MAP(kMips32BitData, 0x00000000,
89 kFmtBitBlt, 31, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
90 kFmtUnused, -1, -1, IS_UNARY_OP,
91 "data", "0x!0h(!0d)", 4),
92 ENCODING_MAP(kMipsAddiu, 0x24000000,
93 kFmtBitBlt, 20, 16, kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0,
94 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
95 "addiu", "!0r,!1r,0x!2h(!2d)", 4),
96 ENCODING_MAP(kMipsAddu, 0x00000021,
97 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16,
98 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
99 "addu", "!0r,!1r,!2r", 4),
100 ENCODING_MAP(kMipsAnd, 0x00000024,
101 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16,
102 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
103 "and", "!0r,!1r,!2r", 4),
104 ENCODING_MAP(kMipsAndi, 0x30000000,
105 kFmtBitBlt, 20, 16, kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0,
106 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
107 "andi", "!0r,!1r,0x!2h(!2d)", 4),
108 ENCODING_MAP(kMipsB, 0x10000000,
109 kFmtBitBlt, 15, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
110 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH | NEEDS_FIXUP,
111 "b", "!0t!0N", 8),
112 ENCODING_MAP(kMipsBal, 0x04110000,
113 kFmtBitBlt, 15, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
114 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH | REG_DEF_LR |
115 NEEDS_FIXUP, "bal", "!0t!0N", 8),
116 ENCODING_MAP(kMipsBeq, 0x10000000,
117 kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0,
118 kFmtUnused, -1, -1, IS_BINARY_OP | IS_BRANCH | REG_USE01 |
119 NEEDS_FIXUP, "beq", "!0r,!1r,!2t!0N", 8),
120 ENCODING_MAP(kMipsBeqz, 0x10000000, /* same as beq above with t = $zero */
121 kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
122 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH | REG_USE0 |
123 NEEDS_FIXUP, "beqz", "!0r,!1t!0N", 8),
124 ENCODING_MAP(kMipsBgez, 0x04010000,
125 kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
126 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH | REG_USE0 |
127 NEEDS_FIXUP, "bgez", "!0r,!1t!0N", 8),
128 ENCODING_MAP(kMipsBgtz, 0x1C000000,
129 kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
130 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH | REG_USE0 |
131 NEEDS_FIXUP, "bgtz", "!0r,!1t!0N", 8),
132 ENCODING_MAP(kMipsBlez, 0x18000000,
133 kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
134 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH | REG_USE0 |
135 NEEDS_FIXUP, "blez", "!0r,!1t!0N", 8),
136 ENCODING_MAP(kMipsBltz, 0x04000000,
137 kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
138 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH | REG_USE0 |
139 NEEDS_FIXUP, "bltz", "!0r,!1t!0N", 8),
140 ENCODING_MAP(kMipsBnez, 0x14000000, /* same as bne below with t = $zero */
141 kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
142 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH | REG_USE0 |
143 NEEDS_FIXUP, "bnez", "!0r,!1t!0N", 8),
144 ENCODING_MAP(kMipsBne, 0x14000000,
145 kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0,
146 kFmtUnused, -1, -1, IS_BINARY_OP | IS_BRANCH | REG_USE01 |
147 NEEDS_FIXUP, "bne", "!0r,!1r,!2t!0N", 8),
148 ENCODING_MAP(kMipsDiv, 0x0000001a,
buzbee9da5c102014-03-28 12:59:18 -0700149 kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16, kFmtUnused, -1, -1,
150 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF_HI | REG_DEF_LO | REG_USE01,
151 "div", "!0r,!1r", 4),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152 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,
155 "ext", "!0r,!1r,!2d,!3D", 4),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156 ENCODING_MAP(kMipsJal, 0x0c000000,
157 kFmtBitBlt, 25, 0, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
158 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH | REG_DEF_LR,
159 "jal", "!0T(!0E)!0N", 8),
160 ENCODING_MAP(kMipsJalr, 0x00000009,
161 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtUnused, -1, -1,
162 kFmtUnused, -1, -1, IS_BINARY_OP | IS_BRANCH | REG_DEF0_USE1,
163 "jalr", "!0r,!1r!0N", 8),
164 ENCODING_MAP(kMipsJr, 0x00000008,
165 kFmtBitBlt, 25, 21, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
166 kFmtUnused, -1, -1, IS_UNARY_OP | IS_BRANCH | REG_USE0 |
167 NEEDS_FIXUP, "jr", "!0r!0N", 8),
168 ENCODING_MAP(kMipsLahi, 0x3C000000,
169 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
170 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0,
171 "lahi/lui", "!0r,0x!1h(!1d)", 4),
172 ENCODING_MAP(kMipsLalo, 0x34000000,
173 kFmtBitBlt, 20, 16, kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0,
174 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
175 "lalo/ori", "!0r,!1r,0x!2h(!2d)", 4),
176 ENCODING_MAP(kMipsLui, 0x3C000000,
177 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
178 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0,
179 "lui", "!0r,0x!1h(!1d)", 4),
180 ENCODING_MAP(kMipsLb, 0x80000000,
181 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
182 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE2 | IS_LOAD,
183 "lb", "!0r,!1d(!2r)", 4),
184 ENCODING_MAP(kMipsLbu, 0x90000000,
185 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
186 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE2 | IS_LOAD,
187 "lbu", "!0r,!1d(!2r)", 4),
188 ENCODING_MAP(kMipsLh, 0x84000000,
189 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
190 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE2 | IS_LOAD,
191 "lh", "!0r,!1d(!2r)", 4),
192 ENCODING_MAP(kMipsLhu, 0x94000000,
193 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
194 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE2 | IS_LOAD,
195 "lhu", "!0r,!1d(!2r)", 4),
196 ENCODING_MAP(kMipsLw, 0x8C000000,
197 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
198 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE2 | IS_LOAD,
199 "lw", "!0r,!1d(!2r)", 4),
200 ENCODING_MAP(kMipsMfhi, 0x00000010,
201 kFmtBitBlt, 15, 11, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
buzbee9da5c102014-03-28 12:59:18 -0700202 kFmtUnused, -1, -1, IS_UNARY_OP | REG_DEF0 | REG_USE_HI,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700203 "mfhi", "!0r", 4),
204 ENCODING_MAP(kMipsMflo, 0x00000012,
205 kFmtBitBlt, 15, 11, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
buzbee9da5c102014-03-28 12:59:18 -0700206 kFmtUnused, -1, -1, IS_UNARY_OP | REG_DEF0 | REG_USE_LO,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700207 "mflo", "!0r", 4),
208 ENCODING_MAP(kMipsMove, 0x00000025, /* or using zero reg */
209 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtUnused, -1, -1,
210 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
211 "move", "!0r,!1r", 4),
212 ENCODING_MAP(kMipsMovz, 0x0000000a,
213 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16,
214 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
215 "movz", "!0r,!1r,!2r", 4),
216 ENCODING_MAP(kMipsMul, 0x70000002,
217 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16,
218 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
219 "mul", "!0r,!1r,!2r", 4),
220 ENCODING_MAP(kMipsNop, 0x00000000,
221 kFmtUnused, -1, -1, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
222 kFmtUnused, -1, -1, NO_OPERAND,
223 "nop", ";", 4),
224 ENCODING_MAP(kMipsNor, 0x00000027, /* used for "not" too */
225 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16,
226 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
227 "nor", "!0r,!1r,!2r", 4),
228 ENCODING_MAP(kMipsOr, 0x00000025,
229 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16,
230 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
231 "or", "!0r,!1r,!2r", 4),
232 ENCODING_MAP(kMipsOri, 0x34000000,
233 kFmtBitBlt, 20, 16, kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0,
234 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
235 "ori", "!0r,!1r,0x!2h(!2d)", 4),
236 ENCODING_MAP(kMipsPref, 0xCC000000,
237 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
238 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE2,
239 "pref", "!0d,!1d(!2r)", 4),
240 ENCODING_MAP(kMipsSb, 0xA0000000,
241 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
242 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE02 | IS_STORE,
243 "sb", "!0r,!1d(!2r)", 4),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700244 ENCODING_MAP(kMipsSeb, 0x7c000420,
245 kFmtBitBlt, 15, 11, kFmtBitBlt, 20, 16, kFmtUnused, -1, -1,
246 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
247 "seb", "!0r,!1r", 4),
248 ENCODING_MAP(kMipsSeh, 0x7c000620,
249 kFmtBitBlt, 15, 11, kFmtBitBlt, 20, 16, kFmtUnused, -1, -1,
250 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
251 "seh", "!0r,!1r", 4),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 ENCODING_MAP(kMipsSh, 0xA4000000,
253 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
254 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE02 | IS_STORE,
255 "sh", "!0r,!1d(!2r)", 4),
256 ENCODING_MAP(kMipsSll, 0x00000000,
257 kFmtBitBlt, 15, 11, kFmtBitBlt, 20, 16, kFmtBitBlt, 10, 6,
258 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
259 "sll", "!0r,!1r,0x!2h(!2d)", 4),
260 ENCODING_MAP(kMipsSllv, 0x00000004,
261 kFmtBitBlt, 15, 11, kFmtBitBlt, 20, 16, kFmtBitBlt, 25, 21,
262 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
263 "sllv", "!0r,!1r,!2r", 4),
264 ENCODING_MAP(kMipsSlt, 0x0000002a,
265 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16,
266 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
267 "slt", "!0r,!1r,!2r", 4),
268 ENCODING_MAP(kMipsSlti, 0x28000000,
269 kFmtBitBlt, 20, 16, kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0,
270 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
271 "slti", "!0r,!1r,0x!2h(!2d)", 4),
272 ENCODING_MAP(kMipsSltu, 0x0000002b,
273 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16,
274 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
275 "sltu", "!0r,!1r,!2r", 4),
276 ENCODING_MAP(kMipsSra, 0x00000003,
277 kFmtBitBlt, 15, 11, kFmtBitBlt, 20, 16, kFmtBitBlt, 10, 6,
278 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
279 "sra", "!0r,!1r,0x!2h(!2d)", 4),
280 ENCODING_MAP(kMipsSrav, 0x00000007,
281 kFmtBitBlt, 15, 11, kFmtBitBlt, 20, 16, kFmtBitBlt, 25, 21,
282 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
283 "srav", "!0r,!1r,!2r", 4),
284 ENCODING_MAP(kMipsSrl, 0x00000002,
285 kFmtBitBlt, 15, 11, kFmtBitBlt, 20, 16, kFmtBitBlt, 10, 6,
286 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
287 "srl", "!0r,!1r,0x!2h(!2d)", 4),
288 ENCODING_MAP(kMipsSrlv, 0x00000006,
289 kFmtBitBlt, 15, 11, kFmtBitBlt, 20, 16, kFmtBitBlt, 25, 21,
290 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
291 "srlv", "!0r,!1r,!2r", 4),
292 ENCODING_MAP(kMipsSubu, 0x00000023, /* used for "neg" too */
293 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16,
294 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
295 "subu", "!0r,!1r,!2r", 4),
296 ENCODING_MAP(kMipsSw, 0xAC000000,
297 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
298 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE02 | IS_STORE,
299 "sw", "!0r,!1d(!2r)", 4),
300 ENCODING_MAP(kMipsXor, 0x00000026,
301 kFmtBitBlt, 15, 11, kFmtBitBlt, 25, 21, kFmtBitBlt, 20, 16,
302 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
303 "xor", "!0r,!1r,!2r", 4),
304 ENCODING_MAP(kMipsXori, 0x38000000,
305 kFmtBitBlt, 20, 16, kFmtBitBlt, 25, 21, kFmtBitBlt, 15, 0,
306 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE1,
307 "xori", "!0r,!1r,0x!2h(!2d)", 4),
308 ENCODING_MAP(kMipsFadds, 0x46000000,
309 kFmtSfp, 10, 6, kFmtSfp, 15, 11, kFmtSfp, 20, 16,
310 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
311 "add.s", "!0s,!1s,!2s", 4),
312 ENCODING_MAP(kMipsFsubs, 0x46000001,
313 kFmtSfp, 10, 6, kFmtSfp, 15, 11, kFmtSfp, 20, 16,
314 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
315 "sub.s", "!0s,!1s,!2s", 4),
316 ENCODING_MAP(kMipsFmuls, 0x46000002,
317 kFmtSfp, 10, 6, kFmtSfp, 15, 11, kFmtSfp, 20, 16,
318 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
319 "mul.s", "!0s,!1s,!2s", 4),
320 ENCODING_MAP(kMipsFdivs, 0x46000003,
321 kFmtSfp, 10, 6, kFmtSfp, 15, 11, kFmtSfp, 20, 16,
322 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
323 "div.s", "!0s,!1s,!2s", 4),
324 ENCODING_MAP(kMipsFaddd, 0x46200000,
325 kFmtDfp, 10, 6, kFmtDfp, 15, 11, kFmtDfp, 20, 16,
326 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
327 "add.d", "!0S,!1S,!2S", 4),
328 ENCODING_MAP(kMipsFsubd, 0x46200001,
329 kFmtDfp, 10, 6, kFmtDfp, 15, 11, kFmtDfp, 20, 16,
330 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
331 "sub.d", "!0S,!1S,!2S", 4),
332 ENCODING_MAP(kMipsFmuld, 0x46200002,
333 kFmtDfp, 10, 6, kFmtDfp, 15, 11, kFmtDfp, 20, 16,
334 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
335 "mul.d", "!0S,!1S,!2S", 4),
336 ENCODING_MAP(kMipsFdivd, 0x46200003,
337 kFmtDfp, 10, 6, kFmtDfp, 15, 11, kFmtDfp, 20, 16,
338 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE12,
339 "div.d", "!0S,!1S,!2S", 4),
340 ENCODING_MAP(kMipsFcvtsd, 0x46200020,
341 kFmtSfp, 10, 6, kFmtDfp, 15, 11, kFmtUnused, -1, -1,
342 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
343 "cvt.s.d", "!0s,!1S", 4),
344 ENCODING_MAP(kMipsFcvtsw, 0x46800020,
345 kFmtSfp, 10, 6, kFmtSfp, 15, 11, kFmtUnused, -1, -1,
346 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
347 "cvt.s.w", "!0s,!1s", 4),
348 ENCODING_MAP(kMipsFcvtds, 0x46000021,
349 kFmtDfp, 10, 6, kFmtSfp, 15, 11, kFmtUnused, -1, -1,
350 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
351 "cvt.d.s", "!0S,!1s", 4),
352 ENCODING_MAP(kMipsFcvtdw, 0x46800021,
353 kFmtDfp, 10, 6, kFmtSfp, 15, 11, kFmtUnused, -1, -1,
354 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
355 "cvt.d.w", "!0S,!1s", 4),
356 ENCODING_MAP(kMipsFcvtws, 0x46000024,
357 kFmtSfp, 10, 6, kFmtSfp, 15, 11, kFmtUnused, -1, -1,
358 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
359 "cvt.w.s", "!0s,!1s", 4),
360 ENCODING_MAP(kMipsFcvtwd, 0x46200024,
361 kFmtSfp, 10, 6, kFmtDfp, 15, 11, kFmtUnused, -1, -1,
362 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
363 "cvt.w.d", "!0s,!1S", 4),
364 ENCODING_MAP(kMipsFmovs, 0x46000006,
365 kFmtSfp, 10, 6, kFmtSfp, 15, 11, kFmtUnused, -1, -1,
366 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
367 "mov.s", "!0s,!1s", 4),
368 ENCODING_MAP(kMipsFmovd, 0x46200006,
369 kFmtDfp, 10, 6, kFmtDfp, 15, 11, kFmtUnused, -1, -1,
370 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
371 "mov.d", "!0S,!1S", 4),
372 ENCODING_MAP(kMipsFlwc1, 0xC4000000,
373 kFmtSfp, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
374 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE2 | IS_LOAD,
375 "lwc1", "!0s,!1d(!2r)", 4),
376 ENCODING_MAP(kMipsFldc1, 0xD4000000,
377 kFmtDfp, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
378 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_DEF0_USE2 | IS_LOAD,
379 "ldc1", "!0S,!1d(!2r)", 4),
380 ENCODING_MAP(kMipsFswc1, 0xE4000000,
381 kFmtSfp, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
382 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE02 | IS_STORE,
383 "swc1", "!0s,!1d(!2r)", 4),
384 ENCODING_MAP(kMipsFsdc1, 0xF4000000,
385 kFmtDfp, 20, 16, kFmtBitBlt, 15, 0, kFmtBitBlt, 25, 21,
386 kFmtUnused, -1, -1, IS_TERTIARY_OP | REG_USE02 | IS_STORE,
387 "sdc1", "!0S,!1d(!2r)", 4),
388 ENCODING_MAP(kMipsMfc1, 0x44000000,
389 kFmtBitBlt, 20, 16, kFmtSfp, 15, 11, kFmtUnused, -1, -1,
390 kFmtUnused, -1, -1, IS_BINARY_OP | REG_DEF0_USE1,
391 "mfc1", "!0r,!1s", 4),
392 ENCODING_MAP(kMipsMtc1, 0x44800000,
393 kFmtBitBlt, 20, 16, kFmtSfp, 15, 11, kFmtUnused, -1, -1,
394 kFmtUnused, -1, -1, IS_BINARY_OP | REG_USE0 | REG_DEF1,
395 "mtc1", "!0r,!1s", 4),
396 ENCODING_MAP(kMipsDelta, 0x27e00000,
397 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtUnused, 15, 0,
398 kFmtUnused, -1, -1, IS_QUAD_OP | REG_DEF0 | REG_USE_LR |
399 NEEDS_FIXUP, "addiu", "!0r,ra,0x!1h(!1d)", 4),
400 ENCODING_MAP(kMipsDeltaHi, 0x3C000000,
401 kFmtBitBlt, 20, 16, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
402 kFmtUnused, -1, -1, IS_QUAD_OP | REG_DEF0 | NEEDS_FIXUP,
403 "lui", "!0r,0x!1h(!1d)", 4),
404 ENCODING_MAP(kMipsDeltaLo, 0x34000000,
405 kFmtBlt5_2, 16, 21, kFmtBitBlt, 15, 0, kFmtUnused, -1, -1,
406 kFmtUnused, -1, -1, IS_QUAD_OP | REG_DEF0_USE0 | NEEDS_FIXUP,
407 "ori", "!0r,!0r,0x!1h(!1d)", 4),
408 ENCODING_MAP(kMipsCurrPC, 0x04110001,
409 kFmtUnused, -1, -1, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
410 kFmtUnused, -1, -1, NO_OPERAND | IS_BRANCH | REG_DEF_LR,
411 "addiu", "ra,pc,8", 4),
412 ENCODING_MAP(kMipsSync, 0x0000000f,
413 kFmtBitBlt, 10, 6, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
414 kFmtUnused, -1, -1, IS_UNARY_OP,
415 "sync", ";", 4),
416 ENCODING_MAP(kMipsUndefined, 0x64000000,
417 kFmtUnused, -1, -1, kFmtUnused, -1, -1, kFmtUnused, -1, -1,
418 kFmtUnused, -1, -1, NO_OPERAND,
419 "undefined", "", 4),
420};
421
422
423/*
424 * Convert a short-form branch to long form. Hopefully, this won't happen
425 * very often because the PIC sequence is especially unfortunate.
426 *
427 * Orig conditional branch
428 * -----------------------
429 * beq rs,rt,target
430 *
431 * Long conditional branch
432 * -----------------------
433 * bne rs,rt,hop
buzbee2700f7e2014-03-07 09:46:20 -0800434 * bal .+8 ; rRA <- anchor
435 * lui rAT, ((target-anchor) >> 16)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700436 * anchor:
buzbee2700f7e2014-03-07 09:46:20 -0800437 * ori rAT, rAT, ((target-anchor) & 0xffff)
438 * addu rAT, rAT, rRA
Andreas Gampe8d365912015-01-13 11:32:32 -0800439 * jalr rZERO, rAT
Brian Carlstrom7940e442013-07-12 13:46:57 -0700440 * hop:
441 *
442 * Orig unconditional branch
443 * -------------------------
444 * b target
445 *
446 * Long unconditional branch
447 * -----------------------
buzbee2700f7e2014-03-07 09:46:20 -0800448 * bal .+8 ; rRA <- anchor
449 * lui rAT, ((target-anchor) >> 16)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700450 * anchor:
buzbee2700f7e2014-03-07 09:46:20 -0800451 * ori rAT, rAT, ((target-anchor) & 0xffff)
452 * addu rAT, rAT, rRA
Andreas Gampe8d365912015-01-13 11:32:32 -0800453 * jalr rZERO, rAT
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454 *
455 *
456 * NOTE: An out-of-range bal isn't supported because it should
457 * never happen with the current PIC model.
458 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700459void MipsMir2Lir::ConvertShortToLongBranch(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700460 // For conditional branches we'll need to reverse the sense
461 bool unconditional = false;
462 int opcode = lir->opcode;
463 int dalvik_offset = lir->dalvik_offset;
464 switch (opcode) {
465 case kMipsBal:
466 LOG(FATAL) << "long branch and link unsupported";
Ian Rogersfc787ec2014-10-09 21:56:44 -0700467 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700468 case kMipsB:
469 unconditional = true;
470 break;
471 case kMipsBeq: opcode = kMipsBne; break;
472 case kMipsBne: opcode = kMipsBeq; break;
473 case kMipsBeqz: opcode = kMipsBnez; break;
474 case kMipsBgez: opcode = kMipsBltz; break;
475 case kMipsBgtz: opcode = kMipsBlez; break;
476 case kMipsBlez: opcode = kMipsBgtz; break;
477 case kMipsBltz: opcode = kMipsBgez; break;
478 case kMipsBnez: opcode = kMipsBeqz; break;
479 default:
480 LOG(FATAL) << "Unexpected branch kind " << opcode;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700481 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700482 }
483 LIR* hop_target = NULL;
484 if (!unconditional) {
485 hop_target = RawLIR(dalvik_offset, kPseudoTargetLabel);
486 LIR* hop_branch = RawLIR(dalvik_offset, opcode, lir->operands[0],
Andreas Gampe8ebdc2b2015-01-14 12:09:25 -0800487 lir->operands[1], 0, 0, 0, hop_target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700488 InsertLIRBefore(lir, hop_branch);
489 }
490 LIR* curr_pc = RawLIR(dalvik_offset, kMipsCurrPC);
491 InsertLIRBefore(lir, curr_pc);
492 LIR* anchor = RawLIR(dalvik_offset, kPseudoTargetLabel);
buzbee2700f7e2014-03-07 09:46:20 -0800493 LIR* delta_hi = RawLIR(dalvik_offset, kMipsDeltaHi, rAT, 0, WrapPointer(anchor), 0, 0,
buzbee0d829482013-10-11 15:24:55 -0700494 lir->target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700495 InsertLIRBefore(lir, delta_hi);
496 InsertLIRBefore(lir, anchor);
buzbee2700f7e2014-03-07 09:46:20 -0800497 LIR* delta_lo = RawLIR(dalvik_offset, kMipsDeltaLo, rAT, 0, WrapPointer(anchor), 0, 0,
buzbee0d829482013-10-11 15:24:55 -0700498 lir->target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499 InsertLIRBefore(lir, delta_lo);
buzbee2700f7e2014-03-07 09:46:20 -0800500 LIR* addu = RawLIR(dalvik_offset, kMipsAddu, rAT, rAT, rRA);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700501 InsertLIRBefore(lir, addu);
Andreas Gampe8d365912015-01-13 11:32:32 -0800502 LIR* jalr = RawLIR(dalvik_offset, kMipsJalr, rZERO, rAT);
503 InsertLIRBefore(lir, jalr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700504 if (!unconditional) {
505 InsertLIRBefore(lir, hop_target);
506 }
buzbee252254b2013-09-08 16:20:53 -0700507 NopLIR(lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700508}
509
510/*
511 * Assemble the LIR into binary instruction format. Note that we may
512 * discover that pc-relative displacements may not fit the selected
513 * instruction. In those cases we will try to substitute a new code
514 * sequence or request that the trace be shortened and retried.
515 */
buzbee0d829482013-10-11 15:24:55 -0700516AssemblerStatus MipsMir2Lir::AssembleInstructions(CodeOffset start_addr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517 LIR *lir;
518 AssemblerStatus res = kSuccess; // Assume success
519
520 for (lir = first_lir_insn_; lir != NULL; lir = NEXT_LIR(lir)) {
521 if (lir->opcode < 0) {
522 continue;
523 }
524
525
526 if (lir->flags.is_nop) {
527 continue;
528 }
529
buzbeeb48819d2013-09-14 16:15:25 -0700530 if (lir->flags.fixup != kFixupNone) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700531 if (lir->opcode == kMipsDelta) {
532 /*
533 * The "Delta" pseudo-ops load the difference between
534 * two pc-relative locations into a the target register
535 * found in operands[0]. The delta is determined by
536 * (label2 - label1), where label1 is a standard
537 * kPseudoTargetLabel and is stored in operands[2].
538 * If operands[3] is null, then label2 is a kPseudoTargetLabel
539 * and is found in lir->target. If operands[3] is non-NULL,
540 * then it is a Switch/Data table.
541 */
buzbee0d829482013-10-11 15:24:55 -0700542 int offset1 = (reinterpret_cast<LIR*>(UnwrapPointer(lir->operands[2])))->offset;
543 EmbeddedData *tab_rec = reinterpret_cast<EmbeddedData*>(UnwrapPointer(lir->operands[3]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700544 int offset2 = tab_rec ? tab_rec->offset : lir->target->offset;
545 int delta = offset2 - offset1;
546 if ((delta & 0xffff) == delta && ((delta & 0x8000) == 0)) {
547 // Fits
548 lir->operands[1] = delta;
549 } else {
550 // Doesn't fit - must expand to kMipsDelta[Hi|Lo] pair
551 LIR *new_delta_hi =
552 RawLIR(lir->dalvik_offset, kMipsDeltaHi,
553 lir->operands[0], 0, lir->operands[2],
554 lir->operands[3], 0, lir->target);
555 InsertLIRBefore(lir, new_delta_hi);
556 LIR *new_delta_lo =
557 RawLIR(lir->dalvik_offset, kMipsDeltaLo,
558 lir->operands[0], 0, lir->operands[2],
559 lir->operands[3], 0, lir->target);
560 InsertLIRBefore(lir, new_delta_lo);
561 LIR *new_addu =
562 RawLIR(lir->dalvik_offset, kMipsAddu,
buzbee2700f7e2014-03-07 09:46:20 -0800563 lir->operands[0], lir->operands[0], rRA);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700564 InsertLIRBefore(lir, new_addu);
buzbee252254b2013-09-08 16:20:53 -0700565 NopLIR(lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700566 res = kRetryAll;
567 }
568 } else if (lir->opcode == kMipsDeltaLo) {
buzbee0d829482013-10-11 15:24:55 -0700569 int offset1 = (reinterpret_cast<LIR*>(UnwrapPointer(lir->operands[2])))->offset;
570 EmbeddedData *tab_rec = reinterpret_cast<EmbeddedData*>(UnwrapPointer(lir->operands[3]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700571 int offset2 = tab_rec ? tab_rec->offset : lir->target->offset;
572 int delta = offset2 - offset1;
573 lir->operands[1] = delta & 0xffff;
574 } else if (lir->opcode == kMipsDeltaHi) {
buzbee0d829482013-10-11 15:24:55 -0700575 int offset1 = (reinterpret_cast<LIR*>(UnwrapPointer(lir->operands[2])))->offset;
576 EmbeddedData *tab_rec = reinterpret_cast<EmbeddedData*>(UnwrapPointer(lir->operands[3]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700577 int offset2 = tab_rec ? tab_rec->offset : lir->target->offset;
578 int delta = offset2 - offset1;
579 lir->operands[1] = (delta >> 16) & 0xffff;
580 } else if (lir->opcode == kMipsB || lir->opcode == kMipsBal) {
581 LIR *target_lir = lir->target;
buzbee0d829482013-10-11 15:24:55 -0700582 CodeOffset pc = lir->offset + 4;
583 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700584 int delta = target - pc;
585 if (delta & 0x3) {
586 LOG(FATAL) << "PC-rel offset not multiple of 4: " << delta;
587 }
588 if (delta > 131068 || delta < -131069) {
589 res = kRetryAll;
590 ConvertShortToLongBranch(lir);
591 } else {
592 lir->operands[0] = delta >> 2;
593 }
594 } else if (lir->opcode >= kMipsBeqz && lir->opcode <= kMipsBnez) {
595 LIR *target_lir = lir->target;
buzbee0d829482013-10-11 15:24:55 -0700596 CodeOffset pc = lir->offset + 4;
597 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700598 int delta = target - pc;
599 if (delta & 0x3) {
600 LOG(FATAL) << "PC-rel offset not multiple of 4: " << delta;
601 }
602 if (delta > 131068 || delta < -131069) {
603 res = kRetryAll;
604 ConvertShortToLongBranch(lir);
605 } else {
606 lir->operands[1] = delta >> 2;
607 }
608 } else if (lir->opcode == kMipsBeq || lir->opcode == kMipsBne) {
609 LIR *target_lir = lir->target;
buzbee0d829482013-10-11 15:24:55 -0700610 CodeOffset pc = lir->offset + 4;
611 CodeOffset target = target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612 int delta = target - pc;
613 if (delta & 0x3) {
614 LOG(FATAL) << "PC-rel offset not multiple of 4: " << delta;
615 }
616 if (delta > 131068 || delta < -131069) {
617 res = kRetryAll;
618 ConvertShortToLongBranch(lir);
619 } else {
620 lir->operands[2] = delta >> 2;
621 }
622 } else if (lir->opcode == kMipsJal) {
buzbee0d829482013-10-11 15:24:55 -0700623 CodeOffset cur_pc = (start_addr + lir->offset + 4) & ~3;
624 CodeOffset target = lir->operands[0];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700625 /* ensure PC-region branch can be used */
626 DCHECK_EQ((cur_pc & 0xF0000000), (target & 0xF0000000));
627 if (target & 0x3) {
628 LOG(FATAL) << "Jump target not multiple of 4: " << target;
629 }
630 lir->operands[0] = target >> 2;
631 } else if (lir->opcode == kMipsLahi) { /* ld address hi (via lui) */
632 LIR *target_lir = lir->target;
buzbee0d829482013-10-11 15:24:55 -0700633 CodeOffset target = start_addr + target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700634 lir->operands[1] = target >> 16;
635 } else if (lir->opcode == kMipsLalo) { /* ld address lo (via ori) */
636 LIR *target_lir = lir->target;
buzbee0d829482013-10-11 15:24:55 -0700637 CodeOffset target = start_addr + target_lir->offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700638 lir->operands[2] = lir->operands[2] + target;
639 }
640 }
641
642 /*
643 * If one of the pc-relative instructions expanded we'll have
644 * to make another pass. Don't bother to fully assemble the
645 * instruction.
646 */
647 if (res != kSuccess) {
648 continue;
649 }
buzbee409fe942013-10-11 10:49:56 -0700650 DCHECK(!IsPseudoLirOp(lir->opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651 const MipsEncodingMap *encoder = &EncodingMap[lir->opcode];
652 uint32_t bits = encoder->skeleton;
653 int i;
654 for (i = 0; i < 4; i++) {
655 uint32_t operand;
656 uint32_t value;
657 operand = lir->operands[i];
658 switch (encoder->field_loc[i].kind) {
659 case kFmtUnused:
660 break;
661 case kFmtBitBlt:
662 if (encoder->field_loc[i].start == 0 && encoder->field_loc[i].end == 31) {
663 value = operand;
664 } else {
665 value = (operand << encoder->field_loc[i].start) &
666 ((1 << (encoder->field_loc[i].end + 1)) - 1);
667 }
668 bits |= value;
669 break;
670 case kFmtBlt5_2:
671 value = (operand & 0x1f);
672 bits |= (value << encoder->field_loc[i].start);
673 bits |= (value << encoder->field_loc[i].end);
674 break;
675 case kFmtDfp: {
buzbee091cc402014-03-31 10:14:40 -0700676 // TODO: do we need to adjust now that we're using 64BitSolo?
677 DCHECK(RegStorage::IsDouble(operand)) << ", Operand = 0x" << std::hex << operand;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700678 DCHECK_EQ((operand & 0x1), 0U);
buzbee091cc402014-03-31 10:14:40 -0700679 value = (RegStorage::RegNum(operand) << encoder->field_loc[i].start) &
Brian Carlstrom7940e442013-07-12 13:46:57 -0700680 ((1 << (encoder->field_loc[i].end + 1)) - 1);
681 bits |= value;
682 break;
683 }
684 case kFmtSfp:
buzbee091cc402014-03-31 10:14:40 -0700685 DCHECK(RegStorage::IsSingle(operand)) << ", Operand = 0x" << std::hex << operand;
686 value = (RegStorage::RegNum(operand) << encoder->field_loc[i].start) &
Brian Carlstrom7940e442013-07-12 13:46:57 -0700687 ((1 << (encoder->field_loc[i].end + 1)) - 1);
688 bits |= value;
689 break;
690 default:
691 LOG(FATAL) << "Bad encoder format: " << encoder->field_loc[i].kind;
692 }
693 }
694 // We only support little-endian MIPS.
695 code_buffer_.push_back(bits & 0xff);
696 code_buffer_.push_back((bits >> 8) & 0xff);
697 code_buffer_.push_back((bits >> 16) & 0xff);
698 code_buffer_.push_back((bits >> 24) & 0xff);
699 // TUNING: replace with proper delay slot handling
700 if (encoder->size == 8) {
buzbee409fe942013-10-11 10:49:56 -0700701 DCHECK(!IsPseudoLirOp(lir->opcode));
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800702 const MipsEncodingMap *encoder2 = &EncodingMap[kMipsNop];
703 uint32_t bits2 = encoder2->skeleton;
704 code_buffer_.push_back(bits2 & 0xff);
705 code_buffer_.push_back((bits2 >> 8) & 0xff);
706 code_buffer_.push_back((bits2 >> 16) & 0xff);
707 code_buffer_.push_back((bits2 >> 24) & 0xff);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700708 }
709 }
710 return res;
711}
712
Ian Rogers5aa6e042014-06-13 16:38:24 -0700713size_t MipsMir2Lir::GetInsnSize(LIR* lir) {
buzbee409fe942013-10-11 10:49:56 -0700714 DCHECK(!IsPseudoLirOp(lir->opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700715 return EncodingMap[lir->opcode].size;
716}
717
buzbeeb48819d2013-09-14 16:15:25 -0700718// LIR offset assignment.
719// TODO: consolidate w/ Arm assembly mechanism.
720int MipsMir2Lir::AssignInsnOffsets() {
721 LIR* lir;
722 int offset = 0;
723
724 for (lir = first_lir_insn_; lir != NULL; lir = NEXT_LIR(lir)) {
725 lir->offset = offset;
726 if (LIKELY(lir->opcode >= 0)) {
727 if (!lir->flags.is_nop) {
728 offset += lir->flags.size;
729 }
730 } else if (UNLIKELY(lir->opcode == kPseudoPseudoAlign4)) {
731 if (offset & 0x2) {
732 offset += 2;
733 lir->operands[0] = 1;
734 } else {
735 lir->operands[0] = 0;
736 }
737 }
738 /* Pseudo opcodes don't consume space */
739 }
740 return offset;
741}
742
743/*
744 * Walk the compilation unit and assign offsets to instructions
745 * and literals and compute the total size of the compiled unit.
746 * TODO: consolidate w/ Arm assembly mechanism.
747 */
748void MipsMir2Lir::AssignOffsets() {
749 int offset = AssignInsnOffsets();
750
751 /* Const values have to be word aligned */
Andreas Gampe66018822014-05-05 20:47:19 -0700752 offset = RoundUp(offset, 4);
buzbeeb48819d2013-09-14 16:15:25 -0700753
754 /* Set up offsets for literals */
755 data_offset_ = offset;
756
757 offset = AssignLiteralOffset(offset);
758
759 offset = AssignSwitchTablesOffset(offset);
760
761 offset = AssignFillArrayDataOffset(offset);
762
763 total_size_ = offset;
764}
765
766/*
767 * Go over each instruction in the list and calculate the offset from the top
768 * before sending them off to the assembler. If out-of-range branch distance is
769 * seen rearrange the instructions a bit to correct it.
770 * TODO: consolidate w/ Arm assembly mechanism.
771 */
772void MipsMir2Lir::AssembleLIR() {
buzbeea61f4952013-08-23 14:27:06 -0700773 cu_->NewTimingSplit("Assemble");
buzbeeb48819d2013-09-14 16:15:25 -0700774 AssignOffsets();
775 int assembler_retries = 0;
776 /*
777 * Assemble here. Note that we generate code with optimistic assumptions
778 * and if found now to work, we'll have to redo the sequence and retry.
779 */
780
781 while (true) {
782 AssemblerStatus res = AssembleInstructions(0);
783 if (res == kSuccess) {
784 break;
785 } else {
786 assembler_retries++;
787 if (assembler_retries > MAX_ASSEMBLER_RETRIES) {
788 CodegenDump();
789 LOG(FATAL) << "Assembler error - too many retries";
790 }
791 // Redo offsets and try again
792 AssignOffsets();
793 code_buffer_.clear();
794 }
795 }
796
797 // Install literals
798 InstallLiteralPools();
799
800 // Install switch tables
801 InstallSwitchTables();
802
803 // Install fill array data
804 InstallFillArrayData();
805
806 // Create the mapping table and native offset to reference map.
buzbeea61f4952013-08-23 14:27:06 -0700807 cu_->NewTimingSplit("PcMappingTable");
buzbeeb48819d2013-09-14 16:15:25 -0700808 CreateMappingTables();
809
buzbeea61f4952013-08-23 14:27:06 -0700810 cu_->NewTimingSplit("GcMap");
buzbeeb48819d2013-09-14 16:15:25 -0700811 CreateNativeGcMap();
812}
813
Brian Carlstrom7940e442013-07-12 13:46:57 -0700814} // namespace art