dabb45c74148288512e152458a3c58d3b3f2b835
[cacao.git] / src / vm / jit / s390 / asmpart.S
1 /* src/vm/jit/s390/asmpart.S - Java-C interface functions for s390
2
3    Copyright (C) 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/s390/arch.h"
31 #include "vm/jit/s390/md-abi.h"
32 #include "vm/jit/s390/md-asm.h"
33
34 #include "vm/jit/abi-asm.h"
35 #include "vm/jit/methodheader.h"
36
37         .text
38
39
40 /* export functions ***********************************************************/
41
42         .globl asm_vm_call_method
43         .globl asm_vm_call_method_int
44         .globl asm_vm_call_method_long
45         .globl asm_vm_call_method_float
46         .globl asm_vm_call_method_double
47         .globl asm_vm_call_method_exception_handler
48         .globl asm_vm_call_method_end
49
50         .globl asm_call_jit_compiler
51
52         .globl asm_handle_exception
53         .globl asm_handle_nat_exception
54
55         .globl asm_abstractmethoderror
56
57         .globl asm_builtin_f2i
58         .globl asm_builtin_f2l
59         .globl asm_builtin_d2i
60         .globl asm_builtin_d2l
61
62
63 asm_abstractmethoderror:
64         .long 0
65 asm_builtin_f2i:
66         .long 0
67 asm_builtin_f2l:
68         .long 0
69 asm_builtin_d2i:
70         .long 0
71 asm_builtin_d2l:
72         .long 0
73
74 /* Generates a PIC call.
75  *
76  * func: function to call
77  * tag: tag unique for this call to generate a label
78  * tmp: one temporary register needed for calculation
79  *
80  * The offset table used is located at the bottom of this file.
81  *
82  * Note: destroys r12. r12 MUST contain GOT for PIC calls!
83  */
84 #define CALL_PIC(func, tag)                                                         \
85         bras   %r14, L_bras_##tag;                           /* get PC */               \
86 L_bras_##tag:                                                                       \
87         l      %r12, L_offsets - L_bras_##tag (%r14);        /* load offset to PLT */   \
88         la     %r12, L_offsets - L_bras_##tag (%r12, %r14);  /* load PLT address */     \
89         l      %r14, L_offset_##func - L_bras_##tag (%r14);  /* load offset to func */  \
90         bas    %r14, 0(%r14, %r12);
91
92 /********************* function asm_calljavafunction ***************************
93 *                                                                              *
94 *   This function calls a Java-method (which possibly needs compilation)       *
95 *   with up to 4 address parameters.                                           *
96 *                                                                              *
97 *   This functions calls the JIT-compiler which eventually translates the      *
98 *   method into machine code.                                                  *
99 *                                                                              *
100 *   C-prototype:                                                               *
101 *    javaobject_header *asm_calljavamethod (methodinfo *m,                     *
102 *         void *arg1, void *arg2, void *arg3, void *arg4);                     *
103 *                                                                              *
104 *******************************************************************************/
105
106         .long   0                         /* line number table start              */
107         .long   0                         /* line number table size               */
108         .long   0                         /* fltsave                              */
109         .long   0                         /* intsave                              */
110         .long   0                         /* IsLeaf                               */
111         .long   0                         /* frame size                           */
112         .long   0                         /* codeinfo pointer                     */
113
114 asm_vm_call_method:
115 asm_vm_call_method_int:
116 asm_vm_call_method_long:
117 asm_vm_call_method_float:
118 asm_vm_call_method_double:
119
120         ahi sp, -8*4                /* allocate stack frame */
121
122         /* a0: PV */
123         /* a1: data structure */
124         /* a2: number of stack arguments */
125
126         st    s0, 0*4(sp)           /* store used calle saved registers */
127         st    s1, 1*4(sp)
128         st    a0, 2*4(sp)
129         st    %r12, 3*4(sp)         /* %r12 is callee saved, we'll need it as GOT */
130         st    pv, 4*4(sp)
131         st    a4, 5*4(sp)           /* a4 is callee saved in terms of C abi */
132         std   ftmp1, 6*4(sp)        /* ftmp1 and ftmp2 are callees saved in terms of C abi */
133         std   ftmp2, 8*4(sp)
134         st    ra, 10*4(sp)
135
136         lr    s0, a1                /* data structure */
137         lr    %r0, a2               /* number of stack arguments */
138
139         l     a0, 0*8+4(s0)         /* big endian */
140         l     a1, 1*8+4(s0)
141         l     a2, 2*8+4(s0)
142         l     a3, 3*8+4(s0)
143         l     a4, 4*8+4(s0)         
144
145         ld    fa0, 5*8(s0)
146         ld    fa1, 6*8(s0)
147
148         lr    s1, sp                /* backup stack pointer */
149
150         ltr   %r0, %r0              /* are there any stack arguments ? */
151         je    L_asm_vm_call_method_stack_copy_done
152         lr    %r1, %r0              /* copy number of stack arguments */
153         sll   %r1, 3                /* calculate stackframe size */ 
154         sr    sp, %r1               /* allocate stack frame */
155         lr    %r1, sp               /* temporary stack pointer */
156
157 L_asm_vm_call_method_stack_copy_loop:
158
159         mvc   0(8, %r1), 7*8(s0)    /* copy argument */
160         ahi   %r1, 8                /* increase sp */
161         ahi   s0, 8                 /* set address of next argument */
162         ahi   %r0, -1               /* substract 1 argument */
163         jh    L_asm_vm_call_method_stack_copy_loop
164
165 L_asm_vm_call_method_stack_copy_done:
166
167         la    mptr, 2*4(s1)         /* load method pointer */
168         l     pv, 0(mptr)           /* load procedure vector from method pointer */
169         basr  ra, pv                /* call method */
170         lr    sp, s1                /* restore stack pointer */
171
172 L_asm_vm_call_method_return:
173
174         l     s0, 0*4(sp)           /* restore used callee saved registers */
175         l     s1, 1*4(sp)
176         l     %r12, 3*4(sp)
177         l     pv, 4*4(sp)
178         l     a4, 5*4(sp)
179         ld    ftmp1, 6*4(sp)
180         ld    ftmp2, 8*4(sp)
181         l     ra, 10*4(sp)
182
183         ahi   sp, 8*4               /* remove stackframe */
184         br    ra                    /* return */
185
186
187 asm_vm_call_method_exception_handler:
188         lr    a0, xptr
189
190         ahi   sp, -96
191         CALL_PIC(builtin_throw_exception, avcmeh)
192         ahi   sp, 96
193
194         j     L_asm_vm_call_method_return
195
196 asm_vm_call_method_end:
197         brc   0,0       
198
199 /****************** function asm_call_jit_compiler *****************************
200 *                                                                              *
201 *   invokes the compiler for untranslated JavaVM methods.                      *
202 *                                                                              *
203 *   itmp3: methodinfo pointer                                                  *
204 *   itmp1: method pointer                                                      *
205 *                                                                              *
206 *******************************************************************************/
207
208 /*
209
210 argument registers: arguments (like in JIT)
211
212         arguments on stack (like in JIT)
213 ------------------------------------------------------------- <- SP on entry
214
215         saved return address                                           \
216         stored volatile (in terms of C ABI) floag argument registers   | 
217 96      stored volatile (in terms of C ABI) integer argument registers | ACJC_STACKFRAME
218 0 - 96  register save area (C ABI)                                     /
219 -------------------------------------------------- <- SP for jit_asm_compile
220 */
221
222 /* This is called from a compiler stub.
223  * Arguments are already in registers and the stack is setup like in CACAO.
224  */
225
226 asm_call_jit_compiler:
227 L_asm_call_jit_compiler:
228
229 #       define ACJC_STACKFRAME ((2 * 4) + (4 * 4) + (2 * 8) + 96)
230
231         ahi     sp,-ACJC_STACKFRAME        /* allocate stack space */
232
233         stm         %r2,%r5,96(sp)             /* store volatile int arg regs */
234         std     %f0,96+16(sp)              /* store volatile float arg regs */
235         std     %f2,96+24(sp)              
236         st      %r12,96+32(sp)             /* store %r12, used as GOT pointer */
237         st      %r14,96+36(sp)             /* store return address */
238
239         /* load arguments */
240
241         lr      a0,itmp3                   /* pass methodinfo pointer            */
242         lr      a1,mptr                    /* pass method pointer                */
243         la      a2,ACJC_STACKFRAME(sp)     /* pass java sp                       */
244         la      a3,0(%r14)                 /* pass return address, make sure bit 32 is 0 */
245
246         CALL_PIC(jit_asm_compile, acjc)
247
248         lr      pv, v0                     /* save return value */
249
250         lm      %r2,%r5,96(sp)             /* restore volatile int arg regs */
251         ld      %f0,96+16(sp)              /* restore volatile float arg regs */
252         ld      %f2,96+24(sp)              /* restore volatile float arg regs */
253
254         ltr     pv,pv
255         je      L_asm_call_jit_compiler_exception
256
257         l       %r12,96+32(sp)             /* restore %r12 */
258         l       %r14,96+36(sp)             /* restore return address */
259         ahi     sp, ACJC_STACKFRAME        /* remove stack frame */
260
261 jit_code_entry:                        /* label to set breakpoint on */
262         br      pv                         /* call the method, it will return to the caller */
263
264
265 L_asm_call_jit_compiler_exception:
266
267         CALL_PIC(exceptions_get_and_clear_exception, acjce)
268
269         lr      xptr, %r2
270         l       xpc,96+36(sp)              /* restore return address */
271         l       %r12,96+32(sp)             /* restore %r12 */
272         ahi     sp, ACJC_STACKFRAME        /* remove stack frame */
273         j       L_asm_handle_nat_exception
274
275
276 /* asm_handle_exception ********************************************************
277 *                                                                              *
278 *   This function handles an exception. It does not use the usual calling      *
279 *   conventions. The exception pointer is passed in REG_ITMP1 and the          *
280 *   pc from the exception raising position is passed in REG_ITMP2. It searches *
281 *   the local exception table for a handler. If no one is found, it unwinds    *
282 *   stacks and continues searching the callers.                                *
283 *                                                                              *
284 *******************************************************************************/
285
286 asm_handle_nat_exception:
287 L_asm_handle_nat_exception:
288         /* TODO really nothing here ? */
289 asm_handle_exception:
290 L_asm_handle_exception:
291
292         /* a wrapper for md_handle_exception */
293
294 #       define STACKFRAMESIZE (96 + (16 * 4) + (16 * 8) + (4 * 4))
295 #       define REGS 96
296 #       define FREGS (96 + (16 * 4))
297 #       define OUT (96 + (16 * 4) + (16 * 8))
298
299         ahi   sp, -STACKFRAMESIZE   /* allocate stack frame containing the arrays */
300
301         /* store special registers to array */
302
303         st    xptr, REGS+(0*4)(sp)
304         st    xpc, REGS+(1*4)(sp)
305         st    pv, REGS+(13*4)(sp)
306         la    itmp3, STACKFRAMESIZE(sp)
307         st    itmp3, REGS+(15*4)(sp)
308
309         /* store temporary and argument registers */
310
311         stm   a0, a4, REGS+(2*4)(sp)
312         std   %f0, FREGS+(0*8)(sp)
313         std   %f1, FREGS+(1*8)(sp)
314         std   %f2, FREGS+(2*8)(sp)
315         std   %f3, FREGS+(3*8)(sp)
316         std   %f5, FREGS+(5*8)(sp)
317         std   %f7, FREGS+(7*8)(sp)
318         std   %f8, FREGS+(8*8)(sp)
319         std   %f9, FREGS+(9*8)(sp)
320         std   %f10, FREGS+(10*8)(sp)
321         std   %f11, FREGS+(11*8)(sp)
322         std   %f12, FREGS+(12*8)(sp)
323         std   %f13, FREGS+(13*8)(sp)
324         std   %f14, FREGS+(14*8)(sp)
325         std   %f15, FREGS+(15*8)(sp)
326
327         /* store %r12 used as GOT */
328         
329         st    %r12, REGS+(12*4)(sp)
330
331         /* call md_handle_exception */
332
333         la    a0, REGS(sp)
334         la    a1, FREGS(sp)
335         la    a2, OUT(sp)
336
337         CALL_PIC(md_handle_exception, ahe)
338
339         /* restore %r12 */
340
341         l     %r12, REGS+(12*4)(sp)
342
343         l     itmp3, OUT+(2*4)(sp)  /* out[2] contains maybe leaf flag */
344         ltr   itmp3, itmp3           
345         je    L_restore_saved
346
347 L_restore_temporary_and_argument:
348
349         /* if we are maybe leaf,
350          * we have to restore argument and temporary registers
351          */
352
353         lm    a0, a4, REGS+(2*4)(sp)
354         ld    %f0, FREGS+(0*8)(sp)
355         ld    %f1, FREGS+(1*8)(sp)
356         ld    %f2, FREGS+(2*8)(sp)
357         ld    %f3, FREGS+(3*8)(sp)
358         ld    %f5, FREGS+(5*8)(sp)
359         ld    %f7, FREGS+(7*8)(sp)
360         ld    %f8, FREGS+(8*8)(sp)
361         ld    %f9, FREGS+(9*8)(sp)
362         ld    %f10, FREGS+(10*8)(sp)
363         ld    %f11, FREGS+(11*8)(sp)
364         ld    %f12, FREGS+(12*8)(sp)
365         ld    %f13, FREGS+(13*8)(sp)
366         ld    %f14, FREGS+(14*8)(sp)
367         ld    %f15, FREGS+(15*8)(sp)
368
369         j     L_restore_done
370
371 L_restore_saved:
372
373         /* if we are not a maybe leaf,
374          * we have to restore callee saved registers of the callee
375          */
376
377         l     itmp3, OUT+(0*4)(sp)  /* out[0] contains IntSav */
378
379         ahi   itmp3, -1
380         jl    L_int_done
381         l     s5, REGS+(12*4)(sp)
382
383         ahi   itmp3, -1
384         jl    L_int_done
385         l     s4, REGS+(11*4)(sp)
386
387         ahi   itmp3, -1
388         jl    L_int_done
389         l     s3, REGS+(10*4)(sp)
390
391         ahi   itmp3, -1
392         jl    L_int_done
393         l     s2, REGS+(9*4)(sp)
394
395         ahi   itmp3, -1
396         jl    L_int_done
397         l     s1, REGS+(8*4)(sp)
398
399         ahi   itmp3, -1
400         jl    L_int_done
401         l     s0, REGS+(7*4)(sp)
402
403 L_int_done:
404
405         /* restore callee saved float registers */
406
407         /* there are currently none */
408
409 L_flt_done:
410
411 L_restore_done:
412
413         /* write new values for special registers */
414
415         l     xptr, REGS+(0*4)(sp)
416         l     xpc, REGS+(1*4)(sp)
417         l     pv, REGS+(13*4)(sp)  
418         l     sp, REGS+(15*4)(sp)   
419
420         br    xpc                   /* jump to handler */
421
422 #       undef STACKFRAMESIZE
423 #       undef REGS
424 #       undef FREGS
425 #       undef OUT
426
427 #if 0
428
429 /* asm_abstractmethoderror *****************************************************
430
431    Creates and throws an AbstractMethodError.
432
433 *******************************************************************************/
434
435 asm_abstractmethoderror:
436         mov     sp,a0                       /* pass java sp                       */
437         add     $1*8,a0
438         mov     0*8(sp),a1                  /* pass exception address             */
439         sub     $3,a1
440         call    exceptions_asm_new_abstractmethoderror@PLT
441                                             /* exception pointer is return value  */
442         pop     xpc                         /* get exception address              */
443         sub     $3,xpc                      /* exception address is ra - 3        */
444         jmp     L_asm_handle_exception
445
446 #endif
447
448 /* Offset table for PIC calls, see CALL_PIC */
449
450 L_offsets:
451         .long  _GLOBAL_OFFSET_TABLE_ - L_offsets
452 L_offset_builtin_throw_exception:
453         .long  builtin_throw_exception@PLTOFF
454 L_offset_jit_asm_compile:
455         .long  jit_asm_compile@PLTOFF
456 L_offset_exceptions_get_and_clear_exception:
457         .long  exceptions_get_and_clear_exception@PLTOFF
458 L_offset_md_handle_exception:
459         .long  md_handle_exception@PLTOFF
460
461
462 /*
463  * These are local overrides for various environment variables in Emacs.
464  * Please do not remove this and leave it at the end of the file, where
465  * Emacs will automagically detect them.
466  * ---------------------------------------------------------------------
467  * Local variables:
468  * mode: asm
469  * indent-tabs-mode: t
470  * c-basic-offset: 4
471  * tab-width: 4
472  * End:
473  * vim:noexpandtab:sw=4:ts=4:
474  */