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