Merged revisions 7918-7939 via svnmerge from
[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 7932 2007-05-22 07:00:57Z michi $
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_patcher_wrapper
61
62         .globl asm_cacheflush
63
64         .globl asm_getclassvalues_atomic
65         .globl asm_criticalsections
66
67
68 #if !defined(ENABLE_THREADS)
69 asm_exceptionptr:
70         .word _no_threads_exceptionptr
71 #endif
72
73 asm_jitcompilerptr:
74         .word asm_call_jit_compiler
75
76 asm_criticalsections:
77 #if defined(ENABLE_THREADS)
78         .word _crit_begin
79         .word _crit_end
80         .word _crit_restart
81 #endif
82         .word 0
83
84
85 /* asm_vm_call_method **********************************************************
86
87    This function calls a Java-method (which possibly needs compilation)
88    with up to 4 address parameters.
89
90    This functions calls the JIT-compiler which eventually translates the
91    method into machine code.
92
93 *******************************************************************************/
94
95         .align  2
96
97         .word   0                           /* catch type all                     */
98         .word   0                           /* handler pc                         */
99         .word   0                           /* end pc                             */
100         .word   0                           /* start pc                           */
101         .word   1                           /* extable size                       */
102         .word   0                           /* line number table start            */
103         .word   0                           /* line number table size             */
104         .word   0                           /* FltSave                            */
105         .word   0                           /* IntSave                            */
106         .word   0                           /* IsLeaf                             */
107         .word   0                           /* IsSync                             */
108         .word   0                           /* FrameSize                          */
109         .word   0                           /* CodeinfoPointer                    */
110
111 asm_vm_call_method:
112 asm_vm_call_method_int:
113 asm_vm_call_method_long:
114 /* asm_vm_call_method_float:
115 asm_vm_call_method_double: */
116         SAVE_SCRATCH_REGISTERS            /* save our personal scratch regs   */
117         stmfd sp!, {v1}                   /* V1 is used to recompute SP ...   */
118         mov   v1, #0                      /* ... when using stack arguments   */
119         ldr   ip, asm_jitcompilerptr
120         str   ip, [sp, #-4]!              /* store fake address               */
121         mov   mptr, sp                    /* set method pointer               */
122
123         mov   itmp1, a0                   /* pass methodinfo* via ITMP1       */
124
125         cmp   a1, #0                      /* do we have arguments?            */
126         ble   asm_calljava_copyfinish     /* no -> do not care :-)            */
127
128         /* REMEMBER: stack space for arguments is reserved here! */
129         /* TODO: we possibly reserve to much here */
130         mov   v1, a1, lsl #3              /* how much stack do we alloc?      */
131         sub   sp, sp, v1                  /* allocate stack for arguments!    */
132
133         mov   itmp3, #0                   /* stack position                   */
134 asm_calljava_copyloop:                /* reorder stack arguments!         */
135 #if defined(__ARMEL__)
136         ldr   ip, [a2,#offvmargtype]      /* align 2_WORD_TYPEs               */
137         tst   ip, #1
138         tstne itmp3, #4
139         addne itmp3, itmp3, #4
140         ldr   ip, [a2,#offvmargdata]      /* get LOW word of argument         */
141         str   ip, [sp, itmp3]
142         add   itmp3, itmp3, #4
143         ldr   ip, [a2,#offvmargtype]      /* is it a 2_WORD_TYPE?             */
144         tst   ip, #1
145         ldrne ip, [a2,#offvmargdata + 4]  /* yes -> get HIGH word of argument */
146         strne ip, [sp, itmp3]
147         addne itmp3, itmp3, #4
148 #else /* defined(__ARMEB__) */
149         ldr   ip, [a2,#offvmargtype + 4]  /* get our item type (it is u8)    */
150         tst   ip, #1                      /* align 2_WORD_TYPEs               */
151         tstne itmp3, #4
152         addne itmp3, itmp3, #4
153         teq   ip, #2                      /* is it a TYPE_FLOAT?              */
154         ldreq ip, [a2,#offvmargdata]      /* yes -> get LOW word of float     */
155         streq ip, [sp, itmp3]
156         addeq itmp3, itmp3, #4
157         beq   asm_calljava_copydone
158         tst   ip, #1                      /* is it a 2_WORD_TYPE?             */
159         ldrne ip, [a2,#offvmargdata]      /* yes -> get HIGH word of argument */
160         strne ip, [sp, itmp3]
161         addne itmp3, itmp3, #4
162         ldr   ip, [a2,#offvmargdata + 4]  /* get LOW word of argument         */
163         str   ip, [sp, itmp3]
164         add   itmp3, itmp3, #4
165 asm_calljava_copydone:
166 #endif
167         add   a2, a2, #sizevmarg          /* next argument block              */
168         subs  a1, a1, #1
169         bgt   asm_calljava_copyloop
170
171         /* REMEMBER: first four args are passed in regs, take them out again  */
172         ldmfd sp, {a0, a1, a2, a3}        /* load first four args to register */
173         cmp   v1, #16                     /* do we have four arguments?       */
174         addlt sp, sp, v1
175         movlt v1, #0
176         addge sp, sp, #16
177         subge v1, v1, #16
178
179 asm_calljava_copyfinish:
180         /* REMEMBER: do the method call just like in java! */
181         ldr   ip, [mptr]                  /* fake virtual function call       */
182         mov   lr, pc 
183         mov   pc, ip 
184 fake2:
185         sub   ip, pc, #(fake2 - asm_vm_call_method)+8
186
187         add   sp, sp, v1                  /* free stack arguments!            */
188         add   sp, sp, #4                  /* free fake address                */
189         ldmfd sp!, {v1}
190         RESTORE_SCRATCH_REGS_AND_RETURN   /* return to caller, restore regs   */
191
192 asm_vm_call_method_exception_handler:
193         mov   a0, xptr                    /* exception pointer is arg1        */
194         bl    builtin_throw_exception     /* throw the exception              */
195         mov   res1, #0                    /* return NULL                      */
196         mov   res2, #0                    /* return NULL                      */
197         add   sp, sp, v1                  /* free stack arguments!            */
198         add   sp, sp, #4                  /* free fake address                */
199         ldmfd sp!, {v1}
200         RESTORE_SCRATCH_REGS_AND_RETURN   /* return to caller, restore regs   */
201
202 asm_vm_call_method_float:
203         mov a0,#0x51
204         b asm_debug
205 asm_vm_call_method_double:
206         mov a0,#0x52
207         b asm_debug
208
209 asm_vm_call_method_end:
210
211
212 /****************** function asm_call_jit_compiler *****************************
213 *                                                                              *
214 *   Invokes the compiler for untranslated JavaVM methods.                      *
215 *   What this method does:                                                     *
216 *    - save args and LR                                                        *
217 *    - fire up jit_compile (pass methodinfo pointer)                           *
218 *    - try to find out where to write back the new method pointer              *
219 *    - restore args and LR                                                     *
220 *    - check for exceptions                                                    *
221 *    - eventually write back new method pointer                                *
222 *    - call jit code (wich will then return to caller)                         *
223 *                                                                              *
224 *   These methods can call us: codegen_compilerstub & asm_calljavafunction     *
225 *   ATTENTION: use REG_ITMP1 to pass methodinfo pointer to me!                 *
226 *                                                                              *
227 *******************************************************************************/
228
229 #define MYSTACKSIZE (6*4)
230
231 asm_call_jit_compiler:
232         SAVE_ARGUMENT_REGISTERS             /* save our argument registers & LR   */
233         sub   sp, sp, #4                    /* keep stack 8-byte aligned          */
234
235         mov   a0, itmp1                     /* pass methodinfo pointer            */
236         mov   a1, mptr                      /* pass method pointer                */
237         add   a2, sp, #MYSTACKSIZE          /* pass Java sp                       */
238         mov   a3, lr                        /* pass Java RA (correct for leafs)   */
239         bl    jit_asm_compile
240         mov   itmp1, res1                   /* save pointer to new jit-code       */
241
242         tst   itmp1,itmp1                   /* check for exeption                 */
243         beq   L_asm_call_jit_compiler_exception
244
245         add   sp, sp, #4                    /* keep stack 8-byte aligned          */
246         RESTORE_ARGUMENT_REGISTERS          /* load our argument registers & LR   */
247
248         mov   ip, itmp1
249         mov   pc, ip                        /* call jit-code                      */
250
251 L_asm_call_jit_compiler_exception:
252         bl    exceptions_get_and_clear_exception
253         mov   xptr, res1                    /* get exception                      */
254
255         add   sp, sp, #4                    /* keep stack 8-byte aligned          */
256         RESTORE_ARGUMENT_REGISTERS          /* load LR                            */
257
258         sub   xpc, lr, #4                   /* xpc = instruction that called us   */
259         b     asm_handle_nat_exception
260
261
262 /********************* function asm_handle_exception ***************************
263 *                                                                              *
264 *   This function handles an exception. It does not use the usual calling      *
265 *   conventions. The exception pointer is passed in REG_ITMP1 and the          *
266 *   pc from the exception raising position is passed in REG_ITMP2. It searches *
267 *   the local exception table for a handler. If no one is found, it unwinds    *
268 *   stacks and continues searching the callers.                                *
269 *                                                                              *
270 *   void asm_handle_exception (exceptionptr, exceptionpc);                     *
271 *                                                                              *
272 *******************************************************************************/
273
274 asm_handle_nat_exception:
275         /*TODO:maybe make a macro out of it!!!*/
276         SAVE_ARGUMENT_REGISTERS  
277         mov   a0, lr
278         bl    md_codegen_get_pv_from_pc
279         mov   ip, res1
280         RESTORE_ARGUMENT_REGISTERS  
281         /* fall through */
282
283 asm_handle_exception:
284         stmfd sp!, {r0 - r3}               /* save possible used registers    */
285         mov   itmp3, #1                    /* set maybe-leaf flag             */
286         mov   a3, #(4*4)                   /* prepare a3 for handle_exception */
287
288 asm_handle_exception_loop:
289         stmfd sp!, {ip,lr}                 /* call exception helper here!     */
290         mov   a0, xptr                     /* pass exception pointer          */
291         mov   a1, xpc                      /* pass exception pointer          */
292         mov   a2, ip                       /* pass data segment pointer       */
293         add   a3, sp, a3                   /* calculate Java sp into a3...    */
294         add   a3, a3, #(2*4)
295         bl    exceptions_handle_exception
296         ldmfd sp!, {ip,lr}
297
298         tst   a0, a0
299         beq   asm_handle_exception_not_catched
300
301         mov   xpc, a0                      /* move handlerpc into xpc         */
302         tst   itmp3,itmp3                  /* if this is a lead method ...    */
303         ldmnefd sp!, {r0 - r3}             /* restore argument registers      */
304
305         mov   pc, xpc                      /* jump to handler                 */
306
307 asm_handle_exception_not_catched:
308         tst   itmp3,itmp3                  /* if this is a lead method ...    */
309         addne sp, sp, #(4*4)               /* remove maybe-leaf stackframe    */
310         movne itmp3, #0                    /* remove maybe-leaf flag          */
311
312         ldr   a2, [ip, #FrameSize]         /* t2 = frame size                 */
313         add   a0, sp, a2                   /* t0 = pointer to save area       */
314         ldr   a1, [ip, #IsLeaf]            /* t1 = is leaf procedure          */
315         tst   a1, a1                       /* if is leaf ...                  */
316         ldreq lr, [a0, #-4]!               /* ... restore RA                  */
317         mov   xpc, lr                      /* the new xpc is RA               */
318
319         ldr   a1, [ip, #IntSave]           /* t1 = saved int register count   */
320         rsb   a1, a1, #5                   /* t1 = count of unsaved registers */
321         sub   a1, a1, #1
322         add   pc, pc, a1, lsl #2           /* do not load unsaved registers   */
323         ldr   v1, [a0, #-20]               /* ... but restore the other ones  */
324         ldr   v2, [a0, #-16]
325         ldr   v3, [a0, #-12]
326         ldr   v4, [a0, #- 8]
327         ldr   v5, [a0, #- 4]
328
329         add   sp, sp, a2                   /* unwind stack (using t2)         */
330         mov   a3, #0                       /* prepare a3 for handle_exception */
331
332         /*TODO:maybe make a macro out of it!!!*/
333         SAVE_ARGUMENT_REGISTERS  
334         mov   a0, lr
335         bl    md_codegen_get_pv_from_pc
336         mov   ip, res1
337         RESTORE_ARGUMENT_REGISTERS  
338
339         b     asm_handle_exception_loop
340
341
342 /* asm_patcher_wrapper *********************************************************
343 *                                                                              *
344 *   TODO: document me                                                          *
345 *                                                                              *
346 *   Stack layout when calling patcher function:                                *
347 *    28   empty because stack needs to be aligned                              *
348 *    24   saved REG_ITMP3, should be restored                ( -8)             *
349 *    20   data segment displacement from load instructions   (-12)             *
350 *    16   return address into JIT code (patch position)      (-16)             *
351 *    12   pointer to virtual java_objectheader                                 *
352 *     8   machine code (which is patched back later)                           *
353 *   [ 8   result of patcher function (indicates exception)  ]                  *
354 *     4   unresolved class/method/field reference                              *
355 *   [ 0   patcher function pointer to call                  ]                  *
356 *     0   saved IP of caller (caller needs it!)                                *
357 *                                                                              *
358 *******************************************************************************/
359
360 #define PATCHSTACKSIZE 8*4
361
362 asm_patcher_wrapper:
363         mov   itmp3, sp                     /* preserve original SP in ITMP3      */
364
365         SAVE_ARGUMENT_REGISTERS_IP          /* save our argument registers & LR   */
366         SAVE_FLOAT_REGISTERS                /* save our float registers here      */
367
368         mov   a0, itmp3                     /* pass SP of patcher stub            */
369         mov   a1, ip                        /* pass PV                            */
370         mov   a2, lr                        /* pass RA (correct for leafs)        */
371         bl    patcher_wrapper
372         mov   itmp3, res1                   /* save return value                  */
373
374         RESTORE_FLOAT_REGISTERS             /* restore our float registers here   */
375         RESTORE_ARGUMENT_REGISTERS_IP       /* load our argument registers & LR   */
376
377         add   sp, sp, #PATCHSTACKSIZE       /* remove patcher stack frame         */
378
379         tst   itmp3, itmp3                  /* check for an exception             */
380         bne   L_asm_patcher_wrapper_exception
381
382         ldr   itmp3, [sp, #-8]              /* restore ITMP3 for calling method   */
383         ldr   pc, [sp, #-16]                /* jump to new patched code           */
384
385 L_asm_patcher_wrapper_exception:
386         mov   xptr, itmp3                   /* get exception                      */
387         ldr   xpc, [sp, #-16]               /* RA is xpc                          */
388
389         /* Note: A normal branch instruction could modify the PV here,            */
390         /*       so we use this construct instead.                                */
391         ldr   pc, [pc, #-4]
392         .word asm_handle_exception
393
394
395 /* asm_abstractmethoderror *****************************************************
396
397    Creates and throws an AbstractMethodError.
398
399 *******************************************************************************/
400
401 asm_abstractmethoderror:
402         stmfd sp!, {lr}                     /* save return address                */
403         add   a0, sp, #(1*4)                /* pass java sp                       */
404         mov   a1, lr                        /* pass exception address             */
405         bl    exceptions_asm_new_abstractmethoderror
406         ldmfd sp!, {lr}                     /* restore return address             */
407
408         mov   xptr, res1                    /* get exception pointer              */
409         sub   xpc, lr, #4                   /* exception address is ra - 4        */
410         b     asm_handle_nat_exception
411
412                 
413 /********************* function asm_cacheflush *********************************
414 *                                                                              *
415 *   TODO: document me                                                          *
416 *                                                                              *
417 *   void asm_cacheflush(void *p, s4 size);                                     *
418 *                                                                              *
419 *******************************************************************************/
420
421 #if 1
422 .equ sys_cacheflush, 0x9f0002
423 asm_cacheflush:
424         add   a1, a0, a1
425         mov   a2, #0
426
427 #if 0
428         /* TWISTI: required on iyonix, maybe a linux-2.4 bug */
429         /* TODO: repeair this! */
430         /* cacheflush is messed up beyond all repair! */
431         mov a0, #0x0
432         mov a1, #0xff000000
433 #endif
434
435         swi   #sys_cacheflush
436         mov   pc, lr
437 #endif
438
439
440 /********************* function asm_getclassvalues_atomic *********************/
441
442 asm_getclassvalues_atomic:
443         stmfd sp!, {r4, r5, r6}
444 _crit_restart:
445 _crit_begin:
446         ldr   r4,[a0,#offbaseval]
447         ldr   r5,[a0,#offdiffval]
448         ldr   r6,[a1,#offbaseval]
449 _crit_end:
450         str   r4,[a2,#offcast_super_baseval]
451         str   r5,[a2,#offcast_super_diffval]
452         str   r6,[a2,#offcast_sub_baseval]
453         ldmfd sp!, {r4, r5, r6}
454         mov   pc, lr
455
456
457 /* disable exec-stacks ********************************************************/
458
459 #if defined(__linux__) && defined(__ELF__)
460         .section .note.GNU-stack,"",%progbits
461 #endif
462
463
464 /*
465  * These are local overrides for various environment variables in Emacs.
466  * Please do not remove this and leave it at the end of the file, where
467  * Emacs will automagically detect them.
468  * ---------------------------------------------------------------------
469  * Local variables:
470  * mode: asm
471  * indent-tabs-mode: t
472  * c-basic-offset: 4
473  * tab-width: 4
474  * End:
475  * vim:noexpandtab:sw=4:ts=4:
476  */