blob: fb85253abd737dc75f7dfa1c04cb62e90652c89b [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
Bill Buzbee9727c3d2009-08-01 11:32:36 -070066 * s -> single precision floating point register
67 * S -> double precision floating point register
Ben Chengba4fc8b2009-06-01 13:00:29 -070068 *
69 * [!] escape. To insert "!", use "!!"
70 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -070071/* NOTE: must be kept in sync with enum ArmOpcode from ArmLIR.h */
72ArmEncodingMap EncodingMap[ARM_LAST] = {
Bill Buzbee9bc3df32009-07-30 10:52:29 -070073 ENCODING_MAP(ARM_16BIT_DATA, 0x0000,
74 BITBLT, 15, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -070075 IS_UNARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -070076 "data", "0x!0h(!0d)", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -070077 ENCODING_MAP(THUMB_ADC, 0x4140,
78 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -070079 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -070080 "adc", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -070081 ENCODING_MAP(THUMB_ADD_RRI3, 0x1c00,
82 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -070083 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -070084 "add", "r!0d, r!1d, #!2d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -070085 ENCODING_MAP(THUMB_ADD_RI8, 0x3000,
86 BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -070087 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -070088 "add", "r!0d, r!0d, #!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -070089 ENCODING_MAP(THUMB_ADD_RRR, 0x1800,
90 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -070091 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -070092 "add", "r!0d, r!1d, r!2d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -070093 ENCODING_MAP(THUMB_ADD_RR_LH, 0x4440,
94 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -070095 IS_BINARY_OP | CLOBBER_DEST,
96 "add",
Bill Buzbee716f1202009-07-23 13:22:09 -070097 "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -070098 ENCODING_MAP(THUMB_ADD_RR_HL, 0x4480,
99 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700100 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700101 "add", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700102 ENCODING_MAP(THUMB_ADD_RR_HH, 0x44c0,
103 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700104 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700105 "add", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700106 ENCODING_MAP(THUMB_ADD_PC_REL, 0xa000,
107 BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1,
Ben Cheng38329f52009-07-07 14:19:20 -0700108 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700109 "add", "r!0d, pc, #!1E", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700110 ENCODING_MAP(THUMB_ADD_SP_REL, 0xa800,
111 BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700112 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700113 "add", "r!0d, sp, #!1E", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700114 ENCODING_MAP(THUMB_ADD_SPI7, 0xb000,
115 BITBLT, 6, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700116 IS_UNARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700117 "add", "sp, #!0d*4", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700118 ENCODING_MAP(THUMB_AND_RR, 0x4000,
119 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700120 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700121 "and", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700122 ENCODING_MAP(THUMB_ASR, 0x1000,
123 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700124 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700125 "asr", "r!0d, r!1d, #!2d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700126 ENCODING_MAP(THUMB_ASRV, 0x4100,
127 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700128 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700129 "asr", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700130 ENCODING_MAP(THUMB_B_COND, 0xd000,
131 BITBLT, 7, 0, BITBLT, 11, 8, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700132 IS_BINARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700133 "!1c", "!0t", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700134 ENCODING_MAP(THUMB_B_UNCOND, 0xe000,
135 BITBLT, 10, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700136 NO_OPERAND | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700137 "b", "!0t", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700138 ENCODING_MAP(THUMB_BIC, 0x4380,
139 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700140 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700141 "bic", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700142 ENCODING_MAP(THUMB_BKPT, 0xbe00,
143 BITBLT, 7, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700144 IS_UNARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700145 "bkpt", "!0d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700146 ENCODING_MAP(THUMB_BLX_1, 0xf000,
147 BITBLT, 10, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700148 IS_BINARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700149 "blx_1", "!0u", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700150 ENCODING_MAP(THUMB_BLX_2, 0xe800,
151 BITBLT, 10, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700152 IS_BINARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700153 "blx_2", "!0v", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700154 ENCODING_MAP(THUMB_BL_1, 0xf000,
155 BITBLT, 10, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700156 IS_UNARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700157 "bl_1", "!0u", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700158 ENCODING_MAP(THUMB_BL_2, 0xf800,
159 BITBLT, 10, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700160 IS_UNARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700161 "bl_2", "!0v", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700162 ENCODING_MAP(THUMB_BLX_R, 0x4780,
163 BITBLT, 6, 3, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700164 IS_UNARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700165 "blx", "r!0d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700166 ENCODING_MAP(THUMB_BX, 0x4700,
167 BITBLT, 6, 3, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700168 IS_UNARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700169 "bx", "r!0d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700170 ENCODING_MAP(THUMB_CMN, 0x42c0,
171 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700172 IS_BINARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700173 "cmn", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700174 ENCODING_MAP(THUMB_CMP_RI8, 0x2800,
175 BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700176 IS_BINARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700177 "cmp", "r!0d, #!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700178 ENCODING_MAP(THUMB_CMP_RR, 0x4280,
179 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700180 IS_BINARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700181 "cmp", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700182 ENCODING_MAP(THUMB_CMP_LH, 0x4540,
183 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700184 IS_BINARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700185 "cmp", "r!0d, r!1D", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700186 ENCODING_MAP(THUMB_CMP_HL, 0x4580,
187 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700188 IS_BINARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700189 "cmp", "r!0D, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700190 ENCODING_MAP(THUMB_CMP_HH, 0x45c0,
191 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700192 IS_BINARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700193 "cmp", "r!0D, r!1D", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700194 ENCODING_MAP(THUMB_EOR, 0x4040,
195 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700196 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700197 "eor", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700198 ENCODING_MAP(THUMB_LDMIA, 0xc800,
199 BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700200 IS_BINARY_OP | CLOBBER_DEST | CLOBBER_SRC1,
Bill Buzbee716f1202009-07-23 13:22:09 -0700201 "ldmia", "r!0d!!, <!1R>", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700202 ENCODING_MAP(THUMB_LDR_RRI5, 0x6800,
203 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700204 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700205 "ldr", "r!0d, [r!1d, #!2E]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700206 ENCODING_MAP(THUMB_LDR_RRR, 0x5800,
207 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700208 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700209 "ldr", "r!0d, [r!1d, r!2d]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700210 ENCODING_MAP(THUMB_LDR_PC_REL, 0x4800,
211 BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1,
Ben Cheng38329f52009-07-07 14:19:20 -0700212 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700213 "ldr", "r!0d, [pc, #!1E]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700214 ENCODING_MAP(THUMB_LDR_SP_REL, 0x9800,
215 BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700216 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700217 "ldr", "r!0d, [sp, #!1E]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700218 ENCODING_MAP(THUMB_LDRB_RRI5, 0x7800,
219 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700220 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700221 "ldrb", "r!0d, [r!1d, #2d]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700222 ENCODING_MAP(THUMB_LDRB_RRR, 0x5c00,
223 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700224 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700225 "ldrb", "r!0d, [r!1d, r!2d]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700226 ENCODING_MAP(THUMB_LDRH_RRI5, 0x8800,
227 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700228 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700229 "ldrh", "r!0d, [r!1d, #!2F]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700230 ENCODING_MAP(THUMB_LDRH_RRR, 0x5a00,
231 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700232 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700233 "ldrh", "r!0d, [r!1d, r!2d]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700234 ENCODING_MAP(THUMB_LDRSB_RRR, 0x5600,
235 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700236 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700237 "ldrsb", "r!0d, [r!1d, r!2d]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700238 ENCODING_MAP(THUMB_LDRSH_RRR, 0x5e00,
239 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700240 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700241 "ldrsh", "r!0d, [r!1d, r!2d]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700242 ENCODING_MAP(THUMB_LSL, 0x0000,
243 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700244 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700245 "lsl", "r!0d, r!1d, #!2d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700246 ENCODING_MAP(THUMB_LSLV, 0x4080,
247 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700248 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700249 "lsl", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700250 ENCODING_MAP(THUMB_LSR, 0x0800,
251 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700252 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700253 "lsr", "r!0d, r!1d, #!2d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700254 ENCODING_MAP(THUMB_LSRV, 0x40c0,
255 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700256 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700257 "lsr", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700258 ENCODING_MAP(THUMB_MOV_IMM, 0x2000,
259 BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700260 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700261 "mov", "r!0d, #!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700262 ENCODING_MAP(THUMB_MOV_RR, 0x1c00,
263 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700264 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700265 "mov", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700266 ENCODING_MAP(THUMB_MOV_RR_H2H, 0x46c0,
267 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700268 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700269 "mov", "r!0D, r!1D", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700270 ENCODING_MAP(THUMB_MOV_RR_H2L, 0x4640,
271 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Cheng38329f52009-07-07 14:19:20 -0700272 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700273 "mov", "r!0d, r!1D", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700274 ENCODING_MAP(THUMB_MOV_RR_L2H, 0x4680,
275 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Cheng38329f52009-07-07 14:19:20 -0700276 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700277 "mov", "r!0D, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700278 ENCODING_MAP(THUMB_MUL, 0x4340,
279 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700280 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700281 "mul", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700282 ENCODING_MAP(THUMB_MVN, 0x43c0,
283 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700284 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700285 "mvn", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700286 ENCODING_MAP(THUMB_NEG, 0x4240,
287 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700288 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700289 "neg", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700290 ENCODING_MAP(THUMB_ORR, 0x4300,
291 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700292 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700293 "orr", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700294 ENCODING_MAP(THUMB_POP, 0xbc00,
295 BITBLT, 8, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700296 IS_UNARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700297 "pop", "<!0R>", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700298 ENCODING_MAP(THUMB_PUSH, 0xb400,
299 BITBLT, 8, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700300 IS_UNARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700301 "push", "<!0R>", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700302 ENCODING_MAP(THUMB_ROR, 0x41c0,
303 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700304 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700305 "ror", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700306 ENCODING_MAP(THUMB_SBC, 0x4180,
307 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700308 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700309 "sbc", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700310 ENCODING_MAP(THUMB_STMIA, 0xc000,
311 BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700312 IS_BINARY_OP | CLOBBER_SRC1,
Bill Buzbee716f1202009-07-23 13:22:09 -0700313 "stmia", "r!0d!!, <!1R>", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700314 ENCODING_MAP(THUMB_STR_RRI5, 0x6000,
315 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700316 IS_TERTIARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700317 "str", "r!0d, [r!1d, #!2E]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700318 ENCODING_MAP(THUMB_STR_RRR, 0x5000,
319 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700320 IS_TERTIARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700321 "str", "r!0d, [r!1d, r!2d]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700322 ENCODING_MAP(THUMB_STR_SP_REL, 0x9000,
323 BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700324 IS_BINARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700325 "str", "r!0d, [sp, #!1E]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700326 ENCODING_MAP(THUMB_STRB_RRI5, 0x7000,
327 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700328 IS_TERTIARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700329 "strb", "r!0d, [r!1d, #!2d]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700330 ENCODING_MAP(THUMB_STRB_RRR, 0x5400,
331 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700332 IS_TERTIARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700333 "strb", "r!0d, [r!1d, r!2d]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700334 ENCODING_MAP(THUMB_STRH_RRI5, 0x8000,
335 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700336 IS_TERTIARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700337 "strh", "r!0d, [r!1d, #!2F]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700338 ENCODING_MAP(THUMB_STRH_RRR, 0x5200,
339 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700340 IS_TERTIARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700341 "strh", "r!0d, [r!1d, r!2d]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700342 ENCODING_MAP(THUMB_SUB_RRI3, 0x1e00,
343 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700344 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700345 "sub", "r!0d, r!1d, #!2d]", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700346 ENCODING_MAP(THUMB_SUB_RI8, 0x3800,
347 BITBLT, 10, 8, BITBLT, 7, 0, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700348 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700349 "sub", "r!0d, #!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700350 ENCODING_MAP(THUMB_SUB_RRR, 0x1a00,
351 BITBLT, 2, 0, BITBLT, 5, 3, BITBLT, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700352 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700353 "sub", "r!0d, r!1d, r!2d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700354 ENCODING_MAP(THUMB_SUB_SPI7, 0xb080,
355 BITBLT, 6, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700356 IS_UNARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700357 "sub", "sp, #!0d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700358 ENCODING_MAP(THUMB_SWI, 0xdf00,
359 BITBLT, 7, 0, UNUSED, -1, -1, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700360 IS_UNARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700361 "swi", "!0d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700362 ENCODING_MAP(THUMB_TST, 0x4200,
363 BITBLT, 2, 0, BITBLT, 5, 3, UNUSED, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700364 IS_UNARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700365 "tst", "r!0d, r!1d", 1),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700366 ENCODING_MAP(THUMB2_VLDRS, 0xed900a00,
367 SFP, 22, 12, BITBLT, 19, 16, BITBLT, 7, 0,
368 IS_TERTIARY_OP,
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700369 "vldr", "!0s, [r!1d, #!2E]", 2),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700370 ENCODING_MAP(THUMB2_VLDRD, 0xed900b00,
371 DFP, 22, 12, BITBLT, 19, 16, BITBLT, 7, 0,
372 IS_TERTIARY_OP,
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700373 "vldr", "!0S, [r!1d, #!2E]", 2),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700374 ENCODING_MAP(THUMB2_VMULS, 0xee200a00,
375 SFP, 22, 12, SFP, 7, 16, SFP, 5, 0,
376 IS_TERTIARY_OP,
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700377 "vmuls", "!0s, !1s, !2s", 2),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700378 ENCODING_MAP(THUMB2_VMULD, 0xee200b00,
379 DFP, 22, 12, DFP, 7, 16, DFP, 5, 0,
380 IS_TERTIARY_OP,
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700381 "vmuld", "!0S, !1S, !2S", 2),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700382 ENCODING_MAP(THUMB2_VSTRS, 0xed800a00,
383 SFP, 22, 12, BITBLT, 19, 16, BITBLT, 7, 0,
384 IS_TERTIARY_OP,
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700385 "vstr", "!0s, [r!1d, #!2E]", 2),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700386 ENCODING_MAP(THUMB2_VSTRD, 0xed800b00,
387 DFP, 22, 12, BITBLT, 19, 16, BITBLT, 7, 0,
388 IS_TERTIARY_OP,
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700389 "vstr", "!0S, [r!1d, #!2E]", 2),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700390 ENCODING_MAP(THUMB2_VSUBS, 0xee300a40,
391 SFP, 22, 12, SFP, 7, 16, SFP, 5, 0,
392 IS_TERTIARY_OP,
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700393 "vsub", "!0s, !1s, !2s", 2),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700394 ENCODING_MAP(THUMB2_VSUBD, 0xee300b40,
395 DFP, 22, 12, DFP, 7, 16, DFP, 5, 0,
396 IS_TERTIARY_OP,
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700397 "vsub", "!0S, !1S, !2S", 2),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700398 ENCODING_MAP(THUMB2_VADDS, 0xee300a00,
399 SFP, 22, 12, SFP, 7, 16, SFP, 5, 0,
400 IS_TERTIARY_OP,
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700401 "vadd", "!0s, !1s, !2s", 2),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700402 ENCODING_MAP(THUMB2_VADDD, 0xee300b00,
403 DFP, 22, 12, DFP, 7, 16, DFP, 5, 0,
404 IS_TERTIARY_OP,
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700405 "vadd", "!0S, !1S, !2S", 2),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700406 ENCODING_MAP(THUMB2_VDIVS, 0xee800a00,
407 SFP, 22, 12, SFP, 7, 16, SFP, 5, 0,
408 IS_TERTIARY_OP,
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700409 "vdivs", "!0s, !1s, !2s", 2),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700410 ENCODING_MAP(THUMB2_VDIVD, 0xee800b00,
411 DFP, 22, 12, DFP, 7, 16, DFP, 5, 0,
412 IS_TERTIARY_OP,
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700413 "vdivs", "!0S, !1S, !2S", 2),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700414 ENCODING_MAP(THUMB2_VCVTIF, 0xeeb80ac0,
415 SFP, 22, 12, SFP, 5, 0, UNUSED, -1, -1,
416 IS_BINARY_OP,
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700417 "vcvt.f32", "!0s, !1s", 2),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700418 ENCODING_MAP(THUMB2_VCVTID, 0xeeb80bc0,
419 DFP, 22, 12, SFP, 5, 0, UNUSED, -1, -1,
420 IS_BINARY_OP,
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700421 "vcvt.f64", "!0S, !1s", 2),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700422 ENCODING_MAP(THUMB2_VCVTFI, 0xeebd0ac0,
423 SFP, 22, 12, SFP, 5, 0, UNUSED, -1, -1,
424 IS_BINARY_OP,
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700425 "vcvt.s32.f32 ", "!0s, !1s", 2),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700426 ENCODING_MAP(THUMB2_VCVTDI, 0xeebd0bc0,
427 SFP, 22, 12, DFP, 5, 0, UNUSED, -1, -1,
428 IS_BINARY_OP,
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700429 "vcvt.s32.f64 ", "!0s, !1S", 2),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700430 ENCODING_MAP(THUMB2_VCVTFD, 0xeeb70ac0,
431 DFP, 22, 12, SFP, 5, 0, UNUSED, -1, -1,
432 IS_BINARY_OP,
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700433 "vcvt.f64.f32 ", "!0S, !1s", 2),
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700434 ENCODING_MAP(THUMB2_VCVTDF, 0xeeb70bc0,
435 SFP, 22, 12, DFP, 5, 0, UNUSED, -1, -1,
436 IS_BINARY_OP,
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700437 "vcvt.f32.f64 ", "!0s, !1S", 2),
438 ENCODING_MAP(THUMB2_VSQRTS, 0xeeb10ac0,
439 SFP, 22, 12, SFP, 5, 0, UNUSED, -1, -1,
440 IS_BINARY_OP,
441 "vsqrt.f32 ", "!0s, !1s", 2),
442 ENCODING_MAP(THUMB2_VSQRTD, 0xeeb10bc0,
443 DFP, 22, 12, DFP, 5, 0, UNUSED, -1, -1,
444 IS_BINARY_OP,
445 "vsqrt.f64 ", "!0S, !1S", 2),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700446};
447
448#define PADDING_MOV_R0_R0 0x1C00
449
450/* Write the numbers in the literal pool to the codegen stream */
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700451static void installDataContent(CompilationUnit *cUnit)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700452{
Ben Chenge80cd942009-07-17 15:54:23 -0700453 int *dataPtr = (int *) ((char *) cUnit->baseAddr + cUnit->dataOffset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700454 ArmLIR *dataLIR = (ArmLIR *) cUnit->wordList;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700455 while (dataLIR) {
456 *dataPtr++ = dataLIR->operands[0];
457 dataLIR = NEXT_LIR(dataLIR);
458 }
459}
460
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700461/* Returns the size of a Jit trace description */
462static int jitTraceDescriptionSize(const JitTraceDescription *desc)
463{
464 int runCount;
465 for (runCount = 0; ; runCount++) {
466 if (desc->trace[runCount].frag.runEnd)
467 break;
468 }
469 return sizeof(JitCodeDesc) + ((runCount+1) * sizeof(JitTraceRun));
470}
471
Ben Chengba4fc8b2009-06-01 13:00:29 -0700472/* Return TRUE if error happens */
473static bool assembleInstructions(CompilationUnit *cUnit, intptr_t startAddr)
474{
475 short *bufferAddr = (short *) cUnit->codeBuffer;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700476 ArmLIR *lir;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700477
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700478 for (lir = (ArmLIR *) cUnit->firstLIRInsn; lir; lir = NEXT_LIR(lir)) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700479 if (lir->opCode < 0) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700480 if ((lir->opCode == ARM_PSEUDO_ALIGN4) &&
Ben Cheng1efc9c52009-06-08 18:25:27 -0700481 /* 1 means padding is needed */
482 (lir->operands[0] == 1)) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700483 *bufferAddr++ = PADDING_MOV_R0_R0;
484 }
485 continue;
486 }
487
Ben Chenge9695e52009-06-16 16:11:47 -0700488 if (lir->isNop) {
489 continue;
490 }
491
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700492 if (lir->opCode == THUMB_LDR_PC_REL ||
493 lir->opCode == THUMB_ADD_PC_REL) {
494 ArmLIR *lirTarget = (ArmLIR *) lir->generic.target;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700495 intptr_t pc = (lir->generic.offset + 4) & ~3;
Ben Cheng38329f52009-07-07 14:19:20 -0700496 /*
497 * Allow an offset (stored in operands[2] to be added to the
498 * PC-relative target. Useful to get to a fixed field inside a
499 * chaining cell.
500 */
501 intptr_t target = lirTarget->generic.offset + lir->operands[2];
Ben Chengba4fc8b2009-06-01 13:00:29 -0700502 int delta = target - pc;
503 if (delta & 0x3) {
504 LOGE("PC-rel distance is not multiples of 4: %d\n", delta);
505 dvmAbort();
506 }
Ben Cheng1efc9c52009-06-08 18:25:27 -0700507 if (delta > 1023) {
508 return true;
509 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700510 lir->operands[1] = delta >> 2;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700511 } else if (lir->opCode == THUMB_B_COND) {
512 ArmLIR *targetLIR = (ArmLIR *) lir->generic.target;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700513 intptr_t pc = lir->generic.offset + 4;
514 intptr_t target = targetLIR->generic.offset;
515 int delta = target - pc;
516 if (delta > 254 || delta < -256) {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700517 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700518 }
519 lir->operands[0] = delta >> 1;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700520 } else if (lir->opCode == THUMB_B_UNCOND) {
521 ArmLIR *targetLIR = (ArmLIR *) lir->generic.target;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700522 intptr_t pc = lir->generic.offset + 4;
523 intptr_t target = targetLIR->generic.offset;
524 int delta = target - pc;
525 if (delta > 2046 || delta < -2048) {
526 LOGE("Unconditional branch distance out of range: %d\n", delta);
527 dvmAbort();
528 }
529 lir->operands[0] = delta >> 1;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700530 } else if (lir->opCode == THUMB_BLX_1) {
531 assert(NEXT_LIR(lir)->opCode == THUMB_BLX_2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700532 /* curPC is Thumb */
533 intptr_t curPC = (startAddr + lir->generic.offset + 4) & ~3;
534 intptr_t target = lir->operands[1];
535
536 /* Match bit[1] in target with base */
537 if (curPC & 0x2) {
538 target |= 0x2;
539 }
540 int delta = target - curPC;
541 assert((delta >= -(1<<22)) && (delta <= ((1<<22)-2)));
542
543 lir->operands[0] = (delta >> 12) & 0x7ff;
544 NEXT_LIR(lir)->operands[0] = (delta>> 1) & 0x7ff;
545 }
546
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700547 ArmEncodingMap *encoder = &EncodingMap[lir->opCode];
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700548 u4 bits = encoder->skeleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700549 int i;
550 for (i = 0; i < 3; i++) {
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700551 u4 value;
552 switch(encoder->fieldLoc[i].kind) {
553 case UNUSED:
554 break;
555 case BITBLT:
556 value = (lir->operands[i] << encoder->fieldLoc[i].start) &
557 ((1 << (encoder->fieldLoc[i].end + 1)) - 1);
558 bits |= value;
559 break;
560 case DFP:
561 /* Snag the 1-bit slice and position it */
562 value = ((lir->operands[i] & 0x10) >> 4) <<
563 encoder->fieldLoc[i].end;
564 /* Extract and position the 4-bit slice */
565 value |= (lir->operands[i] & 0x0f) <<
566 encoder->fieldLoc[i].start;
567 bits |= value;
568 break;
569 case SFP:
570 /* Snag the 1-bit slice and position it */
571 value = (lir->operands[i] & 0x1) <<
572 encoder->fieldLoc[i].end;
573 /* Extract and position the 4-bit slice */
574 value |= ((lir->operands[i] & 0x1e) >> 1) <<
575 encoder->fieldLoc[i].start;
576 bits |= value;
577 break;
578 case IMMSHIFT8:
579 case IMM12:
580 value = ((lir->operands[i] & 0x800) >> 11) << 26;
581 value |= ((lir->operands[i] & 0x700) >> 8) << 12;
582 value |= lir->operands[i] & 0x0ff;
583 break;
584 default:
585 assert(0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700586 }
587 }
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700588 if (encoder->size == 2) {
589 *bufferAddr++ = (bits >> 16) & 0xffff;
590 }
591 *bufferAddr++ = bits & 0xffff;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700592 }
Ben Cheng1efc9c52009-06-08 18:25:27 -0700593 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700594}
595
596/*
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700597 * Translation layout in the code cache. Note that the codeAddress pointer
598 * in JitTable will point directly to the code body (field codeAddress). The
599 * chain cell offset codeAddress - 2, and (if present) executionCount is at
600 * codeAddress - 6.
601 *
602 * +----------------------------+
603 * | Execution count | -> [Optional] 4 bytes
604 * +----------------------------+
605 * +--| Offset to chain cell counts| -> 2 bytes
606 * | +----------------------------+
607 * | | Code body | -> Start address for translation
608 * | | | variable in 2-byte chunks
609 * | . . (JitTable's codeAddress points here)
610 * | . .
611 * | | |
612 * | +----------------------------+
613 * | | Chaining Cells | -> 8 bytes each, must be 4 byte aligned
614 * | . .
615 * | . .
616 * | | |
617 * | +----------------------------+
618 * +->| Chaining cell counts | -> 4 bytes, chain cell counts by type
619 * +----------------------------+
620 * | Trace description | -> variable sized
621 * . .
622 * | |
623 * +----------------------------+
624 * | Literal pool | -> 4-byte aligned, variable size
625 * . .
626 * . .
627 * | |
628 * +----------------------------+
629 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700630 * Go over each instruction in the list and calculate the offset from the top
631 * before sending them off to the assembler. If out-of-range branch distance is
632 * seen rearrange the instructions a bit to correct it.
633 */
Bill Buzbee716f1202009-07-23 13:22:09 -0700634void dvmCompilerAssembleLIR(CompilationUnit *cUnit, JitTranslationInfo *info)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700635{
636 LIR *lir;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700637 ArmLIR *armLIR;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700638 int offset = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700639 int i;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700640 ChainCellCounts chainCellCounts;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700641 int descSize = jitTraceDescriptionSize(cUnit->traceDesc);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700642
Bill Buzbee716f1202009-07-23 13:22:09 -0700643 info->codeAddress = NULL;
644 info->instructionSet = cUnit->instructionSet;
645
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700646 /* Beginning offset needs to allow space for chain cell offset */
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700647 for (armLIR = (ArmLIR *) cUnit->firstLIRInsn;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700648 armLIR;
649 armLIR = NEXT_LIR(armLIR)) {
650 armLIR->generic.offset = offset;
Ben Chenge9695e52009-06-16 16:11:47 -0700651 if (armLIR->opCode >= 0 && !armLIR->isNop) {
Bill Buzbee9bc3df32009-07-30 10:52:29 -0700652 armLIR->size = EncodingMap[armLIR->opCode].size * 2;
653 offset += armLIR->size;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700654 } else if (armLIR->opCode == ARM_PSEUDO_ALIGN4) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700655 if (offset & 0x2) {
656 offset += 2;
657 armLIR->operands[0] = 1;
658 } else {
659 armLIR->operands[0] = 0;
660 }
661 }
662 /* Pseudo opcodes don't consume space */
663 }
664
665 /* Const values have to be word aligned */
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700666 offset = (offset + 3) & ~3;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700667
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700668 /* Add space for chain cell counts & trace description */
Ben Cheng1efc9c52009-06-08 18:25:27 -0700669 u4 chainCellOffset = offset;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700670 ArmLIR *chainCellOffsetLIR = (ArmLIR *) cUnit->chainCellOffsetLIR;
Bill Buzbee6e963e12009-06-17 16:56:19 -0700671 assert(chainCellOffsetLIR);
Ben Cheng1efc9c52009-06-08 18:25:27 -0700672 assert(chainCellOffset < 0x10000);
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700673 assert(chainCellOffsetLIR->opCode == ARM_16BIT_DATA &&
Ben Cheng1efc9c52009-06-08 18:25:27 -0700674 chainCellOffsetLIR->operands[0] == CHAIN_CELL_OFFSET_TAG);
675
Ben Chenge80cd942009-07-17 15:54:23 -0700676 /*
677 * Replace the CHAIN_CELL_OFFSET_TAG with the real value. If trace
678 * profiling is enabled, subtract 4 (occupied by the counter word) from
679 * the absolute offset as the value stored in chainCellOffsetLIR is the
680 * delta from &chainCellOffsetLIR to &ChainCellCounts.
681 */
682 chainCellOffsetLIR->operands[0] =
683 gDvmJit.profile ? (chainCellOffset - 4) : chainCellOffset;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700684
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700685 offset += sizeof(chainCellCounts) + descSize;
686
687 assert((offset & 0x3) == 0); /* Should still be word aligned */
688
689 /* Set up offsets for literals */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700690 cUnit->dataOffset = offset;
691
692 for (lir = cUnit->wordList; lir; lir = lir->next) {
693 lir->offset = offset;
694 offset += 4;
695 }
696
697 cUnit->totalSize = offset;
698
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700699 if (gDvmJit.codeCacheByteUsed + cUnit->totalSize > CODE_CACHE_SIZE) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700700 gDvmJit.codeCacheFull = true;
701 cUnit->baseAddr = NULL;
702 return;
703 }
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700704
705 /* Allocate enough space for the code block */
706 cUnit->codeBuffer = dvmCompilerNew(chainCellOffset, true);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700707 if (cUnit->codeBuffer == NULL) {
708 LOGE("Code buffer allocation failure\n");
709 cUnit->baseAddr = NULL;
710 return;
711 }
712
Ben Cheng1efc9c52009-06-08 18:25:27 -0700713 bool assemblerFailure = assembleInstructions(
Ben Chengba4fc8b2009-06-01 13:00:29 -0700714 cUnit, (intptr_t) gDvmJit.codeCache + gDvmJit.codeCacheByteUsed);
715
Ben Cheng1efc9c52009-06-08 18:25:27 -0700716 /*
717 * Currently the only reason that can cause the assembler to fail is due to
718 * trace length - cut it in half and retry.
719 */
720 if (assemblerFailure) {
721 cUnit->halveInstCount = true;
722 return;
723 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700724
Bill Buzbee6e963e12009-06-17 16:56:19 -0700725
Ben Chengba4fc8b2009-06-01 13:00:29 -0700726 cUnit->baseAddr = (char *) gDvmJit.codeCache + gDvmJit.codeCacheByteUsed;
727 gDvmJit.codeCacheByteUsed += offset;
728
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700729 /* Install the code block */
Ben Cheng1efc9c52009-06-08 18:25:27 -0700730 memcpy((char*)cUnit->baseAddr, cUnit->codeBuffer, chainCellOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700731 gDvmJit.numCompilations++;
732
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700733 /* Install the chaining cell counts */
734 for (i=0; i< CHAINING_CELL_LAST; i++) {
735 chainCellCounts.u.count[i] = cUnit->numChainingCells[i];
736 }
737 memcpy((char*)cUnit->baseAddr + chainCellOffset, &chainCellCounts,
738 sizeof(chainCellCounts));
739
740 /* Install the trace description */
741 memcpy((char*)cUnit->baseAddr + chainCellOffset + sizeof(chainCellCounts),
742 cUnit->traceDesc, descSize);
743
744 /* Write the literals directly into the code cache */
745 installDataContent(cUnit);
746
Ben Chengba4fc8b2009-06-01 13:00:29 -0700747 /* Flush dcache and invalidate the icache to maintain coherence */
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700748 cacheflush((long)cUnit->baseAddr,
Ben Chenge80cd942009-07-17 15:54:23 -0700749 (long)((char *) cUnit->baseAddr + offset), 0);
Bill Buzbee716f1202009-07-23 13:22:09 -0700750
751 /* Record code entry point and instruction set */
752 info->codeAddress = (char*)cUnit->baseAddr + cUnit->headerSize;
753 info->instructionSet = cUnit->instructionSet;
754 /* If applicable, mark low bit to denote thumb */
755 if (info->instructionSet != DALVIK_JIT_ARM)
756 info->codeAddress = (char*)info->codeAddress + 1;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700757}
758
Ben Cheng38329f52009-07-07 14:19:20 -0700759static u4 assembleBXPair(int branchOffset)
760{
761 u4 thumb1, thumb2;
762
763 if ((branchOffset < -2048) | (branchOffset > 2046)) {
764 thumb1 = (0xf000 | ((branchOffset>>12) & 0x7ff));
765 thumb2 = (0xf800 | ((branchOffset>> 1) & 0x7ff));
766 } else {
767 thumb1 = (0xe000 | ((branchOffset>> 1) & 0x7ff));
768 thumb2 = 0x4300; /* nop -> or r0, r0 */
769 }
770
771 return thumb2<<16 | thumb1;
772}
773
Ben Chengba4fc8b2009-06-01 13:00:29 -0700774/*
775 * Perform translation chain operation.
776 * For ARM, we'll use a pair of thumb instructions to generate
777 * an unconditional chaining branch of up to 4MB in distance.
778 * Use a BL, though we don't really need the link. The format is
779 * 111HHooooooooooo
780 * Where HH is 10 for the 1st inst, and 11 for the second and
781 * the "o" field is each instruction's 11-bit contribution to the
782 * 22-bit branch offset.
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700783 * If the target is nearby, use a single-instruction bl.
784 * If one or more threads is suspended, don't chain.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700785 */
786void* dvmJitChain(void* tgtAddr, u4* branchAddr)
787{
788 int baseAddr = (u4) branchAddr + 4;
789 int branchOffset = (int) tgtAddr - baseAddr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700790 u4 newInst;
791
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700792 if (gDvm.sumThreadSuspendCount == 0) {
793 assert((branchOffset >= -(1<<22)) && (branchOffset <= ((1<<22)-2)));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700794
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700795 gDvmJit.translationChains++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700796
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700797 COMPILER_TRACE_CHAINING(
798 LOGD("Jit Runtime: chaining 0x%x to 0x%x\n",
799 (int) branchAddr, (int) tgtAddr & -2));
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700800
Ben Cheng38329f52009-07-07 14:19:20 -0700801 newInst = assembleBXPair(branchOffset);
802
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700803 *branchAddr = newInst;
804 cacheflush((long)branchAddr, (long)branchAddr + 4, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700805 }
806
Ben Chengba4fc8b2009-06-01 13:00:29 -0700807 return tgtAddr;
808}
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700809
810/*
Ben Cheng38329f52009-07-07 14:19:20 -0700811 * This method is called from the invoke templates for virtual and interface
812 * methods to speculatively setup a chain to the callee. The templates are
813 * written in assembly and have setup method, cell, and clazz at r0, r2, and
814 * r3 respectively, so there is a unused argument in the list. Upon return one
815 * of the following three results may happen:
816 * 1) Chain is not setup because the callee is native. Reset the rechain
817 * count to a big number so that it will take a long time before the next
818 * rechain attempt to happen.
819 * 2) Chain is not setup because the callee has not been created yet. Reset
820 * the rechain count to a small number and retry in the near future.
821 * 3) Ask all other threads to stop before patching this chaining cell.
822 * This is required because another thread may have passed the class check
823 * but hasn't reached the chaining cell yet to follow the chain. If we
824 * patch the content before halting the other thread, there could be a
825 * small window for race conditions to happen that it may follow the new
826 * but wrong chain to invoke a different method.
827 */
828const Method *dvmJitToPatchPredictedChain(const Method *method,
829 void *unused,
830 PredictedChainingCell *cell,
831 const ClassObject *clazz)
832{
833 /* Don't come back here for a long time if the method is native */
834 if (dvmIsNativeMethod(method)) {
835 cell->counter = PREDICTED_CHAIN_COUNTER_AVOID;
836 cacheflush((long) cell, (long) (cell+1), 0);
837 COMPILER_TRACE_CHAINING(
838 LOGD("Jit Runtime: predicted chain %p to native method %s ignored",
839 cell, method->name));
840 goto done;
841 }
842 int tgtAddr = (int) dvmJitGetCodeAddr(method->insns);
843
844 /*
845 * Compilation not made yet for the callee. Reset the counter to a small
846 * value and come back to check soon.
847 */
848 if (tgtAddr == 0) {
849 /*
850 * Wait for a few invocations (currently set to be 16) before trying
851 * to setup the chain again.
852 */
853 cell->counter = PREDICTED_CHAIN_COUNTER_DELAY;
854 cacheflush((long) cell, (long) (cell+1), 0);
855 COMPILER_TRACE_CHAINING(
856 LOGD("Jit Runtime: predicted chain %p to method %s delayed",
857 cell, method->name));
858 goto done;
859 }
860
861 /* Stop the world */
862 dvmSuspendAllThreads(SUSPEND_FOR_JIT);
863
864 int baseAddr = (int) cell + 4; // PC is cur_addr + 4
865 int branchOffset = tgtAddr - baseAddr;
866
867 COMPILER_TRACE_CHAINING(
868 LOGD("Jit Runtime: predicted chain %p from %s to %s (%s) patched",
869 cell, cell->clazz ? cell->clazz->descriptor : "NULL",
870 clazz->descriptor,
871 method->name));
872
873 cell->branch = assembleBXPair(branchOffset);
874 cell->clazz = clazz;
875 cell->method = method;
876 cell->counter = PREDICTED_CHAIN_COUNTER_RECHAIN;
877
878 cacheflush((long) cell, (long) (cell+1), 0);
879
880 /* All done - resume all other threads */
881 dvmResumeAllThreads(SUSPEND_FOR_JIT);
882
883done:
884 return method;
885}
886
887/*
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700888 * Unchain a trace given the starting address of the translation
889 * in the code cache. Refer to the diagram in dvmCompilerAssembleLIR.
890 * Returns the address following the last cell unchained. Note that
891 * the incoming codeAddr is a thumb code address, and therefore has
892 * the low bit set.
893 */
894u4* dvmJitUnchain(void* codeAddr)
895{
896 u2* pChainCellOffset = (u2*)((char*)codeAddr - 3);
897 u2 chainCellOffset = *pChainCellOffset;
898 ChainCellCounts *pChainCellCounts =
Ben Chenge80cd942009-07-17 15:54:23 -0700899 (ChainCellCounts*)((char*)codeAddr + chainCellOffset - 3);
Ben Cheng38329f52009-07-07 14:19:20 -0700900 int cellSize;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700901 u4* pChainCells;
902 u4* pStart;
903 u4 thumb1;
904 u4 thumb2;
905 u4 newInst;
906 int i,j;
Ben Cheng38329f52009-07-07 14:19:20 -0700907 PredictedChainingCell *predChainCell;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700908
909 /* Get total count of chain cells */
Ben Cheng38329f52009-07-07 14:19:20 -0700910 for (i = 0, cellSize = 0; i < CHAINING_CELL_LAST; i++) {
911 if (i != CHAINING_CELL_INVOKE_PREDICTED) {
912 cellSize += pChainCellCounts->u.count[i] * 2;
913 } else {
914 cellSize += pChainCellCounts->u.count[i] * 4;
915 }
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700916 }
917
918 /* Locate the beginning of the chain cell region */
Ben Cheng38329f52009-07-07 14:19:20 -0700919 pStart = pChainCells = ((u4 *) pChainCellCounts) - cellSize;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700920
921 /* The cells are sorted in order - walk through them and reset */
922 for (i = 0; i < CHAINING_CELL_LAST; i++) {
Ben Cheng38329f52009-07-07 14:19:20 -0700923 int elemSize = 2; /* Most chaining cell has two words */
924 if (i == CHAINING_CELL_INVOKE_PREDICTED) {
925 elemSize = 4;
926 }
927
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700928 for (j = 0; j < pChainCellCounts->u.count[i]; j++) {
929 int targetOffset;
930 switch(i) {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700931 case CHAINING_CELL_NORMAL:
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700932 targetOffset = offsetof(InterpState,
933 jitToInterpEntries.dvmJitToInterpNormal);
934 break;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700935 case CHAINING_CELL_HOT:
Ben Cheng38329f52009-07-07 14:19:20 -0700936 case CHAINING_CELL_INVOKE_SINGLETON:
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700937 targetOffset = offsetof(InterpState,
938 jitToInterpEntries.dvmJitToTraceSelect);
939 break;
Ben Cheng38329f52009-07-07 14:19:20 -0700940 case CHAINING_CELL_INVOKE_PREDICTED:
941 targetOffset = 0;
942 predChainCell = (PredictedChainingCell *) pChainCells;
943 /* Reset the cell to the init state */
944 predChainCell->branch = PREDICTED_CHAIN_BX_PAIR_INIT;
945 predChainCell->clazz = PREDICTED_CHAIN_CLAZZ_INIT;
946 predChainCell->method = PREDICTED_CHAIN_METHOD_INIT;
947 predChainCell->counter = PREDICTED_CHAIN_COUNTER_INIT;
948 break;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700949 default:
950 dvmAbort();
951 }
Ben Cheng38329f52009-07-07 14:19:20 -0700952 COMPILER_TRACE_CHAINING(
953 LOGD("Jit Runtime: unchaining 0x%x", (int)pChainCells));
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700954 /*
Ben Cheng38329f52009-07-07 14:19:20 -0700955 * Thumb code sequence for a chaining cell is:
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700956 * ldr r0, rGLUE, #<word offset>
957 * blx r0
958 */
Ben Cheng38329f52009-07-07 14:19:20 -0700959 if (i != CHAINING_CELL_INVOKE_PREDICTED) {
960 targetOffset = targetOffset >> 2; /* convert to word offset */
961 thumb1 = 0x6800 | (targetOffset << 6) |
962 (rGLUE << 3) | (r0 << 0);
963 thumb2 = 0x4780 | (r0 << 3);
964 newInst = thumb2<<16 | thumb1;
965 *pChainCells = newInst;
966 }
967 pChainCells += elemSize; /* Advance by a fixed number of words */
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700968 }
969 }
970 return pChainCells;
971}
972
973/* Unchain all translation in the cache. */
974void dvmJitUnchainAll()
975{
976 u4* lowAddress = NULL;
977 u4* highAddress = NULL;
978 unsigned int i;
979 if (gDvmJit.pJitEntryTable != NULL) {
980 COMPILER_TRACE_CHAINING(LOGD("Jit Runtime: unchaining all"));
981 dvmLockMutex(&gDvmJit.tableLock);
Bill Buzbee27176222009-06-09 09:20:16 -0700982 for (i = 0; i < gDvmJit.jitTableSize; i++) {
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700983 if (gDvmJit.pJitEntryTable[i].dPC &&
984 gDvmJit.pJitEntryTable[i].codeAddress) {
985 u4* lastAddress;
986 lastAddress =
987 dvmJitUnchain(gDvmJit.pJitEntryTable[i].codeAddress);
988 if (lowAddress == NULL ||
989 (u4*)gDvmJit.pJitEntryTable[i].codeAddress < lowAddress)
990 lowAddress = lastAddress;
991 if (lastAddress > highAddress)
992 highAddress = lastAddress;
993 }
994 }
995 cacheflush((long)lowAddress, (long)highAddress, 0);
996 dvmUnlockMutex(&gDvmJit.tableLock);
997 }
998}
Bill Buzbee716f1202009-07-23 13:22:09 -0700999
1000typedef struct jitProfileAddrToLine {
1001 u4 lineNum;
1002 u4 bytecodeOffset;
1003} jitProfileAddrToLine;
1004
1005
1006/* Callback function to track the bytecode offset/line number relationiship */
1007static int addrToLineCb (void *cnxt, u4 bytecodeOffset, u4 lineNum)
1008{
1009 jitProfileAddrToLine *addrToLine = (jitProfileAddrToLine *) cnxt;
1010
1011 /* Best match so far for this offset */
1012 if (addrToLine->bytecodeOffset >= bytecodeOffset) {
1013 addrToLine->lineNum = lineNum;
1014 }
1015 return 0;
1016}
1017
1018char *getTraceBase(const JitEntry *p)
1019{
1020 return (char*)p->codeAddress -
1021 (6 + (p->u.info.instructionSet == DALVIK_JIT_ARM ? 0 : 1));
1022}
1023
1024/* Dumps profile info for a single trace */
1025static int dumpTraceProfile(JitEntry *p)
1026{
1027 ChainCellCounts* pCellCounts;
1028 char* traceBase;
1029 u4* pExecutionCount;
1030 u2* pCellOffset;
1031 JitTraceDescription *desc;
1032 const Method* method;
1033
1034 traceBase = getTraceBase(p);
1035
1036 if (p->codeAddress == NULL) {
1037 LOGD("TRACEPROFILE 0x%08x 0 NULL 0 0", (int)traceBase);
1038 return 0;
1039 }
1040
1041 pExecutionCount = (u4*) (traceBase);
1042 pCellOffset = (u2*) (traceBase + 4);
1043 pCellCounts = (ChainCellCounts*) ((char *)pCellOffset + *pCellOffset);
1044 desc = (JitTraceDescription*) ((char*)pCellCounts + sizeof(*pCellCounts));
1045 method = desc->method;
1046 char *methodDesc = dexProtoCopyMethodDescriptor(&method->prototype);
1047 jitProfileAddrToLine addrToLine = {0, desc->trace[0].frag.startOffset};
1048
1049 /*
1050 * We may end up decoding the debug information for the same method
1051 * multiple times, but the tradeoff is we don't need to allocate extra
1052 * space to store the addr/line mapping. Since this is a debugging feature
1053 * and done infrequently so the slower but simpler mechanism should work
1054 * just fine.
1055 */
1056 dexDecodeDebugInfo(method->clazz->pDvmDex->pDexFile,
1057 dvmGetMethodCode(method),
1058 method->clazz->descriptor,
1059 method->prototype.protoIdx,
1060 method->accessFlags,
1061 addrToLineCb, NULL, &addrToLine);
1062
1063 LOGD("TRACEPROFILE 0x%08x % 10d [%#x(+%d), %d] %s%s;%s",
1064 (int)traceBase,
1065 *pExecutionCount,
1066 desc->trace[0].frag.startOffset,
1067 desc->trace[0].frag.numInsts,
1068 addrToLine.lineNum,
1069 method->clazz->descriptor, method->name, methodDesc);
1070 free(methodDesc);
1071
1072 return *pExecutionCount;
1073}
1074
1075/* Handy function to retrieve the profile count */
1076static inline int getProfileCount(const JitEntry *entry)
1077{
1078 if (entry->dPC == 0 || entry->codeAddress == 0)
1079 return 0;
1080 u4 *pExecutionCount = (u4 *) getTraceBase(entry);
1081
1082 return *pExecutionCount;
1083}
1084
1085
1086/* qsort callback function */
1087static int sortTraceProfileCount(const void *entry1, const void *entry2)
1088{
1089 const JitEntry *jitEntry1 = entry1;
1090 const JitEntry *jitEntry2 = entry2;
1091
1092 int count1 = getProfileCount(jitEntry1);
1093 int count2 = getProfileCount(jitEntry2);
1094 return (count1 == count2) ? 0 : ((count1 > count2) ? -1 : 1);
1095}
1096
1097/* Sort the trace profile counts and dump them */
1098void dvmCompilerSortAndPrintTraceProfiles()
1099{
1100 JitEntry *sortedEntries;
1101 int numTraces = 0;
1102 unsigned long counts = 0;
1103 unsigned int i;
1104
1105 /* Make sure that the table is not changing */
1106 dvmLockMutex(&gDvmJit.tableLock);
1107
1108 /* Sort the entries by descending order */
1109 sortedEntries = malloc(sizeof(JitEntry) * gDvmJit.jitTableSize);
1110 if (sortedEntries == NULL)
1111 goto done;
1112 memcpy(sortedEntries, gDvmJit.pJitEntryTable,
1113 sizeof(JitEntry) * gDvmJit.jitTableSize);
1114 qsort(sortedEntries, gDvmJit.jitTableSize, sizeof(JitEntry),
1115 sortTraceProfileCount);
1116
1117 /* Dump the sorted entries */
1118 for (i=0; i < gDvmJit.jitTableSize; i++) {
1119 if (sortedEntries[i].dPC != 0) {
1120 counts += dumpTraceProfile(&sortedEntries[i]);
1121 numTraces++;
1122 }
1123 }
1124 if (numTraces == 0)
1125 numTraces = 1;
1126 LOGD("JIT: Average execution count -> %d",(int)(counts / numTraces));
1127
1128 free(sortedEntries);
1129done:
1130 dvmUnlockMutex(&gDvmJit.tableLock);
1131 return;
1132}