Merged with tip.
[cacao.git] / src / vm / jit / alpha / asmpart.S
1 /* src/vm/jit/alpha/asmpart.S - Java-C interface functions for alpha
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008 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/alpha/md-abi.h"
31 #include "vm/jit/alpha/md-asm.h"
32
33 #include "vm/jit/abi-asm.h"
34 #include "vm/jit/methodheader.h"
35
36
37         .text
38         .set    noat
39         .set    noreorder
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_handle_exception
53         .globl asm_handle_nat_exception
54
55         .globl asm_abstractmethoderror
56
57         .globl asm_md_init
58         .globl asm_cacheflush
59
60
61 /* asm_vm_call_method **********************************************************
62 *                                                                              *
63 *   This function calls a Java-method (which possibly needs compilation)       *
64 *   with up to 4 address parameters.                                           *
65 *                                                                              *
66 *   This functions calls the JIT-compiler which eventually translates the      *
67 *   method into machine code.                                                  *
68 *                                                                              *
69 *   C-prototype:                                                               *
70 *    javaobject_header *asm_calljavafunction (methodinfo *m,                   *
71 *         void *arg1, void *arg2, void *arg3, void *arg4);                     *
72 *                                                                              *
73 *******************************************************************************/
74
75         .ent    asm_vm_call_method
76
77         .align  3
78
79         .long   0                           /* fltsave                            */
80         .long   1                           /* intsave                            */
81         .long   0                           /* isleaf                             */
82         .long   0                           /* frame size                         */
83         .quad   0                           /* codeinfo pointer                   */
84
85 asm_vm_call_method:
86 asm_vm_call_method_int:
87 asm_vm_call_method_long:
88 asm_vm_call_method_float:
89 asm_vm_call_method_double:
90         ldgp    gp,0(pv)
91         lda     sp,-5*8(sp)                 /* allocate stack space               */
92         stq     ra,0*8(sp)                  /* save return address                */
93         stq     gp,1*8(sp)                  /* save global pointer                */
94
95         stq     s0,3*8(sp)
96         stq     a0,4*8(sp)                  /* save method PV                     */
97
98         mov     a1,t0                       /* address of argument array          */
99         mov     a2,t1                       /* stack argument count               */
100         mov     sp,s0                       /* save stack pointer                 */
101
102         ldq     a0,0*8(t0)
103         ldq     a1,1*8(t0)
104         ldq     a2,2*8(t0)
105         ldq     a3,3*8(t0)
106         ldq     a4,4*8(t0)
107         ldq     a5,5*8(t0)
108
109         ldt     fa0,6*8(t0)
110         ldt     fa1,7*8(t0)
111         ldt     fa2,8*8(t0)
112         ldt     fa3,9*8(t0)
113         ldt     fa4,10*8(t0)
114         ldt     fa5,11*8(t0)
115
116         beq     t1,L_asm_vm_call_method_stack_copy_done
117
118         negq    t1,t2                       /* calculate stackframe size (* 8)    */
119         s8addq  t2,sp,sp                    /* create stackframe                  */
120         mov     sp,t2                       /* temporary stack pointer            */
121
122 L_asm_vm_call_method_stack_copy_loop:
123         ldq     t3,12*8(t0)                 /* load argument                      */
124         stq     t3,0(t2)                    /* store argument on stack            */
125
126         subq    t1,1,t1                     /* subtract 1 argument                */
127         addq    t0,8,t0                     /* load address of next argument      */
128         addq    t2,8,t2                     /* increase stack pointer             */
129
130         bgt     t1,L_asm_vm_call_method_stack_copy_loop
131
132 L_asm_vm_call_method_stack_copy_done:
133         lda     mptr,4*8(s0)                /* get address of PV                  */
134         ldq     pv,0*8(mptr)                /* load PV                            */
135         jmp     ra,(pv)
136 L_asm_vm_call_method_recompute_pv:
137         lda     pv,(asm_vm_call_method - L_asm_vm_call_method_recompute_pv)(ra)
138
139 L_asm_vm_call_method_recompute_return:
140         mov     s0,sp                       /* restore stack pointer              */
141
142         ldq     ra,0*8(sp)                  /* restore RA                         */
143         ldq     gp,1*8(sp)                  /* restore global pointer             */
144         ldq     s0,3*8(sp)
145
146         lda     sp,5*8(sp)                  /* free stack space                   */
147         jmp     zero,(ra)
148
149 asm_vm_call_method_exception_handler:
150         mov     s0,sp                       /* restore stack pointer              */
151         ldq     gp,1*8(sp)                  /* restore global pointer             */
152         mov     itmp1,a0
153         jsr     ra,builtin_throw_exception
154
155 asm_vm_call_method_end:                                 
156         br      L_asm_vm_call_method_recompute_return
157
158         .end    asm_vm_call_method
159
160
161 /* asm_handle_exception ********************************************************
162
163    This function handles an exception. It does not use the usual calling
164    conventions. The exception pointer is passed in REG_ITMP1 and the
165    pc from the exception raising position is passed in REG_ITMP2. It searches
166    the local exception table for a handler. If no one is found, it unwinds
167    stacks and continues searching the callers.
168
169    ATTENTION: itmp3 == gp!
170
171 *******************************************************************************/
172
173         .ent    asm_handle_nat_exception
174
175 asm_handle_nat_exception:
176 L_asm_handle_nat_exception:       /* required for PIC code                    */
177 L_asm_handle_exception_stack_loop:
178         lda     sp,-6*8(sp)                 /* keep stack 16-byte aligned         */
179         stq     xptr,0*8(sp)                /* save xptr                          */
180         stq     xpc,1*8(sp)                 /* save xpc                           */
181         stq     ra,3*8(sp)                  /* save RA                            */
182         stq     zero,4*8(sp)                /* save maybe-leaf flag (cleared)     */
183
184         mov     ra,a0                       /* pass RA                            */
185
186         br      ra,L_asm_handle_exception_load_gp
187 L_asm_handle_exception_load_gp:
188         ldgp    gp,0(ra)                    /* load gp                            */
189
190         jsr     ra,md_asm_codegen_get_pv_from_pc /* get PV from RA                */
191         stq     v0,2*8(sp)                  /* save PV                            */
192
193         ldq     a0,0*8(sp)                  /* pass xptr                          */
194         ldq     a1,1*8(sp)                  /* pass xpc                           */
195         mov     v0,a2                       /* pass PV                            */
196         addq    sp,6*8,a3                   /* pass Java SP                       */
197
198         br      L_asm_handle_exception_continue
199
200         .aent    asm_handle_exception
201
202 asm_handle_exception:
203 L_asm_handle_exception:                 /* required for PIC code              */
204         lda     sp,-(ARG_CNT+TMP_CNT)*8(sp) /* create maybe-leaf stackframe       */
205
206         SAVE_ARGUMENT_REGISTERS(0)          /* we save arg and temp registers in  */
207         SAVE_TEMPORARY_REGISTERS(ARG_CNT)   /* case this is a leaf method         */
208
209         lda     sp,-6*8(sp)                 /* keep stack 16-byte aligned         */
210         stq     xptr,0*8(sp)                /* save xptr                          */
211         stq     pv,2*8(sp)                  /* save PV                            */
212         stq     ra,3*8(sp)                  /* save RA                            */
213         lda     t0,1(zero)                  /* set maybe-leaf flag                */
214         stq     t0,4*8(sp)                  /* save maybe-leaf flag               */
215
216         br      ra,L_asm_handle_exception_load_gp_2
217 L_asm_handle_exception_load_gp_2:
218         ldgp    gp,0(ra)                    /* load gp                            */
219
220         mov     xptr,a0                     /* pass xptr                          */
221         mov     xpc,a1                      /* pass xpc                           */
222         mov     pv,a2                       /* pass PV                            */
223         lda     a3,(ARG_CNT+TMP_CNT+6)*8(sp)/* pass Java SP                       */
224
225 L_asm_handle_exception_continue:
226         jsr     ra,exceptions_handle_exception
227
228         beq     v0,L_asm_handle_exception_not_catched
229
230         mov     v0,xpc                      /* move handlerpc into xpc            */
231         ldq     xptr,0*8(sp)                /* restore xptr                       */
232         ldq     pv,2*8(sp)                  /* restore PV                         */
233         ldq     ra,3*8(sp)                  /* restore RA                         */
234         ldq     t0,4*8(sp)                  /* get maybe-leaf flag                */
235         lda     sp,6*8(sp)                  /* free stack frame                   */
236
237         beq     t0,L_asm_handle_exception_no_leaf
238
239         RESTORE_ARGUMENT_REGISTERS(0)       /* if this is a leaf method, we have  */
240         RESTORE_TEMPORARY_REGISTERS(ARG_CNT)/* to restore arg and temp registers  */
241         
242         lda     sp,(ARG_CNT+TMP_CNT)*8(sp)  /* remove maybe-leaf stackframe       */
243
244 L_asm_handle_exception_no_leaf:
245         jmp     zero,(xpc)                  /* jump to the handler                */
246
247 L_asm_handle_exception_not_catched:
248         ldq     xptr,0*8(sp)                /* restore xptr                       */
249         ldq     pv,2*8(sp)                  /* restore PV                         */
250         ldq     ra,3*8(sp)                  /* restore RA                         */
251         ldq     t0,4*8(sp)                  /* get maybe-leaf flag                */
252         lda     sp,6*8(sp)
253
254         beq     t0,L_asm_handle_exception_no_leaf_stack
255
256         lda     sp,(ARG_CNT+TMP_CNT)*8(sp)  /* remove maybe-leaf stackframe       */
257         mov     zero,t0                     /* clear the maybe-leaf flag          */
258
259 L_asm_handle_exception_no_leaf_stack:
260         ldl     t1,FrameSize(pv)            /* get frame size                     */
261         addq    t1,sp,t1                    /* pointer to save area               */
262
263         ldl     t2,IsLeaf(pv)               /* is leaf procedure                  */
264         bne     t2,L_asm_handle_exception_no_ra_restore
265
266         ldq     ra,-1*8(t1)                 /* restore ra                         */
267         subq    t1,8,t1                     /* t1--                               */
268
269 L_asm_handle_exception_no_ra_restore:
270         mov     ra,xpc                      /* the new xpc is ra                  */
271         ldl     t2,IntSave(pv)              /* t2 = saved int register count      */
272         br      t3,ex_int1                  /* t3 = current pc                    */
273 ex_int1:
274         lda     t3,(ex_int2 - ex_int1)(t3)
275         negl    t2,t2                       /* negate register count              */
276         s4addq  t2,t3,t3                    /* t2 = IntSave - register count * 4  */
277         jmp     zero,(t3)                   /* jump to save position              */
278
279         ldq     s0,-7*8(t1)
280         ldq     s1,-6*8(t1)
281         ldq     s2,-5*8(t1)
282         ldq     s3,-4*8(t1)
283         ldq     s4,-3*8(t1)
284         ldq     s5,-2*8(t1)
285         ldq     s6,-1*8(t1)
286
287 ex_int2:
288         s8addq  t2,t1,t1                    /* t1 = t1 - 8 * register count       */
289
290         ldl     t2,FltSave(pv)              /* t2 = saved flt register count      */
291         br      t3,ex_flt1                  /* t3 = current pc                    */
292 ex_flt1:
293         lda     t3,(ex_flt2 - ex_flt1)(t3)
294         negl    t2,t2                       /* negate register count              */
295         s4addq  t2,t3,t3                    /* t2 = FltSave - 4 * register count  */
296         jmp     zero,(t3)                   /* jump to save position              */
297
298         ldt     fs0,-8*8(t1)
299         ldt     fs1,-7*8(t1)
300         ldt     fs2,-6*8(t1)
301         ldt     fs3,-5*8(t1)
302         ldt     fs4,-4*8(t1)
303         ldt     fs5,-3*8(t1)
304         ldt     fs6,-2*8(t1)
305         ldt     fs7,-1*8(t1)
306
307 ex_flt2:
308         ldl     t1,FrameSize(pv)            /* get frame size                     */
309         addq    sp,t1,sp                    /* unwind stack                       */
310         br      L_asm_handle_exception_stack_loop
311
312         .end    asm_handle_nat_exception
313
314
315 /* asm_abstractmethoderror *****************************************************
316
317    Creates and throws an AbstractMethodError.
318
319 *******************************************************************************/
320
321         .ent    asm_abstractmethoderror
322
323 asm_abstractmethoderror:
324         subq    sp,2*8,sp                   /* create stackframe                  */
325         stq     ra,0*8(sp)                  /* save return address                */
326         addq    sp,2*8,a0                   /* pass java sp                       */
327         mov     ra,a1                       /* pass exception address             */
328         jsr     ra,exceptions_asm_new_abstractmethoderror
329         ldq     ra,0*8(sp)                  /* restore return address             */
330         addq    sp,2*8,sp                   /* remove stackframe                  */
331
332         mov     v0,xptr                     /* get exception pointer              */
333         subq    ra,4,xpc                    /* exception address is ra - 4        */
334         br      L_asm_handle_nat_exception
335
336         .end    asm_abstractmethoderror
337
338
339 /* asm_md_init *****************************************************************
340
341    Initialize machine dependent stuff.
342
343    Determines if the byte support instruction set (21164a and higher)
344    is available.
345
346 *******************************************************************************/
347
348         .ent    asm_md_init
349
350 asm_md_init:
351         .long   0x47e03c20                  /* amask   1,v0                       */
352         jmp     zero,(ra)                   /* return                             */
353
354         .end    asm_md_init
355
356
357 /* asm_cacheflush **************************************************************
358
359    XXX
360
361 *******************************************************************************/
362
363         .ent    asm_cacheflush
364
365 asm_cacheflush:
366         call_pal PAL_imb              /* synchronize instruction cache            */
367         jmp     zero,(ra)
368
369         .end    asm_cacheflush
370
371
372 /* disable exec-stacks ********************************************************/
373
374 #if defined(__linux__) && defined(__ELF__)
375         .section .note.GNU-stack,"",%progbits
376 #endif
377
378
379 /*
380  * These are local overrides for various environment variables in Emacs.
381  * Please do not remove this and leave it at the end of the file, where
382  * Emacs will automagically detect them.
383  * ---------------------------------------------------------------------
384  * Local variables:
385  * mode: asm
386  * indent-tabs-mode: t
387  * c-basic-offset: 4
388  * tab-width: 4
389  * End:
390  * vim:noexpandtab:sw=4:ts=4:
391  */