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