| 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 */ |
| 219 | static void writeDataContent(CompilationUnit *cUnit) |
| 220 | { |
| 221 | int *dataPtr = (int *) (cUnit->codeBuffer + cUnit->dataOffset); |
| 222 | Armv5teLIR *dataLIR = (Armv5teLIR *) cUnit->wordList; |
| 223 | while (dataLIR) { |
| 224 | *dataPtr++ = dataLIR->operands[0]; |
| 225 | dataLIR = NEXT_LIR(dataLIR); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /* Return TRUE if error happens */ |
| 230 | static bool assembleInstructions(CompilationUnit *cUnit, intptr_t startAddr) |
| 231 | { |
| 232 | short *bufferAddr = (short *) cUnit->codeBuffer; |
| 233 | Armv5teLIR *lir; |
| 234 | bool retry = false; |
| 235 | |
| 236 | for (lir = (Armv5teLIR *) cUnit->firstLIRInsn; lir; lir = NEXT_LIR(lir)) { |
| 237 | if (lir->opCode < 0) { |
| 238 | if ((lir->opCode == ARMV5TE_PSEUDO_ALIGN4) && |
| 239 | (lir->operands[0] == 1) && |
| 240 | !retry) { |
| 241 | *bufferAddr++ = PADDING_MOV_R0_R0; |
| 242 | } |
| 243 | continue; |
| 244 | } |
| 245 | |
| 246 | if (lir->opCode == ARMV5TE_LDR_PC_REL || |
| 247 | lir->opCode == ARMV5TE_ADD_PC_REL) { |
| 248 | Armv5teLIR *lirTarget = (Armv5teLIR *) lir->generic.target; |
| 249 | intptr_t pc = (lir->generic.offset + 4) & ~3; |
| 250 | intptr_t target = lirTarget->generic.offset; |
| 251 | int delta = target - pc; |
| 252 | if (delta & 0x3) { |
| 253 | LOGE("PC-rel distance is not multiples of 4: %d\n", delta); |
| 254 | dvmAbort(); |
| 255 | } |
| 256 | lir->operands[1] = delta >> 2; |
| 257 | } else if (lir->opCode == ARMV5TE_B_COND) { |
| 258 | Armv5teLIR *targetLIR = (Armv5teLIR *) lir->generic.target; |
| 259 | intptr_t pc = lir->generic.offset + 4; |
| 260 | intptr_t target = targetLIR->generic.offset; |
| 261 | int delta = target - pc; |
| 262 | if (delta > 254 || delta < -256) { |
| 263 | /* Pull in the PC reconstruction code inline */ |
| 264 | if (targetLIR->opCode == ARMV5TE_PSEUDO_PC_RECONSTRUCTION_CELL){ |
| 265 | /* |
| 266 | * The original code is: |
| 267 | * |
| 268 | * bxx targetLIR |
| 269 | * origNextLir |
| 270 | * : |
| 271 | * : |
| 272 | * targetLIR (a PC reconstruction cell) |
| 273 | * : |
| 274 | * lastLIR (should be a unconditional branch) |
| 275 | * |
| 276 | * The distance from bxx to targetLIR is too far, so we want |
| 277 | * to rearrange the code to be: |
| 278 | * |
| 279 | * bxx targetLIR |
| 280 | * branchoverLIR to origNextLir |
| 281 | * targetLIR (a PC reconstruction cell) |
| 282 | * : |
| 283 | * lastLIR (should be a unconditional branch) |
| 284 | * origNextLir |
| 285 | * |
| 286 | * Although doing so adds a unconditional branchover |
| 287 | * instruction, it can be predicted for free by ARM so |
| 288 | * the penalty should be minimal. |
| 289 | */ |
| 290 | Armv5teLIR *pcrLIR = targetLIR; |
| 291 | Armv5teLIR *lastLIR = pcrLIR; |
| 292 | Armv5teLIR *origNextLIR = NEXT_LIR(lir); |
| 293 | |
| 294 | /* |
| 295 | * Find out the last instruction in the PC reconstruction |
| 296 | * cell |
| 297 | */ |
| 298 | while (lastLIR->opCode != ARMV5TE_B_UNCOND) { |
| 299 | lastLIR = NEXT_LIR(lastLIR); |
| 300 | } |
| 301 | |
| 302 | /* Yank out the PCR code */ |
| 303 | PREV_LIR_LVALUE(NEXT_LIR(lastLIR)) = |
| 304 | (LIR *) PREV_LIR(targetLIR); |
| 305 | NEXT_LIR_LVALUE(PREV_LIR(targetLIR)) = |
| 306 | (LIR *) NEXT_LIR(lastLIR); |
| 307 | |
| 308 | /* Create the branch over instruction */ |
| 309 | Armv5teLIR *branchoverLIR = |
| 310 | dvmCompilerNew(sizeof(Armv5teLIR), true); |
| 311 | branchoverLIR->opCode = ARMV5TE_B_UNCOND; |
| 312 | branchoverLIR->generic.target = (LIR *) origNextLIR; |
| 313 | |
| 314 | /* Reconnect the instructions */ |
| 315 | NEXT_LIR_LVALUE(lir) = (LIR *) branchoverLIR; |
| 316 | PREV_LIR_LVALUE(branchoverLIR) = (LIR *) lir; |
| 317 | |
| 318 | NEXT_LIR_LVALUE(branchoverLIR) = (LIR *) targetLIR; |
| 319 | PREV_LIR_LVALUE(targetLIR) = (LIR *) branchoverLIR; |
| 320 | |
| 321 | NEXT_LIR_LVALUE(lastLIR) = (LIR *) origNextLIR; |
| 322 | PREV_LIR_LVALUE(origNextLIR) = (LIR *) lastLIR; |
| 323 | |
| 324 | retry = true; |
| 325 | continue; |
| 326 | } else { |
| 327 | LOGE("Conditional branch distance out of range: %d\n", |
| 328 | delta); |
| 329 | dvmAbort(); |
| 330 | } |
| 331 | } |
| 332 | lir->operands[0] = delta >> 1; |
| 333 | } else if (lir->opCode == ARMV5TE_B_UNCOND) { |
| 334 | Armv5teLIR *targetLIR = (Armv5teLIR *) lir->generic.target; |
| 335 | intptr_t pc = lir->generic.offset + 4; |
| 336 | intptr_t target = targetLIR->generic.offset; |
| 337 | int delta = target - pc; |
| 338 | if (delta > 2046 || delta < -2048) { |
| 339 | LOGE("Unconditional branch distance out of range: %d\n", delta); |
| 340 | dvmAbort(); |
| 341 | } |
| 342 | lir->operands[0] = delta >> 1; |
| 343 | } else if (lir->opCode == ARMV5TE_BLX_1) { |
| 344 | assert(NEXT_LIR(lir)->opCode == ARMV5TE_BLX_2); |
| 345 | /* curPC is Thumb */ |
| 346 | intptr_t curPC = (startAddr + lir->generic.offset + 4) & ~3; |
| 347 | intptr_t target = lir->operands[1]; |
| 348 | |
| 349 | /* Match bit[1] in target with base */ |
| 350 | if (curPC & 0x2) { |
| 351 | target |= 0x2; |
| 352 | } |
| 353 | int delta = target - curPC; |
| 354 | assert((delta >= -(1<<22)) && (delta <= ((1<<22)-2))); |
| 355 | |
| 356 | lir->operands[0] = (delta >> 12) & 0x7ff; |
| 357 | NEXT_LIR(lir)->operands[0] = (delta>> 1) & 0x7ff; |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | * The code offset will be recalculated, just continue to check if |
| 362 | * there are other places where code will be rescheduled and do not |
| 363 | * write to the output buffer |
| 364 | */ |
| 365 | if (retry) { |
| 366 | continue; |
| 367 | } |
| 368 | Armv5teEncodingMap *encoder = &EncodingMap[lir->opCode]; |
| 369 | short bits = encoder->skeleton; |
| 370 | int i; |
| 371 | for (i = 0; i < 3; i++) { |
| 372 | short value; |
| 373 | if (encoder->fieldLoc[i].end != -1) { |
| 374 | value = (lir->operands[i] << encoder->fieldLoc[i].start) & |
| 375 | ((1 << (encoder->fieldLoc[i].end + 1)) - 1); |
| 376 | bits |= value; |
| 377 | |
| 378 | } |
| 379 | } |
| 380 | *bufferAddr++ = bits; |
| 381 | } |
| 382 | return retry; |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | * Go over each instruction in the list and calculate the offset from the top |
| 387 | * before sending them off to the assembler. If out-of-range branch distance is |
| 388 | * seen rearrange the instructions a bit to correct it. |
| 389 | */ |
| 390 | void dvmCompilerAssembleLIR(CompilationUnit *cUnit) |
| 391 | { |
| 392 | LIR *lir; |
| 393 | Armv5teLIR *armLIR; |
| 394 | int offset; |
| 395 | int i; |
| 396 | |
| 397 | retry: |
| 398 | for (armLIR = (Armv5teLIR *) cUnit->firstLIRInsn, offset = 0; |
| 399 | armLIR; |
| 400 | armLIR = NEXT_LIR(armLIR)) { |
| 401 | armLIR->generic.offset = offset; |
| 402 | if (armLIR->opCode >= 0) { |
| 403 | offset += 2; |
| 404 | } else if (armLIR->opCode == ARMV5TE_PSEUDO_ALIGN4) { |
| 405 | if (offset & 0x2) { |
| 406 | offset += 2; |
| 407 | armLIR->operands[0] = 1; |
| 408 | } else { |
| 409 | armLIR->operands[0] = 0; |
| 410 | } |
| 411 | } |
| 412 | /* Pseudo opcodes don't consume space */ |
| 413 | } |
| 414 | |
| 415 | /* Const values have to be word aligned */ |
| 416 | offset = ((offset + 3) >> 2) << 2; |
| 417 | |
| 418 | cUnit->dataOffset = offset; |
| 419 | |
| 420 | for (lir = cUnit->wordList; lir; lir = lir->next) { |
| 421 | lir->offset = offset; |
| 422 | offset += 4; |
| 423 | } |
| 424 | |
| 425 | cUnit->totalSize = offset; |
| 426 | |
| 427 | if (gDvmJit.codeCacheByteUsed + offset > CODE_CACHE_SIZE) { |
| 428 | gDvmJit.codeCacheFull = true; |
| 429 | cUnit->baseAddr = NULL; |
| 430 | return; |
| 431 | } |
| 432 | cUnit->codeBuffer = dvmCompilerNew(offset, true); |
| 433 | if (cUnit->codeBuffer == NULL) { |
| 434 | LOGE("Code buffer allocation failure\n"); |
| 435 | cUnit->baseAddr = NULL; |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | bool needRetry = assembleInstructions( |
| 440 | cUnit, (intptr_t) gDvmJit.codeCache + gDvmJit.codeCacheByteUsed); |
| 441 | |
| 442 | if (needRetry) |
| 443 | goto retry; |
| 444 | |
| 445 | writeDataContent(cUnit); |
| 446 | |
| 447 | cUnit->baseAddr = (char *) gDvmJit.codeCache + gDvmJit.codeCacheByteUsed; |
| 448 | gDvmJit.codeCacheByteUsed += offset; |
| 449 | |
| 450 | |
| 451 | /* Install the compilation */ |
| 452 | memcpy(cUnit->baseAddr, cUnit->codeBuffer, offset); |
| 453 | gDvmJit.numCompilations++; |
| 454 | |
| 455 | /* Flush dcache and invalidate the icache to maintain coherence */ |
| 456 | cacheflush((intptr_t) cUnit->baseAddr, |
| 457 | (intptr_t) (cUnit->baseAddr + offset), 0); |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * Perform translation chain operation. |
| 462 | * For ARM, we'll use a pair of thumb instructions to generate |
| 463 | * an unconditional chaining branch of up to 4MB in distance. |
| 464 | * Use a BL, though we don't really need the link. The format is |
| 465 | * 111HHooooooooooo |
| 466 | * Where HH is 10 for the 1st inst, and 11 for the second and |
| 467 | * the "o" field is each instruction's 11-bit contribution to the |
| 468 | * 22-bit branch offset. |
| 469 | * TUNING: use a single-instruction variant if it reaches. |
| 470 | */ |
| 471 | void* dvmJitChain(void* tgtAddr, u4* branchAddr) |
| 472 | { |
| 473 | int baseAddr = (u4) branchAddr + 4; |
| 474 | int branchOffset = (int) tgtAddr - baseAddr; |
| 475 | u4 thumb1; |
| 476 | u4 thumb2; |
| 477 | u4 newInst; |
| 478 | |
| 479 | assert((branchOffset >= -(1<<22)) && (branchOffset <= ((1<<22)-2))); |
| 480 | |
| 481 | gDvmJit.translationChains++; |
| 482 | |
| 483 | COMPILER_TRACE_CHAINING( |
| 484 | LOGD("Jit Runtime: chaining 0x%x to 0x%x\n", |
| 485 | (int) branchAddr, (int) tgtAddr & -2)); |
| 486 | if ((branchOffset < -2048) | (branchOffset > 2046)) { |
| 487 | thumb1 = (0xf000 | ((branchOffset>>12) & 0x7ff)); |
| 488 | thumb2 = (0xf800 | ((branchOffset>> 1) & 0x7ff)); |
| 489 | } else { |
| 490 | thumb1 = (0xe000 | ((branchOffset>> 1) & 0x7ff)); |
| 491 | thumb2 = 0x4300; /* nop -> or r0, r0 */ |
| 492 | } |
| 493 | |
| 494 | newInst = thumb2<<16 | thumb1; |
| 495 | *branchAddr = newInst; |
| 496 | cacheflush((intptr_t) branchAddr, (intptr_t) branchAddr + 4, 0); |
| 497 | |
| 498 | return tgtAddr; |
| 499 | } |