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