3e7bacb2427beb23d782587f7a30648fdb606cc0
[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 8276 2007-08-08 16:09:14Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include "vm/jit/arm/md-asm.h"
33
34 #include "vm/jit/methodheader.h"
35
36
37         .file "asmpart.S"
38         .text
39         .align 2
40
41
42 /* export functions ***********************************************************/
43
44         .globl asm_vm_call_method
45         .globl asm_vm_call_method_int
46         .globl asm_vm_call_method_long
47         .globl asm_vm_call_method_float
48         .globl asm_vm_call_method_double
49         .globl asm_vm_call_method_exception_handler
50         .globl asm_vm_call_method_end
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_cacheflush
60
61 #if !defined(ENABLE_THREADS)
62 asm_exceptionptr:
63         .word _no_threads_exceptionptr
64 #endif
65
66
67 /* asm_vm_call_method **********************************************************
68
69    This function calls a Java-method (which possibly needs compilation)
70    with up to 4 address parameters.
71
72    This functions calls the JIT-compiler which eventually translates the
73    method into machine code.
74
75 *******************************************************************************/
76
77         .align  2
78
79         .word   0                           /* catch type all                     */
80         .word   0                           /* handler pc                         */
81         .word   0                           /* end pc                             */
82         .word   0                           /* start pc                           */
83         .word   1                           /* extable size                       */
84         .word   0                           /* line number table start            */
85         .word   0                           /* line number table size             */
86         .word   0                           /* FltSave                            */
87         .word   0                           /* IntSave                            */
88         .word   0                           /* IsLeaf                             */
89         .word   0                           /* IsSync                             */
90         .word   0                           /* FrameSize                          */
91         .word   0                           /* CodeinfoPointer                    */
92
93 asm_vm_call_method:
94 asm_vm_call_method_int:
95 asm_vm_call_method_long:
96 /* asm_vm_call_method_float:
97 asm_vm_call_method_double: */
98         SAVE_SCRATCH_REGISTERS            /* save our personal scratch regs   */
99         stmfd sp!, {v1}                   /* V1 is used to remember SP        */
100         str   a0, [sp, #-4]!              /* store methods entrypoint         */
101
102         mov   v1, sp                      /* remember SP                      */
103
104         mov   itmp1, a1                   /* address of data structure        */
105         mov   itmp3, a2                   /* stack argument count             */
106
107         ldr   a0, [itmp1], #8             /* load argument registers          */
108         ldr   a1, [itmp1], #8
109         ldr   a2, [itmp1], #8
110         ldr   a3, [itmp1], #8
111
112         cmp   itmp3, #0                   /* do we have stack arguments?      */
113         ble   asm_calljava_copyfinish     /* no -> do not care :-)            */
114
115         mov   itmp2, #0
116         sub   sp, sp, itmp3, lsl #3       /* create stackframe for arguments  */
117 asm_calljava_copyloop:                /* reorder stack arguments!         */
118         ldr   ip, [itmp1], #4             /* load argument                    */
119         str   ip, [sp, itmp2]             /* store argument on stack          */
120         add   itmp2, itmp2, #4            /* next stackslot                   */
121         ldr   ip, [itmp1], #4             /* load argument                    */
122         str   ip, [sp, itmp2]             /* store argument on stack          */
123         add   itmp2, itmp2, #4            /* next stackslot                   */
124         subs  itmp3, itmp3, #1            /* next argument                    */
125         bgt   asm_calljava_copyloop
126
127 asm_calljava_copyfinish:
128         mov   mptr, v1                    /* set method pointer               */
129
130         /* REMEMBER: do the method call just like in java! */
131         ldr   ip, [mptr]                  /* fake virtual function call       */
132         mov   lr, pc
133         mov   pc, ip
134 fake:
135         sub   ip, pc, #(fake - asm_vm_call_method)+8
136
137         mov   sp, v1                      /* restore SP                       */
138         add   sp, sp, #4                  /* free fake address                */
139         ldmfd sp!, {v1}
140         RESTORE_SCRATCH_REGS_AND_RETURN   /* return to caller, restore regs   */
141
142 asm_vm_call_method_exception_handler:
143         mov   a0, xptr                    /* exception pointer is arg1        */
144         bl    builtin_throw_exception     /* throw the exception              */
145         mov   res1, #0                    /* return NULL                      */
146         mov   res2, #0                    /* return NULL                      */
147         mov   sp, v1                      /* restore SP                       */
148         add   sp, sp, #4                  /* free fake address                */
149         ldmfd sp!, {v1}
150         RESTORE_SCRATCH_REGS_AND_RETURN   /* return to caller, restore regs   */
151
152 asm_vm_call_method_float:
153         mov a0,#0x51
154         b asm_debug
155 asm_vm_call_method_double:
156         mov a0,#0x52
157         b asm_debug
158
159 asm_vm_call_method_end:
160
161
162 /****************** function asm_call_jit_compiler *****************************
163 *                                                                              *
164 *   Invokes the compiler for untranslated JavaVM methods.                      *
165 *   What this method does:                                                     *
166 *    - save args and LR                                                        *
167 *    - fire up jit_compile (pass methodinfo pointer)                           *
168 *    - try to find out where to write back the new method pointer              *
169 *    - restore args and LR                                                     *
170 *    - check for exceptions                                                    *
171 *    - eventually write back new method pointer                                *
172 *    - call jit code (wich will then return to caller)                         *
173 *                                                                              *
174 *   These methods can call us: codegen_compilerstub & asm_calljavafunction     *
175 *   ATTENTION: use REG_ITMP1 to pass methodinfo pointer to me!                 *
176 *                                                                              *
177 *******************************************************************************/
178
179 #define MYSTACKSIZE (6*4)
180
181 asm_call_jit_compiler:
182         SAVE_ARGUMENT_REGISTERS             /* save our argument registers & LR   */
183         sub   sp, sp, #4                    /* keep stack 8-byte aligned          */
184
185         mov   a0, itmp1                     /* pass methodinfo pointer            */
186         mov   a1, mptr                      /* pass method pointer                */
187         add   a2, sp, #MYSTACKSIZE          /* pass Java sp                       */
188         mov   a3, lr                        /* pass Java RA (correct for leafs)   */
189         bl    jit_asm_compile
190         mov   itmp1, res1                   /* save pointer to new jit-code       */
191
192         tst   itmp1,itmp1                   /* check for exeption                 */
193         beq   L_asm_call_jit_compiler_exception
194
195         add   sp, sp, #4                    /* keep stack 8-byte aligned          */
196         RESTORE_ARGUMENT_REGISTERS          /* load our argument registers & LR   */
197
198         mov   ip, itmp1
199         mov   pc, ip                        /* call jit-code                      */
200
201 L_asm_call_jit_compiler_exception:
202         bl    exceptions_get_and_clear_exception
203         mov   xptr, res1                    /* get exception                      */
204
205         add   sp, sp, #4                    /* keep stack 8-byte aligned          */
206         RESTORE_ARGUMENT_REGISTERS          /* load LR                            */
207
208         sub   xpc, lr, #4                   /* xpc = instruction that called us   */
209         b     asm_handle_nat_exception
210
211
212 /********************* function asm_handle_exception ***************************
213 *                                                                              *
214 *   This function handles an exception. It does not use the usual calling      *
215 *   conventions. The exception pointer is passed in REG_ITMP1 and the          *
216 *   pc from the exception raising position is passed in REG_ITMP2. It searches *
217 *   the local exception table for a handler. If no one is found, it unwinds    *
218 *   stacks and continues searching the callers.                                *
219 *                                                                              *
220 *   void asm_handle_exception (exceptionptr, exceptionpc);                     *
221 *                                                                              *
222 *******************************************************************************/
223
224 asm_handle_nat_exception:
225         /*TODO:maybe make a macro out of it!!!*/
226         SAVE_ARGUMENT_REGISTERS  
227         mov   a0, lr
228         bl    md_codegen_get_pv_from_pc
229         mov   ip, res1
230         RESTORE_ARGUMENT_REGISTERS  
231         /* fall through */
232
233 asm_handle_exception:
234         stmfd sp!, {r0 - r3}               /* save possible used registers    */
235         mov   itmp3, #1                    /* set maybe-leaf flag             */
236         mov   a3, #(4*4)                   /* prepare a3 for handle_exception */
237
238 asm_handle_exception_loop:
239         stmfd sp!, {ip,lr}                 /* call exception helper here!     */
240         mov   a0, xptr                     /* pass exception pointer          */
241         mov   a1, xpc                      /* pass exception pointer          */
242         mov   a2, ip                       /* pass data segment pointer       */
243         add   a3, sp, a3                   /* calculate Java sp into a3...    */
244         add   a3, a3, #(2*4)
245         bl    exceptions_handle_exception
246         ldmfd sp!, {ip,lr}
247
248         tst   a0, a0
249         beq   asm_handle_exception_not_catched
250
251         mov   xpc, a0                      /* move handlerpc into xpc         */
252         tst   itmp3,itmp3                  /* if this is a lead method ...    */
253         ldmnefd sp!, {r0 - r3}             /* restore argument registers      */
254
255         mov   pc, xpc                      /* jump to handler                 */
256
257 asm_handle_exception_not_catched:
258         tst   itmp3,itmp3                  /* if this is a lead method ...    */
259         addne sp, sp, #(4*4)               /* remove maybe-leaf stackframe    */
260         movne itmp3, #0                    /* remove maybe-leaf flag          */
261
262         ldr   a2, [ip, #FrameSize]         /* t2 = frame size                 */
263         add   a0, sp, a2                   /* t0 = pointer to save area       */
264         ldr   a1, [ip, #IsLeaf]            /* t1 = is leaf procedure          */
265         tst   a1, a1                       /* if is leaf ...                  */
266         ldreq lr, [a0, #-4]!               /* ... restore RA                  */
267         mov   xpc, lr                      /* the new xpc is RA               */
268
269         ldr   a1, [ip, #IntSave]           /* t1 = saved int register count   */
270         rsb   a1, a1, #5                   /* t1 = count of unsaved registers */
271         sub   a1, a1, #1
272         add   pc, pc, a1, lsl #2           /* do not load unsaved registers   */
273         ldr   v1, [a0, #-20]               /* ... but restore the other ones  */
274         ldr   v2, [a0, #-16]
275         ldr   v3, [a0, #-12]
276         ldr   v4, [a0, #- 8]
277         ldr   v5, [a0, #- 4]
278
279         add   sp, sp, a2                   /* unwind stack (using t2)         */
280         mov   a3, #0                       /* prepare a3 for handle_exception */
281
282         /*TODO:maybe make a macro out of it!!!*/
283         SAVE_ARGUMENT_REGISTERS  
284         mov   a0, lr
285         bl    md_codegen_get_pv_from_pc
286         mov   ip, res1
287         RESTORE_ARGUMENT_REGISTERS  
288
289         b     asm_handle_exception_loop
290
291
292 /* asm_abstractmethoderror *****************************************************
293
294    Creates and throws an AbstractMethodError.
295
296 *******************************************************************************/
297
298 asm_abstractmethoderror:
299         stmfd sp!, {lr}                     /* save return address                */
300         add   a0, sp, #(1*4)                /* pass java sp                       */
301         mov   a1, lr                        /* pass exception address             */
302         bl    exceptions_asm_new_abstractmethoderror
303         ldmfd sp!, {lr}                     /* restore return address             */
304
305         mov   xptr, res1                    /* get exception pointer              */
306         sub   xpc, lr, #4                   /* exception address is ra - 4        */
307         b     asm_handle_nat_exception
308
309                 
310 /********************* function asm_cacheflush *********************************
311 *                                                                              *
312 *   TODO: document me                                                          *
313 *                                                                              *
314 *   void asm_cacheflush(void *p, s4 size);                                     *
315 *                                                                              *
316 *******************************************************************************/
317
318 #if 1
319 .equ sys_cacheflush, 0x9f0002
320 asm_cacheflush:
321         add   a1, a0, a1
322         mov   a2, #0
323
324 #if 0
325         /* TWISTI: required on iyonix, maybe a linux-2.4 bug */
326         /* TODO: repeair this! */
327         /* cacheflush is messed up beyond all repair! */
328         mov a0, #0x0
329         mov a1, #0xff000000
330 #endif
331
332         swi   #sys_cacheflush
333         mov   pc, lr
334 #endif
335
336
337 /* disable exec-stacks ********************************************************/
338
339 #if defined(__linux__) && defined(__ELF__)
340         .section .note.GNU-stack,"",%progbits
341 #endif
342
343
344 /*
345  * These are local overrides for various environment variables in Emacs.
346  * Please do not remove this and leave it at the end of the file, where
347  * Emacs will automagically detect them.
348  * ---------------------------------------------------------------------
349  * Local variables:
350  * mode: asm
351  * indent-tabs-mode: t
352  * c-basic-offset: 4
353  * tab-width: 4
354  * End:
355  * vim:noexpandtab:sw=4:ts=4:
356  */