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