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