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