* src/vm/jit/arm/emit.c (emit_verbosecall_enter): Keep stack 8-byte
[cacao.git] / src / vm / jit / arm / asmpart.S
1 /* src/vm/jit/arm/asmpart.S - Java-C interface functions for ARM
2
3    Copyright (C) 1996-2005, 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    $Id: asmpart.S 7325 2007-02-11 20:10:35Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include "vm/jit/arm/offsets.h"
33 #include "vm/jit/arm/md-asm.h"
34
35 #include "vm/jit/methodheader.h"
36
37
38         .file "asmpart.S"
39         .text
40         .align 2
41
42
43 /* export functions ***********************************************************/
44
45         .globl asm_vm_call_method
46         .globl asm_vm_call_method_int
47         .globl asm_vm_call_method_long
48         .globl asm_vm_call_method_float
49         .globl asm_vm_call_method_double
50         .globl asm_vm_call_method_exception_handler
51
52         .globl asm_call_jit_compiler
53
54         .globl asm_handle_exception
55         .globl asm_handle_nat_exception
56
57         .globl asm_abstractmethoderror
58
59         .globl asm_patcher_wrapper
60
61         .globl asm_cacheflush
62
63         .globl asm_getclassvalues_atomic
64         .globl asm_criticalsections
65
66
67 #if !defined(ENABLE_THREADS)
68 asm_exceptionptr:
69         .word _no_threads_exceptionptr
70 #endif
71
72 asm_jitcompilerptr:
73         .word asm_call_jit_compiler
74
75 asm_criticalsections:
76 #if defined(ENABLE_THREADS)
77         .word _crit_begin
78         .word _crit_end
79         .word _crit_restart
80 #endif
81         .word 0
82
83
84 /* asm_vm_call_method **********************************************************
85
86    This function calls a Java-method (which possibly needs compilation)
87    with up to 4 address parameters.
88
89    This functions calls the JIT-compiler which eventually translates the
90    method into machine code.
91
92 *******************************************************************************/
93
94         .align  2
95
96         .word   0                           /* catch type all                     */
97         .word   0                           /* handler pc                         */
98         .word   0                           /* end pc                             */
99         .word   0                           /* start pc                           */
100         .word   1                           /* extable size                       */
101         .word   0                           /* line number table start            */
102         .word   0                           /* line number table size             */
103         .word   0                           /* FltSave                            */
104         .word   0                           /* IntSave                            */
105         .word   0                           /* IsLeaf                             */
106         .word   0                           /* IsSync                             */
107         .word   0                           /* FrameSize                          */
108         .word   0                           /* CodeinfoPointer                    */
109
110 asm_vm_call_method:
111 asm_vm_call_method_int:
112 asm_vm_call_method_long:
113 /* asm_vm_call_method_float:
114 asm_vm_call_method_double: */
115         SAVE_SCRATCH_REGISTERS            /* save our personal scratch regs   */
116         stmfd sp!, {v1}                   /* V1 is used to recompute SP ...   */
117         mov   v1, #0                      /* ... when using stack arguments   */
118         ldr   ip, asm_jitcompilerptr
119         str   ip, [sp, #-4]!              /* store fake address               */
120         mov   mptr, sp                    /* set method pointer               */
121
122         mov   itmp1, a0                   /* pass methodinfo* via ITMP1       */
123
124         cmp   a1, #0                      /* do we have arguments?            */
125         ble   asm_calljava_copyfinish     /* no -> do not care :-)            */
126
127         /* REMEMBER: stack space for arguments is reserved here! */
128         /* TODO: we possibly reserve to much here */
129         mov   v1, a1, lsl #3              /* how much stack do we alloc?      */
130         sub   sp, sp, v1                  /* allocate stack for arguments!    */
131
132         mov   itmp3, #0                   /* stack position                   */
133 asm_calljava_copyloop:                /* reorder stack arguments!         */
134 #if defined(__ARMEL__)
135         ldr   ip, [a2,#offvmargdata]      /* get LOW word of argument         */
136         str   ip, [sp, itmp3]
137         add   itmp3, itmp3, #4
138         ldr   ip, [a2,#offvmargtype]      /* is it a 2_WORD_TYPE?             */
139         tst   ip, #1
140         ldrne ip, [a2,#offvmargdata + 4]  /* yes -> get HIGH word of argument */
141         strne ip, [sp, itmp3]
142         addne itmp3, itmp3, #4
143 #else /* defined(__ARMEB__) */
144         ldr   ip, [a2,#offvmargtype + 4]  /* get our item type (it is u8)    */
145         teq   ip, #2                      /* is it a TYPE_FLOAT?              */
146         ldreq ip, [a2,#offvmargdata]      /* yes -> get LOW word of float     */
147         streq ip, [sp, itmp3]
148         addeq itmp3, itmp3, #4
149         beq   asm_calljava_copydone
150         tst   ip, #1                      /* is it a 2_WORD_TYPE?             */
151         ldrne ip, [a2,#offvmargdata]      /* yes -> get HIGH word of argument */
152         strne ip, [sp, itmp3]
153         addne itmp3, itmp3, #4
154         ldr   ip, [a2,#offvmargdata + 4]  /* get LOW word of argument         */
155         str   ip, [sp, itmp3]
156         add   itmp3, itmp3, #4
157 asm_calljava_copydone:
158 #endif
159         add   a2, a2, #sizevmarg          /* next argument block              */
160         subs  a1, a1, #1
161         bgt   asm_calljava_copyloop
162
163         /* REMEMBER: first four args are passed in regs, take them out again  */
164         ldmfd sp, {a0, a1, a2, a3}        /* load first four args to register */
165         cmp   v1, #16                     /* do we have four arguments?       */
166         addlt sp, sp, v1
167         movlt v1, #0
168         addge sp, sp, #16
169         subge v1, v1, #16
170
171 asm_calljava_copyfinish:
172         /* REMEMBER: do the method call just like in java! */
173         ldr   ip, [mptr]                  /* fake virtual function call       */
174         mov   lr, pc 
175         mov   pc, ip 
176 fake2:
177         sub   ip, pc, #(fake2 - asm_vm_call_method)+8
178
179         add   sp, sp, v1                  /* free stack arguments!            */
180         add   sp, sp, #4                  /* free fake address                */
181         ldmfd sp!, {v1}
182         RESTORE_SCRATCH_REGS_AND_RETURN   /* return to caller, restore regs   */
183
184 asm_vm_call_method_exception_handler:
185         mov   a0, xptr                    /* exception pointer is arg1        */
186         bl    builtin_throw_exception     /* throw the exception              */
187         mov   res1, #0                    /* return NULL                      */
188         mov   res2, #0                    /* return NULL                      */
189         add   sp, sp, v1                  /* free stack arguments!            */
190         add   sp, sp, #4                  /* free fake address                */
191         ldmfd sp!, {v1}
192         RESTORE_SCRATCH_REGS_AND_RETURN   /* return to caller, restore regs   */
193
194 asm_vm_call_method_float:
195         mov a0,#0x51
196         b asm_debug
197 asm_vm_call_method_double:
198         mov a0,#0x52
199         b asm_debug
200
201
202 /****************** function asm_call_jit_compiler *****************************
203 *                                                                              *
204 *   Invokes the compiler for untranslated JavaVM methods.                      *
205 *   What this method does:                                                     *
206 *    - save args and LR                                                        *
207 *    - fire up jit_compile (pass methodinfo pointer)                           *
208 *    - try to find out where to write back the new method pointer              *
209 *    - restore args and LR                                                     *
210 *    - check for exceptions                                                    *
211 *    - eventually write back new method pointer                                *
212 *    - call jit code (wich will then return to caller)                         *
213 *                                                                              *
214 *   These methods can call us: codegen_compilerstub & asm_calljavafunction     *
215 *   ATTENTION: use REG_ITMP1 to pass methodinfo pointer to me!                 *
216 *                                                                              *
217 *******************************************************************************/
218
219 #define MYSTACKSIZE (6*4)
220
221 asm_call_jit_compiler:
222         SAVE_ARGUMENT_REGISTERS             /* save our argument registers & LR   */
223         sub   sp, sp, #4                    /* keep stack 8-byte aligned          */
224
225         mov   a0, itmp1                     /* pass methodinfo pointer            */
226         mov   a1, mptr                      /* pass method pointer                */
227         add   a2, sp, #MYSTACKSIZE          /* pass Java sp                       */
228         mov   a3, lr                        /* pass Java RA (correct for leafs)   */
229         bl    jit_asm_compile
230         mov   itmp1, res1                   /* save pointer to new jit-code       */
231
232         tst   itmp1,itmp1                   /* check for exeption                 */
233         beq   L_asm_call_jit_compiler_exception
234
235         add   sp, sp, #4                    /* keep stack 8-byte aligned          */
236         RESTORE_ARGUMENT_REGISTERS          /* load our argument registers & LR   */
237
238         mov   ip, itmp1
239         mov   pc, ip                        /* call jit-code                      */
240
241 L_asm_call_jit_compiler_exception:
242         bl    exceptions_get_and_clear_exception
243         mov   xptr, res1                    /* get exception                      */
244
245         add   sp, sp, #4                    /* keep stack 8-byte aligned          */
246         RESTORE_ARGUMENT_REGISTERS          /* load LR                            */
247
248         sub   xpc, lr, #4                   /* xpc = instruction that called us   */
249         b     asm_handle_nat_exception
250
251
252 /********************* function asm_handle_exception ***************************
253 *                                                                              *
254 *   This function handles an exception. It does not use the usual calling      *
255 *   conventions. The exception pointer is passed in REG_ITMP1 and the          *
256 *   pc from the exception raising position is passed in REG_ITMP2. It searches *
257 *   the local exception table for a handler. If no one is found, it unwinds    *
258 *   stacks and continues searching the callers.                                *
259 *                                                                              *
260 *   void asm_handle_exception (exceptionptr, exceptionpc);                     *
261 *                                                                              *
262 *******************************************************************************/
263
264 asm_handle_nat_exception:
265         /*TODO:maybe make a macro out of it!!!*/
266         SAVE_ARGUMENT_REGISTERS  
267         mov   a0, lr
268         bl    md_codegen_get_pv_from_pc
269         mov   ip, res1
270         RESTORE_ARGUMENT_REGISTERS  
271         /* fall through */
272
273 asm_handle_exception:
274         stmfd sp!, {r0 - r3}               /* save possible used registers    */
275         mov   itmp3, #1                    /* set maybe-leaf flag             */
276         mov   a3, #(4*4)                   /* prepare a3 for handle_exception */
277
278 asm_handle_exception_loop:
279         stmfd sp!, {ip,lr}                 /* call exception helper here!     */
280         mov   a0, xptr                     /* pass exception pointer          */
281         mov   a1, xpc                      /* pass exception pointer          */
282         mov   a2, ip                       /* pass data segment pointer       */
283         add   a3, sp, a3                   /* calculate Java sp into a3...    */
284         add   a3, a3, #(2*4)
285         bl    exceptions_handle_exception
286         ldmfd sp!, {ip,lr}
287
288         tst   a0, a0
289         beq   asm_handle_exception_not_catched
290
291         mov   xpc, a0                      /* move handlerpc into xpc         */
292         tst   itmp3,itmp3                  /* if this is a lead method ...    */
293         ldmnefd sp!, {r0 - r3}             /* restore argument registers      */
294
295         mov   pc, xpc                      /* jump to handler                 */
296
297 asm_handle_exception_not_catched:
298         tst   itmp3,itmp3                  /* if this is a lead method ...    */
299         addne sp, sp, #(4*4)               /* remove maybe-leaf stackframe    */
300         movne itmp3, #0                    /* remove maybe-leaf flag          */
301
302         ldr   a2, [ip, #FrameSize]         /* t2 = frame size                 */
303         add   a0, sp, a2                   /* t0 = pointer to save area       */
304         ldr   a1, [ip, #IsLeaf]            /* t1 = is leaf procedure          */
305         tst   a1, a1                       /* if is leaf ...                  */
306         ldreq lr, [a0, #-4]!               /* ... restore RA                  */
307         mov   xpc, lr                      /* the new xpc is RA               */
308
309         ldr   a1, [ip, #IntSave]           /* t1 = saved int register count   */
310         rsb   a1, a1, #5                   /* t1 = count of unsaved registers */
311         sub   a1, a1, #1
312         add   pc, pc, a1, lsl #2           /* do not load unsaved registers   */
313         ldr   v1, [a0, #-20]               /* ... but restore the other ones  */
314         ldr   v2, [a0, #-16]
315         ldr   v3, [a0, #-12]
316         ldr   v4, [a0, #- 8]
317         ldr   v5, [a0, #- 4]
318
319         add   sp, sp, a2                   /* unwind stack (using t2)         */
320         mov   a3, #0                       /* prepare a3 for handle_exception */
321
322         /*TODO:maybe make a macro out of it!!!*/
323         SAVE_ARGUMENT_REGISTERS  
324         mov   a0, lr
325         bl    md_codegen_get_pv_from_pc
326         mov   ip, res1
327         RESTORE_ARGUMENT_REGISTERS  
328
329         b     asm_handle_exception_loop
330
331
332 /* asm_patcher_wrapper *********************************************************
333 *                                                                              *
334 *   TODO: document me                                                          *
335 *                                                                              *
336 *   Stack layout when calling patcher function:                                *
337 *    24   saved REG_ITMP3, should be restored                ( -4)             *
338 *    20   data segment displacement from load instructions   ( -8)             *
339 *    16   return address into JIT code (patch position)      (-12)             *
340 *    12   pointer to virtual java_objectheader                                 *
341 *     8   machine code (which is patched back later)                           *
342 *   [ 8   result of patcher function (indicates exception)  ]                  *
343 *     4   unresolved class/method/field reference                              *
344 *   [ 0   patcher function pointer to call                  ]                  *
345 *     0   saved IP of caller (caller needs it!)                                *
346 *                                                                              *
347 *******************************************************************************/
348
349 #define PATCHSTACKSIZE 8*4
350
351 asm_patcher_wrapper:
352         mov   itmp3, sp                     /* preserve original SP in ITMP3      */
353
354         SAVE_ARGUMENT_REGISTERS_IP          /* save our argument registers & LR   */
355         SAVE_FLOAT_REGISTERS                /* save our float registers here      */
356
357         mov   a0, itmp3                     /* pass SP of patcher stub            */
358         mov   a1, ip                        /* pass PV                            */
359         mov   a2, lr                        /* pass RA (correct for leafs)        */
360         bl    patcher_wrapper
361         mov   itmp3, res1                   /* save return value                  */
362
363         RESTORE_FLOAT_REGISTERS             /* restore our float registers here   */
364         RESTORE_ARGUMENT_REGISTERS_IP       /* load our argument registers & LR   */
365
366         tst   itmp3, itmp3                  /* check for an exception             */
367         bne   L_asm_patcher_wrapper_exception
368
369         add   sp, sp, #PATCHSTACKSIZE       /* remove patcher stack frame         */
370
371         ldr   itmp3, [sp, #-8]              /* restore ITMP3 for calling method   */
372         ldr   pc, [sp, #-16]                /* jump to new patched code           */
373
374 L_asm_patcher_wrapper_exception:
375         mov   xptr, itmp3                   /* get exception                      */
376         ldr   xpc, [sp, #16]                /* RA is xpc                          */
377
378         add   sp, sp, #PATCHSTACKSIZE       /* remove patcher stack frame         */
379
380         b     asm_handle_exception
381
382
383 /* asm_abstractmethoderror *****************************************************
384
385    Creates and throws an AbstractMethodError.
386
387 *******************************************************************************/
388
389 asm_abstractmethoderror:
390         stmfd sp!, {lr}                     /* save return address                */
391         add   a0, sp, #(1*4)                /* pass java sp                       */
392         mov   a1, lr                        /* pass exception address             */
393         bl    exceptions_asm_new_abstractmethoderror
394         ldmfd sp!, {lr}                     /* restore return address             */
395
396         mov   xptr, res1                    /* get exception pointer              */
397         sub   xpc, lr, #4                   /* exception address is ra - 4        */
398         b     asm_handle_nat_exception
399
400                 
401 /********************* function asm_cacheflush *********************************
402 *                                                                              *
403 *   TODO: document me                                                          *
404 *                                                                              *
405 *   void asm_cacheflush(void *p, s4 size);                                     *
406 *                                                                              *
407 *******************************************************************************/
408
409 #if 1
410 .equ sys_cacheflush, 0x9f0002
411 asm_cacheflush:
412         add   a1, a0, a1
413         mov   a2, #0
414
415 #if 0
416         /* TWISTI: required on iyonix, maybe a linux-2.4 bug */
417         /* TODO: repeair this! */
418         /* cacheflush is messed up beyond all repair! */
419         mov a0, #0x0
420         mov a1, #0xff000000
421 #endif
422
423         swi   #sys_cacheflush
424         mov   pc, lr
425 #endif
426
427
428 /********************* function asm_getclassvalues_atomic *********************/
429
430 asm_getclassvalues_atomic:
431         stmfd sp!, {r4, r5, r6}
432 _crit_restart:
433 _crit_begin:
434         ldr   r4,[a0,#offbaseval]
435         ldr   r5,[a0,#offdiffval]
436         ldr   r6,[a1,#offbaseval]
437 _crit_end:
438         str   r4,[a2,#offcast_super_baseval]
439         str   r5,[a2,#offcast_super_diffval]
440         str   r6,[a2,#offcast_sub_baseval]
441         ldmfd sp!, {r4, r5, r6}
442         mov   pc, lr
443
444
445 /* Disable exec-stacks, required for Gentoo ***********************************/
446
447 #if defined(__GCC__) && defined(__ELF__)
448         .section .note.GNU-stack,"",@progbits
449 #endif
450
451
452 /*
453  * These are local overrides for various environment variables in Emacs.
454  * Please do not remove this and leave it at the end of the file, where
455  * Emacs will automagically detect them.
456  * ---------------------------------------------------------------------
457  * Local variables:
458  * mode: asm
459  * indent-tabs-mode: t
460  * c-basic-offset: 4
461  * tab-width: 4
462  * End:
463  * vim:noexpandtab:sw=4:ts=4:
464  */