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