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