* src/vm/jit/i386/asmpart.S (asm_replacement_out): Added pv.
[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 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    Contact: cacao@cacaojvm.org
26
27    Authors: Andreas Krall
28             Reinhard Grafl
29             Christian Thalinger
30
31    Changes: Edwin Steiner
32
33    $Id: asmpart.S 4643 2006-03-16 18:38:42Z edwin $
34
35 */
36
37
38 #include "config.h"
39
40 #include "md-abi.h"
41 #include "md-asm.h"
42
43 #include "vm/jit/abi.h"
44 #include "vm/jit/x86_64/arch.h"
45 #include "vm/jit/x86_64/offsets.h"
46
47 #include "vm/jit/methodheader.h"
48
49
50         .text
51
52
53 /* exported functions and variables *******************************************/
54
55         .globl asm_vm_call_method
56         .globl asm_vm_call_method_int
57         .globl asm_vm_call_method_long
58         .globl asm_vm_call_method_float
59         .globl asm_vm_call_method_double
60
61         .globl asm_call_jit_compiler
62         .globl asm_handle_exception
63         .globl asm_handle_nat_exception
64
65         .globl asm_wrapper_patcher
66
67         .globl asm_replacement_out
68         .globl asm_replacement_in
69
70         .globl asm_builtin_f2i
71         .globl asm_builtin_f2l
72         .globl asm_builtin_d2i
73         .globl asm_builtin_d2l
74
75         .globl asm_perform_threadswitch
76         .globl asm_initialize_thread_stack
77         .globl asm_switchstackandcall
78         .globl asm_criticalsections
79         .globl asm_getclassvalues_atomic
80
81
82 /********************* function asm_calljavafunction ***************************
83 *                                                                              *
84 *   This function calls a Java-method (which possibly needs compilation)       *
85 *   with up to 4 address parameters.                                           *
86 *                                                                              *
87 *   This functions calls the JIT-compiler which eventually translates the      *
88 *   method into machine code.                                                  *
89 *                                                                              *
90 *   C-prototype:                                                               *
91 *    javaobject_header *asm_calljavamethod (methodinfo *m,                     *
92 *         void *arg1, void *arg2, void *arg3, void *arg4);                     *
93 *                                                                              *
94 *******************************************************************************/
95
96         .align  8
97
98         .quad   0                           /* catch type all                     */
99         .quad   calljava_xhandler2          /* handler pc                         */
100         .quad   calljava_xhandler2          /* end pc                             */
101         .quad   L_asm_vm_call_method        /* start pc                           */
102         .long   1                           /* extable size                       */
103         .long   0                                                                                                                        
104         .quad   0                           /* line number table  start           */
105         .quad   0                           /* line number table  size            */
106         .long   0                                                                                                                        
107         .long   0                           /* fltsave                            */
108         .long   0                           /* intsave                            */
109         .long   0                           /* isleaf                             */
110         .long   0                           /* IsSync                             */
111         .long   24                          /* frame size                         */
112         .quad   0                           /* method pointer (pointer to name)   */
113
114 asm_vm_call_method:
115 asm_vm_call_method_int:
116 asm_vm_call_method_long:
117 asm_vm_call_method_float:
118 asm_vm_call_method_double:
119 L_asm_vm_call_method:                   /* required for PIC code              */
120         sub     $(7*8),sp                   /* keep stack 16-byte aligned         */
121         mov     %rbx,0*8(sp)                /* %rbx is not a callee saved in cacao*/
122         mov     s0,1*8(sp)
123         mov     s1,2*8(sp)
124         mov     s2,3*8(sp)
125         mov     s3,4*8(sp)
126         mov     s4,5*8(sp)
127
128         mov     a0,itmp1                    /* move method pointer for compiler   */
129         xor     %rbp,%rbp                   /* set argument stack frame to zero   */
130
131         test    a1,a1                       /* maybe we have no args...           */
132         jle     L_copy_done
133
134         mov     a1,itmp3                    /* arg count                          */
135         mov     a2,itmp2                    /* pointer to arg block               */
136
137         mov     itmp2,%r14                  /* save argument block pointer        */
138         mov     itmp3,%r15                  /* save argument count                */
139
140         sub     $sizevmarg,itmp2            /* initialize pointer (smaller code)  */
141         add     $1,itmp3                    /* initialize argument count          */
142         xor     %r12,%r12                   /* initialize integer argument counter*/
143         xor     %r13,%r13                   /* initialize float argument counter  */
144
145 L_register_copy:
146         add     $sizevmarg,itmp2            /* goto next argument block           */
147         dec     itmp3                       /* argument count - 1                 */
148         jz      L_register_copy_done
149         andb    $0x02,offvmargtype(itmp2)   /* is this a float/double type?       */
150         jnz     L_register_handle_float     /* yes, handle it                     */
151
152         cmp     $INT_ARG_CNT,%r12           /* are we out of integer argument     */
153         je      L_register_copy             /* register? yes, next loop           */
154
155         lea     jumptable_integer(%rip),%rbp
156         mov     0(%rbp,%r12,8),%rbx
157         inc     %r12                      /* integer argument counter + 1         */
158         jmp     *%rbx
159
160 L_register_handle_float:
161         cmp     $FLT_ARG_CNT,%r13         /* are we out of float argument         */
162         je      L_register_copy           /* register? yes, next loop             */
163
164         lea     jumptable_float(%rip),%rbp
165         mov     0(%rbp,%r13,8),%rbx
166         inc     %r13                      /* float argument counter + 1           */
167         jmp     *%rbx
168         
169 L_register_copy_done:
170         mov     %r15,%rbp                 /* calculate remaining arguments        */
171         sub     %r12,%rbp                 /* - integer arguments in registers     */
172         sub     %r13,%rbp                 /* - float arguments in registers       */
173         jle     L_copy_done               /* are all assigned to registers?       */
174
175         and     $0xfffffffffffffffe,%rbp  /* keep stack 16-byte aligned           */
176         shl     $3,%rbp                   /* calculate stack size                 */
177         sub     %rbp,sp                   /* stack frame for arguments            */
178         mov     sp,%rbx                   /* use %rbx as temp sp                  */
179
180         sub     $sizevmarg,%r14           /* initialize pointer (smaller code)    */
181         add     $1,%r15                   /* initialize argument count            */
182                 
183 L_stack_copy_loop:
184         add     $sizevmarg,%r14           /* goto next argument block             */
185         dec     %r15                      /* are there any arguments left?        */
186         jz      L_copy_done               /* no test needed after dec             */
187
188         andb    $0x02,offvmargtype(%r14)    /* is this a float/double type?       */
189         jnz     L_stack_handle_float
190         dec     %r12                        /* arguments assigned to registers    */
191         jge     L_stack_copy_loop
192         jmp     L_stack_copy
193
194 L_stack_handle_float:
195         dec     %r13                        /* arguments assigned to registers    */
196         jge     L_stack_copy_loop
197
198 L_stack_copy:
199         mov     offvmargdata(%r14),itmp3    /* copy s8 argument onto stack        */
200         mov     itmp3,0(%rbx)
201         add     $8,%rbx                     /* increase sp to next argument       */
202         jmp     L_stack_copy_loop
203
204 L_copy_done:
205                                             /* itmp1 still contains method pointer*/
206         lea     L_asm_call_jit_compiler(%rip),itmp3
207         call    *itmp3                      /* call JIT compiler                  */
208
209         add     bp,sp                       /* remove argument stack frame if any */
210
211 L_asm_vm_call_method_return:
212         mov     0*8(sp),%rbx                /* restore callee saved registers     */
213         mov     1*8(sp),s0
214         mov     2*8(sp),s1
215         mov     3*8(sp),s2
216         mov     4*8(sp),s3
217         mov     5*8(sp),s4
218         add     $(7*8),sp                   /* free stack space                   */
219         ret
220                 
221 calljava_xhandler2:
222         mov     xptr,a0                     /* pass exception pointer             */
223         call    builtin_throw_exception@PLT
224         xor     v0,v0                       /* return NULL                        */
225         jmp     L_asm_vm_call_method_return
226
227
228 jumptable_integer:
229         .quad   handle_a0
230         .quad   handle_a1
231         .quad   handle_a2
232         .quad   handle_a3
233         .quad   handle_a4
234         .quad   handle_a5
235
236 handle_a0:
237         mov     offvmargdata(itmp2),a0
238         jmp     L_register_copy
239 handle_a1:
240         mov     offvmargdata(itmp2),a1
241         jmp     L_register_copy
242 handle_a2:
243         mov     offvmargdata(itmp2),a2
244         jmp     L_register_copy
245 handle_a3:
246         mov     offvmargdata(itmp2),a3
247         jmp     L_register_copy
248 handle_a4:
249         mov     offvmargdata(itmp2),a4
250         jmp     L_register_copy
251 handle_a5:
252         mov     offvmargdata(itmp2),a5
253         jmp     L_register_copy
254
255
256 jumptable_float:
257         .quad   handle_fa0
258         .quad   handle_fa1
259         .quad   handle_fa2
260         .quad   handle_fa3
261         .quad   handle_fa4
262         .quad   handle_fa5
263         .quad   handle_fa6
264         .quad   handle_fa7
265
266 handle_fa0:
267         movq    offvmargdata(itmp2),fa0
268         jmp     L_register_copy
269 handle_fa1:
270         movq    offvmargdata(itmp2),fa1
271         jmp     L_register_copy
272 handle_fa2:
273         movq    offvmargdata(itmp2),fa2
274         jmp     L_register_copy
275 handle_fa3:
276         movq    offvmargdata(itmp2),fa3
277         jmp     L_register_copy
278 handle_fa4:
279         movq    offvmargdata(itmp2),fa4
280         jmp     L_register_copy
281 handle_fa5:
282         movq    offvmargdata(itmp2),fa5
283         jmp     L_register_copy
284 handle_fa6:
285         movq    offvmargdata(itmp2),fa6
286         jmp     L_register_copy
287 handle_fa7:
288         movq    offvmargdata(itmp2),fa7
289         jmp     L_register_copy
290
291
292 /****************** function asm_call_jit_compiler *****************************
293 *                                                                              *
294 *   invokes the compiler for untranslated JavaVM methods.                      *
295 *                                                                              *
296 *   Register R0 contains a pointer to the method info structure (prepared      *
297 *   by createcompilerstub). Using the return address in R26 and the            *
298 *   offset in the LDA instruction or using the value in methodptr R28 the      *
299 *   patching address for storing the method address can be computed:           *
300 *                                                                              *
301 *   method address was either loaded using                                     *
302 *                                                                              *
303 *   i386_mov_imm_reg(a, REG_ITMP2)                ; invokestatic/special       *
304 *   i386_call_reg(REG_ITMP2)                                                   *
305 *                                                                              *
306 *   or                                                                         *
307 *                                                                              *
308 *   i386_mov_membase_reg(REG_SP, 0, REG_ITMP2)    ; invokevirtual/interface    *
309 *   i386_mov_membase_reg(REG_ITMP2, OFFSET(, vftbl), REG_ITMP3)                *
310 *   i386_mov_membase_reg(REG_ITMP3, OFFSET(vftbl, table[0]) + \                *
311 *       sizeof(methodptr) * m->vftblindex, REG_ITMP1)                          *
312 *   i386_call_reg(REG_ITMP1)                                                   *
313 *                                                                              *
314 *   in the static case the method pointer can be computed using the            *
315 *   return address and the lda function following the jmp instruction          *
316 *                                                                              *
317 *******************************************************************************/
318
319 asm_call_jit_compiler:
320 L_asm_call_jit_compiler:                /* required for PIC code              */
321                                             /* keep stack 16-byte aligned         */
322         sub     $((3+ARG_CNT)*8+sizestackframeinfo),sp
323
324         mov     t0,0*8(sp)                  /* save register                      */
325
326         mov     (3+ARG_CNT)*8+sizestackframeinfo(sp),itmp3 /* get return address  */
327         mov     -1(itmp3),%bl               /* get function code                  */
328         cmp     $0xd2,%bl                   /* called with `call *REG_ITMP2'?     */
329         jne     L_not_static_special
330
331         sub     $11,itmp3                   /* calculate address of immediate     */
332         jmp     L_call_jit_compile
333                 
334 L_not_static_special:
335         cmp     $0xd0,%bl                   /* called with `call *REG_ITMP1'      */
336         jne     L_not_virtual_interface
337                 
338         sub     $7,itmp3                    /* calculate address of offset        */
339         mov     (itmp3),itmp3l              /* get offset (32-bit)                */
340         add     itmp2,itmp3                 /* add base address to get method addr*/
341         jmp     L_call_jit_compile
342
343 L_not_virtual_interface:                /* a call from asm_calljavamethod     */
344         xor     itmp3,itmp3
345                 
346 L_call_jit_compile:
347         mov     0*8(sp),t0                  /* restore register                   */
348         mov     itmp3,0*8(sp)               /* save address for method pointer    */
349         mov     itmp1,1*8(sp)               /* save method pointer                */
350
351         SAVE_ARGUMENT_REGISTERS(3)
352
353         mov     sp,a0                       /* create stackframe info             */
354         add     $((3+ARG_CNT)*8),a0         /* pass sfi                           */
355         xor     a1,a1                       /* if pv is NULL, use findmethod      */
356         mov     sp,a2                       /* pass java sp                       */
357         add     $((1+3+ARG_CNT)*8+sizestackframeinfo),a2
358                                             /* pass ra to java function           */
359         mov     ((3+ARG_CNT)*8+sizestackframeinfo)(sp),a3
360         mov     a3,a4                       /* xpc is equal to ra                 */
361         call    stacktrace_create_extern_stackframeinfo@PLT
362
363         mov     1*8(sp),a0                  /* pass method pointer                */
364         call    jit_compile@PLT
365         mov     v0,1*8(sp)                  /* save return value                  */
366
367         mov     sp,a0                       /* remove stackframe info             */
368         add     $((3+ARG_CNT)*8),a0         /* pass sfi                           */
369         call    stacktrace_remove_stackframeinfo@PLT
370
371         mov     0*8(sp),itmp3               /* restore address for method pointer */
372         mov     1*8(sp),v0                  /* restore return value               */
373
374         RESTORE_ARGUMENT_REGISTERS(3)
375
376         add     $((3+ARG_CNT)*8+sizestackframeinfo),sp /* remove stack frame      */
377
378         test    v0,v0                       /* check for exception                */
379         je      L_asm_call_jit_compiler_exception
380
381         test    itmp3,itmp3                 /* is address == 0 (asm_calljavamethod*/
382         je      L_call_method
383
384         mov     v0,(itmp3)                  /* and now save the new pointer       */
385
386 L_call_method:
387         jmp     *v0                         /* ...and now call the new method     */
388
389 L_asm_call_jit_compiler_exception:
390 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
391         call    builtin_asm_get_exceptionptrptr@PLT
392         mov     v0,itmp2                    /* v0 == xptr                         */
393 #else
394         lea     _no_threads_exceptionptr(%rip),itmp2
395 #endif
396         mov     (itmp2),xptr                /* get the exception pointer          */
397         movl    $0,(itmp2)                  /* clear exception pointer            */
398
399         pop     xpc                         /* delete return address              */
400         sub     $5,xpc                      /* faulting address is ra - 5         */
401         jmp     L_asm_handle_exception
402
403
404 /* asm_handle_exception ********************************************************
405 *                                                                              *
406 *   This function handles an exception. It does not use the usual calling      *
407 *   conventions. The exception pointer is passed in REG_ITMP1 and the          *
408 *   pc from the exception raising position is passed in REG_ITMP2. It searches *
409 *   the local exception table for a handler. If no one is found, it unwinds    *
410 *   stacks and continues searching the callers.                                *
411 *                                                                              *
412 *******************************************************************************/
413
414 asm_handle_nat_exception:
415         add     $8,sp                       /* clear return address of native stub*/
416                 
417 asm_handle_exception:
418 L_asm_handle_exception:                 /* required for PIC code              */
419         sub     $((ARG_CNT+TMP_CNT)*8),sp   /* create maybe-leaf stackframe       */
420
421         SAVE_ARGUMENT_REGISTERS(0)          /* we save arg and temp registers in  */
422         SAVE_TEMPORARY_REGISTERS(ARG_CNT)   /* case this is a leaf method         */
423
424         mov     $((ARG_CNT+TMP_CNT)*8),a3   /* prepare a3 for handle_exception    */
425         mov     $1,t0                       /* set maybe-leaf flag                */
426
427 L_asm_handle_exception_stack_loop:
428         sub     $(6*8),sp
429         mov     xptr,0*8(sp)                /* save exception pointer             */
430         mov     xpc,1*8(sp)                 /* save exception pc                  */
431         add     sp,a3                       /* calculate Java sp into a3...       */
432         add     $(6*8),a3
433         mov     a3,3*8(sp)                  /* ...and save it                     */
434         mov     t0,4*8(sp)                  /* save maybe-leaf flag               */
435
436         mov     xpc,a0                      /* exception pc                       */
437         call    codegen_findmethod@PLT
438         mov     v0,2*8(sp)                  /* save data segment pointer          */
439         
440         mov     0*8(sp),a0                  /* pass exception pointer             */
441         mov     1*8(sp),a1                  /* pass exception pc                  */
442         mov     v0,a2                       /* pass data segment pointer          */
443         mov     3*8(sp),a3                  /* pass Java stack pointer            */
444         call    exceptions_handle_exception@PLT
445
446         test    v0,v0
447         jz      L_asm_handle_exception_not_catched
448
449         mov     v0,xpc                      /* move handlerpc into xpc            */
450         mov     0*8(sp),xptr                /* restore exception pointer          */
451         mov     4*8(sp),t0                  /* get maybe-leaf flag                */
452         add     $(6*8),sp                   /* free stack frame                   */
453
454         test    t0,t0                       /* test for maybe-leaf flag           */
455         jz      L_asm_handle_exception_no_leaf
456
457         RESTORE_ARGUMENT_REGISTERS(0)       /* if this is a leaf method, we have  */
458         RESTORE_TEMPORARY_REGISTERS(ARG_CNT)/* to restore arg and temp registers  */
459
460         add     $((ARG_CNT+TMP_CNT)*8),sp   /* remove maybe-leaf stackframe       */
461
462 L_asm_handle_exception_no_leaf:
463         jmp     *xpc                        /* jump to the handler                */
464
465 L_asm_handle_exception_not_catched:
466         mov     0*8(sp),xptr                /* restore exception pointer          */
467         mov     2*8(sp),itmp3               /* restore data segment pointer       */
468         mov     4*8(sp),t0                  /* get maybe-leaf flag                */
469         add     $(6*8),sp
470
471         test    t0,t0
472         jz      L_asm_handle_exception_no_leaf_stack
473
474         add     $((ARG_CNT+TMP_CNT)*8),sp   /* remove maybe-leaf stackframe       */
475         xor     t0,t0                       /* clear the isleaf flags             */
476
477 L_asm_handle_exception_no_leaf_stack:
478         mov     FrameSize(itmp3),itmp2l     /* get frame size                     */
479         add     sp,itmp2                    /* pointer to save area               */
480         
481         mov     IntSave(itmp3),a0l          /* a0l = saved int register count     */
482         test    a0l,a0l
483         je      noint
484         
485         cmp     $1,a0l
486         je      int1
487         cmp     $2,a0l
488         je      int2
489         cmp     $3,a0l
490         je      int3
491         cmp     $4,a0l
492         je      int4
493         
494         mov     -5*8(itmp2),s0
495 int4:   
496         mov     -4*8(itmp2),s1
497 int3:   
498         mov     -3*8(itmp2),s2
499 int2:   
500         mov     -2*8(itmp2),s3
501 int1:   
502         mov     -1*8(itmp2),s4
503
504         shl     $3,a0l                      /* multiply by 8 bytes                */
505         sub     a0,itmp2
506                 
507 noint:
508 #if 0
509         mov     FltSave(itmp3),a0l          /* a0l = saved flt register count     */
510         test    a0l,a0l
511         je      noflt
512         
513         cmpl    $1,a0l
514         je      flt1
515         cmpl    $2,a0l
516         je      flt2
517         cmpl    $3,a0l
518         je      flt3
519         cmpl    $4,a0l
520         je      flt4
521
522         movq    -5*8(itmp2),%xmm11
523 flt4:   
524         movq    -4*8(itmp2),%xmm12
525 flt3:   
526         movq    -3*8(itmp2),%xmm13
527 flt2:   
528         movq    -2*8(itmp2),%xmm14
529 flt1:   
530         movq    -1*8(itmp2),%xmm15
531                 
532 noflt:
533 #endif
534         mov     FrameSize(itmp3),itmp2l     /* get frame size                     */
535         add     itmp2,sp                    /* unwind stack                       */
536
537                                             /* exception pointer is still set     */
538         pop     xpc                         /* the new xpc is return address      */
539         sub     $3,xpc                      /* subtract 3 bytes for call          */
540
541         xor     a3,a3                       /* prepare a3 for handle_exception    */
542         
543         jmp             L_asm_handle_exception_stack_loop
544
545
546 /* asm_wrapper_patcher *********************************************************
547
548    XXX
549
550    Stack layout:
551      40   return address
552      32   pointer to virtual java_objectheader
553      24   machine code (which is patched back later)
554      16   unresolved class/method/field reference
555       8   data segment displacement from load instructions
556       0   pointer to patcher function
557      -8   bp
558
559 *******************************************************************************/
560
561 asm_wrapper_patcher:
562         push    bp                          /* save base pointer                  */
563         mov     sp,bp                       /* move actual sp to bp               */
564         sub     $((3+ARG_CNT+TMP_CNT)*8+sizestackframeinfo),sp
565         and     $0xfffffffffffffff0,sp      /* align sp to 16-byte (this is for   */
566                                             /* leaf functions)                    */
567
568         SAVE_ARGUMENT_REGISTERS(3)
569         SAVE_TEMPORARY_REGISTERS(3+ARG_CNT)
570
571         mov     itmp1,0*8(sp)               /* save itmp1 and itmp2               */
572         mov     itmp2,1*8(sp)               /* can be used by some instructions   */
573
574         mov     sp,a0                       /* create stackframe info             */
575         add     $((3+ARG_CNT+TMP_CNT)*8),a0
576         xor     a1,a1                       /* if pv is NULL, use findmethod      */
577         mov     bp,a2                       /* pass java sp                       */
578         add     $((6+1)*8),a2
579         mov     ((5+1)*8)(bp),a3            /* pass ra to java function           */
580         mov     a3,a4                       /* xpc is equal to ra                 */
581         call    stacktrace_create_extern_stackframeinfo@PLT
582
583         mov     bp,a0                       /* pass stack pointer                 */
584         add     $((1+1)*8),a0               /* skip function pointer              */
585         mov     1*8(bp),itmp3               /* get function pointer               */
586         call    *itmp3                      /* call the patcher function          */
587         mov     v0,2*8(sp)                  /* save return value                  */
588
589         mov     sp,a0                       /* remove stackframe info             */
590         add     $((3+ARG_CNT+TMP_CNT)*8),a0
591         call    stacktrace_remove_stackframeinfo@PLT
592
593         RESTORE_ARGUMENT_REGISTERS(3)
594         RESTORE_TEMPORARY_REGISTERS(3+ARG_CNT)
595
596         mov     0*8(sp),itmp1               /* restore itmp1 and itmp2            */
597         mov     1*8(sp),itmp2               /* can be used by some instructions   */
598         mov     2*8(sp),itmp3               /* restore return value               */
599
600         mov     bp,sp                       /* restore original sp                */
601         pop     bp                          /* restore bp                         */
602         add     $(5*8),sp                   /* remove patcher stackframe, keep ra */
603
604         test    itmp3,itmp3                 /* exception thrown?                  */
605         jz      L_asm_wrapper_patcher_exception
606         ret                                 /* call new patched code              */
607
608 L_asm_wrapper_patcher_exception:
609 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
610         call    builtin_asm_get_exceptionptrptr@PLT
611         mov     v0,itmp2                    /* v0 == xptr                         */
612 #else
613         mov     _no_threads_exceptionptr,itmp2
614 #endif
615         mov     (itmp2),xptr                /* get the exception pointer          */
616         movl    $0,(itmp2)                  /* clear exception pointer            */
617
618         pop     xpc                         /* get and remove return address      */
619         jmp     L_asm_handle_exception
620
621
622 /* asm_replacement_out *********************************************************
623
624    This code is jumped to from the replacement-out stubs that are executed
625    when a thread reaches an activated replacement point.
626
627    The purpose of asm_replacement_out is to read out the parts of the
628    execution state that cannot be accessed from C code, store this state,
629    and then call the C function replace_me.
630
631    Stack layout:
632       8                 start of stack inside method to replace
633       0   rplpoint *    info on the replacement point that was reached
634
635 *******************************************************************************/
636
637 /* some room to accomodate changes of the stack frame size during replacement */
638         /* XXX we should find a cleaner solution here */
639 #define REPLACEMENT_ROOM  512
640
641 asm_replacement_out:
642     /* create stack frame */
643         sub     $(sizeexecutionstate + REPLACEMENT_ROOM),sp
644
645         /* save registers in execution state */
646         mov     %rax,(RAX*8+offes_intregs)(sp)
647         mov     %rbx,(RBX*8+offes_intregs)(sp)
648         mov     %rcx,(RCX*8+offes_intregs)(sp)
649         mov     %rdx,(RDX*8+offes_intregs)(sp)
650         mov     %rsi,(RSI*8+offes_intregs)(sp)
651         mov     %rdi,(RDI*8+offes_intregs)(sp)
652         mov     %rbp,(RBP*8+offes_intregs)(sp)
653         movq    $0  ,(RSP*8+offes_intregs)(sp) /* not used */
654         mov     %r8 ,(R8 *8+offes_intregs)(sp)
655         mov     %r9 ,(R9 *8+offes_intregs)(sp)
656         mov     %r10,(R10*8+offes_intregs)(sp)
657         mov     %r11,(R11*8+offes_intregs)(sp)
658         mov     %r12,(R12*8+offes_intregs)(sp)
659         mov     %r13,(R13*8+offes_intregs)(sp)
660         mov     %r14,(R14*8+offes_intregs)(sp)
661         mov     %r15,(R15*8+offes_intregs)(sp)
662
663         movq    %xmm0 ,(XMM0 *8+offes_fltregs)(sp)
664         movq    %xmm1 ,(XMM1 *8+offes_fltregs)(sp)
665         movq    %xmm2 ,(XMM2 *8+offes_fltregs)(sp)
666         movq    %xmm3 ,(XMM3 *8+offes_fltregs)(sp)
667         movq    %xmm4 ,(XMM4 *8+offes_fltregs)(sp)
668         movq    %xmm5 ,(XMM5 *8+offes_fltregs)(sp)
669         movq    %xmm6 ,(XMM6 *8+offes_fltregs)(sp)
670         movq    %xmm7 ,(XMM7 *8+offes_fltregs)(sp)
671         movq    %xmm8 ,(XMM8 *8+offes_fltregs)(sp)
672         movq    %xmm9 ,(XMM9 *8+offes_fltregs)(sp)
673         movq    %xmm10,(XMM10*8+offes_fltregs)(sp)
674         movq    %xmm11,(XMM11*8+offes_fltregs)(sp)
675         movq    %xmm12,(XMM12*8+offes_fltregs)(sp)
676         movq    %xmm13,(XMM13*8+offes_fltregs)(sp)
677         movq    %xmm14,(XMM14*8+offes_fltregs)(sp)
678         movq    %xmm15,(XMM15*8+offes_fltregs)(sp)
679
680         /* calculate sp of method */
681         mov     sp,itmp1
682         add     $(sizeexecutionstate + REPLACEMENT_ROOM + 8),itmp1
683         mov     itmp1,(offes_sp)(sp)
684
685         /* pv must be looked up via AVL tree */
686         movq    $0,(offes_pv)(sp)
687
688         /* call replace_me */
689         mov     -8(itmp1),a0                /* rplpoint *                         */
690     mov     sp,a1                       /* arg1: execution state              */
691     call    replace_me@PLT              /* call C function replace_me         */
692     call    abort@PLT                   /* NEVER REACHED                      */
693
694 /* asm_replacement_in **********************************************************
695
696    This code writes the given execution state and jumps to the replacement
697    code.
698
699    This function never returns!
700
701    C prototype:
702       void asm_replacement_in(executionstate *es);
703
704 *******************************************************************************/
705
706 asm_replacement_in:
707         mov     a0,%rbp                     /* executionstate *es                 */
708
709         /* set new sp */
710         mov     (offes_sp)(%rbp),%rsp
711         
712         /* store address of new code */
713         push    (offes_pc)(%rbp)
714         
715         /* copy registers from execution state */
716         movq    (XMM0 *8+offes_fltregs)(%rbp),%xmm0
717         movq    (XMM1 *8+offes_fltregs)(%rbp),%xmm1
718         movq    (XMM2 *8+offes_fltregs)(%rbp),%xmm2
719         movq    (XMM3 *8+offes_fltregs)(%rbp),%xmm3
720         movq    (XMM4 *8+offes_fltregs)(%rbp),%xmm4
721         movq    (XMM5 *8+offes_fltregs)(%rbp),%xmm5
722         movq    (XMM6 *8+offes_fltregs)(%rbp),%xmm6
723         movq    (XMM7 *8+offes_fltregs)(%rbp),%xmm7
724         movq    (XMM8 *8+offes_fltregs)(%rbp),%xmm8
725         movq    (XMM9 *8+offes_fltregs)(%rbp),%xmm9
726         movq    (XMM10*8+offes_fltregs)(%rbp),%xmm10
727         movq    (XMM11*8+offes_fltregs)(%rbp),%xmm11
728         movq    (XMM12*8+offes_fltregs)(%rbp),%xmm12
729         movq    (XMM13*8+offes_fltregs)(%rbp),%xmm13
730         movq    (XMM14*8+offes_fltregs)(%rbp),%xmm14
731         movq    (XMM15*8+offes_fltregs)(%rbp),%xmm15
732
733         mov     (RAX*8+offes_intregs)(%rbp),%rax
734         mov     (RBX*8+offes_intregs)(%rbp),%rbx
735         mov     (RCX*8+offes_intregs)(%rbp),%rcx
736         mov     (RDX*8+offes_intregs)(%rbp),%rdx
737         mov     (RSI*8+offes_intregs)(%rbp),%rsi
738         mov     (RDI*8+offes_intregs)(%rbp),%rdi
739         mov     (R8 *8+offes_intregs)(%rbp),%r8
740         mov     (R9 *8+offes_intregs)(%rbp),%r9
741         mov     (R10*8+offes_intregs)(%rbp),%r10
742         mov     (R11*8+offes_intregs)(%rbp),%r11
743         mov     (R12*8+offes_intregs)(%rbp),%r12
744         mov     (R13*8+offes_intregs)(%rbp),%r13
745         mov     (R14*8+offes_intregs)(%rbp),%r14
746         mov     (R15*8+offes_intregs)(%rbp),%r15
747
748         mov     (RBP*8+offes_intregs)(%rbp),%rbp
749
750         /* jump to new code */
751         ret
752
753 /* asm_builtin_x2x *************************************************************
754 *                                                                              *
755 *   Wrapper functions for float to int corner cases                            *
756 *                                                                              *
757 *******************************************************************************/
758
759 asm_builtin_f2i:
760         sub     $(ARG_CNT*8),sp
761         
762         SAVE_ARGUMENT_REGISTERS(0)
763         
764         movq    ftmp1,fa0
765         call    builtin_f2i@PLT
766         
767         RESTORE_ARGUMENT_REGISTERS(0)
768         
769         add     $(ARG_CNT*8),sp
770         ret
771
772
773 asm_builtin_f2l:
774         sub     $(ARG_CNT*8),sp
775         
776         SAVE_ARGUMENT_REGISTERS(0)
777         
778         movq    ftmp1,fa0
779         call    builtin_f2l@PLT
780         
781         RESTORE_ARGUMENT_REGISTERS(0)
782         
783         add     $(ARG_CNT*8),sp
784         ret
785
786
787 asm_builtin_d2i:
788         sub     $(ARG_CNT*8),sp
789         
790         SAVE_ARGUMENT_REGISTERS(0)
791         
792         movq    ftmp1,fa0
793         call    builtin_d2i@PLT
794         
795         RESTORE_ARGUMENT_REGISTERS(0)
796         
797         add     $(ARG_CNT*8),sp
798         ret
799
800
801 asm_builtin_d2l:
802         sub     $(ARG_CNT*8),sp
803         
804         SAVE_ARGUMENT_REGISTERS(0)
805         
806         movq    ftmp1,fa0
807         call    builtin_d2l@PLT
808         
809         RESTORE_ARGUMENT_REGISTERS(0)
810         
811         add     $(ARG_CNT*8),sp
812         ret
813
814
815 /******************* function asm_initialize_thread_stack **********************
816 *                                                                              *
817 * initialized a thread stack                                                   *
818 * (to)->restorePoint = asm_initialize_thread_stack((u1*)(func), (to)->stackEnd)*
819 *                                                                              *
820 *******************************************************************************/
821
822 asm_initialize_thread_stack:
823         sub     $(7*8),%rsi
824
825         xor     %r10,%r10
826         mov     %r10,0*8(%rsi)
827         mov     %r10,1*8(%rsi)
828         mov     %r10,2*8(%rsi)
829         mov     %r10,3*8(%rsi)
830         mov     %r10,4*8(%rsi)
831         mov     %r10,5*8(%rsi)
832
833         mov     %rdi,6*8(%rsi)          /* save (u1*) (func)                  */
834         mov     %rsi,%rax               /* return restorepoint in %rax        */
835         ret
836
837
838 /******************* function asm_perform_threadswitch *************************
839 *                                                                              *
840 *   void asm_perform_threadswitch (u1 **from, u1 **to, u1 **stackTop);         *
841 *                                                                              *
842 *   performs a threadswitch                                                    *
843 *                                                                              *
844 *******************************************************************************/
845
846 asm_perform_threadswitch:
847         sub     $(7*8),%rsp             /* allocate stack frame               */
848
849         mov     %rbx,0*8(%rsp)
850         mov     %rbp,1*8(%rsp)
851         mov     %r12,2*8(%rsp)
852         mov     %r13,3*8(%rsp)
853         mov     %r14,4*8(%rsp)
854         mov     %r15,5*8(%rsp)
855
856         mov     7*8(%rsp),%rax                  /* save current return address        */
857         mov     %rax,6*8(%rsp)
858
859         mov     %rsp,(%rdi)                     /* first argument **from              */
860         mov     %rsp,(%rdx)                     /* third argument **stackTop          */
861
862         mov     (%rsi),%rsp                     /* load new stack pointer             */
863
864         mov     0*8(%rsp),%rbx
865         mov     1*8(%rsp),%rbp
866         mov     2*8(%rsp),%r12
867         mov     3*8(%rsp),%r13
868         mov     4*8(%rsp),%r14
869         mov     5*8(%rsp),%r15
870
871         mov     6*8(%rsp),%rax          /* restore return address             */
872         add     $(7*8),%rsp             /* free stack frame                   */
873         mov     %rax,(%rsp)
874         ret
875                 
876
877 /********************* function asm_switchstackandcall *************************
878 *                                                                              *
879 *  int asm_switchstackandcall (void *stack, void *func, void **stacktopsave,   *
880 *                                      void *p);                                       *
881 *                                                                              *
882 *   Switches to a new stack, calls a function and switches back.               *
883 *       a0 (%rdi)     new stack pointer                                        *
884 *       a1 (%rsi)     function pointer                                         *
885 *               a2 (%rdx)     pointer to variable where stack top should be stored     *
886 *       a3 (%rcx)     pointer to user data, is passed to the function          *
887 *                                                                              *
888 *******************************************************************************/
889
890 asm_switchstackandcall:
891         sub     $(1*8),%rsp             /* keep stack 16-byte aligned         */
892         sub     $16,%rdi                                /* allocate new stack                 */
893
894         mov     8(%rsp),%rax                    /* save return address on new stack   */
895         mov     %rax,(%rdi)
896         mov     %rsp,8(%rdi)                    /* save old stack pointer on new stack*/
897         mov     %rsp,(%rdx)                             /* save old stack pointer to variable */
898
899         mov     %rdi,%rsp                               /* switch to new stack                */
900
901         mov     %rcx,%rdi                       /* pass pointer                       */
902         call    *%rsi                                   /* and call function                  */
903
904         mov     (%rsp),%r10                             /* load return address                */
905         mov     8(%rsp),%rsp                    /* switch to old stack                */
906         add     $(1*8),%rsp             /* free stack space                   */
907         mov     %r10,(%rsp)             /* write return adress                */
908         ret
909
910
911 asm_getclassvalues_atomic:
912 _crit_restart:
913 _crit_begin:
914         movl    offbaseval(a0),itmp1l
915         movl    offdiffval(a0),itmp2l
916         movl    offbaseval(a1),itmp3l
917 _crit_end:
918         movl    itmp1l,offcast_super_baseval(a2)
919         movl    itmp2l,offcast_super_diffval(a2)
920         movl    itmp3l,offcast_sub_baseval(a2)
921         ret
922
923         .data
924                 
925 asm_criticalsections:
926 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
927         .quad   _crit_begin
928         .quad   _crit_end
929         .quad   _crit_restart
930 #endif
931         .quad 0
932
933
934 /* Disable exec-stacks, required for Gentoo ***********************************/
935
936 #if defined(__GCC__) && defined(__ELF__)
937         .section .note.GNU-stack,"",@progbits
938 #endif
939
940
941 /*
942  * These are local overrides for various environment variables in Emacs.
943  * Please do not remove this and leave it at the end of the file, where
944  * Emacs will automagically detect them.
945  * ---------------------------------------------------------------------
946  * Local variables:
947  * mode: asm
948  * indent-tabs-mode: t
949  * c-basic-offset: 4
950  * tab-width: 4
951  * End:
952  * vim:noexpandtab:sw=4:ts=4:
953  */