blob: 7101ba972cf9fbe3ccf18378d140ee726e59d260 [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
Bill Buzbeefd522f92016-02-11 22:37:42 +000090#define MTERP_PROFILE_BRANCHES 1
91#define MTERP_LOGGING 0
92
Bill Buzbee3b0b4b92016-02-02 13:45:36 +000093/* During bringup, we'll use the shadow frame model instead of xFP */
94/* single-purpose registers, given names for clarity */
Hiroshi Yamauchi961ea9f2016-04-01 12:02:58 -070095#define xPC x20
96#define xFP x21
97#define xSELF x22
98#define xINST x23
99#define wINST w23
100#define xIBASE x24
101#define xREFS x25
102#define ip x16
103#define ip2 x17
Bill Buzbee3b0b4b92016-02-02 13:45:36 +0000104
105/*
106 * Instead of holding a pointer to the shadow frame, we keep xFP at the base of the vregs. So,
107 * to access other shadow frame fields, we need to use a backwards offset. Define those here.
108 */
109#define OFF_FP(a) (a - SHADOWFRAME_VREGS_OFFSET)
110#define OFF_FP_NUMBER_OF_VREGS OFF_FP(SHADOWFRAME_NUMBER_OF_VREGS_OFFSET)
111#define OFF_FP_DEX_PC OFF_FP(SHADOWFRAME_DEX_PC_OFFSET)
112#define OFF_FP_LINK OFF_FP(SHADOWFRAME_LINK_OFFSET)
113#define OFF_FP_METHOD OFF_FP(SHADOWFRAME_METHOD_OFFSET)
114#define OFF_FP_RESULT_REGISTER OFF_FP(SHADOWFRAME_RESULT_REGISTER_OFFSET)
115#define OFF_FP_DEX_PC_PTR OFF_FP(SHADOWFRAME_DEX_PC_PTR_OFFSET)
116#define OFF_FP_CODE_ITEM OFF_FP(SHADOWFRAME_CODE_ITEM_OFFSET)
Hiroshi Yamauchi961ea9f2016-04-01 12:02:58 -0700117#define OFF_FP_SHADOWFRAME (-SHADOWFRAME_VREGS_OFFSET)
Bill Buzbee3b0b4b92016-02-02 13:45:36 +0000118
119/*
Bill Buzbee3b0b4b92016-02-02 13:45:36 +0000120 * "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 xPC, [xFP, #OFF_FP_DEX_PC_PTR]
133.endm
134
135/*
136 * Fetch the next instruction from xPC into wINST. Does not advance xPC.
137 */
138.macro FETCH_INST
139 ldrh wINST, [xPC]
140.endm
141
142/*
143 * Fetch the next instruction from the specified offset. Advances xPC
144 * to point to the next instruction. "_count" is in 16-bit code units.
145 *
146 * Because of the limited size of immediate constants on ARM, this is only
147 * suitable for small forward movements (i.e. don't try to implement "goto"
148 * with this).
149 *
150 * This must come AFTER anything that can throw an exception, or the
151 * exception catch may miss. (This also implies that it must come after
152 * EXPORT_PC.)
153 */
154.macro FETCH_ADVANCE_INST count
155 ldrh wINST, [xPC, #((\count)*2)]!
156.endm
157
158/*
159 * The operation performed here is similar to FETCH_ADVANCE_INST, except the
160 * src and dest registers are parameterized (not hard-wired to xPC and xINST).
161 */
162.macro PREFETCH_ADVANCE_INST dreg, sreg, count
163 ldrh \dreg, [\sreg, #((\count)*2)]!
164.endm
165
166/*
167 * Similar to FETCH_ADVANCE_INST, but does not update xPC. Used to load
168 * xINST ahead of possible exception point. Be sure to manually advance xPC
169 * later.
170 */
171.macro PREFETCH_INST count
172 ldrh wINST, [xPC, #((\count)*2)]
173.endm
174
175/* Advance xPC by some number of code units. */
176.macro ADVANCE count
177 add xPC, xPC, #((\count)*2)
178.endm
179
180/*
181 * Fetch the next instruction from an offset specified by _reg and advance xPC.
182 * xPC to point to the next instruction. "_reg" must specify the distance
183 * in bytes, *not* 16-bit code units, and may be a signed value. Must not set flags.
184 *
185 */
186.macro FETCH_ADVANCE_INST_RB reg
187 add xPC, xPC, \reg, sxtw
188 ldrh wINST, [xPC]
189.endm
190
191/*
192 * Fetch a half-word code unit from an offset past the current PC. The
193 * "_count" value is in 16-bit code units. Does not advance xPC.
194 *
195 * The "_S" variant works the same but treats the value as signed.
196 */
197.macro FETCH reg, count
198 ldrh \reg, [xPC, #((\count)*2)]
199.endm
200
201.macro FETCH_S reg, count
202 ldrsh \reg, [xPC, #((\count)*2)]
203.endm
204
205/*
206 * Fetch one byte from an offset past the current PC. Pass in the same
207 * "_count" as you would for FETCH, and an additional 0/1 indicating which
208 * byte of the halfword you want (lo/hi).
209 */
210.macro FETCH_B reg, count, byte
211 ldrb \reg, [xPC, #((\count)*2+(\byte))]
212.endm
213
214/*
215 * Put the instruction's opcode field into the specified register.
216 */
217.macro GET_INST_OPCODE reg
218 and \reg, xINST, #255
219.endm
220
221/*
222 * Put the prefetched instruction's opcode field into the specified register.
223 */
224.macro GET_PREFETCHED_OPCODE oreg, ireg
225 and \oreg, \ireg, #255
226.endm
227
228/*
229 * Begin executing the opcode in _reg. Clobbers reg
230 */
231
232.macro GOTO_OPCODE reg
233 add \reg, xIBASE, \reg, lsl #${handler_size_bits}
234 br \reg
235.endm
236.macro GOTO_OPCODE_BASE base,reg
237 add \reg, \base, \reg, lsl #${handler_size_bits}
238 br \reg
239.endm
240
241/*
242 * Get/set the 32-bit value from a Dalvik register.
243 */
244.macro GET_VREG reg, vreg
245 ldr \reg, [xFP, \vreg, uxtw #2]
246.endm
247.macro SET_VREG reg, vreg
248 str \reg, [xFP, \vreg, uxtw #2]
249 str wzr, [xREFS, \vreg, uxtw #2]
250.endm
251.macro SET_VREG_OBJECT reg, vreg, tmpreg
252 str \reg, [xFP, \vreg, uxtw #2]
253 str \reg, [xREFS, \vreg, uxtw #2]
254.endm
255
256/*
257 * Get/set the 64-bit value from a Dalvik register.
258 * TUNING: can we do better here?
259 */
260.macro GET_VREG_WIDE reg, vreg
261 add ip2, xFP, \vreg, lsl #2
262 ldr \reg, [ip2]
263.endm
264.macro SET_VREG_WIDE reg, vreg
265 add ip2, xFP, \vreg, lsl #2
266 str \reg, [ip2]
267 add ip2, xREFS, \vreg, lsl #2
268 str xzr, [ip2]
269.endm
270
271/*
272 * Convert a virtual register index into an address.
273 */
274.macro VREG_INDEX_TO_ADDR reg, vreg
buzbeeace690f2016-03-11 09:51:11 -0800275 add \reg, xFP, \vreg, lsl #2 /* WARNING: handle shadow frame vreg zero if store */
Bill Buzbee3b0b4b92016-02-02 13:45:36 +0000276.endm
277
278/*
279 * Refresh handler table.
280 */
281.macro REFRESH_IBASE
282 ldr xIBASE, [xSELF, #THREAD_CURRENT_IBASE_OFFSET]
283.endm