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