blob: 5a870147fda663935eb4e14444f190f2ec1b96ca [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"
Bill Buzbee89efc3d2009-07-28 11:22:22 -070022#include "ArmLIR.h"
Ben Chengba4fc8b2009-06-01 13:00:29 -070023#include <unistd.h> /* for cacheflush */
24
25/*
Bill Buzbee89efc3d2009-07-28 11:22:22 -070026 * opcode: ArmOpCode enum
Ben Chengba4fc8b2009-06-01 13:00:29 -070027 * skeleton: pre-designated bit-pattern for this opcode
Bill Buzbee9bc3df32009-07-30 10:52:29 -070028 * k0: key to applying ds/de
Ben Chengba4fc8b2009-06-01 13:00:29 -070029 * ds: dest start bit position
30 * de: dest end bit position
Bill Buzbee9bc3df32009-07-30 10:52:29 -070031 * k1: key to applying s1s/s1e
Ben Chengba4fc8b2009-06-01 13:00:29 -070032 * s1s: src1 start bit position
33 * s1e: src1 end bit position
Bill Buzbee9bc3df32009-07-30 10:52:29 -070034 * k2: key to applying s2s/s2e
Ben Chengba4fc8b2009-06-01 13:00:29 -070035 * s2s: src2 start bit position
36 * s2e: src2 end bit position
37 * operands: number of operands (for sanity check purposes)
38 * name: mnemonic name
39 * fmt: for pretty-prining
40 */
Bill Buzbee9bc3df32009-07-30 10:52:29 -070041#define ENCODING_MAP(opcode, skeleton, k0, ds, de, k1, s1s, s1e, k2, s2s, s2e, \
42 operands, name, fmt, size) \
43 {skeleton, {{k0, ds, de}, {k1, s1s, s1e}, {k2, s2s, s2e}}, \
44 opcode, operands, name, fmt, size}
Ben Chengba4fc8b2009-06-01 13:00:29 -070045
46/* Instruction dump string format keys: !pf, where "!" is the start
47 * of the key, "p" is which numeric operand to use and "f" is the
48 * print format.
49 *
50 * [p]ositions:
51 * 0 -> operands[0] (dest)
52 * 1 -> operands[1] (src1)
53 * 2 -> operands[2] (src2)
54 *
55 * [f]ormats:
56 * h -> 4-digit hex
57 * d -> decimal
58 * D -> decimal+8 (used to convert 3-bit regnum field to high reg)
59 * E -> decimal*4
60 * F -> decimal*2
61 * c -> branch condition (beq, bne, etc.)
62 * t -> pc-relative target
63 * u -> 1st half of bl[x] target
64 * v -> 2nd half ob bl[x] target
65 * R -> register list
66 *
67 * [!] escape. To insert "!", use "!!"
68 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -070069/* NOTE: must be kept in sync with enum ArmOpcode from ArmLIR.h */
70ArmEncodingMap EncodingMap[ARM_LAST] = {
Bill Buzbee9bc3df32009-07-30 10:52:29 -070071 ENCODING_MAP(ARM_16BIT_DATA, 0x0000,
72 BITBLT, 15, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -070073 IS_UNARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -070074 "data", "0x!0h(!0d)", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -070075 ENCODING_MAP(THUMB_ADC, 0x4140,
76 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -070077 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -070078 "adc", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -070079 ENCODING_MAP(THUMB_ADD_RRI3, 0x1c00,
80 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -070081 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -070082 "add", "r!0d, r!1d, #!2d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -070083 ENCODING_MAP(THUMB_ADD_RI8, 0x3000,
84 BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -070085 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -070086 "add", "r!0d, r!0d, #!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -070087 ENCODING_MAP(THUMB_ADD_RRR, 0x1800,
88 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -070089 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -070090 "add", "r!0d, r!1d, r!2d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -070091 ENCODING_MAP(THUMB_ADD_RR_LH, 0x4440,
92 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -070093 IS_BINARY_OP | CLOBBER_DEST,
94 "add",
Bill Buzbee716f1202009-07-23 13:22:09 -070095 "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -070096 ENCODING_MAP(THUMB_ADD_RR_HL, 0x4480,
97 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -070098 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -070099 "add", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700100 ENCODING_MAP(THUMB_ADD_RR_HH, 0x44c0,
101 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700102 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700103 "add", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700104 ENCODING_MAP(THUMB_ADD_PC_REL, 0xa000,
105 BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1,
Ben Cheng38329f52009-07-07 14:19:20 -0700106 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700107 "add", "r!0d, pc, #!1E", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700108 ENCODING_MAP(THUMB_ADD_SP_REL, 0xa800,
109 BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700110 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700111 "add", "r!0d, sp, #!1E", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700112 ENCODING_MAP(THUMB_ADD_SPI7, 0xb000,
113 BITBLT, 6, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700114 IS_UNARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700115 "add", "sp, #!0d*4", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700116 ENCODING_MAP(THUMB_AND_RR, 0x4000,
117 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700118 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700119 "and", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700120 ENCODING_MAP(THUMB_ASR, 0x1000,
121 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700122 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700123 "asr", "r!0d, r!1d, #!2d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700124 ENCODING_MAP(THUMB_ASRV, 0x4100,
125 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700126 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700127 "asr", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700128 ENCODING_MAP(THUMB_B_COND, 0xd000,
129 BITBLT, 7, 0, BITBLT, 11, 8, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700130 IS_BINARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700131 "!1c", "!0t", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700132 ENCODING_MAP(THUMB_B_UNCOND, 0xe000,
133 BITBLT, 10, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700134 NO_OPERAND | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700135 "b", "!0t", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700136 ENCODING_MAP(THUMB_BIC, 0x4380,
137 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700138 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700139 "bic", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700140 ENCODING_MAP(THUMB_BKPT, 0xbe00,
141 BITBLT, 7, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700142 IS_UNARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700143 "bkpt", "!0d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700144 ENCODING_MAP(THUMB_BLX_1, 0xf000,
145 BITBLT, 10, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700146 IS_BINARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700147 "blx_1", "!0u", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700148 ENCODING_MAP(THUMB_BLX_2, 0xe800,
149 BITBLT, 10, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700150 IS_BINARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700151 "blx_2", "!0v", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700152 ENCODING_MAP(THUMB_BL_1, 0xf000,
153 BITBLT, 10, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700154 IS_UNARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700155 "bl_1", "!0u", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700156 ENCODING_MAP(THUMB_BL_2, 0xf800,
157 BITBLT, 10, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700158 IS_UNARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700159 "bl_2", "!0v", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700160 ENCODING_MAP(THUMB_BLX_R, 0x4780,
161 BITBLT, 6, 3, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700162 IS_UNARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700163 "blx", "r!0d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700164 ENCODING_MAP(THUMB_BX, 0x4700,
165 BITBLT, 6, 3, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700166 IS_UNARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700167 "bx", "r!0d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700168 ENCODING_MAP(THUMB_CMN, 0x42c0,
169 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700170 IS_BINARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700171 "cmn", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700172 ENCODING_MAP(THUMB_CMP_RI8, 0x2800,
173 BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700174 IS_BINARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700175 "cmp", "r!0d, #!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700176 ENCODING_MAP(THUMB_CMP_RR, 0x4280,
177 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700178 IS_BINARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700179 "cmp", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700180 ENCODING_MAP(THUMB_CMP_LH, 0x4540,
181 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700182 IS_BINARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700183 "cmp", "r!0d, r!1D", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700184 ENCODING_MAP(THUMB_CMP_HL, 0x4580,
185 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700186 IS_BINARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700187 "cmp", "r!0D, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700188 ENCODING_MAP(THUMB_CMP_HH, 0x45c0,
189 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700190 IS_BINARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700191 "cmp", "r!0D, r!1D", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700192 ENCODING_MAP(THUMB_EOR, 0x4040,
193 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700194 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700195 "eor", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700196 ENCODING_MAP(THUMB_LDMIA, 0xc800,
197 BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700198 IS_BINARY_OP | CLOBBER_DEST | CLOBBER_SRC1,
Bill Buzbee716f1202009-07-23 13:22:09 -0700199 "ldmia", "r!0d!!, <!1R>", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700200 ENCODING_MAP(THUMB_LDR_RRI5, 0x6800,
201 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700202 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700203 "ldr", "r!0d, [r!1d, #!2E]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700204 ENCODING_MAP(THUMB_LDR_RRR, 0x5800,
205 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700206 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700207 "ldr", "r!0d, [r!1d, r!2d]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700208 ENCODING_MAP(THUMB_LDR_PC_REL, 0x4800,
209 BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1,
Ben Cheng38329f52009-07-07 14:19:20 -0700210 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700211 "ldr", "r!0d, [pc, #!1E]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700212 ENCODING_MAP(THUMB_LDR_SP_REL, 0x9800,
213 BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700214 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700215 "ldr", "r!0d, [sp, #!1E]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700216 ENCODING_MAP(THUMB_LDRB_RRI5, 0x7800,
217 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700218 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700219 "ldrb", "r!0d, [r!1d, #2d]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700220 ENCODING_MAP(THUMB_LDRB_RRR, 0x5c00,
221 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700222 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700223 "ldrb", "r!0d, [r!1d, r!2d]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700224 ENCODING_MAP(THUMB_LDRH_RRI5, 0x8800,
225 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700226 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700227 "ldrh", "r!0d, [r!1d, #!2F]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700228 ENCODING_MAP(THUMB_LDRH_RRR, 0x5a00,
229 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700230 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700231 "ldrh", "r!0d, [r!1d, r!2d]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700232 ENCODING_MAP(THUMB_LDRSB_RRR, 0x5600,
233 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700234 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700235 "ldrsb", "r!0d, [r!1d, r!2d]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700236 ENCODING_MAP(THUMB_LDRSH_RRR, 0x5e00,
237 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700238 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700239 "ldrsh", "r!0d, [r!1d, r!2d]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700240 ENCODING_MAP(THUMB_LSL, 0x0000,
241 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700242 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700243 "lsl", "r!0d, r!1d, #!2d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700244 ENCODING_MAP(THUMB_LSLV, 0x4080,
245 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700246 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700247 "lsl", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700248 ENCODING_MAP(THUMB_LSR, 0x0800,
249 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700250 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700251 "lsr", "r!0d, r!1d, #!2d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700252 ENCODING_MAP(THUMB_LSRV, 0x40c0,
253 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700254 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700255 "lsr", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700256 ENCODING_MAP(THUMB_MOV_IMM, 0x2000,
257 BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700258 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700259 "mov", "r!0d, #!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700260 ENCODING_MAP(THUMB_MOV_RR, 0x1c00,
261 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700262 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700263 "mov", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700264 ENCODING_MAP(THUMB_MOV_RR_H2H, 0x46c0,
265 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700266 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700267 "mov", "r!0D, r!1D", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700268 ENCODING_MAP(THUMB_MOV_RR_H2L, 0x4640,
269 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Cheng38329f52009-07-07 14:19:20 -0700270 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700271 "mov", "r!0d, r!1D", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700272 ENCODING_MAP(THUMB_MOV_RR_L2H, 0x4680,
273 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Cheng38329f52009-07-07 14:19:20 -0700274 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700275 "mov", "r!0D, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700276 ENCODING_MAP(THUMB_MUL, 0x4340,
277 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700278 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700279 "mul", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700280 ENCODING_MAP(THUMB_MVN, 0x43c0,
281 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700282 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700283 "mvn", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700284 ENCODING_MAP(THUMB_NEG, 0x4240,
285 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700286 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700287 "neg", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700288 ENCODING_MAP(THUMB_ORR, 0x4300,
289 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700290 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700291 "orr", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700292 ENCODING_MAP(THUMB_POP, 0xbc00,
293 BITBLT, 8, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700294 IS_UNARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700295 "pop", "<!0R>", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700296 ENCODING_MAP(THUMB_PUSH, 0xb400,
297 BITBLT, 8, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700298 IS_UNARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700299 "push", "<!0R>", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700300 ENCODING_MAP(THUMB_ROR, 0x41c0,
301 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700302 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700303 "ror", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700304 ENCODING_MAP(THUMB_SBC, 0x4180,
305 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700306 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700307 "sbc", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700308 ENCODING_MAP(THUMB_STMIA, 0xc000,
309 BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700310 IS_BINARY_OP | CLOBBER_SRC1,
Bill Buzbee716f1202009-07-23 13:22:09 -0700311 "stmia", "r!0d!!, <!1R>", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700312 ENCODING_MAP(THUMB_STR_RRI5, 0x6000,
313 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700314 IS_TERTIARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700315 "str", "r!0d, [r!1d, #!2E]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700316 ENCODING_MAP(THUMB_STR_RRR, 0x5000,
317 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700318 IS_TERTIARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700319 "str", "r!0d, [r!1d, r!2d]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700320 ENCODING_MAP(THUMB_STR_SP_REL, 0x9000,
321 BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700322 IS_BINARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700323 "str", "r!0d, [sp, #!1E]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700324 ENCODING_MAP(THUMB_STRB_RRI5, 0x7000,
325 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700326 IS_TERTIARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700327 "strb", "r!0d, [r!1d, #!2d]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700328 ENCODING_MAP(THUMB_STRB_RRR, 0x5400,
329 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700330 IS_TERTIARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700331 "strb", "r!0d, [r!1d, r!2d]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700332 ENCODING_MAP(THUMB_STRH_RRI5, 0x8000,
333 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700334 IS_TERTIARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700335 "strh", "r!0d, [r!1d, #!2F]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700336 ENCODING_MAP(THUMB_STRH_RRR, 0x5200,
337 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700338 IS_TERTIARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700339 "strh", "r!0d, [r!1d, r!2d]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700340 ENCODING_MAP(THUMB_SUB_RRI3, 0x1e00,
341 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700342 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700343 "sub", "r!0d, r!1d, #!2d]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700344 ENCODING_MAP(THUMB_SUB_RI8, 0x3800,
345 BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700346 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700347 "sub", "r!0d, #!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700348 ENCODING_MAP(THUMB_SUB_RRR, 0x1a00,
349 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700350 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700351 "sub", "r!0d, r!1d, r!2d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700352 ENCODING_MAP(THUMB_SUB_SPI7, 0xb080,
353 BITBLT, 6, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700354 IS_UNARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700355 "sub", "sp, #!0d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700356 ENCODING_MAP(THUMB_SWI, 0xdf00,
357 BITBLT, 7, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700358 IS_UNARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700359 "swi", "!0d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700360 ENCODING_MAP(THUMB_TST, 0x4200,
361 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700362 IS_UNARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700363 "tst", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700364 ENCODING_MAP(THUMB2_VLDRS, 0xed900a00,
365 SFP, 22, 12, BITBLT, 19, 16, BITBLT, 7, 0,
366 IS_TERTIARY_OP,
367 "vldr", "s!0d, [r!1d, #!2E]", 2),
368 ENCODING_MAP(THUMB2_VLDRD, 0xed900b00,
369 DFP, 22, 12, BITBLT, 19, 16, BITBLT, 7, 0,
370 IS_TERTIARY_OP,
371 "vldr", "d!0d, [r!1d, #!2E]", 2),
372 ENCODING_MAP(THUMB2_VMULS, 0xee200a00,
373 SFP, 22, 12, SFP, 7, 16, SFP, 5, 0,
374 IS_TERTIARY_OP,
375 "vmuls", "s!0d, s!1d, s!2d", 2),
376 ENCODING_MAP(THUMB2_VMULD, 0xee200b00,
377 DFP, 22, 12, DFP, 7, 16, DFP, 5, 0,
378 IS_TERTIARY_OP,
379 "vmuld", "d!0d, d!1d, d!2d", 2),
380 ENCODING_MAP(THUMB2_VSTRS, 0xed800a00,
381 SFP, 22, 12, BITBLT, 19, 16, BITBLT, 7, 0,
382 IS_TERTIARY_OP,
383 "vstr", "s!0d, [r!1d, #!2E]", 2),
384 ENCODING_MAP(THUMB2_VSTRD, 0xed800b00,
385 DFP, 22, 12, BITBLT, 19, 16, BITBLT, 7, 0,
386 IS_TERTIARY_OP,
387 "vstr", "d!0d, [r!1d, #!2E]", 2),
388 ENCODING_MAP(THUMB2_VSUBS, 0xee300a40,
389 SFP, 22, 12, SFP, 7, 16, SFP, 5, 0,
390 IS_TERTIARY_OP,
391 "vsub", "s!0d, s!1d, s!2d", 2),
392 ENCODING_MAP(THUMB2_VSUBD, 0xee300b40,
393 DFP, 22, 12, DFP, 7, 16, DFP, 5, 0,
394 IS_TERTIARY_OP,
395 "vsub", "d!0d, s!1d, s!2d", 2),
396 ENCODING_MAP(THUMB2_VADDS, 0xee300a00,
397 SFP, 22, 12, SFP, 7, 16, SFP, 5, 0,
398 IS_TERTIARY_OP,
399 "vadd", "s!0d, s!1d, s!2d", 2),
400 ENCODING_MAP(THUMB2_VADDD, 0xee300b00,
401 DFP, 22, 12, DFP, 7, 16, DFP, 5, 0,
402 IS_TERTIARY_OP,
403 "vadd", "d!0d, s!1d, s!2d", 2),
404 ENCODING_MAP(THUMB2_VDIVS, 0xee800a00,
405 SFP, 22, 12, SFP, 7, 16, SFP, 5, 0,
406 IS_TERTIARY_OP,
407 "vdivs", "s!0d, s!1d, s!2d", 2),
408 ENCODING_MAP(THUMB2_VDIVD, 0xee800b00,
409 DFP, 22, 12, DFP, 7, 16, DFP, 5, 0,
410 IS_TERTIARY_OP,
411 "vdivs", "s!0d, s!1d, s!2d", 2),
412 ENCODING_MAP(THUMB2_VCVTIF, 0xeeb80ac0,
413 SFP, 22, 12, SFP, 5, 0, UNUSED, -1, -1,
414 IS_BINARY_OP,
415 "vcvf.f32", "s!0d, s!1d", 2),
416 ENCODING_MAP(THUMB2_VCVTID, 0xeeb80bc0,
417 DFP, 22, 12, SFP, 5, 0, UNUSED, -1, -1,
418 IS_BINARY_OP,
419 "vcvf.f64", "s!0d, s!1d", 2),
420 ENCODING_MAP(THUMB2_VCVTFI, 0xeebd0ac0,
421 SFP, 22, 12, SFP, 5, 0, UNUSED, -1, -1,
422 IS_BINARY_OP,
423 "vcvf.s32.f32", "s!0d, s!1d", 2),
424 ENCODING_MAP(THUMB2_VCVTDI, 0xeebd0bc0,
425 SFP, 22, 12, DFP, 5, 0, UNUSED, -1, -1,
426 IS_BINARY_OP,
427 "vcvf.s32.f64", "s!0d, s!1d", 2),
428 ENCODING_MAP(THUMB2_VCVTFD, 0xeeb70ac0,
429 DFP, 22, 12, SFP, 5, 0, UNUSED, -1, -1,
430 IS_BINARY_OP,
431 "vcvf.f64.f32", "s!0d, s!1d", 2),
432 ENCODING_MAP(THUMB2_VCVTDF, 0xeeb70bc0,
433 SFP, 22, 12, DFP, 5, 0, UNUSED, -1, -1,
434 IS_BINARY_OP,
435 "vcvf.f32.f64", "s!0d, s!1d", 2),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700436};
437
438#define PADDING_MOV_R0_R0 0x1C00
439
440/* Write the numbers in the literal pool to the codegen stream */
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700441static void installDataContent(CompilationUnit *cUnit)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700442{
Ben Chenge80cd942009-07-17 15:54:23 -0700443 int *dataPtr = (int *) ((char *) cUnit->baseAddr + cUnit->dataOffset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700444 ArmLIR *dataLIR = (ArmLIR *) cUnit->wordList;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700445 while (dataLIR) {
446 *dataPtr++ = dataLIR->operands[0];
447 dataLIR = NEXT_LIR(dataLIR);
448 }
449}
450
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700451/* Returns the size of a Jit trace description */
452static int jitTraceDescriptionSize(const JitTraceDescription *desc)
453{
454 int runCount;
455 for (runCount = 0; ; runCount++) {
456 if (desc->trace[runCount].frag.runEnd)
457 break;
458 }
459 return sizeof(JitCodeDesc) + ((runCount+1) * sizeof(JitTraceRun));
460}
461
Ben Chengba4fc8b2009-06-01 13:00:29 -0700462/* Return TRUE if error happens */
463static bool assembleInstructions(CompilationUnit *cUnit, intptr_t startAddr)
464{
465 short *bufferAddr = (short *) cUnit->codeBuffer;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700466 ArmLIR *lir;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700467
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700468 for (lir = (ArmLIR *) cUnit->firstLIRInsn; lir; lir = NEXT_LIR(lir)) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700469 if (lir->opCode < 0) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700470 if ((lir->opCode == ARM_PSEUDO_ALIGN4) &&
Ben Cheng1efc9c52009-06-08 18:25:27 -0700471 /* 1 means padding is needed */
472 (lir->operands[0] == 1)) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700473 *bufferAddr++ = PADDING_MOV_R0_R0;
474 }
475 continue;
476 }
477
Ben Chenge9695e52009-06-16 16:11:47 -0700478 if (lir->isNop) {
479 continue;
480 }
481
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700482 if (lir->opCode == THUMB_LDR_PC_REL ||
483 lir->opCode == THUMB_ADD_PC_REL) {
484 ArmLIR *lirTarget = (ArmLIR *) lir->generic.target;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700485 intptr_t pc = (lir->generic.offset + 4) & ~3;
Ben Cheng38329f52009-07-07 14:19:20 -0700486 /*
487 * Allow an offset (stored in operands[2] to be added to the
488 * PC-relative target. Useful to get to a fixed field inside a
489 * chaining cell.
490 */
491 intptr_t target = lirTarget->generic.offset + lir->operands[2];
Ben Chengba4fc8b2009-06-01 13:00:29 -0700492 int delta = target - pc;
493 if (delta & 0x3) {
494 LOGE("PC-rel distance is not multiples of 4: %d\n", delta);
495 dvmAbort();
496 }
Ben Cheng1efc9c52009-06-08 18:25:27 -0700497 if (delta > 1023) {
498 return true;
499 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700500 lir->operands[1] = delta >> 2;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700501 } else if (lir->opCode == THUMB_B_COND) {
502 ArmLIR *targetLIR = (ArmLIR *) lir->generic.target;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700503 intptr_t pc = lir->generic.offset + 4;
504 intptr_t target = targetLIR->generic.offset;
505 int delta = target - pc;
506 if (delta > 254 || delta < -256) {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700507 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700508 }
509 lir->operands[0] = delta >> 1;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700510 } else if (lir->opCode == THUMB_B_UNCOND) {
511 ArmLIR *targetLIR = (ArmLIR *) lir->generic.target;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700512 intptr_t pc = lir->generic.offset + 4;
513 intptr_t target = targetLIR->generic.offset;
514 int delta = target - pc;
515 if (delta > 2046 || delta < -2048) {
516 LOGE("Unconditional branch distance out of range: %d\n", delta);
517 dvmAbort();
518 }
519 lir->operands[0] = delta >> 1;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700520 } else if (lir->opCode == THUMB_BLX_1) {
521 assert(NEXT_LIR(lir)->opCode == THUMB_BLX_2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700522 /* curPC is Thumb */
523 intptr_t curPC = (startAddr + lir->generic.offset + 4) & ~3;
524 intptr_t target = lir->operands[1];
525
526 /* Match bit[1] in target with base */
527 if (curPC & 0x2) {
528 target |= 0x2;
529 }
530 int delta = target - curPC;
531 assert((delta >= -(1<<22)) && (delta <= ((1<<22)-2)));
532
533 lir->operands[0] = (delta >> 12) & 0x7ff;
534 NEXT_LIR(lir)->operands[0] = (delta>> 1) & 0x7ff;
535 }
536
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700537 ArmEncodingMap *encoder = &EncodingMap[lir->opCode];
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700538 u4 bits = encoder->skeleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700539 int i;
540 for (i = 0; i < 3; i++) {
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700541 u4 value;
542 switch(encoder->fieldLoc[i].kind) {
543 case UNUSED:
544 break;
545 case BITBLT:
546 value = (lir->operands[i] << encoder->fieldLoc[i].start) &
547 ((1 << (encoder->fieldLoc[i].end + 1)) - 1);
548 bits |= value;
549 break;
550 case DFP:
551 /* Snag the 1-bit slice and position it */
552 value = ((lir->operands[i] & 0x10) >> 4) <<
553 encoder->fieldLoc[i].end;
554 /* Extract and position the 4-bit slice */
555 value |= (lir->operands[i] & 0x0f) <<
556 encoder->fieldLoc[i].start;
557 bits |= value;
558 break;
559 case SFP:
560 /* Snag the 1-bit slice and position it */
561 value = (lir->operands[i] & 0x1) <<
562 encoder->fieldLoc[i].end;
563 /* Extract and position the 4-bit slice */
564 value |= ((lir->operands[i] & 0x1e) >> 1) <<
565 encoder->fieldLoc[i].start;
566 bits |= value;
567 break;
568 case IMMSHIFT8:
569 case IMM12:
570 value = ((lir->operands[i] & 0x800) >> 11) << 26;
571 value |= ((lir->operands[i] & 0x700) >> 8) << 12;
572 value |= lir->operands[i] & 0x0ff;
573 break;
574 default:
575 assert(0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700576 }
577 }
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700578 if (encoder->size == 2) {
579 *bufferAddr++ = (bits >> 16) & 0xffff;
580 }
581 *bufferAddr++ = bits & 0xffff;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700582 }
Ben Cheng1efc9c52009-06-08 18:25:27 -0700583 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700584}
585
586/*
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700587 * Translation layout in the code cache. Note that the codeAddress pointer
588 * in JitTable will point directly to the code body (field codeAddress). The
589 * chain cell offset codeAddress - 2, and (if present) executionCount is at
590 * codeAddress - 6.
591 *
592 * +----------------------------+
593 * | Execution count | -> [Optional] 4 bytes
594 * +----------------------------+
595 * +--| Offset to chain cell counts| -> 2 bytes
596 * | +----------------------------+
597 * | | Code body | -> Start address for translation
598 * | | | variable in 2-byte chunks
599 * | . . (JitTable's codeAddress points here)
600 * | . .
601 * | | |
602 * | +----------------------------+
603 * | | Chaining Cells | -> 8 bytes each, must be 4 byte aligned
604 * | . .
605 * | . .
606 * | | |
607 * | +----------------------------+
608 * +->| Chaining cell counts | -> 4 bytes, chain cell counts by type
609 * +----------------------------+
610 * | Trace description | -> variable sized
611 * . .
612 * | |
613 * +----------------------------+
614 * | Literal pool | -> 4-byte aligned, variable size
615 * . .
616 * . .
617 * | |
618 * +----------------------------+
619 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700620 * Go over each instruction in the list and calculate the offset from the top
621 * before sending them off to the assembler. If out-of-range branch distance is
622 * seen rearrange the instructions a bit to correct it.
623 */
Bill Buzbee716f1202009-07-23 13:22:09 -0700624void dvmCompilerAssembleLIR(CompilationUnit *cUnit, JitTranslationInfo *info)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700625{
626 LIR *lir;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700627 ArmLIR *armLIR;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700628 int offset = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700629 int i;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700630 ChainCellCounts chainCellCounts;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700631 int descSize = jitTraceDescriptionSize(cUnit->traceDesc);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700632
Bill Buzbee716f1202009-07-23 13:22:09 -0700633 info->codeAddress = NULL;
634 info->instructionSet = cUnit->instructionSet;
635
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700636 /* Beginning offset needs to allow space for chain cell offset */
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700637 for (armLIR = (ArmLIR *) cUnit->firstLIRInsn;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700638 armLIR;
639 armLIR = NEXT_LIR(armLIR)) {
640 armLIR->generic.offset = offset;
Ben Chenge9695e52009-06-16 16:11:47 -0700641 if (armLIR->opCode >= 0 && !armLIR->isNop) {
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700642 armLIR->size = EncodingMap[armLIR->opCode].size * 2;
643 offset += armLIR->size;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700644 } else if (armLIR->opCode == ARM_PSEUDO_ALIGN4) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700645 if (offset & 0x2) {
646 offset += 2;
647 armLIR->operands[0] = 1;
648 } else {
649 armLIR->operands[0] = 0;
650 }
651 }
652 /* Pseudo opcodes don't consume space */
653 }
654
655 /* Const values have to be word aligned */
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700656 offset = (offset + 3) & ~3;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700657
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700658 /* Add space for chain cell counts & trace description */
Ben Cheng1efc9c52009-06-08 18:25:27 -0700659 u4 chainCellOffset = offset;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700660 ArmLIR *chainCellOffsetLIR = (ArmLIR *) cUnit->chainCellOffsetLIR;
Bill Buzbee6e963e12009-06-17 16:56:19 -0700661 assert(chainCellOffsetLIR);
Ben Cheng1efc9c52009-06-08 18:25:27 -0700662 assert(chainCellOffset < 0x10000);
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700663 assert(chainCellOffsetLIR->opCode == ARM_16BIT_DATA &&
Ben Cheng1efc9c52009-06-08 18:25:27 -0700664 chainCellOffsetLIR->operands[0] == CHAIN_CELL_OFFSET_TAG);
665
Ben Chenge80cd942009-07-17 15:54:23 -0700666 /*
667 * Replace the CHAIN_CELL_OFFSET_TAG with the real value. If trace
668 * profiling is enabled, subtract 4 (occupied by the counter word) from
669 * the absolute offset as the value stored in chainCellOffsetLIR is the
670 * delta from &chainCellOffsetLIR to &ChainCellCounts.
671 */
672 chainCellOffsetLIR->operands[0] =
673 gDvmJit.profile ? (chainCellOffset - 4) : chainCellOffset;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700674
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700675 offset += sizeof(chainCellCounts) + descSize;
676
677 assert((offset & 0x3) == 0); /* Should still be word aligned */
678
679 /* Set up offsets for literals */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700680 cUnit->dataOffset = offset;
681
682 for (lir = cUnit->wordList; lir; lir = lir->next) {
683 lir->offset = offset;
684 offset += 4;
685 }
686
687 cUnit->totalSize = offset;
688
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700689 if (gDvmJit.codeCacheByteUsed + cUnit->totalSize > CODE_CACHE_SIZE) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700690 gDvmJit.codeCacheFull = true;
691 cUnit->baseAddr = NULL;
692 return;
693 }
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700694
695 /* Allocate enough space for the code block */
696 cUnit->codeBuffer = dvmCompilerNew(chainCellOffset, true);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700697 if (cUnit->codeBuffer == NULL) {
698 LOGE("Code buffer allocation failure\n");
699 cUnit->baseAddr = NULL;
700 return;
701 }
702
Ben Cheng1efc9c52009-06-08 18:25:27 -0700703 bool assemblerFailure = assembleInstructions(
Ben Chengba4fc8b2009-06-01 13:00:29 -0700704 cUnit, (intptr_t) gDvmJit.codeCache + gDvmJit.codeCacheByteUsed);
705
Ben Cheng1efc9c52009-06-08 18:25:27 -0700706 /*
707 * Currently the only reason that can cause the assembler to fail is due to
708 * trace length - cut it in half and retry.
709 */
710 if (assemblerFailure) {
711 cUnit->halveInstCount = true;
712 return;
713 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700714
Bill Buzbee6e963e12009-06-17 16:56:19 -0700715
Ben Chengba4fc8b2009-06-01 13:00:29 -0700716 cUnit->baseAddr = (char *) gDvmJit.codeCache + gDvmJit.codeCacheByteUsed;
717 gDvmJit.codeCacheByteUsed += offset;
718
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700719 /* Install the code block */
Ben Cheng1efc9c52009-06-08 18:25:27 -0700720 memcpy((char*)cUnit->baseAddr, cUnit->codeBuffer, chainCellOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700721 gDvmJit.numCompilations++;
722
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700723 /* Install the chaining cell counts */
724 for (i=0; i< CHAINING_CELL_LAST; i++) {
725 chainCellCounts.u.count[i] = cUnit->numChainingCells[i];
726 }
727 memcpy((char*)cUnit->baseAddr + chainCellOffset, &chainCellCounts,
728 sizeof(chainCellCounts));
729
730 /* Install the trace description */
731 memcpy((char*)cUnit->baseAddr + chainCellOffset + sizeof(chainCellCounts),
732 cUnit->traceDesc, descSize);
733
734 /* Write the literals directly into the code cache */
735 installDataContent(cUnit);
736
Ben Chengba4fc8b2009-06-01 13:00:29 -0700737 /* Flush dcache and invalidate the icache to maintain coherence */
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700738 cacheflush((long)cUnit->baseAddr,
Ben Chenge80cd942009-07-17 15:54:23 -0700739 (long)((char *) cUnit->baseAddr + offset), 0);
Bill Buzbee716f1202009-07-23 13:22:09 -0700740
741 /* Record code entry point and instruction set */
742 info->codeAddress = (char*)cUnit->baseAddr + cUnit->headerSize;
743 info->instructionSet = cUnit->instructionSet;
744 /* If applicable, mark low bit to denote thumb */
745 if (info->instructionSet != DALVIK_JIT_ARM)
746 info->codeAddress = (char*)info->codeAddress + 1;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700747}
748
Ben Cheng38329f52009-07-07 14:19:20 -0700749static u4 assembleBXPair(int branchOffset)
750{
751 u4 thumb1, thumb2;
752
753 if ((branchOffset < -2048) | (branchOffset > 2046)) {
754 thumb1 = (0xf000 | ((branchOffset>>12) & 0x7ff));
755 thumb2 = (0xf800 | ((branchOffset>> 1) & 0x7ff));
756 } else {
757 thumb1 = (0xe000 | ((branchOffset>> 1) & 0x7ff));
758 thumb2 = 0x4300; /* nop -> or r0, r0 */
759 }
760
761 return thumb2<<16 | thumb1;
762}
763
Ben Chengba4fc8b2009-06-01 13:00:29 -0700764/*
765 * Perform translation chain operation.
766 * For ARM, we'll use a pair of thumb instructions to generate
767 * an unconditional chaining branch of up to 4MB in distance.
768 * Use a BL, though we don't really need the link. The format is
769 * 111HHooooooooooo
770 * Where HH is 10 for the 1st inst, and 11 for the second and
771 * the "o" field is each instruction's 11-bit contribution to the
772 * 22-bit branch offset.
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700773 * If the target is nearby, use a single-instruction bl.
774 * If one or more threads is suspended, don't chain.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700775 */
776void* dvmJitChain(void* tgtAddr, u4* branchAddr)
777{
778 int baseAddr = (u4) branchAddr + 4;
779 int branchOffset = (int) tgtAddr - baseAddr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700780 u4 newInst;
781
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700782 if (gDvm.sumThreadSuspendCount == 0) {
783 assert((branchOffset >= -(1<<22)) && (branchOffset <= ((1<<22)-2)));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700784
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700785 gDvmJit.translationChains++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700786
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700787 COMPILER_TRACE_CHAINING(
788 LOGD("Jit Runtime: chaining 0x%x to 0x%x\n",
789 (int) branchAddr, (int) tgtAddr & -2));
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700790
Ben Cheng38329f52009-07-07 14:19:20 -0700791 newInst = assembleBXPair(branchOffset);
792
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700793 *branchAddr = newInst;
794 cacheflush((long)branchAddr, (long)branchAddr + 4, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700795 }
796
Ben Chengba4fc8b2009-06-01 13:00:29 -0700797 return tgtAddr;
798}
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700799
800/*
Ben Cheng38329f52009-07-07 14:19:20 -0700801 * This method is called from the invoke templates for virtual and interface
802 * methods to speculatively setup a chain to the callee. The templates are
803 * written in assembly and have setup method, cell, and clazz at r0, r2, and
804 * r3 respectively, so there is a unused argument in the list. Upon return one
805 * of the following three results may happen:
806 * 1) Chain is not setup because the callee is native. Reset the rechain
807 * count to a big number so that it will take a long time before the next
808 * rechain attempt to happen.
809 * 2) Chain is not setup because the callee has not been created yet. Reset
810 * the rechain count to a small number and retry in the near future.
811 * 3) Ask all other threads to stop before patching this chaining cell.
812 * This is required because another thread may have passed the class check
813 * but hasn't reached the chaining cell yet to follow the chain. If we
814 * patch the content before halting the other thread, there could be a
815 * small window for race conditions to happen that it may follow the new
816 * but wrong chain to invoke a different method.
817 */
818const Method *dvmJitToPatchPredictedChain(const Method *method,
819 void *unused,
820 PredictedChainingCell *cell,
821 const ClassObject *clazz)
822{
823 /* Don't come back here for a long time if the method is native */
824 if (dvmIsNativeMethod(method)) {
825 cell->counter = PREDICTED_CHAIN_COUNTER_AVOID;
826 cacheflush((long) cell, (long) (cell+1), 0);
827 COMPILER_TRACE_CHAINING(
828 LOGD("Jit Runtime: predicted chain %p to native method %s ignored",
829 cell, method->name));
830 goto done;
831 }
832 int tgtAddr = (int) dvmJitGetCodeAddr(method->insns);
833
834 /*
835 * Compilation not made yet for the callee. Reset the counter to a small
836 * value and come back to check soon.
837 */
838 if (tgtAddr == 0) {
839 /*
840 * Wait for a few invocations (currently set to be 16) before trying
841 * to setup the chain again.
842 */
843 cell->counter = PREDICTED_CHAIN_COUNTER_DELAY;
844 cacheflush((long) cell, (long) (cell+1), 0);
845 COMPILER_TRACE_CHAINING(
846 LOGD("Jit Runtime: predicted chain %p to method %s delayed",
847 cell, method->name));
848 goto done;
849 }
850
851 /* Stop the world */
852 dvmSuspendAllThreads(SUSPEND_FOR_JIT);
853
854 int baseAddr = (int) cell + 4; // PC is cur_addr + 4
855 int branchOffset = tgtAddr - baseAddr;
856
857 COMPILER_TRACE_CHAINING(
858 LOGD("Jit Runtime: predicted chain %p from %s to %s (%s) patched",
859 cell, cell->clazz ? cell->clazz->descriptor : "NULL",
860 clazz->descriptor,
861 method->name));
862
863 cell->branch = assembleBXPair(branchOffset);
864 cell->clazz = clazz;
865 cell->method = method;
866 cell->counter = PREDICTED_CHAIN_COUNTER_RECHAIN;
867
868 cacheflush((long) cell, (long) (cell+1), 0);
869
870 /* All done - resume all other threads */
871 dvmResumeAllThreads(SUSPEND_FOR_JIT);
872
873done:
874 return method;
875}
876
877/*
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700878 * Unchain a trace given the starting address of the translation
879 * in the code cache. Refer to the diagram in dvmCompilerAssembleLIR.
880 * Returns the address following the last cell unchained. Note that
881 * the incoming codeAddr is a thumb code address, and therefore has
882 * the low bit set.
883 */
884u4* dvmJitUnchain(void* codeAddr)
885{
886 u2* pChainCellOffset = (u2*)((char*)codeAddr - 3);
887 u2 chainCellOffset = *pChainCellOffset;
888 ChainCellCounts *pChainCellCounts =
Ben Chenge80cd942009-07-17 15:54:23 -0700889 (ChainCellCounts*)((char*)codeAddr + chainCellOffset - 3);
Ben Cheng38329f52009-07-07 14:19:20 -0700890 int cellSize;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700891 u4* pChainCells;
892 u4* pStart;
893 u4 thumb1;
894 u4 thumb2;
895 u4 newInst;
896 int i,j;
Ben Cheng38329f52009-07-07 14:19:20 -0700897 PredictedChainingCell *predChainCell;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700898
899 /* Get total count of chain cells */
Ben Cheng38329f52009-07-07 14:19:20 -0700900 for (i = 0, cellSize = 0; i < CHAINING_CELL_LAST; i++) {
901 if (i != CHAINING_CELL_INVOKE_PREDICTED) {
902 cellSize += pChainCellCounts->u.count[i] * 2;
903 } else {
904 cellSize += pChainCellCounts->u.count[i] * 4;
905 }
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700906 }
907
908 /* Locate the beginning of the chain cell region */
Ben Cheng38329f52009-07-07 14:19:20 -0700909 pStart = pChainCells = ((u4 *) pChainCellCounts) - cellSize;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700910
911 /* The cells are sorted in order - walk through them and reset */
912 for (i = 0; i < CHAINING_CELL_LAST; i++) {
Ben Cheng38329f52009-07-07 14:19:20 -0700913 int elemSize = 2; /* Most chaining cell has two words */
914 if (i == CHAINING_CELL_INVOKE_PREDICTED) {
915 elemSize = 4;
916 }
917
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700918 for (j = 0; j < pChainCellCounts->u.count[i]; j++) {
919 int targetOffset;
920 switch(i) {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700921 case CHAINING_CELL_NORMAL:
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700922 targetOffset = offsetof(InterpState,
923 jitToInterpEntries.dvmJitToInterpNormal);
924 break;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700925 case CHAINING_CELL_HOT:
Ben Cheng38329f52009-07-07 14:19:20 -0700926 case CHAINING_CELL_INVOKE_SINGLETON:
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700927 targetOffset = offsetof(InterpState,
928 jitToInterpEntries.dvmJitToTraceSelect);
929 break;
Ben Cheng38329f52009-07-07 14:19:20 -0700930 case CHAINING_CELL_INVOKE_PREDICTED:
931 targetOffset = 0;
932 predChainCell = (PredictedChainingCell *) pChainCells;
933 /* Reset the cell to the init state */
934 predChainCell->branch = PREDICTED_CHAIN_BX_PAIR_INIT;
935 predChainCell->clazz = PREDICTED_CHAIN_CLAZZ_INIT;
936 predChainCell->method = PREDICTED_CHAIN_METHOD_INIT;
937 predChainCell->counter = PREDICTED_CHAIN_COUNTER_INIT;
938 break;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700939 default:
940 dvmAbort();
941 }
Ben Cheng38329f52009-07-07 14:19:20 -0700942 COMPILER_TRACE_CHAINING(
943 LOGD("Jit Runtime: unchaining 0x%x", (int)pChainCells));
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700944 /*
Ben Cheng38329f52009-07-07 14:19:20 -0700945 * Thumb code sequence for a chaining cell is:
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700946 * ldr r0, rGLUE, #<word offset>
947 * blx r0
948 */
Ben Cheng38329f52009-07-07 14:19:20 -0700949 if (i != CHAINING_CELL_INVOKE_PREDICTED) {
950 targetOffset = targetOffset >> 2; /* convert to word offset */
951 thumb1 = 0x6800 | (targetOffset << 6) |
952 (rGLUE << 3) | (r0 << 0);
953 thumb2 = 0x4780 | (r0 << 3);
954 newInst = thumb2<<16 | thumb1;
955 *pChainCells = newInst;
956 }
957 pChainCells += elemSize; /* Advance by a fixed number of words */
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700958 }
959 }
960 return pChainCells;
961}
962
963/* Unchain all translation in the cache. */
964void dvmJitUnchainAll()
965{
966 u4* lowAddress = NULL;
967 u4* highAddress = NULL;
968 unsigned int i;
969 if (gDvmJit.pJitEntryTable != NULL) {
970 COMPILER_TRACE_CHAINING(LOGD("Jit Runtime: unchaining all"));
971 dvmLockMutex(&gDvmJit.tableLock);
Bill Buzbee27176222009-06-09 09:20:16 -0700972 for (i = 0; i < gDvmJit.jitTableSize; i++) {
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700973 if (gDvmJit.pJitEntryTable[i].dPC &&
974 gDvmJit.pJitEntryTable[i].codeAddress) {
975 u4* lastAddress;
976 lastAddress =
977 dvmJitUnchain(gDvmJit.pJitEntryTable[i].codeAddress);
978 if (lowAddress == NULL ||
979 (u4*)gDvmJit.pJitEntryTable[i].codeAddress < lowAddress)
980 lowAddress = lastAddress;
981 if (lastAddress > highAddress)
982 highAddress = lastAddress;
983 }
984 }
985 cacheflush((long)lowAddress, (long)highAddress, 0);
986 dvmUnlockMutex(&gDvmJit.tableLock);
987 }
988}
Bill Buzbee716f1202009-07-23 13:22:09 -0700989
990typedef struct jitProfileAddrToLine {
991 u4 lineNum;
992 u4 bytecodeOffset;
993} jitProfileAddrToLine;
994
995
996/* Callback function to track the bytecode offset/line number relationiship */
997static int addrToLineCb (void *cnxt, u4 bytecodeOffset, u4 lineNum)
998{
999 jitProfileAddrToLine *addrToLine = (jitProfileAddrToLine *) cnxt;
1000
1001 /* Best match so far for this offset */
1002 if (addrToLine->bytecodeOffset >= bytecodeOffset) {
1003 addrToLine->lineNum = lineNum;
1004 }
1005 return 0;
1006}
1007
1008char *getTraceBase(const JitEntry *p)
1009{
1010 return (char*)p->codeAddress -
1011 (6 + (p->u.info.instructionSet == DALVIK_JIT_ARM ? 0 : 1));
1012}
1013
1014/* Dumps profile info for a single trace */
1015static int dumpTraceProfile(JitEntry *p)
1016{
1017 ChainCellCounts* pCellCounts;
1018 char* traceBase;
1019 u4* pExecutionCount;
1020 u2* pCellOffset;
1021 JitTraceDescription *desc;
1022 const Method* method;
1023
1024 traceBase = getTraceBase(p);
1025
1026 if (p->codeAddress == NULL) {
1027 LOGD("TRACEPROFILE 0x%08x 0 NULL 0 0", (int)traceBase);
1028 return 0;
1029 }
1030
1031 pExecutionCount = (u4*) (traceBase);
1032 pCellOffset = (u2*) (traceBase + 4);
1033 pCellCounts = (ChainCellCounts*) ((char *)pCellOffset + *pCellOffset);
1034 desc = (JitTraceDescription*) ((char*)pCellCounts + sizeof(*pCellCounts));
1035 method = desc->method;
1036 char *methodDesc = dexProtoCopyMethodDescriptor(&method->prototype);
1037 jitProfileAddrToLine addrToLine = {0, desc->trace[0].frag.startOffset};
1038
1039 /*
1040 * We may end up decoding the debug information for the same method
1041 * multiple times, but the tradeoff is we don't need to allocate extra
1042 * space to store the addr/line mapping. Since this is a debugging feature
1043 * and done infrequently so the slower but simpler mechanism should work
1044 * just fine.
1045 */
1046 dexDecodeDebugInfo(method->clazz->pDvmDex->pDexFile,
1047 dvmGetMethodCode(method),
1048 method->clazz->descriptor,
1049 method->prototype.protoIdx,
1050 method->accessFlags,
1051 addrToLineCb, NULL, &addrToLine);
1052
1053 LOGD("TRACEPROFILE 0x%08x % 10d [%#x(+%d), %d] %s%s;%s",
1054 (int)traceBase,
1055 *pExecutionCount,
1056 desc->trace[0].frag.startOffset,
1057 desc->trace[0].frag.numInsts,
1058 addrToLine.lineNum,
1059 method->clazz->descriptor, method->name, methodDesc);
1060 free(methodDesc);
1061
1062 return *pExecutionCount;
1063}
1064
1065/* Handy function to retrieve the profile count */
1066static inline int getProfileCount(const JitEntry *entry)
1067{
1068 if (entry->dPC == 0 || entry->codeAddress == 0)
1069 return 0;
1070 u4 *pExecutionCount = (u4 *) getTraceBase(entry);
1071
1072 return *pExecutionCount;
1073}
1074
1075
1076/* qsort callback function */
1077static int sortTraceProfileCount(const void *entry1, const void *entry2)
1078{
1079 const JitEntry *jitEntry1 = entry1;
1080 const JitEntry *jitEntry2 = entry2;
1081
1082 int count1 = getProfileCount(jitEntry1);
1083 int count2 = getProfileCount(jitEntry2);
1084 return (count1 == count2) ? 0 : ((count1 > count2) ? -1 : 1);
1085}
1086
1087/* Sort the trace profile counts and dump them */
1088void dvmCompilerSortAndPrintTraceProfiles()
1089{
1090 JitEntry *sortedEntries;
1091 int numTraces = 0;
1092 unsigned long counts = 0;
1093 unsigned int i;
1094
1095 /* Make sure that the table is not changing */
1096 dvmLockMutex(&gDvmJit.tableLock);
1097
1098 /* Sort the entries by descending order */
1099 sortedEntries = malloc(sizeof(JitEntry) * gDvmJit.jitTableSize);
1100 if (sortedEntries == NULL)
1101 goto done;
1102 memcpy(sortedEntries, gDvmJit.pJitEntryTable,
1103 sizeof(JitEntry) * gDvmJit.jitTableSize);
1104 qsort(sortedEntries, gDvmJit.jitTableSize, sizeof(JitEntry),
1105 sortTraceProfileCount);
1106
1107 /* Dump the sorted entries */
1108 for (i=0; i < gDvmJit.jitTableSize; i++) {
1109 if (sortedEntries[i].dPC != 0) {
1110 counts += dumpTraceProfile(&sortedEntries[i]);
1111 numTraces++;
1112 }
1113 }
1114 if (numTraces == 0)
1115 numTraces = 1;
1116 LOGD("JIT: Average execution count -> %d",(int)(counts / numTraces));
1117
1118 free(sortedEntries);
1119done:
1120 dvmUnlockMutex(&gDvmJit.tableLock);
1121 return;
1122}