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