* Removed all Id tags.
[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 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_call_jit_compiler
53
54         .globl asm_handle_exception
55         .globl asm_handle_nat_exception
56
57         .globl asm_abstractmethoderror
58
59         .globl asm_compare_and_swap
60         .globl asm_memory_barrier
61
62         .globl asm_md_init
63         .globl asm_cacheflush
64
65
66 /* asm_vm_call_method **********************************************************
67 *                                                                              *
68 *   This function calls a Java-method (which possibly needs compilation)       *
69 *   with up to 4 address parameters.                                           *
70 *                                                                              *
71 *   This functions calls the JIT-compiler which eventually translates the      *
72 *   method into machine code.                                                  *
73 *                                                                              *
74 *   C-prototype:                                                               *
75 *    javaobject_header *asm_calljavafunction (methodinfo *m,                   *
76 *         void *arg1, void *arg2, void *arg3, void *arg4);                     *
77 *                                                                              *
78 *******************************************************************************/
79
80         .ent    asm_vm_call_method
81
82         .align  3
83
84         .quad   0                           /* catch type all                     */
85         .quad   0                           /* handler pc                         */
86         .quad   0                           /* end pc                             */
87         .quad   0                           /* start pc                           */
88         .long   1                           /* extable size                       */
89         .long   0                           /* ALIGNMENT PADDING                  */
90         .quad   0                           /* line number table start            */
91         .quad   0                           /* line number table size             */
92         .long   0                           /* ALIGNMENT PADDING                  */
93         .long   0                           /* fltsave                            */
94         .long   1                           /* intsave                            */
95         .long   0                           /* isleaf                             */
96         .long   0                           /* IsSync                             */
97         .long   0                           /* frame size                         */
98         .quad   0                           /* codeinfo pointer                   */
99
100 asm_vm_call_method:
101 asm_vm_call_method_int:
102 asm_vm_call_method_long:
103 asm_vm_call_method_float:
104 asm_vm_call_method_double:
105         ldgp    gp,0(pv)
106         lda     sp,-5*8(sp)                 /* allocate stack space               */
107         stq     ra,0*8(sp)                  /* save return address                */
108         stq     gp,1*8(sp)                  /* save global pointer                */
109
110         stq     s0,3*8(sp)
111         stq     a0,4*8(sp)                  /* save method PV                     */
112
113         mov     a1,t0                       /* address of argument array          */
114         mov     a2,t1                       /* stack argument count               */
115         mov     sp,s0                       /* save stack pointer                 */
116
117         ldq     a0,0*8(t0)
118         ldq     a1,1*8(t0)
119         ldq     a2,2*8(t0)
120         ldq     a3,3*8(t0)
121         ldq     a4,4*8(t0)
122         ldq     a5,5*8(t0)
123
124         ldt     fa0,6*8(t0)
125         ldt     fa1,7*8(t0)
126         ldt     fa2,8*8(t0)
127         ldt     fa3,9*8(t0)
128         ldt     fa4,10*8(t0)
129         ldt     fa5,11*8(t0)
130
131         beq     t1,L_asm_vm_call_method_stack_copy_done
132
133         negq    t1,t2                       /* calculate stackframe size (* 8)    */
134         s8addq  t2,sp,sp                    /* create stackframe                  */
135         mov     sp,t2                       /* temporary stack pointer            */
136
137 L_asm_vm_call_method_stack_copy_loop:
138         ldq     t3,12*8(t0)                 /* load argument                      */
139         stq     t3,0(t2)                    /* store argument on stack            */
140
141         subq    t1,1,t1                     /* subtract 1 argument                */
142         addq    t0,8,t0                     /* load address of next argument      */
143         addq    t2,8,t2                     /* increase stack pointer             */
144
145         bgt     t1,L_asm_vm_call_method_stack_copy_loop
146
147 L_asm_vm_call_method_stack_copy_done:
148         lda     mptr,4*8(s0)                /* get address of PV                  */
149         ldq     pv,0*8(mptr)                /* load PV                            */
150         jmp     ra,(pv)
151 L_asm_vm_call_method_recompute_pv:
152         lda     pv,(asm_vm_call_method - L_asm_vm_call_method_recompute_pv)(ra)
153
154         mov     s0,sp                       /* restore stack pointer              */
155
156 L_asm_vm_call_method_recompute_return:
157         ldq     ra,0*8(sp)                  /* restore RA                         */
158         ldq     gp,1*8(sp)                  /* restore global pointer             */
159         ldq     s0,3*8(sp)
160
161         lda     sp,5*8(sp)                  /* free stack space                   */
162         jmp     zero,(ra)
163
164 asm_vm_call_method_exception_handler:
165         mov     s0,sp                       /* restore stack pointer              */
166         ldq     gp,1*8(sp)                  /* restore global pointer             */
167         mov     itmp1,a0
168         jsr     ra,builtin_throw_exception
169
170 asm_vm_call_method_end:                                 
171         br      L_asm_vm_call_method_recompute_return
172
173         .end    asm_vm_call_method
174
175
176 /* asm_call_jit_compiler *******************************************************
177
178    Invokes the compiler for untranslated Java methods.
179
180 *******************************************************************************/
181
182         .ent    asm_call_jit_compiler
183
184 asm_call_jit_compiler:
185         ldgp    gp,0(pv)
186         lda     sp,-(ARG_CNT+2)*8(sp) /* +2: keep stack 16-byte aligned           */
187
188         stq     ra,0*8(sp)            /* save return address                      */
189
190         SAVE_ARGUMENT_REGISTERS(1)    /* save 6 int/6 float argument registers    */
191
192         mov     itmp1,a0              /* pass methodinfo pointer                  */
193         mov     mptr,a1               /* pass method pointer                      */
194         lda     a2,(ARG_CNT+2)*8(sp)  /* pass java sp                             */
195         mov     ra,a3
196         jsr     ra,jit_asm_compile    /* call jit compiler                        */
197         mov     v0,pv
198
199         ldq     ra,0*8(sp)            /* load return address                      */
200
201         RESTORE_ARGUMENT_REGISTERS(1) /* restore 6 int/6 float argument registers */
202
203         lda     sp,(ARG_CNT+2)*8(sp)  /* remove stack frame                       */
204
205         beq     pv,L_asm_call_jit_compiler_exception
206
207         jmp     zero,(pv)             /* and call method, the method returns      */
208                                       /* directly to the caller (ra).             */
209
210 L_asm_call_jit_compiler_exception:
211         subq    sp,2*8,sp
212         stq     ra,0*8(sp)            /* save return address (xpc)                */
213         jsr     ra,exceptions_get_and_clear_exception
214         ldq     ra,0*8(sp)            /* restore return address (xpc)             */
215         addq    sp,2*8,sp
216
217         mov     v0,xptr               /* get exception                            */
218         subq    ra,4,xpc              /* exception address is ra - 4              */
219         br      L_asm_handle_nat_exception
220
221         .end    asm_call_jit_compiler
222
223
224 /* asm_handle_exception ********************************************************
225
226    This function handles an exception. It does not use the usual calling
227    conventions. The exception pointer is passed in REG_ITMP1 and the
228    pc from the exception raising position is passed in REG_ITMP2. It searches
229    the local exception table for a handler. If no one is found, it unwinds
230    stacks and continues searching the callers.
231
232    ATTENTION: itmp3 == gp!
233
234 *******************************************************************************/
235
236         .ent    asm_handle_nat_exception
237
238 asm_handle_nat_exception:
239 L_asm_handle_nat_exception:       /* required for PIC code                    */
240 L_asm_handle_exception_stack_loop:
241         lda     sp,-6*8(sp)                 /* keep stack 16-byte aligned         */
242         stq     xptr,0*8(sp)                /* save xptr                          */
243         stq     xpc,1*8(sp)                 /* save xpc                           */
244         stq     ra,3*8(sp)                  /* save RA                            */
245         stq     zero,4*8(sp)                /* save maybe-leaf flag (cleared)     */
246
247         mov     ra,a0                       /* pass RA                            */
248
249         br      ra,L_asm_handle_exception_load_gp
250 L_asm_handle_exception_load_gp:
251         ldgp    gp,0(ra)                    /* load gp                            */
252
253         jsr     ra,md_codegen_get_pv_from_pc/* get PV from RA                     */
254         stq     v0,2*8(sp)                  /* save PV                            */
255
256         ldq     a0,0*8(sp)                  /* pass xptr                          */
257         ldq     a1,1*8(sp)                  /* pass xpc                           */
258         mov     v0,a2                       /* pass PV                            */
259         addq    sp,6*8,a3                   /* pass Java SP                       */
260
261         br      L_asm_handle_exception_continue
262
263         .aent    asm_handle_exception
264
265 asm_handle_exception:
266 L_asm_handle_exception:                 /* required for PIC code              */
267         lda     sp,-(ARG_CNT+TMP_CNT)*8(sp) /* create maybe-leaf stackframe       */
268
269         SAVE_ARGUMENT_REGISTERS(0)          /* we save arg and temp registers in  */
270         SAVE_TEMPORARY_REGISTERS(ARG_CNT)   /* case this is a leaf method         */
271
272         lda     sp,-6*8(sp)                 /* keep stack 16-byte aligned         */
273         stq     xptr,0*8(sp)                /* save xptr                          */
274         stq     pv,2*8(sp)                  /* save PV                            */
275         stq     ra,3*8(sp)                  /* save RA                            */
276         lda     t0,1(zero)                  /* set maybe-leaf flag                */
277         stq     t0,4*8(sp)                  /* save maybe-leaf flag               */
278
279         br      ra,L_asm_handle_exception_load_gp_2
280 L_asm_handle_exception_load_gp_2:
281         ldgp    gp,0(ra)                    /* load gp                            */
282
283         mov     xptr,a0                     /* pass xptr                          */
284         mov     xpc,a1                      /* pass xpc                           */
285         mov     pv,a2                       /* pass PV                            */
286         lda     a3,(ARG_CNT+TMP_CNT+6)*8(sp)/* pass Java SP                       */
287
288 L_asm_handle_exception_continue:
289         jsr     ra,exceptions_handle_exception
290
291         beq     v0,L_asm_handle_exception_not_catched
292
293         mov     v0,xpc                      /* move handlerpc into xpc            */
294         ldq     xptr,0*8(sp)                /* restore xptr                       */
295         ldq     pv,2*8(sp)                  /* restore PV                         */
296         ldq     ra,3*8(sp)                  /* restore RA                         */
297         ldq     t0,4*8(sp)                  /* get maybe-leaf flag                */
298         lda     sp,6*8(sp)                  /* free stack frame                   */
299
300         beq     t0,L_asm_handle_exception_no_leaf
301
302         RESTORE_ARGUMENT_REGISTERS(0)       /* if this is a leaf method, we have  */
303         RESTORE_TEMPORARY_REGISTERS(ARG_CNT)/* to restore arg and temp registers  */
304         
305         lda     sp,(ARG_CNT+TMP_CNT)*8(sp)  /* remove maybe-leaf stackframe       */
306
307 L_asm_handle_exception_no_leaf:
308         jmp     zero,(xpc)                  /* jump to the handler                */
309
310 L_asm_handle_exception_not_catched:
311         ldq     xptr,0*8(sp)                /* restore xptr                       */
312         ldq     pv,2*8(sp)                  /* restore PV                         */
313         ldq     ra,3*8(sp)                  /* restore RA                         */
314         ldq     t0,4*8(sp)                  /* get maybe-leaf flag                */
315         lda     sp,6*8(sp)
316
317         beq     t0,L_asm_handle_exception_no_leaf_stack
318
319         lda     sp,(ARG_CNT+TMP_CNT)*8(sp)  /* remove maybe-leaf stackframe       */
320         mov     zero,t0                     /* clear the maybe-leaf flag          */
321
322 L_asm_handle_exception_no_leaf_stack:
323         ldl     t1,FrameSize(pv)            /* get frame size                     */
324         addq    t1,sp,t1                    /* pointer to save area               */
325
326         ldl     t2,IsLeaf(pv)               /* is leaf procedure                  */
327         bne     t2,L_asm_handle_exception_no_ra_restore
328
329         ldq     ra,-1*8(t1)                 /* restore ra                         */
330         subq    t1,8,t1                     /* t1--                               */
331
332 L_asm_handle_exception_no_ra_restore:
333         mov     ra,xpc                      /* the new xpc is ra                  */
334         ldl     t2,IntSave(pv)              /* t2 = saved int register count      */
335         br      t3,ex_int1                  /* t3 = current pc                    */
336 ex_int1:
337         lda     t3,(ex_int2 - ex_int1)(t3)
338         negl    t2,t2                       /* negate register count              */
339         s4addq  t2,t3,t3                    /* t2 = IntSave - register count * 4  */
340         jmp     zero,(t3)                   /* jump to save position              */
341
342         ldq     s0,-7*8(t1)
343         ldq     s1,-6*8(t1)
344         ldq     s2,-5*8(t1)
345         ldq     s3,-4*8(t1)
346         ldq     s4,-3*8(t1)
347         ldq     s5,-2*8(t1)
348         ldq     s6,-1*8(t1)
349
350 ex_int2:
351         s8addq  t2,t1,t1                    /* t1 = t1 - 8 * register count       */
352
353         ldl     t2,FltSave(pv)              /* t2 = saved flt register count      */
354         br      t3,ex_flt1                  /* t3 = current pc                    */
355 ex_flt1:
356         lda     t3,(ex_flt2 - ex_flt1)(t3)
357         negl    t2,t2                       /* negate register count              */
358         s4addq  t2,t3,t3                    /* t2 = FltSave - 4 * register count  */
359         jmp     zero,(t3)                   /* jump to save position              */
360
361         ldt     fs0,-8*8(t1)
362         ldt     fs1,-7*8(t1)
363         ldt     fs2,-6*8(t1)
364         ldt     fs3,-5*8(t1)
365         ldt     fs4,-4*8(t1)
366         ldt     fs5,-3*8(t1)
367         ldt     fs6,-2*8(t1)
368         ldt     fs7,-1*8(t1)
369
370 ex_flt2:
371         ldl     t1,FrameSize(pv)            /* get frame size                     */
372         addq    sp,t1,sp                    /* unwind stack                       */
373         br      L_asm_handle_exception_stack_loop
374
375         .end    asm_handle_nat_exception
376
377
378 /* asm_abstractmethoderror *****************************************************
379
380    Creates and throws an AbstractMethodError.
381
382 *******************************************************************************/
383
384         .ent    asm_abstractmethoderror
385
386 asm_abstractmethoderror:
387         subq    sp,2*8,sp                   /* create stackframe                  */
388         stq     ra,0*8(sp)                  /* save return address                */
389         addq    sp,2*8,a0                   /* pass java sp                       */
390         mov     ra,a1                       /* pass exception address             */
391         jsr     ra,exceptions_asm_new_abstractmethoderror
392         ldq     ra,0*8(sp)                  /* restore return address             */
393         addq    sp,2*8,sp                   /* remove stackframe                  */
394
395         mov     v0,xptr                     /* get exception pointer              */
396         subq    ra,4,xpc                    /* exception address is ra - 4        */
397         br      L_asm_handle_nat_exception
398
399         .end    asm_abstractmethoderror
400
401
402 /* asm_compare_and_swap ********************************************************
403
404    Does an atomic compare and swap.  Required for the lock
405    implementation.
406
407    Atomically do the following: Check if the location still contains
408    `oldval`. If so, replace it by `newval` and return `oldval`.
409
410    RETURN VALUE:
411        the old value at *p
412
413    long compare_and_swap(volatile long *p, long oldval, long newval);
414
415 *******************************************************************************/
416
417         .ent    asm_compare_and_swap
418
419 asm_compare_and_swap:
420 1:
421         ldq_l   v0,0(a0)
422         cmpeq   v0,a1,t0
423         beq     t0,2f
424         mov     a2,t0
425         stq_c   t0,0(a0)
426         beq     t0,1b
427 2:
428         jmp     zero,(ra)
429
430         .end    asm_compare_and_swap
431
432
433 /* asm_memory_barrier **********************************************************
434
435    A memory barrier for the Java Memory Model.
436
437 *******************************************************************************/
438
439         .ent    asm_memory_barrier
440
441 asm_memory_barrier:
442         mb
443         jmp     zero,(ra)
444
445         .end    asm_memory_barrier
446
447
448 /* asm_md_init *****************************************************************
449
450    Initialize machine dependent stuff.
451
452    Determines if the byte support instruction set (21164a and higher)
453    is available.
454
455 *******************************************************************************/
456
457         .ent    asm_md_init
458
459 asm_md_init:
460         .long   0x47e03c20                  /* amask   1,v0                       */
461         jmp     zero,(ra)                   /* return                             */
462
463         .end    asm_md_init
464
465
466 /* asm_cacheflush **************************************************************
467
468    XXX
469
470 *******************************************************************************/
471
472         .ent    asm_cacheflush
473
474 asm_cacheflush:
475         call_pal PAL_imb              /* synchronize instruction cache            */
476         jmp     zero,(ra)
477
478         .end    asm_cacheflush
479
480
481 /* disable exec-stacks ********************************************************/
482
483 #if defined(__linux__) && defined(__ELF__)
484         .section .note.GNU-stack,"",%progbits
485 #endif
486
487
488 /*
489  * These are local overrides for various environment variables in Emacs.
490  * Please do not remove this and leave it at the end of the file, where
491  * Emacs will automagically detect them.
492  * ---------------------------------------------------------------------
493  * Local variables:
494  * mode: asm
495  * indent-tabs-mode: t
496  * c-basic-offset: 4
497  * tab-width: 4
498  * End:
499  * vim:noexpandtab:sw=4:ts=4:
500  */