* src/vm/jit/m68k/emit.c (emit_mov_imm_reg): Fixed.
[cacao.git] / src / vm / jit / m68k / asmpart.S
1 /*      src/vm/jit/m68k/asmpart.S
2  *      for cacao 
3  *      Roland Lezuo
4  */
5
6 #include "config.h"
7
8 #include "vm/jit/m68k/offsets.h"
9 #include "md-abi.h"
10 #include "vm/jit/methodheader.h"
11
12 .data
13 call_jit_dummy:
14         .long 0xdeadbeef
15
16 .text
17
18 .globl asm_vm_call_method
19 .globl asm_vm_call_method_int
20 .globl asm_vm_call_method_long
21 .globl asm_vm_call_method_float
22 .globl asm_vm_call_method_double
23 .globl asm_vm_call_method_end
24 .globl asm_vm_call_method_exception_handler
25
26 .globl asm_call_jit_compiler
27
28 .globl asm_patcher_wrapper
29
30 .globl asm_getclassvalues_atomic
31 .globl asm_abstractmethoderror
32 .globl asm_criticalsections
33
34 .globl asm_handle_exception
35 .globl asm_handle_nat_exception
36
37 /*
38  *      This functions implement the C prototyped funtion
39  *      java_objectheader *asm_vm_call_method(methodinfo *m, s4 vmargscount,vm_arg *vmargs);
40  *      the _int, _long, _float, _double are used for different return types
41  *
42  *      The function may still be uncompiled, so the jit compiler gets invoked.
43  *
44  *
45  */
46
47         /* this is the method header see src/vm/jit/methodheader.h */
48         .align  4
49         .long   0                           /* catch type all                     */
50         .long   0                           /* handler pc                         */
51         .long   0                           /* end pc                             */
52         .long   0                           /* start pc                           */
53         .long   1                           /* extable size                       */
54         .long   0                           /* line number table start            */
55         .long   0                           /* line number table size             */
56         .long   0                           /* fltsave                            */
57         .long   0                           /* intsave                            */
58         .long   0                           /* isleaf                             */
59         .long   0                           /* IsSync                             */
60         .long   0                           /* frame size                         */
61         .long   0                           /* codeinfo pointer                   */
62 asm_vm_call_method:
63 asm_vm_call_method_int:
64 asm_vm_call_method_long:
65 asm_vm_call_method_float:
66 asm_vm_call_method_double:
67         addal   #(-11*4),%sp                                            /* create stackframe to save registers */
68         moveml  %d2/%d3/%d4/%d5/%d6/%d7/%a2/%a3/%a4/%a5/%fp,%sp@        /* save registers */
69
70         /* fetch arguments from vmargs data structure */
71         movel   %sp@(11*4+1*4),%a2                              /* methodinfo argument in atmp1 */
72         movel   %sp@(11*4+3*4),%a3                              /* args block */
73         movel   %sp@(11*4+2*4),%d2                              /* arg count */
74
75         moveal  %sp, %a5                                        /* memorize stack */
76         tst     %d2                                             /* do we have arguments ? */
77         beq     L_asm_vm_call_method_copy_done
78         subql   #1,%d2                                          /* simplifies offset calulation */
79
80         movel   #(sizevmarg), %d0
81         mulsl   %d2, %d0                                        
82         moveal  %a3, %a0
83         addal   %d0, %a0                                        /* %a0 points to last vmarg block */
84
85         addql   #1, %d2
86 L_asm_vm_call_copy_arg:
87         subql   #1, %d2                                         /* why wasn't that here */
88
89         movel   %a0@(offvmargdata+4), %d0
90         movel   %d0,%sp@-                                       /* push argument onto stack */
91
92         movel   %a0@(offvmargtype), %d0                         /* %d0 contains type of arg */  
93         btstl   #0, %d0                                         /* test if 2 word type */
94         beq     L_asm_vm_call_copy_1_word_type
95         movel   %a0@(offvmargdata), %d0                         /* push second word onto stack */
96         movel   %d0,%sp@-
97 L_asm_vm_call_copy_1_word_type:
98
99         subl    #(sizevmarg),%a0
100         tst     %d2                                             /* arguments left ? */
101         bne     L_asm_vm_call_copy_arg
102
103 L_asm_vm_call_method_copy_done:
104
105         leal    asm_call_jit_compiler,%a4               /* we need to fake a invocation as it would happen from jit code */
106         movel   %a4, call_jit_dummy                             /* we need a writeable memory location */
107         moveal  call_jit_dummy, %a4                             /* XXX do we have a race condition here ? */
108         jsr     %a4@
109
110 L_asm_vm_call_method_return:
111         movel   %a5, %sp                                                /* pop arguments off stack */
112         moveml  %sp@, %d2/%d3/%d4/%d5/%d6/%d7/%a2/%a3/%a4/%a5/%fp       /* restore registers */
113         addal   #(11*4),%sp                                             /* restore stack */
114         moveal  %d0, %a0                                                /* return value in %a0, too */
115 asm_vm_call_method_end:                                         /* symbol needed to insert method into avl tree */
116         rts                                                                             /* return to c code */
117
118 /* asm_vm_call_method_exception_handler ********************************************************************
119  *
120  * calls void *builtin_throw_exception(java_objectheader *xptr) when no other handler is appropiate
121  * this functions gets called indirectly from asm_handle_exception, which back then moved xptr to %a2
122  * clear software design is in the eye of the beholder.
123  ************************************************************************************************************/
124 asm_vm_call_method_exception_handler:
125         movel   %a2, %sp@-                      /* push xptr argument */
126         jsr     builtin_throw_exception
127         lea     %sp@(4), %sp                    /* pop arg off stack */
128         moveal  %sp, %a5
129         jmp     L_asm_vm_call_method_return
130
131
132 /* asm_call_jit_compiler ************************************************************************************
133  *      Invokes compiler for untranslated java methods.
134  *      C prototype: void asm_call_jit_compiler(void);
135  *      BUT: this does not match reality, arguments _ARE_ passed via createcompilerstub and asm_vm_call_method...
136  *      arguments passed via %a2(methodinfo) == REG_ATMP1
137  *                       %a3(mptr)       == REG_ATMP2
138  ************************************************************************************************************/
139 asm_call_jit_compiler:
140         addal   #(-4*4),%sp                                             /* create stackframe to save registers */
141         moveml  %a0/%a1/%d0/%d1,%sp@                    /* save volatile registers */
142
143         movel   %sp@(4*4),%sp@-                                 /* push arguments onto stack (ra)*/
144         pea     %sp@(4*4+4)                                             /* the old stack pointer */
145         movel   %a3,%sp@-                                               /* mptr */
146         movel   %a2,%sp@-                                               /* methodinfo */
147
148         /* C prototype: u1 *jit_asm_compile(methodinfo *m, u1 *mptr, u1 *sp, u1 *ra); */
149         jsr     jit_asm_compile                                         /* invoke compiler */
150         lea     %sp@(4*4),%sp                                           /* pop arguments off stack */
151         moveal  %d0, %a2                                                /* to tmp register */
152
153         moveml %sp@,%a0/%a1/%d0/%d1                                     /* restore volatile registers */
154         addal   #(4*4),%sp                                              /* remove stackframe */
155
156         tstl    %a2                                                     /* check for exception */
157         beq     L_asm_call_jit_compiler_exception
158
159         jmp     %a2@                                                    /* invoke java method */
160         jsr     0                                                       /* we should not ever return here */
161
162 L_asm_call_jit_compiler_exception:
163         jsr             exceptions_get_and_clear_exception              /* exception object in %d0 now */
164         moveal  %d0, %a2                                                                /* move exception object into sptr register */
165         moveal  %sp@+, %a3                                                              /* pop return address into exception address reg */
166         jmp asm_handle_exception                                                /* handle exception */
167
168
169 /* asm_patcher_wrapper ********************************************************
170         
171   prepares arguments on stack
172   calls patcher_wrapper signature: java_objectheader *patcher_wrapper(u1 *sp, u1 *pv, u1 *ra);
173
174   Stack layout:
175      24   return address
176      20   REG_ITMP3
177      16   pointer to virtual java_objectheader
178      12   last byte of machine code (xmcode)
179       8   machine code (which is patched back later)
180       4   unresolved field reference
181       0   patcher function pointer to call
182 *******************************************************************************/
183 asm_patcher_wrapper:
184
185   /* save scratch registers */
186   movel %a0, %sp@-
187   movel %a1, %sp@-
188   movel %d0, %sp@-
189   movel %d1, %sp@-
190
191   /* calculate original value of sp */
192   movel %sp, %d0
193   addil #4*4, %d0
194
195 #if !defined(ENABLE_SOFTFLOAT)
196   movel %f0, %sp@-
197   movel %f1, %sp@-
198 #endif
199
200   clrl  %sp@-                           /* pass ra */
201   clrl  %sp@-                           /* pass pv, if null use findmethod */
202   movel %d0, %sp@-                      /* pass sp of patcher stub */
203   jsr   patcher_wrapper         /* return value in %d0 */
204
205   lea   %sp@(3*4), %sp          /* pop arguments off stack */
206   tst   %d0                                     /* test if exception occured */
207   bne   L_asm_patcher_wrapper_exception
208
209 #if !defined(ENABLE_SOFTFLOAT)
210   movel %sp@+, %f0
211   movel %sp@+, %f1
212 #endif
213   movel %sp@+, %d1
214   movel %sp@+, %d0
215   movel %sp@+, %a1
216   movel %sp@+, %a0
217
218   lea   %sp@(6*4), %sp                  /* restore stack and remove patcher stub*/
219   rts                                                   /* back to jit code */
220
221 L_asm_patcher_wrapper_exception:
222   /* WARNING: the stack is still disturbed, look at asm_patcher_wrapper for details */
223   /* we do not need to restore the content of the registers, I hope */
224 #if !defined(ENABLE_SOFTFLOAT)
225         lea             %sp@(6*4), %sp
226 #else
227         lea             %sp@(4*4), %sp
228 #endif
229
230   lea           %sp@(6*4), %sp                          /* restore stack and remove patcher stub*/
231   moveal        %d0, %a2                                        /* xptr, pointer to exception object */
232   moveal        %sp@+, %a3                                      /* pop return address into exception address register */
233   jmp           asm_handle_exception            /* handle exception */
234   illegal                                                               /* XXX: we never come back */
235
236 /********************************************************************************
237         Reads a few values atomically. 
238         C signature:
239         void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out);
240
241         super ... sp@(4)
242         sub   ... sp@(8)
243         out   ... sp@(12)
244 ********************************************************************************/
245 asm_getclassvalues_atomic:
246 _crit_restart:
247 _crit_begin:
248         moveal  %sp@(4), %a0
249         movel   %a0@(offbaseval), %d0
250         movel   %a0@(offdiffval), %d1
251         
252         moveal  %sp@(8), %a0
253         moveal  %a0@(offbaseval), %a0
254 _crit_end:
255         moveal  %sp@(12), %a1
256         movel   %d0, %a1@(offcast_super_baseval)
257         movel   %d1, %a1@(offcast_super_diffval)
258         movel   %a0, %a1@(offcast_sub_baseval)
259
260         rts
261 .data
262 asm_criticalsections:
263 #if defined(ENABLE_THREADS)
264         .long _crit_begin
265         .long _crit_end
266         .long _crit_restart
267 #endif
268         .long 0
269 .text
270
271 asm_abstractmethoderror:
272
273 /* asm_handle_exception ********************************************************
274 *                                                                              *
275 *   This function handles an exception. It does not use the usual calling      *
276 *   conventions. The exception pointer is passed in REG_ATMP1 and the          *
277 *   pc from the exception raising position is passed in REG_ATMP2. It searches *
278 *   the local exception table for a handler. If no one is found, it unwinds    *
279 *   stacks and continues searching the callers.                                *
280 *                                                                              *
281 *   void asm_handle_exception (void);
282 *   exception object pointer...%a2   exception raising address...%a3           *
283 *                                                                              *
284 *******************************************************************************/
285 asm_handle_nat_exception:
286         lea     %sp@(4), %sp
287 asm_handle_exception:
288 L_asm_handle_exception_stack_loop:
289         
290         /* we need the dseg, figure it out */
291         movel   %a3, %sp@-                              /* push ra argument */
292         jsr     md_codegen_get_pv_from_pc       /* pv in %d0 now */
293         movel   %d0, %d2                                /* move to safe register */
294         lea     %sp@(4), %sp                            /* pop args off stack */
295
296         /* now call the following c function */
297         /* u1 *exceptions_handle_exception(java_objectheader *xptr, u1 *xpc, u1 *pv, u1 *sp) */
298         movel   %sp,%sp@-
299         movel   %d2,%sp@-
300         movel   %a3,%sp@-
301         movel   %a2,%sp@-
302         jsr     exceptions_handle_exception             /* %d0 is address of handler or 0 when not catched */
303         lea     %sp@(4*4), %sp                          /* pop args off stack */
304         tstl    %d0
305         beq     L_asm_handle_exception_not_catched
306
307         /* %d0 contains address of exception handler */
308         moveal  %d0, %a0
309         jmp     %a0@
310
311 L_asm_handle_exception_not_catched:
312         /* we did not find an exception handler in this stackframe */
313         /* remove this frame and search in the one above */
314         /* %a2 containts exception object ptr, %d2 the actual pv */
315         moveal  %d2, %a3
316         movel   %a3@(FrameSize), %d2
317
318         moveal  %sp, %a0
319         addal   %d2, %a0        /* %a0 now points to top of stackframe, where saved regs are */
320
321         /* the saved registers have to be restored */
322         /* XXX ugly hack: intsave and adrsave share one field */
323         movel   %a3@(IntSave), %d0
324         andil   #0x0000ffff, %d0        /* this is IntSave */
325         cmpb    #0,%d0
326         beq     L_asm_handle_ex_int_done
327         movel   (%a0)+, %d7
328
329         cmpb    #1,%d0
330         beq     L_asm_handle_ex_int_done
331         movel   (%a0)+, %d6
332
333         cmpb    #2,%d0 
334         beq     L_asm_handle_ex_int_done
335         movel   (%a0)+, %d5
336
337 L_asm_handle_ex_int_done:
338
339         movel   %a3@(IntSave), %d0
340         andil   #0xffff0000, %d0        /* this is AdrSave */
341         tstl    %d0
342         beq     L_handle_exception_nat_catched_no_adr
343         moveal  (%a0)+, %a5
344 L_handle_exception_nat_catched_no_adr:
345
346 #if !defined(ENABLE_SOFTFLOAT)
347         FIXME
348 #else
349         /* nothing to do */
350 #endif
351         addal   %d2, %sp                /* remove old stackframe */
352         moveal  %sp@+, %a3              /* make return address, new exception rasing address */
353         subal   #2, %a3                 /* it was a jsr %aX, which is 4 bytes long */
354         jmp     L_asm_handle_exception_stack_loop
355
356
357 illegal
358 .long 0x23234242
359
360
361 /*
362  * These are local overrides for various environment variables in Emacs.
363  * Please do not remove this and leave it at the end of the file, where
364  * Emacs will automagically detect them.
365  * ---------------------------------------------------------------------
366  * Local variables:
367  * mode: c
368  * indent-tabs-mode: t
369  * c-basic-offset: 4
370  * tab-width: 4
371  * End:
372  * vim:noexpandtab:sw=4:ts=4:
373  */