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