blob: 9642c7d3e4d44fc94000ad70dd7fec1752eb1837 [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,
Ben Chenge9695e52009-06-16 16:11:47 -070069 IS_UNARY_OP,
70 "data", "0x!0h(!0d)"),
Ben Chengba4fc8b2009-06-01 13:00:29 -070071 ENCODING_MAP(ARMV5TE_ADC, 0x4140, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -070072 IS_BINARY_OP | CLOBBER_DEST,
73 "adc", "r!0d, r!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -070074 ENCODING_MAP(ARMV5TE_ADD_RRI3, 0x1c00, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -070075 IS_TERTIARY_OP | CLOBBER_DEST,
76 "add", "r!0d, r!1d, #!2d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -070077 ENCODING_MAP(ARMV5TE_ADD_RI8, 0x3000, 10, 8, 7, 0, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -070078 IS_BINARY_OP | CLOBBER_DEST,
79 "add", "r!0d, r!0d, #!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -070080 ENCODING_MAP(ARMV5TE_ADD_RRR, 0x1800, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -070081 IS_TERTIARY_OP | CLOBBER_DEST,
82 "add", "r!0d, r!1d, r!2d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -070083 ENCODING_MAP(ARMV5TE_ADD_RR_LH, 0x4440, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -070084 IS_BINARY_OP | CLOBBER_DEST,
85 "add",
86 "r!0d, r!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -070087 ENCODING_MAP(ARMV5TE_ADD_RR_HL, 0x4480, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -070088 IS_BINARY_OP | CLOBBER_DEST,
89 "add", "r!0d, r!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -070090 ENCODING_MAP(ARMV5TE_ADD_RR_HH, 0x44c0, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -070091 IS_BINARY_OP | CLOBBER_DEST,
92 "add", "r!0d, r!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -070093 ENCODING_MAP(ARMV5TE_ADD_PC_REL, 0xa000, 10, 8, 7, 0, -1, -1,
Ben Cheng38329f52009-07-07 14:19:20 -070094 IS_TERTIARY_OP | CLOBBER_DEST,
Ben Chenge9695e52009-06-16 16:11:47 -070095 "add", "r!0d, pc, #!1E"),
Ben Chengba4fc8b2009-06-01 13:00:29 -070096 ENCODING_MAP(ARMV5TE_ADD_SP_REL, 0xa800, 10, 8, 7, 0, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -070097 IS_BINARY_OP | CLOBBER_DEST,
98 "add", "r!0d, sp, #!1E"),
Ben Chengba4fc8b2009-06-01 13:00:29 -070099 ENCODING_MAP(ARMV5TE_ADD_SPI7, 0xb000, 6, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700100 IS_UNARY_OP | CLOBBER_DEST,
101 "add", "sp, #!0d*4"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700102 ENCODING_MAP(ARMV5TE_AND_RR, 0x4000, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700103 IS_BINARY_OP | CLOBBER_DEST,
104 "and", "r!0d, r!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700105 ENCODING_MAP(ARMV5TE_ASR, 0x1000, 2, 0, 5, 3, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700106 IS_TERTIARY_OP | CLOBBER_DEST,
107 "asr", "r!0d, r!1d, #!2d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700108 ENCODING_MAP(ARMV5TE_ASRV, 0x4100, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700109 IS_BINARY_OP | CLOBBER_DEST,
110 "asr", "r!0d, r!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700111 ENCODING_MAP(ARMV5TE_B_COND, 0xd000, 7, 0, 11, 8, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700112 IS_BINARY_OP | IS_BRANCH,
113 "!1c", "!0t"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700114 ENCODING_MAP(ARMV5TE_B_UNCOND, 0xe000, 10, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700115 NO_OPERAND | IS_BRANCH,
116 "b", "!0t"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700117 ENCODING_MAP(ARMV5TE_BIC, 0x4380, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700118 IS_BINARY_OP | CLOBBER_DEST,
119 "bic", "r!0d, r!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700120 ENCODING_MAP(ARMV5TE_BKPT, 0xbe00, 7, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700121 IS_UNARY_OP | IS_BRANCH,
122 "bkpt", "!0d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700123 ENCODING_MAP(ARMV5TE_BLX_1, 0xf000, 10, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700124 IS_BINARY_OP | IS_BRANCH,
125 "blx_1", "!0u"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700126 ENCODING_MAP(ARMV5TE_BLX_2, 0xe800, 10, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700127 IS_BINARY_OP | IS_BRANCH,
128 "blx_2", "!0v"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700129 ENCODING_MAP(ARMV5TE_BL_1, 0xf000, 10, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700130 IS_UNARY_OP | IS_BRANCH,
131 "bl_1", "!0u"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700132 ENCODING_MAP(ARMV5TE_BL_2, 0xf800, 10, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700133 IS_UNARY_OP | IS_BRANCH,
134 "bl_2", "!0v"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700135 ENCODING_MAP(ARMV5TE_BLX_R, 0x4780, 6, 3, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700136 IS_UNARY_OP | IS_BRANCH,
137 "blx", "r!0d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700138 ENCODING_MAP(ARMV5TE_BX, 0x4700, 6, 3, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700139 IS_UNARY_OP | IS_BRANCH,
140 "bx", "r!0d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700141 ENCODING_MAP(ARMV5TE_CMN, 0x42c0, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700142 IS_BINARY_OP,
143 "cmn", "r!0d, r!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700144 ENCODING_MAP(ARMV5TE_CMP_RI8, 0x2800, 10, 8, 7, 0, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700145 IS_BINARY_OP,
146 "cmp", "r!0d, #!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700147 ENCODING_MAP(ARMV5TE_CMP_RR, 0x4280, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700148 IS_BINARY_OP,
149 "cmp", "r!0d, r!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700150 ENCODING_MAP(ARMV5TE_CMP_LH, 0x4540, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700151 IS_BINARY_OP,
152 "cmp", "r!0d, r!1D"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700153 ENCODING_MAP(ARMV5TE_CMP_HL, 0x4580, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700154 IS_BINARY_OP,
155 "cmp", "r!0D, r!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700156 ENCODING_MAP(ARMV5TE_CMP_HH, 0x45c0, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700157 IS_BINARY_OP,
158 "cmp", "r!0D, r!1D"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700159 ENCODING_MAP(ARMV5TE_EOR, 0x4040, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700160 IS_BINARY_OP | CLOBBER_DEST,
161 "eor", "r!0d, r!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700162 ENCODING_MAP(ARMV5TE_LDMIA, 0xc800, 10, 8, 7, 0, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700163 IS_BINARY_OP | CLOBBER_DEST | CLOBBER_SRC1,
164 "ldmia", "r!0d!!, <!1R>"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700165 ENCODING_MAP(ARMV5TE_LDR_RRI5, 0x6800, 2, 0, 5, 3, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700166 IS_TERTIARY_OP | CLOBBER_DEST,
167 "ldr", "r!0d, [r!1d, #!2E]"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700168 ENCODING_MAP(ARMV5TE_LDR_RRR, 0x5800, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700169 IS_TERTIARY_OP | CLOBBER_DEST,
170 "ldr", "r!0d, [r!1d, r!2d]"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700171 ENCODING_MAP(ARMV5TE_LDR_PC_REL, 0x4800, 10, 8, 7, 0, -1, -1,
Ben Cheng38329f52009-07-07 14:19:20 -0700172 IS_TERTIARY_OP | CLOBBER_DEST,
Ben Chenge9695e52009-06-16 16:11:47 -0700173 "ldr", "r!0d, [pc, #!1E]"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700174 ENCODING_MAP(ARMV5TE_LDR_SP_REL, 0x9800, 10, 8, 7, 0, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700175 IS_BINARY_OP | CLOBBER_DEST,
176 "ldr", "r!0d, [sp, #!1E]"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700177 ENCODING_MAP(ARMV5TE_LDRB_RRI5, 0x7800, 2, 0, 5, 3, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700178 IS_TERTIARY_OP | CLOBBER_DEST,
179 "ldrb", "r!0d, [r!1d, #2d]"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700180 ENCODING_MAP(ARMV5TE_LDRB_RRR, 0x5c00, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700181 IS_TERTIARY_OP | CLOBBER_DEST,
182 "ldrb", "r!0d, [r!1d, r!2d]"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700183 ENCODING_MAP(ARMV5TE_LDRH_RRI5, 0x8800, 2, 0, 5, 3, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700184 IS_TERTIARY_OP | CLOBBER_DEST,
185 "ldrh", "r!0d, [r!1d, #!2F]"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700186 ENCODING_MAP(ARMV5TE_LDRH_RRR, 0x5a00, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700187 IS_TERTIARY_OP | CLOBBER_DEST,
188 "ldrh", "r!0d, [r!1d, r!2d]"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700189 ENCODING_MAP(ARMV5TE_LDRSB_RRR, 0x5600, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700190 IS_TERTIARY_OP | CLOBBER_DEST,
191 "ldrsb", "r!0d, [r!1d, r!2d]"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700192 ENCODING_MAP(ARMV5TE_LDRSH_RRR, 0x5e00, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700193 IS_TERTIARY_OP | CLOBBER_DEST,
194 "ldrsh", "r!0d, [r!1d, r!2d]"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700195 ENCODING_MAP(ARMV5TE_LSL, 0x0000, 2, 0, 5, 3, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700196 IS_TERTIARY_OP | CLOBBER_DEST,
197 "lsl", "r!0d, r!1d, #!2d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700198 ENCODING_MAP(ARMV5TE_LSLV, 0x4080, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700199 IS_BINARY_OP | CLOBBER_DEST,
200 "lsl", "r!0d, r!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700201 ENCODING_MAP(ARMV5TE_LSR, 0x0800, 2, 0, 5, 3, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700202 IS_TERTIARY_OP | CLOBBER_DEST,
203 "lsr", "r!0d, r!1d, #!2d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700204 ENCODING_MAP(ARMV5TE_LSRV, 0x40c0, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700205 IS_BINARY_OP | CLOBBER_DEST,
206 "lsr", "r!0d, r!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700207 ENCODING_MAP(ARMV5TE_MOV_IMM, 0x2000, 10, 8, 7, 0, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700208 IS_BINARY_OP | CLOBBER_DEST,
209 "mov", "r!0d, #!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700210 ENCODING_MAP(ARMV5TE_MOV_RR, 0x1c00, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700211 IS_BINARY_OP | CLOBBER_DEST,
212 "mov", "r!0d, r!1d"),
Ben Cheng38329f52009-07-07 14:19:20 -0700213 ENCODING_MAP(ARMV5TE_MOV_RR_H2H, 0x46c0, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700214 IS_BINARY_OP | CLOBBER_DEST,
215 "mov", "r!0D, r!1D"),
Ben Cheng38329f52009-07-07 14:19:20 -0700216 ENCODING_MAP(ARMV5TE_MOV_RR_H2L, 0x4640, 2, 0, 5, 3, -1, -1,
217 IS_BINARY_OP | CLOBBER_DEST,
218 "mov", "r!0d, r!1D"),
219 ENCODING_MAP(ARMV5TE_MOV_RR_L2H, 0x4680, 2, 0, 5, 3, -1, -1,
220 IS_BINARY_OP | CLOBBER_DEST,
221 "mov", "r!0D, r!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700222 ENCODING_MAP(ARMV5TE_MUL, 0x4340, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700223 IS_BINARY_OP | CLOBBER_DEST,
224 "mul", "r!0d, r!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700225 ENCODING_MAP(ARMV5TE_MVN, 0x43c0, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700226 IS_BINARY_OP | CLOBBER_DEST,
227 "mvn", "r!0d, r!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700228 ENCODING_MAP(ARMV5TE_NEG, 0x4240, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700229 IS_BINARY_OP | CLOBBER_DEST,
230 "neg", "r!0d, r!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700231 ENCODING_MAP(ARMV5TE_ORR, 0x4300, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700232 IS_BINARY_OP | CLOBBER_DEST,
233 "orr", "r!0d, r!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700234 ENCODING_MAP(ARMV5TE_POP, 0xbc00, 8, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700235 IS_UNARY_OP,
236 "pop", "<!0R>"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700237 ENCODING_MAP(ARMV5TE_PUSH, 0xb400, 8, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700238 IS_UNARY_OP,
239 "push", "<!0R>"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700240 ENCODING_MAP(ARMV5TE_ROR, 0x41c0, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700241 IS_BINARY_OP | CLOBBER_DEST,
242 "ror", "r!0d, r!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700243 ENCODING_MAP(ARMV5TE_SBC, 0x4180, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700244 IS_BINARY_OP | CLOBBER_DEST,
245 "sbc", "r!0d, r!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700246 ENCODING_MAP(ARMV5TE_STMIA, 0xc000, 10, 8, 7, 0, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700247 IS_BINARY_OP | CLOBBER_SRC1,
248 "stmia", "r!0d!!, <!1R>"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700249 ENCODING_MAP(ARMV5TE_STR_RRI5, 0x6000, 2, 0, 5, 3, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700250 IS_TERTIARY_OP,
251 "str", "r!0d, [r!1d, #!2E]"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700252 ENCODING_MAP(ARMV5TE_STR_RRR, 0x5000, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700253 IS_TERTIARY_OP,
254 "str", "r!0d, [r!1d, r!2d]"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700255 ENCODING_MAP(ARMV5TE_STR_SP_REL, 0x9000, 10, 8, 7, 0, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700256 IS_BINARY_OP,
257 "str", "r!0d, [sp, #!1E]"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700258 ENCODING_MAP(ARMV5TE_STRB_RRI5, 0x7000, 2, 0, 5, 3, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700259 IS_TERTIARY_OP,
260 "strb", "r!0d, [r!1d, #!2d]"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700261 ENCODING_MAP(ARMV5TE_STRB_RRR, 0x5400, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700262 IS_TERTIARY_OP,
263 "strb", "r!0d, [r!1d, r!2d]"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700264 ENCODING_MAP(ARMV5TE_STRH_RRI5, 0x8000, 2, 0, 5, 3, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700265 IS_TERTIARY_OP,
266 "strh", "r!0d, [r!1d, #!2F]"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700267 ENCODING_MAP(ARMV5TE_STRH_RRR, 0x5200, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700268 IS_TERTIARY_OP,
269 "strh", "r!0d, [r!1d, r!2d]"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700270 ENCODING_MAP(ARMV5TE_SUB_RRI3, 0x1e00, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700271 IS_TERTIARY_OP | CLOBBER_DEST,
272 "sub", "r!0d, r!1d, #!2d]"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700273 ENCODING_MAP(ARMV5TE_SUB_RI8, 0x3800, 10, 8, 7, 0, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700274 IS_BINARY_OP | CLOBBER_DEST,
275 "sub", "r!0d, #!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700276 ENCODING_MAP(ARMV5TE_SUB_RRR, 0x1a00, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700277 IS_TERTIARY_OP | CLOBBER_DEST,
278 "sub", "r!0d, r!1d, r!2d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700279 ENCODING_MAP(ARMV5TE_SUB_SPI7, 0xb080, 6, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700280 IS_UNARY_OP | CLOBBER_DEST,
281 "sub", "sp, #!0d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700282 ENCODING_MAP(ARMV5TE_SWI, 0xdf00, 7, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700283 IS_UNARY_OP | IS_BRANCH,
284 "swi", "!0d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700285 ENCODING_MAP(ARMV5TE_TST, 0x4200, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700286 IS_UNARY_OP,
287 "tst", "r!0d, r!1d"),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700288};
289
290#define PADDING_MOV_R0_R0 0x1C00
291
292/* Write the numbers in the literal pool to the codegen stream */
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700293static void installDataContent(CompilationUnit *cUnit)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700294{
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700295 int *dataPtr = (int *) (cUnit->baseAddr + cUnit->dataOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700296 Armv5teLIR *dataLIR = (Armv5teLIR *) cUnit->wordList;
297 while (dataLIR) {
298 *dataPtr++ = dataLIR->operands[0];
299 dataLIR = NEXT_LIR(dataLIR);
300 }
301}
302
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700303/* Returns the size of a Jit trace description */
304static int jitTraceDescriptionSize(const JitTraceDescription *desc)
305{
306 int runCount;
307 for (runCount = 0; ; runCount++) {
308 if (desc->trace[runCount].frag.runEnd)
309 break;
310 }
311 return sizeof(JitCodeDesc) + ((runCount+1) * sizeof(JitTraceRun));
312}
313
Ben Chengba4fc8b2009-06-01 13:00:29 -0700314/* Return TRUE if error happens */
315static bool assembleInstructions(CompilationUnit *cUnit, intptr_t startAddr)
316{
317 short *bufferAddr = (short *) cUnit->codeBuffer;
318 Armv5teLIR *lir;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700319
320 for (lir = (Armv5teLIR *) cUnit->firstLIRInsn; lir; lir = NEXT_LIR(lir)) {
321 if (lir->opCode < 0) {
322 if ((lir->opCode == ARMV5TE_PSEUDO_ALIGN4) &&
Ben Cheng1efc9c52009-06-08 18:25:27 -0700323 /* 1 means padding is needed */
324 (lir->operands[0] == 1)) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700325 *bufferAddr++ = PADDING_MOV_R0_R0;
326 }
327 continue;
328 }
329
Ben Chenge9695e52009-06-16 16:11:47 -0700330 if (lir->isNop) {
331 continue;
332 }
333
Ben Chengba4fc8b2009-06-01 13:00:29 -0700334 if (lir->opCode == ARMV5TE_LDR_PC_REL ||
335 lir->opCode == ARMV5TE_ADD_PC_REL) {
336 Armv5teLIR *lirTarget = (Armv5teLIR *) lir->generic.target;
337 intptr_t pc = (lir->generic.offset + 4) & ~3;
Ben Cheng38329f52009-07-07 14:19:20 -0700338 /*
339 * Allow an offset (stored in operands[2] to be added to the
340 * PC-relative target. Useful to get to a fixed field inside a
341 * chaining cell.
342 */
343 intptr_t target = lirTarget->generic.offset + lir->operands[2];
Ben Chengba4fc8b2009-06-01 13:00:29 -0700344 int delta = target - pc;
345 if (delta & 0x3) {
346 LOGE("PC-rel distance is not multiples of 4: %d\n", delta);
347 dvmAbort();
348 }
Ben Cheng1efc9c52009-06-08 18:25:27 -0700349 if (delta > 1023) {
350 return true;
351 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700352 lir->operands[1] = delta >> 2;
353 } else if (lir->opCode == ARMV5TE_B_COND) {
354 Armv5teLIR *targetLIR = (Armv5teLIR *) lir->generic.target;
355 intptr_t pc = lir->generic.offset + 4;
356 intptr_t target = targetLIR->generic.offset;
357 int delta = target - pc;
358 if (delta > 254 || delta < -256) {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700359 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700360 }
361 lir->operands[0] = delta >> 1;
362 } else if (lir->opCode == ARMV5TE_B_UNCOND) {
363 Armv5teLIR *targetLIR = (Armv5teLIR *) lir->generic.target;
364 intptr_t pc = lir->generic.offset + 4;
365 intptr_t target = targetLIR->generic.offset;
366 int delta = target - pc;
367 if (delta > 2046 || delta < -2048) {
368 LOGE("Unconditional branch distance out of range: %d\n", delta);
369 dvmAbort();
370 }
371 lir->operands[0] = delta >> 1;
372 } else if (lir->opCode == ARMV5TE_BLX_1) {
373 assert(NEXT_LIR(lir)->opCode == ARMV5TE_BLX_2);
374 /* curPC is Thumb */
375 intptr_t curPC = (startAddr + lir->generic.offset + 4) & ~3;
376 intptr_t target = lir->operands[1];
377
378 /* Match bit[1] in target with base */
379 if (curPC & 0x2) {
380 target |= 0x2;
381 }
382 int delta = target - curPC;
383 assert((delta >= -(1<<22)) && (delta <= ((1<<22)-2)));
384
385 lir->operands[0] = (delta >> 12) & 0x7ff;
386 NEXT_LIR(lir)->operands[0] = (delta>> 1) & 0x7ff;
387 }
388
Ben Chengba4fc8b2009-06-01 13:00:29 -0700389 Armv5teEncodingMap *encoder = &EncodingMap[lir->opCode];
390 short bits = encoder->skeleton;
391 int i;
392 for (i = 0; i < 3; i++) {
393 short value;
394 if (encoder->fieldLoc[i].end != -1) {
395 value = (lir->operands[i] << encoder->fieldLoc[i].start) &
396 ((1 << (encoder->fieldLoc[i].end + 1)) - 1);
397 bits |= value;
398
399 }
400 }
401 *bufferAddr++ = bits;
402 }
Ben Cheng1efc9c52009-06-08 18:25:27 -0700403 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700404}
405
406/*
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700407 * Translation layout in the code cache. Note that the codeAddress pointer
408 * in JitTable will point directly to the code body (field codeAddress). The
409 * chain cell offset codeAddress - 2, and (if present) executionCount is at
410 * codeAddress - 6.
411 *
412 * +----------------------------+
413 * | Execution count | -> [Optional] 4 bytes
414 * +----------------------------+
415 * +--| Offset to chain cell counts| -> 2 bytes
416 * | +----------------------------+
417 * | | Code body | -> Start address for translation
418 * | | | variable in 2-byte chunks
419 * | . . (JitTable's codeAddress points here)
420 * | . .
421 * | | |
422 * | +----------------------------+
423 * | | Chaining Cells | -> 8 bytes each, must be 4 byte aligned
424 * | . .
425 * | . .
426 * | | |
427 * | +----------------------------+
428 * +->| Chaining cell counts | -> 4 bytes, chain cell counts by type
429 * +----------------------------+
430 * | Trace description | -> variable sized
431 * . .
432 * | |
433 * +----------------------------+
434 * | Literal pool | -> 4-byte aligned, variable size
435 * . .
436 * . .
437 * | |
438 * +----------------------------+
439 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700440 * Go over each instruction in the list and calculate the offset from the top
441 * before sending them off to the assembler. If out-of-range branch distance is
442 * seen rearrange the instructions a bit to correct it.
443 */
444void dvmCompilerAssembleLIR(CompilationUnit *cUnit)
445{
446 LIR *lir;
447 Armv5teLIR *armLIR;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700448 int offset = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700449 int i;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700450 ChainCellCounts chainCellCounts;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700451 int descSize = jitTraceDescriptionSize(cUnit->traceDesc);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700452
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700453 /* Beginning offset needs to allow space for chain cell offset */
Ben Cheng1efc9c52009-06-08 18:25:27 -0700454 for (armLIR = (Armv5teLIR *) cUnit->firstLIRInsn;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700455 armLIR;
456 armLIR = NEXT_LIR(armLIR)) {
457 armLIR->generic.offset = offset;
Ben Chenge9695e52009-06-16 16:11:47 -0700458 if (armLIR->opCode >= 0 && !armLIR->isNop) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700459 offset += 2;
460 } else if (armLIR->opCode == ARMV5TE_PSEUDO_ALIGN4) {
461 if (offset & 0x2) {
462 offset += 2;
463 armLIR->operands[0] = 1;
464 } else {
465 armLIR->operands[0] = 0;
466 }
467 }
468 /* Pseudo opcodes don't consume space */
469 }
470
471 /* Const values have to be word aligned */
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700472 offset = (offset + 3) & ~3;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700473
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700474 /* Add space for chain cell counts & trace description */
Ben Cheng1efc9c52009-06-08 18:25:27 -0700475 u4 chainCellOffset = offset;
Ben Cheng38329f52009-07-07 14:19:20 -0700476 Armv5teLIR *chainCellOffsetLIR = (Armv5teLIR *) cUnit->chainCellOffsetLIR;
Bill Buzbee6e963e12009-06-17 16:56:19 -0700477 assert(chainCellOffsetLIR);
Ben Cheng1efc9c52009-06-08 18:25:27 -0700478 assert(chainCellOffset < 0x10000);
479 assert(chainCellOffsetLIR->opCode == ARMV5TE_16BIT_DATA &&
480 chainCellOffsetLIR->operands[0] == CHAIN_CELL_OFFSET_TAG);
481
482 /* Replace the CHAIN_CELL_OFFSET_TAG with the real value */
483 chainCellOffsetLIR->operands[0] = chainCellOffset;
484
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700485 offset += sizeof(chainCellCounts) + descSize;
486
487 assert((offset & 0x3) == 0); /* Should still be word aligned */
488
489 /* Set up offsets for literals */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700490 cUnit->dataOffset = offset;
491
492 for (lir = cUnit->wordList; lir; lir = lir->next) {
493 lir->offset = offset;
494 offset += 4;
495 }
496
497 cUnit->totalSize = offset;
498
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700499 if (gDvmJit.codeCacheByteUsed + cUnit->totalSize > CODE_CACHE_SIZE) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700500 gDvmJit.codeCacheFull = true;
501 cUnit->baseAddr = NULL;
502 return;
503 }
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700504
505 /* Allocate enough space for the code block */
506 cUnit->codeBuffer = dvmCompilerNew(chainCellOffset, true);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700507 if (cUnit->codeBuffer == NULL) {
508 LOGE("Code buffer allocation failure\n");
509 cUnit->baseAddr = NULL;
510 return;
511 }
512
Ben Cheng1efc9c52009-06-08 18:25:27 -0700513 bool assemblerFailure = assembleInstructions(
Ben Chengba4fc8b2009-06-01 13:00:29 -0700514 cUnit, (intptr_t) gDvmJit.codeCache + gDvmJit.codeCacheByteUsed);
515
Ben Cheng1efc9c52009-06-08 18:25:27 -0700516 /*
517 * Currently the only reason that can cause the assembler to fail is due to
518 * trace length - cut it in half and retry.
519 */
520 if (assemblerFailure) {
521 cUnit->halveInstCount = true;
522 return;
523 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700524
Bill Buzbee6e963e12009-06-17 16:56:19 -0700525
Ben Chengba4fc8b2009-06-01 13:00:29 -0700526 cUnit->baseAddr = (char *) gDvmJit.codeCache + gDvmJit.codeCacheByteUsed;
527 gDvmJit.codeCacheByteUsed += offset;
528
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700529 /* Install the code block */
Ben Cheng1efc9c52009-06-08 18:25:27 -0700530 memcpy((char*)cUnit->baseAddr, cUnit->codeBuffer, chainCellOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700531 gDvmJit.numCompilations++;
532
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700533 /* Install the chaining cell counts */
534 for (i=0; i< CHAINING_CELL_LAST; i++) {
535 chainCellCounts.u.count[i] = cUnit->numChainingCells[i];
536 }
537 memcpy((char*)cUnit->baseAddr + chainCellOffset, &chainCellCounts,
538 sizeof(chainCellCounts));
539
540 /* Install the trace description */
541 memcpy((char*)cUnit->baseAddr + chainCellOffset + sizeof(chainCellCounts),
542 cUnit->traceDesc, descSize);
543
544 /* Write the literals directly into the code cache */
545 installDataContent(cUnit);
546
Ben Chengba4fc8b2009-06-01 13:00:29 -0700547 /* Flush dcache and invalidate the icache to maintain coherence */
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700548 cacheflush((long)cUnit->baseAddr,
549 (long)(cUnit->baseAddr + offset), 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700550}
551
Ben Cheng38329f52009-07-07 14:19:20 -0700552static u4 assembleBXPair(int branchOffset)
553{
554 u4 thumb1, thumb2;
555
556 if ((branchOffset < -2048) | (branchOffset > 2046)) {
557 thumb1 = (0xf000 | ((branchOffset>>12) & 0x7ff));
558 thumb2 = (0xf800 | ((branchOffset>> 1) & 0x7ff));
559 } else {
560 thumb1 = (0xe000 | ((branchOffset>> 1) & 0x7ff));
561 thumb2 = 0x4300; /* nop -> or r0, r0 */
562 }
563
564 return thumb2<<16 | thumb1;
565}
566
Ben Chengba4fc8b2009-06-01 13:00:29 -0700567/*
568 * Perform translation chain operation.
569 * For ARM, we'll use a pair of thumb instructions to generate
570 * an unconditional chaining branch of up to 4MB in distance.
571 * Use a BL, though we don't really need the link. The format is
572 * 111HHooooooooooo
573 * Where HH is 10 for the 1st inst, and 11 for the second and
574 * the "o" field is each instruction's 11-bit contribution to the
575 * 22-bit branch offset.
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700576 * If the target is nearby, use a single-instruction bl.
577 * If one or more threads is suspended, don't chain.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700578 */
579void* dvmJitChain(void* tgtAddr, u4* branchAddr)
580{
581 int baseAddr = (u4) branchAddr + 4;
582 int branchOffset = (int) tgtAddr - baseAddr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700583 u4 newInst;
584
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700585 if (gDvm.sumThreadSuspendCount == 0) {
586 assert((branchOffset >= -(1<<22)) && (branchOffset <= ((1<<22)-2)));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700587
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700588 gDvmJit.translationChains++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700589
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700590 COMPILER_TRACE_CHAINING(
591 LOGD("Jit Runtime: chaining 0x%x to 0x%x\n",
592 (int) branchAddr, (int) tgtAddr & -2));
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700593
Ben Cheng38329f52009-07-07 14:19:20 -0700594 newInst = assembleBXPair(branchOffset);
595
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700596 *branchAddr = newInst;
597 cacheflush((long)branchAddr, (long)branchAddr + 4, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700598 }
599
Ben Chengba4fc8b2009-06-01 13:00:29 -0700600 return tgtAddr;
601}
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700602
603/*
Ben Cheng38329f52009-07-07 14:19:20 -0700604 * This method is called from the invoke templates for virtual and interface
605 * methods to speculatively setup a chain to the callee. The templates are
606 * written in assembly and have setup method, cell, and clazz at r0, r2, and
607 * r3 respectively, so there is a unused argument in the list. Upon return one
608 * of the following three results may happen:
609 * 1) Chain is not setup because the callee is native. Reset the rechain
610 * count to a big number so that it will take a long time before the next
611 * rechain attempt to happen.
612 * 2) Chain is not setup because the callee has not been created yet. Reset
613 * the rechain count to a small number and retry in the near future.
614 * 3) Ask all other threads to stop before patching this chaining cell.
615 * This is required because another thread may have passed the class check
616 * but hasn't reached the chaining cell yet to follow the chain. If we
617 * patch the content before halting the other thread, there could be a
618 * small window for race conditions to happen that it may follow the new
619 * but wrong chain to invoke a different method.
620 */
621const Method *dvmJitToPatchPredictedChain(const Method *method,
622 void *unused,
623 PredictedChainingCell *cell,
624 const ClassObject *clazz)
625{
626 /* Don't come back here for a long time if the method is native */
627 if (dvmIsNativeMethod(method)) {
628 cell->counter = PREDICTED_CHAIN_COUNTER_AVOID;
629 cacheflush((long) cell, (long) (cell+1), 0);
630 COMPILER_TRACE_CHAINING(
631 LOGD("Jit Runtime: predicted chain %p to native method %s ignored",
632 cell, method->name));
633 goto done;
634 }
635 int tgtAddr = (int) dvmJitGetCodeAddr(method->insns);
636
637 /*
638 * Compilation not made yet for the callee. Reset the counter to a small
639 * value and come back to check soon.
640 */
641 if (tgtAddr == 0) {
642 /*
643 * Wait for a few invocations (currently set to be 16) before trying
644 * to setup the chain again.
645 */
646 cell->counter = PREDICTED_CHAIN_COUNTER_DELAY;
647 cacheflush((long) cell, (long) (cell+1), 0);
648 COMPILER_TRACE_CHAINING(
649 LOGD("Jit Runtime: predicted chain %p to method %s delayed",
650 cell, method->name));
651 goto done;
652 }
653
654 /* Stop the world */
655 dvmSuspendAllThreads(SUSPEND_FOR_JIT);
656
657 int baseAddr = (int) cell + 4; // PC is cur_addr + 4
658 int branchOffset = tgtAddr - baseAddr;
659
660 COMPILER_TRACE_CHAINING(
661 LOGD("Jit Runtime: predicted chain %p from %s to %s (%s) patched",
662 cell, cell->clazz ? cell->clazz->descriptor : "NULL",
663 clazz->descriptor,
664 method->name));
665
666 cell->branch = assembleBXPair(branchOffset);
667 cell->clazz = clazz;
668 cell->method = method;
669 cell->counter = PREDICTED_CHAIN_COUNTER_RECHAIN;
670
671 cacheflush((long) cell, (long) (cell+1), 0);
672
673 /* All done - resume all other threads */
674 dvmResumeAllThreads(SUSPEND_FOR_JIT);
675
676done:
677 return method;
678}
679
680/*
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700681 * Unchain a trace given the starting address of the translation
682 * in the code cache. Refer to the diagram in dvmCompilerAssembleLIR.
683 * Returns the address following the last cell unchained. Note that
684 * the incoming codeAddr is a thumb code address, and therefore has
685 * the low bit set.
686 */
687u4* dvmJitUnchain(void* codeAddr)
688{
689 u2* pChainCellOffset = (u2*)((char*)codeAddr - 3);
690 u2 chainCellOffset = *pChainCellOffset;
691 ChainCellCounts *pChainCellCounts =
692 (ChainCellCounts*)((char*)codeAddr + chainCellOffset -3);
Ben Cheng38329f52009-07-07 14:19:20 -0700693 int cellSize;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700694 u4* pChainCells;
695 u4* pStart;
696 u4 thumb1;
697 u4 thumb2;
698 u4 newInst;
699 int i,j;
Ben Cheng38329f52009-07-07 14:19:20 -0700700 PredictedChainingCell *predChainCell;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700701
702 /* Get total count of chain cells */
Ben Cheng38329f52009-07-07 14:19:20 -0700703 for (i = 0, cellSize = 0; i < CHAINING_CELL_LAST; i++) {
704 if (i != CHAINING_CELL_INVOKE_PREDICTED) {
705 cellSize += pChainCellCounts->u.count[i] * 2;
706 } else {
707 cellSize += pChainCellCounts->u.count[i] * 4;
708 }
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700709 }
710
711 /* Locate the beginning of the chain cell region */
Ben Cheng38329f52009-07-07 14:19:20 -0700712 pStart = pChainCells = ((u4 *) pChainCellCounts) - cellSize;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700713
714 /* The cells are sorted in order - walk through them and reset */
715 for (i = 0; i < CHAINING_CELL_LAST; i++) {
Ben Cheng38329f52009-07-07 14:19:20 -0700716 int elemSize = 2; /* Most chaining cell has two words */
717 if (i == CHAINING_CELL_INVOKE_PREDICTED) {
718 elemSize = 4;
719 }
720
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700721 for (j = 0; j < pChainCellCounts->u.count[i]; j++) {
722 int targetOffset;
723 switch(i) {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700724 case CHAINING_CELL_NORMAL:
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700725 targetOffset = offsetof(InterpState,
726 jitToInterpEntries.dvmJitToInterpNormal);
727 break;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700728 case CHAINING_CELL_HOT:
Ben Cheng38329f52009-07-07 14:19:20 -0700729 case CHAINING_CELL_INVOKE_SINGLETON:
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700730 targetOffset = offsetof(InterpState,
731 jitToInterpEntries.dvmJitToTraceSelect);
732 break;
Ben Cheng38329f52009-07-07 14:19:20 -0700733 case CHAINING_CELL_INVOKE_PREDICTED:
734 targetOffset = 0;
735 predChainCell = (PredictedChainingCell *) pChainCells;
736 /* Reset the cell to the init state */
737 predChainCell->branch = PREDICTED_CHAIN_BX_PAIR_INIT;
738 predChainCell->clazz = PREDICTED_CHAIN_CLAZZ_INIT;
739 predChainCell->method = PREDICTED_CHAIN_METHOD_INIT;
740 predChainCell->counter = PREDICTED_CHAIN_COUNTER_INIT;
741 break;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700742 default:
743 dvmAbort();
744 }
Ben Cheng38329f52009-07-07 14:19:20 -0700745 COMPILER_TRACE_CHAINING(
746 LOGD("Jit Runtime: unchaining 0x%x", (int)pChainCells));
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700747 /*
Ben Cheng38329f52009-07-07 14:19:20 -0700748 * Thumb code sequence for a chaining cell is:
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700749 * ldr r0, rGLUE, #<word offset>
750 * blx r0
751 */
Ben Cheng38329f52009-07-07 14:19:20 -0700752 if (i != CHAINING_CELL_INVOKE_PREDICTED) {
753 targetOffset = targetOffset >> 2; /* convert to word offset */
754 thumb1 = 0x6800 | (targetOffset << 6) |
755 (rGLUE << 3) | (r0 << 0);
756 thumb2 = 0x4780 | (r0 << 3);
757 newInst = thumb2<<16 | thumb1;
758 *pChainCells = newInst;
759 }
760 pChainCells += elemSize; /* Advance by a fixed number of words */
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700761 }
762 }
763 return pChainCells;
764}
765
766/* Unchain all translation in the cache. */
767void dvmJitUnchainAll()
768{
769 u4* lowAddress = NULL;
770 u4* highAddress = NULL;
771 unsigned int i;
772 if (gDvmJit.pJitEntryTable != NULL) {
773 COMPILER_TRACE_CHAINING(LOGD("Jit Runtime: unchaining all"));
774 dvmLockMutex(&gDvmJit.tableLock);
Bill Buzbee27176222009-06-09 09:20:16 -0700775 for (i = 0; i < gDvmJit.jitTableSize; i++) {
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700776 if (gDvmJit.pJitEntryTable[i].dPC &&
777 gDvmJit.pJitEntryTable[i].codeAddress) {
778 u4* lastAddress;
779 lastAddress =
780 dvmJitUnchain(gDvmJit.pJitEntryTable[i].codeAddress);
781 if (lowAddress == NULL ||
782 (u4*)gDvmJit.pJitEntryTable[i].codeAddress < lowAddress)
783 lowAddress = lastAddress;
784 if (lastAddress > highAddress)
785 highAddress = lastAddress;
786 }
787 }
788 cacheflush((long)lowAddress, (long)highAddress, 0);
789 dvmUnlockMutex(&gDvmJit.tableLock);
790 }
791}