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