blob: f874f48ff467fd56bf38a556b161243284017732 [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2009 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 "libdex/OpCode.h"
19#include "dexdump/OpCodeNames.h"
20
21#include "../../CompilerInternals.h"
22#include "Armv5teLIR.h"
23#include <unistd.h> /* for cacheflush */
24
25/*
26 * opcode: Armv5teOpCode enum
27 * skeleton: pre-designated bit-pattern for this opcode
28 * ds: dest start bit position
29 * de: dest end bit position
30 * s1s: src1 start bit position
31 * s1e: src1 end bit position
32 * s2s: src2 start bit position
33 * s2e: src2 end bit position
34 * operands: number of operands (for sanity check purposes)
35 * name: mnemonic name
36 * fmt: for pretty-prining
37 */
38#define ENCODING_MAP(opcode, skeleton, ds, de, s1s, s1e, s2s, s2e, operands, \
39 name, fmt) \
40 {skeleton, {{ds, de}, {s1s, s1e}, {s2s, s2e}}, opcode, operands, name, \
41 fmt}
42
43/* Instruction dump string format keys: !pf, where "!" is the start
44 * of the key, "p" is which numeric operand to use and "f" is the
45 * print format.
46 *
47 * [p]ositions:
48 * 0 -> operands[0] (dest)
49 * 1 -> operands[1] (src1)
50 * 2 -> operands[2] (src2)
51 *
52 * [f]ormats:
53 * h -> 4-digit hex
54 * d -> decimal
55 * D -> decimal+8 (used to convert 3-bit regnum field to high reg)
56 * E -> decimal*4
57 * F -> decimal*2
58 * c -> branch condition (beq, bne, etc.)
59 * t -> pc-relative target
60 * u -> 1st half of bl[x] target
61 * v -> 2nd half ob bl[x] target
62 * R -> register list
63 *
64 * [!] escape. To insert "!", use "!!"
65 */
66/* NOTE: must be kept in sync with enum Armv5teOpcode from Armv5teLIR.h */
67Armv5teEncodingMap EncodingMap[ARMV5TE_LAST] = {
68 ENCODING_MAP(ARMV5TE_16BIT_DATA, 0x0000, 15, 0, -1, -1, -1, -1,
69 1, "data", "0x!0h(!0d)"),
70 ENCODING_MAP(ARMV5TE_ADC, 0x4140, 2, 0, 5, 3, -1, -1,
71 2, "adc", "r!0d, r!1d"),
72 ENCODING_MAP(ARMV5TE_ADD_RRI3, 0x1c00, 2, 0, 5, 3, 8, 6,
73 3, "add", "r!0d, r!1d, #!2d"),
74 ENCODING_MAP(ARMV5TE_ADD_RI8, 0x3000, 10, 8, 7, 0, -1, -1,
75 2, "add", "r!0d, r!0d, #!1d"),
76 ENCODING_MAP(ARMV5TE_ADD_RRR, 0x1800, 2, 0, 5, 3, 8, 6,
77 3, "add", "r!0d, r!1d, r!2d"),
78 ENCODING_MAP(ARMV5TE_ADD_RR_LH, 0x4440, 2, 0, 5, 3, -1, -1,
79 2, "add", "r!0d, r!1d"),
80 ENCODING_MAP(ARMV5TE_ADD_RR_HL, 0x4480, 2, 0, 5, 3, -1, -1,
81 2, "add", "r!0d, r!1d"),
82 ENCODING_MAP(ARMV5TE_ADD_RR_HH, 0x44c0, 2, 0, 5, 3, -1, -1,
83 2, "add", "r!0d, r!1d"),
84 ENCODING_MAP(ARMV5TE_ADD_PC_REL, 0xa000, 10, 8, 7, 0, -1, -1,
85 2, "add", "r!0d, pc, #!1E"),
86 ENCODING_MAP(ARMV5TE_ADD_SP_REL, 0xa800, 10, 8, 7, 0, -1, -1,
87 2, "add", "r!0d, sp, #!1E"),
88 ENCODING_MAP(ARMV5TE_ADD_SPI7, 0xb000, 6, 0, -1, -1, -1, -1,
89 1, "add", "sp, #!0d*4"),
90 ENCODING_MAP(ARMV5TE_AND_RR, 0x4000, 2, 0, 5, 3, -1, -1,
91 2, "and", "r!0d, r!1d"),
92 ENCODING_MAP(ARMV5TE_ASR, 0x1000, 2, 0, 5, 3, 10, 6,
93 3, "asr", "r!0d, r!1d, #!2d"),
94 ENCODING_MAP(ARMV5TE_ASRV, 0x4100, 2, 0, 5, 3, -1, -1,
95 2, "asr", "r!0d, r!1d"),
96 ENCODING_MAP(ARMV5TE_B_COND, 0xd000, 7, 0, 11, 8, -1, -1,
97 2, "!1c", "!0t"),
98 ENCODING_MAP(ARMV5TE_B_UNCOND, 0xe000, 10, 0, -1, -1, -1, -1,
99 0, "b", "!0t"),
100 ENCODING_MAP(ARMV5TE_BIC, 0x4380, 2, 0, 5, 3, -1, -1,
101 2, "bic", "r!0d, r!1d"),
102 ENCODING_MAP(ARMV5TE_BKPT, 0xbe00, 7, 0, -1, -1, -1, -1,
103 1, "bkpt", "!0d"),
104 ENCODING_MAP(ARMV5TE_BLX_1, 0xf000, 10, 0, -1, -1, -1, -1,
105 2, "blx_1", "!0u"),
106 ENCODING_MAP(ARMV5TE_BLX_2, 0xe800, 10, 0, -1, -1, -1, -1,
107 2, "blx_2", "!0v"),
108 ENCODING_MAP(ARMV5TE_BL_1, 0xf000, 10, 0, -1, -1, -1, -1,
109 1, "bl_1", "!0u"),
110 ENCODING_MAP(ARMV5TE_BL_2, 0xf800, 10, 0, -1, -1, -1, -1,
111 1, "bl_2", "!0v"),
112 ENCODING_MAP(ARMV5TE_BLX_R, 0x4780, 6, 3, -1, -1, -1, -1,
113 1, "blx", "r!0d"),
114 ENCODING_MAP(ARMV5TE_BX, 0x4700, 6, 3, -1, -1, -1, -1,
115 1, "bx", "r!0d"),
116 ENCODING_MAP(ARMV5TE_CMN, 0x42c0, 2, 0, 5, 3, -1, -1,
117 2, "cmn", "r!0d, r!1d"),
118 ENCODING_MAP(ARMV5TE_CMP_RI8, 0x2800, 10, 8, 7, 0, -1, -1,
119 2, "cmp", "r!0d, #!1d"),
120 ENCODING_MAP(ARMV5TE_CMP_RR, 0x4280, 2, 0, 5, 3, -1, -1,
121 2, "cmp", "r!0d, r!1d"),
122 ENCODING_MAP(ARMV5TE_CMP_LH, 0x4540, 2, 0, 5, 3, -1, -1,
123 2, "cmp", "r!0d, r!1D"),
124 ENCODING_MAP(ARMV5TE_CMP_HL, 0x4580, 2, 0, 5, 3, -1, -1,
125 2, "cmp", "r!0D, r!1d"),
126 ENCODING_MAP(ARMV5TE_CMP_HH, 0x45c0, 2, 0, 5, 3, -1, -1,
127 2, "cmp", "r!0D, r!1D"),
128 ENCODING_MAP(ARMV5TE_EOR, 0x4040, 2, 0, 5, 3, -1, -1,
129 2, "eor", "r!0d, r!1d"),
130 ENCODING_MAP(ARMV5TE_LDMIA, 0xc800, 10, 8, 7, 0, -1, -1,
131 2, "ldmia", "r!0d!!, <!1R>"),
132 ENCODING_MAP(ARMV5TE_LDR_RRI5, 0x6800, 2, 0, 5, 3, 10, 6,
133 3, "ldr", "r!0d, [r!1d, #!2E]"),
134 ENCODING_MAP(ARMV5TE_LDR_RRR, 0x5800, 2, 0, 5, 3, 8, 6,
135 3, "ldr", "r!0d, [r!1d, r!2d]"),
136 ENCODING_MAP(ARMV5TE_LDR_PC_REL, 0x4800, 10, 8, 7, 0, -1, -1,
137 2, "ldr", "r!0d, [pc, #!1E]"),
138 ENCODING_MAP(ARMV5TE_LDR_SP_REL, 0x9800, 10, 8, 7, 0, -1, -1,
139 2, "ldr", "r!0d, [sp, #!1E]"),
140 ENCODING_MAP(ARMV5TE_LDRB_RRI5, 0x7800, 2, 0, 5, 3, 10, 6,
141 3, "ldrb", "r!0d, [r!1d, #2d]"),
142 ENCODING_MAP(ARMV5TE_LDRB_RRR, 0x5c00, 2, 0, 5, 3, 8, 6,
143 3, "ldrb", "r!0d, [r!1d, r!2d]"),
144 ENCODING_MAP(ARMV5TE_LDRH_RRI5, 0x8800, 2, 0, 5, 3, 10, 6,
145 3, "ldrh", "r!0d, [r!1d, #!2F]"),
146 ENCODING_MAP(ARMV5TE_LDRH_RRR, 0x5a00, 2, 0, 5, 3, 8, 6,
147 3, "ldrh", "r!0d, [r!1d, r!2d]"),
148 ENCODING_MAP(ARMV5TE_LDRSB_RRR, 0x5600, 2, 0, 5, 3, 8, 6,
149 3, "ldrsb", "r!0d, [r!1d, r!2d]"),
150 ENCODING_MAP(ARMV5TE_LDRSH_RRR, 0x5e00, 2, 0, 5, 3, 8, 6,
151 3, "ldrsh", "r!0d, [r!1d, r!2d]"),
152 ENCODING_MAP(ARMV5TE_LSL, 0x0000, 2, 0, 5, 3, 10, 6,
153 3, "lsl", "r!0d, r!1d, #!2d"),
154 ENCODING_MAP(ARMV5TE_LSLV, 0x4080, 2, 0, 5, 3, -1, -1,
155 2, "lsl", "r!0d, r!1d"),
156 ENCODING_MAP(ARMV5TE_LSR, 0x0800, 2, 0, 5, 3, 10, 6,
157 3, "lsr", "r!0d, r!1d, #!2d"),
158 ENCODING_MAP(ARMV5TE_LSRV, 0x40c0, 2, 0, 5, 3, -1, -1,
159 2, "lsr", "r!0d, r!1d"),
160 ENCODING_MAP(ARMV5TE_MOV_IMM, 0x2000, 10, 8, 7, 0, -1, -1,
161 2, "mov", "r!0d, #!1d"),
162 ENCODING_MAP(ARMV5TE_MOV_RR, 0x1c00, 2, 0, 5, 3, -1, -1,
163 2, "mov", "r!0d, r!1d"),
164 ENCODING_MAP(ARMV5TE_MOV_RR_LH, 0x4640, 2, 0, 5, 3, -1, -1,
165 2, "mov", "r!0D, r!1d"),
166 ENCODING_MAP(ARMV5TE_MOV_RR_HL, 0x4680, 2, 0, 5, 3, -1, -1,
167 2, "mov", "r!0d, r!1D"),
168 ENCODING_MAP(ARMV5TE_MOV_RR_HH, 0x46c0, 2, 0, 5, 3, -1, -1,
169 2, "mov", "r!0D, r!1D"),
170 ENCODING_MAP(ARMV5TE_MUL, 0x4340, 2, 0, 5, 3, -1, -1,
171 2, "mul", "r!0d, r!1d"),
172 ENCODING_MAP(ARMV5TE_MVN, 0x43c0, 2, 0, 5, 3, -1, -1,
173 2, "mvn", "r!0d, r!1d"),
174 ENCODING_MAP(ARMV5TE_NEG, 0x4240, 2, 0, 5, 3, -1, -1,
175 2, "neg", "r!0d, r!1d"),
176 ENCODING_MAP(ARMV5TE_ORR, 0x4300, 2, 0, 5, 3, -1, -1,
177 2, "orr", "r!0d, r!1d"),
178 ENCODING_MAP(ARMV5TE_POP, 0xbc00, 8, 0, -1, -1, -1, -1,
179 1, "pop", "<!0R>"),
180 ENCODING_MAP(ARMV5TE_PUSH, 0xb400, 8, 0, -1, -1, -1, -1,
181 1, "push", "<!0R>"),
182 ENCODING_MAP(ARMV5TE_ROR, 0x41c0, 2, 0, 5, 3, -1, -1,
183 2, "ror", "r!0d, r!1d"),
184 ENCODING_MAP(ARMV5TE_SBC, 0x4180, 2, 0, 5, 3, -1, -1,
185 2, "sbc", "r!0d, r!1d"),
186 ENCODING_MAP(ARMV5TE_STMIA, 0xc000, 10, 8, 7, 0, -1, -1,
187 2, "stmia", "r!0d!!, <!1R>"),
188 ENCODING_MAP(ARMV5TE_STR_RRI5, 0x6000, 2, 0, 5, 3, 10, 6,
189 3, "str", "r!0d, [r!1d, #!2E]"),
190 ENCODING_MAP(ARMV5TE_STR_RRR, 0x5000, 2, 0, 5, 3, 8, 6,
191 3, "str", "r!0d, [r!1d, r!2d]"),
192 ENCODING_MAP(ARMV5TE_STR_SP_REL, 0x9000, 10, 8, 7, 0, -1, -1,
193 2, "str", "r!0d, [sp, #!1E]"),
194 ENCODING_MAP(ARMV5TE_STRB_RRI5, 0x7000, 2, 0, 5, 3, 10, 6,
195 3, "strb", "r!0d, [r!1d, #!2d]"),
196 ENCODING_MAP(ARMV5TE_STRB_RRR, 0x5400, 2, 0, 5, 3, 8, 6,
197 3, "strb", "r!0d, [r!1d, r!2d]"),
198 ENCODING_MAP(ARMV5TE_STRH_RRI5, 0x8000, 2, 0, 5, 3, 10, 6,
199 3, "strh", "r!0d, [r!1d, #!2F]"),
200 ENCODING_MAP(ARMV5TE_STRH_RRR, 0x5200, 2, 0, 5, 3, 8, 6,
201 3, "strh", "r!0d, [r!1d, r!2d]"),
202 ENCODING_MAP(ARMV5TE_SUB_RRI3, 0x1e00, 2, 0, 5, 3, 8, 6,
203 3, "sub", "r!0d, r!1d, #!2d]"),
204 ENCODING_MAP(ARMV5TE_SUB_RI8, 0x3800, 10, 8, 7, 0, -1, -1,
205 2, "sub", "r!0d, #!1d"),
206 ENCODING_MAP(ARMV5TE_SUB_RRR, 0x1a00, 2, 0, 5, 3, 8, 6,
207 3, "sub", "r!0d, r!1d, r!2d"),
208 ENCODING_MAP(ARMV5TE_SUB_SPI7, 0xb080, 6, 0, -1, -1, -1, -1,
209 1, "sub", "sp, #!0d"),
210 ENCODING_MAP(ARMV5TE_SWI, 0xdf00, 7, 0, -1, -1, -1, -1,
211 1, "swi", "!0d"),
212 ENCODING_MAP(ARMV5TE_TST, 0x4200, 2, 0, 5, 3, -1, -1,
213 1, "tst", "r!0d, r!1d"),
214};
215
216#define PADDING_MOV_R0_R0 0x1C00
217
218/* Write the numbers in the literal pool to the codegen stream */
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700219static void installDataContent(CompilationUnit *cUnit)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700220{
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700221 int *dataPtr = (int *) (cUnit->baseAddr + cUnit->dataOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700222 Armv5teLIR *dataLIR = (Armv5teLIR *) cUnit->wordList;
223 while (dataLIR) {
224 *dataPtr++ = dataLIR->operands[0];
225 dataLIR = NEXT_LIR(dataLIR);
226 }
227}
228
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700229/* Returns the size of a Jit trace description */
230static int jitTraceDescriptionSize(const JitTraceDescription *desc)
231{
232 int runCount;
233 for (runCount = 0; ; runCount++) {
234 if (desc->trace[runCount].frag.runEnd)
235 break;
236 }
237 return sizeof(JitCodeDesc) + ((runCount+1) * sizeof(JitTraceRun));
238}
239
Ben Chengba4fc8b2009-06-01 13:00:29 -0700240/* Return TRUE if error happens */
241static bool assembleInstructions(CompilationUnit *cUnit, intptr_t startAddr)
242{
243 short *bufferAddr = (short *) cUnit->codeBuffer;
244 Armv5teLIR *lir;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700245
246 for (lir = (Armv5teLIR *) cUnit->firstLIRInsn; lir; lir = NEXT_LIR(lir)) {
247 if (lir->opCode < 0) {
248 if ((lir->opCode == ARMV5TE_PSEUDO_ALIGN4) &&
Ben Cheng1efc9c52009-06-08 18:25:27 -0700249 /* 1 means padding is needed */
250 (lir->operands[0] == 1)) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700251 *bufferAddr++ = PADDING_MOV_R0_R0;
252 }
253 continue;
254 }
255
256 if (lir->opCode == ARMV5TE_LDR_PC_REL ||
257 lir->opCode == ARMV5TE_ADD_PC_REL) {
258 Armv5teLIR *lirTarget = (Armv5teLIR *) lir->generic.target;
259 intptr_t pc = (lir->generic.offset + 4) & ~3;
260 intptr_t target = lirTarget->generic.offset;
261 int delta = target - pc;
262 if (delta & 0x3) {
263 LOGE("PC-rel distance is not multiples of 4: %d\n", delta);
264 dvmAbort();
265 }
Ben Cheng1efc9c52009-06-08 18:25:27 -0700266 if (delta > 1023) {
267 return true;
268 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700269 lir->operands[1] = delta >> 2;
270 } else if (lir->opCode == ARMV5TE_B_COND) {
271 Armv5teLIR *targetLIR = (Armv5teLIR *) lir->generic.target;
272 intptr_t pc = lir->generic.offset + 4;
273 intptr_t target = targetLIR->generic.offset;
274 int delta = target - pc;
275 if (delta > 254 || delta < -256) {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700276 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700277 }
278 lir->operands[0] = delta >> 1;
279 } else if (lir->opCode == ARMV5TE_B_UNCOND) {
280 Armv5teLIR *targetLIR = (Armv5teLIR *) lir->generic.target;
281 intptr_t pc = lir->generic.offset + 4;
282 intptr_t target = targetLIR->generic.offset;
283 int delta = target - pc;
284 if (delta > 2046 || delta < -2048) {
285 LOGE("Unconditional branch distance out of range: %d\n", delta);
286 dvmAbort();
287 }
288 lir->operands[0] = delta >> 1;
289 } else if (lir->opCode == ARMV5TE_BLX_1) {
290 assert(NEXT_LIR(lir)->opCode == ARMV5TE_BLX_2);
291 /* curPC is Thumb */
292 intptr_t curPC = (startAddr + lir->generic.offset + 4) & ~3;
293 intptr_t target = lir->operands[1];
294
295 /* Match bit[1] in target with base */
296 if (curPC & 0x2) {
297 target |= 0x2;
298 }
299 int delta = target - curPC;
300 assert((delta >= -(1<<22)) && (delta <= ((1<<22)-2)));
301
302 lir->operands[0] = (delta >> 12) & 0x7ff;
303 NEXT_LIR(lir)->operands[0] = (delta>> 1) & 0x7ff;
304 }
305
Ben Chengba4fc8b2009-06-01 13:00:29 -0700306 Armv5teEncodingMap *encoder = &EncodingMap[lir->opCode];
307 short bits = encoder->skeleton;
308 int i;
309 for (i = 0; i < 3; i++) {
310 short value;
311 if (encoder->fieldLoc[i].end != -1) {
312 value = (lir->operands[i] << encoder->fieldLoc[i].start) &
313 ((1 << (encoder->fieldLoc[i].end + 1)) - 1);
314 bits |= value;
315
316 }
317 }
318 *bufferAddr++ = bits;
319 }
Ben Cheng1efc9c52009-06-08 18:25:27 -0700320 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700321}
322
323/*
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700324 * Translation layout in the code cache. Note that the codeAddress pointer
325 * in JitTable will point directly to the code body (field codeAddress). The
326 * chain cell offset codeAddress - 2, and (if present) executionCount is at
327 * codeAddress - 6.
328 *
329 * +----------------------------+
330 * | Execution count | -> [Optional] 4 bytes
331 * +----------------------------+
332 * +--| Offset to chain cell counts| -> 2 bytes
333 * | +----------------------------+
334 * | | Code body | -> Start address for translation
335 * | | | variable in 2-byte chunks
336 * | . . (JitTable's codeAddress points here)
337 * | . .
338 * | | |
339 * | +----------------------------+
340 * | | Chaining Cells | -> 8 bytes each, must be 4 byte aligned
341 * | . .
342 * | . .
343 * | | |
344 * | +----------------------------+
345 * +->| Chaining cell counts | -> 4 bytes, chain cell counts by type
346 * +----------------------------+
347 * | Trace description | -> variable sized
348 * . .
349 * | |
350 * +----------------------------+
351 * | Literal pool | -> 4-byte aligned, variable size
352 * . .
353 * . .
354 * | |
355 * +----------------------------+
356 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700357 * Go over each instruction in the list and calculate the offset from the top
358 * before sending them off to the assembler. If out-of-range branch distance is
359 * seen rearrange the instructions a bit to correct it.
360 */
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700361#define CHAIN_CELL_OFFSET_SIZE 2
Ben Chengba4fc8b2009-06-01 13:00:29 -0700362void dvmCompilerAssembleLIR(CompilationUnit *cUnit)
363{
364 LIR *lir;
365 Armv5teLIR *armLIR;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700366 int offset = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700367 int i;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700368 ChainCellCounts chainCellCounts;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700369 int descSize = jitTraceDescriptionSize(cUnit->traceDesc);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700370
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700371 /* Beginning offset needs to allow space for chain cell offset */
Ben Cheng1efc9c52009-06-08 18:25:27 -0700372 for (armLIR = (Armv5teLIR *) cUnit->firstLIRInsn;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700373 armLIR;
374 armLIR = NEXT_LIR(armLIR)) {
375 armLIR->generic.offset = offset;
376 if (armLIR->opCode >= 0) {
377 offset += 2;
378 } else if (armLIR->opCode == ARMV5TE_PSEUDO_ALIGN4) {
379 if (offset & 0x2) {
380 offset += 2;
381 armLIR->operands[0] = 1;
382 } else {
383 armLIR->operands[0] = 0;
384 }
385 }
386 /* Pseudo opcodes don't consume space */
387 }
388
389 /* Const values have to be word aligned */
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700390 offset = (offset + 3) & ~3;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700391
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700392 /* Add space for chain cell counts & trace description */
Ben Cheng1efc9c52009-06-08 18:25:27 -0700393 u4 chainCellOffset = offset;
394 Armv5teLIR *chainCellOffsetLIR = (Armv5teLIR *) (cUnit->firstLIRInsn);
395 assert(chainCellOffset < 0x10000);
396 assert(chainCellOffsetLIR->opCode == ARMV5TE_16BIT_DATA &&
397 chainCellOffsetLIR->operands[0] == CHAIN_CELL_OFFSET_TAG);
398
399 /* Replace the CHAIN_CELL_OFFSET_TAG with the real value */
400 chainCellOffsetLIR->operands[0] = chainCellOffset;
401
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700402 offset += sizeof(chainCellCounts) + descSize;
403
404 assert((offset & 0x3) == 0); /* Should still be word aligned */
405
406 /* Set up offsets for literals */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700407 cUnit->dataOffset = offset;
408
409 for (lir = cUnit->wordList; lir; lir = lir->next) {
410 lir->offset = offset;
411 offset += 4;
412 }
413
414 cUnit->totalSize = offset;
415
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700416 if (gDvmJit.codeCacheByteUsed + cUnit->totalSize > CODE_CACHE_SIZE) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700417 gDvmJit.codeCacheFull = true;
418 cUnit->baseAddr = NULL;
419 return;
420 }
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700421
422 /* Allocate enough space for the code block */
423 cUnit->codeBuffer = dvmCompilerNew(chainCellOffset, true);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700424 if (cUnit->codeBuffer == NULL) {
425 LOGE("Code buffer allocation failure\n");
426 cUnit->baseAddr = NULL;
427 return;
428 }
429
Ben Cheng1efc9c52009-06-08 18:25:27 -0700430 bool assemblerFailure = assembleInstructions(
Ben Chengba4fc8b2009-06-01 13:00:29 -0700431 cUnit, (intptr_t) gDvmJit.codeCache + gDvmJit.codeCacheByteUsed);
432
Ben Cheng1efc9c52009-06-08 18:25:27 -0700433 /*
434 * Currently the only reason that can cause the assembler to fail is due to
435 * trace length - cut it in half and retry.
436 */
437 if (assemblerFailure) {
438 cUnit->halveInstCount = true;
439 return;
440 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700441
Ben Chengba4fc8b2009-06-01 13:00:29 -0700442 cUnit->baseAddr = (char *) gDvmJit.codeCache + gDvmJit.codeCacheByteUsed;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700443 cUnit->headerSize = CHAIN_CELL_OFFSET_SIZE;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700444 gDvmJit.codeCacheByteUsed += offset;
445
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700446 /* Install the code block */
Ben Cheng1efc9c52009-06-08 18:25:27 -0700447 memcpy((char*)cUnit->baseAddr, cUnit->codeBuffer, chainCellOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700448 gDvmJit.numCompilations++;
449
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700450 /* Install the chaining cell counts */
451 for (i=0; i< CHAINING_CELL_LAST; i++) {
452 chainCellCounts.u.count[i] = cUnit->numChainingCells[i];
453 }
454 memcpy((char*)cUnit->baseAddr + chainCellOffset, &chainCellCounts,
455 sizeof(chainCellCounts));
456
457 /* Install the trace description */
458 memcpy((char*)cUnit->baseAddr + chainCellOffset + sizeof(chainCellCounts),
459 cUnit->traceDesc, descSize);
460
461 /* Write the literals directly into the code cache */
462 installDataContent(cUnit);
463
Ben Chengba4fc8b2009-06-01 13:00:29 -0700464 /* Flush dcache and invalidate the icache to maintain coherence */
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700465 cacheflush((long)cUnit->baseAddr,
466 (long)(cUnit->baseAddr + offset), 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700467}
468
469/*
470 * Perform translation chain operation.
471 * For ARM, we'll use a pair of thumb instructions to generate
472 * an unconditional chaining branch of up to 4MB in distance.
473 * Use a BL, though we don't really need the link. The format is
474 * 111HHooooooooooo
475 * Where HH is 10 for the 1st inst, and 11 for the second and
476 * the "o" field is each instruction's 11-bit contribution to the
477 * 22-bit branch offset.
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700478 * If the target is nearby, use a single-instruction bl.
479 * If one or more threads is suspended, don't chain.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700480 */
481void* dvmJitChain(void* tgtAddr, u4* branchAddr)
482{
483 int baseAddr = (u4) branchAddr + 4;
484 int branchOffset = (int) tgtAddr - baseAddr;
485 u4 thumb1;
486 u4 thumb2;
487 u4 newInst;
488
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700489 if (gDvm.sumThreadSuspendCount == 0) {
490 assert((branchOffset >= -(1<<22)) && (branchOffset <= ((1<<22)-2)));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700491
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700492 gDvmJit.translationChains++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700493
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700494 COMPILER_TRACE_CHAINING(
495 LOGD("Jit Runtime: chaining 0x%x to 0x%x\n",
496 (int) branchAddr, (int) tgtAddr & -2));
497 if ((branchOffset < -2048) | (branchOffset > 2046)) {
498 thumb1 = (0xf000 | ((branchOffset>>12) & 0x7ff));
499 thumb2 = (0xf800 | ((branchOffset>> 1) & 0x7ff));
500 } else {
501 thumb1 = (0xe000 | ((branchOffset>> 1) & 0x7ff));
502 thumb2 = 0x4300; /* nop -> or r0, r0 */
503 }
504
505 newInst = thumb2<<16 | thumb1;
506 *branchAddr = newInst;
507 cacheflush((long)branchAddr, (long)branchAddr + 4, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700508 }
509
Ben Chengba4fc8b2009-06-01 13:00:29 -0700510 return tgtAddr;
511}
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700512
513/*
514 * Unchain a trace given the starting address of the translation
515 * in the code cache. Refer to the diagram in dvmCompilerAssembleLIR.
516 * Returns the address following the last cell unchained. Note that
517 * the incoming codeAddr is a thumb code address, and therefore has
518 * the low bit set.
519 */
520u4* dvmJitUnchain(void* codeAddr)
521{
522 u2* pChainCellOffset = (u2*)((char*)codeAddr - 3);
523 u2 chainCellOffset = *pChainCellOffset;
524 ChainCellCounts *pChainCellCounts =
525 (ChainCellCounts*)((char*)codeAddr + chainCellOffset -3);
526 int cellCount;
527 u4* pChainCells;
528 u4* pStart;
529 u4 thumb1;
530 u4 thumb2;
531 u4 newInst;
532 int i,j;
533
534 /* Get total count of chain cells */
535 for (i = 0, cellCount = 0; i < CHAINING_CELL_LAST; i++) {
536 cellCount += pChainCellCounts->u.count[i];
537 }
538
539 /* Locate the beginning of the chain cell region */
540 pStart = pChainCells = (u4*)((char*)pChainCellCounts - (cellCount * 8));
541
542 /* The cells are sorted in order - walk through them and reset */
543 for (i = 0; i < CHAINING_CELL_LAST; i++) {
544 for (j = 0; j < pChainCellCounts->u.count[i]; j++) {
545 int targetOffset;
546 switch(i) {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700547 case CHAINING_CELL_NORMAL:
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700548 targetOffset = offsetof(InterpState,
549 jitToInterpEntries.dvmJitToInterpNormal);
550 break;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700551 case CHAINING_CELL_HOT:
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700552 case CHAINING_CELL_INVOKE:
553 targetOffset = offsetof(InterpState,
554 jitToInterpEntries.dvmJitToTraceSelect);
555 break;
556 default:
557 dvmAbort();
558 }
559 /*
560 * Arm code sequence for a chaining cell is:
561 * ldr r0, rGLUE, #<word offset>
562 * blx r0
563 */
564 COMPILER_TRACE_CHAINING(
565 LOGD("Jit Runtime: unchaining 0x%x", (int)pChainCells));
566 targetOffset = targetOffset >> 2; /* convert to word offset */
567 thumb1 = 0x6800 | (targetOffset << 6) | (rGLUE << 3) | (r0 << 0);
568 thumb2 = 0x4780 | (r0 << 3);
569 newInst = thumb2<<16 | thumb1;
570 *pChainCells = newInst;
571 pChainCells += 2; /* Advance by 2 words */
572 }
573 }
574 return pChainCells;
575}
576
577/* Unchain all translation in the cache. */
578void dvmJitUnchainAll()
579{
580 u4* lowAddress = NULL;
581 u4* highAddress = NULL;
582 unsigned int i;
583 if (gDvmJit.pJitEntryTable != NULL) {
584 COMPILER_TRACE_CHAINING(LOGD("Jit Runtime: unchaining all"));
585 dvmLockMutex(&gDvmJit.tableLock);
Bill Buzbee27176222009-06-09 09:20:16 -0700586 for (i = 0; i < gDvmJit.jitTableSize; i++) {
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700587 if (gDvmJit.pJitEntryTable[i].dPC &&
588 gDvmJit.pJitEntryTable[i].codeAddress) {
589 u4* lastAddress;
590 lastAddress =
591 dvmJitUnchain(gDvmJit.pJitEntryTable[i].codeAddress);
592 if (lowAddress == NULL ||
593 (u4*)gDvmJit.pJitEntryTable[i].codeAddress < lowAddress)
594 lowAddress = lastAddress;
595 if (lastAddress > highAddress)
596 highAddress = lastAddress;
597 }
598 }
599 cacheflush((long)lowAddress, (long)highAddress, 0);
600 dvmUnlockMutex(&gDvmJit.tableLock);
601 }
602}