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