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