* src/vm/jit/i386/asmpart.S (asm_replacement_in, asm_replacement_out): Removed
[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                         /* catch type all                       */
107         .long   0                         /* exception handler pc                 */
108         .long   0                         /* end pc                               */
109         .long   0                         /* start pc                             */
110         .long   1                         /* extable size                         */
111         .long   0                         /* line number table start              */
112         .long   0                         /* line number table size               */
113         .long   0                         /* fltsave                              */
114         .long   0                         /* intsave                              */
115         .long   0                         /* IsLeaf                               */
116         .long   0                         /* IsSync                               */
117         .long   0                         /* frame size                           */
118         .long   0                         /* codeinfo pointer                     */
119
120 asm_vm_call_method:
121 asm_vm_call_method_int:
122 asm_vm_call_method_long:
123 asm_vm_call_method_float:
124 asm_vm_call_method_double:
125
126         ahi sp, -8*4                /* allocate stack frame */
127
128         /* a0: PV */
129         /* a1: data structure */
130         /* a2: number of stack arguments */
131
132         st    s0, 0*4(sp)           /* store used calle saved registers */
133         st    s1, 1*4(sp)
134         st    a0, 2*4(sp)
135         st    %r12, 3*4(sp)         /* %r12 is callee saved, we'll need it as GOT */
136         st    pv, 4*4(sp)
137         st    a4, 5*4(sp)           /* a4 is callee saved in terms of C abi */
138         std   ftmp1, 6*4(sp)        /* ftmp1 and ftmp2 are callees saved in terms of C abi */
139         std   ftmp2, 8*4(sp)
140         st    ra, 10*4(sp)
141
142         lr    s0, a1                /* data structure */
143         lr    %r0, a2               /* number of stack arguments */
144
145         l     a0, 0*8+4(s0)         /* big endian */
146         l     a1, 1*8+4(s0)
147         l     a2, 2*8+4(s0)
148         l     a3, 3*8+4(s0)
149         l     a4, 4*8+4(s0)         
150
151         ld    fa0, 5*8(s0)
152         ld    fa1, 6*8(s0)
153
154         lr    s1, sp                /* backup stack pointer */
155
156         ltr   %r0, %r0              /* are there any stack arguments ? */
157         je    L_asm_vm_call_method_stack_copy_done
158         lr    %r1, %r0              /* copy number of stack arguments */
159         sll   %r1, 3                /* calculate stackframe size */ 
160         sr    sp, %r1               /* allocate stack frame */
161         lr    %r1, sp               /* temporary stack pointer */
162
163 L_asm_vm_call_method_stack_copy_loop:
164
165         mvc   0(8, %r1), 7*8(s0)    /* copy argument */
166         ahi   %r1, 8                /* increase sp */
167         ahi   s0, 8                 /* set address of next argument */
168         ahi   %r0, -1               /* substract 1 argument */
169         jh    L_asm_vm_call_method_stack_copy_loop
170
171 L_asm_vm_call_method_stack_copy_done:
172
173         la    mptr, 2*4(s1)         /* load method pointer */
174         l     pv, 0(mptr)           /* load procedure vector from method pointer */
175         basr  ra, pv                /* call method */
176         lr    sp, s1                /* restore stack pointer */
177
178 L_asm_vm_call_method_return:
179
180         l     s0, 0*4(sp)           /* restore used callee saved registers */
181         l     s1, 1*4(sp)
182         l     %r12, 3*4(sp)
183         l     pv, 4*4(sp)
184         l     a4, 5*4(sp)
185         ld    ftmp1, 6*4(sp)
186         ld    ftmp2, 8*4(sp)
187         l     ra, 10*4(sp)
188
189         ahi   sp, 8*4               /* remove stackframe */
190         br    ra                    /* return */
191
192
193 asm_vm_call_method_exception_handler:
194         lr    a0, xptr
195
196         ahi   sp, -96
197         CALL_PIC(builtin_throw_exception, avcmeh)
198         ahi   sp, 96
199
200         j     L_asm_vm_call_method_return
201
202 asm_vm_call_method_end:
203         brc   0,0       
204
205 /****************** function asm_call_jit_compiler *****************************
206 *                                                                              *
207 *   invokes the compiler for untranslated JavaVM methods.                      *
208 *                                                                              *
209 *   itmp3: methodinfo pointer                                                  *
210 *   itmp1: method pointer                                                      *
211 *                                                                              *
212 *******************************************************************************/
213
214 /*
215
216 argument registers: arguments (like in JIT)
217
218         arguments on stack (like in JIT)
219 ------------------------------------------------------------- <- SP on entry
220
221         saved return address                                           \
222         stored volatile (in terms of C ABI) floag argument registers   | 
223 96      stored volatile (in terms of C ABI) integer argument registers | ACJC_STACKFRAME
224 0 - 96  register save area (C ABI)                                     /
225 -------------------------------------------------- <- SP for jit_asm_compile
226 */
227
228 /* This is called from a compiler stub.
229  * Arguments are already in registers and the stack is setup like in CACAO.
230  */
231
232 asm_call_jit_compiler:
233 L_asm_call_jit_compiler:
234
235 #       define ACJC_STACKFRAME ((2 * 4) + (4 * 4) + (2 * 8) + 96)
236
237         ahi     sp,-ACJC_STACKFRAME        /* allocate stack space */
238
239         stm         %r2,%r5,96(sp)             /* store volatile int arg regs */
240         std     %f0,96+16(sp)              /* store volatile float arg regs */
241         std     %f2,96+24(sp)              
242         st      %r12,96+32(sp)             /* store %r12, used as GOT pointer */
243         st      %r14,96+36(sp)             /* store return address */
244
245         /* load arguments */
246
247         lr      a0,itmp3                   /* pass methodinfo pointer            */
248         lr      a1,mptr                    /* pass method pointer                */
249         la      a2,ACJC_STACKFRAME(sp)     /* pass java sp                       */
250         la      a3,0(%r14)                 /* pass return address, make sure bit 32 is 0 */
251
252         CALL_PIC(jit_asm_compile, acjc)
253
254         lr      pv, v0                     /* save return value */
255
256         lm      %r2,%r5,96(sp)             /* restore volatile int arg regs */
257         ld      %f0,96+16(sp)              /* restore volatile float arg regs */
258         ld      %f2,96+24(sp)              /* restore volatile float arg regs */
259
260         ltr     pv,pv
261         je      L_asm_call_jit_compiler_exception
262
263         l       %r12,96+32(sp)             /* restore %r12 */
264         l       %r14,96+36(sp)             /* restore return address */
265         ahi     sp, ACJC_STACKFRAME        /* remove stack frame */
266
267 jit_code_entry:                        /* label to set breakpoint on */
268         br      pv                         /* call the method, it will return to the caller */
269
270
271 L_asm_call_jit_compiler_exception:
272
273         CALL_PIC(exceptions_get_and_clear_exception, acjce)
274
275         lr      xptr, %r2
276         l       xpc,96+36(sp)              /* restore return address */
277         l       %r12,96+32(sp)             /* restore %r12 */
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+(0*4)(sp)
310         st    xpc, REGS+(1*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         /* store %r12 used as GOT */
334         
335         st    %r12, REGS+(12*4)(sp)
336
337         /* call md_handle_exception */
338
339         la    a0, REGS(sp)
340         la    a1, FREGS(sp)
341         la    a2, OUT(sp)
342
343         CALL_PIC(md_handle_exception, ahe)
344
345         /* restore %r12 */
346
347         l     %r12, REGS+(12*4)(sp)
348
349         l     itmp3, OUT+(2*4)(sp)  /* out[2] contains maybe leaf flag */
350         ltr   itmp3, itmp3           
351         je    L_restore_saved
352
353 L_restore_temporary_and_argument:
354
355         /* if we are maybe leaf,
356          * we have to restore argument and temporary registers
357          */
358
359         lm    a0, a4, REGS+(2*4)(sp)
360         ld    %f0, FREGS+(0*8)(sp)
361         ld    %f1, FREGS+(1*8)(sp)
362         ld    %f2, FREGS+(2*8)(sp)
363         ld    %f3, FREGS+(3*8)(sp)
364         ld    %f5, FREGS+(5*8)(sp)
365         ld    %f7, FREGS+(7*8)(sp)
366         ld    %f8, FREGS+(8*8)(sp)
367         ld    %f9, FREGS+(9*8)(sp)
368         ld    %f10, FREGS+(10*8)(sp)
369         ld    %f11, FREGS+(11*8)(sp)
370         ld    %f12, FREGS+(12*8)(sp)
371         ld    %f13, FREGS+(13*8)(sp)
372         ld    %f14, FREGS+(14*8)(sp)
373         ld    %f15, FREGS+(15*8)(sp)
374
375         j     L_restore_done
376
377 L_restore_saved:
378
379         /* if we are not a maybe leaf,
380          * we have to restore callee saved registers of the callee
381          */
382
383         l     itmp3, OUT+(0*4)(sp)  /* out[0] contains IntSav */
384
385         ahi   itmp3, -1
386         jl    L_int_done
387         l     s5, REGS+(12*4)(sp)
388
389         ahi   itmp3, -1
390         jl    L_int_done
391         l     s4, REGS+(11*4)(sp)
392
393         ahi   itmp3, -1
394         jl    L_int_done
395         l     s3, REGS+(10*4)(sp)
396
397         ahi   itmp3, -1
398         jl    L_int_done
399         l     s2, REGS+(9*4)(sp)
400
401         ahi   itmp3, -1
402         jl    L_int_done
403         l     s1, REGS+(8*4)(sp)
404
405         ahi   itmp3, -1
406         jl    L_int_done
407         l     s0, REGS+(7*4)(sp)
408
409 L_int_done:
410
411         /* restore callee saved float registers */
412
413         /* there are currently none */
414
415 L_flt_done:
416
417 L_restore_done:
418
419         /* write new values for special registers */
420
421         l     xptr, REGS+(0*4)(sp)
422         l     xpc, REGS+(1*4)(sp)
423         l     pv, REGS+(13*4)(sp)  
424         l     sp, REGS+(15*4)(sp)   
425
426         br    xpc                   /* jump to handler */
427
428 #       undef STACKFRAMESIZE
429 #       undef REGS
430 #       undef FREGS
431 #       undef OUT
432
433 #if 0
434
435 /* asm_abstractmethoderror *****************************************************
436
437    Creates and throws an AbstractMethodError.
438
439 *******************************************************************************/
440
441 asm_abstractmethoderror:
442         mov     sp,a0                       /* pass java sp                       */
443         add     $1*8,a0
444         mov     0*8(sp),a1                  /* pass exception address             */
445         sub     $3,a1
446         call    exceptions_asm_new_abstractmethoderror@PLT
447                                             /* exception pointer is return value  */
448         pop     xpc                         /* get exception address              */
449         sub     $3,xpc                      /* exception address is ra - 3        */
450         jmp     L_asm_handle_exception
451
452 #endif
453
454 /* Offset table for PIC calls, see CALL_PIC */
455
456 L_offsets:
457         .long  _GLOBAL_OFFSET_TABLE_ - L_offsets
458 L_offset_builtin_throw_exception:
459         .long  builtin_throw_exception@PLTOFF
460 L_offset_jit_asm_compile:
461         .long  jit_asm_compile@PLTOFF
462 L_offset_exceptions_get_and_clear_exception:
463         .long  exceptions_get_and_clear_exception@PLTOFF
464 L_offset_md_handle_exception:
465         .long  md_handle_exception@PLTOFF
466
467
468 /*
469  * These are local overrides for various environment variables in Emacs.
470  * Please do not remove this and leave it at the end of the file, where
471  * Emacs will automagically detect them.
472  * ---------------------------------------------------------------------
473  * Local variables:
474  * mode: asm
475  * indent-tabs-mode: t
476  * c-basic-offset: 4
477  * tab-width: 4
478  * End:
479  * vim:noexpandtab:sw=4:ts=4:
480  */