• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/*
35x86_64 ABI general notes:
36
37Caller save set:
38   rax, rdx, rcx, rsi, rdi, r8-r11, st(0)-st(7)
39Callee save set:
40   rbx, rbp, r12-r15
41Return regs:
42   32-bit in eax
43   64-bit in rax
44   fp on xmm0
45
46First 8 fp parameters came in xmm0-xmm7.
47First 6 non-fp parameters came in rdi, rsi, rdx, rcx, r8, r9.
48Other parameters passed on stack, pushed right-to-left.  On entry to target, first
49param is at 8(%esp).  Traditional entry code is:
50
51Stack must be 16-byte aligned to support SSE in native code.
52
53If we're not doing variable stack allocation (alloca), the frame pointer can be
54eliminated and all arg references adjusted to be esp relative.
55*/
56
57/*
58Mterp and x86_64 notes:
59
60Some key interpreter variables will be assigned to registers.
61
62  nick     reg   purpose
63  rPROFILE rbp   countdown register for jit profiling
64  rPC      r12   interpreted program counter, used for fetching instructions
65  rFP      r13   interpreted frame pointer, used for accessing locals and args
66  rINSTw   bx    first 16-bit code of current instruction
67  rINSTbl  bl    opcode portion of instruction word
68  rINSTbh  bh    high byte of inst word, usually contains src/tgt reg names
69  rIBASE   r14   base of instruction handler table
70  rREFS    r15   base of object references in shadow frame.
71
72Notes:
73   o High order 16 bits of ebx must be zero on entry to handler
74   o rPC, rFP, rINSTw/rINSTbl valid on handler entry and exit
75   o eax and ecx are scratch, rINSTw/ebx sometimes scratch
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/*
89 * Handle mac compiler specific
90 */
91#if defined(__APPLE__)
92    #define MACRO_LITERAL(value) $$(value)
93    #define FUNCTION_TYPE(name)
94    #define SIZE(start,end)
95    // Mac OS' symbols have an _ prefix.
96    #define SYMBOL(name) _ ## name
97#else
98    #define MACRO_LITERAL(value) $$value
99    #define FUNCTION_TYPE(name) .type name, @function
100    #define SIZE(start,end) .size start, .-end
101    #define SYMBOL(name) name
102#endif
103
104.macro PUSH _reg
105    pushq \_reg
106    .cfi_adjust_cfa_offset 8
107    .cfi_rel_offset \_reg, 0
108.endm
109
110.macro POP _reg
111    popq \_reg
112    .cfi_adjust_cfa_offset -8
113    .cfi_restore \_reg
114.endm
115
116/*
117 * Instead of holding a pointer to the shadow frame, we keep rFP at the base of the vregs.  So,
118 * to access other shadow frame fields, we need to use a backwards offset.  Define those here.
119 */
120#define OFF_FP(a) (a - SHADOWFRAME_VREGS_OFFSET)
121#define OFF_FP_NUMBER_OF_VREGS OFF_FP(SHADOWFRAME_NUMBER_OF_VREGS_OFFSET)
122#define OFF_FP_DEX_PC OFF_FP(SHADOWFRAME_DEX_PC_OFFSET)
123#define OFF_FP_LINK OFF_FP(SHADOWFRAME_LINK_OFFSET)
124#define OFF_FP_METHOD OFF_FP(SHADOWFRAME_METHOD_OFFSET)
125#define OFF_FP_RESULT_REGISTER OFF_FP(SHADOWFRAME_RESULT_REGISTER_OFFSET)
126#define OFF_FP_DEX_PC_PTR OFF_FP(SHADOWFRAME_DEX_PC_PTR_OFFSET)
127#define OFF_FP_CODE_ITEM OFF_FP(SHADOWFRAME_CODE_ITEM_OFFSET)
128#define OFF_FP_COUNTDOWN_OFFSET OFF_FP(SHADOWFRAME_HOTNESS_COUNTDOWN_OFFSET)
129#define OFF_FP_SHADOWFRAME (-SHADOWFRAME_VREGS_OFFSET)
130
131/* Frame size must be 16-byte aligned.
132 * Remember about 8 bytes for return address + 6 * 8 for spills.
133 */
134#define FRAME_SIZE     8
135
136/* Frame diagram while executing ExecuteMterpImpl, high to low addresses */
137#define IN_ARG3        %rcx
138#define IN_ARG2        %rdx
139#define IN_ARG1        %rsi
140#define IN_ARG0        %rdi
141/* Spill offsets relative to %esp */
142#define SELF_SPILL     (FRAME_SIZE -  8)
143/* Out Args  */
144#define OUT_ARG3       %rcx
145#define OUT_ARG2       %rdx
146#define OUT_ARG1       %rsi
147#define OUT_ARG0       %rdi
148#define OUT_32_ARG3    %ecx
149#define OUT_32_ARG2    %edx
150#define OUT_32_ARG1    %esi
151#define OUT_32_ARG0    %edi
152#define OUT_FP_ARG1    %xmm1
153#define OUT_FP_ARG0    %xmm0
154
155/* During bringup, we'll use the shadow frame model instead of rFP */
156/* single-purpose registers, given names for clarity */
157#define rSELF    SELF_SPILL(%rsp)
158#define rPC      %r12
159#define rFP      %r13
160#define rINST    %ebx
161#define rINSTq   %rbx
162#define rINSTw   %bx
163#define rINSTbh  %bh
164#define rINSTbl  %bl
165#define rIBASE   %r14
166#define rREFS    %r15
167#define rPROFILE %ebp
168
169#define MTERP_LOGGING 0
170
171/*
172 * "export" the PC to dex_pc field in the shadow frame, f/b/o future exception objects.  Must
173 * be done *before* something throws.
174 *
175 * It's okay to do this more than once.
176 *
177 * NOTE: the fast interpreter keeps track of dex pc as a direct pointer to the mapped
178 * dex byte codes.  However, the rest of the runtime expects dex pc to be an instruction
179 * offset into the code_items_[] array.  For effiency, we will "export" the
180 * current dex pc as a direct pointer using the EXPORT_PC macro, and rely on GetDexPC
181 * to convert to a dex pc when needed.
182 */
183.macro EXPORT_PC
184    movq    rPC, OFF_FP_DEX_PC_PTR(rFP)
185.endm
186
187/*
188 * Refresh handler table.
189 * IBase handles uses the caller save register so we must restore it after each call.
190 * Also it is used as a result of some 64-bit operations (like imul) and we should
191 * restore it in such cases also.
192 *
193 */
194.macro REFRESH_IBASE
195    movq    rSELF, rIBASE
196    movq    THREAD_CURRENT_IBASE_OFFSET(rIBASE), rIBASE
197.endm
198
199/*
200 * Refresh rINST.
201 * At enter to handler rINST does not contain the opcode number.
202 * However some utilities require the full value, so this macro
203 * restores the opcode number.
204 */
205.macro REFRESH_INST _opnum
206    movb    rINSTbl, rINSTbh
207    movb    $$\_opnum, rINSTbl
208.endm
209
210/*
211 * Fetch the next instruction from rPC into rINSTw.  Does not advance rPC.
212 */
213.macro FETCH_INST
214    movzwq  (rPC), rINSTq
215.endm
216
217/*
218 * Remove opcode from rINST, compute the address of handler and jump to it.
219 */
220.macro GOTO_NEXT
221    movzx   rINSTbl,%eax
222    movzbl  rINSTbh,rINST
223    shll    MACRO_LITERAL(${handler_size_bits}), %eax
224    addq    rIBASE, %rax
225    jmp     *%rax
226.endm
227
228/*
229 * Advance rPC by instruction count.
230 */
231.macro ADVANCE_PC _count
232    leaq    2*\_count(rPC), rPC
233.endm
234
235/*
236 * Advance rPC by instruction count, fetch instruction and jump to handler.
237 */
238.macro ADVANCE_PC_FETCH_AND_GOTO_NEXT _count
239    ADVANCE_PC \_count
240    FETCH_INST
241    GOTO_NEXT
242.endm
243
244/*
245 * Get/set the 32-bit value from a Dalvik register.
246 */
247#define VREG_ADDRESS(_vreg) (rFP,_vreg,4)
248#define VREG_REF_ADDRESS(_vreg) (rREFS,_vreg,4)
249
250.macro GET_VREG _reg _vreg
251    movl    (rFP,\_vreg,4), \_reg
252.endm
253
254/* Read wide value. */
255.macro GET_WIDE_VREG _reg _vreg
256    movq    (rFP,\_vreg,4), \_reg
257.endm
258
259.macro SET_VREG _reg _vreg
260    movl    \_reg, (rFP,\_vreg,4)
261    movl    MACRO_LITERAL(0), (rREFS,\_vreg,4)
262.endm
263
264/* Write wide value. reg is clobbered. */
265.macro SET_WIDE_VREG _reg _vreg
266    movq    \_reg, (rFP,\_vreg,4)
267    xorq    \_reg, \_reg
268    movq    \_reg, (rREFS,\_vreg,4)
269.endm
270
271.macro SET_VREG_OBJECT _reg _vreg
272    movl    \_reg, (rFP,\_vreg,4)
273    movl    \_reg, (rREFS,\_vreg,4)
274.endm
275
276.macro GET_VREG_HIGH _reg _vreg
277    movl    4(rFP,\_vreg,4), \_reg
278.endm
279
280.macro SET_VREG_HIGH _reg _vreg
281    movl    \_reg, 4(rFP,\_vreg,4)
282    movl    MACRO_LITERAL(0), 4(rREFS,\_vreg,4)
283.endm
284
285.macro CLEAR_REF _vreg
286    movl    MACRO_LITERAL(0),  (rREFS,\_vreg,4)
287.endm
288
289.macro CLEAR_WIDE_REF _vreg
290    movl    MACRO_LITERAL(0),  (rREFS,\_vreg,4)
291    movl    MACRO_LITERAL(0), 4(rREFS,\_vreg,4)
292.endm
293