blob: f2f6e8c530fbc93ebbdc27ff3a88c9d117f7e682 [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Dalvik.h"
18#include "libdex/OpCode.h"
19#include "dexdump/OpCodeNames.h"
20
21#include "../../CompilerInternals.h"
22#include "Armv5teLIR.h"
23#include <unistd.h> /* for cacheflush */
24
25/*
26 * opcode: Armv5teOpCode enum
27 * skeleton: pre-designated bit-pattern for this opcode
28 * ds: dest start bit position
29 * de: dest end bit position
30 * s1s: src1 start bit position
31 * s1e: src1 end bit position
32 * s2s: src2 start bit position
33 * s2e: src2 end bit position
34 * operands: number of operands (for sanity check purposes)
35 * name: mnemonic name
36 * fmt: for pretty-prining
37 */
38#define ENCODING_MAP(opcode, skeleton, ds, de, s1s, s1e, s2s, s2e, operands, \
Bill Buzbee716f1202009-07-23 13:22:09 -070039 name, fmt, size) \
Ben Chengba4fc8b2009-06-01 13:00:29 -070040 {skeleton, {{ds, de}, {s1s, s1e}, {s2s, s2e}}, opcode, operands, name, \
Bill Buzbee716f1202009-07-23 13:22:09 -070041 fmt, size}
Ben Chengba4fc8b2009-06-01 13:00:29 -070042
43/* Instruction dump string format keys: !pf, where "!" is the start
44 * of the key, "p" is which numeric operand to use and "f" is the
45 * print format.
46 *
47 * [p]ositions:
48 * 0 -> operands[0] (dest)
49 * 1 -> operands[1] (src1)
50 * 2 -> operands[2] (src2)
51 *
52 * [f]ormats:
53 * h -> 4-digit hex
54 * d -> decimal
55 * D -> decimal+8 (used to convert 3-bit regnum field to high reg)
56 * E -> decimal*4
57 * F -> decimal*2
58 * c -> branch condition (beq, bne, etc.)
59 * t -> pc-relative target
60 * u -> 1st half of bl[x] target
61 * v -> 2nd half ob bl[x] target
62 * R -> register list
63 *
64 * [!] escape. To insert "!", use "!!"
65 */
66/* NOTE: must be kept in sync with enum Armv5teOpcode from Armv5teLIR.h */
67Armv5teEncodingMap EncodingMap[ARMV5TE_LAST] = {
68 ENCODING_MAP(ARMV5TE_16BIT_DATA, 0x0000, 15, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -070069 IS_UNARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -070070 "data", "0x!0h(!0d)", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -070071 ENCODING_MAP(ARMV5TE_ADC, 0x4140, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -070072 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -070073 "adc", "r!0d, r!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -070074 ENCODING_MAP(ARMV5TE_ADD_RRI3, 0x1c00, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -070075 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -070076 "add", "r!0d, r!1d, #!2d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -070077 ENCODING_MAP(ARMV5TE_ADD_RI8, 0x3000, 10, 8, 7, 0, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -070078 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -070079 "add", "r!0d, r!0d, #!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -070080 ENCODING_MAP(ARMV5TE_ADD_RRR, 0x1800, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -070081 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -070082 "add", "r!0d, r!1d, r!2d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -070083 ENCODING_MAP(ARMV5TE_ADD_RR_LH, 0x4440, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -070084 IS_BINARY_OP | CLOBBER_DEST,
85 "add",
Bill Buzbee716f1202009-07-23 13:22:09 -070086 "r!0d, r!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -070087 ENCODING_MAP(ARMV5TE_ADD_RR_HL, 0x4480, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -070088 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -070089 "add", "r!0d, r!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -070090 ENCODING_MAP(ARMV5TE_ADD_RR_HH, 0x44c0, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -070091 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -070092 "add", "r!0d, r!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -070093 ENCODING_MAP(ARMV5TE_ADD_PC_REL, 0xa000, 10, 8, 7, 0, -1, -1,
Ben Cheng38329f52009-07-07 14:19:20 -070094 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -070095 "add", "r!0d, pc, #!1E", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -070096 ENCODING_MAP(ARMV5TE_ADD_SP_REL, 0xa800, 10, 8, 7, 0, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -070097 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -070098 "add", "r!0d, sp, #!1E", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -070099 ENCODING_MAP(ARMV5TE_ADD_SPI7, 0xb000, 6, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700100 IS_UNARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700101 "add", "sp, #!0d*4", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700102 ENCODING_MAP(ARMV5TE_AND_RR, 0x4000, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700103 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700104 "and", "r!0d, r!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700105 ENCODING_MAP(ARMV5TE_ASR, 0x1000, 2, 0, 5, 3, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700106 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700107 "asr", "r!0d, r!1d, #!2d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700108 ENCODING_MAP(ARMV5TE_ASRV, 0x4100, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700109 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700110 "asr", "r!0d, r!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700111 ENCODING_MAP(ARMV5TE_B_COND, 0xd000, 7, 0, 11, 8, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700112 IS_BINARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700113 "!1c", "!0t", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700114 ENCODING_MAP(ARMV5TE_B_UNCOND, 0xe000, 10, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700115 NO_OPERAND | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700116 "b", "!0t", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700117 ENCODING_MAP(ARMV5TE_BIC, 0x4380, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700118 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700119 "bic", "r!0d, r!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700120 ENCODING_MAP(ARMV5TE_BKPT, 0xbe00, 7, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700121 IS_UNARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700122 "bkpt", "!0d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700123 ENCODING_MAP(ARMV5TE_BLX_1, 0xf000, 10, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700124 IS_BINARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700125 "blx_1", "!0u", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700126 ENCODING_MAP(ARMV5TE_BLX_2, 0xe800, 10, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700127 IS_BINARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700128 "blx_2", "!0v", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700129 ENCODING_MAP(ARMV5TE_BL_1, 0xf000, 10, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700130 IS_UNARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700131 "bl_1", "!0u", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700132 ENCODING_MAP(ARMV5TE_BL_2, 0xf800, 10, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700133 IS_UNARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700134 "bl_2", "!0v", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700135 ENCODING_MAP(ARMV5TE_BLX_R, 0x4780, 6, 3, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700136 IS_UNARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700137 "blx", "r!0d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700138 ENCODING_MAP(ARMV5TE_BX, 0x4700, 6, 3, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700139 IS_UNARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700140 "bx", "r!0d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700141 ENCODING_MAP(ARMV5TE_CMN, 0x42c0, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700142 IS_BINARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700143 "cmn", "r!0d, r!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700144 ENCODING_MAP(ARMV5TE_CMP_RI8, 0x2800, 10, 8, 7, 0, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700145 IS_BINARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700146 "cmp", "r!0d, #!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700147 ENCODING_MAP(ARMV5TE_CMP_RR, 0x4280, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700148 IS_BINARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700149 "cmp", "r!0d, r!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700150 ENCODING_MAP(ARMV5TE_CMP_LH, 0x4540, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700151 IS_BINARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700152 "cmp", "r!0d, r!1D", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700153 ENCODING_MAP(ARMV5TE_CMP_HL, 0x4580, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700154 IS_BINARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700155 "cmp", "r!0D, r!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700156 ENCODING_MAP(ARMV5TE_CMP_HH, 0x45c0, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700157 IS_BINARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700158 "cmp", "r!0D, r!1D", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700159 ENCODING_MAP(ARMV5TE_EOR, 0x4040, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700160 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700161 "eor", "r!0d, r!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700162 ENCODING_MAP(ARMV5TE_LDMIA, 0xc800, 10, 8, 7, 0, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700163 IS_BINARY_OP | CLOBBER_DEST | CLOBBER_SRC1,
Bill Buzbee716f1202009-07-23 13:22:09 -0700164 "ldmia", "r!0d!!, <!1R>", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700165 ENCODING_MAP(ARMV5TE_LDR_RRI5, 0x6800, 2, 0, 5, 3, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700166 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700167 "ldr", "r!0d, [r!1d, #!2E]", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700168 ENCODING_MAP(ARMV5TE_LDR_RRR, 0x5800, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700169 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700170 "ldr", "r!0d, [r!1d, r!2d]", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700171 ENCODING_MAP(ARMV5TE_LDR_PC_REL, 0x4800, 10, 8, 7, 0, -1, -1,
Ben Cheng38329f52009-07-07 14:19:20 -0700172 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700173 "ldr", "r!0d, [pc, #!1E]", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700174 ENCODING_MAP(ARMV5TE_LDR_SP_REL, 0x9800, 10, 8, 7, 0, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700175 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700176 "ldr", "r!0d, [sp, #!1E]", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700177 ENCODING_MAP(ARMV5TE_LDRB_RRI5, 0x7800, 2, 0, 5, 3, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700178 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700179 "ldrb", "r!0d, [r!1d, #2d]", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700180 ENCODING_MAP(ARMV5TE_LDRB_RRR, 0x5c00, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700181 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700182 "ldrb", "r!0d, [r!1d, r!2d]", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700183 ENCODING_MAP(ARMV5TE_LDRH_RRI5, 0x8800, 2, 0, 5, 3, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700184 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700185 "ldrh", "r!0d, [r!1d, #!2F]", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700186 ENCODING_MAP(ARMV5TE_LDRH_RRR, 0x5a00, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700187 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700188 "ldrh", "r!0d, [r!1d, r!2d]", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700189 ENCODING_MAP(ARMV5TE_LDRSB_RRR, 0x5600, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700190 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700191 "ldrsb", "r!0d, [r!1d, r!2d]", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700192 ENCODING_MAP(ARMV5TE_LDRSH_RRR, 0x5e00, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700193 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700194 "ldrsh", "r!0d, [r!1d, r!2d]", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700195 ENCODING_MAP(ARMV5TE_LSL, 0x0000, 2, 0, 5, 3, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700196 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700197 "lsl", "r!0d, r!1d, #!2d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700198 ENCODING_MAP(ARMV5TE_LSLV, 0x4080, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700199 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700200 "lsl", "r!0d, r!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700201 ENCODING_MAP(ARMV5TE_LSR, 0x0800, 2, 0, 5, 3, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700202 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700203 "lsr", "r!0d, r!1d, #!2d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700204 ENCODING_MAP(ARMV5TE_LSRV, 0x40c0, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700205 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700206 "lsr", "r!0d, r!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700207 ENCODING_MAP(ARMV5TE_MOV_IMM, 0x2000, 10, 8, 7, 0, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700208 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700209 "mov", "r!0d, #!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700210 ENCODING_MAP(ARMV5TE_MOV_RR, 0x1c00, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700211 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700212 "mov", "r!0d, r!1d", 1),
Ben Cheng38329f52009-07-07 14:19:20 -0700213 ENCODING_MAP(ARMV5TE_MOV_RR_H2H, 0x46c0, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700214 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700215 "mov", "r!0D, r!1D", 1),
Ben Cheng38329f52009-07-07 14:19:20 -0700216 ENCODING_MAP(ARMV5TE_MOV_RR_H2L, 0x4640, 2, 0, 5, 3, -1, -1,
217 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700218 "mov", "r!0d, r!1D", 1),
Ben Cheng38329f52009-07-07 14:19:20 -0700219 ENCODING_MAP(ARMV5TE_MOV_RR_L2H, 0x4680, 2, 0, 5, 3, -1, -1,
220 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700221 "mov", "r!0D, r!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700222 ENCODING_MAP(ARMV5TE_MUL, 0x4340, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700223 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700224 "mul", "r!0d, r!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700225 ENCODING_MAP(ARMV5TE_MVN, 0x43c0, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700226 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700227 "mvn", "r!0d, r!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700228 ENCODING_MAP(ARMV5TE_NEG, 0x4240, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700229 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700230 "neg", "r!0d, r!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700231 ENCODING_MAP(ARMV5TE_ORR, 0x4300, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700232 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700233 "orr", "r!0d, r!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700234 ENCODING_MAP(ARMV5TE_POP, 0xbc00, 8, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700235 IS_UNARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700236 "pop", "<!0R>", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700237 ENCODING_MAP(ARMV5TE_PUSH, 0xb400, 8, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700238 IS_UNARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700239 "push", "<!0R>", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700240 ENCODING_MAP(ARMV5TE_ROR, 0x41c0, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700241 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700242 "ror", "r!0d, r!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700243 ENCODING_MAP(ARMV5TE_SBC, 0x4180, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700244 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700245 "sbc", "r!0d, r!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700246 ENCODING_MAP(ARMV5TE_STMIA, 0xc000, 10, 8, 7, 0, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700247 IS_BINARY_OP | CLOBBER_SRC1,
Bill Buzbee716f1202009-07-23 13:22:09 -0700248 "stmia", "r!0d!!, <!1R>", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700249 ENCODING_MAP(ARMV5TE_STR_RRI5, 0x6000, 2, 0, 5, 3, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700250 IS_TERTIARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700251 "str", "r!0d, [r!1d, #!2E]", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700252 ENCODING_MAP(ARMV5TE_STR_RRR, 0x5000, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700253 IS_TERTIARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700254 "str", "r!0d, [r!1d, r!2d]", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700255 ENCODING_MAP(ARMV5TE_STR_SP_REL, 0x9000, 10, 8, 7, 0, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700256 IS_BINARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700257 "str", "r!0d, [sp, #!1E]", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700258 ENCODING_MAP(ARMV5TE_STRB_RRI5, 0x7000, 2, 0, 5, 3, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700259 IS_TERTIARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700260 "strb", "r!0d, [r!1d, #!2d]", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700261 ENCODING_MAP(ARMV5TE_STRB_RRR, 0x5400, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700262 IS_TERTIARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700263 "strb", "r!0d, [r!1d, r!2d]", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700264 ENCODING_MAP(ARMV5TE_STRH_RRI5, 0x8000, 2, 0, 5, 3, 10, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700265 IS_TERTIARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700266 "strh", "r!0d, [r!1d, #!2F]", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700267 ENCODING_MAP(ARMV5TE_STRH_RRR, 0x5200, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700268 IS_TERTIARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700269 "strh", "r!0d, [r!1d, r!2d]", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700270 ENCODING_MAP(ARMV5TE_SUB_RRI3, 0x1e00, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700271 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700272 "sub", "r!0d, r!1d, #!2d]", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700273 ENCODING_MAP(ARMV5TE_SUB_RI8, 0x3800, 10, 8, 7, 0, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700274 IS_BINARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700275 "sub", "r!0d, #!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700276 ENCODING_MAP(ARMV5TE_SUB_RRR, 0x1a00, 2, 0, 5, 3, 8, 6,
Ben Chenge9695e52009-06-16 16:11:47 -0700277 IS_TERTIARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700278 "sub", "r!0d, r!1d, r!2d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700279 ENCODING_MAP(ARMV5TE_SUB_SPI7, 0xb080, 6, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700280 IS_UNARY_OP | CLOBBER_DEST,
Bill Buzbee716f1202009-07-23 13:22:09 -0700281 "sub", "sp, #!0d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700282 ENCODING_MAP(ARMV5TE_SWI, 0xdf00, 7, 0, -1, -1, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700283 IS_UNARY_OP | IS_BRANCH,
Bill Buzbee716f1202009-07-23 13:22:09 -0700284 "swi", "!0d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700285 ENCODING_MAP(ARMV5TE_TST, 0x4200, 2, 0, 5, 3, -1, -1,
Ben Chenge9695e52009-06-16 16:11:47 -0700286 IS_UNARY_OP,
Bill Buzbee716f1202009-07-23 13:22:09 -0700287 "tst", "r!0d, r!1d", 1),
Ben Chengba4fc8b2009-06-01 13:00:29 -0700288};
289
290#define PADDING_MOV_R0_R0 0x1C00
291
292/* Write the numbers in the literal pool to the codegen stream */
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700293static void installDataContent(CompilationUnit *cUnit)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700294{
Ben Chenge80cd942009-07-17 15:54:23 -0700295 int *dataPtr = (int *) ((char *) cUnit->baseAddr + cUnit->dataOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700296 Armv5teLIR *dataLIR = (Armv5teLIR *) cUnit->wordList;
297 while (dataLIR) {
298 *dataPtr++ = dataLIR->operands[0];
299 dataLIR = NEXT_LIR(dataLIR);
300 }
301}
302
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700303/* Returns the size of a Jit trace description */
304static int jitTraceDescriptionSize(const JitTraceDescription *desc)
305{
306 int runCount;
307 for (runCount = 0; ; runCount++) {
308 if (desc->trace[runCount].frag.runEnd)
309 break;
310 }
311 return sizeof(JitCodeDesc) + ((runCount+1) * sizeof(JitTraceRun));
312}
313
Ben Chengba4fc8b2009-06-01 13:00:29 -0700314/* Return TRUE if error happens */
315static bool assembleInstructions(CompilationUnit *cUnit, intptr_t startAddr)
316{
317 short *bufferAddr = (short *) cUnit->codeBuffer;
318 Armv5teLIR *lir;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700319
320 for (lir = (Armv5teLIR *) cUnit->firstLIRInsn; lir; lir = NEXT_LIR(lir)) {
321 if (lir->opCode < 0) {
322 if ((lir->opCode == ARMV5TE_PSEUDO_ALIGN4) &&
Ben Cheng1efc9c52009-06-08 18:25:27 -0700323 /* 1 means padding is needed */
324 (lir->operands[0] == 1)) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700325 *bufferAddr++ = PADDING_MOV_R0_R0;
326 }
327 continue;
328 }
329
Ben Chenge9695e52009-06-16 16:11:47 -0700330 if (lir->isNop) {
331 continue;
332 }
333
Ben Chengba4fc8b2009-06-01 13:00:29 -0700334 if (lir->opCode == ARMV5TE_LDR_PC_REL ||
335 lir->opCode == ARMV5TE_ADD_PC_REL) {
336 Armv5teLIR *lirTarget = (Armv5teLIR *) lir->generic.target;
337 intptr_t pc = (lir->generic.offset + 4) & ~3;
Ben Cheng38329f52009-07-07 14:19:20 -0700338 /*
339 * Allow an offset (stored in operands[2] to be added to the
340 * PC-relative target. Useful to get to a fixed field inside a
341 * chaining cell.
342 */
343 intptr_t target = lirTarget->generic.offset + lir->operands[2];
Ben Chengba4fc8b2009-06-01 13:00:29 -0700344 int delta = target - pc;
345 if (delta & 0x3) {
346 LOGE("PC-rel distance is not multiples of 4: %d\n", delta);
347 dvmAbort();
348 }
Ben Cheng1efc9c52009-06-08 18:25:27 -0700349 if (delta > 1023) {
350 return true;
351 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700352 lir->operands[1] = delta >> 2;
353 } else if (lir->opCode == ARMV5TE_B_COND) {
354 Armv5teLIR *targetLIR = (Armv5teLIR *) lir->generic.target;
355 intptr_t pc = lir->generic.offset + 4;
356 intptr_t target = targetLIR->generic.offset;
357 int delta = target - pc;
358 if (delta > 254 || delta < -256) {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700359 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700360 }
361 lir->operands[0] = delta >> 1;
362 } else if (lir->opCode == ARMV5TE_B_UNCOND) {
363 Armv5teLIR *targetLIR = (Armv5teLIR *) lir->generic.target;
364 intptr_t pc = lir->generic.offset + 4;
365 intptr_t target = targetLIR->generic.offset;
366 int delta = target - pc;
367 if (delta > 2046 || delta < -2048) {
368 LOGE("Unconditional branch distance out of range: %d\n", delta);
369 dvmAbort();
370 }
371 lir->operands[0] = delta >> 1;
372 } else if (lir->opCode == ARMV5TE_BLX_1) {
373 assert(NEXT_LIR(lir)->opCode == ARMV5TE_BLX_2);
374 /* curPC is Thumb */
375 intptr_t curPC = (startAddr + lir->generic.offset + 4) & ~3;
376 intptr_t target = lir->operands[1];
377
378 /* Match bit[1] in target with base */
379 if (curPC & 0x2) {
380 target |= 0x2;
381 }
382 int delta = target - curPC;
383 assert((delta >= -(1<<22)) && (delta <= ((1<<22)-2)));
384
385 lir->operands[0] = (delta >> 12) & 0x7ff;
386 NEXT_LIR(lir)->operands[0] = (delta>> 1) & 0x7ff;
387 }
388
Ben Chengba4fc8b2009-06-01 13:00:29 -0700389 Armv5teEncodingMap *encoder = &EncodingMap[lir->opCode];
390 short bits = encoder->skeleton;
391 int i;
392 for (i = 0; i < 3; i++) {
393 short value;
394 if (encoder->fieldLoc[i].end != -1) {
395 value = (lir->operands[i] << encoder->fieldLoc[i].start) &
396 ((1 << (encoder->fieldLoc[i].end + 1)) - 1);
397 bits |= value;
398
399 }
400 }
401 *bufferAddr++ = bits;
402 }
Ben Cheng1efc9c52009-06-08 18:25:27 -0700403 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700404}
405
406/*
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700407 * Translation layout in the code cache. Note that the codeAddress pointer
408 * in JitTable will point directly to the code body (field codeAddress). The
409 * chain cell offset codeAddress - 2, and (if present) executionCount is at
410 * codeAddress - 6.
411 *
412 * +----------------------------+
413 * | Execution count | -> [Optional] 4 bytes
414 * +----------------------------+
415 * +--| Offset to chain cell counts| -> 2 bytes
416 * | +----------------------------+
417 * | | Code body | -> Start address for translation
418 * | | | variable in 2-byte chunks
419 * | . . (JitTable's codeAddress points here)
420 * | . .
421 * | | |
422 * | +----------------------------+
423 * | | Chaining Cells | -> 8 bytes each, must be 4 byte aligned
424 * | . .
425 * | . .
426 * | | |
427 * | +----------------------------+
428 * +->| Chaining cell counts | -> 4 bytes, chain cell counts by type
429 * +----------------------------+
430 * | Trace description | -> variable sized
431 * . .
432 * | |
433 * +----------------------------+
434 * | Literal pool | -> 4-byte aligned, variable size
435 * . .
436 * . .
437 * | |
438 * +----------------------------+
439 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700440 * Go over each instruction in the list and calculate the offset from the top
441 * before sending them off to the assembler. If out-of-range branch distance is
442 * seen rearrange the instructions a bit to correct it.
443 */
Bill Buzbee716f1202009-07-23 13:22:09 -0700444void dvmCompilerAssembleLIR(CompilationUnit *cUnit, JitTranslationInfo *info)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700445{
446 LIR *lir;
447 Armv5teLIR *armLIR;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700448 int offset = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700449 int i;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700450 ChainCellCounts chainCellCounts;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700451 int descSize = jitTraceDescriptionSize(cUnit->traceDesc);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700452
Bill Buzbee716f1202009-07-23 13:22:09 -0700453 info->codeAddress = NULL;
454 info->instructionSet = cUnit->instructionSet;
455
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700456 /* Beginning offset needs to allow space for chain cell offset */
Ben Cheng1efc9c52009-06-08 18:25:27 -0700457 for (armLIR = (Armv5teLIR *) cUnit->firstLIRInsn;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700458 armLIR;
459 armLIR = NEXT_LIR(armLIR)) {
460 armLIR->generic.offset = offset;
Ben Chenge9695e52009-06-16 16:11:47 -0700461 if (armLIR->opCode >= 0 && !armLIR->isNop) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700462 offset += 2;
463 } else if (armLIR->opCode == ARMV5TE_PSEUDO_ALIGN4) {
464 if (offset & 0x2) {
465 offset += 2;
466 armLIR->operands[0] = 1;
467 } else {
468 armLIR->operands[0] = 0;
469 }
470 }
471 /* Pseudo opcodes don't consume space */
472 }
473
474 /* Const values have to be word aligned */
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700475 offset = (offset + 3) & ~3;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700476
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700477 /* Add space for chain cell counts & trace description */
Ben Cheng1efc9c52009-06-08 18:25:27 -0700478 u4 chainCellOffset = offset;
Ben Cheng38329f52009-07-07 14:19:20 -0700479 Armv5teLIR *chainCellOffsetLIR = (Armv5teLIR *) cUnit->chainCellOffsetLIR;
Bill Buzbee6e963e12009-06-17 16:56:19 -0700480 assert(chainCellOffsetLIR);
Ben Cheng1efc9c52009-06-08 18:25:27 -0700481 assert(chainCellOffset < 0x10000);
482 assert(chainCellOffsetLIR->opCode == ARMV5TE_16BIT_DATA &&
483 chainCellOffsetLIR->operands[0] == CHAIN_CELL_OFFSET_TAG);
484
Ben Chenge80cd942009-07-17 15:54:23 -0700485 /*
486 * Replace the CHAIN_CELL_OFFSET_TAG with the real value. If trace
487 * profiling is enabled, subtract 4 (occupied by the counter word) from
488 * the absolute offset as the value stored in chainCellOffsetLIR is the
489 * delta from &chainCellOffsetLIR to &ChainCellCounts.
490 */
491 chainCellOffsetLIR->operands[0] =
492 gDvmJit.profile ? (chainCellOffset - 4) : chainCellOffset;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700493
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700494 offset += sizeof(chainCellCounts) + descSize;
495
496 assert((offset & 0x3) == 0); /* Should still be word aligned */
497
498 /* Set up offsets for literals */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700499 cUnit->dataOffset = offset;
500
501 for (lir = cUnit->wordList; lir; lir = lir->next) {
502 lir->offset = offset;
503 offset += 4;
504 }
505
506 cUnit->totalSize = offset;
507
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700508 if (gDvmJit.codeCacheByteUsed + cUnit->totalSize > CODE_CACHE_SIZE) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700509 gDvmJit.codeCacheFull = true;
510 cUnit->baseAddr = NULL;
511 return;
512 }
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700513
514 /* Allocate enough space for the code block */
515 cUnit->codeBuffer = dvmCompilerNew(chainCellOffset, true);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700516 if (cUnit->codeBuffer == NULL) {
517 LOGE("Code buffer allocation failure\n");
518 cUnit->baseAddr = NULL;
519 return;
520 }
521
Ben Cheng1efc9c52009-06-08 18:25:27 -0700522 bool assemblerFailure = assembleInstructions(
Ben Chengba4fc8b2009-06-01 13:00:29 -0700523 cUnit, (intptr_t) gDvmJit.codeCache + gDvmJit.codeCacheByteUsed);
524
Ben Cheng1efc9c52009-06-08 18:25:27 -0700525 /*
526 * Currently the only reason that can cause the assembler to fail is due to
527 * trace length - cut it in half and retry.
528 */
529 if (assemblerFailure) {
530 cUnit->halveInstCount = true;
531 return;
532 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700533
Bill Buzbee6e963e12009-06-17 16:56:19 -0700534
Ben Chengba4fc8b2009-06-01 13:00:29 -0700535 cUnit->baseAddr = (char *) gDvmJit.codeCache + gDvmJit.codeCacheByteUsed;
536 gDvmJit.codeCacheByteUsed += offset;
537
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700538 /* Install the code block */
Ben Cheng1efc9c52009-06-08 18:25:27 -0700539 memcpy((char*)cUnit->baseAddr, cUnit->codeBuffer, chainCellOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700540 gDvmJit.numCompilations++;
541
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700542 /* Install the chaining cell counts */
543 for (i=0; i< CHAINING_CELL_LAST; i++) {
544 chainCellCounts.u.count[i] = cUnit->numChainingCells[i];
545 }
546 memcpy((char*)cUnit->baseAddr + chainCellOffset, &chainCellCounts,
547 sizeof(chainCellCounts));
548
549 /* Install the trace description */
550 memcpy((char*)cUnit->baseAddr + chainCellOffset + sizeof(chainCellCounts),
551 cUnit->traceDesc, descSize);
552
553 /* Write the literals directly into the code cache */
554 installDataContent(cUnit);
555
Ben Chengba4fc8b2009-06-01 13:00:29 -0700556 /* Flush dcache and invalidate the icache to maintain coherence */
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700557 cacheflush((long)cUnit->baseAddr,
Ben Chenge80cd942009-07-17 15:54:23 -0700558 (long)((char *) cUnit->baseAddr + offset), 0);
Bill Buzbee716f1202009-07-23 13:22:09 -0700559
560 /* Record code entry point and instruction set */
561 info->codeAddress = (char*)cUnit->baseAddr + cUnit->headerSize;
562 info->instructionSet = cUnit->instructionSet;
563 /* If applicable, mark low bit to denote thumb */
564 if (info->instructionSet != DALVIK_JIT_ARM)
565 info->codeAddress = (char*)info->codeAddress + 1;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700566}
567
Ben Cheng38329f52009-07-07 14:19:20 -0700568static u4 assembleBXPair(int branchOffset)
569{
570 u4 thumb1, thumb2;
571
572 if ((branchOffset < -2048) | (branchOffset > 2046)) {
573 thumb1 = (0xf000 | ((branchOffset>>12) & 0x7ff));
574 thumb2 = (0xf800 | ((branchOffset>> 1) & 0x7ff));
575 } else {
576 thumb1 = (0xe000 | ((branchOffset>> 1) & 0x7ff));
577 thumb2 = 0x4300; /* nop -> or r0, r0 */
578 }
579
580 return thumb2<<16 | thumb1;
581}
582
Ben Chengba4fc8b2009-06-01 13:00:29 -0700583/*
584 * Perform translation chain operation.
585 * For ARM, we'll use a pair of thumb instructions to generate
586 * an unconditional chaining branch of up to 4MB in distance.
587 * Use a BL, though we don't really need the link. The format is
588 * 111HHooooooooooo
589 * Where HH is 10 for the 1st inst, and 11 for the second and
590 * the "o" field is each instruction's 11-bit contribution to the
591 * 22-bit branch offset.
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700592 * If the target is nearby, use a single-instruction bl.
593 * If one or more threads is suspended, don't chain.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700594 */
595void* dvmJitChain(void* tgtAddr, u4* branchAddr)
596{
597 int baseAddr = (u4) branchAddr + 4;
598 int branchOffset = (int) tgtAddr - baseAddr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700599 u4 newInst;
600
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700601 if (gDvm.sumThreadSuspendCount == 0) {
602 assert((branchOffset >= -(1<<22)) && (branchOffset <= ((1<<22)-2)));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700603
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700604 gDvmJit.translationChains++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700605
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700606 COMPILER_TRACE_CHAINING(
607 LOGD("Jit Runtime: chaining 0x%x to 0x%x\n",
608 (int) branchAddr, (int) tgtAddr & -2));
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700609
Ben Cheng38329f52009-07-07 14:19:20 -0700610 newInst = assembleBXPair(branchOffset);
611
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700612 *branchAddr = newInst;
613 cacheflush((long)branchAddr, (long)branchAddr + 4, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700614 }
615
Ben Chengba4fc8b2009-06-01 13:00:29 -0700616 return tgtAddr;
617}
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700618
619/*
Ben Cheng38329f52009-07-07 14:19:20 -0700620 * This method is called from the invoke templates for virtual and interface
621 * methods to speculatively setup a chain to the callee. The templates are
622 * written in assembly and have setup method, cell, and clazz at r0, r2, and
623 * r3 respectively, so there is a unused argument in the list. Upon return one
624 * of the following three results may happen:
625 * 1) Chain is not setup because the callee is native. Reset the rechain
626 * count to a big number so that it will take a long time before the next
627 * rechain attempt to happen.
628 * 2) Chain is not setup because the callee has not been created yet. Reset
629 * the rechain count to a small number and retry in the near future.
630 * 3) Ask all other threads to stop before patching this chaining cell.
631 * This is required because another thread may have passed the class check
632 * but hasn't reached the chaining cell yet to follow the chain. If we
633 * patch the content before halting the other thread, there could be a
634 * small window for race conditions to happen that it may follow the new
635 * but wrong chain to invoke a different method.
636 */
637const Method *dvmJitToPatchPredictedChain(const Method *method,
638 void *unused,
639 PredictedChainingCell *cell,
640 const ClassObject *clazz)
641{
642 /* Don't come back here for a long time if the method is native */
643 if (dvmIsNativeMethod(method)) {
644 cell->counter = PREDICTED_CHAIN_COUNTER_AVOID;
645 cacheflush((long) cell, (long) (cell+1), 0);
646 COMPILER_TRACE_CHAINING(
647 LOGD("Jit Runtime: predicted chain %p to native method %s ignored",
648 cell, method->name));
649 goto done;
650 }
651 int tgtAddr = (int) dvmJitGetCodeAddr(method->insns);
652
653 /*
654 * Compilation not made yet for the callee. Reset the counter to a small
655 * value and come back to check soon.
656 */
657 if (tgtAddr == 0) {
658 /*
659 * Wait for a few invocations (currently set to be 16) before trying
660 * to setup the chain again.
661 */
662 cell->counter = PREDICTED_CHAIN_COUNTER_DELAY;
663 cacheflush((long) cell, (long) (cell+1), 0);
664 COMPILER_TRACE_CHAINING(
665 LOGD("Jit Runtime: predicted chain %p to method %s delayed",
666 cell, method->name));
667 goto done;
668 }
669
670 /* Stop the world */
671 dvmSuspendAllThreads(SUSPEND_FOR_JIT);
672
673 int baseAddr = (int) cell + 4; // PC is cur_addr + 4
674 int branchOffset = tgtAddr - baseAddr;
675
676 COMPILER_TRACE_CHAINING(
677 LOGD("Jit Runtime: predicted chain %p from %s to %s (%s) patched",
678 cell, cell->clazz ? cell->clazz->descriptor : "NULL",
679 clazz->descriptor,
680 method->name));
681
682 cell->branch = assembleBXPair(branchOffset);
683 cell->clazz = clazz;
684 cell->method = method;
685 cell->counter = PREDICTED_CHAIN_COUNTER_RECHAIN;
686
687 cacheflush((long) cell, (long) (cell+1), 0);
688
689 /* All done - resume all other threads */
690 dvmResumeAllThreads(SUSPEND_FOR_JIT);
691
692done:
693 return method;
694}
695
696/*
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700697 * Unchain a trace given the starting address of the translation
698 * in the code cache. Refer to the diagram in dvmCompilerAssembleLIR.
699 * Returns the address following the last cell unchained. Note that
700 * the incoming codeAddr is a thumb code address, and therefore has
701 * the low bit set.
702 */
703u4* dvmJitUnchain(void* codeAddr)
704{
705 u2* pChainCellOffset = (u2*)((char*)codeAddr - 3);
706 u2 chainCellOffset = *pChainCellOffset;
707 ChainCellCounts *pChainCellCounts =
Ben Chenge80cd942009-07-17 15:54:23 -0700708 (ChainCellCounts*)((char*)codeAddr + chainCellOffset - 3);
Ben Cheng38329f52009-07-07 14:19:20 -0700709 int cellSize;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700710 u4* pChainCells;
711 u4* pStart;
712 u4 thumb1;
713 u4 thumb2;
714 u4 newInst;
715 int i,j;
Ben Cheng38329f52009-07-07 14:19:20 -0700716 PredictedChainingCell *predChainCell;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700717
718 /* Get total count of chain cells */
Ben Cheng38329f52009-07-07 14:19:20 -0700719 for (i = 0, cellSize = 0; i < CHAINING_CELL_LAST; i++) {
720 if (i != CHAINING_CELL_INVOKE_PREDICTED) {
721 cellSize += pChainCellCounts->u.count[i] * 2;
722 } else {
723 cellSize += pChainCellCounts->u.count[i] * 4;
724 }
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700725 }
726
727 /* Locate the beginning of the chain cell region */
Ben Cheng38329f52009-07-07 14:19:20 -0700728 pStart = pChainCells = ((u4 *) pChainCellCounts) - cellSize;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700729
730 /* The cells are sorted in order - walk through them and reset */
731 for (i = 0; i < CHAINING_CELL_LAST; i++) {
Ben Cheng38329f52009-07-07 14:19:20 -0700732 int elemSize = 2; /* Most chaining cell has two words */
733 if (i == CHAINING_CELL_INVOKE_PREDICTED) {
734 elemSize = 4;
735 }
736
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700737 for (j = 0; j < pChainCellCounts->u.count[i]; j++) {
738 int targetOffset;
739 switch(i) {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700740 case CHAINING_CELL_NORMAL:
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700741 targetOffset = offsetof(InterpState,
742 jitToInterpEntries.dvmJitToInterpNormal);
743 break;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700744 case CHAINING_CELL_HOT:
Ben Cheng38329f52009-07-07 14:19:20 -0700745 case CHAINING_CELL_INVOKE_SINGLETON:
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700746 targetOffset = offsetof(InterpState,
747 jitToInterpEntries.dvmJitToTraceSelect);
748 break;
Ben Cheng38329f52009-07-07 14:19:20 -0700749 case CHAINING_CELL_INVOKE_PREDICTED:
750 targetOffset = 0;
751 predChainCell = (PredictedChainingCell *) pChainCells;
752 /* Reset the cell to the init state */
753 predChainCell->branch = PREDICTED_CHAIN_BX_PAIR_INIT;
754 predChainCell->clazz = PREDICTED_CHAIN_CLAZZ_INIT;
755 predChainCell->method = PREDICTED_CHAIN_METHOD_INIT;
756 predChainCell->counter = PREDICTED_CHAIN_COUNTER_INIT;
757 break;
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700758 default:
759 dvmAbort();
760 }
Ben Cheng38329f52009-07-07 14:19:20 -0700761 COMPILER_TRACE_CHAINING(
762 LOGD("Jit Runtime: unchaining 0x%x", (int)pChainCells));
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700763 /*
Ben Cheng38329f52009-07-07 14:19:20 -0700764 * Thumb code sequence for a chaining cell is:
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700765 * ldr r0, rGLUE, #<word offset>
766 * blx r0
767 */
Ben Cheng38329f52009-07-07 14:19:20 -0700768 if (i != CHAINING_CELL_INVOKE_PREDICTED) {
769 targetOffset = targetOffset >> 2; /* convert to word offset */
770 thumb1 = 0x6800 | (targetOffset << 6) |
771 (rGLUE << 3) | (r0 << 0);
772 thumb2 = 0x4780 | (r0 << 3);
773 newInst = thumb2<<16 | thumb1;
774 *pChainCells = newInst;
775 }
776 pChainCells += elemSize; /* Advance by a fixed number of words */
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700777 }
778 }
779 return pChainCells;
780}
781
782/* Unchain all translation in the cache. */
783void dvmJitUnchainAll()
784{
785 u4* lowAddress = NULL;
786 u4* highAddress = NULL;
787 unsigned int i;
788 if (gDvmJit.pJitEntryTable != NULL) {
789 COMPILER_TRACE_CHAINING(LOGD("Jit Runtime: unchaining all"));
790 dvmLockMutex(&gDvmJit.tableLock);
Bill Buzbee27176222009-06-09 09:20:16 -0700791 for (i = 0; i < gDvmJit.jitTableSize; i++) {
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700792 if (gDvmJit.pJitEntryTable[i].dPC &&
793 gDvmJit.pJitEntryTable[i].codeAddress) {
794 u4* lastAddress;
795 lastAddress =
796 dvmJitUnchain(gDvmJit.pJitEntryTable[i].codeAddress);
797 if (lowAddress == NULL ||
798 (u4*)gDvmJit.pJitEntryTable[i].codeAddress < lowAddress)
799 lowAddress = lastAddress;
800 if (lastAddress > highAddress)
801 highAddress = lastAddress;
802 }
803 }
804 cacheflush((long)lowAddress, (long)highAddress, 0);
805 dvmUnlockMutex(&gDvmJit.tableLock);
806 }
807}
Bill Buzbee716f1202009-07-23 13:22:09 -0700808
809typedef struct jitProfileAddrToLine {
810 u4 lineNum;
811 u4 bytecodeOffset;
812} jitProfileAddrToLine;
813
814
815/* Callback function to track the bytecode offset/line number relationiship */
816static int addrToLineCb (void *cnxt, u4 bytecodeOffset, u4 lineNum)
817{
818 jitProfileAddrToLine *addrToLine = (jitProfileAddrToLine *) cnxt;
819
820 /* Best match so far for this offset */
821 if (addrToLine->bytecodeOffset >= bytecodeOffset) {
822 addrToLine->lineNum = lineNum;
823 }
824 return 0;
825}
826
827char *getTraceBase(const JitEntry *p)
828{
829 return (char*)p->codeAddress -
830 (6 + (p->u.info.instructionSet == DALVIK_JIT_ARM ? 0 : 1));
831}
832
833/* Dumps profile info for a single trace */
834static int dumpTraceProfile(JitEntry *p)
835{
836 ChainCellCounts* pCellCounts;
837 char* traceBase;
838 u4* pExecutionCount;
839 u2* pCellOffset;
840 JitTraceDescription *desc;
841 const Method* method;
842
843 traceBase = getTraceBase(p);
844
845 if (p->codeAddress == NULL) {
846 LOGD("TRACEPROFILE 0x%08x 0 NULL 0 0", (int)traceBase);
847 return 0;
848 }
849
850 pExecutionCount = (u4*) (traceBase);
851 pCellOffset = (u2*) (traceBase + 4);
852 pCellCounts = (ChainCellCounts*) ((char *)pCellOffset + *pCellOffset);
853 desc = (JitTraceDescription*) ((char*)pCellCounts + sizeof(*pCellCounts));
854 method = desc->method;
855 char *methodDesc = dexProtoCopyMethodDescriptor(&method->prototype);
856 jitProfileAddrToLine addrToLine = {0, desc->trace[0].frag.startOffset};
857
858 /*
859 * We may end up decoding the debug information for the same method
860 * multiple times, but the tradeoff is we don't need to allocate extra
861 * space to store the addr/line mapping. Since this is a debugging feature
862 * and done infrequently so the slower but simpler mechanism should work
863 * just fine.
864 */
865 dexDecodeDebugInfo(method->clazz->pDvmDex->pDexFile,
866 dvmGetMethodCode(method),
867 method->clazz->descriptor,
868 method->prototype.protoIdx,
869 method->accessFlags,
870 addrToLineCb, NULL, &addrToLine);
871
872 LOGD("TRACEPROFILE 0x%08x % 10d [%#x(+%d), %d] %s%s;%s",
873 (int)traceBase,
874 *pExecutionCount,
875 desc->trace[0].frag.startOffset,
876 desc->trace[0].frag.numInsts,
877 addrToLine.lineNum,
878 method->clazz->descriptor, method->name, methodDesc);
879 free(methodDesc);
880
881 return *pExecutionCount;
882}
883
884/* Handy function to retrieve the profile count */
885static inline int getProfileCount(const JitEntry *entry)
886{
887 if (entry->dPC == 0 || entry->codeAddress == 0)
888 return 0;
889 u4 *pExecutionCount = (u4 *) getTraceBase(entry);
890
891 return *pExecutionCount;
892}
893
894
895/* qsort callback function */
896static int sortTraceProfileCount(const void *entry1, const void *entry2)
897{
898 const JitEntry *jitEntry1 = entry1;
899 const JitEntry *jitEntry2 = entry2;
900
901 int count1 = getProfileCount(jitEntry1);
902 int count2 = getProfileCount(jitEntry2);
903 return (count1 == count2) ? 0 : ((count1 > count2) ? -1 : 1);
904}
905
906/* Sort the trace profile counts and dump them */
907void dvmCompilerSortAndPrintTraceProfiles()
908{
909 JitEntry *sortedEntries;
910 int numTraces = 0;
911 unsigned long counts = 0;
912 unsigned int i;
913
914 /* Make sure that the table is not changing */
915 dvmLockMutex(&gDvmJit.tableLock);
916
917 /* Sort the entries by descending order */
918 sortedEntries = malloc(sizeof(JitEntry) * gDvmJit.jitTableSize);
919 if (sortedEntries == NULL)
920 goto done;
921 memcpy(sortedEntries, gDvmJit.pJitEntryTable,
922 sizeof(JitEntry) * gDvmJit.jitTableSize);
923 qsort(sortedEntries, gDvmJit.jitTableSize, sizeof(JitEntry),
924 sortTraceProfileCount);
925
926 /* Dump the sorted entries */
927 for (i=0; i < gDvmJit.jitTableSize; i++) {
928 if (sortedEntries[i].dPC != 0) {
929 counts += dumpTraceProfile(&sortedEntries[i]);
930 numTraces++;
931 }
932 }
933 if (numTraces == 0)
934 numTraces = 1;
935 LOGD("JIT: Average execution count -> %d",(int)(counts / numTraces));
936
937 free(sortedEntries);
938done:
939 dvmUnlockMutex(&gDvmJit.tableLock);
940 return;
941}