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