* src/vm/jit/powerpc/linux/md-abi.c (md_return_alloc): Use
[cacao.git] / src / vm / jit / powerpc / asmpart.S
1 /* src/vm/jit/powerpc/asmpart.S - Java-C interface functions for PowerPC
2                 
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software.text;  you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation;  either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY ;  without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program;  if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25 */
26
27
28 #include "config.h"
29
30 #include "md-abi.h"
31 #include "md-asm.h"
32
33 #include "vm/jit/abi-asm.h"
34 #include "vm/jit/methodheader.h"
35
36
37         .text
38
39         .align 2
40
41
42 /* export functions ***********************************************************/
43
44         .globl asm_vm_call_method
45         .globl asm_vm_call_method_int
46         .globl asm_vm_call_method_long
47         .globl asm_vm_call_method_float
48         .globl asm_vm_call_method_double
49
50         .globl asm_vm_call_method_exception_handler
51         .globl asm_vm_call_method_end
52
53         .globl asm_handle_nat_exception
54         .globl asm_handle_exception
55
56         .globl asm_abstractmethoderror
57
58         .globl asm_cacheflush
59
60         .globl asm_compare_and_swap
61         .globl asm_memory_barrier
62
63
64 /* asm_vm_call_method **********************************************************
65 *                                                                              *
66 *   This function calls a Java-method (which possibly needs compilation)       *
67 *   with up to 4 address parameters.                                           *
68 *                                                                              *
69 *   This functions calls the JIT-compiler which eventually translates the      *
70 *   method into machine code.                                                  *
71 *                                                                              *
72 *   C-prototype:                                                               *
73 *    javaobject_header *asm_calljavamethod (methodinfo *m,                     *
74 *         void *arg1, void *arg2, void *arg3, void *arg4);                     *
75 *                                                                              *
76 *******************************************************************************/
77
78         .align 2
79
80         .long   0                         /* fltsave                              */
81         .long   0                         /* intsave                              */
82         .long   0                         /* IsLeaf                               */
83         .long   0                         /* frame size                           */
84         .long   0                         /* codeinfo pointer                     */
85
86 asm_vm_call_method:
87 asm_vm_call_method_int:
88 asm_vm_call_method_long:
89 asm_vm_call_method_float:
90 asm_vm_call_method_double:
91         mflr    r0
92         stw     r0,LA_LR_OFFSET(sp)
93         stwu    sp,-40*4(sp)              /* keep stack 16-byte aligned           */
94
95         stw     s0,8*4(sp)                /* save used callee saved registers     */
96
97 #if defined(__DARWIN__)
98         stw     itmp1,10*4(sp)            /* register r11 is callee saved         */
99 #endif
100         stw     pv,11*4(sp)               /* save PV register                     */
101
102         stw     itmp3,12*4(sp)            /* registers r14-r31 are callee saved   */
103         stfd    ftmp1,14*4(sp)            /* registers f14-f31 are callee saved   */
104         stfd    ftmp2,16*4(sp)
105
106 #if defined(__DARWIN__)
107         stw     t1,18*4(sp)
108         stw     t2,19*4(sp)
109         stw     t3,20*4(sp)
110         stw     t4,21*4(sp)
111         stw     t5,22*4(sp)
112         stw     t6,23*4(sp)
113         stw     t7,24*4(sp)
114
115         stfd    ft0,26*4(sp)
116         stfd    ft1,28*4(sp)
117         stfd    ft2,30*4(sp)
118         stfd    ft3,32*4(sp)
119         stfd    ft4,34*4(sp)
120         stfd    ft5,36*4(sp)
121 #else
122         SAVE_TEMPORARY_REGISTERS(18)        /* the offset has to be even          */
123 #endif
124
125         mr      pv,a0                       /* move PV into PV register           */
126         mtctr   pv                          /* move PV into branch register       */
127
128         mr      t0,a1                       /* address of data structure          */
129         mr      t1,a2                       /* stack argument count               */
130
131         mr      s0,sp                       /* save SP                            */
132
133         lwz     a0,0*8+4(t0)                /* we are on big-endian               */
134         lwz     a1,1*8+4(t0)
135         lwz     a2,2*8+4(t0)
136         lwz     a3,3*8+4(t0)
137         lwz     a4,4*8+4(t0)
138         lwz     a5,5*8+4(t0)
139         lwz     a6,6*8+4(t0)
140         lwz     a7,7*8+4(t0)
141
142         lfd     fa0,8*8(t0)
143         lfd     fa1,9*8(t0)
144         lfd     fa2,10*8(t0)
145         lfd     fa3,11*8(t0)
146         lfd     fa4,12*8(t0)
147         lfd     fa5,13*8(t0)
148         lfd     fa6,14*8(t0)
149         lfd     fa7,15*8(t0)
150
151 #if defined(__DARWIN__)
152         lfd     fa8,16*8(t0)
153         lfd     fa9,17*8(t0)
154         lfd     fa10,18*8(t0)
155         lfd     fa11,19*8(t0)
156         lfd     fa12,20*8(t0)
157 #endif
158
159         mr.     t1,t1
160         beq     L_asm_vm_call_method_stack_copy_done
161
162         slwi    t2,t1,3                     /* calculate stackframe size (* 8)    */
163
164         sub     sp,sp,t2                    /* create stackframe                  */
165         mr      t2,sp                       /* temporary stack pointer            */
166
167 L_asm_vm_call_method_stack_copy_loop:
168 #if defined(__DARWIN__)
169         lwz     t3,21*8+0(t0)               /* load argument                      */
170         lwz     t4,21*8+4(t0)
171 #else
172         lwz     t3,16*8+0(t0)               /* load argument                      */
173         lwz     t4,16*8+4(t0)
174 #endif
175         stw     t3,0(t2)                    /* store argument on stack            */
176         stw     t4,4(t2)
177
178         addi    t0,t0,8                     /* load address of next argument      */
179         addi    t2,t2,8                     /* increase stack pointer             */
180         addi    t1,t1,-1                    /* subtract 1 argument                */
181         mr.     t1,t1
182         bgt     L_asm_vm_call_method_stack_copy_loop
183
184 L_asm_vm_call_method_stack_copy_done:
185         bctrl
186 1:
187         mflr    itmp1
188 #if defined(__DARWIN__)
189         addi    pv,itmp1,lo16(asm_vm_call_method - 1b)
190 #else
191         addi    pv,itmp1,(asm_vm_call_method - 1b)@l
192 #endif
193
194 L_asm_vm_call_method_return:
195         mr      sp,s0                       /* restore the SP                     */
196
197         lwz     s0,8*4(sp)                  /* restore used callee saved registers*/
198
199 #if defined(__DARWIN__)
200         lwz     itmp1,10*4(sp)              /* register r11 is callee saved       */
201 #endif
202         lwz     pv,11*4(sp)                 /* save PV register                   */
203
204         lwz     itmp3,12*4(sp)
205         lfd     ftmp1,14*4(sp)              /* registers f14-f31 are callee saved */
206         lfd     ftmp2,16*4(sp)
207
208 #if defined(__DARWIN__)
209         lwz     t1,18*4(sp)
210         lwz     t2,19*4(sp)
211         lwz     t3,20*4(sp)
212         lwz     t4,21*4(sp)
213         lwz     t5,22*4(sp)
214         lwz     t6,23*4(sp)
215         lwz     t7,24*4(sp)
216
217         lfd     ft0,26*4(sp)
218         lfd     ft1,28*4(sp)
219         lfd     ft2,30*4(sp)
220         lfd     ft3,32*4(sp)
221         lfd     ft4,34*4(sp)
222         lfd     ft5,36*4(sp)
223 #else
224         RESTORE_TEMPORARY_REGISTERS(18)   /* the offset has to be even            */
225 #endif
226
227         lwz     r0,40*4+LA_LR_OFFSET(sp)
228         mtlr    r0
229         addi    sp,sp,40*4
230         blr
231
232 asm_vm_call_method_exception_handler:
233         mr      a0,itmp1
234         bl      builtin_throw_exception
235         b       L_asm_vm_call_method_return
236
237 asm_vm_call_method_end:
238         nop
239
240
241 /********************* function asm_handle_exception ***************************
242 *                                                                              *
243 *   This function handles an exception. It does not use the usual calling      *
244 *   conventions. The exception pointer is passed in REG_ITMP1 and the          *
245 *   pc from the exception raising position is passed in REG_ITMP2. It searches *
246 *   the local exception table for a handler. If no one is found, it unwinds    *
247 *   stacks and continues searching the callers.                                *
248 *                                                                              *
249 *   void asm_handle_exception (exceptionptr, exceptionpc);                     *
250 *                                                                              *
251 *******************************************************************************/
252                 
253 asm_handle_nat_exception:
254 L_asm_handle_nat_exception:             /* required for PIC code              */
255 L_asm_handle_exception_stack_loop:
256         mflr    r0
257         addi    sp,sp,-(LA_SIZE+((4+6)*4))  /* allocate stack (+4 for darwin)     */
258         stw     xptr,LA_SIZE+(4+0)*4(sp)    /* save exception pointer             */
259         stw     xpc,LA_SIZE+(4+1)*4(sp)     /* save exception pc                  */
260         stw     r0,LA_SIZE+(4+3)*4(sp)      /* save return address                */
261         li      itmp3,0
262         stw     itmp3,LA_SIZE+(4+4)*4(sp)   /* save maybe-leaf flag (cleared)     */
263
264         mr      a0,r0                       /* pass return address                */
265         bl      md_asm_codegen_get_pv_from_pc /* get PV from RA                   */
266         stw     v0,LA_SIZE+(4+2)*4(sp)      /* save data segment pointer          */
267
268         lwz     a0,LA_SIZE+(4+0)*4(sp)      /* pass xptr                          */
269         lwz     a1,LA_SIZE+(4+1)*4(sp)      /* pass xpc                           */
270         lwz     a2,LA_SIZE+(4+2)*4(sp)      /* pass PV (v0 == a0)                 */
271         addi    a3,sp,LA_SIZE+((4+6)*4)     /* pass Java SP                       */
272
273         b       L_asm_handle_exception_continue
274
275 asm_handle_exception:
276 L_asm_handle_exception:                 /* required for PIC code              */
277         addi    sp,sp,-(ARG_CNT+TMP_CNT)*8  /* create maybe-leaf stackframe       */
278
279 #if defined(__DARWIN__)
280 #else
281         SAVE_ARGUMENT_REGISTERS(0)          /* we save arg and temp registers in  */
282         SAVE_TEMPORARY_REGISTERS(ARG_CNT)   /* case this is a leaf method         */
283 #endif
284
285         addi    sp,sp,-(LA_SIZE+(4+6)*4)    /* allocate stack                     */
286         stw     xptr,LA_SIZE+(4+0)*4(sp)    /* save xptr                          */
287         stw     pv,LA_SIZE+(4+2)*4(sp)      /* save PV                            */
288         mflr    r0                          /* save RA                            */
289         stw     r0,LA_SIZE+(4+3)*4(sp)
290         li      t0,1                        /* set maybe-leaf flag                */
291         stw     t0,LA_SIZE+(4+4)*4(sp)      /* save maybe-leaf flag               */
292
293         mr      a0,xptr                     /* pass exception pointer             */
294         mr      a1,xpc                      /* pass exception pc                  */
295         mr      a2,pv                       /* pass data segment pointer          */
296         addi    a3,sp,LA_SIZE+(ARG_CNT+TMP_CNT)*8+(4+6)*4
297
298 L_asm_handle_exception_continue:
299         bl      exceptions_handle_exception
300
301         mr.     v0,v0
302         beq     L_asm_handle_exception_not_catched
303
304         mr      xpc,v0                      /* move handlerpc into xpc            */
305         lwz     xptr,LA_SIZE+(4+0)*4(sp)    /* restore xptr                       */
306         lwz     pv,LA_SIZE+(4+2)*4(sp)      /* restore PV                         */
307         lwz     r0,LA_SIZE+(4+3)*4(sp)      /* restore RA                         */
308         mtlr    r0
309         lwz     t0,LA_SIZE+(4+4)*4(sp)      /* get maybe-leaf flag                */
310         addi    sp,sp,LA_SIZE+(4+6)*4       /* free stack frame                   */
311
312         mr.     t0,t0
313         beq     L_asm_handle_exception_no_leaf
314
315 #if defined(__DARWIN__)
316 #else
317         RESTORE_ARGUMENT_REGISTERS(0)       /* if this is a leaf method, we have  */
318         RESTORE_TEMPORARY_REGISTERS(ARG_CNT)/* to restore arg and temp registers  */
319 #endif
320
321         addi    sp,sp,(ARG_CNT+TMP_CNT)*8   /* remove maybe-leaf stackframe       */
322
323 L_asm_handle_exception_no_leaf:
324         mtctr   xpc                         /* jump to the handler                */
325         bctr
326
327 L_asm_handle_exception_not_catched:
328         lwz     xptr,LA_SIZE+(4+0)*4(sp)    /* restore xptr                       */
329         lwz     pv,LA_SIZE+(4+2)*4(sp)      /* restore PV                         */
330         lwz     r0,LA_SIZE+(4+3)*4(sp)      /* restore RA                         */
331         mtlr    r0
332         lwz     t0,LA_SIZE+(4+4)*4(sp)      /* get maybe-leaf flag                */
333         addi    sp,sp,LA_SIZE+(4+6)*4       /* free stack frame                   */
334
335         mr.     t0,t0
336         beq     L_asm_handle_exception_no_leaf_stack
337
338         addi    sp,sp,(ARG_CNT+TMP_CNT)*8   /* remove maybe-leaf stackframe       */
339         li      t0,0                        /* clear the maybe-leaf flag          */
340
341 L_asm_handle_exception_no_leaf_stack:
342         lwz     t1,FrameSize(pv)            /* get frame size                     */
343         add     t1,sp,t1                    /* pointer to save area               */
344
345         lwz     t2,IsLeaf(pv)               /* is leaf procedure                  */
346         mr.     t2,t2
347         bne     L_asm_handle_exception_no_ra_restore
348
349         lwz     r0,LA_LR_OFFSET(t1)         /* restore ra                         */
350         mtlr    r0
351
352 L_asm_handle_exception_no_ra_restore:
353         mflr    xpc                         /* the new xpc is ra                  */
354         mr      t4,xpc                      /* save RA                            */
355         lwz     t2,IntSave(pv)              /* t2 = saved int register count      */
356         bl      ex_int1
357 ex_int1:
358         mflr    t3                          /* t3 = current pc                    */
359 #if defined(__DARWIN__)
360         addi    t3,t3,lo16(ex_int2-ex_int1)
361 #else
362         addi    t3,t3,(ex_int2-ex_int1)@l
363 #endif
364         slwi    t2,t2,2                     /* t2 = register count * 4            */
365         subf    t3,t2,t3                    /* t3 = IntSave - t2                  */
366         mtctr   t3
367         bctr
368
369         lwz     s0,-10*8(t1)
370         lwz     s1,-9*8(t1)
371         lwz     s2,-8*8(t1)
372         lwz     s3,-7*8(t1)
373         lwz     s4,-6*8(t1)
374         lwz     s5,-5*8(t1)
375         lwz     s6,-4*8(t1)
376         lwz     s7,-3*8(t1)
377         lwz     s8,-2*8(t1)
378         lwz     s9,-1*8(t1)
379
380 ex_int2:
381         subf    t1,t2,t1                    /* t1 = t1 - register count * 4       */
382
383         lwz     t2,FltSave(pv)
384         bl      ex_flt1
385 ex_flt1:
386         mflr    t3
387 #if defined(__DARWIN__)
388         addi    t3,t3,lo16(ex_flt2-ex_flt1)
389 #else
390         addi    t3,t3,(ex_flt2-ex_flt1)@l
391 #endif
392         slwi    t2,t2,2                     /* t2 = register count * 4            */
393         subf    t3,t2,t3                    /* t3 = FltSave - t2                  */
394         mtctr   t3
395         bctr
396
397         lfd     fs0,-10*8(t1)
398         lfd     fs1,-9*8(t1)
399         lfd     fs2,-8*8(t1)
400         lfd     fs3,-7*8(t1)
401         lfd     fs4,-6*8(t1)
402         lfd     fs5,-5*8(t1)
403         lfd     fs6,-4*8(t1)
404         lfd     fs7,-3*8(t1)
405         lfd     fs8,-2*8(t1)
406         lfd     fs9,-1*8(t1)
407
408 ex_flt2:
409         mtlr    t4                          /* restore RA                         */
410         lwz     t1,FrameSize(pv)            /* get frame size                     */
411         add     sp,sp,t1                    /* unwind stack                       */
412         b       L_asm_handle_exception_stack_loop
413
414
415 /* asm_abstractmethoderror *****************************************************
416
417    Creates and throws an AbstractMethodError.
418
419 *******************************************************************************/
420
421 asm_abstractmethoderror:
422         mflr    r0
423         stw     r0,LA_LR_OFFSET(sp)
424         stwu    sp,-LA_SIZE_ALIGNED(sp)     /* preserve linkage area              */
425         addi    a0,sp,LA_SIZE_ALIGNED       /* pass java sp                       */
426         mr      a1,r0                       /* pass exception address             */
427         bl      exceptions_asm_new_abstractmethoderror
428         lwz     r0,LA_SIZE_ALIGNED+LA_LR_OFFSET(sp)
429         mtlr    r0                          /* restore return address             */
430         addi    sp,sp,LA_SIZE_ALIGNED
431
432         mr      xptr,v0                     /* get exception pointer              */
433         mr      xpc,r0                      /* we can't use r0 directly in addi   */
434         addi    xpc,xpc,-4                  /* exception address is ra - 4        */
435         b       L_asm_handle_nat_exception
436
437
438 /*********************************************************************/
439
440 asm_cacheflush:
441         add     a1,a0,a1
442         rlwinm  a0,a0,0,0,26
443         addi    a1,a1,31
444         rlwinm  a1,a1,0,0,26
445         mr      a2,a0
446 1:
447         cmplw   a0,a1
448         bge     0f
449         dcbst   0,a0
450         addi    a0,a0,32
451         b       1b
452 0:
453         sync
454 1:
455         cmplw   a2,a1
456         bge     0f
457         icbi    0,a2
458         addi    a2,a2,32
459         b       1b
460 0:
461         sync
462         isync
463         blr
464
465
466 /* asm_compare_and_swap ********************************************************
467
468    XXX
469
470 *******************************************************************************/
471
472 asm_compare_and_swap:
473 1:
474         lwarx   a6,0,a0 
475         subf.   r0,a6,a1 
476         bne-    2f 
477         or      r0,a2,a2 
478         stwcx.  r0,0,a0 
479         bne-    1b 
480 2: 
481         mr      a0,a6
482         blr
483
484
485 /* asm_memory_barrier **********************************************************
486
487    XXX
488
489 *******************************************************************************/
490
491 asm_memory_barrier:
492         sync
493         blr
494
495
496 #if defined(__DARWIN__)
497
498 .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
499         .align 2
500 L_builtin_throw_exception$stub:
501         .indirect_symbol _builtin_throw_exception
502         mflr r0
503         bcl 20,31,L00$_builtin_throw_exception
504 L00$_builtin_throw_exception:
505         mflr r11
506         addis r11,r11,ha16(L_builtin_throw_exception$lazy_ptr - L00$_builtin_throw_exception)
507         mtlr r0
508         lwzu r12,lo16(L_builtin_throw_exception$lazy_ptr - L00$_builtin_throw_exception)(r11)
509         mtctr r12
510         bctr
511 .data
512 .lazy_symbol_pointer
513 L_builtin_throw_exception$lazy_ptr:
514         .indirect_symbol _builtin_throw_exception
515         .long dyld_stub_binding_helper
516
517
518 .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
519         .align 2
520 L_md_asm_codegen_get_pv_from_pc$stub:
521         .indirect_symbol _md_asm_codegen_get_pv_from_pc
522         mflr r0
523         bcl 20,31,L00$_md_asm_codegen_get_pv_from_pc
524 L00$_md_asm_codegen_get_pv_from_pc:
525         mflr r11
526         addis r11,r11,ha16(L_md_asm_codegen_get_pv_from_pc$lazy_ptr - L00$_md_asm_codegen_get_pv_from_pc)
527         mtlr r0
528         lwzu r12,lo16(L_md_asm_codegen_get_pv_from_pc$lazy_ptr - L00$_md_asm_codegen_get_pv_from_pc)(r11)
529         mtctr r12
530         bctr
531 .data
532 .lazy_symbol_pointer
533 L_md_asm_codegen_get_pv_from_pc$lazy_ptr:
534         .indirect_symbol _md_asm_codegen_get_pv_from_pc
535         .long dyld_stub_binding_helper
536
537
538 .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
539         .align 2
540 L_exceptions_handle_exception$stub:
541         .indirect_symbol _exceptions_handle_exception
542         mflr r0
543         bcl 20,31,L00$_exceptions_handle_exception
544 L00$_exceptions_handle_exception:
545         mflr r11
546         addis r11,r11,ha16(L_exceptions_handle_exception$lazy_ptr - L00$_exceptions_handle_exception)
547         mtlr r0
548         lwzu r12,lo16(L_exceptions_handle_exception$lazy_ptr - L00$_exceptions_handle_exception)(r11)
549         mtctr r12
550         bctr
551 .data
552 .lazy_symbol_pointer
553 L_exceptions_handle_exception$lazy_ptr:
554         .indirect_symbol _exceptions_handle_exception
555         .long dyld_stub_binding_helper
556
557
558 .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
559         .align 2
560 L_exceptions_asm_new_abstractmethoderror$stub:
561         .indirect_symbol _exceptions_asm_new_abstractmethoderror
562         mflr r0
563         bcl 20,31,L00$_exceptions_asm_new_abstractmethoderror
564 L00$_exceptions_asm_new_abstractmethoderror:
565         mflr r11
566         addis r11,r11,ha16(L_exceptions_asm_new_abstractmethoderror$lazy_ptr - L00$_exceptions_asm_new_abstractmethoderror)
567         mtlr r0
568         lwzu r12,lo16(L_exceptions_asm_new_abstractmethoderror$lazy_ptr - L00$_exceptions_asm_new_abstractmethoderror)(r11)
569         mtctr r12
570         bctr
571 .data
572 .lazy_symbol_pointer
573 L_exceptions_asm_new_abstractmethoderror$lazy_ptr:
574         .indirect_symbol _exceptions_asm_new_abstractmethoderror
575         .long dyld_stub_binding_helper
576
577 #endif /* defined(__DARWIN__) */
578
579
580 /* disable exec-stacks ********************************************************/
581
582 #if defined(__linux__) && defined(__ELF__)
583         .section .note.GNU-stack,"",%progbits
584 #endif
585
586
587 /*
588  * These are local overrides for various environment variables in Emacs.
589  * Please do not remove this and leave it at the end of the file, where
590  * Emacs will automagically detect them.
591  * ---------------------------------------------------------------------
592  * Local variables:
593  * mode: asm
594  * indent-tabs-mode: t
595  * c-basic-offset: 4
596  * tab-width: 4
597  * End:
598  * vim:noexpandtab:sw=4:ts=4:
599  */