1/*
2 * Copyright (C) 2014 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#include "asm_support_mips64.S"
18
19#include "arch/quick_alloc_entrypoints.S"
20
21    .set noreorder
22    .balign 16
23
24    /* Deliver the given exception */
25    .extern artDeliverExceptionFromCode
26    /* Deliver an exception pending on a thread */
27    .extern artDeliverPendingExceptionFromCode
28
29    /*
30     * Macro that sets up $gp and stores the previous $gp value to $t8.
31     * This macro modifies v1 and t8.
32     */
33.macro SETUP_GP
34    move $v1, $ra
35    bal 1f
36    nop
371:
38    .cpsetup $ra, $t8, 1b
39    move $ra, $v1
40.endm
41
42    /*
43     * Macro that sets up the callee save frame to conform with
44     * Runtime::CreateCalleeSaveMethod(kSaveAll)
45     * callee-save: padding + $f24-$f31 + $s0-$s7 + $gp + $ra + $s8 = 19 total + 1x8 bytes padding
46     */
47.macro SETUP_SAVE_ALL_CALLEE_SAVE_FRAME
48    daddiu $sp, $sp, -160
49    .cfi_adjust_cfa_offset 160
50
51     // Ugly compile-time check, but we only have the preprocessor.
52#if (FRAME_SIZE_SAVE_ALL_CALLEE_SAVE != 160)
53#error "SAVE_ALL_CALLEE_SAVE_FRAME(MIPS64) size not as expected."
54#endif
55
56    sd     $ra, 152($sp)
57    .cfi_rel_offset 31, 152
58    sd     $s8, 144($sp)
59    .cfi_rel_offset 30, 144
60    sd     $t8, 136($sp)           # t8 holds caller's gp, now save it to the stack.
61    .cfi_rel_offset 28, 136        # Value from gp is pushed, so set the cfi offset accordingly.
62    sd     $s7, 128($sp)
63    .cfi_rel_offset 23, 128
64    sd     $s6, 120($sp)
65    .cfi_rel_offset 22, 120
66    sd     $s5, 112($sp)
67    .cfi_rel_offset 21, 112
68    sd     $s4, 104($sp)
69    .cfi_rel_offset 20, 104
70    sd     $s3,  96($sp)
71    .cfi_rel_offset 19, 96
72    sd     $s2,  88($sp)
73    .cfi_rel_offset 18, 88
74    sd     $s1,  80($sp)
75    .cfi_rel_offset 17, 80
76    sd     $s0,  72($sp)
77    .cfi_rel_offset 16, 72
78
79    // FP callee-saves
80    s.d    $f31, 64($sp)
81    s.d    $f30, 56($sp)
82    s.d    $f29, 48($sp)
83    s.d    $f28, 40($sp)
84    s.d    $f27, 32($sp)
85    s.d    $f26, 24($sp)
86    s.d    $f25, 16($sp)
87    s.d    $f24,  8($sp)
88
89    # load appropriate callee-save-method
90    ld      $t1, %got(_ZN3art7Runtime9instance_E)($gp)
91    ld      $t1, 0($t1)
92    ld      $t1, RUNTIME_SAVE_ALL_CALLEE_SAVE_FRAME_OFFSET($t1)
93    sd      $t1, 0($sp)                                # Place ArtMethod* at bottom of stack.
94    sd      $sp, THREAD_TOP_QUICK_FRAME_OFFSET(rSELF)  # Place sp in Thread::Current()->top_quick_frame.
95.endm
96
97    /*
98     * Macro that sets up the callee save frame to conform with
99     * Runtime::CreateCalleeSaveMethod(kRefsOnly). Restoration assumes
100     * non-moving GC.
101     * Does not include rSUSPEND or rSELF
102     * callee-save: padding + $s2-$s7 + $gp + $ra + $s8 = 9 total + 1x8 bytes padding
103     */
104.macro SETUP_REFS_ONLY_CALLEE_SAVE_FRAME
105    daddiu $sp, $sp, -80
106    .cfi_adjust_cfa_offset 80
107
108    // Ugly compile-time check, but we only have the preprocessor.
109#if (FRAME_SIZE_REFS_ONLY_CALLEE_SAVE != 80)
110#error "REFS_ONLY_CALLEE_SAVE_FRAME(MIPS64) size not as expected."
111#endif
112
113    sd     $ra, 72($sp)
114    .cfi_rel_offset 31, 72
115    sd     $s8, 64($sp)
116    .cfi_rel_offset 30, 64
117    sd     $t8, 56($sp)            # t8 holds caller's gp, now save it to the stack.
118    .cfi_rel_offset 28, 56         # Value from gp is pushed, so set the cfi offset accordingly.
119    sd     $s7, 48($sp)
120    .cfi_rel_offset 23, 48
121    sd     $s6, 40($sp)
122    .cfi_rel_offset 22, 40
123    sd     $s5, 32($sp)
124    .cfi_rel_offset 21, 32
125    sd     $s4, 24($sp)
126    .cfi_rel_offset 20, 24
127    sd     $s3, 16($sp)
128    .cfi_rel_offset 19, 16
129    sd     $s2, 8($sp)
130    .cfi_rel_offset 18, 8
131    # load appropriate callee-save-method
132    ld      $t1, %got(_ZN3art7Runtime9instance_E)($gp)
133    ld      $t1, 0($t1)
134    ld      $t1, RUNTIME_REFS_ONLY_CALLEE_SAVE_FRAME_OFFSET($t1)
135    sd      $t1, 0($sp)                                # Place Method* at bottom of stack.
136    sd      $sp, THREAD_TOP_QUICK_FRAME_OFFSET(rSELF)  # Place sp in Thread::Current()->top_quick_frame.
137.endm
138
139.macro RESTORE_REFS_ONLY_CALLEE_SAVE_FRAME
140    ld     $ra, 72($sp)
141    .cfi_restore 31
142    ld     $s8, 64($sp)
143    .cfi_restore 30
144    ld     $t8, 56($sp)            # Restore gp back to it's temp storage.
145    .cfi_restore 28
146    ld     $s7, 48($sp)
147    .cfi_restore 23
148    ld     $s6, 40($sp)
149    .cfi_restore 22
150    ld     $s5, 32($sp)
151    .cfi_restore 21
152    ld     $s4, 24($sp)
153    .cfi_restore 20
154    ld     $s3, 16($sp)
155    .cfi_restore 19
156    ld     $s2, 8($sp)
157    .cfi_restore 18
158    daddiu $sp, $sp, 80
159    .cfi_adjust_cfa_offset -80
160    .cpreturn
161.endm
162
163.macro RESTORE_REFS_ONLY_CALLEE_SAVE_FRAME_AND_RETURN
164    ld     $ra, 72($sp)
165    .cfi_restore 31
166    ld     $s8, 64($sp)
167    .cfi_restore 30
168    ld     $t8, 56($sp)            # Restore gp back to it's temp storage.
169    .cfi_restore 28
170    ld     $s7, 48($sp)
171    .cfi_restore 23
172    ld     $s6, 40($sp)
173    .cfi_restore 22
174    ld     $s5, 32($sp)
175    .cfi_restore 21
176    ld     $s4, 24($sp)
177    .cfi_restore 20
178    ld     $s3, 16($sp)
179    .cfi_restore 19
180    ld     $s2, 8($sp)
181    .cfi_restore 18
182    .cpreturn
183    jalr   $zero, $ra
184    daddiu $sp, $sp, 80
185    .cfi_adjust_cfa_offset -80
186.endm
187
188// This assumes the top part of these stack frame types are identical.
189#define REFS_AND_ARGS_MINUS_REFS_SIZE (FRAME_SIZE_REFS_AND_ARGS_CALLEE_SAVE - FRAME_SIZE_REFS_ONLY_CALLEE_SAVE)
190
191.macro SETUP_REFS_AND_ARGS_CALLEE_SAVE_FRAME_INTERNAL
192    daddiu  $sp, $sp, -208
193    .cfi_adjust_cfa_offset 208
194
195    // Ugly compile-time check, but we only have the preprocessor.
196#if (FRAME_SIZE_REFS_AND_ARGS_CALLEE_SAVE != 208)
197#error "REFS_AND_ARGS_CALLEE_SAVE_FRAME(MIPS64) size not as expected."
198#endif
199
200    sd     $ra, 200($sp)           # = kQuickCalleeSaveFrame_RefAndArgs_LrOffset
201    .cfi_rel_offset 31, 200
202    sd     $s8, 192($sp)
203    .cfi_rel_offset 30, 192
204    sd     $t8, 184($sp)           # t8 holds caller's gp, now save it to the stack.
205    .cfi_rel_offset 28, 184        # Value from gp is pushed, so set the cfi offset accordingly.
206    sd     $s7, 176($sp)
207    .cfi_rel_offset 23, 176
208    sd     $s6, 168($sp)
209    .cfi_rel_offset 22, 168
210    sd     $s5, 160($sp)
211    .cfi_rel_offset 21, 160
212    sd     $s4, 152($sp)
213    .cfi_rel_offset 20, 152
214    sd     $s3, 144($sp)
215    .cfi_rel_offset 19, 144
216    sd     $s2, 136($sp)
217    .cfi_rel_offset 18, 136
218
219    sd     $a7, 128($sp)
220    .cfi_rel_offset 11, 128
221    sd     $a6, 120($sp)
222    .cfi_rel_offset 10, 120
223    sd     $a5, 112($sp)
224    .cfi_rel_offset 9, 112
225    sd     $a4, 104($sp)
226    .cfi_rel_offset 8, 104
227    sd     $a3,  96($sp)
228    .cfi_rel_offset 7, 96
229    sd     $a2,  88($sp)
230    .cfi_rel_offset 6, 88
231    sd     $a1,  80($sp)           # = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset
232    .cfi_rel_offset 5, 80
233
234    s.d    $f19, 72($sp)
235    s.d    $f18, 64($sp)
236    s.d    $f17, 56($sp)
237    s.d    $f16, 48($sp)
238    s.d    $f15, 40($sp)
239    s.d    $f14, 32($sp)
240    s.d    $f13, 24($sp)           # = kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset
241    s.d    $f12, 16($sp)           # This isn't necessary to store.
242    # 1x8 bytes padding + Method*
243.endm
244
245    /*
246     * Macro that sets up the callee save frame to conform with
247     * Runtime::CreateCalleeSaveMethod(kRefsAndArgs). Restoration assumes
248     * non-moving GC.
249     * callee-save: padding + $f12-$f19 + $a1-$a7 + $s2-$s7 + $gp + $ra + $s8 = 24 total + 1 words padding + Method*
250     */
251.macro SETUP_REFS_AND_ARGS_CALLEE_SAVE_FRAME
252    SETUP_REFS_AND_ARGS_CALLEE_SAVE_FRAME_INTERNAL
253    # load appropriate callee-save-method
254    ld      $t1, %got(_ZN3art7Runtime9instance_E)($gp)
255    ld      $t1, 0($t1)
256    ld      $t1, RUNTIME_REFS_AND_ARGS_CALLEE_SAVE_FRAME_OFFSET($t1)
257    sd      $t1, 0($sp)                                # Place Method* at bottom of stack.
258    sd      $sp, THREAD_TOP_QUICK_FRAME_OFFSET(rSELF)  # Place sp in Thread::Current()->top_quick_frame.
259.endm
260
261.macro SETUP_REFS_AND_ARGS_CALLEE_SAVE_FRAME_WITH_METHOD_IN_A0
262    SETUP_REFS_AND_ARGS_CALLEE_SAVE_FRAME_INTERNAL
263    sd      $a0, 0($sp)                                # Place Method* at bottom of stack.
264    sd      $sp, THREAD_TOP_QUICK_FRAME_OFFSET(rSELF)  # Place sp in Thread::Current()->top_quick_frame.
265.endm
266
267.macro RESTORE_REFS_AND_ARGS_CALLEE_SAVE_FRAME
268    ld     $ra, 200($sp)
269    .cfi_restore 31
270    ld     $s8, 192($sp)
271    .cfi_restore 30
272    ld     $t8, 184($sp)           # Restore gp back to it's temp storage.
273    .cfi_restore 28
274    ld     $s7, 176($sp)
275    .cfi_restore 23
276    ld     $s6, 168($sp)
277    .cfi_restore 22
278    ld     $s5, 160($sp)
279    .cfi_restore 21
280    ld     $s4, 152($sp)
281    .cfi_restore 20
282    ld     $s3, 144($sp)
283    .cfi_restore 19
284    ld     $s2, 136($sp)
285    .cfi_restore 18
286
287    ld     $a7, 128($sp)
288    .cfi_restore 11
289    ld     $a6, 120($sp)
290    .cfi_restore 10
291    ld     $a5, 112($sp)
292    .cfi_restore 9
293    ld     $a4, 104($sp)
294    .cfi_restore 8
295    ld     $a3,  96($sp)
296    .cfi_restore 7
297    ld     $a2,  88($sp)
298    .cfi_restore 6
299    ld     $a1,  80($sp)
300    .cfi_restore 5
301
302    l.d    $f19, 72($sp)
303    l.d    $f18, 64($sp)
304    l.d    $f17, 56($sp)
305    l.d    $f16, 48($sp)
306    l.d    $f15, 40($sp)
307    l.d    $f14, 32($sp)
308    l.d    $f13, 24($sp)
309    l.d    $f12, 16($sp)
310
311    .cpreturn
312    daddiu $sp, $sp, 208
313    .cfi_adjust_cfa_offset -208
314.endm
315
316    /*
317     * Macro that set calls through to artDeliverPendingExceptionFromCode,
318     * where the pending
319     * exception is Thread::Current()->exception_
320     */
321.macro DELIVER_PENDING_EXCEPTION
322    SETUP_GP
323    SETUP_SAVE_ALL_CALLEE_SAVE_FRAME     # save callee saves for throw
324    dla     $t9, artDeliverPendingExceptionFromCode
325    jalr    $zero, $t9                   # artDeliverPendingExceptionFromCode(Thread*)
326    move    $a0, rSELF                   # pass Thread::Current
327.endm
328
329.macro RETURN_IF_NO_EXCEPTION
330    ld     $t0, THREAD_EXCEPTION_OFFSET(rSELF) # load Thread::Current()->exception_
331    RESTORE_REFS_ONLY_CALLEE_SAVE_FRAME
332    bne    $t0, $zero, 1f                      # success if no exception is pending
333    nop
334    jalr   $zero, $ra
335    nop
3361:
337    DELIVER_PENDING_EXCEPTION
338.endm
339
340.macro RETURN_IF_ZERO
341    RESTORE_REFS_ONLY_CALLEE_SAVE_FRAME
342    bne    $v0, $zero, 1f                # success?
343    nop
344    jalr   $zero, $ra                    # return on success
345    nop
3461:
347    DELIVER_PENDING_EXCEPTION
348.endm
349
350.macro RETURN_IF_RESULT_IS_NON_ZERO_OR_DELIVER
351    RESTORE_REFS_ONLY_CALLEE_SAVE_FRAME
352    beq    $v0, $zero, 1f                # success?
353    nop
354    jalr   $zero, $ra                    # return on success
355    nop
3561:
357    DELIVER_PENDING_EXCEPTION
358.endm
359
360    /*
361     * On stack replacement stub.
362     * On entry:
363     *   a0 = stack to copy
364     *   a1 = size of stack
365     *   a2 = pc to call
366     *   a3 = JValue* result
367     *   a4 = shorty
368     *   a5 = thread
369     */
370ENTRY art_quick_osr_stub
371    move   $t0, $sp               # save stack pointer
372    daddiu $t1, $sp, -112         # reserve stack space
373    dsrl   $t1, $t1, 4            # enforce 16 byte stack alignment
374    dsll   $sp, $t1, 4            # update stack pointer
375
376    // Save callee general purpose registers, SP, T8(GP), RA, A3, and A4 (8x14 bytes)
377    sd     $ra, 104($sp)
378    .cfi_rel_offset 31, 104
379    sd     $s8, 96($sp)
380    .cfi_rel_offset 30, 96
381    sd     $t0, 88($sp)           # save original stack pointer stored in t0
382    .cfi_rel_offset 29, 88
383    sd     $t8, 80($sp)           # t8 holds caller's gp, now save it to the stack.
384    .cfi_rel_offset 28, 80        # Value from gp is pushed, so set the cfi offset accordingly.
385    sd     $s7, 72($sp)
386    .cfi_rel_offset 23, 72
387    sd     $s6, 64($sp)
388    .cfi_rel_offset 22, 64
389    sd     $s5, 56($sp)
390    .cfi_rel_offset 21, 56
391    sd     $s4, 48($sp)
392    .cfi_rel_offset 20, 48
393    sd     $s3, 40($sp)
394    .cfi_rel_offset 19, 40
395    sd     $s2, 32($sp)
396    .cfi_rel_offset 18, 32
397    sd     $s1, 24($sp)
398    .cfi_rel_offset 17, 24
399    sd     $s0, 16($sp)
400    .cfi_rel_offset 16, 16
401    sd     $a4, 8($sp)
402    .cfi_rel_offset 8, 8
403    sd     $a3, 0($sp)
404    .cfi_rel_offset 7, 0
405    move   rSELF, $a5                      # Save managed thread pointer into rSELF
406
407    daddiu $sp, $sp, -16
408    jal    .Losr_entry
409    sd     $zero, 0($sp)                   # Store null for ArtMethod* at bottom of frame
410    daddiu $sp, $sp, 16
411
412    // Restore return value address and shorty address
413    ld     $a4, 8($sp)                     # shorty address
414    .cfi_restore 8
415    ld     $a3, 0($sp)                     # result value address
416    .cfi_restore 7
417
418    lbu    $t1, 0($a4)                     # load return type
419    li     $t2, 'D'                        # put char 'D' into t2
420    beq    $t1, $t2, .Losr_fp_result       # branch if result type char == 'D'
421    li     $t2, 'F'                        # put char 'F' into t2
422    beq    $t1, $t2, .Losr_fp_result       # branch if result type char == 'F'
423    nop
424    b      .Losr_exit
425    dsrl   $v1, $v0, 32                    # put high half of result in v1
426.Losr_fp_result:
427    mfc1   $v0, $f0
428    mfhc1  $v1, $f0                        # put high half of FP result in v1
429.Losr_exit:
430    sw     $v0, 0($a3)                     # store low half of result
431    sw     $v1, 4($a3)                     # store high half of result
432
433    // Restore callee registers
434    ld     $ra, 104($sp)
435    .cfi_restore 31
436    ld     $s8, 96($sp)
437    .cfi_restore 30
438    ld     $t0, 88($sp)                    # save SP into t0 for now
439    .cfi_restore 29
440    ld     $t8, 80($sp)                    # Restore gp back to it's temp storage.
441    .cfi_restore 28
442    ld     $s7, 72($sp)
443    .cfi_restore 23
444    ld     $s6, 64($sp)
445    .cfi_restore 22
446    ld     $s5, 56($sp)
447    .cfi_restore 21
448    ld     $s4, 48($sp)
449    .cfi_restore 20
450    ld     $s3, 40($sp)
451    .cfi_restore 19
452    ld     $s2, 32($sp)
453    .cfi_restore 18
454    ld     $s1, 24($sp)
455    .cfi_restore 17
456    ld     $s0, 16($sp)
457    .cfi_restore 16
458    jalr   $zero, $ra
459    move   $sp, $t0
460
461.Losr_entry:
462    dsubu  $sp, $sp, $a1                   # Reserve space for callee stack
463    daddiu $a1, $a1, -8
464    daddu  $t0, $a1, $sp
465    sw     $ra, 0($t0)                     # Store low half of RA per compiler ABI
466    dsrl   $t1, $ra, 32
467    sw     $t1, 4($t0)                     # Store high half of RA per compiler ABI
468
469    // Copy arguments into callee stack
470    // Use simple copy routine for now.
471    // 4 bytes per slot.
472    // a0 = source address
473    // a1 = args length in bytes (does not include 8 bytes for RA)
474    // sp = destination address
475    beqz   $a1, .Losr_loop_exit
476    daddiu $a1, $a1, -4
477    daddu  $t1, $a0, $a1
478    daddu  $t2, $sp, $a1
479.Losr_loop_entry:
480    lw     $t0, 0($t1)
481    daddiu $t1, $t1, -4
482    sw     $t0, 0($t2)
483    bne    $sp, $t2, .Losr_loop_entry
484    daddiu $t2, $t2, -4
485
486.Losr_loop_exit:
487    move   $t9, $a2
488    jalr   $zero, $t9                      # Jump to the OSR entry point.
489    nop
490END art_quick_osr_stub
491
492    /*
493     * On entry $a0 is uint32_t* gprs_ and $a1 is uint32_t* fprs_
494     * FIXME: just guessing about the shape of the jmpbuf.  Where will pc be?
495     */
496ENTRY_NO_GP art_quick_do_long_jump
497    l.d     $f0, 0($a1)
498    l.d     $f1, 8($a1)
499    l.d     $f2, 16($a1)
500    l.d     $f3, 24($a1)
501    l.d     $f4, 32($a1)
502    l.d     $f5, 40($a1)
503    l.d     $f6, 48($a1)
504    l.d     $f7, 56($a1)
505    l.d     $f8, 64($a1)
506    l.d     $f9, 72($a1)
507    l.d     $f10, 80($a1)
508    l.d     $f11, 88($a1)
509    l.d     $f12, 96($a1)
510    l.d     $f13, 104($a1)
511    l.d     $f14, 112($a1)
512    l.d     $f15, 120($a1)
513    l.d     $f16, 128($a1)
514    l.d     $f17, 136($a1)
515    l.d     $f18, 144($a1)
516    l.d     $f19, 152($a1)
517    l.d     $f20, 160($a1)
518    l.d     $f21, 168($a1)
519    l.d     $f22, 176($a1)
520    l.d     $f23, 184($a1)
521    l.d     $f24, 192($a1)
522    l.d     $f25, 200($a1)
523    l.d     $f26, 208($a1)
524    l.d     $f27, 216($a1)
525    l.d     $f28, 224($a1)
526    l.d     $f29, 232($a1)
527    l.d     $f30, 240($a1)
528    l.d     $f31, 248($a1)
529    .set push
530    .set nomacro
531    .set noat
532# no need to load zero
533    ld      $at, 8($a0)
534    .set pop
535    ld      $v0, 16($a0)
536    ld      $v1, 24($a0)
537# a0 has to be loaded last
538    ld      $a1, 40($a0)
539    ld      $a2, 48($a0)
540    ld      $a3, 56($a0)
541    ld      $a4, 64($a0)
542    ld      $a5, 72($a0)
543    ld      $a6, 80($a0)
544    ld      $a7, 88($a0)
545    ld      $t0, 96($a0)
546    ld      $t1, 104($a0)
547    ld      $t2, 112($a0)
548    ld      $t3, 120($a0)
549    ld      $s0, 128($a0)
550    ld      $s1, 136($a0)
551    ld      $s2, 144($a0)
552    ld      $s3, 152($a0)
553    ld      $s4, 160($a0)
554    ld      $s5, 168($a0)
555    ld      $s6, 176($a0)
556    ld      $s7, 184($a0)
557    ld      $t8, 192($a0)
558    ld      $t9, 200($a0)
559# no need to load k0, k1
560    ld      $gp, 224($a0)
561    ld      $sp, 232($a0)
562    ld      $s8, 240($a0)
563    ld      $ra, 248($a0)
564    ld      $a0, 32($a0)
565    move    $v0, $zero          # clear result registers v0 and v1
566    jalr    $zero, $t9          # do long jump (do not use ra, it must not be clobbered)
567    move    $v1, $zero
568END art_quick_do_long_jump
569
570    /*
571     * Called by managed code, saves most registers (forms basis of long jump
572     * context) and passes the bottom of the stack.
573     * artDeliverExceptionFromCode will place the callee save Method* at
574     * the bottom of the thread. On entry a0 holds Throwable*
575     */
576ENTRY art_quick_deliver_exception
577    SETUP_SAVE_ALL_CALLEE_SAVE_FRAME
578    dla  $t9, artDeliverExceptionFromCode
579    jalr $zero, $t9                 # artDeliverExceptionFromCode(Throwable*, Thread*)
580    move $a1, rSELF                 # pass Thread::Current
581END art_quick_deliver_exception
582
583    /*
584     * Called by managed code to create and deliver a NullPointerException
585     */
586    .extern artThrowNullPointerExceptionFromCode
587ENTRY art_quick_throw_null_pointer_exception
588.Lart_quick_throw_null_pointer_exception_gp_set:
589    SETUP_SAVE_ALL_CALLEE_SAVE_FRAME
590    dla  $t9, artThrowNullPointerExceptionFromCode
591    jalr $zero, $t9                 # artThrowNullPointerExceptionFromCode(Thread*)
592    move $a0, rSELF                 # pass Thread::Current
593END art_quick_throw_null_pointer_exception
594
595    /*
596     * Called by managed code to create and deliver an ArithmeticException
597     */
598    .extern artThrowDivZeroFromCode
599ENTRY art_quick_throw_div_zero
600    SETUP_SAVE_ALL_CALLEE_SAVE_FRAME
601    dla  $t9, artThrowDivZeroFromCode
602    jalr $zero, $t9                 # artThrowDivZeroFromCode(Thread*)
603    move $a0, rSELF                 # pass Thread::Current
604END art_quick_throw_div_zero
605
606    /*
607     * Called by managed code to create and deliver an
608     * ArrayIndexOutOfBoundsException
609     */
610    .extern artThrowArrayBoundsFromCode
611ENTRY art_quick_throw_array_bounds
612.Lart_quick_throw_array_bounds_gp_set:
613    SETUP_SAVE_ALL_CALLEE_SAVE_FRAME
614    dla  $t9, artThrowArrayBoundsFromCode
615    jalr $zero, $t9                 # artThrowArrayBoundsFromCode(index, limit, Thread*)
616    move $a2, rSELF                 # pass Thread::Current
617END art_quick_throw_array_bounds
618
619    /*
620     * Called by managed code to create and deliver a StackOverflowError.
621     */
622    .extern artThrowStackOverflowFromCode
623ENTRY art_quick_throw_stack_overflow
624    SETUP_SAVE_ALL_CALLEE_SAVE_FRAME
625    dla  $t9, artThrowStackOverflowFromCode
626    jalr $zero, $t9                 # artThrowStackOverflowFromCode(Thread*)
627    move $a0, rSELF                 # pass Thread::Current
628END art_quick_throw_stack_overflow
629
630    /*
631     * Called by managed code to create and deliver a NoSuchMethodError.
632     */
633    .extern artThrowNoSuchMethodFromCode
634ENTRY art_quick_throw_no_such_method
635    SETUP_SAVE_ALL_CALLEE_SAVE_FRAME
636    dla  $t9, artThrowNoSuchMethodFromCode
637    jalr $zero, $t9                 # artThrowNoSuchMethodFromCode(method_idx, Thread*)
638    move $a1, rSELF                 # pass Thread::Current
639END art_quick_throw_no_such_method
640
641    /*
642     * All generated callsites for interface invokes and invocation slow paths will load arguments
643     * as usual - except instead of loading arg0/$a0 with the target Method*, arg0/$a0 will contain
644     * the method_idx.  This wrapper will save arg1-arg3, load the caller's Method*, align the
645     * stack and call the appropriate C helper.
646     * NOTE: "this" is first visable argument of the target, and so can be found in arg1/$a1.
647     *
648     * The helper will attempt to locate the target and return a 128-bit result in $v0/$v1 consisting
649     * of the target Method* in $v0 and method->code_ in $v1.
650     *
651     * If unsuccessful, the helper will return null/null. There will be a pending exception in the
652     * thread and we branch to another stub to deliver it.
653     *
654     * On success this wrapper will restore arguments and *jump* to the target, leaving the ra
655     * pointing back to the original caller.
656     */
657.macro INVOKE_TRAMPOLINE_BODY cxx_name
658    .extern \cxx_name
659    SETUP_REFS_AND_ARGS_CALLEE_SAVE_FRAME  # save callee saves in case allocation triggers GC
660    move  $a2, rSELF                       # pass Thread::Current
661    jal   \cxx_name                        # (method_idx, this, Thread*, $sp)
662    move  $a3, $sp                         # pass $sp
663    move  $a0, $v0                         # save target Method*
664    move  $t9, $v1                         # save $v0->code_
665    RESTORE_REFS_AND_ARGS_CALLEE_SAVE_FRAME
666    beq   $v0, $zero, 1f
667    nop
668    jalr  $zero, $t9
669    nop
6701:
671    DELIVER_PENDING_EXCEPTION
672.endm
673.macro INVOKE_TRAMPOLINE c_name, cxx_name
674ENTRY \c_name
675    INVOKE_TRAMPOLINE_BODY \cxx_name
676END \c_name
677.endm
678
679INVOKE_TRAMPOLINE art_quick_invoke_interface_trampoline_with_access_check, artInvokeInterfaceTrampolineWithAccessCheck
680
681INVOKE_TRAMPOLINE art_quick_invoke_static_trampoline_with_access_check, artInvokeStaticTrampolineWithAccessCheck
682INVOKE_TRAMPOLINE art_quick_invoke_direct_trampoline_with_access_check, artInvokeDirectTrampolineWithAccessCheck
683INVOKE_TRAMPOLINE art_quick_invoke_super_trampoline_with_access_check, artInvokeSuperTrampolineWithAccessCheck
684INVOKE_TRAMPOLINE art_quick_invoke_virtual_trampoline_with_access_check, artInvokeVirtualTrampolineWithAccessCheck
685
686    # On entry:
687    #   t0 = shorty
688    #   t1 = ptr to arg_array
689    #   t2 = number of argument bytes remain
690    #   v0 = ptr to stack frame where to copy arg_array
691    # This macro modifies t3, t9 and v0
692.macro LOOP_OVER_SHORTY_LOADING_REG gpu, fpu, label
693    lbu    $t3, 0($t0)           # get argument type from shorty
694    beqz   $t3, \label
695    daddiu $t0, 1
696    li     $t9, 68               # put char 'D' into t9
697    beq    $t9, $t3, 1f          # branch if result type char == 'D'
698    li     $t9, 70               # put char 'F' into t9
699    beq    $t9, $t3, 2f          # branch if result type char == 'F'
700    li     $t9, 74               # put char 'J' into t9
701    beq    $t9, $t3, 3f          # branch if result type char == 'J'
702    nop
703    lw     $\gpu, 0($t1)
704    sw     $\gpu, 0($v0)
705    daddiu $v0, 4
706    daddiu $t1, 4
707    b      4f
708    daddiu $t2, -4               # delay slot
709
7101:  # found double
711    lwu    $t3, 0($t1)
712    mtc1   $t3, $\fpu
713    sw     $t3, 0($v0)
714    lwu    $t3, 4($t1)
715    mthc1  $t3, $\fpu
716    sw     $t3, 4($v0)
717    daddiu $v0, 8
718    daddiu $t1, 8
719    b      4f
720    daddiu $t2, -8               # delay slot
721
7222:  # found float
723    lwu    $t3, 0($t1)
724    mtc1   $t3, $\fpu
725    sw     $t3, 0($v0)
726    daddiu $v0, 4
727    daddiu $t1, 4
728    b      4f
729    daddiu $t2, -4               # delay slot
730
7313:  # found long (8 bytes)
732    lwu    $t3, 0($t1)
733    sw     $t3, 0($v0)
734    lwu    $t9, 4($t1)
735    sw     $t9, 4($v0)
736    dsll   $t9, $t9, 32
737    or     $\gpu, $t9, $t3
738    daddiu $v0, 8
739    daddiu $t1, 8
740    daddiu $t2, -8
7414:
742.endm
743
744    /*
745     * Invocation stub for quick code.
746     * On entry:
747     *   a0 = method pointer
748     *   a1 = argument array that must at least contain the this ptr.
749     *   a2 = size of argument array in bytes
750     *   a3 = (managed) thread pointer
751     *   a4 = JValue* result
752     *   a5 = shorty
753     */
754ENTRY_NO_GP art_quick_invoke_stub
755    # push a4, a5, s0(rSUSPEND), s1(rSELF), s8, ra onto the stack
756    daddiu $sp, $sp, -48
757    .cfi_adjust_cfa_offset 48
758    sd     $ra, 40($sp)
759    .cfi_rel_offset 31, 40
760    sd     $s8, 32($sp)
761    .cfi_rel_offset 30, 32
762    sd     $s1, 24($sp)
763    .cfi_rel_offset 17, 24
764    sd     $s0, 16($sp)
765    .cfi_rel_offset 16, 16
766    sd     $a5, 8($sp)
767    .cfi_rel_offset 9, 8
768    sd     $a4, 0($sp)
769    .cfi_rel_offset 8, 0
770
771    daddiu $s0, $zero, SUSPEND_CHECK_INTERVAL   # reset rSUSPEND to SUSPEND_CHECK_INTERVAL
772    move   $s1, $a3              # move managed thread pointer into s1 (rSELF)
773    move   $s8, $sp              # save sp in s8 (fp)
774
775    daddiu $t3, $a2, 24          # add 8 for ArtMethod* and 16 for stack alignment
776    dsrl   $t3, $t3, 4           # shift the frame size right 4
777    dsll   $t3, $t3, 4           # shift the frame size left 4 to align to 16 bytes
778    dsubu  $sp, $sp, $t3         # reserve stack space for argument array
779
780    daddiu $t0, $a5, 1           # t0 = shorty[1] (skip 1 for return type)
781    daddiu $t1, $a1, 4           # t1 = ptr to arg_array[4] (skip this ptr)
782    daddiu $t2, $a2, -4          # t2 = number of argument bytes remain (skip this ptr)
783    daddiu $v0, $sp, 12          # v0 points to where to copy arg_array
784    LOOP_OVER_SHORTY_LOADING_REG a2, f14, call_fn
785    LOOP_OVER_SHORTY_LOADING_REG a3, f15, call_fn
786    LOOP_OVER_SHORTY_LOADING_REG a4, f16, call_fn
787    LOOP_OVER_SHORTY_LOADING_REG a5, f17, call_fn
788    LOOP_OVER_SHORTY_LOADING_REG a6, f18, call_fn
789    LOOP_OVER_SHORTY_LOADING_REG a7, f19, call_fn
790
791    # copy arguments onto stack (t2 should be multiples of 4)
792    ble    $t2, $zero, call_fn   # t2 = number of argument bytes remain
7931:
794    lw     $t3, 0($t1)           # load from argument array
795    daddiu $t1, $t1, 4
796    sw     $t3, 0($v0)           # save to stack
797    daddiu $t2, -4
798    bgt    $t2, $zero, 1b        # t2 = number of argument bytes remain
799    daddiu $v0, $v0, 4
800
801call_fn:
802    # call method (a0 and a1 have been untouched)
803    lwu    $a1, 0($a1)           # make a1 = this ptr
804    sw     $a1, 8($sp)           # copy this ptr (skip 8 bytes for ArtMethod*)
805    sd     $zero, 0($sp)         # store null for ArtMethod* at bottom of frame
806    ld     $t9, ART_METHOD_QUICK_CODE_OFFSET_64($a0)  # get pointer to the code
807    jalr   $t9                   # call the method
808    nop
809    move   $sp, $s8              # restore sp
810
811    # pop a4, a5, s1(rSELF), s8, ra off of the stack
812    ld     $a4, 0($sp)
813    .cfi_restore 8
814    ld     $a5, 8($sp)
815    .cfi_restore 9
816    ld     $s0, 16($sp)
817    .cfi_restore 16
818    ld     $s1, 24($sp)
819    .cfi_restore 17
820    ld     $s8, 32($sp)
821    .cfi_restore 30
822    ld     $ra, 40($sp)
823    .cfi_restore 31
824    daddiu $sp, $sp, 48
825    .cfi_adjust_cfa_offset -48
826
827    # a4 = JValue* result
828    # a5 = shorty string
829    lbu   $t1, 0($a5)           # get result type from shorty
830    li    $t2, 68               # put char 'D' into t2
831    beq   $t1, $t2, 1f          # branch if result type char == 'D'
832    li    $t3, 70               # put char 'F' into t3
833    beq   $t1, $t3, 1f          # branch if result type char == 'F'
834    sw    $v0, 0($a4)           # store the result
835    dsrl  $v1, $v0, 32
836    jalr  $zero, $ra
837    sw    $v1, 4($a4)           # store the other half of the result
8381:
839    mfc1  $v0, $f0
840    mfhc1 $v1, $f0
841    sw    $v0, 0($a4)           # store the result
842    jalr  $zero, $ra
843    sw    $v1, 4($a4)           # store the other half of the result
844END art_quick_invoke_stub
845
846    /*
847     * Invocation static stub for quick code.
848     * On entry:
849     *   a0 = method pointer
850     *   a1 = argument array that must at least contain the this ptr.
851     *   a2 = size of argument array in bytes
852     *   a3 = (managed) thread pointer
853     *   a4 = JValue* result
854     *   a5 = shorty
855     */
856ENTRY_NO_GP art_quick_invoke_static_stub
857
858    # push a4, a5, s0(rSUSPEND), s1(rSELF), s8, ra, onto the stack
859    daddiu $sp, $sp, -48
860    .cfi_adjust_cfa_offset 48
861    sd     $ra, 40($sp)
862    .cfi_rel_offset 31, 40
863    sd     $s8, 32($sp)
864    .cfi_rel_offset 30, 32
865    sd     $s1, 24($sp)
866    .cfi_rel_offset 17, 24
867    sd     $s0, 16($sp)
868    .cfi_rel_offset 16, 16
869    sd     $a5, 8($sp)
870    .cfi_rel_offset 9, 8
871    sd     $a4, 0($sp)
872    .cfi_rel_offset 8, 0
873
874    daddiu $s0, $zero, SUSPEND_CHECK_INTERVAL   # reset rSUSPEND to SUSPEND_CHECK_INTERVAL
875    move   $s1, $a3              # move managed thread pointer into s1 (rSELF)
876    move   $s8, $sp              # save sp in s8 (fp)
877
878    daddiu $t3, $a2, 24          # add 8 for ArtMethod* and 16 for stack alignment
879    dsrl   $t3, $t3, 4           # shift the frame size right 4
880    dsll   $t3, $t3, 4           # shift the frame size left 4 to align to 16 bytes
881    dsubu  $sp, $sp, $t3         # reserve stack space for argument array
882
883    daddiu $t0, $a5, 1           # t0 = shorty[1] (skip 1 for return type)
884    move   $t1, $a1              # t1 = arg_array
885    move   $t2, $a2              # t2 = number of argument bytes remain
886    daddiu $v0, $sp, 8           # v0 points to where to copy arg_array
887    LOOP_OVER_SHORTY_LOADING_REG a1, f13, call_sfn
888    LOOP_OVER_SHORTY_LOADING_REG a2, f14, call_sfn
889    LOOP_OVER_SHORTY_LOADING_REG a3, f15, call_sfn
890    LOOP_OVER_SHORTY_LOADING_REG a4, f16, call_sfn
891    LOOP_OVER_SHORTY_LOADING_REG a5, f17, call_sfn
892    LOOP_OVER_SHORTY_LOADING_REG a6, f18, call_sfn
893    LOOP_OVER_SHORTY_LOADING_REG a7, f19, call_sfn
894
895    # copy arguments onto stack (t2 should be multiples of 4)
896    ble    $t2, $zero, call_sfn  # t2 = number of argument bytes remain
8971:
898    lw     $t3, 0($t1)           # load from argument array
899    daddiu $t1, $t1, 4
900    sw     $t3, 0($v0)           # save to stack
901    daddiu $t2, -4
902    bgt    $t2, $zero, 1b        # t2 = number of argument bytes remain
903    daddiu $v0, $v0, 4
904
905call_sfn:
906    # call method (a0 has been untouched)
907    sd     $zero, 0($sp)         # store null for ArtMethod* at bottom of frame
908    ld     $t9, ART_METHOD_QUICK_CODE_OFFSET_64($a0)  # get pointer to the code
909    jalr   $t9                   # call the method
910    nop
911    move   $sp, $s8              # restore sp
912
913    # pop a4, a5, s0(rSUSPEND), s1(rSELF), s8, ra off of the stack
914    ld     $a4, 0($sp)
915    .cfi_restore 8
916    ld     $a5, 8($sp)
917    .cfi_restore 9
918    ld     $s0, 16($sp)
919    .cfi_restore 16
920    ld     $s1, 24($sp)
921    .cfi_restore 17
922    ld     $s8, 32($sp)
923    .cfi_restore 30
924    ld     $ra, 40($sp)
925    .cfi_restore 31
926    daddiu $sp, $sp, 48
927    .cfi_adjust_cfa_offset -48
928
929    # a4 = JValue* result
930    # a5 = shorty string
931    lbu   $t1, 0($a5)           # get result type from shorty
932    li    $t2, 68               # put char 'D' into t2
933    beq   $t1, $t2, 1f          # branch if result type char == 'D'
934    li    $t3, 70               # put char 'F' into t3
935    beq   $t1, $t3, 1f          # branch if result type char == 'F'
936    sw    $v0, 0($a4)           # store the result
937    dsrl  $v1, $v0, 32
938    jalr  $zero, $ra
939    sw    $v1, 4($a4)           # store the other half of the result
9401:
941    mfc1  $v0, $f0
942    mfhc1 $v1, $f0
943    sw    $v0, 0($a4)           # store the result
944    jalr  $zero, $ra
945    sw    $v1, 4($a4)           # store the other half of the result
946END art_quick_invoke_static_stub
947
948    /*
949     * Entry from managed code that calls artHandleFillArrayDataFromCode and
950     * delivers exception on failure.
951     */
952    .extern artHandleFillArrayDataFromCode
953ENTRY art_quick_handle_fill_data
954    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME  # save callee saves in case exception allocation triggers GC
955    ld      $a2, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
956    jal     artHandleFillArrayDataFromCode              # (payload offset, Array*, method, Thread*)
957    move    $a3, rSELF                                  # pass Thread::Current
958    RETURN_IF_ZERO
959END art_quick_handle_fill_data
960
961    /*
962     * Entry from managed code that calls artLockObjectFromCode, may block for GC.
963     */
964    .extern artLockObjectFromCode
965ENTRY art_quick_lock_object
966    beq     $a0, $zero, .Lart_quick_throw_null_pointer_exception_gp_set
967    nop
968    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME     # save callee saves in case we block
969    jal     artLockObjectFromCode         # (Object* obj, Thread*)
970    move    $a1, rSELF                    # pass Thread::Current
971    RETURN_IF_ZERO
972END art_quick_lock_object
973
974ENTRY art_quick_lock_object_no_inline
975    beq     $a0, $zero, .Lart_quick_throw_null_pointer_exception_gp_set
976    nop
977    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME     # save callee saves in case we block
978    jal     artLockObjectFromCode         # (Object* obj, Thread*)
979    move    $a1, rSELF                    # pass Thread::Current
980    RETURN_IF_ZERO
981END art_quick_lock_object_no_inline
982
983    /*
984     * Entry from managed code that calls artUnlockObjectFromCode and delivers exception on failure.
985     */
986    .extern artUnlockObjectFromCode
987ENTRY art_quick_unlock_object
988    beq     $a0, $zero, .Lart_quick_throw_null_pointer_exception_gp_set
989    nop
990    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME  # save callee saves in case exception allocation triggers GC
991    jal     artUnlockObjectFromCode    # (Object* obj, Thread*)
992    move    $a1, rSELF                 # pass Thread::Current
993    RETURN_IF_ZERO
994END art_quick_unlock_object
995
996ENTRY art_quick_unlock_object_no_inline
997    beq     $a0, $zero, .Lart_quick_throw_null_pointer_exception_gp_set
998    nop
999    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME  # save callee saves in case exception allocation triggers GC
1000    jal     artUnlockObjectFromCode    # (Object* obj, Thread*)
1001    move    $a1, rSELF                 # pass Thread::Current
1002    RETURN_IF_ZERO
1003END art_quick_unlock_object_no_inline
1004
1005    /*
1006     * Entry from managed code that calls artCheckCastFromCode and delivers exception on failure.
1007     */
1008    .extern artThrowClassCastException
1009ENTRY art_quick_check_cast
1010    daddiu $sp, $sp, -32
1011    .cfi_adjust_cfa_offset 32
1012    sd     $ra, 24($sp)
1013    .cfi_rel_offset 31, 24
1014    sd     $t9, 16($sp)
1015    sd     $a1, 8($sp)
1016    sd     $a0, 0($sp)
1017    jal    artIsAssignableFromCode
1018    .cpreturn                       # Restore gp from t8 in branch delay slot.
1019                                    # t8 may be clobbered in artIsAssignableFromCode.
1020    beq    $v0, $zero, .Lthrow_class_cast_exception
1021    ld     $ra, 24($sp)
1022    jalr   $zero, $ra
1023    daddiu $sp, $sp, 32
1024    .cfi_adjust_cfa_offset -32
1025.Lthrow_class_cast_exception:
1026    ld     $t9, 16($sp)
1027    ld     $a1, 8($sp)
1028    ld     $a0, 0($sp)
1029    daddiu $sp, $sp, 32
1030    .cfi_adjust_cfa_offset -32
1031    SETUP_GP
1032    SETUP_SAVE_ALL_CALLEE_SAVE_FRAME
1033    dla  $t9, artThrowClassCastException
1034    jalr $zero, $t9                 # artThrowClassCastException (Class*, Class*, Thread*)
1035    move $a2, rSELF                 # pass Thread::Current
1036END art_quick_check_cast
1037
1038
1039    /*
1040     * Restore rReg's value from offset($sp) if rReg is not the same as rExclude.
1041     * nReg is the register number for rReg.
1042     */
1043.macro POP_REG_NE rReg, nReg, offset, rExclude
1044    .ifnc \rReg, \rExclude
1045        ld \rReg, \offset($sp)      # restore rReg
1046        .cfi_restore \nReg
1047    .endif
1048.endm
1049
1050    /*
1051     * Macro to insert read barrier, only used in art_quick_aput_obj.
1052     * rObj and rDest are registers, offset is a defined literal such as MIRROR_OBJECT_CLASS_OFFSET.
1053     * TODO: When read barrier has a fast path, add heap unpoisoning support for the fast path.
1054     */
1055.macro READ_BARRIER rDest, rObj, offset
1056#ifdef USE_READ_BARRIER
1057    # saved registers used in art_quick_aput_obj: a0-a2, t0-t1, t9, ra. 16B-aligned.
1058    daddiu  $sp, $sp, -64
1059    .cfi_adjust_cfa_offset 64
1060    sd     $ra, 56($sp)
1061    .cfi_rel_offset 31, 56
1062    sd     $t9, 48($sp)
1063    .cfi_rel_offset 25, 48
1064    sd     $t1, 40($sp)
1065    .cfi_rel_offset 13, 40
1066    sd     $t0, 32($sp)
1067    .cfi_rel_offset 12, 32
1068    sd     $a2, 16($sp)             # padding slot at offset 24 (padding can be any slot in the 64B)
1069    .cfi_rel_offset 6, 16
1070    sd     $a1, 8($sp)
1071    .cfi_rel_offset 5, 8
1072    sd     $a0, 0($sp)
1073    .cfi_rel_offset 4, 0
1074
1075    # move $a0, \rRef               # pass ref in a0 (no-op for now since parameter ref is unused)
1076    .ifnc \rObj, $a1
1077        move $a1, \rObj             # pass rObj
1078    .endif
1079    daddiu $a2, $zero, \offset      # pass offset
1080    jal artReadBarrierSlow          # artReadBarrierSlow(ref, rObj, offset)
1081    .cpreturn                       # Restore gp from t8 in branch delay slot.
1082                                    # t8 may be clobbered in artReadBarrierSlow.
1083    # No need to unpoison return value in v0, artReadBarrierSlow() would do the unpoisoning.
1084    move \rDest, $v0                # save return value in rDest
1085                                    # (rDest cannot be v0 in art_quick_aput_obj)
1086
1087    ld     $a0, 0($sp)              # restore registers except rDest
1088                                    # (rDest can only be t0 or t1 in art_quick_aput_obj)
1089    .cfi_restore 4
1090    ld     $a1, 8($sp)
1091    .cfi_restore 5
1092    ld     $a2, 16($sp)
1093    .cfi_restore 6
1094    POP_REG_NE $t0, 12, 32, \rDest
1095    POP_REG_NE $t1, 13, 40, \rDest
1096    ld     $t9, 48($sp)
1097    .cfi_restore 25
1098    ld     $ra, 56($sp)             # restore $ra
1099    .cfi_restore 31
1100    daddiu  $sp, $sp, 64
1101    .cfi_adjust_cfa_offset -64
1102    SETUP_GP                        # set up gp because we are not returning
1103#else
1104    lwu     \rDest, \offset(\rObj)
1105    UNPOISON_HEAP_REF \rDest
1106#endif  // USE_READ_BARRIER
1107.endm
1108
1109    /*
1110     * Entry from managed code for array put operations of objects where the value being stored
1111     * needs to be checked for compatibility.
1112     * a0 = array, a1 = index, a2 = value
1113     */
1114ENTRY art_quick_aput_obj_with_null_and_bound_check
1115    bne    $a0, $zero, .Lart_quick_aput_obj_with_bound_check_gp_set
1116    nop
1117    b .Lart_quick_throw_null_pointer_exception_gp_set
1118    nop
1119END art_quick_aput_obj_with_null_and_bound_check
1120
1121ENTRY art_quick_aput_obj_with_bound_check
1122    lwu  $t0, MIRROR_ARRAY_LENGTH_OFFSET($a0)
1123    sltu $t1, $a1, $t0
1124    bne  $t1, $zero, .Lart_quick_aput_obj_gp_set
1125    nop
1126    move $a0, $a1
1127    b .Lart_quick_throw_array_bounds_gp_set
1128    move $a1, $t0
1129END art_quick_aput_obj_with_bound_check
1130
1131ENTRY art_quick_aput_obj
1132    beq  $a2, $zero, .Ldo_aput_null
1133    nop
1134    READ_BARRIER $t0, $a0, MIRROR_OBJECT_CLASS_OFFSET
1135    READ_BARRIER $t1, $a2, MIRROR_OBJECT_CLASS_OFFSET
1136    READ_BARRIER $t0, $t0, MIRROR_CLASS_COMPONENT_TYPE_OFFSET
1137    bne $t1, $t0, .Lcheck_assignability  # value's type == array's component type - trivial assignability
1138    nop
1139.Ldo_aput:
1140    dsll  $a1, $a1, 2
1141    daddu $t0, $a0, $a1
1142    POISON_HEAP_REF $a2
1143    sw   $a2, MIRROR_OBJECT_ARRAY_DATA_OFFSET($t0)
1144    ld   $t0, THREAD_CARD_TABLE_OFFSET(rSELF)
1145    dsrl  $t1, $a0, 7
1146    daddu $t1, $t1, $t0
1147    sb   $t0, ($t1)
1148    jalr $zero, $ra
1149    .cpreturn                       # Restore gp from t8 in branch delay slot.
1150.Ldo_aput_null:
1151    dsll  $a1, $a1, 2
1152    daddu $t0, $a0, $a1
1153    sw   $a2, MIRROR_OBJECT_ARRAY_DATA_OFFSET($t0)
1154    jalr $zero, $ra
1155    .cpreturn                       # Restore gp from t8 in branch delay slot.
1156.Lcheck_assignability:
1157    daddiu $sp, $sp, -64
1158    .cfi_adjust_cfa_offset 64
1159    sd     $ra, 56($sp)
1160    .cfi_rel_offset 31, 56
1161    sd     $t9, 24($sp)
1162    sd     $a2, 16($sp)
1163    sd     $a1, 8($sp)
1164    sd     $a0, 0($sp)
1165    move   $a1, $t1
1166    move   $a0, $t0
1167    jal    artIsAssignableFromCode  # (Class*, Class*)
1168    .cpreturn                       # Restore gp from t8 in branch delay slot.
1169                                    # t8 may be clobbered in artIsAssignableFromCode.
1170    ld     $ra, 56($sp)
1171    ld     $t9, 24($sp)
1172    ld     $a2, 16($sp)
1173    ld     $a1, 8($sp)
1174    ld     $a0, 0($sp)
1175    daddiu $sp, $sp, 64
1176    .cfi_adjust_cfa_offset -64
1177    SETUP_GP
1178    bne    $v0, $zero, .Ldo_aput
1179    nop
1180    SETUP_SAVE_ALL_CALLEE_SAVE_FRAME
1181    move   $a1, $a2
1182    dla  $t9, artThrowArrayStoreException
1183    jalr $zero, $t9                 # artThrowArrayStoreException(Class*, Class*, Thread*)
1184    move   $a2, rSELF               # pass Thread::Current
1185END art_quick_aput_obj
1186
1187    /*
1188     * Called by managed code to resolve a static field and load a boolean primitive value.
1189     */
1190    .extern artGetBooleanStaticFromCode
1191ENTRY art_quick_get_boolean_static
1192    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1193    ld     $a1, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1194    jal    artGetBooleanStaticFromCode   # (uint32_t field_idx, const Method* referrer, Thread*)
1195    move   $a2, rSELF                    # pass Thread::Current
1196    RETURN_IF_NO_EXCEPTION
1197END art_quick_get_boolean_static
1198
1199    /*
1200     * Called by managed code to resolve a static field and load a byte primitive value.
1201     */
1202    .extern artGetByteStaticFromCode
1203ENTRY art_quick_get_byte_static
1204    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1205    ld     $a1, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1206    jal    artGetByteStaticFromCode      # (uint32_t field_idx, const Method* referrer, Thread*)
1207    move   $a2, rSELF                    # pass Thread::Current
1208    RETURN_IF_NO_EXCEPTION
1209END art_quick_get_byte_static
1210
1211    /*
1212     * Called by managed code to resolve a static field and load a char primitive value.
1213     */
1214    .extern artGetCharStaticFromCode
1215ENTRY art_quick_get_char_static
1216    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1217    ld     $a1, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1218    jal    artGetCharStaticFromCode      # (uint32_t field_idx, const Method* referrer, Thread*)
1219    move   $a2, rSELF                    # pass Thread::Current
1220    RETURN_IF_NO_EXCEPTION
1221END art_quick_get_char_static
1222
1223    /*
1224     * Called by managed code to resolve a static field and load a short primitive value.
1225     */
1226    .extern artGetShortStaticFromCode
1227ENTRY art_quick_get_short_static
1228    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1229    ld     $a1, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1230    jal    artGetShortStaticFromCode     # (uint32_t field_idx, const Method* referrer, Thread*)
1231    move   $a2, rSELF                    # pass Thread::Current
1232    RETURN_IF_NO_EXCEPTION
1233END art_quick_get_short_static
1234
1235    /*
1236     * Called by managed code to resolve a static field and load a 32-bit primitive value.
1237     */
1238    .extern artGet32StaticFromCode
1239ENTRY art_quick_get32_static
1240    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1241    ld     $a1, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1242    jal    artGet32StaticFromCode        # (uint32_t field_idx, const Method* referrer, Thread*)
1243    move   $a2, rSELF                    # pass Thread::Current
1244    RETURN_IF_NO_EXCEPTION
1245END art_quick_get32_static
1246
1247    /*
1248     * Called by managed code to resolve a static field and load a 64-bit primitive value.
1249     */
1250    .extern artGet64StaticFromCode
1251ENTRY art_quick_get64_static
1252    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1253    ld     $a1, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1254    jal    artGet64StaticFromCode        # (uint32_t field_idx, const Method* referrer, Thread*)
1255    move   $a2, rSELF                    # pass Thread::Current
1256    RETURN_IF_NO_EXCEPTION
1257END art_quick_get64_static
1258
1259    /*
1260     * Called by managed code to resolve a static field and load an object reference.
1261     */
1262    .extern artGetObjStaticFromCode
1263ENTRY art_quick_get_obj_static
1264    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1265    ld     $a1, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1266    jal    artGetObjStaticFromCode       # (uint32_t field_idx, const Method* referrer, Thread*)
1267    move   $a2, rSELF                    # pass Thread::Current
1268    RETURN_IF_NO_EXCEPTION
1269END art_quick_get_obj_static
1270
1271    /*
1272     * Called by managed code to resolve an instance field and load a boolean primitive value.
1273     */
1274    .extern artGetBooleanInstanceFromCode
1275ENTRY art_quick_get_boolean_instance
1276    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1277    ld     $a2, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1278    jal    artGetBooleanInstanceFromCode # (field_idx, Object*, referrer, Thread*)
1279    move   $a3, rSELF                    # pass Thread::Current
1280    RETURN_IF_NO_EXCEPTION
1281END art_quick_get_boolean_instance
1282
1283    /*
1284     * Called by managed code to resolve an instance field and load a byte primitive value.
1285     */
1286    .extern artGetByteInstanceFromCode
1287ENTRY art_quick_get_byte_instance
1288    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1289    ld     $a2, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1290    jal    artGetByteInstanceFromCode    # (field_idx, Object*, referrer, Thread*)
1291    move   $a3, rSELF                    # pass Thread::Current
1292    RETURN_IF_NO_EXCEPTION
1293END art_quick_get_byte_instance
1294
1295    /*
1296     * Called by managed code to resolve an instance field and load a char primitive value.
1297     */
1298    .extern artGetCharInstanceFromCode
1299ENTRY art_quick_get_char_instance
1300    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1301    ld     $a2, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1302    jal    artGetCharInstanceFromCode    # (field_idx, Object*, referrer, Thread*)
1303    move   $a3, rSELF                    # pass Thread::Current
1304    RETURN_IF_NO_EXCEPTION
1305END art_quick_get_char_instance
1306
1307    /*
1308     * Called by managed code to resolve an instance field and load a short primitive value.
1309     */
1310    .extern artGetShortInstanceFromCode
1311ENTRY art_quick_get_short_instance
1312    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1313    ld     $a2, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1314    jal    artGetShortInstanceFromCode   # (field_idx, Object*, referrer, Thread*)
1315    move   $a3, rSELF                    # pass Thread::Current
1316    RETURN_IF_NO_EXCEPTION
1317END art_quick_get_short_instance
1318
1319    /*
1320     * Called by managed code to resolve an instance field and load a 32-bit primitive value.
1321     */
1322    .extern artGet32InstanceFromCode
1323ENTRY art_quick_get32_instance
1324    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1325    ld     $a2, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1326    jal    artGet32InstanceFromCode      # (field_idx, Object*, referrer, Thread*)
1327    move   $a3, rSELF                    # pass Thread::Current
1328    RETURN_IF_NO_EXCEPTION
1329END art_quick_get32_instance
1330
1331    /*
1332     * Called by managed code to resolve an instance field and load a 64-bit primitive value.
1333     */
1334    .extern artGet64InstanceFromCode
1335ENTRY art_quick_get64_instance
1336    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1337    ld     $a2, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1338    jal    artGet64InstanceFromCode      # (field_idx, Object*, referrer, Thread*)
1339    move   $a3, rSELF                    # pass Thread::Current
1340    RETURN_IF_NO_EXCEPTION
1341END art_quick_get64_instance
1342
1343    /*
1344     * Called by managed code to resolve an instance field and load an object reference.
1345     */
1346    .extern artGetObjInstanceFromCode
1347ENTRY art_quick_get_obj_instance
1348    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1349    ld     $a2, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1350    jal    artGetObjInstanceFromCode     # (field_idx, Object*, referrer, Thread*)
1351    move   $a3, rSELF                    # pass Thread::Current
1352    RETURN_IF_NO_EXCEPTION
1353END art_quick_get_obj_instance
1354
1355    /*
1356     * Called by managed code to resolve a static field and store a 8-bit primitive value.
1357     */
1358    .extern artSet8StaticFromCode
1359ENTRY art_quick_set8_static
1360    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1361    ld     $a2, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1362    jal    artSet8StaticFromCode         # (field_idx, new_val, referrer, Thread*)
1363    move   $a3, rSELF                    # pass Thread::Current
1364    RETURN_IF_ZERO
1365END art_quick_set8_static
1366
1367    /*
1368     * Called by managed code to resolve a static field and store a 16-bit primitive value.
1369     */
1370    .extern artSet16StaticFromCode
1371ENTRY art_quick_set16_static
1372    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1373    ld     $a2, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1374    jal    artSet16StaticFromCode        # (field_idx, new_val, referrer, Thread*)
1375    move   $a3, rSELF                    # pass Thread::Current
1376    RETURN_IF_ZERO
1377END art_quick_set16_static
1378
1379    /*
1380     * Called by managed code to resolve a static field and store a 32-bit primitive value.
1381     */
1382    .extern artSet32StaticFromCode
1383ENTRY art_quick_set32_static
1384    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1385    ld     $a2, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1386    jal    artSet32StaticFromCode        # (field_idx, new_val, referrer, Thread*)
1387    move   $a3, rSELF                    # pass Thread::Current
1388    RETURN_IF_ZERO
1389END art_quick_set32_static
1390
1391    /*
1392     * Called by managed code to resolve a static field and store a 64-bit primitive value.
1393     */
1394    .extern artSet64StaticFromCode
1395ENTRY art_quick_set64_static
1396    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1397                                         # a2 contains the new val
1398    ld     $a1, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1399    jal    artSet64StaticFromCode        # (field_idx, referrer, new_val, Thread*)
1400    move   $a3, rSELF                    # pass Thread::Current
1401    RETURN_IF_ZERO
1402END art_quick_set64_static
1403
1404    /*
1405     * Called by managed code to resolve a static field and store an object reference.
1406     */
1407    .extern artSetObjStaticFromCode
1408ENTRY art_quick_set_obj_static
1409    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1410    ld     $a2, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1411    jal    artSetObjStaticFromCode       # (field_idx, new_val, referrer, Thread*)
1412    move   $a3, rSELF                    # pass Thread::Current
1413    RETURN_IF_ZERO
1414END art_quick_set_obj_static
1415
1416    /*
1417     * Called by managed code to resolve an instance field and store a 8-bit primitive value.
1418     */
1419    .extern artSet8InstanceFromCode
1420ENTRY art_quick_set8_instance
1421    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1422    ld     $a3, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1423    jal    artSet8InstanceFromCode       # (field_idx, Object*, new_val, referrer, Thread*)
1424    move   $a4, rSELF                    # pass Thread::Current
1425    RETURN_IF_ZERO
1426END art_quick_set8_instance
1427
1428    /*
1429     * Called by managed code to resolve an instance field and store a 16-bit primitive value.
1430     */
1431    .extern artSet16InstanceFromCode
1432ENTRY art_quick_set16_instance
1433    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1434    ld     $a3, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1435    jal    artSet16InstanceFromCode      # (field_idx, Object*, new_val, referrer, Thread*)
1436    move   $a4, rSELF                    # pass Thread::Current
1437    RETURN_IF_ZERO
1438END art_quick_set16_instance
1439
1440    /*
1441     * Called by managed code to resolve an instance field and store a 32-bit primitive value.
1442     */
1443    .extern artSet32InstanceFromCode
1444ENTRY art_quick_set32_instance
1445    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1446    ld     $a3, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1447    jal    artSet32InstanceFromCode      # (field_idx, Object*, new_val, referrer, Thread*)
1448    move   $a4, rSELF                    # pass Thread::Current
1449    RETURN_IF_ZERO
1450END art_quick_set32_instance
1451
1452    /*
1453     * Called by managed code to resolve an instance field and store a 64-bit primitive value.
1454     */
1455    .extern artSet64InstanceFromCode
1456ENTRY art_quick_set64_instance
1457    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1458    ld     $a3, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1459    jal    artSet64InstanceFromCode      # (field_idx, Object*, new_val, referrer, Thread*)
1460    move   $a4, rSELF                    # pass Thread::Current
1461    RETURN_IF_ZERO
1462END art_quick_set64_instance
1463
1464    /*
1465     * Called by managed code to resolve an instance field and store an object reference.
1466     */
1467    .extern artSetObjInstanceFromCode
1468ENTRY art_quick_set_obj_instance
1469    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME    # save callee saves in case of GC
1470    ld     $a3, FRAME_SIZE_REFS_ONLY_CALLEE_SAVE($sp)  # pass referrer's Method*
1471    jal    artSetObjInstanceFromCode     # (field_idx, Object*, new_val, referrer, Thread*)
1472    move   $a4, rSELF                    # pass Thread::Current
1473    RETURN_IF_ZERO
1474END art_quick_set_obj_instance
1475
1476// Macro to facilitate adding new allocation entrypoints.
1477.macro ONE_ARG_DOWNCALL name, entrypoint, return
1478    .extern \entrypoint
1479ENTRY \name
1480    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME  # save callee saves in case of GC
1481    jal     \entrypoint
1482    move    $a1, rSELF                 # pass Thread::Current
1483    \return
1484END \name
1485.endm
1486
1487// Macro to facilitate adding new allocation entrypoints.
1488.macro TWO_ARG_DOWNCALL name, entrypoint, return
1489    .extern \entrypoint
1490ENTRY \name
1491    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME  # save callee saves in case of GC
1492    jal     \entrypoint
1493    move    $a2, rSELF                 # pass Thread::Current
1494    \return
1495END \name
1496.endm
1497
1498.macro THREE_ARG_DOWNCALL name, entrypoint, return
1499    .extern \entrypoint
1500ENTRY \name
1501    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME  # save callee saves in case of GC
1502    jal     \entrypoint
1503    move    $a3, rSELF                 # pass Thread::Current
1504    \return
1505END \name
1506.endm
1507
1508.macro FOUR_ARG_DOWNCALL name, entrypoint, return
1509    .extern \entrypoint
1510ENTRY \name
1511    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME  # save callee saves in case of GC
1512    jal     \entrypoint
1513    move    $a4, rSELF                 # pass Thread::Current
1514    \return
1515END \name
1516.endm
1517
1518// Generate the allocation entrypoints for each allocator.
1519GENERATE_ALLOC_ENTRYPOINTS_FOR_EACH_ALLOCATOR
1520
1521// A hand-written override for GENERATE_ALLOC_ENTRYPOINTS_ALLOC_OBJECT(_rosalloc, RosAlloc).
1522ENTRY art_quick_alloc_object_rosalloc
1523
1524    # Fast path rosalloc allocation
1525    # a0: type_idx
1526    # a1: ArtMethod*
1527    # s1: Thread::Current
1528    # -----------------------------
1529    # t0: class
1530    # t1: object size
1531    # t2: rosalloc run
1532    # t3: thread stack top offset
1533    # a4: thread stack bottom offset
1534    # v0: free list head
1535    #
1536    # a5, a6 : temps
1537
1538    ld     $t0, ART_METHOD_DEX_CACHE_TYPES_OFFSET_64($a1)   # Load dex cache resolved types array.
1539
1540    dsll   $a5, $a0, COMPRESSED_REFERENCE_SIZE_SHIFT        # Shift the value.
1541    daddu  $a5, $t0, $a5                                    # Compute the index.
1542    lwu    $t0, 0($a5)                                      # Load class (t0).
1543    beqzc  $t0, .Lart_quick_alloc_object_rosalloc_slow_path
1544
1545    li     $a6, MIRROR_CLASS_STATUS_INITIALIZED
1546    lwu    $a5, MIRROR_CLASS_STATUS_OFFSET($t0)             # Check class status.
1547    bnec   $a5, $a6, .Lart_quick_alloc_object_rosalloc_slow_path
1548
1549    # Add a fake dependence from the following access flag and size loads to the status load. This
1550    # is to prevent those loads from being reordered above the status load and reading wrong values.
1551    xor    $a5, $a5, $a5
1552    daddu  $t0, $t0, $a5
1553
1554    lwu    $a5, MIRROR_CLASS_ACCESS_FLAGS_OFFSET($t0)       # Check if access flags has
1555    li     $a6, ACCESS_FLAGS_CLASS_IS_FINALIZABLE           # kAccClassIsFinalizable.
1556    and    $a6, $a5, $a6
1557    bnezc  $a6, .Lart_quick_alloc_object_rosalloc_slow_path
1558
1559    ld     $t3, THREAD_LOCAL_ALLOC_STACK_TOP_OFFSET($s1)    # Check if thread local allocation stack
1560    ld     $a4, THREAD_LOCAL_ALLOC_STACK_END_OFFSET($s1)    # has any room left.
1561    bgeuc  $t3, $a4, .Lart_quick_alloc_object_rosalloc_slow_path
1562
1563    lwu    $t1, MIRROR_CLASS_OBJECT_SIZE_OFFSET($t0)        # Load object size (t1).
1564    li     $a5, ROSALLOC_MAX_THREAD_LOCAL_BRACKET_SIZE      # Check if size is for a thread local
1565                                                            # allocation.
1566    bltuc  $a5, $t1, .Lart_quick_alloc_object_rosalloc_slow_path
1567
1568    # Compute the rosalloc bracket index from the size. Allign up the size by the rosalloc bracket
1569    # quantum size and divide by the quantum size and subtract by 1.
1570    daddiu $t1, $t1, -1                                     # Decrease obj size and shift right by
1571    dsrl   $t1, $t1, ROSALLOC_BRACKET_QUANTUM_SIZE_SHIFT    # quantum.
1572
1573    dsll   $t2, $t1, POINTER_SIZE_SHIFT
1574    daddu  $t2, $t2, $s1
1575    ld     $t2, THREAD_ROSALLOC_RUNS_OFFSET($t2)            # Load rosalloc run (t2).
1576
1577    # Load the free list head (v0).
1578    # NOTE: this will be the return val.
1579    ld     $v0, (ROSALLOC_RUN_FREE_LIST_OFFSET + ROSALLOC_RUN_FREE_LIST_HEAD_OFFSET)($t2)
1580    beqzc  $v0, .Lart_quick_alloc_object_rosalloc_slow_path
1581
1582    # Load the next pointer of the head and update the list head with the next pointer.
1583    ld     $a5, ROSALLOC_SLOT_NEXT_OFFSET($v0)
1584    sd     $a5, (ROSALLOC_RUN_FREE_LIST_OFFSET + ROSALLOC_RUN_FREE_LIST_HEAD_OFFSET)($t2)
1585
1586    # Store the class pointer in the header. This also overwrites the first pointer. The offsets are
1587    # asserted to match.
1588
1589#if ROSALLOC_SLOT_NEXT_OFFSET != MIRROR_OBJECT_CLASS_OFFSET
1590#error "Class pointer needs to overwrite next pointer."
1591#endif
1592
1593    POISON_HEAP_REF $t0
1594    sw     $t0, MIRROR_OBJECT_CLASS_OFFSET($v0)
1595
1596    # Push the new object onto the thread local allocation stack and increment the thread local
1597    # allocation stack top.
1598    sd     $v0, 0($t3)
1599    daddiu $t3, $t3, COMPRESSED_REFERENCE_SIZE
1600    sd     $t3, THREAD_LOCAL_ALLOC_STACK_TOP_OFFSET($s1)
1601
1602    # Decrement the size of the free list.
1603    lw     $a5, (ROSALLOC_RUN_FREE_LIST_OFFSET + ROSALLOC_RUN_FREE_LIST_SIZE_OFFSET)($t2)
1604    addiu  $a5, $a5, -1
1605    sw     $a5, (ROSALLOC_RUN_FREE_LIST_OFFSET + ROSALLOC_RUN_FREE_LIST_SIZE_OFFSET)($t2)
1606
1607    sync                                         # Fence.
1608
1609    jalr   $zero, $ra
1610    .cpreturn                                    # Restore gp from t8 in branch delay slot.
1611
1612.Lart_quick_alloc_object_rosalloc_slow_path:
1613    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME
1614    jal    artAllocObjectFromCodeRosAlloc
1615    move   $a2 ,$s1                              # Pass self as argument.
1616    RETURN_IF_RESULT_IS_NON_ZERO_OR_DELIVER
1617
1618END art_quick_alloc_object_rosalloc
1619
1620GENERATE_ALLOC_ENTRYPOINTS_ALLOC_OBJECT(_tlab, TLAB)
1621GENERATE_ALLOC_ENTRYPOINTS_ALLOC_OBJECT(_region_tlab, RegionTLAB)
1622
1623    /*
1624     * Entry from managed code to resolve a string, this stub will allocate a String and deliver an
1625     * exception on error. On success the String is returned. A0 holds the string index. The fast
1626     * path check for hit in strings cache has already been performed.
1627     */
1628ONE_ARG_DOWNCALL art_quick_resolve_string, artResolveStringFromCode, RETURN_IF_RESULT_IS_NON_ZERO_OR_DELIVER
1629
1630    /*
1631     * Entry from managed code when uninitialized static storage, this stub will run the class
1632     * initializer and deliver the exception on error. On success the static storage base is
1633     * returned.
1634     */
1635ONE_ARG_DOWNCALL art_quick_initialize_static_storage, artInitializeStaticStorageFromCode, RETURN_IF_RESULT_IS_NON_ZERO_OR_DELIVER
1636
1637    /*
1638     * Entry from managed code when dex cache misses for a type_idx.
1639     */
1640ONE_ARG_DOWNCALL art_quick_initialize_type, artInitializeTypeFromCode, RETURN_IF_RESULT_IS_NON_ZERO_OR_DELIVER
1641
1642    /*
1643     * Entry from managed code when type_idx needs to be checked for access and dex cache may also
1644     * miss.
1645     */
1646ONE_ARG_DOWNCALL art_quick_initialize_type_and_verify_access, artInitializeTypeAndVerifyAccessFromCode, RETURN_IF_RESULT_IS_NON_ZERO_OR_DELIVER
1647
1648    /*
1649     * Called by managed code when the value in rSUSPEND has been decremented to 0.
1650     */
1651    .extern artTestSuspendFromCode
1652ENTRY art_quick_test_suspend
1653    lh     $a0, THREAD_FLAGS_OFFSET(rSELF)
1654    bne    $a0, $zero, 1f
1655    daddiu rSUSPEND, $zero, SUSPEND_CHECK_INTERVAL   # reset rSUSPEND to SUSPEND_CHECK_INTERVAL
1656    jalr   $zero, $ra
1657    .cpreturn                                 # Restore gp from t8 in branch delay slot.
16581:
1659    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME         # save callee saves for stack crawl
1660    jal    artTestSuspendFromCode             # (Thread*)
1661    move   $a0, rSELF
1662    RESTORE_REFS_ONLY_CALLEE_SAVE_FRAME_AND_RETURN
1663END art_quick_test_suspend
1664
1665    /*
1666     * Called by managed code that is attempting to call a method on a proxy class. On entry
1667     * r0 holds the proxy method; r1, r2 and r3 may contain arguments.
1668     */
1669    .extern artQuickProxyInvokeHandler
1670ENTRY art_quick_proxy_invoke_handler
1671    SETUP_REFS_AND_ARGS_CALLEE_SAVE_FRAME_WITH_METHOD_IN_A0
1672    move    $a2, rSELF             # pass Thread::Current
1673    jal     artQuickProxyInvokeHandler  # (Method* proxy method, receiver, Thread*, SP)
1674    move    $a3, $sp               # pass $sp
1675    ld      $t0, THREAD_EXCEPTION_OFFSET(rSELF) # load Thread::Current()->exception_
1676    daddiu  $sp, $sp, REFS_AND_ARGS_MINUS_REFS_SIZE  # skip a0-a7 and f12-f19
1677    RESTORE_REFS_ONLY_CALLEE_SAVE_FRAME
1678    bne     $t0, $zero, 1f
1679    dmtc1   $v0, $f0               # place return value to FP return value
1680    jalr    $zero, $ra
1681    dmtc1   $v1, $f1               # place return value to FP return value
16821:
1683    DELIVER_PENDING_EXCEPTION
1684END art_quick_proxy_invoke_handler
1685
1686    /*
1687     * Called to resolve an imt conflict.
1688     * a0 is the conflict ArtMethod.
1689     * t0 is a hidden argument that holds the target interface method's dex method index.
1690     *
1691     * Mote that this stub writes to a0, t0 and t1.
1692     */
1693ENTRY art_quick_imt_conflict_trampoline
1694    ld      $t1, 0($sp)                                      # Load referrer.
1695    ld      $t1, ART_METHOD_DEX_CACHE_METHODS_OFFSET_64($t1) # Load dex cache methods array.
1696    dsll    $t0, $t0, POINTER_SIZE_SHIFT                     # Calculate offset.
1697    daddu   $t0, $t1, $t0                                    # Add offset to base.
1698    ld      $t0, 0($t0)                                      # Load interface method.
1699    ld      $a0, ART_METHOD_JNI_OFFSET_64($a0)               # Load ImtConflictTable.
1700
1701.Limt_table_iterate:
1702    ld      $t1, 0($a0)                                      # Load next entry in ImtConflictTable.
1703    # Branch if found.
1704    beq     $t1, $t0, .Limt_table_found
1705    nop
1706    # If the entry is null, the interface method is not in the ImtConflictTable.
1707    beqzc   $t1, .Lconflict_trampoline
1708    # Iterate over the entries of the ImtConflictTable.
1709    daddiu  $a0, $a0, 2 * __SIZEOF_POINTER__                 # Iterate to the next entry.
1710    bc       .Limt_table_iterate
1711
1712.Limt_table_found:
1713    # We successfully hit an entry in the table. Load the target method and jump to it.
1714    ld      $a0, __SIZEOF_POINTER__($a0)
1715    ld      $t9, ART_METHOD_QUICK_CODE_OFFSET_64($a0)
1716    jr      $t9
1717    .cpreturn                      # Restore gp from t8 in branch delay slot.
1718
1719.Lconflict_trampoline:
1720    # Call the runtime stub to populate the ImtConflictTable and jump to the resolved method.
1721    INVOKE_TRAMPOLINE_BODY artInvokeInterfaceTrampoline
1722END art_quick_imt_conflict_trampoline
1723
1724    .extern artQuickResolutionTrampoline
1725ENTRY art_quick_resolution_trampoline
1726    SETUP_REFS_AND_ARGS_CALLEE_SAVE_FRAME
1727    move    $a2, rSELF             # pass Thread::Current
1728    jal     artQuickResolutionTrampoline  # (Method* called, receiver, Thread*, SP)
1729    move    $a3, $sp               # pass $sp
1730    beq     $v0, $zero, 1f
1731    ld      $a0, 0($sp)            # load resolved method in $a0
1732                                   # artQuickResolutionTrampoline puts resolved method in *SP
1733    RESTORE_REFS_AND_ARGS_CALLEE_SAVE_FRAME
1734    move    $t9, $v0               # code pointer must be in $t9 to generate the global pointer
1735    jalr    $zero, $t9             # tail call to method
1736    nop
17371:
1738    RESTORE_REFS_AND_ARGS_CALLEE_SAVE_FRAME
1739    DELIVER_PENDING_EXCEPTION
1740END art_quick_resolution_trampoline
1741
1742    .extern artQuickGenericJniTrampoline
1743    .extern artQuickGenericJniEndTrampoline
1744ENTRY art_quick_generic_jni_trampoline
1745    SETUP_REFS_AND_ARGS_CALLEE_SAVE_FRAME_WITH_METHOD_IN_A0
1746    move    $s8, $sp               # save $sp
1747
1748    # prepare for call to artQuickGenericJniTrampoline(Thread*, SP)
1749    move    $a0, rSELF             # pass Thread::Current
1750    move    $a1, $sp               # pass $sp
1751    jal     artQuickGenericJniTrampoline   # (Thread*, SP)
1752    daddiu  $sp, $sp, -5120        # reserve space on the stack
1753
1754    # The C call will have registered the complete save-frame on success.
1755    # The result of the call is:
1756    # v0: ptr to native code, 0 on error.
1757    # v1: ptr to the bottom of the used area of the alloca, can restore stack till here.
1758    beq     $v0, $zero, 1f         # check entry error
1759    move    $t9, $v0               # save the code ptr
1760    move    $sp, $v1               # release part of the alloca
1761
1762    # Load parameters from stack into registers
1763    ld      $a0,   0($sp)
1764    ld      $a1,   8($sp)
1765    ld      $a2,  16($sp)
1766    ld      $a3,  24($sp)
1767    ld      $a4,  32($sp)
1768    ld      $a5,  40($sp)
1769    ld      $a6,  48($sp)
1770    ld      $a7,  56($sp)
1771    # Load FPRs the same as GPRs. Look at BuildNativeCallFrameStateMachine.
1772    l.d     $f12,  0($sp)
1773    l.d     $f13,  8($sp)
1774    l.d     $f14, 16($sp)
1775    l.d     $f15, 24($sp)
1776    l.d     $f16, 32($sp)
1777    l.d     $f17, 40($sp)
1778    l.d     $f18, 48($sp)
1779    l.d     $f19, 56($sp)
1780    jalr    $t9                    # native call
1781    daddiu  $sp, $sp, 64
1782
1783    # result sign extension is handled in C code
1784    # prepare for call to artQuickGenericJniEndTrampoline(Thread*, result, result_f)
1785    move    $a0, rSELF             # pass Thread::Current
1786    move    $a1, $v0
1787    jal     artQuickGenericJniEndTrampoline
1788    dmfc1   $a2, $f0
1789
1790    ld      $t0, THREAD_EXCEPTION_OFFSET(rSELF) # load Thread::Current()->exception_
1791    bne     $t0, $zero, 1f         # check for pending exceptions
1792    move    $sp, $s8               # tear down the alloca
1793
1794    # tear dpown the callee-save frame
1795    RESTORE_REFS_AND_ARGS_CALLEE_SAVE_FRAME
1796
1797    jalr    $zero, $ra
1798    dmtc1   $v0, $f0               # place return value to FP return value
1799
18001:
1801    ld      $sp, THREAD_TOP_QUICK_FRAME_OFFSET(rSELF)
1802    # This will create a new save-all frame, required by the runtime.
1803    DELIVER_PENDING_EXCEPTION
1804END art_quick_generic_jni_trampoline
1805
1806    .extern artQuickToInterpreterBridge
1807ENTRY art_quick_to_interpreter_bridge
1808    SETUP_REFS_AND_ARGS_CALLEE_SAVE_FRAME
1809    move    $a1, rSELF             # pass Thread::Current
1810    jal     artQuickToInterpreterBridge    # (Method* method, Thread*, SP)
1811    move    $a2, $sp               # pass $sp
1812    ld      $t0, THREAD_EXCEPTION_OFFSET(rSELF) # load Thread::Current()->exception_
1813    daddiu  $sp, $sp, REFS_AND_ARGS_MINUS_REFS_SIZE  # skip a0-a7 and f12-f19
1814    RESTORE_REFS_ONLY_CALLEE_SAVE_FRAME
1815    bne     $t0, $zero, 1f
1816    dmtc1   $v0, $f0               # place return value to FP return value
1817    jalr    $zero, $ra
1818    dmtc1   $v1, $f1               # place return value to FP return value
18191:
1820    DELIVER_PENDING_EXCEPTION
1821END art_quick_to_interpreter_bridge
1822
1823    /*
1824     * Routine that intercepts method calls and returns.
1825     */
1826    .extern artInstrumentationMethodEntryFromCode
1827    .extern artInstrumentationMethodExitFromCode
1828ENTRY art_quick_instrumentation_entry
1829    SETUP_REFS_AND_ARGS_CALLEE_SAVE_FRAME
1830    daddiu   $sp, $sp, -16     # space for saving arg0
1831    .cfi_adjust_cfa_offset 16
1832    sd       $a0, 0($sp)       # save arg0
1833    move     $a3, $ra          # pass $ra
1834    jal      artInstrumentationMethodEntryFromCode  # (Method*, Object*, Thread*, RA)
1835    move     $a2, rSELF        # pass Thread::Current
1836    move     $t9, $v0          # $t9 holds reference to code
1837    ld       $a0, 0($sp)       # restore arg0
1838    daddiu   $sp, $sp, 16      # remove args
1839    .cfi_adjust_cfa_offset -16
1840    RESTORE_REFS_AND_ARGS_CALLEE_SAVE_FRAME
1841    jalr     $t9               # call method
1842    nop
1843END art_quick_instrumentation_entry
1844    /* intentional fallthrough */
1845    .global art_quick_instrumentation_exit
1846art_quick_instrumentation_exit:
1847    .cfi_startproc
1848    SETUP_GP
1849    move     $ra, $zero        # link register is to here, so clobber with 0 for later checks
1850    SETUP_REFS_ONLY_CALLEE_SAVE_FRAME
1851    move     $t0, $sp          # remember bottom of caller's frame
1852    daddiu   $sp, $sp, -16     # save return values and set up args
1853    .cfi_adjust_cfa_offset 16
1854    sd       $v0, 0($sp)
1855    .cfi_rel_offset 2, 0
1856    s.d      $f0, 8($sp)
1857    mov.d    $f15, $f0         # pass fpr result
1858    move     $a2, $v0          # pass gpr result
1859    move     $a1, $t0          # pass $sp
1860    move     $a0, rSELF        # pass Thread::Current
1861    jal      artInstrumentationMethodExitFromCode  # (Thread*, SP, gpr_res, fpr_res)
1862    .cpreturn                  # Restore gp from t8 in branch delay slot. gp is not used anymore,
1863                               # and t8 may be clobbered in artInstrumentationMethodExitFromCode.
1864
1865    move     $t9, $v0          # set aside returned link register
1866    move     $ra, $v1          # set link register for deoptimization
1867    ld       $v0, 0($sp)       # restore return values
1868    l.d      $f0, 8($sp)
1869    jalr     $zero, $t9        # return
1870    daddiu   $sp, $sp, 16+FRAME_SIZE_REFS_ONLY_CALLEE_SAVE  # 16 bytes of saved values + ref_only callee save frame
1871    .cfi_adjust_cfa_offset -(16+FRAME_SIZE_REFS_ONLY_CALLEE_SAVE)
1872END art_quick_instrumentation_exit
1873
1874    /*
1875     * Instrumentation has requested that we deoptimize into the interpreter. The deoptimization
1876     * will long jump to the upcall with a special exception of -1.
1877     */
1878    .extern artDeoptimize
1879    .extern artEnterInterpreterFromDeoptimize
1880ENTRY art_quick_deoptimize
1881    SETUP_SAVE_ALL_CALLEE_SAVE_FRAME
1882    jal      artDeoptimize     # artDeoptimize(Thread*, SP)
1883                               # Returns caller method's frame size.
1884    move     $a0, rSELF        # pass Thread::current
1885END art_quick_deoptimize
1886
1887    /*
1888     * Compiled code has requested that we deoptimize into the interpreter. The deoptimization
1889     * will long jump to the upcall with a special exception of -1.
1890     */
1891    .extern artDeoptimizeFromCompiledCode
1892ENTRY art_quick_deoptimize_from_compiled_code
1893    SETUP_SAVE_ALL_CALLEE_SAVE_FRAME
1894    jal      artDeoptimizeFromCompiledCode    # artDeoptimizeFromCompiledCode(Thread*, SP)
1895                                              # Returns caller method's frame size.
1896    move     $a0, rSELF                       # pass Thread::current
1897END art_quick_deoptimize_from_compiled_code
1898
1899  .set push
1900  .set noat
1901/* java.lang.String.compareTo(String anotherString) */
1902ENTRY_NO_GP art_quick_string_compareto
1903/* $a0 holds address of "this" */
1904/* $a1 holds address of "anotherString" */
1905  beq    $a0,$a1,9f     # this and anotherString are the same object
1906  move   $v0,$zero
1907
1908  lw     $a2,MIRROR_STRING_COUNT_OFFSET($a0)    # this.length()
1909  lw     $a3,MIRROR_STRING_COUNT_OFFSET($a1)    # anotherString.length()
1910  MINu   $t2, $a2, $a3
1911# $t2 now holds min(this.length(),anotherString.length())
1912
1913  beqz   $t2,9f         # while min(this.length(),anotherString.length())-i != 0
1914  subu   $v0,$a2,$a3    # if $t2==0 return
1915                        #     (this.length() - anotherString.length())
19161:
1917  lhu    $t0,MIRROR_STRING_VALUE_OFFSET($a0)    # while this.charAt(i) == anotherString.charAt(i)
1918  lhu    $t1,MIRROR_STRING_VALUE_OFFSET($a1)
1919  bne    $t0,$t1,9f     # if this.charAt(i) != anotherString.charAt(i)
1920  subu   $v0,$t0,$t1    #     return (this.charAt(i) - anotherString.charAt(i))
1921  daddiu $a0,$a0,2      # point at this.charAt(i++)
1922  subu   $t2,$t2,1      # new value of
1923                        # min(this.length(),anotherString.length())-i
1924  bnez   $t2,1b
1925  daddiu $a1,$a1,2      # point at anotherString.charAt(i++)
1926  subu   $v0,$a2,$a3
1927
19289:
1929  j      $ra
1930  nop
1931END art_quick_string_compareto
1932
1933/* java.lang.String.indexOf(int ch, int fromIndex=0) */
1934ENTRY_NO_GP art_quick_indexof
1935/* $a0 holds address of "this" */
1936/* $a1 holds "ch" */
1937/* $a2 holds "fromIndex" */
1938  lw    $t0,MIRROR_STRING_COUNT_OFFSET($a0)     # this.length()
1939  slt   $at, $a2, $zero # if fromIndex < 0
1940  seleqz $a2, $a2, $at  #     fromIndex = 0;
1941  subu  $t0,$t0,$a2     # this.length() - fromIndex
1942  blez  $t0,6f          # if this.length()-fromIndex <= 0
1943  li    $v0,-1          #     return -1;
1944
1945  sll   $v0,$a2,1       # $a0 += $a2 * 2
1946  daddu $a0,$a0,$v0     #  "  "   "  " "
1947  move  $v0,$a2         # Set i to fromIndex.
1948
19491:
1950  lhu   $t3,MIRROR_STRING_VALUE_OFFSET($a0)     # if this.charAt(i) == ch
1951  beq   $t3,$a1,6f                              #     return i;
1952  daddu $a0,$a0,2       # i++
1953  subu  $t0,$t0,1       # this.length() - i
1954  bnez  $t0,1b          # while this.length() - i > 0
1955  addu  $v0,$v0,1       # i++
1956
1957  li    $v0,-1          # if this.length() - i <= 0
1958                        #     return -1;
1959
19606:
1961  j     $ra
1962  nop
1963END art_quick_indexof
1964
1965  .set pop
1966