buzbee | 1452bee | 2015-03-06 14:43:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | /* |
| 18 | Art assembly interpreter notes: |
| 19 | |
| 20 | First validate assembly code by implementing ExecuteXXXImpl() style body (doesn't |
| 21 | handle invoke, allows higher-level code to create frame & shadow frame. |
| 22 | |
| 23 | Once that's working, support direct entry code & eliminate shadow frame (and |
| 24 | excess locals allocation. |
| 25 | |
| 26 | Some (hopefully) temporary ugliness. We'll treat rFP as pointing to the |
| 27 | base of the vreg array within the shadow frame. Access the other fields, |
| 28 | dex_pc_, method_ and number_of_vregs_ via negative offsets. For now, we'll continue |
| 29 | the shadow frame mechanism of double-storing object references - via rFP & |
| 30 | number_of_vregs_. |
| 31 | |
| 32 | */ |
| 33 | |
| 34 | /* |
| 35 | ARM EABI general notes: |
| 36 | |
| 37 | r0-r3 hold first 4 args to a method; they are not preserved across method calls |
| 38 | r4-r8 are available for general use |
| 39 | r9 is given special treatment in some situations, but not for us |
| 40 | r10 (sl) seems to be generally available |
| 41 | r11 (fp) is used by gcc (unless -fomit-frame-pointer is set) |
| 42 | r12 (ip) is scratch -- not preserved across method calls |
| 43 | r13 (sp) should be managed carefully in case a signal arrives |
| 44 | r14 (lr) must be preserved |
| 45 | r15 (pc) can be tinkered with directly |
| 46 | |
| 47 | r0 holds returns of <= 4 bytes |
| 48 | r0-r1 hold returns of 8 bytes, low word in r0 |
| 49 | |
| 50 | Callee must save/restore r4+ (except r12) if it modifies them. If VFP |
| 51 | is present, registers s16-s31 (a/k/a d8-d15, a/k/a q4-q7) must be preserved, |
| 52 | s0-s15 (d0-d7, q0-a3) do not need to be. |
| 53 | |
| 54 | Stack is "full descending". Only the arguments that don't fit in the first 4 |
| 55 | registers are placed on the stack. "sp" points at the first stacked argument |
| 56 | (i.e. the 5th arg). |
| 57 | |
| 58 | VFP: single-precision results in s0, double-precision results in d0. |
| 59 | |
| 60 | In the EABI, "sp" must be 64-bit aligned on entry to a function, and any |
| 61 | 64-bit quantities (long long, double) must be 64-bit aligned. |
| 62 | */ |
| 63 | |
| 64 | /* |
| 65 | Mterp and ARM notes: |
| 66 | |
| 67 | The following registers have fixed assignments: |
| 68 | |
| 69 | reg nick purpose |
| 70 | r4 rPC interpreted program counter, used for fetching instructions |
| 71 | r5 rFP interpreted frame pointer, used for accessing locals and args |
| 72 | r6 rSELF self (Thread) pointer |
| 73 | r7 rINST first 16-bit code unit of current instruction |
| 74 | r8 rIBASE interpreted instruction base pointer, used for computed goto |
| 75 | r11 rREFS base of object references in shadow frame (ideally, we'll get rid of this later). |
| 76 | |
| 77 | Macros are provided for common operations. Each macro MUST emit only |
| 78 | one instruction to make instruction-counting easier. They MUST NOT alter |
| 79 | unspecified registers or condition codes. |
| 80 | */ |
| 81 | |
| 82 | /* |
| 83 | * This is a #include, not a %include, because we want the C pre-processor |
| 84 | * to expand the macros into assembler assignment statements. |
| 85 | */ |
| 86 | #include "asm_support.h" |
| 87 | |
Bill Buzbee | fd522f9 | 2016-02-11 22:37:42 +0000 | [diff] [blame] | 88 | #define MTERP_PROFILE_BRANCHES 1 |
| 89 | #define MTERP_LOGGING 0 |
| 90 | |
buzbee | 1452bee | 2015-03-06 14:43:04 -0800 | [diff] [blame] | 91 | /* During bringup, we'll use the shadow frame model instead of rFP */ |
| 92 | /* single-purpose registers, given names for clarity */ |
| 93 | #define rPC r4 |
| 94 | #define rFP r5 |
| 95 | #define rSELF r6 |
| 96 | #define rINST r7 |
| 97 | #define rIBASE r8 |
| 98 | #define rREFS r11 |
| 99 | |
| 100 | /* |
| 101 | * Instead of holding a pointer to the shadow frame, we keep rFP at the base of the vregs. So, |
| 102 | * to access other shadow frame fields, we need to use a backwards offset. Define those here. |
| 103 | */ |
| 104 | #define OFF_FP(a) (a - SHADOWFRAME_VREGS_OFFSET) |
| 105 | #define OFF_FP_NUMBER_OF_VREGS OFF_FP(SHADOWFRAME_NUMBER_OF_VREGS_OFFSET) |
| 106 | #define OFF_FP_DEX_PC OFF_FP(SHADOWFRAME_DEX_PC_OFFSET) |
| 107 | #define OFF_FP_LINK OFF_FP(SHADOWFRAME_LINK_OFFSET) |
| 108 | #define OFF_FP_METHOD OFF_FP(SHADOWFRAME_METHOD_OFFSET) |
| 109 | #define OFF_FP_RESULT_REGISTER OFF_FP(SHADOWFRAME_RESULT_REGISTER_OFFSET) |
| 110 | #define OFF_FP_DEX_PC_PTR OFF_FP(SHADOWFRAME_DEX_PC_PTR_OFFSET) |
| 111 | #define OFF_FP_CODE_ITEM OFF_FP(SHADOWFRAME_CODE_ITEM_OFFSET) |
| 112 | #define OFF_FP_SHADOWFRAME (-SHADOWFRAME_VREGS_OFFSET) |
| 113 | |
| 114 | /* |
buzbee | 1452bee | 2015-03-06 14:43:04 -0800 | [diff] [blame] | 115 | * "export" the PC to dex_pc field in the shadow frame, f/b/o future exception objects. Must |
| 116 | * be done *before* something throws. |
| 117 | * |
| 118 | * It's okay to do this more than once. |
| 119 | * |
| 120 | * NOTE: the fast interpreter keeps track of dex pc as a direct pointer to the mapped |
| 121 | * dex byte codes. However, the rest of the runtime expects dex pc to be an instruction |
| 122 | * offset into the code_items_[] array. For effiency, we will "export" the |
| 123 | * current dex pc as a direct pointer using the EXPORT_PC macro, and rely on GetDexPC |
| 124 | * to convert to a dex pc when needed. |
| 125 | */ |
| 126 | .macro EXPORT_PC |
| 127 | str rPC, [rFP, #OFF_FP_DEX_PC_PTR] |
| 128 | .endm |
| 129 | |
| 130 | .macro EXPORT_DEX_PC tmp |
| 131 | ldr \tmp, [rFP, #OFF_FP_CODE_ITEM] |
| 132 | str rPC, [rFP, #OFF_FP_DEX_PC_PTR] |
| 133 | add \tmp, #CODEITEM_INSNS_OFFSET |
| 134 | sub \tmp, rPC, \tmp |
| 135 | asr \tmp, #1 |
| 136 | str \tmp, [rFP, #OFF_FP_DEX_PC] |
| 137 | .endm |
| 138 | |
| 139 | /* |
| 140 | * Fetch the next instruction from rPC into rINST. Does not advance rPC. |
| 141 | */ |
| 142 | .macro FETCH_INST |
| 143 | ldrh rINST, [rPC] |
| 144 | .endm |
| 145 | |
| 146 | /* |
| 147 | * Fetch the next instruction from the specified offset. Advances rPC |
| 148 | * to point to the next instruction. "_count" is in 16-bit code units. |
| 149 | * |
| 150 | * Because of the limited size of immediate constants on ARM, this is only |
| 151 | * suitable for small forward movements (i.e. don't try to implement "goto" |
| 152 | * with this). |
| 153 | * |
| 154 | * This must come AFTER anything that can throw an exception, or the |
| 155 | * exception catch may miss. (This also implies that it must come after |
| 156 | * EXPORT_PC.) |
| 157 | */ |
| 158 | .macro FETCH_ADVANCE_INST count |
| 159 | ldrh rINST, [rPC, #((\count)*2)]! |
| 160 | .endm |
| 161 | |
| 162 | /* |
| 163 | * The operation performed here is similar to FETCH_ADVANCE_INST, except the |
| 164 | * src and dest registers are parameterized (not hard-wired to rPC and rINST). |
| 165 | */ |
| 166 | .macro PREFETCH_ADVANCE_INST dreg, sreg, count |
| 167 | ldrh \dreg, [\sreg, #((\count)*2)]! |
| 168 | .endm |
| 169 | |
| 170 | /* |
| 171 | * Similar to FETCH_ADVANCE_INST, but does not update rPC. Used to load |
| 172 | * rINST ahead of possible exception point. Be sure to manually advance rPC |
| 173 | * later. |
| 174 | */ |
| 175 | .macro PREFETCH_INST count |
| 176 | ldrh rINST, [rPC, #((\count)*2)] |
| 177 | .endm |
| 178 | |
| 179 | /* Advance rPC by some number of code units. */ |
| 180 | .macro ADVANCE count |
| 181 | add rPC, #((\count)*2) |
| 182 | .endm |
| 183 | |
| 184 | /* |
| 185 | * Fetch the next instruction from an offset specified by _reg. Updates |
| 186 | * rPC to point to the next instruction. "_reg" must specify the distance |
| 187 | * in bytes, *not* 16-bit code units, and may be a signed value. |
| 188 | * |
| 189 | * We want to write "ldrh rINST, [rPC, _reg, lsl #1]!", but some of the |
| 190 | * bits that hold the shift distance are used for the half/byte/sign flags. |
| 191 | * In some cases we can pre-double _reg for free, so we require a byte offset |
| 192 | * here. |
| 193 | */ |
| 194 | .macro FETCH_ADVANCE_INST_RB reg |
| 195 | ldrh rINST, [rPC, \reg]! |
| 196 | .endm |
| 197 | |
| 198 | /* |
| 199 | * Fetch a half-word code unit from an offset past the current PC. The |
| 200 | * "_count" value is in 16-bit code units. Does not advance rPC. |
| 201 | * |
| 202 | * The "_S" variant works the same but treats the value as signed. |
| 203 | */ |
| 204 | .macro FETCH reg, count |
| 205 | ldrh \reg, [rPC, #((\count)*2)] |
| 206 | .endm |
| 207 | |
| 208 | .macro FETCH_S reg, count |
| 209 | ldrsh \reg, [rPC, #((\count)*2)] |
| 210 | .endm |
| 211 | |
| 212 | /* |
| 213 | * Fetch one byte from an offset past the current PC. Pass in the same |
| 214 | * "_count" as you would for FETCH, and an additional 0/1 indicating which |
| 215 | * byte of the halfword you want (lo/hi). |
| 216 | */ |
| 217 | .macro FETCH_B reg, count, byte |
| 218 | ldrb \reg, [rPC, #((\count)*2+(\byte))] |
| 219 | .endm |
| 220 | |
| 221 | /* |
| 222 | * Put the instruction's opcode field into the specified register. |
| 223 | */ |
| 224 | .macro GET_INST_OPCODE reg |
| 225 | and \reg, rINST, #255 |
| 226 | .endm |
| 227 | |
| 228 | /* |
| 229 | * Put the prefetched instruction's opcode field into the specified register. |
| 230 | */ |
| 231 | .macro GET_PREFETCHED_OPCODE oreg, ireg |
| 232 | and \oreg, \ireg, #255 |
| 233 | .endm |
| 234 | |
| 235 | /* |
| 236 | * Begin executing the opcode in _reg. Because this only jumps within the |
| 237 | * interpreter, we don't have to worry about pre-ARMv5 THUMB interwork. |
| 238 | */ |
| 239 | .macro GOTO_OPCODE reg |
| 240 | add pc, rIBASE, \reg, lsl #${handler_size_bits} |
| 241 | .endm |
| 242 | .macro GOTO_OPCODE_BASE base,reg |
| 243 | add pc, \base, \reg, lsl #${handler_size_bits} |
| 244 | .endm |
| 245 | |
| 246 | /* |
| 247 | * Get/set the 32-bit value from a Dalvik register. |
| 248 | */ |
| 249 | .macro GET_VREG reg, vreg |
| 250 | ldr \reg, [rFP, \vreg, lsl #2] |
| 251 | .endm |
| 252 | .macro SET_VREG reg, vreg |
| 253 | str \reg, [rFP, \vreg, lsl #2] |
| 254 | mov \reg, #0 |
| 255 | str \reg, [rREFS, \vreg, lsl #2] |
| 256 | .endm |
| 257 | .macro SET_VREG_OBJECT reg, vreg, tmpreg |
| 258 | str \reg, [rFP, \vreg, lsl #2] |
| 259 | str \reg, [rREFS, \vreg, lsl #2] |
| 260 | .endm |
buzbee | 50cf600 | 2016-02-10 08:59:12 -0800 | [diff] [blame] | 261 | .macro SET_VREG_SHADOW reg, vreg |
| 262 | str \reg, [rREFS, \vreg, lsl #2] |
| 263 | .endm |
| 264 | |
| 265 | /* |
| 266 | * Clear the corresponding shadow regs for a vreg pair |
| 267 | */ |
| 268 | .macro CLEAR_SHADOW_PAIR vreg, tmp1, tmp2 |
| 269 | mov \tmp1, #0 |
| 270 | add \tmp2, \vreg, #1 |
| 271 | SET_VREG_SHADOW \tmp1, \vreg |
| 272 | SET_VREG_SHADOW \tmp1, \tmp2 |
| 273 | .endm |
buzbee | 1452bee | 2015-03-06 14:43:04 -0800 | [diff] [blame] | 274 | |
| 275 | /* |
| 276 | * Convert a virtual register index into an address. |
| 277 | */ |
| 278 | .macro VREG_INDEX_TO_ADDR reg, vreg |
| 279 | add \reg, rFP, \vreg, lsl #2 /* WARNING/FIXME: handle shadow frame vreg zero if store */ |
| 280 | .endm |
| 281 | |
| 282 | /* |
| 283 | * Refresh handler table. |
| 284 | */ |
| 285 | .macro REFRESH_IBASE |
| 286 | ldr rIBASE, [rSELF, #THREAD_CURRENT_IBASE_OFFSET] |
| 287 | .endm |