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