blob: 622abc83eec363dfea1656d3e729bc899a02ae20 [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
buzbeea0a16102016-02-03 15:23:56 -080090#define MTERP_PROFILE_BRANCHES 1
91
Bill Buzbee3b0b4b92016-02-02 13:45:36 +000092/* During bringup, we'll use the shadow frame model instead of xFP */
93/* single-purpose registers, given names for clarity */
94#define xPC x20
95#define xFP x21
96#define xSELF x22
97#define xINST x23
98#define wINST w23
99#define xIBASE x24
100#define xREFS x25
101#define ip x16
102#define ip2 x17
103
104/*
105 * Instead of holding a pointer to the shadow frame, we keep xFP at the base of the vregs. So,
106 * to access other shadow frame fields, we need to use a backwards offset. Define those here.
107 */
108#define OFF_FP(a) (a - SHADOWFRAME_VREGS_OFFSET)
109#define OFF_FP_NUMBER_OF_VREGS OFF_FP(SHADOWFRAME_NUMBER_OF_VREGS_OFFSET)
110#define OFF_FP_DEX_PC OFF_FP(SHADOWFRAME_DEX_PC_OFFSET)
111#define OFF_FP_LINK OFF_FP(SHADOWFRAME_LINK_OFFSET)
112#define OFF_FP_METHOD OFF_FP(SHADOWFRAME_METHOD_OFFSET)
113#define OFF_FP_RESULT_REGISTER OFF_FP(SHADOWFRAME_RESULT_REGISTER_OFFSET)
114#define OFF_FP_DEX_PC_PTR OFF_FP(SHADOWFRAME_DEX_PC_PTR_OFFSET)
115#define OFF_FP_CODE_ITEM OFF_FP(SHADOWFRAME_CODE_ITEM_OFFSET)
116#define OFF_FP_SHADOWFRAME (-SHADOWFRAME_VREGS_OFFSET)
117
118/*
119 *
120 * The reference interpreter performs explicit suspect checks, which is somewhat wasteful.
121 * Dalvik's interpreter folded suspend checks into the jump table mechanism, and eventually
122 * mterp should do so as well.
123 */
124#define MTERP_SUSPEND 0
125
126/*
127 * "export" the PC to dex_pc field in the shadow frame, f/b/o future exception objects. Must
128 * be done *before* something throws.
129 *
130 * It's okay to do this more than once.
131 *
132 * NOTE: the fast interpreter keeps track of dex pc as a direct pointer to the mapped
133 * dex byte codes. However, the rest of the runtime expects dex pc to be an instruction
134 * offset into the code_items_[] array. For effiency, we will "export" the
135 * current dex pc as a direct pointer using the EXPORT_PC macro, and rely on GetDexPC
136 * to convert to a dex pc when needed.
137 */
138.macro EXPORT_PC
139 str xPC, [xFP, #OFF_FP_DEX_PC_PTR]
140.endm
141
142/*
143 * Fetch the next instruction from xPC into wINST. Does not advance xPC.
144 */
145.macro FETCH_INST
146 ldrh wINST, [xPC]
147.endm
148
149/*
150 * Fetch the next instruction from the specified offset. Advances xPC
151 * to point to the next instruction. "_count" is in 16-bit code units.
152 *
153 * Because of the limited size of immediate constants on ARM, this is only
154 * suitable for small forward movements (i.e. don't try to implement "goto"
155 * with this).
156 *
157 * This must come AFTER anything that can throw an exception, or the
158 * exception catch may miss. (This also implies that it must come after
159 * EXPORT_PC.)
160 */
161.macro FETCH_ADVANCE_INST count
162 ldrh wINST, [xPC, #((\count)*2)]!
163.endm
164
165/*
166 * The operation performed here is similar to FETCH_ADVANCE_INST, except the
167 * src and dest registers are parameterized (not hard-wired to xPC and xINST).
168 */
169.macro PREFETCH_ADVANCE_INST dreg, sreg, count
170 ldrh \dreg, [\sreg, #((\count)*2)]!
171.endm
172
173/*
174 * Similar to FETCH_ADVANCE_INST, but does not update xPC. Used to load
175 * xINST ahead of possible exception point. Be sure to manually advance xPC
176 * later.
177 */
178.macro PREFETCH_INST count
179 ldrh wINST, [xPC, #((\count)*2)]
180.endm
181
182/* Advance xPC by some number of code units. */
183.macro ADVANCE count
184 add xPC, xPC, #((\count)*2)
185.endm
186
187/*
188 * Fetch the next instruction from an offset specified by _reg and advance xPC.
189 * xPC to point to the next instruction. "_reg" must specify the distance
190 * in bytes, *not* 16-bit code units, and may be a signed value. Must not set flags.
191 *
192 */
193.macro FETCH_ADVANCE_INST_RB reg
194 add xPC, xPC, \reg, sxtw
195 ldrh wINST, [xPC]
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 xPC.
201 *
202 * The "_S" variant works the same but treats the value as signed.
203 */
204.macro FETCH reg, count
205 ldrh \reg, [xPC, #((\count)*2)]
206.endm
207
208.macro FETCH_S reg, count
209 ldrsh \reg, [xPC, #((\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, [xPC, #((\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, xINST, #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. Clobbers reg
237 */
238
239.macro GOTO_OPCODE reg
240 add \reg, xIBASE, \reg, lsl #${handler_size_bits}
241 br \reg
242.endm
243.macro GOTO_OPCODE_BASE base,reg
244 add \reg, \base, \reg, lsl #${handler_size_bits}
245 br \reg
246.endm
247
248/*
249 * Get/set the 32-bit value from a Dalvik register.
250 */
251.macro GET_VREG reg, vreg
252 ldr \reg, [xFP, \vreg, uxtw #2]
253.endm
254.macro SET_VREG reg, vreg
255 str \reg, [xFP, \vreg, uxtw #2]
256 str wzr, [xREFS, \vreg, uxtw #2]
257.endm
258.macro SET_VREG_OBJECT reg, vreg, tmpreg
259 str \reg, [xFP, \vreg, uxtw #2]
260 str \reg, [xREFS, \vreg, uxtw #2]
261.endm
262
263/*
264 * Get/set the 64-bit value from a Dalvik register.
265 * TUNING: can we do better here?
266 */
267.macro GET_VREG_WIDE reg, vreg
268 add ip2, xFP, \vreg, lsl #2
269 ldr \reg, [ip2]
270.endm
271.macro SET_VREG_WIDE reg, vreg
272 add ip2, xFP, \vreg, lsl #2
273 str \reg, [ip2]
274 add ip2, xREFS, \vreg, lsl #2
275 str xzr, [ip2]
276.endm
277
278/*
279 * Convert a virtual register index into an address.
280 */
281.macro VREG_INDEX_TO_ADDR reg, vreg
282 add \reg, xFP, \vreg, lsl #2 /* WARNING/FIXME: handle shadow frame vreg zero if store */
283.endm
284
285/*
286 * Refresh handler table.
287 */
288.macro REFRESH_IBASE
289 ldr xIBASE, [xSELF, #THREAD_CURRENT_IBASE_OFFSET]
290.endm