blob: 351a6075cb67ea2ce9caf444cc03ca422c734609 [file] [log] [blame]
Bill Buzbee3b0b4b92016-02-02 13:45:36 +00001/*
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 xFP 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 xFP &
30 number_of_vregs_.
31
32 */
33
34/*
35ARM64 Runtime register usage conventions.
36
37 r0 : w0 is 32-bit return register and x0 is 64-bit.
38 r0-r7 : Argument registers.
39 r8-r15 : Caller save registers (used as temporary registers).
40 r16-r17: Also known as ip0-ip1, respectively. Used as scratch registers by
41 the linker, by the trampolines and other stubs (the backend uses
42 these as temporary registers).
43 r18 : Caller save register (used as temporary register).
44 r19 : Pointer to thread-local storage.
45 r20-r29: Callee save registers.
46 r30 : (lr) is reserved (the link register).
47 rsp : (sp) is reserved (the stack pointer).
48 rzr : (zr) is reserved (the zero register).
49
50 Floating-point registers
51 v0-v31
52
53 v0 : s0 is return register for singles (32-bit) and d0 for doubles (64-bit).
54 This is analogous to the C/C++ (hard-float) calling convention.
55 v0-v7 : Floating-point argument registers in both Dalvik and C/C++ conventions.
56 Also used as temporary and codegen scratch registers.
57
58 v0-v7 and v16-v31 : trashed across C calls.
59 v8-v15 : bottom 64-bits preserved across C calls (d8-d15 are preserved).
60
61 v16-v31: Used as codegen temp/scratch.
62 v8-v15 : Can be used for promotion.
63
64 Must maintain 16-byte stack alignment.
65
66Mterp notes:
67
68The following registers have fixed assignments:
69
70 reg nick purpose
71 x20 xPC interpreted program counter, used for fetching instructions
72 x21 xFP interpreted frame pointer, used for accessing locals and args
73 x22 xSELF self (Thread) pointer
74 x23 xINST first 16-bit code unit of current instruction
75 x24 xIBASE interpreted instruction base pointer, used for computed goto
76 x25 xREFS base of object references in shadow frame (ideally, we'll get rid of this later).
77 x16 ip scratch reg
78 x17 ip2 scratch reg (used by macros)
79
80Macros are provided for common operations. They MUST NOT alter unspecified registers or condition
81codes.
82*/
83
84/*
85 * This is a #include, not a %include, because we want the C pre-processor
86 * to expand the macros into assembler assignment statements.
87 */
88#include "asm_support.h"
89
90/* During bringup, we'll use the shadow frame model instead of xFP */
91/* single-purpose registers, given names for clarity */
92#define xPC x20
93#define xFP x21
94#define xSELF x22
95#define xINST x23
96#define wINST w23
97#define xIBASE x24
98#define xREFS x25
99#define ip x16
100#define ip2 x17
101
102/*
103 * Instead of holding a pointer to the shadow frame, we keep xFP at the base of the vregs. So,
104 * to access other shadow frame fields, we need to use a backwards offset. Define those here.
105 */
106#define OFF_FP(a) (a - SHADOWFRAME_VREGS_OFFSET)
107#define OFF_FP_NUMBER_OF_VREGS OFF_FP(SHADOWFRAME_NUMBER_OF_VREGS_OFFSET)
108#define OFF_FP_DEX_PC OFF_FP(SHADOWFRAME_DEX_PC_OFFSET)
109#define OFF_FP_LINK OFF_FP(SHADOWFRAME_LINK_OFFSET)
110#define OFF_FP_METHOD OFF_FP(SHADOWFRAME_METHOD_OFFSET)
111#define OFF_FP_RESULT_REGISTER OFF_FP(SHADOWFRAME_RESULT_REGISTER_OFFSET)
112#define OFF_FP_DEX_PC_PTR OFF_FP(SHADOWFRAME_DEX_PC_PTR_OFFSET)
113#define OFF_FP_CODE_ITEM OFF_FP(SHADOWFRAME_CODE_ITEM_OFFSET)
114#define OFF_FP_SHADOWFRAME (-SHADOWFRAME_VREGS_OFFSET)
115
116/*
117 *
118 * The reference interpreter performs explicit suspect checks, which is somewhat wasteful.
119 * Dalvik's interpreter folded suspend checks into the jump table mechanism, and eventually
120 * mterp should do so as well.
121 */
122#define MTERP_SUSPEND 0
123
124/*
125 * "export" the PC to dex_pc field in the shadow frame, f/b/o future exception objects. Must
126 * be done *before* something throws.
127 *
128 * It's okay to do this more than once.
129 *
130 * NOTE: the fast interpreter keeps track of dex pc as a direct pointer to the mapped
131 * dex byte codes. However, the rest of the runtime expects dex pc to be an instruction
132 * offset into the code_items_[] array. For effiency, we will "export" the
133 * current dex pc as a direct pointer using the EXPORT_PC macro, and rely on GetDexPC
134 * to convert to a dex pc when needed.
135 */
136.macro EXPORT_PC
137 str xPC, [xFP, #OFF_FP_DEX_PC_PTR]
138.endm
139
140/*
141 * Fetch the next instruction from xPC into wINST. Does not advance xPC.
142 */
143.macro FETCH_INST
144 ldrh wINST, [xPC]
145.endm
146
147/*
148 * Fetch the next instruction from the specified offset. Advances xPC
149 * to point to the next instruction. "_count" is in 16-bit code units.
150 *
151 * Because of the limited size of immediate constants on ARM, this is only
152 * suitable for small forward movements (i.e. don't try to implement "goto"
153 * with this).
154 *
155 * This must come AFTER anything that can throw an exception, or the
156 * exception catch may miss. (This also implies that it must come after
157 * EXPORT_PC.)
158 */
159.macro FETCH_ADVANCE_INST count
160 ldrh wINST, [xPC, #((\count)*2)]!
161.endm
162
163/*
164 * The operation performed here is similar to FETCH_ADVANCE_INST, except the
165 * src and dest registers are parameterized (not hard-wired to xPC and xINST).
166 */
167.macro PREFETCH_ADVANCE_INST dreg, sreg, count
168 ldrh \dreg, [\sreg, #((\count)*2)]!
169.endm
170
171/*
172 * Similar to FETCH_ADVANCE_INST, but does not update xPC. Used to load
173 * xINST ahead of possible exception point. Be sure to manually advance xPC
174 * later.
175 */
176.macro PREFETCH_INST count
177 ldrh wINST, [xPC, #((\count)*2)]
178.endm
179
180/* Advance xPC by some number of code units. */
181.macro ADVANCE count
182 add xPC, xPC, #((\count)*2)
183.endm
184
185/*
186 * Fetch the next instruction from an offset specified by _reg and advance xPC.
187 * xPC to point to the next instruction. "_reg" must specify the distance
188 * in bytes, *not* 16-bit code units, and may be a signed value. Must not set flags.
189 *
190 */
191.macro FETCH_ADVANCE_INST_RB reg
192 add xPC, xPC, \reg, sxtw
193 ldrh wINST, [xPC]
194.endm
195
196/*
197 * Fetch a half-word code unit from an offset past the current PC. The
198 * "_count" value is in 16-bit code units. Does not advance xPC.
199 *
200 * The "_S" variant works the same but treats the value as signed.
201 */
202.macro FETCH reg, count
203 ldrh \reg, [xPC, #((\count)*2)]
204.endm
205
206.macro FETCH_S reg, count
207 ldrsh \reg, [xPC, #((\count)*2)]
208.endm
209
210/*
211 * Fetch one byte from an offset past the current PC. Pass in the same
212 * "_count" as you would for FETCH, and an additional 0/1 indicating which
213 * byte of the halfword you want (lo/hi).
214 */
215.macro FETCH_B reg, count, byte
216 ldrb \reg, [xPC, #((\count)*2+(\byte))]
217.endm
218
219/*
220 * Put the instruction's opcode field into the specified register.
221 */
222.macro GET_INST_OPCODE reg
223 and \reg, xINST, #255
224.endm
225
226/*
227 * Put the prefetched instruction's opcode field into the specified register.
228 */
229.macro GET_PREFETCHED_OPCODE oreg, ireg
230 and \oreg, \ireg, #255
231.endm
232
233/*
234 * Begin executing the opcode in _reg. Clobbers reg
235 */
236
237.macro GOTO_OPCODE reg
238 add \reg, xIBASE, \reg, lsl #${handler_size_bits}
239 br \reg
240.endm
241.macro GOTO_OPCODE_BASE base,reg
242 add \reg, \base, \reg, lsl #${handler_size_bits}
243 br \reg
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, [xFP, \vreg, uxtw #2]
251.endm
252.macro SET_VREG reg, vreg
253 str \reg, [xFP, \vreg, uxtw #2]
254 str wzr, [xREFS, \vreg, uxtw #2]
255.endm
256.macro SET_VREG_OBJECT reg, vreg, tmpreg
257 str \reg, [xFP, \vreg, uxtw #2]
258 str \reg, [xREFS, \vreg, uxtw #2]
259.endm
260
261/*
262 * Get/set the 64-bit value from a Dalvik register.
263 * TUNING: can we do better here?
264 */
265.macro GET_VREG_WIDE reg, vreg
266 add ip2, xFP, \vreg, lsl #2
267 ldr \reg, [ip2]
268.endm
269.macro SET_VREG_WIDE reg, vreg
270 add ip2, xFP, \vreg, lsl #2
271 str \reg, [ip2]
272 add ip2, xREFS, \vreg, lsl #2
273 str xzr, [ip2]
274.endm
275
276/*
277 * Convert a virtual register index into an address.
278 */
279.macro VREG_INDEX_TO_ADDR reg, vreg
280 add \reg, xFP, \vreg, lsl #2 /* WARNING/FIXME: handle shadow frame vreg zero if store */
281.endm
282
283/*
284 * Refresh handler table.
285 */
286.macro REFRESH_IBASE
287 ldr xIBASE, [xSELF, #THREAD_CURRENT_IBASE_OFFSET]
288.endm