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