* src/vm/jit/sparc64/emit.c: Implemented trace enter/exit functions.
[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 #include "offsets.h"
40 #include "md-asm.h"
41
42         .register %g2,#scratch                         /* define as scratch       */
43         .register %g3,#scratch                    /* XXX  reserve for application */
44         .text
45
46 /* export functions ***********************************************************/
47
48         .global asm_vm_call_method        
49         .global asm_vm_call_method_int    
50         .global asm_vm_call_method_long   
51         .global asm_vm_call_method_float  
52         .global asm_vm_call_method_double 
53         .global asm_vm_call_method_exception_handler
54
55         .global asm_call_jit_compiler
56
57         .global asm_handle_exception
58         .global asm_handle_nat_exception
59
60
61         .global asm_abstractmethoderror
62         
63         .global asm_criticalsections
64         .global asm_getclassvalues_atomic
65
66
67 /* asm_vm_call_method ******************************************************
68  *                                                                         *
69  * This function calls a Java-method (which possibly needs compilation)    *
70  *
71  * C-prototype:
72  *  java_objectheader *asm_vm_call_method(methodinfo *m, s4 vmargscount,
73  *                                               vm_arg *vmargs);
74  **************************************************************************/
75
76         .align 8        /* v9: All data types are aligned to their size */
77
78         .xword 0                         /* catch type all */
79         .xword 0                         /* handler pc */
80         .xword 0                         /* end pc */
81         .xword 0                         /* start pc */
82         .word  1                         /* extable size */
83         .word  0                         /* ALIGNMENT PADDING */
84         .xword 0                         /* line number table start */
85         .xword 0                         /* line number table size */
86         .word  0                         /* ALIGNMENT PADDING */
87         .word  0                         /* fltsave */
88         .word  1                         /* intsave */
89         .word  0                         /* isleaf */
90         .word  0                         /* IsSync */
91         .word  0                         /* frame size */
92         .xword 0                         /* method pointer (pointer to name)*/ 
93
94 asm_vm_call_method:       
95 asm_vm_call_method_int:   
96 asm_vm_call_method_long:  
97 asm_vm_call_method_float: 
98 asm_vm_call_method_double:
99         
100         save %sp, -144, %sp             /* 16 reg-save + 2 */
101
102
103         /* todo: copy fp registers */
104
105         brlez %i1, calljava_argsloaded
106         dec %i1
107         ldx [%i2 + offvmargdata], %o0
108         brlez %i1, calljava_argsloaded
109
110         dec %i1
111         ldx [%i2 + (offvmargdata+sizevmarg*1)], %o1
112         brlez %i1, calljava_argsloaded
113
114         dec %i1
115         ldx [%i2 + (offvmargdata+sizevmarg*2)], %o2
116         brlez %i1, calljava_argsloaded
117
118         dec %i1
119         ldx [%i2 + (offvmargdata+sizevmarg*3)], %o3
120         brlez %i1, calljava_argsloaded
121
122         dec %i1
123         ldx [%i2 + (offvmargdata+sizevmarg*4)], %o4
124
125         /* todo: use more out registers ? */
126
127 calljava_argsloaded:
128         /* todo: stack frame layout!! */
129         
130         brlez %i1, calljava_nocopy
131         sllx %i1, 3, %l0        /* remaining args * 8           */
132         mov %sp, %l1            /* right above window save area */
133         sub %sp, %l0, %sp       /* allocate more stack space    */
134         
135 calljava_copyloop:
136         ldx [%i2 + (offvmargdata+sizevmarg*5)], %l0
137         stx %l0, [%l1]
138         inc sizevmarg, %i2      /* src++                        */
139         inc 8, %l1              /* dst++                        */
140         dec %i1                 /* arg_count--                  */
141         bnz %xcc, calljava_copyloop
142
143 calljava_nocopy:
144
145         mov   %i0,itmp1         /* pass method info pointer via itmp1 */
146         
147         setx  asm_call_jit_compiler,%l0,mptr_itmp2  /* fake virtual function call (2 instr) */
148         stx   mptr_itmp2,[%sp + 2047 + 17*8]        /* store function address               */
149         add   %sp,2047 + 16*8,mptr_itmp2            /* set method pointer                   */
150         
151         ldx  [1*8 + mptr_itmp2], pv_caller          /* method call as in Java               */
152         jmpl pv_caller,ra_caller                      /* call JIT compiler          */
153         nop
154 calljava_jit2:
155         /* pretend to restore pv */
156         add  ra_caller,(asm_vm_call_method - calljava_jit2 + 8),pv_callee
157         
158 calljava_return:
159         mov %o0, %i0            /* pass on the return value     */
160         return %i7 + 8          /* implicit window restore */
161         nop
162
163
164 asm_vm_call_method_exception_handler:
165         mov             itmp1,%o0
166         call    builtin_throw_exception
167         return  %i7 + 8                          /* implicit window restore */
168         nop
169         
170
171
172 /****************** function asm_call_jit_compiler *****************************
173 *                                                                              *
174 *   invokes the compiler for untranslated JavaVM methods.                      *
175 *                                                                              *
176 *   Register R0 contains a pointer to the method info structure (prepared      *
177 *   by createcompilerstub). Using the return address in R26 and the            *
178 *   offset in the LDA instruction or using the value in methodptr R28 the      *
179 *   patching address for storing the method address can be computed:           *
180 *                                                                              *
181 *   method address was either loaded using                                     *
182 *   M_LDQ (REG_PV, REG_PV, a)        ; invokestatic/special    ($27)           *
183 *   M_LDA (REG_PV, REG_RA, low)                                                *
184 *   M_LDAH(REG_PV, REG_RA, high)     ; optional                                *
185 *   or                                                                         *
186 *   M_LDQ (REG_PV, REG_METHODPTR, m) ; invokevirtual/interface ($28)           *
187 *   in the static case the method pointer can be computed using the            *
188 *   return address and the lda function following the jmp instruction          *
189 *                                                                              *
190 *******************************************************************************/
191
192 asm_call_jit_compiler:
193         
194         save    %sp,-160,%sp         /* regsave + 4 slots for float args          */
195
196         SAVE_FLOAT_ARGUMENT_REGISTERS(16)
197
198         mov     itmp1,%o0             /* pass methodinfo pointer                  */
199         mov     mptr_itmp2,%o1        /* pass method pointer                      */
200         mov     %fp,%o2                           /* pass java sp (==fp)                          */
201         mov     ra_callee,%o3         /* pass Java ra                             */
202         mov     %o3,%o4               /* xpc is equal to ra                       */
203         call    jit_asm_compile       /* call jit compiler                        */
204         nop
205         
206         RESTORE_FLOAT_ARGUMENT_REGISTERS(16)
207         
208         restore %o0,%g0,pv_caller     /* restore the callers window               */
209                                       /* the source o0 references the old window  */
210                                       /* pv_caller references the new window      */
211
212
213
214         brz     pv_caller,L_asm_call_jit_compiler_exception
215
216         /* synchronise instruction cache moved somewhere else           */
217
218         jmpl    pv_caller,zero        /* and call method, the method returns      */
219                                       /* directly to the caller (ra).             */    
220         
221 L_asm_call_jit_compiler_exception:
222         /* no need to do a save, only ra needs to be preserved */
223
224         /* we save ra in one of the application globals */
225         mov     ra_caller,xpc_itmp3   /* save return address (xpc)                */
226         
227         call    exceptions_get_and_clear_exception
228
229         mov     xpc_itmp3,ra_caller   /* restore return address (xpc)             */
230
231         mov     %o0,xptr_itmp2        /* get exception                            */
232         sub     ra_caller,4,xpc_itmp3 /* exception address is ra - 4              */
233         ba      L_asm_handle_nat_exception
234
235
236
237 /* asm_handle_exception ********************************************************
238
239    This function handles an exception. It does not use the usual calling
240    conventions. The exception pointer is passed in REG_ITMP2 and the
241    pc from the exception raising position is passed in REG_ITMP3. It searches
242    the local exception table for a handler. If no one is found, it unwinds
243    stacks and continues searching the callers.
244
245 *******************************************************************************/
246
247
248 asm_handle_nat_exception:
249 L_asm_handle_nat_exception:       /* required for PIC code                    */
250 asm_handle_exception:
251         
252         /* nothing here */
253
254         restore zero,0,zero
255
256
257 /* asm_abstractmethoderror *****************************************************
258
259    Creates and throws an AbstractMethodError.
260
261 *******************************************************************************/
262
263 asm_abstractmethoderror:
264         /* do a window save */
265         save %sp,-192,%sp
266
267         mov     %fp,%o0                     /* pass java sp(==fp)                 */
268         mov     ra_callee,%o1               /* pass exception address             */
269         call    exceptions_asm_new_abstractmethoderror
270
271         mov     %o0,xptr_itmp2              /* get exception pointer              */
272         sub     ra_callee,4,xpc_itmp3       /* exception address is ra - 4        */
273         ba      L_asm_handle_nat_exception
274
275         /* XXX: leave the register window open for handle_exception ??? */
276
277 asm_getclassvalues_atomic:
278 _crit_restart:
279 _crit_begin:
280 /* not doing a window save, using the global temporary registers */
281         ldsw    [offbaseval+%o0],itmp1
282         ldsw    [offdiffval+%o0],itmp2
283         ldsw    [offbaseval+%o1],itmp3
284 _crit_end:
285         stw     itmp1,[offcast_super_baseval+%o2]
286         stw     itmp2,[offcast_super_diffval+%o2]
287         stw     itmp3,[offcast_sub_baseval+%o2]
288         jmpl    ra_caller,zero  /* caller's ra, b/c no window save */
289
290         .end    asm_getclassvalues_atomic
291
292
293     .data
294
295 asm_criticalsections:
296 #if defined(ENABLE_THREADS)
297     .xword   _crit_begin
298     .xword   _crit_end
299     .xword   _crit_restart
300 #endif
301     .xword   0
302
303
304