3afa0d92dd72bee69c80824c63dd8a2895f86192
[cacao.git] / src / vm / jit / sparc64 / asmpart.S
1 /* src/vm/jit/sparc64/asmpart.S - Java-C interface functions for Sparc64
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; 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    $Id: asmpart.S 4749 2006-04-11 10:20:18Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include "vm/jit/sparc64/md-abi.h"
33 #include "offsets.h"
34 #include "md-asm.h"
35
36         .register %g2,#scratch                         /* define as scratch       */
37         .register %g3,#scratch                    /* XXX  reserve for application */
38         .text
39
40 /* export functions ***********************************************************/
41
42         .global asm_vm_call_method        
43         .global asm_vm_call_method_int    
44         .global asm_vm_call_method_long   
45         .global asm_vm_call_method_float  
46         .global asm_vm_call_method_double 
47         .global asm_vm_call_method_exception_handler
48         .global asm_vm_call_method_end
49
50         .global asm_call_jit_compiler
51
52         .global asm_handle_exception
53         .global asm_handle_nat_exception
54
55         .global asm_patcher_wrapper
56
57         .global asm_abstractmethoderror
58         
59         .global asm_criticalsections
60         .global asm_getclassvalues_atomic
61         
62         .global asm_store_fp_state_reg
63         .global asm_load_fp_state_reg
64
65
66 /* asm_vm_call_method ******************************************************
67  *                                                                         *
68  * This function calls a Java-method (which possibly needs compilation)    *
69  *
70  * C-prototype:
71  *  java_objectheader *asm_vm_call_method(methodinfo *m, s4 vmargscount,
72  *                                               vm_arg *vmargs);
73  **************************************************************************/
74
75         .align 8        /* v9: All data types are aligned to their size */
76
77         .xword 0                         /* catch type all */
78         .xword 0                         /* handler pc */
79         .xword 0                         /* end pc */
80         .xword 0                         /* start pc */
81         .word  1                         /* extable size */
82         .word  0                         /* ALIGNMENT PADDING */
83         .xword 0                         /* line number table start */
84         .xword 0                         /* line number table size */
85         .word  0                         /* ALIGNMENT PADDING */
86         .word  0                         /* fltsave */
87         .word  1                         /* intsave */
88         .word  0                         /* isleaf */
89         .word  0                         /* IsSync */
90         .word  0                         /* frame size */
91         .xword 0                         /* method pointer (pointer to name)*/ 
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         
99         save %sp,-144,%sp                                /* 16 reg-save + 2 */
100
101
102         /* %i2 not needed after _nocopy -> calculate in branch delay */
103
104         brlez %i1, calljava_nocopy
105         
106         dec %i1   /* branch delay */
107         ldx [%i2 + offvmargdata],%o0
108         ldd [%i2 + offvmargdata],fa0
109         brlez %i1,calljava_nocopy
110
111         dec %i1   /* branch delay */
112         ldx [%i2 + (offvmargdata+sizevmarg*1)],%o1
113         ldd [%i2 + (offvmargdata+sizevmarg*1)],fa1
114         brlez %i1,calljava_nocopy
115
116         dec %i1
117         ldx [%i2 + (offvmargdata+sizevmarg*2)],%o2
118         ldd [%i2 + (offvmargdata+sizevmarg*2)],fa2
119         brlez %i1,calljava_nocopy
120
121         dec %i1
122         ldx [%i2 + (offvmargdata+sizevmarg*3)],%o3
123         ldd [%i2 + (offvmargdata+sizevmarg*3)],fa3
124         brlez %i1,calljava_nocopy
125
126         dec %i1
127         ldx [%i2 + (offvmargdata+sizevmarg*4)],%o4
128         ldd [%i2 + (offvmargdata+sizevmarg*4)],fa4
129
130 calljava_argsloaded:    
131         brlez %i1, calljava_nocopy
132         nop
133         sllx %i1,3,%l0                     /* remaining args * 8           */
134         add %sp,16*8+bias,%l1              /* right above window save area */
135         sub %sp,%l0,%sp                    /* allocate more stack space    */
136         
137 calljava_copyloop:
138         ldx [%i2 + (offvmargdata+sizevmarg*5)],%l0
139         stx %l0,[%l1]
140         inc sizevmarg,%i2                  /* src++                        */
141         subcc %i1,1,%i1                    /* arg_count--                  */
142         bnz %xcc, calljava_copyloop        /* use cc from previous instr   */
143         inc 8,%l1                          /* dst++ (delay)                */
144         
145
146 calljava_nocopy:
147         /* set pv, like a java method does */
148         /*      add  ra_caller,(asm_vm_call_method - calljava_nocopy + 8),pv_callee */
149         setx  asm_vm_call_method,%l0,pv_callee
150         
151         mov   %i0,itmp1         /* pass method info pointer via itmp1 */
152         
153         setx  asm_call_jit_compiler,%l0,mptr_itmp2  /* fake virtual function call (2 instr) */
154         stx   mptr_itmp2,[%sp + 2047 + 17*8]        /* store function address               */
155         add   %sp,2047 + 16*8,mptr_itmp2            /* set method pointer                   */
156         
157         ldx  [1*8 + mptr_itmp2], pv_caller          /* method call as in Java               */
158         jmpl pv_caller,ra_caller                      /* call JIT compiler          */
159         nop
160 calljava_jit2:
161         /* pretend to restore pv */
162         add  ra_caller,(asm_vm_call_method - calljava_jit2 + 8),zero
163         
164 calljava_return:
165         mov %o0, %i0            /* pass on the return value     */
166         return %i7 + 8          /* implicit window restore */
167         nop
168
169
170 asm_vm_call_method_exception_handler:
171         
172         /* so far this function did not call any c functions */
173         /* but now we need ABI compliant argslots on the stack */
174         sub   %sp,6*8,%sp
175         
176         mov             xptr_itmp2,%o0
177         call    builtin_throw_exception
178         nop
179         return  %i7 + 8                          /* implicit window restore */
180 asm_vm_call_method_end:
181         nop
182         
183
184
185 /****************** function asm_call_jit_compiler *****************************
186 *                                                                              *
187 *   invokes the compiler for untranslated JavaVM methods.                      *
188 *                                                                              *
189 *   Register R0 contains a pointer to the method info structure (prepared      *
190 *   by createcompilerstub). Using the return address in R26 and the            *
191 *   offset in the LDA instruction or using the value in methodptr R28 the      *
192 *   patching address for storing the method address can be computed:           *
193 *                                                                              *
194 *   method address was either loaded using                                     *
195 *   M_LDQ (REG_PV, REG_PV, a)        ; invokestatic/special    ($27)           *
196 *   M_LDA (REG_PV, REG_RA, low)                                                *
197 *   M_LDAH(REG_PV, REG_RA, high)     ; optional                                *
198 *   or                                                                         *
199 *   M_LDQ (REG_PV, REG_METHODPTR, m) ; invokevirtual/interface ($28)           *
200 *   in the static case the method pointer can be computed using the            *
201 *   return address and the lda function following the jmp instruction          *
202 *                                                                              *
203 *******************************************************************************/
204
205 asm_call_jit_compiler:
206         
207         /* stacksave for regsave(16) + argslots(6) + float args  */
208         save    %sp,-((16+6+FLT_ARG_CNT)*8),%sp
209
210         SAVE_FLOAT_ARGUMENT_REGISTERS(22)
211
212         mov     itmp1,%o0             /* pass methodinfo pointer                  */
213         mov     mptr_itmp2,%o1        /* pass method pointer                      */
214         mov     %fp,%o2                           /* pass java sp (==fp)                          */
215         mov     ra_callee,%o3         /* pass Java ra                             */
216         mov     %o3,%o4               /* xpc is equal to ra                       */
217         call    jit_asm_compile       /* call jit compiler                        */
218         nop
219         
220         RESTORE_FLOAT_ARGUMENT_REGISTERS(22)
221
222         brz     %o0,L_asm_call_jit_compiler_exception
223         nop
224         
225         restore %o0,%g0,pv_caller     /* restore the callers window               */
226                                       /* the source o0 references the old window  */
227                                       /* pv_caller references the new window      */
228
229
230         /* synchronise instruction cache moved somewhere else           */
231
232         jmpl    pv_caller,zero        /* and call method, the method returns      */
233                                       /* directly to the caller (ra).             */
234         nop     
235         
236 L_asm_call_jit_compiler_exception:
237         /* window still open, ra_callee valid, pv_callee undefined      */
238
239         call    exceptions_get_and_clear_exception
240         nop
241
242         mov     %o0,xptr_itmp2        /* get exception                            */
243         mov     ra_callee,xpc_itmp3  /* exception address is address of call      */
244
245         /* restore the window of the calling function */
246         restore
247
248         b      L_asm_handle_nat_exception
249         nop
250
251
252
253 /* asm_handle_exception ********************************************************
254
255    This function handles an exception. It does not use the usual calling
256    conventions. The exception pointer is passed in REG_ITMP2 and the
257    pc from the exception raising position is passed in REG_ITMP3. It searches
258    the local exception table for a handler. If no one is found, it unwinds
259    stacks and continues searching the callers.
260
261 *******************************************************************************/
262
263
264 asm_handle_nat_exception:
265 L_asm_handle_nat_exception:       /* required for PIC code                    */
266 L_asm_handle_exception_stack_loop:
267         /* exception handling assumes that the current java method saved       */
268         /* the caller's window, and has a valid pv                             */
269
270         /* get ra and pv before saving the window */
271         mov     ra_callee,itmp1
272         mov     pv_callee,%g4
273
274         save    %sp,-176,%sp
275         mov     xptr_itmp2,%l0              /* save exception pointer             */
276         mov     xpc_itmp3,%l1               /* save exception pc                  */
277         mov     zero,%l2                    /* save maybe-leaf flag (cleared)     */
278
279         mov     %l0,%o0                     /* pass xptr                          */
280         mov     %l1,%o1                     /* pass xpc                           */
281         mov     %g4,%o2                     /* pass PV                            */
282         mov     %fp,%o3                     /* pass Java SP                       */
283
284         b       L_asm_handle_exception_continue
285         nop
286
287 asm_handle_exception:
288         mov     pv_callee,%g4
289
290         /* save bigger stack frame for float args and temps */
291         save    %sp,-((FLT_ARG_CNT+FLT_TMP_CNT+CSTACK_CNT)*8),%sp
292
293         SAVE_FLOAT_ARGUMENT_REGISTERS(CSTACK_CNT)
294         SAVE_FLOAT_TEMPORARY_REGISTERS(CSTACK_CNT+FLT_ARG_CNT)
295
296         mov     xptr_itmp2,%l0              /* save exception pointer             */
297         add     zero,1,%l2                  /* set maybe-leaf flag                */
298
299         mov     %l0,%o0                     /* pass xptr                          */
300         mov     xpc_itmp3,%o1               /* pass xpc                           */
301         mov     %g4,%o2                     /* pass PV                            */
302         mov     %fp,%o3                     /* pass Java SP                       */
303
304 L_asm_handle_exception_continue:
305         call    exceptions_handle_exception
306         nop
307
308         brz     %o0,L_asm_handle_exception_not_caught
309         nop
310
311         mov     %o0,xpc_itmp3               /* move handlerpc into xpc            */
312         mov     %l0,xptr_itmp2              /* restore exception pointer          */
313
314         brz     %l2,L_asm_handle_exception_no_leaf
315         nop
316
317         RESTORE_FLOAT_ARGUMENT_REGISTERS(CSTACK_CNT)
318         RESTORE_FLOAT_TEMPORARY_REGISTERS(CSTACK_CNT+FLT_ARG_CNT)
319         
320 L_asm_handle_exception_no_leaf:
321         /* restore java window and stackframe (ra and pv are in there) */
322         restore
323         jmpl    xpc_itmp3, zero             /* jump to the handler                */
324         nop
325
326 L_asm_handle_exception_not_caught:
327         mov     %l0,xptr_itmp2              /* restore xptr                       */
328         restore                             /* free our stackframe and window     */
329         /* maybe leaf flag gets cleared after branch to _loop */
330         
331         restore                             /* unwind stack and window            */
332         ba      L_asm_handle_exception_stack_loop
333         mov     ra_caller,xpc_itmp3         /* the new xpc is ra (delay)          */
334         
335
336
337
338 /* asm_abstractmethoderror *****************************************************
339
340    Creates and throws an AbstractMethodError.
341
342 *******************************************************************************/
343
344 asm_abstractmethoderror:
345         /* do a window save */
346         save %sp,-192,%sp
347
348         mov     %fp,%o0                     /* pass java sp(==fp)                 */
349         mov     ra_callee,%o1               /* pass exception address             */
350         call    exceptions_asm_new_abstractmethoderror
351         nop
352
353         mov     %o0,xptr_itmp2              /* get exception pointer              */
354         sub     ra_callee,4,xpc_itmp3       /* exception address is ra - 4        */
355         ba      L_asm_handle_nat_exception
356         nop
357
358         /* XXX: leave the register window open for handle_exception ??? */
359
360 /* asm_patcher_wrapper *********************************************************
361
362    XXX
363
364    Stack layout, when called from patcher stub
365      40   return address into JIT code (patch position)
366      32   pointer to virtual java_objectheader
367      24   machine code (which is patched back later)
368      16   unresolved class/method/field reference
369       8   data segment displacement from load instructions
370       0   patcher function pointer to call
371    -128   WINSAVE REGS (current SP)
372
373 *******************************************************************************/
374                 
375
376 asm_patcher_wrapper:
377         /* get pv and ra, since the emit code is not passing it on */
378         mov     ra_callee,ra_caller
379         mov     pv_callee,pv_caller
380
381         /* create window and stack frame              */
382         save  %sp,-((FLT_ARG_CNT+FLT_TMP_CNT+CSTACK_CNT+4)*8),%sp
383
384         SAVE_FLOAT_ARGUMENT_REGISTERS(CSTACK_CNT)
385         SAVE_FLOAT_TEMPORARY_REGISTERS(CSTACK_CNT+FLT_ARG_CNT)
386
387         mov     itmp1,%l0               /* save itmp1                             */
388         mov     itmp2,%l1               /* save itmp2                             */
389
390         add     %fp,JITSTACK,%o0      /* pass pseudo SP                           */
391         mov     pv_callee,%o1         /* pass PV                                  */
392         mov     ra_callee,%o2         /* pass RA (correct for leafs)              */
393         call    patcher_wrapper
394         nop
395
396         RESTORE_FLOAT_ARGUMENT_REGISTERS(CSTACK_CNT)
397         RESTORE_FLOAT_TEMPORARY_REGISTERS(CSTACK_CNT+FLT_ARG_CNT)
398
399         mov     %l0,itmp1               /* restore itmp1                          */
400         mov     %l1,itmp2               /* restore itmp2                          */
401
402         brnz    %o0,L_asm_patcher_wrapper_exception
403         nop
404
405         /* load RA (patch position from patcher data on the stack */
406         ldx     [%fp+JITSTACK+5*8],itmp3
407
408         /* remove window and stack frame (and stack space allocated in the stub code */
409         restore  %fp,6*8,%sp /* (source regs refer to old window, rd to new window)  */
410
411         jmpl     itmp3,zero              /* jump to newly patched code               */
412         nop
413
414 L_asm_patcher_wrapper_exception:
415         mov      %o0,xptr_itmp2        /* get exception                            */
416         ldx      [%fp+JITSTACK+5*8],xpc_itmp3 /* xpc is RA                         */
417         restore  %fp,6*8,%sp           /* remove stack frame                       */
418         ba       asm_handle_exception
419         nop
420
421
422 asm_getclassvalues_atomic:
423 _crit_restart:
424 _crit_begin:
425 /* not doing a window save, using the global temporary registers */
426         ldsw    [offbaseval+%o0],itmp1
427         ldsw    [offdiffval+%o0],itmp2
428         ldsw    [offbaseval+%o1],itmp3
429 _crit_end:
430         stw     itmp1,[offcast_super_baseval+%o2]
431         stw     itmp2,[offcast_super_diffval+%o2]
432         stw     itmp3,[offcast_sub_baseval+%o2]
433         jmpl    ra_caller+8,zero  /* caller's ra, b/c no window save */
434         nop
435
436
437     .data
438
439 asm_criticalsections:
440 #if defined(ENABLE_THREADS)
441     .xword   _crit_begin
442     .xword   _crit_end
443     .xword   _crit_restart
444 #endif
445     .xword   0
446     
447     
448 /* asm_store_fp_state_reg **************************************************
449  *                                                                         *
450  * This function stores the 64-bit floating point state register to a      *
451  * memory location. (which needs to be 8-byte aligned)                     *
452  *                                                                         *
453  * C-prototype:                                                            *
454  *  void asm_store_fp_state_reg(u8 *mem);                                  *
455  *                                                                             *
456  **************************************************************************/
457  
458 asm_store_fp_state_reg:
459         stx %fsr,[%o0]
460         retl  /* return from leaf */
461         nop
462         
463 /* asm_load_fp_state_reg ***************************************************
464  *                                                                         *
465  * This function loades the 64-bit floating point state register from a    *
466  * memory location. (which needs to be 8-byte aligned)                     *
467  *                                                                         *
468  * C-prototype:                                                            *
469  *  void asm_load_fp_state_reg(u8 *mem);                                   *
470  *                                                                             *
471  **************************************************************************/
472  
473 asm_load_fp_state_reg:
474         ldx [%o0],%fsr
475         retl  /* return from leaf */
476         nop
477
478
479 /* disable exec-stacks ********************************************************/
480
481 #if defined(__linux__) && defined(__ELF__)
482         .section .note.GNU-stack,"",%progbits
483 #endif
484
485
486 /*
487  * These are local overrides for various environment variables in Emacs.
488  * Please do not remove this and leave it at the end of the file, where
489  * Emacs will automagically detect them.
490  * ---------------------------------------------------------------------
491  * Local variables:
492  * mode: asm
493  * indent-tabs-mode: t
494  * c-basic-offset: 4
495  * tab-width: 4
496  * End:
497  * vim:noexpandtab:sw=4:ts=4:
498  */