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