| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "Dalvik.h" |
| 18 | #include "libdex/OpCode.h" |
| 19 | #include "dexdump/OpCodeNames.h" |
| 20 | |
| 21 | #include "../../CompilerInternals.h" |
| 22 | #include "Armv5teLIR.h" |
| 23 | #include <unistd.h> /* for cacheflush */ |
| 24 | |
| 25 | /* |
| 26 | * opcode: Armv5teOpCode enum |
| 27 | * skeleton: pre-designated bit-pattern for this opcode |
| 28 | * ds: dest start bit position |
| 29 | * de: dest end bit position |
| 30 | * s1s: src1 start bit position |
| 31 | * s1e: src1 end bit position |
| 32 | * s2s: src2 start bit position |
| 33 | * s2e: src2 end bit position |
| 34 | * operands: number of operands (for sanity check purposes) |
| 35 | * name: mnemonic name |
| 36 | * fmt: for pretty-prining |
| 37 | */ |
| 38 | #define ENCODING_MAP(opcode, skeleton, ds, de, s1s, s1e, s2s, s2e, operands, \ |
| 39 | name, fmt) \ |
| 40 | {skeleton, {{ds, de}, {s1s, s1e}, {s2s, s2e}}, opcode, operands, name, \ |
| 41 | fmt} |
| 42 | |
| 43 | /* Instruction dump string format keys: !pf, where "!" is the start |
| 44 | * of the key, "p" is which numeric operand to use and "f" is the |
| 45 | * print format. |
| 46 | * |
| 47 | * [p]ositions: |
| 48 | * 0 -> operands[0] (dest) |
| 49 | * 1 -> operands[1] (src1) |
| 50 | * 2 -> operands[2] (src2) |
| 51 | * |
| 52 | * [f]ormats: |
| 53 | * h -> 4-digit hex |
| 54 | * d -> decimal |
| 55 | * D -> decimal+8 (used to convert 3-bit regnum field to high reg) |
| 56 | * E -> decimal*4 |
| 57 | * F -> decimal*2 |
| 58 | * c -> branch condition (beq, bne, etc.) |
| 59 | * t -> pc-relative target |
| 60 | * u -> 1st half of bl[x] target |
| 61 | * v -> 2nd half ob bl[x] target |
| 62 | * R -> register list |
| 63 | * |
| 64 | * [!] escape. To insert "!", use "!!" |
| 65 | */ |
| 66 | /* NOTE: must be kept in sync with enum Armv5teOpcode from Armv5teLIR.h */ |
| 67 | Armv5teEncodingMap EncodingMap[ARMV5TE_LAST] = { |
| 68 | ENCODING_MAP(ARMV5TE_16BIT_DATA, 0x0000, 15, 0, -1, -1, -1, -1, |
| 69 | 1, "data", "0x!0h(!0d)"), |
| 70 | ENCODING_MAP(ARMV5TE_ADC, 0x4140, 2, 0, 5, 3, -1, -1, |
| 71 | 2, "adc", "r!0d, r!1d"), |
| 72 | ENCODING_MAP(ARMV5TE_ADD_RRI3, 0x1c00, 2, 0, 5, 3, 8, 6, |
| 73 | 3, "add", "r!0d, r!1d, #!2d"), |
| 74 | ENCODING_MAP(ARMV5TE_ADD_RI8, 0x3000, 10, 8, 7, 0, -1, -1, |
| 75 | 2, "add", "r!0d, r!0d, #!1d"), |
| 76 | ENCODING_MAP(ARMV5TE_ADD_RRR, 0x1800, 2, 0, 5, 3, 8, 6, |
| 77 | 3, "add", "r!0d, r!1d, r!2d"), |
| 78 | ENCODING_MAP(ARMV5TE_ADD_RR_LH, 0x4440, 2, 0, 5, 3, -1, -1, |
| 79 | 2, "add", "r!0d, r!1d"), |
| 80 | ENCODING_MAP(ARMV5TE_ADD_RR_HL, 0x4480, 2, 0, 5, 3, -1, -1, |
| 81 | 2, "add", "r!0d, r!1d"), |
| 82 | ENCODING_MAP(ARMV5TE_ADD_RR_HH, 0x44c0, 2, 0, 5, 3, -1, -1, |
| 83 | 2, "add", "r!0d, r!1d"), |
| 84 | ENCODING_MAP(ARMV5TE_ADD_PC_REL, 0xa000, 10, 8, 7, 0, -1, -1, |
| 85 | 2, "add", "r!0d, pc, #!1E"), |
| 86 | ENCODING_MAP(ARMV5TE_ADD_SP_REL, 0xa800, 10, 8, 7, 0, -1, -1, |
| 87 | 2, "add", "r!0d, sp, #!1E"), |
| 88 | ENCODING_MAP(ARMV5TE_ADD_SPI7, 0xb000, 6, 0, -1, -1, -1, -1, |
| 89 | 1, "add", "sp, #!0d*4"), |
| 90 | ENCODING_MAP(ARMV5TE_AND_RR, 0x4000, 2, 0, 5, 3, -1, -1, |
| 91 | 2, "and", "r!0d, r!1d"), |
| 92 | ENCODING_MAP(ARMV5TE_ASR, 0x1000, 2, 0, 5, 3, 10, 6, |
| 93 | 3, "asr", "r!0d, r!1d, #!2d"), |
| 94 | ENCODING_MAP(ARMV5TE_ASRV, 0x4100, 2, 0, 5, 3, -1, -1, |
| 95 | 2, "asr", "r!0d, r!1d"), |
| 96 | ENCODING_MAP(ARMV5TE_B_COND, 0xd000, 7, 0, 11, 8, -1, -1, |
| 97 | 2, "!1c", "!0t"), |
| 98 | ENCODING_MAP(ARMV5TE_B_UNCOND, 0xe000, 10, 0, -1, -1, -1, -1, |
| 99 | 0, "b", "!0t"), |
| 100 | ENCODING_MAP(ARMV5TE_BIC, 0x4380, 2, 0, 5, 3, -1, -1, |
| 101 | 2, "bic", "r!0d, r!1d"), |
| 102 | ENCODING_MAP(ARMV5TE_BKPT, 0xbe00, 7, 0, -1, -1, -1, -1, |
| 103 | 1, "bkpt", "!0d"), |
| 104 | ENCODING_MAP(ARMV5TE_BLX_1, 0xf000, 10, 0, -1, -1, -1, -1, |
| 105 | 2, "blx_1", "!0u"), |
| 106 | ENCODING_MAP(ARMV5TE_BLX_2, 0xe800, 10, 0, -1, -1, -1, -1, |
| 107 | 2, "blx_2", "!0v"), |
| 108 | ENCODING_MAP(ARMV5TE_BL_1, 0xf000, 10, 0, -1, -1, -1, -1, |
| 109 | 1, "bl_1", "!0u"), |
| 110 | ENCODING_MAP(ARMV5TE_BL_2, 0xf800, 10, 0, -1, -1, -1, -1, |
| 111 | 1, "bl_2", "!0v"), |
| 112 | ENCODING_MAP(ARMV5TE_BLX_R, 0x4780, 6, 3, -1, -1, -1, -1, |
| 113 | 1, "blx", "r!0d"), |
| 114 | ENCODING_MAP(ARMV5TE_BX, 0x4700, 6, 3, -1, -1, -1, -1, |
| 115 | 1, "bx", "r!0d"), |
| 116 | ENCODING_MAP(ARMV5TE_CMN, 0x42c0, 2, 0, 5, 3, -1, -1, |
| 117 | 2, "cmn", "r!0d, r!1d"), |
| 118 | ENCODING_MAP(ARMV5TE_CMP_RI8, 0x2800, 10, 8, 7, 0, -1, -1, |
| 119 | 2, "cmp", "r!0d, #!1d"), |
| 120 | ENCODING_MAP(ARMV5TE_CMP_RR, 0x4280, 2, 0, 5, 3, -1, -1, |
| 121 | 2, "cmp", "r!0d, r!1d"), |
| 122 | ENCODING_MAP(ARMV5TE_CMP_LH, 0x4540, 2, 0, 5, 3, -1, -1, |
| 123 | 2, "cmp", "r!0d, r!1D"), |
| 124 | ENCODING_MAP(ARMV5TE_CMP_HL, 0x4580, 2, 0, 5, 3, -1, -1, |
| 125 | 2, "cmp", "r!0D, r!1d"), |
| 126 | ENCODING_MAP(ARMV5TE_CMP_HH, 0x45c0, 2, 0, 5, 3, -1, -1, |
| 127 | 2, "cmp", "r!0D, r!1D"), |
| 128 | ENCODING_MAP(ARMV5TE_EOR, 0x4040, 2, 0, 5, 3, -1, -1, |
| 129 | 2, "eor", "r!0d, r!1d"), |
| 130 | ENCODING_MAP(ARMV5TE_LDMIA, 0xc800, 10, 8, 7, 0, -1, -1, |
| 131 | 2, "ldmia", "r!0d!!, <!1R>"), |
| 132 | ENCODING_MAP(ARMV5TE_LDR_RRI5, 0x6800, 2, 0, 5, 3, 10, 6, |
| 133 | 3, "ldr", "r!0d, [r!1d, #!2E]"), |
| 134 | ENCODING_MAP(ARMV5TE_LDR_RRR, 0x5800, 2, 0, 5, 3, 8, 6, |
| 135 | 3, "ldr", "r!0d, [r!1d, r!2d]"), |
| 136 | ENCODING_MAP(ARMV5TE_LDR_PC_REL, 0x4800, 10, 8, 7, 0, -1, -1, |
| 137 | 2, "ldr", "r!0d, [pc, #!1E]"), |
| 138 | ENCODING_MAP(ARMV5TE_LDR_SP_REL, 0x9800, 10, 8, 7, 0, -1, -1, |
| 139 | 2, "ldr", "r!0d, [sp, #!1E]"), |
| 140 | ENCODING_MAP(ARMV5TE_LDRB_RRI5, 0x7800, 2, 0, 5, 3, 10, 6, |
| 141 | 3, "ldrb", "r!0d, [r!1d, #2d]"), |
| 142 | ENCODING_MAP(ARMV5TE_LDRB_RRR, 0x5c00, 2, 0, 5, 3, 8, 6, |
| 143 | 3, "ldrb", "r!0d, [r!1d, r!2d]"), |
| 144 | ENCODING_MAP(ARMV5TE_LDRH_RRI5, 0x8800, 2, 0, 5, 3, 10, 6, |
| 145 | 3, "ldrh", "r!0d, [r!1d, #!2F]"), |
| 146 | ENCODING_MAP(ARMV5TE_LDRH_RRR, 0x5a00, 2, 0, 5, 3, 8, 6, |
| 147 | 3, "ldrh", "r!0d, [r!1d, r!2d]"), |
| 148 | ENCODING_MAP(ARMV5TE_LDRSB_RRR, 0x5600, 2, 0, 5, 3, 8, 6, |
| 149 | 3, "ldrsb", "r!0d, [r!1d, r!2d]"), |
| 150 | ENCODING_MAP(ARMV5TE_LDRSH_RRR, 0x5e00, 2, 0, 5, 3, 8, 6, |
| 151 | 3, "ldrsh", "r!0d, [r!1d, r!2d]"), |
| 152 | ENCODING_MAP(ARMV5TE_LSL, 0x0000, 2, 0, 5, 3, 10, 6, |
| 153 | 3, "lsl", "r!0d, r!1d, #!2d"), |
| 154 | ENCODING_MAP(ARMV5TE_LSLV, 0x4080, 2, 0, 5, 3, -1, -1, |
| 155 | 2, "lsl", "r!0d, r!1d"), |
| 156 | ENCODING_MAP(ARMV5TE_LSR, 0x0800, 2, 0, 5, 3, 10, 6, |
| 157 | 3, "lsr", "r!0d, r!1d, #!2d"), |
| 158 | ENCODING_MAP(ARMV5TE_LSRV, 0x40c0, 2, 0, 5, 3, -1, -1, |
| 159 | 2, "lsr", "r!0d, r!1d"), |
| 160 | ENCODING_MAP(ARMV5TE_MOV_IMM, 0x2000, 10, 8, 7, 0, -1, -1, |
| 161 | 2, "mov", "r!0d, #!1d"), |
| 162 | ENCODING_MAP(ARMV5TE_MOV_RR, 0x1c00, 2, 0, 5, 3, -1, -1, |
| 163 | 2, "mov", "r!0d, r!1d"), |
| 164 | ENCODING_MAP(ARMV5TE_MOV_RR_LH, 0x4640, 2, 0, 5, 3, -1, -1, |
| 165 | 2, "mov", "r!0D, r!1d"), |
| 166 | ENCODING_MAP(ARMV5TE_MOV_RR_HL, 0x4680, 2, 0, 5, 3, -1, -1, |
| 167 | 2, "mov", "r!0d, r!1D"), |
| 168 | ENCODING_MAP(ARMV5TE_MOV_RR_HH, 0x46c0, 2, 0, 5, 3, -1, -1, |
| 169 | 2, "mov", "r!0D, r!1D"), |
| 170 | ENCODING_MAP(ARMV5TE_MUL, 0x4340, 2, 0, 5, 3, -1, -1, |
| 171 | 2, "mul", "r!0d, r!1d"), |
| 172 | ENCODING_MAP(ARMV5TE_MVN, 0x43c0, 2, 0, 5, 3, -1, -1, |
| 173 | 2, "mvn", "r!0d, r!1d"), |
| 174 | ENCODING_MAP(ARMV5TE_NEG, 0x4240, 2, 0, 5, 3, -1, -1, |
| 175 | 2, "neg", "r!0d, r!1d"), |
| 176 | ENCODING_MAP(ARMV5TE_ORR, 0x4300, 2, 0, 5, 3, -1, -1, |
| 177 | 2, "orr", "r!0d, r!1d"), |
| 178 | ENCODING_MAP(ARMV5TE_POP, 0xbc00, 8, 0, -1, -1, -1, -1, |
| 179 | 1, "pop", "<!0R>"), |
| 180 | ENCODING_MAP(ARMV5TE_PUSH, 0xb400, 8, 0, -1, -1, -1, -1, |
| 181 | 1, "push", "<!0R>"), |
| 182 | ENCODING_MAP(ARMV5TE_ROR, 0x41c0, 2, 0, 5, 3, -1, -1, |
| 183 | 2, "ror", "r!0d, r!1d"), |
| 184 | ENCODING_MAP(ARMV5TE_SBC, 0x4180, 2, 0, 5, 3, -1, -1, |
| 185 | 2, "sbc", "r!0d, r!1d"), |
| 186 | ENCODING_MAP(ARMV5TE_STMIA, 0xc000, 10, 8, 7, 0, -1, -1, |
| 187 | 2, "stmia", "r!0d!!, <!1R>"), |
| 188 | ENCODING_MAP(ARMV5TE_STR_RRI5, 0x6000, 2, 0, 5, 3, 10, 6, |
| 189 | 3, "str", "r!0d, [r!1d, #!2E]"), |
| 190 | ENCODING_MAP(ARMV5TE_STR_RRR, 0x5000, 2, 0, 5, 3, 8, 6, |
| 191 | 3, "str", "r!0d, [r!1d, r!2d]"), |
| 192 | ENCODING_MAP(ARMV5TE_STR_SP_REL, 0x9000, 10, 8, 7, 0, -1, -1, |
| 193 | 2, "str", "r!0d, [sp, #!1E]"), |
| 194 | ENCODING_MAP(ARMV5TE_STRB_RRI5, 0x7000, 2, 0, 5, 3, 10, 6, |
| 195 | 3, "strb", "r!0d, [r!1d, #!2d]"), |
| 196 | ENCODING_MAP(ARMV5TE_STRB_RRR, 0x5400, 2, 0, 5, 3, 8, 6, |
| 197 | 3, "strb", "r!0d, [r!1d, r!2d]"), |
| 198 | ENCODING_MAP(ARMV5TE_STRH_RRI5, 0x8000, 2, 0, 5, 3, 10, 6, |
| 199 | 3, "strh", "r!0d, [r!1d, #!2F]"), |
| 200 | ENCODING_MAP(ARMV5TE_STRH_RRR, 0x5200, 2, 0, 5, 3, 8, 6, |
| 201 | 3, "strh", "r!0d, [r!1d, r!2d]"), |
| 202 | ENCODING_MAP(ARMV5TE_SUB_RRI3, 0x1e00, 2, 0, 5, 3, 8, 6, |
| 203 | 3, "sub", "r!0d, r!1d, #!2d]"), |
| 204 | ENCODING_MAP(ARMV5TE_SUB_RI8, 0x3800, 10, 8, 7, 0, -1, -1, |
| 205 | 2, "sub", "r!0d, #!1d"), |
| 206 | ENCODING_MAP(ARMV5TE_SUB_RRR, 0x1a00, 2, 0, 5, 3, 8, 6, |
| 207 | 3, "sub", "r!0d, r!1d, r!2d"), |
| 208 | ENCODING_MAP(ARMV5TE_SUB_SPI7, 0xb080, 6, 0, -1, -1, -1, -1, |
| 209 | 1, "sub", "sp, #!0d"), |
| 210 | ENCODING_MAP(ARMV5TE_SWI, 0xdf00, 7, 0, -1, -1, -1, -1, |
| 211 | 1, "swi", "!0d"), |
| 212 | ENCODING_MAP(ARMV5TE_TST, 0x4200, 2, 0, 5, 3, -1, -1, |
| 213 | 1, "tst", "r!0d, r!1d"), |
| 214 | }; |
| 215 | |
| 216 | #define PADDING_MOV_R0_R0 0x1C00 |
| 217 | |
| 218 | /* Write the numbers in the literal pool to the codegen stream */ |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame^] | 219 | static void installDataContent(CompilationUnit *cUnit) |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 220 | { |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame^] | 221 | int *dataPtr = (int *) (cUnit->baseAddr + cUnit->dataOffset); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 222 | Armv5teLIR *dataLIR = (Armv5teLIR *) cUnit->wordList; |
| 223 | while (dataLIR) { |
| 224 | *dataPtr++ = dataLIR->operands[0]; |
| 225 | dataLIR = NEXT_LIR(dataLIR); |
| 226 | } |
| 227 | } |
| 228 | |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame^] | 229 | /* Returns the size of a Jit trace description */ |
| 230 | static int jitTraceDescriptionSize(const JitTraceDescription *desc) |
| 231 | { |
| 232 | int runCount; |
| 233 | for (runCount = 0; ; runCount++) { |
| 234 | if (desc->trace[runCount].frag.runEnd) |
| 235 | break; |
| 236 | } |
| 237 | return sizeof(JitCodeDesc) + ((runCount+1) * sizeof(JitTraceRun)); |
| 238 | } |
| 239 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 240 | /* Return TRUE if error happens */ |
| 241 | static bool assembleInstructions(CompilationUnit *cUnit, intptr_t startAddr) |
| 242 | { |
| 243 | short *bufferAddr = (short *) cUnit->codeBuffer; |
| 244 | Armv5teLIR *lir; |
| 245 | bool retry = false; |
| 246 | |
| 247 | for (lir = (Armv5teLIR *) cUnit->firstLIRInsn; lir; lir = NEXT_LIR(lir)) { |
| 248 | if (lir->opCode < 0) { |
| 249 | if ((lir->opCode == ARMV5TE_PSEUDO_ALIGN4) && |
| 250 | (lir->operands[0] == 1) && |
| 251 | !retry) { |
| 252 | *bufferAddr++ = PADDING_MOV_R0_R0; |
| 253 | } |
| 254 | continue; |
| 255 | } |
| 256 | |
| 257 | if (lir->opCode == ARMV5TE_LDR_PC_REL || |
| 258 | lir->opCode == ARMV5TE_ADD_PC_REL) { |
| 259 | Armv5teLIR *lirTarget = (Armv5teLIR *) lir->generic.target; |
| 260 | intptr_t pc = (lir->generic.offset + 4) & ~3; |
| 261 | intptr_t target = lirTarget->generic.offset; |
| 262 | int delta = target - pc; |
| 263 | if (delta & 0x3) { |
| 264 | LOGE("PC-rel distance is not multiples of 4: %d\n", delta); |
| 265 | dvmAbort(); |
| 266 | } |
| 267 | lir->operands[1] = delta >> 2; |
| 268 | } else if (lir->opCode == ARMV5TE_B_COND) { |
| 269 | Armv5teLIR *targetLIR = (Armv5teLIR *) lir->generic.target; |
| 270 | intptr_t pc = lir->generic.offset + 4; |
| 271 | intptr_t target = targetLIR->generic.offset; |
| 272 | int delta = target - pc; |
| 273 | if (delta > 254 || delta < -256) { |
| 274 | /* Pull in the PC reconstruction code inline */ |
| 275 | if (targetLIR->opCode == ARMV5TE_PSEUDO_PC_RECONSTRUCTION_CELL){ |
| 276 | /* |
| 277 | * The original code is: |
| 278 | * |
| 279 | * bxx targetLIR |
| 280 | * origNextLir |
| 281 | * : |
| 282 | * : |
| 283 | * targetLIR (a PC reconstruction cell) |
| 284 | * : |
| 285 | * lastLIR (should be a unconditional branch) |
| 286 | * |
| 287 | * The distance from bxx to targetLIR is too far, so we want |
| 288 | * to rearrange the code to be: |
| 289 | * |
| 290 | * bxx targetLIR |
| 291 | * branchoverLIR to origNextLir |
| 292 | * targetLIR (a PC reconstruction cell) |
| 293 | * : |
| 294 | * lastLIR (should be a unconditional branch) |
| 295 | * origNextLir |
| 296 | * |
| 297 | * Although doing so adds a unconditional branchover |
| 298 | * instruction, it can be predicted for free by ARM so |
| 299 | * the penalty should be minimal. |
| 300 | */ |
| 301 | Armv5teLIR *pcrLIR = targetLIR; |
| 302 | Armv5teLIR *lastLIR = pcrLIR; |
| 303 | Armv5teLIR *origNextLIR = NEXT_LIR(lir); |
| 304 | |
| 305 | /* |
| 306 | * Find out the last instruction in the PC reconstruction |
| 307 | * cell |
| 308 | */ |
| 309 | while (lastLIR->opCode != ARMV5TE_B_UNCOND) { |
| 310 | lastLIR = NEXT_LIR(lastLIR); |
| 311 | } |
| 312 | |
| 313 | /* Yank out the PCR code */ |
| 314 | PREV_LIR_LVALUE(NEXT_LIR(lastLIR)) = |
| 315 | (LIR *) PREV_LIR(targetLIR); |
| 316 | NEXT_LIR_LVALUE(PREV_LIR(targetLIR)) = |
| 317 | (LIR *) NEXT_LIR(lastLIR); |
| 318 | |
| 319 | /* Create the branch over instruction */ |
| 320 | Armv5teLIR *branchoverLIR = |
| 321 | dvmCompilerNew(sizeof(Armv5teLIR), true); |
| 322 | branchoverLIR->opCode = ARMV5TE_B_UNCOND; |
| 323 | branchoverLIR->generic.target = (LIR *) origNextLIR; |
| 324 | |
| 325 | /* Reconnect the instructions */ |
| 326 | NEXT_LIR_LVALUE(lir) = (LIR *) branchoverLIR; |
| 327 | PREV_LIR_LVALUE(branchoverLIR) = (LIR *) lir; |
| 328 | |
| 329 | NEXT_LIR_LVALUE(branchoverLIR) = (LIR *) targetLIR; |
| 330 | PREV_LIR_LVALUE(targetLIR) = (LIR *) branchoverLIR; |
| 331 | |
| 332 | NEXT_LIR_LVALUE(lastLIR) = (LIR *) origNextLIR; |
| 333 | PREV_LIR_LVALUE(origNextLIR) = (LIR *) lastLIR; |
| 334 | |
| 335 | retry = true; |
| 336 | continue; |
| 337 | } else { |
| 338 | LOGE("Conditional branch distance out of range: %d\n", |
| 339 | delta); |
| 340 | dvmAbort(); |
| 341 | } |
| 342 | } |
| 343 | lir->operands[0] = delta >> 1; |
| 344 | } else if (lir->opCode == ARMV5TE_B_UNCOND) { |
| 345 | Armv5teLIR *targetLIR = (Armv5teLIR *) lir->generic.target; |
| 346 | intptr_t pc = lir->generic.offset + 4; |
| 347 | intptr_t target = targetLIR->generic.offset; |
| 348 | int delta = target - pc; |
| 349 | if (delta > 2046 || delta < -2048) { |
| 350 | LOGE("Unconditional branch distance out of range: %d\n", delta); |
| 351 | dvmAbort(); |
| 352 | } |
| 353 | lir->operands[0] = delta >> 1; |
| 354 | } else if (lir->opCode == ARMV5TE_BLX_1) { |
| 355 | assert(NEXT_LIR(lir)->opCode == ARMV5TE_BLX_2); |
| 356 | /* curPC is Thumb */ |
| 357 | intptr_t curPC = (startAddr + lir->generic.offset + 4) & ~3; |
| 358 | intptr_t target = lir->operands[1]; |
| 359 | |
| 360 | /* Match bit[1] in target with base */ |
| 361 | if (curPC & 0x2) { |
| 362 | target |= 0x2; |
| 363 | } |
| 364 | int delta = target - curPC; |
| 365 | assert((delta >= -(1<<22)) && (delta <= ((1<<22)-2))); |
| 366 | |
| 367 | lir->operands[0] = (delta >> 12) & 0x7ff; |
| 368 | NEXT_LIR(lir)->operands[0] = (delta>> 1) & 0x7ff; |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * The code offset will be recalculated, just continue to check if |
| 373 | * there are other places where code will be rescheduled and do not |
| 374 | * write to the output buffer |
| 375 | */ |
| 376 | if (retry) { |
| 377 | continue; |
| 378 | } |
| 379 | Armv5teEncodingMap *encoder = &EncodingMap[lir->opCode]; |
| 380 | short bits = encoder->skeleton; |
| 381 | int i; |
| 382 | for (i = 0; i < 3; i++) { |
| 383 | short value; |
| 384 | if (encoder->fieldLoc[i].end != -1) { |
| 385 | value = (lir->operands[i] << encoder->fieldLoc[i].start) & |
| 386 | ((1 << (encoder->fieldLoc[i].end + 1)) - 1); |
| 387 | bits |= value; |
| 388 | |
| 389 | } |
| 390 | } |
| 391 | *bufferAddr++ = bits; |
| 392 | } |
| 393 | return retry; |
| 394 | } |
| 395 | |
| 396 | /* |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame^] | 397 | * Translation layout in the code cache. Note that the codeAddress pointer |
| 398 | * in JitTable will point directly to the code body (field codeAddress). The |
| 399 | * chain cell offset codeAddress - 2, and (if present) executionCount is at |
| 400 | * codeAddress - 6. |
| 401 | * |
| 402 | * +----------------------------+ |
| 403 | * | Execution count | -> [Optional] 4 bytes |
| 404 | * +----------------------------+ |
| 405 | * +--| Offset to chain cell counts| -> 2 bytes |
| 406 | * | +----------------------------+ |
| 407 | * | | Code body | -> Start address for translation |
| 408 | * | | | variable in 2-byte chunks |
| 409 | * | . . (JitTable's codeAddress points here) |
| 410 | * | . . |
| 411 | * | | | |
| 412 | * | +----------------------------+ |
| 413 | * | | Chaining Cells | -> 8 bytes each, must be 4 byte aligned |
| 414 | * | . . |
| 415 | * | . . |
| 416 | * | | | |
| 417 | * | +----------------------------+ |
| 418 | * +->| Chaining cell counts | -> 4 bytes, chain cell counts by type |
| 419 | * +----------------------------+ |
| 420 | * | Trace description | -> variable sized |
| 421 | * . . |
| 422 | * | | |
| 423 | * +----------------------------+ |
| 424 | * | Literal pool | -> 4-byte aligned, variable size |
| 425 | * . . |
| 426 | * . . |
| 427 | * | | |
| 428 | * +----------------------------+ |
| 429 | * |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 430 | * Go over each instruction in the list and calculate the offset from the top |
| 431 | * before sending them off to the assembler. If out-of-range branch distance is |
| 432 | * seen rearrange the instructions a bit to correct it. |
| 433 | */ |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame^] | 434 | #define CHAIN_CELL_OFFSET_SIZE 2 |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 435 | void dvmCompilerAssembleLIR(CompilationUnit *cUnit) |
| 436 | { |
| 437 | LIR *lir; |
| 438 | Armv5teLIR *armLIR; |
| 439 | int offset; |
| 440 | int i; |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame^] | 441 | ChainCellCounts chainCellCounts; |
| 442 | u2 chainCellOffset; |
| 443 | int descSize = jitTraceDescriptionSize(cUnit->traceDesc); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 444 | |
| 445 | retry: |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame^] | 446 | /* Beginning offset needs to allow space for chain cell offset */ |
| 447 | for (armLIR = (Armv5teLIR *) cUnit->firstLIRInsn, |
| 448 | offset = CHAIN_CELL_OFFSET_SIZE; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 449 | armLIR; |
| 450 | armLIR = NEXT_LIR(armLIR)) { |
| 451 | armLIR->generic.offset = offset; |
| 452 | if (armLIR->opCode >= 0) { |
| 453 | offset += 2; |
| 454 | } else if (armLIR->opCode == ARMV5TE_PSEUDO_ALIGN4) { |
| 455 | if (offset & 0x2) { |
| 456 | offset += 2; |
| 457 | armLIR->operands[0] = 1; |
| 458 | } else { |
| 459 | armLIR->operands[0] = 0; |
| 460 | } |
| 461 | } |
| 462 | /* Pseudo opcodes don't consume space */ |
| 463 | } |
| 464 | |
| 465 | /* Const values have to be word aligned */ |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame^] | 466 | offset = (offset + 3) & ~3; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 467 | |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame^] | 468 | /* Add space for chain cell counts & trace description */ |
| 469 | chainCellOffset = offset; |
| 470 | offset += sizeof(chainCellCounts) + descSize; |
| 471 | |
| 472 | assert((offset & 0x3) == 0); /* Should still be word aligned */ |
| 473 | |
| 474 | /* Set up offsets for literals */ |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 475 | cUnit->dataOffset = offset; |
| 476 | |
| 477 | for (lir = cUnit->wordList; lir; lir = lir->next) { |
| 478 | lir->offset = offset; |
| 479 | offset += 4; |
| 480 | } |
| 481 | |
| 482 | cUnit->totalSize = offset; |
| 483 | |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame^] | 484 | if (gDvmJit.codeCacheByteUsed + cUnit->totalSize > CODE_CACHE_SIZE) { |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 485 | gDvmJit.codeCacheFull = true; |
| 486 | cUnit->baseAddr = NULL; |
| 487 | return; |
| 488 | } |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame^] | 489 | |
| 490 | /* Allocate enough space for the code block */ |
| 491 | cUnit->codeBuffer = dvmCompilerNew(chainCellOffset, true); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 492 | if (cUnit->codeBuffer == NULL) { |
| 493 | LOGE("Code buffer allocation failure\n"); |
| 494 | cUnit->baseAddr = NULL; |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | bool needRetry = assembleInstructions( |
| 499 | cUnit, (intptr_t) gDvmJit.codeCache + gDvmJit.codeCacheByteUsed); |
| 500 | |
| 501 | if (needRetry) |
| 502 | goto retry; |
| 503 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 504 | cUnit->baseAddr = (char *) gDvmJit.codeCache + gDvmJit.codeCacheByteUsed; |
| 505 | gDvmJit.codeCacheByteUsed += offset; |
| 506 | |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame^] | 507 | /* Install the chain cell offset */ |
| 508 | *((char*)cUnit->baseAddr) = chainCellOffset; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 509 | |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame^] | 510 | /* Install the code block */ |
| 511 | memcpy((char*)cUnit->baseAddr + 2, cUnit->codeBuffer, chainCellOffset - 2); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 512 | gDvmJit.numCompilations++; |
| 513 | |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame^] | 514 | /* Install the chaining cell counts */ |
| 515 | for (i=0; i< CHAINING_CELL_LAST; i++) { |
| 516 | chainCellCounts.u.count[i] = cUnit->numChainingCells[i]; |
| 517 | } |
| 518 | memcpy((char*)cUnit->baseAddr + chainCellOffset, &chainCellCounts, |
| 519 | sizeof(chainCellCounts)); |
| 520 | |
| 521 | /* Install the trace description */ |
| 522 | memcpy((char*)cUnit->baseAddr + chainCellOffset + sizeof(chainCellCounts), |
| 523 | cUnit->traceDesc, descSize); |
| 524 | |
| 525 | /* Write the literals directly into the code cache */ |
| 526 | installDataContent(cUnit); |
| 527 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 528 | /* Flush dcache and invalidate the icache to maintain coherence */ |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame^] | 529 | cacheflush((long)cUnit->baseAddr, |
| 530 | (long)(cUnit->baseAddr + offset), 0); |
| 531 | |
| 532 | /* Adjust baseAddr to point to executable code */ |
| 533 | cUnit->baseAddr = (char*)cUnit->baseAddr + CHAIN_CELL_OFFSET_SIZE; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | /* |
| 537 | * Perform translation chain operation. |
| 538 | * For ARM, we'll use a pair of thumb instructions to generate |
| 539 | * an unconditional chaining branch of up to 4MB in distance. |
| 540 | * Use a BL, though we don't really need the link. The format is |
| 541 | * 111HHooooooooooo |
| 542 | * Where HH is 10 for the 1st inst, and 11 for the second and |
| 543 | * the "o" field is each instruction's 11-bit contribution to the |
| 544 | * 22-bit branch offset. |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame^] | 545 | * If the target is nearby, use a single-instruction bl. |
| 546 | * If one or more threads is suspended, don't chain. |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 547 | */ |
| 548 | void* dvmJitChain(void* tgtAddr, u4* branchAddr) |
| 549 | { |
| 550 | int baseAddr = (u4) branchAddr + 4; |
| 551 | int branchOffset = (int) tgtAddr - baseAddr; |
| 552 | u4 thumb1; |
| 553 | u4 thumb2; |
| 554 | u4 newInst; |
| 555 | |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame^] | 556 | if (gDvm.sumThreadSuspendCount == 0) { |
| 557 | assert((branchOffset >= -(1<<22)) && (branchOffset <= ((1<<22)-2))); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 558 | |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame^] | 559 | gDvmJit.translationChains++; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 560 | |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame^] | 561 | COMPILER_TRACE_CHAINING( |
| 562 | LOGD("Jit Runtime: chaining 0x%x to 0x%x\n", |
| 563 | (int) branchAddr, (int) tgtAddr & -2)); |
| 564 | if ((branchOffset < -2048) | (branchOffset > 2046)) { |
| 565 | thumb1 = (0xf000 | ((branchOffset>>12) & 0x7ff)); |
| 566 | thumb2 = (0xf800 | ((branchOffset>> 1) & 0x7ff)); |
| 567 | } else { |
| 568 | thumb1 = (0xe000 | ((branchOffset>> 1) & 0x7ff)); |
| 569 | thumb2 = 0x4300; /* nop -> or r0, r0 */ |
| 570 | } |
| 571 | |
| 572 | newInst = thumb2<<16 | thumb1; |
| 573 | *branchAddr = newInst; |
| 574 | cacheflush((long)branchAddr, (long)branchAddr + 4, 0); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 577 | return tgtAddr; |
| 578 | } |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame^] | 579 | |
| 580 | /* |
| 581 | * Unchain a trace given the starting address of the translation |
| 582 | * in the code cache. Refer to the diagram in dvmCompilerAssembleLIR. |
| 583 | * Returns the address following the last cell unchained. Note that |
| 584 | * the incoming codeAddr is a thumb code address, and therefore has |
| 585 | * the low bit set. |
| 586 | */ |
| 587 | u4* dvmJitUnchain(void* codeAddr) |
| 588 | { |
| 589 | u2* pChainCellOffset = (u2*)((char*)codeAddr - 3); |
| 590 | u2 chainCellOffset = *pChainCellOffset; |
| 591 | ChainCellCounts *pChainCellCounts = |
| 592 | (ChainCellCounts*)((char*)codeAddr + chainCellOffset -3); |
| 593 | int cellCount; |
| 594 | u4* pChainCells; |
| 595 | u4* pStart; |
| 596 | u4 thumb1; |
| 597 | u4 thumb2; |
| 598 | u4 newInst; |
| 599 | int i,j; |
| 600 | |
| 601 | /* Get total count of chain cells */ |
| 602 | for (i = 0, cellCount = 0; i < CHAINING_CELL_LAST; i++) { |
| 603 | cellCount += pChainCellCounts->u.count[i]; |
| 604 | } |
| 605 | |
| 606 | /* Locate the beginning of the chain cell region */ |
| 607 | pStart = pChainCells = (u4*)((char*)pChainCellCounts - (cellCount * 8)); |
| 608 | |
| 609 | /* The cells are sorted in order - walk through them and reset */ |
| 610 | for (i = 0; i < CHAINING_CELL_LAST; i++) { |
| 611 | for (j = 0; j < pChainCellCounts->u.count[i]; j++) { |
| 612 | int targetOffset; |
| 613 | switch(i) { |
| 614 | case CHAINING_CELL_GENERIC: |
| 615 | targetOffset = offsetof(InterpState, |
| 616 | jitToInterpEntries.dvmJitToInterpNormal); |
| 617 | break; |
| 618 | case CHAINING_CELL_POST_INVOKE: |
| 619 | case CHAINING_CELL_INVOKE: |
| 620 | targetOffset = offsetof(InterpState, |
| 621 | jitToInterpEntries.dvmJitToTraceSelect); |
| 622 | break; |
| 623 | default: |
| 624 | dvmAbort(); |
| 625 | } |
| 626 | /* |
| 627 | * Arm code sequence for a chaining cell is: |
| 628 | * ldr r0, rGLUE, #<word offset> |
| 629 | * blx r0 |
| 630 | */ |
| 631 | COMPILER_TRACE_CHAINING( |
| 632 | LOGD("Jit Runtime: unchaining 0x%x", (int)pChainCells)); |
| 633 | targetOffset = targetOffset >> 2; /* convert to word offset */ |
| 634 | thumb1 = 0x6800 | (targetOffset << 6) | (rGLUE << 3) | (r0 << 0); |
| 635 | thumb2 = 0x4780 | (r0 << 3); |
| 636 | newInst = thumb2<<16 | thumb1; |
| 637 | *pChainCells = newInst; |
| 638 | pChainCells += 2; /* Advance by 2 words */ |
| 639 | } |
| 640 | } |
| 641 | return pChainCells; |
| 642 | } |
| 643 | |
| 644 | /* Unchain all translation in the cache. */ |
| 645 | void dvmJitUnchainAll() |
| 646 | { |
| 647 | u4* lowAddress = NULL; |
| 648 | u4* highAddress = NULL; |
| 649 | unsigned int i; |
| 650 | if (gDvmJit.pJitEntryTable != NULL) { |
| 651 | COMPILER_TRACE_CHAINING(LOGD("Jit Runtime: unchaining all")); |
| 652 | dvmLockMutex(&gDvmJit.tableLock); |
| 653 | for (i = 0; i < gDvmJit.maxTableEntries; i++) { |
| 654 | if (gDvmJit.pJitEntryTable[i].dPC && |
| 655 | gDvmJit.pJitEntryTable[i].codeAddress) { |
| 656 | u4* lastAddress; |
| 657 | lastAddress = |
| 658 | dvmJitUnchain(gDvmJit.pJitEntryTable[i].codeAddress); |
| 659 | if (lowAddress == NULL || |
| 660 | (u4*)gDvmJit.pJitEntryTable[i].codeAddress < lowAddress) |
| 661 | lowAddress = lastAddress; |
| 662 | if (lastAddress > highAddress) |
| 663 | highAddress = lastAddress; |
| 664 | } |
| 665 | } |
| 666 | cacheflush((long)lowAddress, (long)highAddress, 0); |
| 667 | dvmUnlockMutex(&gDvmJit.tableLock); |
| 668 | } |
| 669 | } |