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