Moved stuff to md-asm.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 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 2586 2005-06-08 10:56:49Z 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         .globl asm_builtin_aastore
68
69         .globl asm_builtin_f2i
70         .globl asm_builtin_f2l
71         .globl asm_builtin_d2i
72         .globl asm_builtin_d2l
73
74         .globl asm_perform_threadswitch
75         .globl asm_initialize_thread_stack
76         .globl asm_switchstackandcall
77         .globl asm_criticalsections
78         .globl asm_getclassvalues_atomic
79
80         .globl asm_prepare_native_stackinfo
81         .globl asm_remove_native_stackinfo
82         .globl asm_throw_and_handle_exception
83         .globl asm_throw_and_handle_hardware_arithmetic_exception               
84
85
86 /********************* function asm_calljavafunction ***************************
87 *                                                                              *
88 *   This function calls a Java-method (which possibly needs compilation)       *
89 *   with up to 4 address parameters.                                           *
90 *                                                                              *
91 *   This functions calls the JIT-compiler which eventually translates the      *
92 *   method into machine code.                                                  *
93 *                                                                              *
94 *   C-prototype:                                                               *
95 *    javaobject_header *asm_calljavamethod (methodinfo *m,                     *
96 *         void *arg1, void *arg2, void *arg3, void *arg4);                     *
97 *                                                                              *
98 *******************************************************************************/
99
100 call_name:
101         .align  8
102
103         .quad   0                         /* catch type all                       */
104         .quad   calljava_xhandler         /* handler pc                           */
105         .quad   calljava_xhandler         /* end pc                               */
106         .quad   asm_calljavafunction      /* start pc                             */
107         .long   1                         /* extable size                         */
108         .long   0
109         .quad   0                         /* line number table start              */
110         .quad   0                         /* line number table size               */
111         .long   0
112         .long   0                         /* fltsave                              */
113         .long   0                         /* intsave                              */
114         .long   0                         /* isleaf                               */
115         .long   0                         /* IsSync                               */
116         .long   8                         /* frame size                           */
117         .quad   0                         /* method pointer (pointer to name)     */
118
119 asm_calljavafunction:
120 asm_calljavafunction_int:
121         sub     $(3*8),%rsp               /* keep stack 16-byte aligned           */
122         mov     %rbp,0*8(%rsp)
123         mov     %rbx,1*8(%rsp)            /* %rbx is not a callee saved in cacao  */
124         mov     %rdi,%rax                 /* move function pointer to %rax        */
125                                                                           /* compilerstub uses this               */
126
127         mov     %rsi,%rdi                 /* pass remaining parameters            */
128         mov     %rdx,%rsi
129         mov     %rcx,%rdx
130         mov     %r8,%rcx
131         
132         lea     asm_call_jit_compiler,%r11
133         call    *%r11                     /* call JIT compiler                    */
134
135         mov     0*8(%rsp),%rbp
136         mov             1*8(%rsp),%rbx
137         add     $(3*8),%rsp               /* free stack space                     */
138         ret
139
140 calljava_xhandler:
141         mov     %rax,%rdi                 /* pass exception pointer               */
142         call    builtin_throw_exception
143         mov     0*8(%rsp),%rbp
144         mov             1*8(%rsp),%rbx
145         add     $(3*8),%rsp
146         xor     %rax,%rax                 /* return NULL                          */
147         ret
148
149
150 /********************* function asm_calljavafunction ***************************
151 *                                                                              *
152 *   This function calls a Java-method (which possibly needs compilation)       *
153 *   with up to 4 address parameters.                                           *
154 *                                                                              *
155 *   This functions calls the JIT-compiler which eventually translates the      *
156 *   method into machine code.                                                  *
157 *                                                                              *
158 *   C-prototype:                                                               *
159 *    javaobject_header *asm_calljavamethod (methodinfo *m,                     *
160 *         void *arg1, void *arg2, void *arg3, void *arg4);                     *
161 *                                                                              *
162 *******************************************************************************/
163
164 call_name2:
165         .align  8
166
167         .quad   0                         /* catch type all                       */
168         .quad   calljava_xhandler2        /* handler pc                           */
169         .quad   calljava_xhandler2        /* end pc                               */
170         .quad   asm_calljavafunction2     /* start pc                             */
171         .long   1                         /* extable size                         */
172         .long   0
173         .quad   0                         /* line number table  start             */
174         .quad   0                         /* line number table  size              */
175         .long   0
176         .long   0                         /* fltsave                              */
177         .long   0                         /* intsave                              */
178         .long   0                         /* isleaf                               */
179         .long   0                         /* IsSync                               */
180         .long   24                        /* frame size                           */
181         .quad   0                         /* method pointer (pointer to name)     */
182
183 asm_calljavafunction2:
184 asm_calljavafunction2int:
185 asm_calljavafunction2long:
186 asm_calljavafunction2float:
187 asm_calljavafunction2double:
188         sub     $(7*8),%rsp               /* keep stack 16-byte aligned           */
189         mov     %rbx,0*8(%rsp)            /* %rbx is not a callee saved in cacao  */
190         mov     %rbp,1*8(%rsp)
191         mov     %r12,2*8(%rsp)
192         mov     %r13,3*8(%rsp)
193         mov     %r14,4*8(%rsp)
194         mov     %r15,5*8(%rsp)
195
196         mov     %rdi,%rax                 /* move method pointer for compiler     */
197         xor     %rbp,%rbp                 /* set argument stack frame to zero     */
198
199         test    %rsi,%rsi                 /* maybe we have no args...             */
200         jle     L_copy_done
201
202         mov     %rsi,itmp3                /* arg count                            */
203         mov     %rcx,itmp2                /* pointer to arg block                 */
204
205         mov     itmp2,%r14                /* save argument block pointer          */
206         mov     itmp3,%r15                /* save argument count                  */
207
208         sub     $sizejniblock,itmp2       /* initialize pointer (smaller code)    */
209         add     $1,itmp3                  /* initialize argument count            */
210         xor     %r12,%r12                 /* initialize integer argument counter  */
211         xor     %r13,%r13                 /* initialize float argument counter    */
212
213 L_register_copy:
214         add     $sizejniblock,itmp2       /* goto next argument block             */
215         dec     itmp3                     /* argument count - 1                   */
216         jz      L_register_copy_done
217         andb    $0x02,offjniitemtype(itmp2) /* is this a float/double type?       */
218         jnz     L_register_handle_float   /* yes, handle it                       */
219
220         cmp     $INT_ARG_CNT,%r12         /* are we out of integer argument       */
221         je      L_register_copy           /* register? yes, next loop             */
222
223         lea     jumptable_integer,%rbp
224         mov     0(%rbp,%r12,8),%rbx
225         inc     %r12                      /* integer argument counter + 1         */
226         jmp     *%rbx
227
228 L_register_handle_float:
229         cmp     $FLT_ARG_CNT,%r13         /* are we out of float argument         */
230         je      L_register_copy           /* register? yes, next loop             */
231
232         lea     jumptable_float,%rbp
233         mov     0(%rbp,%r13,8),%rbx
234         inc     %r13                      /* float argument counter + 1           */
235         jmp     *%rbx
236         
237 L_register_copy_done:
238         mov     %r15,%rbp                 /* calculate remaining arguments        */
239         sub     %r12,%rbp                 /* - integer arguments in registers     */
240         sub     %r13,%rbp                 /* - float arguments in registers       */
241         jle     L_copy_done               /* are all assigned to registers?       */
242
243         shl     $3,%rbp                   /* calculate stack size                 */
244         sub     %rbp,%rsp                 /* stack frame for arguments            */
245         mov     %rsp,%rbx                 /* use %rbx as temp sp                  */
246
247         sub     $sizejniblock,%r14        /* initialize pointer (smaller code)    */
248         add     $1,%r15                   /* initialize argument count            */
249                 
250 L_stack_copy_loop:
251         add     $sizejniblock,%r14        /* goto next argument block             */
252         dec     %r15                      /* are there any arguments left?        */
253         jz      L_copy_done               /* no test needed after dec             */
254
255         andb    $0x02,offjniitemtype(%r14) /* is this a float/double type?        */
256         jnz     L_stack_handle_float
257         dec     %r12                      /* arguments assigned to registers      */
258         jge     L_stack_copy_loop
259         jmp     L_stack_copy
260
261 L_stack_handle_float:
262         dec     %r13                      /* arguments assigned to registers      */
263         jge     L_stack_copy_loop
264
265 L_stack_copy:
266     mov     offjniitem(%r14),itmp3    /* copy s8 argument onto stack          */
267     mov     itmp3,0(%rbx)
268     add     $8,%rbx                   /* increase sp to next argument         */
269         jmp     L_stack_copy_loop
270
271 L_copy_done:
272         lea     asm_call_jit_compiler,%r11/* %rax still contains method pointer   */
273         call    *%r11                     /* call JIT compiler                    */
274
275         add     %rbp,%rsp                 /* remove argument stack frame if any   */
276
277         mov     5*8(%rsp),%r15            /* restore callee saved registers       */
278         mov     4*8(%rsp),%r14
279         mov     3*8(%rsp),%r13
280         mov     2*8(%rsp),%r12
281         mov     1*8(%rsp),%rbp
282         mov     0*8(%rsp),%rbx
283         add     $(7*8),%rsp               /* free stack space                     */
284         ret
285                 
286 calljava_xhandler2:
287         mov     %rax,%rdi                 /* pass exception pointer               */
288         call    builtin_throw_exception
289
290         mov     5*8(%rsp),%r15            /* restore callee saved registers       */
291         mov     4*8(%rsp),%r14
292         mov     3*8(%rsp),%r13
293         mov     2*8(%rsp),%r12
294         mov     1*8(%rsp),%rbp
295         mov     0*8(%rsp),%rbx
296         add     $(7*8),%rsp               /* free stack space                     */
297         xor     %rax,%rax                 /* return NULL                          */
298         ret
299
300
301 jumptable_integer:
302         .quad   handle_a0
303         .quad   handle_a1
304         .quad   handle_a2
305         .quad   handle_a3
306         .quad   handle_a4
307         .quad   handle_a5
308
309 handle_a0:
310         mov     offjniitem(itmp2),a0
311         jmp     L_register_copy
312 handle_a1:
313         mov     offjniitem(itmp2),a1
314         jmp     L_register_copy
315 handle_a2:
316         mov     offjniitem(itmp2),a2
317         jmp     L_register_copy
318 handle_a3:
319         mov     offjniitem(itmp2),a3
320         jmp     L_register_copy
321 handle_a4:
322         mov     offjniitem(itmp2),a4
323         jmp     L_register_copy
324 handle_a5:
325         mov     offjniitem(itmp2),a5
326         jmp     L_register_copy
327
328
329 jumptable_float:
330         .quad   handle_fa0
331         .quad   handle_fa1
332         .quad   handle_fa2
333         .quad   handle_fa3
334         .quad   handle_fa4
335         .quad   handle_fa5
336         .quad   handle_fa6
337         .quad   handle_fa7
338
339 handle_fa0:
340         movq    offjniitem(itmp2),fa0
341         jmp     L_register_copy
342 handle_fa1:
343         movq    offjniitem(itmp2),fa1
344         jmp     L_register_copy
345 handle_fa2:
346         movq    offjniitem(itmp2),fa2
347         jmp     L_register_copy
348 handle_fa3:
349         movq    offjniitem(itmp2),fa3
350         jmp     L_register_copy
351 handle_fa4:
352         movq    offjniitem(itmp2),fa4
353         jmp     L_register_copy
354 handle_fa5:
355         movq    offjniitem(itmp2),fa5
356         jmp     L_register_copy
357 handle_fa6:
358         movq    offjniitem(itmp2),fa6
359         jmp     L_register_copy
360 handle_fa7:
361         movq    offjniitem(itmp2),fa7
362         jmp     L_register_copy
363
364
365 /****************** function asm_call_jit_compiler *****************************
366 *                                                                              *
367 *   invokes the compiler for untranslated JavaVM methods.                      *
368 *                                                                              *
369 *   Register R0 contains a pointer to the method info structure (prepared      *
370 *   by createcompilerstub). Using the return address in R26 and the            *
371 *   offset in the LDA instruction or using the value in methodptr R28 the      *
372 *   patching address for storing the method address can be computed:           *
373 *                                                                              *
374 *   method address was either loaded using                                     *
375 *                                                                              *
376 *   i386_mov_imm_reg(a, REG_ITMP2)                ; invokestatic/special       *
377 *   i386_call_reg(REG_ITMP2)                                                   *
378 *                                                                              *
379 *   or                                                                         *
380 *                                                                              *
381 *   i386_mov_membase_reg(REG_SP, 0, REG_ITMP2)    ; invokevirtual/interface    *
382 *   i386_mov_membase_reg(REG_ITMP2, OFFSET(, vftbl), REG_ITMP3)                *
383 *   i386_mov_membase_reg(REG_ITMP3, OFFSET(vftbl, table[0]) + \                *
384 *       sizeof(methodptr) * m->vftblindex, REG_ITMP1)                          *
385 *   i386_call_reg(REG_ITMP1)                                                   *
386 *                                                                              *
387 *   in the static case the method pointer can be computed using the            *
388 *   return address and the lda function following the jmp instruction          *
389 *                                                                              *
390 *******************************************************************************/
391
392 asm_call_jit_compiler:
393         sub     $8,%rsp                 /* keep stack 16-byte aligned             */
394
395         mov     %rbx,(%rsp)             /* save register                          */
396         
397         mov     8(%rsp),%r11        /* get return address                         */
398         mov     -1(%r11),%bl        /* get function code                          */
399         cmp     $0xd2,%bl           /* called with `call *REG_ITMP2' (%r10)?      */
400         jne     L_not_static_special
401
402         sub     $11,%r11            /* calculate address of immediate             */
403         jmp     L_call_jit_compile
404                 
405 L_not_static_special:
406         cmp     $0xd0,%bl           /* called with `call *REG_ITMP1' (%rax)       */
407         jne     L_not_virtual_interface
408                 
409         sub     $7,%r11             /* calculate address of offset                */
410         mov     (%r11),%r11d        /* get offset (32-bit)                        */
411         add     %r10,%r11           /* add base address to get method address     */
412         jmp     L_call_jit_compile
413
414 L_not_virtual_interface:        /* a call from asm_calljavamethod             */
415         xor     %r11,%r11
416                 
417 L_call_jit_compile:
418         mov     (%rsp),%rbx         /* restore register                           */
419         
420         sub     $(24*8),%rsp        /* 8 + 6*8 + 8*8 + 8*8                        */
421         
422         mov     %r11,0*8(%rsp)      /* save address for method pointer            */
423
424         SAVE_ARGUMENT_REGISTERS(1)
425
426         mov     %rax,%rdi           /* pass method pointer                        */
427         call    jit_compile
428
429         mov     0*8(%rsp),%r11      /* restore address for method pointer         */
430
431         RESTORE_ARGUMENT_REGISTERS(1)
432
433         add     $(24*8),%rsp
434         add     $8,%rsp                 /* keep stack 16-byte aligned             */
435
436         test    %rax,%rax               /* check for exception                    */
437         je      L_asm_call_jit_compiler_exception
438
439         test    %r11,%r11               /* is address == 0 (asm_calljavamethod)   */
440         je      L_call_method
441
442         mov     %rax,(%r11)             /* and now save the new pointer           */
443
444 L_call_method:
445         jmp     *%rax                   /* ...and now call the new method         */
446
447 L_asm_call_jit_compiler_exception:
448 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
449         call    builtin_asm_get_exceptionptrptr
450         mov     %rax,itmp2
451 #else
452         lea     _exceptionptr,itmp2
453 #endif
454         mov     (itmp2),xptr                /* get the exception pointer          */
455         movl    $0,(itmp2)                  /* clear the exception pointer        */
456
457         pop     xpc                         /* delete return address              */
458         sub     $5,xpc                      /* faulting address is ra - 5         */
459
460         jmp     asm_handle_exception
461
462
463 /********************* function asm_handle_exception ***************************
464 *                                                                              *
465 *   This function handles an exception. It does not use the usual calling      *
466 *   conventions. The exception pointer is passed in REG_ITMP1 and the          *
467 *   pc from the exception raising position is passed in REG_ITMP2. It searches *
468 *   the local exception table for a handler. If no one is found, it unwinds    *
469 *   stacks and continues searching the callers.                                *
470 *                                                                              *
471 *   void asm_handle_exception (exceptionptr, exceptionpc);                     *
472 *                                                                              *
473 *******************************************************************************/
474
475 asm_handle_nat_exception:
476         add     $8,%rsp                     /* clear return address of native stub*/
477                 
478 asm_handle_exception:
479         sub     $(4*8),%rsp
480         mov     xptr,0*8(%rsp)              /* save exception pointer             */
481         mov     xpc,1*8(%rsp)               /* save exception pc                  */
482         
483         mov     xpc,%rdi                    /* exception pc                       */
484         call    codegen_findmethod
485         mov     %rax,itmp3
486         mov     %rax,2*8(%rsp)              /* save data segment pointer          */
487         
488         mov     0*8(%rsp),%rax              /* restore exception pointer          */
489         mov     1*8(%rsp),%r10              /* restore exception pc               */
490         
491 ex_stack_loop:
492         mov     %rax,%rdi                   /* exception pointer                  */
493         mov     MethodPointer(itmp3),%rsi   /* method pointer                     */
494         mov     xpc,%rdx                    /* exception pc                       */
495         mov     $0,%rcx
496         mov     $1,%r8                      /* set noindent flag                  */
497         call    builtin_trace_exception
498
499         mov     2*8(%rsp),itmp3             /* %r11 = data segment pointer        */
500         mov     ExTableSize(itmp3),%rcx     /* %rcx = exception table size        */
501         test    %rcx,%rcx                                       /* if empty table skip                */
502         je      empty_table
503
504         lea     ExTableStart(itmp3),%rdi    /* %rdi = start of exception table    */
505         mov     0*8(%rsp),xptr              /* get xptr                           */
506                 
507 ex_table_loop:
508         mov     1*8(%rsp),xpc               /* get xpc                            */
509                 
510         mov     ExStartPC(%rdi),%rdx        /* %rdx = exception start pc          */
511         cmp     xpc,%rdx                    /* %rdx = (startpc <= xpc)            */
512         jg      ex_table_cont               /* if (false) continue                */
513         mov     ExEndPC(%rdi),%rdx          /* %rdx = exception end pc            */
514         cmp     %rdx,xpc                    /* %rdx = (xpc < endpc)               */
515         jge     ex_table_cont               /* if (false) continue                */
516         mov     ExCatchType(%rdi),%rdx      /* %rdx = exception catch type        */
517         test    %rdx,%rdx                   /* NULL catches everything            */
518         je      ex_handle_it
519
520         cmpl    $0,offclassloaded(%rdx)     /* check if class is loaded           */
521         jne     L_class_loaded
522
523         sub     $(4*8),%rsp
524         mov     %rax,0*8(%rsp)
525         mov     %rcx,1*8(%rsp)
526         mov     %rdx,2*8(%rsp)
527         mov     %rdi,3*8(%rsp)
528
529         mov     %rdx,%rdi
530         call    load_class_bootstrap
531
532         mov     0*8(%rsp),%rax
533         mov     1*8(%rsp),%rcx
534         mov     2*8(%rsp),%rdx
535         mov     3*8(%rsp),%rdi
536         add     $(4*8),%rsp
537
538 L_class_loaded:
539         cmpl    $0,offclasslinked(%rdx)     /* check if class is linked           */
540         jne     L_class_linked
541
542         sub     $(4*8),%rsp
543         mov     %rax,0*8(%rsp)
544         mov     %rcx,1*8(%rsp)
545         mov     %rdx,2*8(%rsp)
546         mov     %rdi,3*8(%rsp)
547
548         mov     %rdx,%rdi
549         call    link_class
550
551         mov     0*8(%rsp),%rax
552         mov     1*8(%rsp),%rcx
553         mov     2*8(%rsp),%rdx
554         mov     3*8(%rsp),%rdi
555         add     $(4*8),%rsp
556
557 L_class_linked:
558 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
559         push    %rdx
560
561 _crit_restart1:
562         mov     0(%rsp),%rdx
563 #endif
564
565 _crit_begin1:
566         mov     offobjvftbl(%rax),%rsi      /* %rsi = vftblptr(xptr)              */
567         mov     offclassvftbl(%rdx),%rdx    /* %rdx = vftblptr(catchtype) class (not obj) */
568         mov     offbaseval(%rsi),%esi       /* %esi = baseval(xptr)               */
569         mov     offbaseval(%rdx),%r10d      /* %r10d = baseval(catchtype)         */
570         mov     offdiffval(%rdx),%edx       /* %edx = diffval(catchtype)          */
571 _crit_end1:
572         sub     %r10d,%esi                  /* %esi = baseval(xptr) - baseval(catchtype) */
573
574 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
575         add     $8,%rsp
576 #endif
577
578         cmp     %edx,%esi                   /* xptr is instanceof catchtype       */
579         ja      ex_table_cont
580                 
581 ex_handle_it:
582                 mov     ExHandlerPC(%rdi),xpc       /* xpc = exception handler pc     */
583
584         mov     0*8(%rsp),%rax              /* restore exception pointer      */
585         add     $(4*8),%rsp                 /* free stack frame               */
586
587         jmp             *xpc                        /* jump to the handler            */
588
589 ex_table_cont:
590         lea             ExEntrySize(%rdi),%rdi      /* next exception table entry     */
591         dec     %rcx                        /* decrement entry counter        */
592         test    %rcx,%rcx                   /* if (t0 > 0) next entry         */
593         jg              ex_table_loop
594                 
595 empty_table:
596         mov     0*8(%rsp),%rax              /* restore exception pointer      */
597         mov     1*8(%rsp),%r10              /* restore exception pc           */
598         mov     2*8(%rsp),%r11                          /* restore data segment pointer   */
599         add     $(4*8),%rsp
600         
601                 mov     %rax,%rcx                                       /* save exception pointer         */
602                                 
603 ex_already_cleared:             
604                 movl    IsSync(%r11),%eax                       /* %rax = SyncOffset              */
605                 test    %rax,%rax                                       /* if zero no monitorexit         */
606                 je              no_monitor_exit
607
608 #if defined(USE_THREADS)                
609                 add     %rsp,%rax
610                 mov     -8(%rax),%rdi
611
612         sub     $(4*8),%rsp
613         mov     %rcx,0*8(%rsp)
614         mov     %r10,1*8(%rsp)
615         mov     %r11,2*8(%rsp)
616         
617                 call    builtin_monitorexit
618
619         mov     0*8(%rsp),%rcx
620         mov     1*8(%rsp),%r10
621         mov     2*8(%rsp),%r11
622         add     $(4*8),%rsp
623 #endif
624
625 no_monitor_exit:
626                 mov             FrameSize(%r11),%eax        /* %eax = frame size              */
627                 add             %rax,%rsp                   /* unwind stack                   */
628                 mov             %rsp,%rax                   /* %rax = pointer to save area    */
629
630                 mov             IntSave(%r11),%edx          /* %edx = saved int register count*/
631         test    %edx,%edx
632         je              noint
633         
634                 cmp     $1,%edx
635                 je              int1
636                 cmp             $2,%edx
637                 je              int2
638                 cmp             $3,%edx
639                 je              int3
640                 cmp             $4,%edx
641                 je              int4
642                 cmp             $5,%edx
643                 je              int5
644         
645         mov     -48(%rax),%rbx
646 int5:   
647         mov     -40(%rax),%rbp
648 int4:   
649         mov     -32(%rax),%r12
650 int3:   
651         mov     -24(%rax),%r13
652 int2:   
653                 mov     -16(%rax),%r14
654 int1:   
655                 mov     -8(%rax),%r15
656
657                 shl             $3,%edx                     /* multiply by 8 bytes            */
658                 sub     %rdx,%rax
659                 
660 noint:
661                 mov     FltSave(%r11),%edx          /* %edx = saved flt register count*/
662                 test    %edx,%edx
663                 je      noflt
664
665         cmpl    $1,%edx
666         je      flt1
667         cmpl    $2,%edx
668         je      flt2
669         cmpl    $3,%edx
670         je      flt3
671         cmpl    $4,%edx
672         je      flt4
673         cmpl    $5,%edx
674         je      flt5
675         cmpl    $6,%edx
676         je      flt7
677         cmpl    $7,%edx
678         je      flt7
679
680         movq    -64(%rax),%xmm8
681 flt7:   
682         movq    -56(%rax),%xmm9
683 flt6:   
684         movq    -48(%rax),%xmm10
685 flt5:   
686         movq    -40(%rax),%xmm11
687 flt4:   
688         movq    -32(%rax),%xmm12
689 flt3:   
690         movq    -24(%rax),%xmm13
691 flt2:   
692         movq    -16(%rax),%xmm14
693 flt1:   
694         movq    -8(%rax),%xmm15
695                 
696 noflt:                                  
697                 pop     %r10                                            /* the new xpc is return address  */
698                 sub     $3,%r10                     /* subtract 3 bytes for call      */
699
700         sub     $(2*8),%rsp
701         mov     %rcx,0*8(%rsp)
702         mov     %r10,1*8(%rsp)
703
704         mov     %r10,%rdi
705         call    codegen_findmethod          /* get the new data segment ptr   */
706         mov     %rax,%r11
707         
708         mov     0*8(%rsp),%rcx
709         mov     1*8(%rsp),%r10
710         add     $(2*8),%rsp
711
712         mov     %rcx,%rax                                       /* restore saved exception pointer*/
713
714         sub     $(4*8),%rsp
715                                         
716         mov     %rax,0*8(%rsp)              /* save exception pointer         */
717         mov     %r10,1*8(%rsp)              /* save exception pc              */
718                 mov     %r11,2*8(%rsp)                          /* save data segment pointer      */
719                 
720                 jmp             ex_stack_loop
721
722
723 /* asm_wrapper_patcher *********************************************************
724
725    XXX
726
727    Stack layout:
728      32   return address
729          24   pointer to virtual java_objectheader
730      16   machine code (which is patched back later)
731       8   unresolved class/method/field reference
732       0   pointer to patcher function
733
734 *******************************************************************************/
735
736 asm_wrapper_patcher:
737         sub     $(17*8),%rsp                /* stack frame (16-byte aligned)      */
738
739         SAVE_ARGUMENT_REGISTERS(0)
740         SAVE_TEMPORARY_REGISTERS(14)
741
742         mov     itmp1,15*8(%rsp)            /* save itmp1 and itmp2               */
743         mov     itmp2,16*8(%rsp)            /* can be used by some instructions   */
744
745         mov     %rsp,a0                     /* pass stack pointer                 */
746         add     $(18*8),a0                  /* skip patcher function pointer      */
747         mov     17*8(%rsp),itmp3            /* get function pointer               */
748         call    *itmp3                      /* call the patcher function          */
749         mov     v0,itmp3                    /* save return value                  */
750
751         RESTORE_ARGUMENT_REGISTERS(0)
752         RESTORE_TEMPORARY_REGISTERS(14)
753
754         mov     15*8(%rsp),itmp1            /* restore itmp1 and itmp2            */
755         mov     16*8(%rsp),itmp2            /* can be used by some instructions   */
756
757         add     $((4+17)*8),%rsp            /* remove stack frame, keep ra        */
758
759         test    itmp3,itmp3                 /* exception thrown?                  */
760         jz      L_asm_wrapper_patcher_exception
761         ret                                 /* call new patched code              */
762
763 L_asm_wrapper_patcher_exception:
764         /*stack bottom is xpc and it is directly below the last java stackframe*/
765         push $0
766         push $0
767         push $0 /*padding*/
768         call asm_prepare_native_stackinfo /* be aware of the stack effect and calling convention explained above*/
769
770 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
771         call    builtin_asm_get_exceptionptrptr
772         mov     v0,itmp2
773 #else
774         lea     _exceptionptr,itmp2
775 #endif
776         mov     (itmp2),a0                  /* get the exception pointer          */
777         movl    $0,(itmp2)                  /* clear exception pointer            */
778         call    helper_fillin_stacktrace
779
780         mov     v0,xptr
781
782         call    asm_remove_native_stackinfo /* be aware of the stack effect and calling convention explained above*/
783         add     $8,%rsp
784
785         pop     xpc                         /* get and remove return address      */
786         jmp     asm_handle_exception
787
788
789 /* asm_builtin_x2x *************************************************************
790 *                                                                              *
791 *   Wrapper functions for float to int corner cases                            *
792 *                                                                              *
793 *******************************************************************************/
794
795 asm_builtin_f2i:
796         sub     $(14*8),%rsp
797         
798         SAVE_ARGUMENT_REGISTERS(0)
799         
800         movq    %xmm8,%xmm0
801         call    builtin_f2i
802         
803         RESTORE_ARGUMENT_REGISTERS(0)
804         
805         add     $(14*8),%rsp
806         ret
807
808
809 asm_builtin_f2l:
810         sub     $(14*8),%rsp
811         
812         SAVE_ARGUMENT_REGISTERS(0)
813         
814         movq    %xmm8,%xmm0
815         call    builtin_f2l
816         
817         RESTORE_ARGUMENT_REGISTERS(0)
818         
819         add     $(14*8),%rsp
820         ret
821
822
823 asm_builtin_d2i:
824         sub     $(14*8),%rsp
825         
826         SAVE_ARGUMENT_REGISTERS(0)
827         
828         movq    %xmm8,%xmm0
829         call    builtin_d2i
830         
831         RESTORE_ARGUMENT_REGISTERS(0)
832         
833         add     $(14*8),%rsp
834         ret
835
836
837 asm_builtin_d2l:
838         sub     $(14*8),%rsp
839         
840         SAVE_ARGUMENT_REGISTERS(0)
841         
842         movq    %xmm8,%xmm0
843         call    builtin_d2l
844         
845         RESTORE_ARGUMENT_REGISTERS(0)
846         
847         add     $(14*8),%rsp
848         ret
849
850
851 /* asm_builtin_arraycheckcast **************************************************
852
853    Does the cast check and eventually throws an exception.
854
855 *******************************************************************************/
856
857 asm_builtin_arraycheckcast:
858         sub     $24,%rsp                    /* keep stack 16-byte aligned         */
859         mov     %rdi,(%rsp)                 /* save object pointer                */
860         call    builtin_arraycheckcast
861         test    %rax,%rax                   /* if (false) throw exception         */
862         je      nb_carray_throw
863         mov     (%rsp),%rax                 /* return object pointer              */
864         add     $24,%rsp                    /* free stack space                   */
865         ret
866
867 nb_carray_throw:
868         /*call    new_classcastexception*/
869         add     $24,%rsp
870         pop     xpc                        /* delete return address              */
871         sub     $3,xpc                     /* faulting address is ra - 3         */
872         mov     string_java_lang_ClassCastException,xptr
873         jmp     asm_throw_and_handle_exception
874
875                 
876 /* asm_builtin_aastore *********************************************************
877
878    Checks if the object can be stored in the given array and stores the
879    address if it's possible. This function can also throw some exceptions.
880
881 *******************************************************************************/
882
883 asm_builtin_aastore:
884         sub     $(3*8),%rsp             /* allocate stack space                   */
885         test    %rdi,%rdi               /* if null pointer throw exception        */
886         je      nb_aastore_null
887
888         movl    offarraysize(%rdi),%eax /* load size                              */
889         cmpl    %eax,%esi               /* do bound check                         */
890         jae     nb_aastore_bound        /* if out of bounds throw exception       */
891
892         shl     $3,%rsi                 /* index * 8                              */
893         mov     %rdi,%r10
894         add     %rsi,%r10               /* add index * 8 to arrayref              */
895
896         mov     %r10,(%rsp)             /* save store position                    */
897         mov     %rdx,8(%rsp)            /* save object                            */
898
899         mov     %rdx,%rsi               /* object is second argument              */
900         call    builtin_canstore        /* builtin_canstore(arrayref,object)      */
901         test    %rax,%rax               /* if (false) throw exception             */
902         je      nb_aastore_throw
903
904         mov     (%rsp),%r10             /* restore store position                 */
905         mov     8(%rsp),%rdx            /* restore object                         */
906         mov     %rdx,offobjarrdata(%r10)/* store objectptr in array               */
907         add     $(3*8),%rsp             /* free stack space                       */
908         ret
909
910 nb_aastore_null:
911         add     $24,%rsp
912         pop     xpc                    /* delete return address from stack       */
913         sub     $3,xpc                 /* faulting address is return adress - 3  */
914         mov     string_java_lang_NullPointerException,xptr
915         jmp    asm_throw_and_handle_exception
916
917 nb_aastore_bound:
918         add     $24,%rsp
919         push    $0 /*directly below return address*/
920         push    $0 /*internal*/
921         push    $0 /*padding*/
922         mov     %rsi,itmp1
923         
924         call    asm_prepare_native_stackinfo
925
926         mov     itmp1,%rdi               /* move index into a0                     */
927         call    new_arrayindexoutofboundsexception
928
929         call    asm_remove_native_stackinfo
930
931         pop     xpc                    /* just remove one quadword                  */
932         pop     xpc                    /* delete return address                  */
933         sub     $3,xpc                 /* faulting address is return adress - 3  */
934         jmp     asm_handle_exception
935                 
936 nb_aastore_throw:
937         /*call    new_arraystoreexception*/
938         add     $24,%rsp
939         pop     xpc                    /* delete return address                  */
940         sub     $3,xpc                 /* faulting address is return adress - 3  */
941         mov     string_java_lang_ArrayStoreException,xptr
942         jmp     asm_throw_and_handle_exception
943
944                 
945 /******************* function asm_initialize_thread_stack **********************
946 *                                                                              *
947 * initialized a thread stack                                                   *
948 * (to)->restorePoint = asm_initialize_thread_stack((u1*)(func), (to)->stackEnd)*
949 *                                                                              *
950 *******************************************************************************/
951
952 asm_initialize_thread_stack:
953         sub     $(7*8),%rsi
954
955         xor     %r10,%r10
956         mov     %r10,0*8(%rsi)
957         mov     %r10,1*8(%rsi)
958         mov     %r10,2*8(%rsi)
959         mov     %r10,3*8(%rsi)
960         mov     %r10,4*8(%rsi)
961         mov     %r10,5*8(%rsi)
962
963         mov     %rdi,6*8(%rsi)          /* save (u1*) (func)                  */
964         mov     %rsi,%rax               /* return restorepoint in %rax        */
965         ret
966
967
968 /******************* function asm_perform_threadswitch *************************
969 *                                                                              *
970 *   void asm_perform_threadswitch (u1 **from, u1 **to, u1 **stackTop);         *
971 *                                                                              *
972 *   performs a threadswitch                                                    *
973 *                                                                              *
974 *******************************************************************************/
975
976 asm_perform_threadswitch:
977         sub     $(7*8),%rsp             /* allocate stack frame               */
978
979         mov     %rbx,0*8(%rsp)
980         mov     %rbp,1*8(%rsp)
981         mov     %r12,2*8(%rsp)
982         mov     %r13,3*8(%rsp)
983         mov     %r14,4*8(%rsp)
984         mov     %r15,5*8(%rsp)
985
986         mov     7*8(%rsp),%rax                  /* save current return address        */
987         mov     %rax,6*8(%rsp)
988
989         mov     %rsp,(%rdi)                     /* first argument **from              */
990         mov     %rsp,(%rdx)                     /* third argument **stackTop          */
991
992         mov     (%rsi),%rsp                     /* load new stack pointer             */
993
994         mov     0*8(%rsp),%rbx
995         mov     1*8(%rsp),%rbp
996         mov     2*8(%rsp),%r12
997         mov     3*8(%rsp),%r13
998         mov     4*8(%rsp),%r14
999         mov     5*8(%rsp),%r15
1000
1001         mov     6*8(%rsp),%rax          /* restore return address             */
1002         add     $(7*8),%rsp             /* free stack frame                   */
1003         mov     %rax,(%rsp)
1004         ret
1005                 
1006
1007 /********************* function asm_switchstackandcall *************************
1008 *                                                                              *
1009 *  int asm_switchstackandcall (void *stack, void *func, void **stacktopsave,   *
1010 *                                      void *p);                                       *
1011 *                                                                              *
1012 *   Switches to a new stack, calls a function and switches back.               *
1013 *       a0 (%rdi)     new stack pointer                                        *
1014 *       a1 (%rsi)     function pointer                                         *
1015 *               a2 (%rdx)     pointer to variable where stack top should be stored     *
1016 *       a3 (%rcx)     pointer to user data, is passed to the function          *
1017 *                                                                              *
1018 *******************************************************************************/
1019
1020 asm_switchstackandcall:
1021         sub     $(1*8),%rsp             /* keep stack 16-byte aligned         */
1022         sub     $16,%rdi                                /* allocate new stack                 */
1023
1024         mov     8(%rsp),%rax                    /* save return address on new stack   */
1025         mov     %rax,(%rdi)
1026         mov     %rsp,8(%rdi)                    /* save old stack pointer on new stack*/
1027         mov     %rsp,(%rdx)                             /* save old stack pointer to variable */
1028
1029         mov     %rdi,%rsp                               /* switch to new stack                */
1030
1031         mov     %rcx,%rdi                       /* pass pointer                       */
1032         call    *%rsi                                   /* and call function                  */
1033
1034         mov     (%rsp),%r10                             /* load return address                */
1035         mov     8(%rsp),%rsp                    /* switch to old stack                */
1036         add     $(1*8),%rsp             /* free stack space                   */
1037         mov     %r10,(%rsp)             /* write return adress                */
1038         ret
1039
1040
1041
1042
1043 /************************ function asm_prepare_native_stackinfo ****************************
1044 *                                                                                          *
1045 *    creates a stackfame for the begin of a native function (either builtin or not )       *
1046 *    expected stack at begin of function                                                   *
1047 *                                        ....                                              *
1048 *                   address of the jit call which invokes the native                       *
1049 *                   begin address of stack frame of the java method                        *
1050 *                   method pointer or 0 (for built ins)                                    *
1051 *                   padding for stackframesize 16*n+8                                      *
1052 *                   return address                                                         *
1053 *                                                                                          *
1054 *    at end of function:                                                                   *
1055 *                                          ...                                             *
1056 *                   address of the jit call which invokes the native                       *
1057 *                   begin address of stack frame of the java method                        *
1058 *                   method pointer or 0 (for built ins)                                    *
1059 *                   address of thread specific top of native list                          *
1060 *                   old value of thread specific head                                      *
1061 *                   padding for stackframesize 16*n+8)                                     *
1062 *                   return address                                                         *
1063 *                                                                                          *
1064 *                                        ....                                              *
1065 * This thing is less efficient than the original #define (callerside)                      *
1066 * destroyes REG_ITMP2, keeps REG_ITMP1                                                     *
1067 ********************************************************************************************/
1068
1069
1070 asm_prepare_native_stackinfo:
1071         sub $16,%rsp  /*space for the 2 new pointers*/
1072         mov 16(%rsp),itmp2
1073         mov itmp2,(%rsp)
1074         push itmp1
1075         call builtin_asm_get_stackframeinfo
1076
1077         mov itmp1, 32(%rsp)
1078         mov (itmp1),itmp2
1079         mov itmp2,24(%rsp)
1080         mov %rsp,itmp2
1081         add $24,itmp2
1082         mov itmp2,(itmp1)
1083         pop itmp1
1084         ret
1085
1086                 
1087
1088 /************************ function asm_remove _native_stackinfo *******************************************
1089 *                                                                                                         *
1090 *    removes a stackfame for the begin of a native function (either builtin or not)                       *
1091 *    expected stack at begin of function                                                                  *
1092 *                   address of the jit call which invokes the native                                      *
1093 *                   begin address of stack frame of the java method                                       *
1094 *                   method pointer or 0 (for built ins)                                                   *
1095 *                   address thread specific top of native list                                            *
1096 *                   old value of thread specific head                                                     *
1097 *                   padding                                                                               *
1098 *                   return address                                                                        *
1099 *                                                                                                         *
1100 *    at end of function:                                                                                  *
1101 *                             ....                                                                        *
1102 *                   return adresss of the jit call which invokes the native                               *
1103 *                   padding                                                                               *
1104 *                   return address                                                                        *
1105 *                                                                                                         *
1106 *                                                                                                         *
1107 *                                                                                                         *
1108 * This thing is less efficient than the original #define (callerside), uses ITMP2,uses ITMP3,keeps ITMP1  *
1109 ***********************************************************************************************************/
1110
1111 asm_remove_native_stackinfo:
1112         mov 16(%rsp),itmp2
1113         mov 24(%rsp),itmp3
1114         mov itmp2,(itmp3)
1115         pop itmp3
1116         add $32,%rsp
1117         push itmp3
1118         ret
1119
1120
1121
1122 asm_throw_and_handle_exception:
1123         push xpc /* the pushed XPC is directly below the java frame*/
1124         push $0 
1125         push $0
1126         push $0 /*padding*/
1127         call asm_prepare_native_stackinfo /* be aware of the stack effect and calling convention explained above*/
1128         mov itmp1,%rdi
1129
1130         call new_exception
1131
1132         call asm_remove_native_stackinfo /* be aware of the stack effect and calling convention explained above*/
1133
1134         pop xpc
1135         pop xpc
1136
1137         jmp asm_handle_exception
1138         ret /*should never be reached */
1139
1140
1141 asm_throw_and_handle_hardware_arithmetic_exception:
1142
1143         push xpc
1144         push $0 /* the pushed XPC is directly below the java frame*/
1145         push $0
1146         push $0 /*padding*/
1147         call asm_prepare_native_stackinfo /* be aware of the stack effect and calling convention explained above*/
1148
1149         mov string_java_lang_ArithmeticException_message,%rsi
1150         mov string_java_lang_ArithmeticException,%rdi
1151
1152         call new_exception_message
1153
1154         call asm_remove_native_stackinfo /* be aware of the stack effect and calling convention explained above*/
1155         pop xpc
1156         pop xpc
1157
1158         jmp asm_handle_exception
1159         ret /*should never be reached */
1160
1161
1162 asm_getclassvalues_atomic:
1163 _crit_restart2:
1164 _crit_begin2:
1165         movl    offbaseval(a0),itmp1l
1166         movl    offdiffval(a0),itmp2l
1167         movl    offbaseval(a1),itmp3l
1168 _crit_end2:
1169         movl    itmp1l,offcast_super_baseval(a2)
1170         movl    itmp2l,offcast_super_diffval(a2)
1171         movl    itmp3l,offcast_sub_baseval(a2)
1172         ret
1173
1174         .data
1175                 
1176 asm_criticalsections:
1177 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
1178         .quad   _crit_begin1
1179         .quad   _crit_end1
1180         .quad   _crit_restart1
1181         .quad   _crit_begin2
1182         .quad   _crit_end2
1183         .quad   _crit_restart2
1184 #endif
1185         .quad 0
1186
1187                 
1188 /*
1189  * These are local overrides for various environment variables in Emacs.
1190  * Please do not remove this and leave it at the end of the file, where
1191  * Emacs will automagically detect them.
1192  * ---------------------------------------------------------------------
1193  * Local variables:
1194  * mode: asm
1195  * indent-tabs-mode: t
1196  * c-basic-offset: 4
1197  * tab-width: 4
1198  * End:
1199  */