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