* changed src/vm/jit/m68k/asmpart.S (asm_patcher_wrapper): Removed.
[cacao.git] / src / vm / jit / m68k / asmpart.S
1 /* src/vm/jit/m68k/asmpart.S - Java-C interface functions for m68k
2
3    Copyright (C) 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 */
26
27
28 #include "config.h"
29
30 #include "md-abi.h"
31
32 #include "vm/jit/methodheader.h"
33
34
35 .text
36
37 .globl asm_vm_call_method
38 .globl asm_vm_call_method_int
39 .globl asm_vm_call_method_long
40 .globl asm_vm_call_method_float
41 .globl asm_vm_call_method_double
42 .globl asm_vm_call_method_end
43 .globl asm_vm_call_method_exception_handler
44
45 .globl asm_call_jit_compiler
46
47 .globl asm_abstractmethoderror
48
49 .globl asm_handle_exception
50 .globl asm_handle_nat_exception
51
52 /*
53  *      This functions implement the C prototyped funtion
54  *      java_objectheader *asm_vm_call_method(methodinfo *m, s4 vmargscount,vm_arg *vmargs);
55  *      the _int, _long, _float, _double are used for different return types
56  *
57  *      The function may still be uncompiled, so the jit compiler gets invoked.
58  *
59  *
60  */
61
62         /* this is the method header see src/vm/jit/methodheader.h */
63
64         .align  4
65
66         .long   0                           /* line number table start            */
67         .long   0                           /* line number table size             */
68         .long   0                           /* fltsave                            */
69         .long   0                           /* intsave                            */
70         .long   0                           /* isleaf                             */
71         .long   0                           /* frame size                         */
72         .long   0                           /* codeinfo pointer                   */
73
74
75 /*
76         This method gets called with 3 arguments:
77         1st arg: addres of method to call (fake invokevirtual here)
78         2nd arg: uint64_t array of argument registers followed by stack 
79         3rd arg: number of 8 byte stack slots to be copied.
80
81         coldifire does not use any argument registers, so just the stack has to be copied
82 */
83 asm_vm_call_method:
84 asm_vm_call_method_int:
85 asm_vm_call_method_long:
86 asm_vm_call_method_float:
87 asm_vm_call_method_double:
88
89 #if defined(ENABLE_SOFTFLOAT)
90         addal   #(-12*4),%sp                                            /* create stackframe to save registers, and 1 slot for method invocation */
91         moveml  %d2/%d3/%d4/%d5/%d6/%d7/%a2/%a3/%a4/%a5/%fp,%sp@        /* save registers */
92
93         /* fetch arguments from vmargs data structure */
94         movel   %sp@(12*4+1*4),%a3                              /* method */
95         movel   %sp@(12*4+2*4),%a2                              /* arg array */
96         movel   %sp@(12*4+3*4),%d2                              /* arg count */
97
98         movel   %a3, %sp@(12*4)                                 /* copy method address to stackslot */
99         leal    %sp@(12*4), %a3                                 /* and store that address in %a3 */
100 #else
101         addal #(-12*4-6*8), %sp
102         moveml  %d2/%d3/%d4/%d5/%d6/%d7/%a2/%a3/%a4/%a5/%fp,%sp@        /* save registers */
103         fmovemd %fp2/%fp3/%fp4/%fp5/%fp6/%fp7,%sp@(11*4)                        /* save registers */
104
105         /* fetch arguments from vmargs data structure */
106         movel   %sp@(12*4+6*8+1*4),%a3                          /* method */
107         movel   %sp@(12*4+6*8+2*4),%a2                          /* arg array */
108         movel   %sp@(12*4+6*8+3*4),%d2                          /* arg count */
109
110         movel   %a3, %sp@(11*4+6*8)                                     /* copy method address to stackslot */
111         leal    %sp@(11*4+6*8), %a3                                     /* and store that address in %a3 */
112 #endif
113
114         moveal  %sp, %a5                                        /* memorize stack */
115
116         tstl    %d2                                                     /* do we have arguments ? */
117         beq     L_asm_vm_call_method_copy_done
118         movel   %d2, %d3                                                /* create stackframe */
119         asll    #3,  %d3                                                /* number args * 8 */
120         subal   %d3, %sp                        
121         moveal  %sp, %a4                                                /* %a4 is temp stack pointer */
122
123 L_asm_vm_call_method_copy_loop:
124         movel   %a2@(0), %d3
125         movel   %d3, %a4@(0)                                    /* copy 4 bytes */
126         movel   %a2@(4), %d3
127         movel   %d3, %a4@(4)                                    /* a whole stack slot has been copied */
128
129         addal   #8, %a2
130         addal   #8, %a4
131         addl    #-1, %d2
132         tstl    %d2                                                             /* do we have more arguments ? */
133         beq     L_asm_vm_call_method_copy_done
134         br      L_asm_vm_call_method_copy_loop
135
136
137 L_asm_vm_call_method_copy_done:
138         /* now we fake method invocation as it would happen from invokevirtual */
139         /* this is needed as we patch the caller site, so we need a writeable slot */
140         /* %a3 points to the address containing the method, %a3 == REG_METHODPTR */
141
142         moveal  %a3@(0), %a4
143         jsr     %a4@
144
145 L_asm_vm_call_method_return:
146         movel   %a5, %sp                                                /* pop arguments off stack */
147
148 #if defined(ENABLE_SOFTFLOAT)
149         moveml  %sp@, %d2/%d3/%d4/%d5/%d6/%d7/%a2/%a3/%a4/%a5/%fp       /* restore registers */
150         addal   #(12*4),%sp                                             /* restore stack */
151 #else
152         fmovemd %sp@(12*4), %fp2/%fp3/%fp4/%fp5/%fp6/%fp7                       /* restore registers */
153         moveml  %sp@, %d2/%d3/%d4/%d5/%d6/%d7/%a2/%a3/%a4/%a5/%fp       /* restore registers */
154         addal   #(12*4+6*8),%sp                                         /* restore stack */
155 #endif
156         moveal  %d0, %a0                                                /* XXX return value in %a0, too, gcc sometimes expects addresses in %a0, wired */
157 asm_vm_call_method_end:                                         /* symbol needed to insert method into avl tree */
158         rts                                                                             /* return to c code */
159
160 /* asm_vm_call_method_exception_handler ********************************************************************
161  *
162  * calls void *builtin_throw_exception(java_objectheader *xptr) when no other handler is appropiate
163  * this functions gets called indirectly from asm_handle_exception, which back then moved xptr to %a2
164  * clear software design is in the eye of the beholder.
165  ************************************************************************************************************/
166 asm_vm_call_method_exception_handler:
167         movel   %a2, %sp@-                      /* push xptr argument */
168         jsr     builtin_throw_exception
169         lea     %sp@(4), %sp                    /* pop arg off stack */
170         jmp     L_asm_vm_call_method_return
171
172
173 /* asm_call_jit_compiler ************************************************************************************
174  *      Invokes compiler for untranslated java methods.
175  *      C prototype: void asm_call_jit_compiler(void);
176  *      BUT: this does not match reality, arguments _ARE_ passed via createcompilerstub and asm_vm_call_method...
177  *      arguments passed via %a2(methodinfo) == REG_ATMP1
178  *                       %a3(mptr)       == REG_ATMP2
179  ************************************************************************************************************/
180 asm_call_jit_compiler:
181         addal   #(-4*4),%sp                                             /* create stackframe to save registers */
182         moveml  %a0/%a1/%d0/%d1,%sp@                    /* save volatile registers */
183 #if !defined(ENABLE_SOFTFLOAT)
184         addal   #-8*2, %sp
185         fmovemd %fp0/%fp1, %sp@
186         movel   %sp@(8*4), %sp@-
187         pea             %sp@(8*4+8)
188 #else
189         movel   %sp@(4*4), %sp@-                                /* push arguments onto stack (ra)*/
190         pea     %sp@(4*4+8)                                             /* the old stack pointer*/
191 #endif
192         movel   %a3,%sp@-                                               /* mptr */
193         movel   %a2,%sp@-                                               /* methodinfo */
194
195         /* C prototype: u1 *jit_asm_compile(methodinfo *m, u1 *mptr, u1 *sp, u1 *ra); */
196         jsr     jit_asm_compile                                         /* invoke compiler */
197         addal   #(4*4),%sp                                              /* pop arguments off stack */
198         moveal  %d0, %a2                                                /* to tmp register */
199
200 #if !defined(ENABLE_SOFTFLOAT)
201         fmovemd %sp@, %fp0/%fp1
202         addal   #8*2, %sp
203 #endif
204
205         moveml %sp@,%a0/%a1/%d0/%d1                             /* restore volatile registers */
206         addal   #(4*4),%sp                                              /* remove stackframe */
207
208         tstl    %a2                                                             /* check for exception */
209         beq     L_asm_call_jit_compiler_exception
210
211         jmp     %a2@                                                            /* invoke java method */
212         jsr     0                                                               /* we should not ever return here */
213
214 L_asm_call_jit_compiler_exception:
215         jsr             exceptions_get_and_clear_exception              /* exception object in %d0 now */
216         moveal  %d0, %a2                                                                /* move exception object into sptr register */
217         moveal  %sp@+, %a3                                                              /* pop return address into exception address reg */
218         jmp asm_handle_exception                                                /* handle exception */
219
220
221
222 asm_abstractmethoderror:
223
224 /* asm_handle_exception ********************************************************
225 *                                                                              *
226 *   This function handles an exception. It does not use the usual calling      *
227 *   conventions. The exception pointer is passed in REG_ATMP1 and the          *
228 *   pc from the exception raising position is passed in REG_ATMP2. It searches *
229 *   the local exception table for a handler. If no one is found, it unwinds    *
230 *   stacks and continues searching the callers.                                *
231 *                                                                              *
232 *   void asm_handle_exception (void);
233 *   exception object pointer...%a2   exception raising address...%a3           *
234 *                                                                              *
235 *******************************************************************************/
236 asm_handle_nat_exception:
237         lea     %sp@(4), %sp
238 asm_handle_exception:
239 L_asm_handle_exception_stack_loop:
240         /* save temporary registers */
241         movel   %d0, %sp@-
242         movel   %d1, %sp@-
243         movel   %a0, %sp@-
244         movel   %a1, %sp@-
245 #if !defined(ENABLE_SOFTFLOAT)
246         addal   #-8*2, %sp
247         fmovemd %fp0, %sp@(0)
248         fmovemd %fp1, %sp@(8)
249 #endif
250
251         /* we need the dseg, figure it out */
252         movel   %a3, %sp@-                              /* push ra argument */
253         jsr     md_codegen_get_pv_from_pc       /* pv in %d0 now */
254         movel   %d0, %d2                                /* move to safe register */
255         lea     %sp@(4), %sp                            /* pop args off stack */
256
257         /* now call the following c function */
258         /* u1 *exceptions_handle_exception(java_objectheader *xptr, u1 *xpc, u1 *pv, u1 *sp) */
259 #if !defined(ENABLE_SOFTFLOAT)
260         pea             %sp@(4*4 + 8*2)
261 #else
262         pea             %sp@(4*4)
263 #endif
264         movel   %d2,%sp@-
265         movel   %a3,%sp@-
266         movel   %a2,%sp@-
267         jsr     exceptions_handle_exception             /* %d0 is address of handler or 0 when not catched */
268         lea     %sp@(4*4), %sp                          /* pop args off stack */
269         tstl    %d0
270         beq     L_asm_handle_exception_not_catched
271
272         /* %d0 contains address of exception handler */
273         moveal  %d0, %a3
274
275         /* restore temporary registers */
276         moveal  %sp@+, %a1
277         moveal  %sp@+, %a0
278         movel   %sp@+, %d1
279         movel   %sp@+, %d0
280 #if !defined(ENABLE_SOFTFLOAT)
281         fmovemd %fp0, %sp@(0)
282         fmovemd %fp1, %sp@(8)
283         addal   #8*2, %sp
284 #endif
285
286         jmp     %a3@
287
288 L_asm_handle_exception_not_catched:
289         /* we did not find an exception handler in this stackframe */
290         /* remove this frame and search in the one above */
291         /* %a2 containts exception object ptr, %d2 the actual pv */
292
293         /* remove temporary registers stored */
294 #if !defined(ENABLE_SOFTFLOAT)
295         addal   #4*4 + 8*2, %sp
296 #else
297         addal   #4*4, %sp
298 #endif
299
300         moveal  %d2, %a3
301         movel   %a3@(FrameSize), %d2
302
303         moveal  %sp, %a0
304         addal   %d2, %a0        /* %a0 now points to top of stackframe, where saved regs are */
305
306         /* the saved registers have to be restored */
307         /* XXX ugly hack: intsave and adrsave share one field */
308         movel   %a3@(IntSave), %d0
309         andil   #0x0000ffff, %d0        /* this is IntSave */
310         cmpb    #0,%d0
311         beq     L_asm_handle_ex_int_done
312         movel   -(%a0), %d7
313
314         cmpb    #1,%d0
315         beq     L_asm_handle_ex_int_done
316         movel   -(%a0), %d6
317
318         cmpb    #2,%d0 
319         beq     L_asm_handle_ex_int_done
320         movel   -(%a0), %d5
321
322 L_asm_handle_ex_int_done:
323
324         movel   %a3@(IntSave), %d0
325         andil   #0xffff0000, %d0        /* this is AdrSave */
326         lsrl    #8, %d0
327         lsrl    #8, %d0
328
329 #if 0
330         cmpb    #0, %d0
331         beq     L_asm_handle_ex_adr_done
332         moveal  -(%a0), %a5
333 #else
334         cmpb    #0, %d0
335         beq     L_asm_handle_ex_adr_done
336         moveal  -(%a0), %fp
337
338         cmpb    #1,%d0
339         beq     L_asm_handle_ex_adr_done
340         moveal  -(%a0), %a5
341 #endif
342
343 L_asm_handle_ex_adr_done:
344
345 #if !defined(ENABLE_SOFTFLOAT)
346         movel   %a3@(FltSave), %d0
347         cmpb    #0,%d0
348         beq     L_asm_handle_ex_flt_done
349         fmovemd %a0@(-8), %fp7
350
351         cmpb    #1,%d0
352         beq     L_asm_handle_ex_flt_done
353         fdmoved %a0@(-16), %fp6
354         
355         cmpb    #2,%d0
356         beq     L_asm_handle_ex_flt_done
357         fdmoved %a0@(-24), %fp5
358
359 L_asm_handle_ex_flt_done:
360 #else
361         /* nothing to do */
362 #endif
363         addal   %d2, %sp                /* remove old stackframe */
364         moveal  %sp@+, %a3              /* make return address, new exception rasing address */
365         subal   #2, %a3                 /* it was a jsr %aX, which is 4 bytes long */
366         jmp     L_asm_handle_exception_stack_loop
367
368
369 illegal
370 .long 0x23234242
371
372
373 /*
374  * These are local overrides for various environment variables in Emacs.
375  * Please do not remove this and leave it at the end of the file, where
376  * Emacs will automagically detect them.
377  * ---------------------------------------------------------------------
378  * Local variables:
379  * mode: c
380  * indent-tabs-mode: t
381  * c-basic-offset: 4
382  * tab-width: 4
383  * End:
384  * vim:noexpandtab:sw=4:ts=4:
385  */