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