* src/vm/jit/x86_64/asmpart.S (asm_abstractmethoderror): Keep stack aligned.
[cacao.git] / src / vm / jit / x86_64 / asmpart.S
1 /* src/vm/jit/x86_64/asmpart.S - Java-C interface functions for x86_64
2
3    Copyright (C) 1996-2012
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include "vm/jit/x86_64/arch.h"
29 #include "vm/jit/x86_64/md-abi.h"
30 #include "vm/jit/x86_64/md-asm.h"
31
32 #include "vm/jit/abi-asm.h"
33 #include "vm/jit/methodheader.h"
34
35
36         .text
37
38
39 /* export functions ***********************************************************/
40
41         .globl asm_vm_call_method
42         .globl asm_vm_call_method_int
43         .globl asm_vm_call_method_long
44         .globl asm_vm_call_method_float
45         .globl asm_vm_call_method_double
46         .globl asm_vm_call_method_exception_handler
47         .globl asm_vm_call_method_end
48
49         .globl asm_handle_exception
50         .globl asm_handle_nat_exception
51
52         .globl asm_abstractmethoderror
53
54         .globl asm_builtin_f2i
55         .globl asm_builtin_f2l
56         .globl asm_builtin_d2i
57         .globl asm_builtin_d2l
58
59
60 /********************* function asm_calljavafunction ***************************
61 *                                                                              *
62 *   This function calls a Java-method (which possibly needs compilation)       *
63 *   with up to 4 address parameters.                                           *
64 *                                                                              *
65 *   This functions calls the JIT-compiler which eventually translates the      *
66 *   method into machine code.                                                  *
67 *                                                                              *
68 *   C-prototype:                                                               *
69 *    javaobject_header *asm_calljavamethod (methodinfo *m,                     *
70 *         void *arg1, void *arg2, void *arg3, void *arg4);                     *
71 *                                                                              *
72 *******************************************************************************/
73
74         .align  8
75
76         .long   0                           /* fltsave                            */
77         .long   0                           /* intsave                            */
78         .long   0                           /* isleaf                             */
79         .long   0                           /* frame size                         */
80         .quad   0                           /* codeinfo pointer                   */
81
82 asm_vm_call_method:
83 asm_vm_call_method_int:
84 asm_vm_call_method_long:
85 asm_vm_call_method_float:
86 asm_vm_call_method_double:
87         sub     $(7*8),sp                   /* keep stack 16-byte aligned         */
88         mov     %rbx,0*8(sp)                /* %rbx is not a callee saved in cacao*/
89         mov     s0,1*8(sp)
90         mov     s1,2*8(sp)
91         mov     s2,3*8(sp)
92         mov     s3,4*8(sp)
93         mov     s4,5*8(sp)
94
95         mov     a0,6*8(sp)                  /* store method PV                    */
96
97         mov     sp,s0                       /* save stack pointer                 */
98
99         mov     a1,t0                       /* address of data structure          */
100         mov     a2,itmp1                    /* number of stack arguments          */
101
102         mov     0*8(t0),a0
103         mov     1*8(t0),a1
104         mov     2*8(t0),a2
105         mov     3*8(t0),a3
106         mov     4*8(t0),a4
107         mov     5*8(t0),a5
108
109         movq    6*8(t0),fa0
110         movq    7*8(t0),fa1
111         movq    8*8(t0),fa2
112         movq    9*8(t0),fa3
113         movq    10*8(t0),fa4
114         movq    11*8(t0),fa5
115         movq    12*8(t0),fa6
116         movq    13*8(t0),fa7
117
118         cmp     $0,itmp1l
119         je      L_asm_vm_call_method_stack_copy_done
120
121         mov     itmp1,itmp2
122         add     $1,itmp2                    /* keep stack 16-byte aligned         */
123         and     $0xfffffffffffffffe,itmp2
124         shl     $3,itmp2                    /* calculate stack size               */
125         sub     itmp2,sp                    /* create stack frame                 */
126         mov     sp,itmp2                    /* temporary stack pointer            */
127
128 L_asm_vm_call_method_stack_copy_loop:
129         mov     14*8(t0),itmp3              /* load argument                      */
130         mov     itmp3,0(itmp2)              /* store argument on stack            */
131
132         sub     $1,itmp1l                   /* subtract 1 argument                */
133         add     $8,t0                       /* set address of next argument       */
134         add     $8,itmp2                    /* increase SP                        */
135
136         cmp     $0,itmp1l
137         jg      L_asm_vm_call_method_stack_copy_loop
138
139 L_asm_vm_call_method_stack_copy_done:
140         lea     (6*8-256)(s0),mptr          /* We subtract 256 to force the next  */
141                                             /* move instruction to have a 32-bit  */
142                                             /* offset.                            */
143
144         mov     (0*8+256)(mptr),itmp3       /* load PV                            */
145         call    *itmp3
146
147 L_asm_vm_call_method_return:
148         mov     s0,sp                       /* restore SP                         */
149
150         mov     0*8(sp),%rbx                /* restore callee saved registers     */
151         mov     1*8(sp),s0
152         mov     2*8(sp),s1
153         mov     3*8(sp),s2
154         mov     4*8(sp),s3
155         mov     5*8(sp),s4
156         add     $(7*8),sp                   /* free stack space                   */
157         ret
158                 
159 asm_vm_call_method_exception_handler:
160         mov     xptr,a0                     /* pass exception pointer             */
161         call    builtin_throw_exception@PLT
162         jmp     L_asm_vm_call_method_return
163
164 asm_vm_call_method_end:
165         nop
166
167
168 /* asm_handle_exception ********************************************************
169 *                                                                              *
170 *   This function handles an exception. It does not use the usual calling      *
171 *   conventions. The exception pointer is passed in REG_ITMP1 and the          *
172 *   pc from the exception raising position is passed in REG_ITMP2. It searches *
173 *   the local exception table for a handler. If no one is found, it unwinds    *
174 *   stacks and continues searching the callers.                                *
175 *                                                                              *
176 *******************************************************************************/
177
178 asm_handle_nat_exception:
179         add     $8,sp                       /* clear return address of native stub*/
180                 
181 asm_handle_exception:
182 L_asm_handle_exception:                 /* required for PIC code              */
183         sub     $((ARG_CNT+TMP_CNT)*8),sp   /* create maybe-leaf stackframe       */
184
185         SAVE_ARGUMENT_REGISTERS(0)          /* we save arg and temp registers in  */
186         SAVE_TEMPORARY_REGISTERS(ARG_CNT)   /* case this is a leaf method         */
187
188         mov     $((ARG_CNT+TMP_CNT)*8),a3   /* prepare a3 for handle_exception    */
189         mov     $1,t0                       /* set maybe-leaf flag                */
190
191 L_asm_handle_exception_stack_loop:
192         sub     $(6*8),sp
193         mov     xptr,0*8(sp)                /* save exception pointer             */
194         mov     xpc,1*8(sp)                 /* save exception pc                  */
195         add     sp,a3                       /* calculate Java sp into a3...       */
196         add     $(6*8),a3
197         mov     a3,3*8(sp)                  /* ...and save it                     */
198         mov     t0,4*8(sp)                  /* save maybe-leaf flag               */
199
200         mov     xpc,a0                      /* exception pc                       */
201         call    methodtree_find@PLT
202         mov     v0,2*8(sp)                  /* save data segment pointer          */
203         
204         mov     0*8(sp),a0                  /* pass exception pointer             */
205         mov     1*8(sp),a1                  /* pass exception pc                  */
206         mov     v0,a2                       /* pass data segment pointer          */
207         mov     3*8(sp),a3                  /* pass Java stack pointer            */
208         call    exceptions_handle_exception@PLT
209
210         test    v0,v0
211         jz      L_asm_handle_exception_not_catched
212
213         mov     v0,xpc                      /* move handlerpc into xpc            */
214         mov     0*8(sp),xptr                /* restore exception pointer          */
215         mov     4*8(sp),t0                  /* get maybe-leaf flag                */
216         add     $(6*8),sp                   /* free stack frame                   */
217
218         test    t0,t0                       /* test for maybe-leaf flag           */
219         jz      L_asm_handle_exception_no_leaf
220
221         RESTORE_ARGUMENT_REGISTERS(0)       /* if this is a leaf method, we have  */
222         RESTORE_TEMPORARY_REGISTERS(ARG_CNT)/* to restore arg and temp registers  */
223
224         add     $((ARG_CNT+TMP_CNT)*8),sp   /* remove maybe-leaf stackframe       */
225
226 L_asm_handle_exception_no_leaf:
227         jmp     *xpc                        /* jump to the handler                */
228
229 L_asm_handle_exception_not_catched:
230         mov     0*8(sp),xptr                /* restore exception pointer          */
231         mov     2*8(sp),itmp3               /* restore data segment pointer       */
232         mov     4*8(sp),t0                  /* get maybe-leaf flag                */
233         add     $(6*8),sp
234
235         test    t0,t0
236         jz      L_asm_handle_exception_no_leaf_stack
237
238         add     $((ARG_CNT+TMP_CNT)*8),sp   /* remove maybe-leaf stackframe       */
239         xor     t0,t0                       /* clear the isleaf flags             */
240
241 L_asm_handle_exception_no_leaf_stack:
242         mov     FrameSize(itmp3),itmp2l     /* get frame size                     */
243         add     sp,itmp2                    /* pointer to save area               */
244         
245         mov     IntSave(itmp3),a0l          /* a0l = saved int register count     */
246         test    a0l,a0l
247         je      noint
248         
249         cmp     $1,a0l
250         je      int1
251         cmp     $2,a0l
252         je      int2
253         cmp     $3,a0l
254         je      int3
255         cmp     $4,a0l
256         je      int4
257         
258         mov     -5*8(itmp2),s0
259 int4:   
260         mov     -4*8(itmp2),s1
261 int3:   
262         mov     -3*8(itmp2),s2
263 int2:   
264         mov     -2*8(itmp2),s3
265 int1:   
266         mov     -1*8(itmp2),s4
267
268         shl     $3,a0l                      /* multiply by 8 bytes                */
269         sub     a0,itmp2
270                 
271 noint:
272 #if 0
273         mov     FltSave(itmp3),a0l          /* a0l = saved flt register count     */
274         test    a0l,a0l
275         je      noflt
276         
277         cmpl    $1,a0l
278         je      flt1
279         cmpl    $2,a0l
280         je      flt2
281         cmpl    $3,a0l
282         je      flt3
283         cmpl    $4,a0l
284         je      flt4
285
286         movq    -5*8(itmp2),%xmm11
287 flt4:   
288         movq    -4*8(itmp2),%xmm12
289 flt3:   
290         movq    -3*8(itmp2),%xmm13
291 flt2:   
292         movq    -2*8(itmp2),%xmm14
293 flt1:   
294         movq    -1*8(itmp2),%xmm15
295                 
296 noflt:
297 #endif
298         mov     FrameSize(itmp3),itmp2l     /* get frame size                     */
299         add     itmp2,sp                    /* unwind stack                       */
300
301                                             /* exception pointer is still set     */
302         pop     xpc                         /* the new xpc is return address      */
303         sub     $3,xpc                      /* subtract 3 bytes for call          */
304
305         xor     a3,a3                       /* prepare a3 for handle_exception    */
306         
307         jmp             L_asm_handle_exception_stack_loop
308
309
310 /* asm_abstractmethoderror *****************************************************
311
312    Creates and throws an AbstractMethodError.
313
314 *******************************************************************************/
315
316 asm_abstractmethoderror:
317         sub     $8,sp                       /* keep stack aligned                 */
318         mov     sp,a0                       /* pass java sp                       */
319         add     $2*8,a0
320         mov     1*8(sp),a1                  /* pass exception address             */
321         sub     $3,a1
322         call    exceptions_asm_new_abstractmethoderror@PLT
323                                             /* exception pointer is return value  */
324         pop     xpc                         /* dummy pop                          */
325         pop     xpc                         /* get exception address              */
326         sub     $3,xpc                      /* exception address is ra - 3        */
327         jmp     L_asm_handle_exception
328
329
330 /* asm_builtin_x2x *************************************************************
331 *                                                                              *
332 *   Wrapper functions for float to int corner cases                            *
333 *                                                                              *
334 *******************************************************************************/
335
336 asm_builtin_f2i:
337         sub     $(ARG_CNT*8),sp
338         
339         SAVE_ARGUMENT_REGISTERS(0)
340         
341         movq    ftmp1,fa0
342         call    builtin_f2i@PLT
343         
344         RESTORE_ARGUMENT_REGISTERS(0)
345         
346         add     $(ARG_CNT*8),sp
347         ret
348
349
350 asm_builtin_f2l:
351         sub     $(ARG_CNT*8),sp
352         
353         SAVE_ARGUMENT_REGISTERS(0)
354         
355         movq    ftmp1,fa0
356         call    builtin_f2l@PLT
357         
358         RESTORE_ARGUMENT_REGISTERS(0)
359         
360         add     $(ARG_CNT*8),sp
361         ret
362
363
364 asm_builtin_d2i:
365         sub     $(ARG_CNT*8),sp
366         
367         SAVE_ARGUMENT_REGISTERS(0)
368         
369         movq    ftmp1,fa0
370         call    builtin_d2i@PLT
371         
372         RESTORE_ARGUMENT_REGISTERS(0)
373         
374         add     $(ARG_CNT*8),sp
375         ret
376
377
378 asm_builtin_d2l:
379         sub     $(ARG_CNT*8),sp
380         
381         SAVE_ARGUMENT_REGISTERS(0)
382         
383         movq    ftmp1,fa0
384         call    builtin_d2l@PLT
385         
386         RESTORE_ARGUMENT_REGISTERS(0)
387         
388         add     $(ARG_CNT*8),sp
389         ret
390
391
392 /* disable exec-stacks ********************************************************/
393
394 #if defined(__linux__) && defined(__ELF__)
395         .section .note.GNU-stack,"",%progbits
396 #endif
397
398
399 /*
400  * These are local overrides for various environment variables in Emacs.
401  * Please do not remove this and leave it at the end of the file, where
402  * Emacs will automagically detect them.
403  * ---------------------------------------------------------------------
404  * Local variables:
405  * mode: asm
406  * indent-tabs-mode: t
407  * c-basic-offset: 4
408  * tab-width: 4
409  * End:
410  * vim:noexpandtab:sw=4:ts=4:
411  */