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