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