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