* src/vm/jit/m68k/codegen.c (ICMD_FNEG): Implemented.
[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         tstl    %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                                         /* decrement argument counter */
88         movel   %a0@(offvmargtype+4), %d1       /* %d1 contains type of arg, vmargtype is u8 */ 
89
90         cmpil   #2, %d1                                         /* float type ? */
91         bne             L_asm_vm_call_copy_int
92
93         movel   %a0@(offvmargdata), %d0         /* float has different endianess as int */
94         movel   %d0, %sp@-
95         bra             L_asm_vm_call_copy_1_word_type
96
97 L_asm_vm_call_copy_int:
98         movel   %a0@(offvmargdata+4), %d0
99         movel   %d0,%sp@-                                       /* push argument onto stack */
100
101         btstl   #0, %d1                                         /* test if 2 word type */
102         beq     L_asm_vm_call_copy_1_word_type
103         movel   %a0@(offvmargdata), %d0         /* push second word onto stack */
104         movel   %d0,%sp@-
105 L_asm_vm_call_copy_1_word_type:
106
107         subl    #(sizevmarg),%a0
108         tstl    %d2                                                     /* arguments left ? */
109         bne     L_asm_vm_call_copy_arg
110
111 L_asm_vm_call_method_copy_done:
112
113         leal    asm_call_jit_compiler,%a4                       /* we need to fake a invocation as it would happen from jit code */
114         movel   %a4, call_jit_dummy                                     /* we need a writeable memory location */
115         moveal  call_jit_dummy, %a4                                     /* XXX do we have a race condition here ? */
116         jsr     %a4@
117
118 L_asm_vm_call_method_return:
119         movel   %a5, %sp                                                /* pop arguments off stack */
120         moveml  %sp@, %d2/%d3/%d4/%d5/%d6/%d7/%a2/%a3/%a4/%a5/%fp       /* restore registers */
121         addal   #(11*4),%sp                                             /* restore stack */
122         moveal  %d0, %a0                                                /* return value in %a0, too */
123 asm_vm_call_method_end:                                         /* symbol needed to insert method into avl tree */
124         rts                                                                             /* return to c code */
125
126 /* asm_vm_call_method_exception_handler ********************************************************************
127  *
128  * calls void *builtin_throw_exception(java_objectheader *xptr) when no other handler is appropiate
129  * this functions gets called indirectly from asm_handle_exception, which back then moved xptr to %a2
130  * clear software design is in the eye of the beholder.
131  ************************************************************************************************************/
132 asm_vm_call_method_exception_handler:
133         movel   %a2, %sp@-                      /* push xptr argument */
134         jsr     builtin_throw_exception
135         lea     %sp@(4), %sp                    /* pop arg off stack */
136         jmp     L_asm_vm_call_method_return
137
138
139 /* asm_call_jit_compiler ************************************************************************************
140  *      Invokes compiler for untranslated java methods.
141  *      C prototype: void asm_call_jit_compiler(void);
142  *      BUT: this does not match reality, arguments _ARE_ passed via createcompilerstub and asm_vm_call_method...
143  *      arguments passed via %a2(methodinfo) == REG_ATMP1
144  *                       %a3(mptr)       == REG_ATMP2
145  ************************************************************************************************************/
146 asm_call_jit_compiler:
147         addal   #(-4*4),%sp                                             /* create stackframe to save registers */
148         moveml  %a0/%a1/%d0/%d1,%sp@                    /* save volatile registers */
149 #if !defined(ENABLE_SOFTFLOAT)
150         fmoved  %fp0, %sp@-
151         fmoved  %fp1, %sp@-
152         movel   %sp@(8*4), %sp@-
153         pea             %sp@(8*4+8)
154 #else
155         movel   %sp@(4*4), %sp@-                                /* push arguments onto stack (ra)*/
156         pea     %sp@(4*4+8)                                             /* the old stack pointer*/
157 #endif
158         movel   %a3,%sp@-                                               /* mptr */
159         movel   %a2,%sp@-                                               /* methodinfo */
160
161         /* C prototype: u1 *jit_asm_compile(methodinfo *m, u1 *mptr, u1 *sp, u1 *ra); */
162         jsr     jit_asm_compile                                         /* invoke compiler */
163         addal   #(4*4),%sp                                              /* pop arguments off stack */
164         moveal  %d0, %a2                                                /* to tmp register */
165
166 #if !defined(ENABLE_SOFTFLOAT)
167         fdmoved %sp@+, %fp1
168         fdmoved %sp@+, %fp0
169 #endif
170
171         moveml %sp@,%a0/%a1/%d0/%d1                             /* restore volatile registers */
172         addal   #(4*4),%sp                                              /* remove stackframe */
173
174         tstl    %a2                                                             /* check for exception */
175         beq     L_asm_call_jit_compiler_exception
176
177         jmp     %a2@                                                            /* invoke java method */
178         jsr     0                                                               /* we should not ever return here */
179
180 L_asm_call_jit_compiler_exception:
181         jsr             exceptions_get_and_clear_exception              /* exception object in %d0 now */
182         moveal  %d0, %a2                                                                /* move exception object into sptr register */
183         moveal  %sp@+, %a3                                                              /* pop return address into exception address reg */
184         jmp asm_handle_exception                                                /* handle exception */
185
186
187 /* asm_patcher_wrapper ********************************************************
188         
189   prepares arguments on stack
190   calls patcher_wrapper signature: java_objectheader *patcher_wrapper(u1 *sp, u1 *pv, u1 *ra);
191
192   Stack layout:
193      24   return address
194      20   REG_ITMP3
195      16   pointer to virtual java_objectheader
196      12   last byte of machine code (xmcode)
197       8   machine code (which is patched back later)
198       4   unresolved field reference
199       0   patcher function pointer to call
200 *******************************************************************************/
201 asm_patcher_wrapper:
202
203   /* save scratch registers */
204   movel %a0, %sp@-
205   movel %a1, %sp@-
206   movel %d0, %sp@-
207   movel %d1, %sp@-
208
209 #if defined(ENABLE_SOFTFLOAT)
210   /* calculate original value of sp */
211   movel %sp, %d0
212   addil #4*4, %d0
213 #else
214   fmoved %fp0, %sp@-
215   fmoved %fp1, %sp@-
216
217   movel %sp, %d0
218   addil #8*4, %d0
219 #endif
220
221   clrl  %sp@-                           /* pass ra */
222   clrl  %sp@-                           /* pass pv, if null use findmethod */
223   movel %d0, %sp@-                      /* pass sp of patcher stub */
224   jsr   patcher_wrapper         /* return value in %d0 */
225
226   lea   %sp@(3*4), %sp          /* pop arguments off stack */
227   tst   %d0                                     /* test if exception occured */
228   bne   L_asm_patcher_wrapper_exception
229
230 #if !defined(ENABLE_SOFTFLOAT)
231   fdmoved %sp@+, %fp1
232   fdmoved %sp@+, %fp0
233 #endif
234   movel %sp@+, %d1
235   movel %sp@+, %d0
236   movel %sp@+, %a1
237   movel %sp@+, %a0
238
239   lea   %sp@(6*4), %sp                  /* restore stack and remove patcher stub*/
240   rts                                                   /* back to jit code */
241
242 L_asm_patcher_wrapper_exception:
243   /* WARNING: the stack is still disturbed, look at asm_patcher_wrapper for details */
244   /* we do not need to restore the content of the registers, I hope */
245 #if !defined(ENABLE_SOFTFLOAT)
246         lea             %sp@(6*4), %sp
247 #else
248         lea             %sp@(4*4), %sp
249 #endif
250
251   lea           %sp@(5*4), %sp                          /* restore stack and remove patcher stub*/
252   movel         %sp@+, %d4                                      /* restore REG_ITMP3, stored in emit_patcher_stubs */
253   moveal        %d0, %a2                                        /* xptr, pointer to exception object */
254   moveal        %sp@+, %a3                                      /* pop return address into exception address register */
255   jmp           asm_handle_exception            /* handle exception */
256   illegal                                                               /* XXX: we never come back */
257
258 /********************************************************************************
259         Reads a few values atomically. 
260         C signature:
261         void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out);
262
263         super ... sp@(4)
264         sub   ... sp@(8)
265         out   ... sp@(12)
266 ********************************************************************************/
267 asm_getclassvalues_atomic:
268 _crit_restart:
269 _crit_begin:
270         moveal  %sp@(4), %a0
271         movel   %a0@(offbaseval), %d0
272         movel   %a0@(offdiffval), %d1
273         
274         moveal  %sp@(8), %a0
275         moveal  %a0@(offbaseval), %a0
276 _crit_end:
277         moveal  %sp@(12), %a1
278         movel   %d0, %a1@(offcast_super_baseval)
279         movel   %d1, %a1@(offcast_super_diffval)
280         movel   %a0, %a1@(offcast_sub_baseval)
281
282         rts
283 .data
284 asm_criticalsections:
285 #if defined(ENABLE_THREADS)
286         .long _crit_begin
287         .long _crit_end
288         .long _crit_restart
289 #endif
290         .long 0
291 .text
292
293 asm_abstractmethoderror:
294
295 /* asm_handle_exception ********************************************************
296 *                                                                              *
297 *   This function handles an exception. It does not use the usual calling      *
298 *   conventions. The exception pointer is passed in REG_ATMP1 and the          *
299 *   pc from the exception raising position is passed in REG_ATMP2. It searches *
300 *   the local exception table for a handler. If no one is found, it unwinds    *
301 *   stacks and continues searching the callers.                                *
302 *                                                                              *
303 *   void asm_handle_exception (void);
304 *   exception object pointer...%a2   exception raising address...%a3           *
305 *                                                                              *
306 *******************************************************************************/
307 asm_handle_nat_exception:
308         lea     %sp@(4), %sp
309 asm_handle_exception:
310 L_asm_handle_exception_stack_loop:
311         
312         /* we need the dseg, figure it out */
313         movel   %a3, %sp@-                              /* push ra argument */
314         jsr     md_codegen_get_pv_from_pc       /* pv in %d0 now */
315         movel   %d0, %d2                                /* move to safe register */
316         lea     %sp@(4), %sp                            /* pop args off stack */
317
318         /* now call the following c function */
319         /* u1 *exceptions_handle_exception(java_objectheader *xptr, u1 *xpc, u1 *pv, u1 *sp) */
320         movel   %sp,%sp@-
321         movel   %d2,%sp@-
322         movel   %a3,%sp@-
323         movel   %a2,%sp@-
324         jsr     exceptions_handle_exception             /* %d0 is address of handler or 0 when not catched */
325         lea     %sp@(4*4), %sp                          /* pop args off stack */
326         tstl    %d0
327         beq     L_asm_handle_exception_not_catched
328
329         /* %d0 contains address of exception handler */
330         moveal  %d0, %a0
331         moveal  %a2, %a1                                        /* XXX FIXME, xptr is expected in %a1 by java code, whyever */
332         jmp     %a0@
333
334 L_asm_handle_exception_not_catched:
335         /* we did not find an exception handler in this stackframe */
336         /* remove this frame and search in the one above */
337         /* %a2 containts exception object ptr, %d2 the actual pv */
338         moveal  %d2, %a3
339         movel   %a3@(FrameSize), %d2
340
341         moveal  %sp, %a0
342         addal   %d2, %a0        /* %a0 now points to top of stackframe, where saved regs are */
343
344         /* the saved registers have to be restored */
345         /* XXX ugly hack: intsave and adrsave share one field */
346         movel   %a3@(IntSave), %d0
347         andil   #0x0000ffff, %d0        /* this is IntSave */
348         cmpb    #0,%d0
349         beq     L_asm_handle_ex_int_done
350         movel   -(%a0), %d7
351
352         cmpb    #1,%d0
353         beq     L_asm_handle_ex_int_done
354         movel   -(%a0), %d6
355
356         cmpb    #2,%d0 
357         beq     L_asm_handle_ex_int_done
358         movel   -(%a0), %d5
359
360 L_asm_handle_ex_int_done:
361
362         movel   %a3@(IntSave), %d0
363         andil   #0xffff0000, %d0        /* this is AdrSave */
364         tstl    %d0
365         beq     L_handle_exception_nat_catched_no_adr
366         moveal  -(%a0), %a5
367 L_handle_exception_nat_catched_no_adr:
368
369 #if !defined(ENABLE_SOFTFLOAT)
370         movel   %a3@(FltSave), %d0
371         cmpb    #0,%d0
372         beq     L_asm_handle_ex_flt_done
373         fdmoved -(%a0), %fp7
374
375         cmpb    #1,%d0
376         beq     L_asm_handle_ex_flt_done
377         fdmoved -(%a0), %fp6
378         
379         cmpb    #2,%d0
380         beq     L_asm_handle_ex_flt_done
381         fdmoved -(%a0), %fp5
382
383 L_asm_handle_ex_flt_done:
384 #else
385         /* nothing to do */
386 #endif
387         addal   %d2, %sp                /* remove old stackframe */
388         moveal  %sp@+, %a3              /* make return address, new exception rasing address */
389         subal   #2, %a3                 /* it was a jsr %aX, which is 4 bytes long */
390         jmp     L_asm_handle_exception_stack_loop
391
392
393 illegal
394 .long 0x23234242
395
396
397 /*
398  * These are local overrides for various environment variables in Emacs.
399  * Please do not remove this and leave it at the end of the file, where
400  * Emacs will automagically detect them.
401  * ---------------------------------------------------------------------
402  * Local variables:
403  * mode: c
404  * indent-tabs-mode: t
405  * c-basic-offset: 4
406  * tab-width: 4
407  * End:
408  * vim:noexpandtab:sw=4:ts=4:
409  */