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