* src/vm/jit/sparc64/asmpart.S: added exception handling code.
[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
63         .global asm_abstractmethoderror
64         
65         .global asm_criticalsections
66         .global asm_getclassvalues_atomic
67
68
69 /* asm_vm_call_method ******************************************************
70  *                                                                         *
71  * This function calls a Java-method (which possibly needs compilation)    *
72  *
73  * C-prototype:
74  *  java_objectheader *asm_vm_call_method(methodinfo *m, s4 vmargscount,
75  *                                               vm_arg *vmargs);
76  **************************************************************************/
77
78         .align 8        /* v9: All data types are aligned to their size */
79
80         .xword 0                         /* catch type all */
81         .xword 0                         /* handler pc */
82         .xword 0                         /* end pc */
83         .xword 0                         /* start pc */
84         .word  1                         /* extable size */
85         .word  0                         /* ALIGNMENT PADDING */
86         .xword 0                         /* line number table start */
87         .xword 0                         /* line number table size */
88         .word  0                         /* ALIGNMENT PADDING */
89         .word  0                         /* fltsave */
90         .word  1                         /* intsave */
91         .word  0                         /* isleaf */
92         .word  0                         /* IsSync */
93         .word  0                         /* frame size */
94         .xword 0                         /* method pointer (pointer to name)*/ 
95
96 asm_vm_call_method:       
97 asm_vm_call_method_int:   
98 asm_vm_call_method_long:  
99 asm_vm_call_method_float: 
100 asm_vm_call_method_double:
101         
102         save %sp, -144, %sp             /* 16 reg-save + 2 */
103
104
105         /* todo: copy fp registers */
106
107         brlez %i1, calljava_argsloaded
108         dec %i1
109         ldx [%i2 + offvmargdata], %o0
110         brlez %i1, calljava_argsloaded
111
112         dec %i1
113         ldx [%i2 + (offvmargdata+sizevmarg*1)], %o1
114         brlez %i1, calljava_argsloaded
115
116         dec %i1
117         ldx [%i2 + (offvmargdata+sizevmarg*2)], %o2
118         brlez %i1, calljava_argsloaded
119
120         dec %i1
121         ldx [%i2 + (offvmargdata+sizevmarg*3)], %o3
122         brlez %i1, calljava_argsloaded
123
124         dec %i1
125         ldx [%i2 + (offvmargdata+sizevmarg*4)], %o4
126
127         /* todo: use more out registers ? */
128
129 calljava_argsloaded:
130         /* todo: stack frame layout!! */
131         
132         brlez %i1, calljava_nocopy
133         sllx %i1, 3, %l0        /* remaining args * 8           */
134         mov %sp, %l1            /* right above window save area */
135         sub %sp, %l0, %sp       /* allocate more stack space    */
136         
137 calljava_copyloop:
138         ldx [%i2 + (offvmargdata+sizevmarg*5)], %l0
139         stx %l0, [%l1]
140         inc sizevmarg, %i2      /* src++                        */
141         inc 8, %l1              /* dst++                        */
142         dec %i1                 /* arg_count--                  */
143         bnz %xcc, calljava_copyloop
144
145 calljava_nocopy:
146
147         mov   %i0,itmp1         /* pass method info pointer via itmp1 */
148         
149         setx  asm_call_jit_compiler,%l0,mptr_itmp2  /* fake virtual function call (2 instr) */
150         stx   mptr_itmp2,[%sp + 2047 + 17*8]        /* store function address               */
151         add   %sp,2047 + 16*8,mptr_itmp2            /* set method pointer                   */
152         
153         ldx  [1*8 + mptr_itmp2], pv_caller          /* method call as in Java               */
154         jmpl pv_caller,ra_caller                      /* call JIT compiler          */
155         nop
156 calljava_jit2:
157         /* pretend to restore pv */
158         add  ra_caller,(asm_vm_call_method - calljava_jit2 + 8),pv_callee
159         
160 calljava_return:
161         mov %o0, %i0            /* pass on the return value     */
162         return %i7 + 8          /* implicit window restore */
163         nop
164
165
166 asm_vm_call_method_exception_handler:
167         
168         mov             itmp1,%o0
169         call    builtin_throw_exception
170         nop
171         return  %i7 + 8                          /* implicit window restore */
172         nop
173         
174
175
176 /****************** function asm_call_jit_compiler *****************************
177 *                                                                              *
178 *   invokes the compiler for untranslated JavaVM methods.                      *
179 *                                                                              *
180 *   Register R0 contains a pointer to the method info structure (prepared      *
181 *   by createcompilerstub). Using the return address in R26 and the            *
182 *   offset in the LDA instruction or using the value in methodptr R28 the      *
183 *   patching address for storing the method address can be computed:           *
184 *                                                                              *
185 *   method address was either loaded using                                     *
186 *   M_LDQ (REG_PV, REG_PV, a)        ; invokestatic/special    ($27)           *
187 *   M_LDA (REG_PV, REG_RA, low)                                                *
188 *   M_LDAH(REG_PV, REG_RA, high)     ; optional                                *
189 *   or                                                                         *
190 *   M_LDQ (REG_PV, REG_METHODPTR, m) ; invokevirtual/interface ($28)           *
191 *   in the static case the method pointer can be computed using the            *
192 *   return address and the lda function following the jmp instruction          *
193 *                                                                              *
194 *******************************************************************************/
195
196 asm_call_jit_compiler:
197         
198         save    %sp,-160,%sp         /* regsave + 4 slots for float args          */
199
200         SAVE_FLOAT_ARGUMENT_REGISTERS(16)
201
202         mov     itmp1,%o0             /* pass methodinfo pointer                  */
203         mov     mptr_itmp2,%o1        /* pass method pointer                      */
204         mov     %fp,%o2                           /* pass java sp (==fp)                          */
205         mov     ra_callee,%o3         /* pass Java ra                             */
206         mov     %o3,%o4               /* xpc is equal to ra                       */
207         call    jit_asm_compile       /* call jit compiler                        */
208         nop
209         
210         RESTORE_FLOAT_ARGUMENT_REGISTERS(16)
211         
212         restore %o0,%g0,pv_caller     /* restore the callers window               */
213                                       /* the source o0 references the old window  */
214                                       /* pv_caller references the new window      */
215
216
217
218         brz     pv_caller,L_asm_call_jit_compiler_exception
219         nop
220
221         /* synchronise instruction cache moved somewhere else           */
222
223         jmpl    pv_caller,zero        /* and call method, the method returns      */
224                                       /* directly to the caller (ra).             */    
225         
226 L_asm_call_jit_compiler_exception:
227         /* no need to do a save, only ra needs to be preserved */
228
229         /* we save ra in one of the application globals */
230         mov     ra_caller,xpc_itmp3   /* save return address (xpc)                */
231         
232         call    exceptions_get_and_clear_exception
233         nop
234
235         mov     xpc_itmp3,ra_caller   /* restore return address (xpc)             */
236
237         mov     %o0,xptr_itmp2        /* get exception                            */
238         sub     ra_caller,4,xpc_itmp3 /* exception address is ra - 4              */
239         ba      L_asm_handle_nat_exception
240         nop
241
242
243
244 /* asm_handle_exception ********************************************************
245
246    This function handles an exception. It does not use the usual calling
247    conventions. The exception pointer is passed in REG_ITMP2 and the
248    pc from the exception raising position is passed in REG_ITMP3. It searches
249    the local exception table for a handler. If no one is found, it unwinds
250    stacks and continues searching the callers.
251
252 *******************************************************************************/
253
254
255 asm_handle_nat_exception:
256 L_asm_handle_nat_exception:       /* required for PIC code                    */
257 L_asm_handle_exception_stack_loop:
258         /* exception handling assumes that the current java method saved       */
259         /* the caller's window                                                 */
260
261         /* get ra and pv before saving the window */
262         mov     ra_callee,itmp1
263         mov     pv_callee,%g4
264
265         save    %sp,-176,%sp
266         mov     xptr_itmp2,%l0              /* save exception pointer             */
267         mov     xpc_itmp3,%l1               /* save exception pc                  */
268         mov     zero,%l2                    /* save maybe-leaf flag (cleared)     */
269
270         mov     %l0,%o0                     /* pass xptr                          */
271         mov     %l1,%o1                     /* pass xpc                           */
272         mov     %g4,%o2                     /* pass PV                            */
273         mov     %fp,%o3                     /* pass Java SP                       */
274
275 asm_handle_exception:
276         mov     pv_callee,%g4
277
278         /* save bigger stack frame for float args and temps */
279         save    %sp,(FLT_ARG_CNT+FLT_TMP_CNT+ABICALL_OFF)*8,%sp
280
281         SAVE_FLOAT_ARGUMENT_REGISTERS(ABICALL_OFF)
282         SAVE_FLOAT_TEMPORARY_REGISTERS(ABICALL_OFF+FLT_ARG_CNT)
283
284         mov     xptr_itmp2,%l0              /* save exception pointer             */
285         add     zero,1,%l2                  /* set maybe-leaf flag                */
286
287         mov     %l0,%o0                     /* pass xptr                          */
288         mov     xpc_itmp3,%o1               /* pass xpc                           */
289         mov     %g4,%o2                     /* pass PV                            */
290         mov     %fp,%o3                     /* pass Java SP                       */
291
292 L_asm_handle_exception_continue:
293         call    exceptions_handle_exception
294
295         brz     %o0,L_asm_handle_exception_not_caught
296
297         mov     %o0,xpc_itmp3               /* move handlerpc into xpc            */
298         mov     %l0,xptr_itmp2              /* restore exception pointer          */
299
300         brz     %l2,L_asm_handle_exception_no_leaf
301
302         RESTORE_FLOAT_ARGUMENT_REGISTERS(ABICALL_OFF)
303         RESTORE_FLOAT_TEMPORARY_REGISTERS(ABICALL_OFF+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
310 L_asm_handle_exception_not_caught:
311         mov     %l0,xptr_itmp2              /* restore xptr                       */
312         restore                             /* free our stackframe and window     */
313         /* maybe leaf flag gets cleared after branch to _loop */
314         
315         restore                             /* unwind stack and window            */
316         ba      L_asm_handle_exception_stack_loop
317         mov     xpc_itmp3,ra_caller         /* the new xpc is ra (delay)          */
318         
319
320
321
322 /* asm_abstractmethoderror *****************************************************
323
324    Creates and throws an AbstractMethodError.
325
326 *******************************************************************************/
327
328 asm_abstractmethoderror:
329         /* do a window save */
330         save %sp,-192,%sp
331
332         mov     %fp,%o0                     /* pass java sp(==fp)                 */
333         mov     ra_callee,%o1               /* pass exception address             */
334         call    exceptions_asm_new_abstractmethoderror
335
336         mov     %o0,xptr_itmp2              /* get exception pointer              */
337         sub     ra_callee,4,xpc_itmp3       /* exception address is ra - 4        */
338         ba      L_asm_handle_nat_exception
339
340         /* XXX: leave the register window open for handle_exception ??? */
341
342 asm_getclassvalues_atomic:
343 _crit_restart:
344 _crit_begin:
345 /* not doing a window save, using the global temporary registers */
346         ldsw    [offbaseval+%o0],itmp1
347         ldsw    [offdiffval+%o0],itmp2
348         ldsw    [offbaseval+%o1],itmp3
349 _crit_end:
350         stw     itmp1,[offcast_super_baseval+%o2]
351         stw     itmp2,[offcast_super_diffval+%o2]
352         stw     itmp3,[offcast_sub_baseval+%o2]
353         jmpl    ra_caller,zero  /* caller's ra, b/c no window save */
354
355         .end    asm_getclassvalues_atomic
356
357
358     .data
359
360 asm_criticalsections:
361 #if defined(ENABLE_THREADS)
362     .xword   _crit_begin
363     .xword   _crit_end
364     .xword   _crit_restart
365 #endif
366     .xword   0
367
368
369