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