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