1 /* src/vm/jit/mips/asmpart.S - Java-C interface functions for mips
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
8 This file is part of CACAO.
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.
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.
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
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Andreas Krall
29 Changes: Christian Thalinger
31 $Id: asmpart.S 3023 2005-07-12 23:49:49Z twisti $
38 #include "vm/jit/mips/offsets.h"
39 #include "vm/jit/mips/asmoffsets.h"
40 #include "vm/jit/mips/md-asm.h"
47 /* exported functions and variables *******************************************/
49 .globl asm_calljavafunction
50 .globl asm_calljavafunction_int
52 .globl asm_calljavafunction2
53 .globl asm_calljavafunction2int
54 .globl asm_calljavafunction2long
55 .globl asm_calljavafunction2float
56 .globl asm_calljavafunction2double
58 .globl asm_call_jit_compiler
59 .globl asm_handle_exception
60 .globl asm_handle_nat_exception
62 .globl asm_wrapper_patcher
64 .globl asm_perform_threadswitch
65 .globl asm_initialize_thread_stack
66 .globl asm_switchstackandcall
67 .globl asm_getclassvalues_atomic
68 .globl asm_criticalsections
70 .globl compare_and_swap
73 /********************* function asm_calljavafunction ***************************
75 * This function calls a Java-method (which possibly needs compilation) *
76 * with up to 4 address parameters. *
78 * This functions calls the JIT-compiler which eventually translates the *
79 * method into machine code. *
81 * A possibly throwed exception will be returned to the caller as function *
82 * return value, so the java method cannot return a fucntion value (this *
83 * function usually calls 'main' and '<clinit>' which do not return a *
87 * javaobject_header *asm_calljavafunction (methodinfo *m, *
88 * void *arg1, void *arg2, void *arg3, void *arg4); *
90 *******************************************************************************/
92 .ent asm_calljavafunction
96 .dword 0 /* catch type all */
97 .dword calljava_xhandler /* handler pc */
98 .dword calljava_xhandler /* end pc */
99 .dword asm_calljavafunction /* start pc */
100 .word 1 /* extable size */
101 .word 0 /* ALIGNMENT PADDING */
102 .dword 0 /* line number table start */
103 .dword 0 /* line number table size */
104 .word 0 /* ALIGNMENT PADDING */
105 .word 0 /* fltsave */
106 .word 0 /* intsave */
109 .word 10*8 /* frame size */
110 .dword 0 /* method pointer (pointer to name) */
112 asm_calljavafunction:
113 asm_calljavafunction_int:
114 aaddiu sp,sp,-10*8 /* allocate stack space */
115 ast ra,0(sp) /* save return address */
120 ast pv,3*8(sp) /* procedure vector (delay slot) */
126 sdc1 fss0,4*8(sp) /* save non JavaABI saved flt registers */
133 move itmp1,a0 /* pass method pointer via tmp1 */
135 move a0,a1 /* pass the remaining parameters */
140 ala mptr,asm_call_jit_compiler/* fake virtual function call (2 instr) */
141 ast mptr,1*8(sp) /* store function address */
142 move mptr,sp /* set method pointer */
146 ald pv,1*8(mptr) /* method call as in Java */
147 jalr pv /* call JIT compiler */
149 aaddiu pv,ra,-22*4 /* recompute procedure vector */
154 ald ra,0(sp) /* restore return address */
155 ald pv,3*8(sp) /* restore procedure vector */
157 ldc1 fss0,4*8(sp) /* restore non JavaABI saved flt regs */
164 aaddiu sp,sp,10*8 /* free stack space */
169 jal builtin_throw_exception
170 move v0,zero /* clear return value for exception */
173 .end asm_calljavafunction
176 .ent asm_calljavafunction2
180 .dword 0 /* catch type all */
181 .dword calljava_xhandler2 /* handler pc */
182 .dword calljava_xhandler2 /* end pc */
183 .dword asm_calljavafunction2 /* start pc */
184 .word 1 /* extable size */
185 .word 0 /* ALIGNMENT PADDING */
186 .dword 0 /* line number table start */
187 .dword 0 /* line number table size */
188 .word 0 /* ALIGNMENT PADDING */
189 .word 0 /* fltsave */
190 .word 1 /* intsave */
193 .word 12*8 /* frame size */
194 .dword 0 /* method pointer (pointer to name) */
196 asm_calljavafunction2:
197 asm_calljavafunction2int:
198 asm_calljavafunction2long:
199 asm_calljavafunction2float:
200 asm_calljavafunction2double:
201 aaddiu sp,sp,-12*8 /* allocate stack space (only 11 needed)*/
202 ast ra,0(sp) /* save return address */
206 ast pv,1*8(sp) /* procedure vector */
213 sdc1 fss0,5*8(sp) /* save non JavaABI saved flt registers */
220 ast a0,4*8(sp) /* save method pointer for compiler */
224 blez s7,calljava_argsloaded
226 ald a0,offjniitem(t0)
227 ldc1 fa0,offjniitem(t0)
229 blez s7,calljava_argsloaded
231 ald a1,offjniitem+sizejniblock*1(t0)
232 ldc1 fa1,offjniitem+sizejniblock*1(t0)
234 blez s7,calljava_argsloaded
236 ald a2,offjniitem+sizejniblock*2(t0)
237 ldc1 fa2,offjniitem+sizejniblock*2(t0)
239 blez s7,calljava_argsloaded
241 ald a3,offjniitem+sizejniblock*3(t0)
242 ldc1 fa3,offjniitem+sizejniblock*3(t0)
244 blez s7,calljava_argsloaded
246 ald a4,offjniitem+sizejniblock*4(t0)
247 ldc1 fa4,offjniitem+sizejniblock*4(t0)
249 blez s7,calljava_argsloaded
251 ald a5,offjniitem+sizejniblock*5(t0)
252 ldc1 fa5,offjniitem+sizejniblock*5(t0)
254 blez s7,calljava_argsloaded
256 ald a6,offjniitem+sizejniblock*6(t0)
257 ldc1 fa6,offjniitem+sizejniblock*6(t0)
259 blez s7,calljava_argsloaded
261 ald a7,offjniitem+sizejniblock*7(t0)
262 ldc1 fa7,offjniitem+sizejniblock*7(t0)
266 move t8,sp /* save stack pointer */
267 blez s7,calljava_nocopy
274 ald t3,offjniitem+sizejniblock*8(t0)
277 ala t0,sizejniblock(t0)
279 bnez t1,calljava_copyloop
282 ald itmp1,4*8(t8) /* pass method pointer via itmp1 */
284 ala mptr,asm_call_jit_compiler/* fake virtual function call (2 instr) */
285 ast mptr,2*8(sp) /* store function address */
286 ala mptr,1*8(sp) /* set method pointer */
290 ald pv,1*8(mptr) /* method call as in Java */
291 jalr pv /* call JIT compiler */
293 aaddiu pv,ra,-76*4 /* recompute procedure vector */
298 ald ra,0(sp) /* restore return address */
299 ald pv,8(sp) /* restore procedure vector */
302 ldc1 fss0,5*8(sp) /* restore non JavaABI saved flt regs */
309 aaddiu sp,sp,12*8 /* free stack space */
316 jal builtin_throw_exception
319 .end asm_calljavafunction2
322 /****************** function asm_call_jit_compiler *****************************
324 * invokes the compiler for untranslated JavaVM methods. *
326 * Register REG_ITEMP1 contains a pointer to the method info structure *
327 * (prepared by createcompilerstub). Using the return address in R31 and the *
328 * offset in the LDA instruction or using the value in methodptr R25 the *
329 * patching address for storing the method address can be computed: *
331 * method address was either loaded using *
332 * M_ALD (REG_PV, REG_PV, a) ; invokestatic/special ($28) *
333 * M_JSR (REG_RA, REG_PV); *
335 * M_LDA (REG_PV, REG_RA, val) *
337 * M_ALD (REG_PV, REG_METHODPTR, m) ; invokevirtual/interface ($25) *
338 * M_JSR (REG_RA, REG_PV); *
340 * in the static case the method pointer can be computed using the *
341 * return address and the lda function following the jmp instruction *
343 *******************************************************************************/
346 .ent asm_call_jit_compiler
348 asm_call_jit_compiler:
349 lw t0,-12(ra) /* load instruction LD PV,xxx($y) */
350 srl t0,t0,21 /* shift right register number $y */
351 and t0,t0,31 /* isolate register number */
352 addiu t0,t0,-mptrreg /* test for REG_METHODPTR */
355 lw t0,0(ra) /* load instruction LDA PV,xxx(RA) */
357 sra t0,t0,16 /* isolate offset */
358 aaddu mptr,t0,ra /* compute update address */
361 aaddiu sp,sp,-18*8 /* allocate stack space */
363 SAVE_ARGUMENT_REGISTERS(0)
365 sd mptr,16*8(sp) /* save method pointer */
366 sd ra,17*8(sp) /* save return address */
368 move a0,itmp1 /* pass 'methodinfo' pointer to */
369 jal jit_compile /* jit compiler */
371 RESTORE_ARGUMENT_REGISTERS(0)
373 ld mptr,16*8(sp) /* restore method pointer */
374 ld ra,17*8(sp) /* restore return address */
375 aaddiu sp,sp,18*8 /* deallocate stack area */
377 beqz v0,L_asm_call_jit_compiler_exception
379 lw t0,-12(ra) /* load instruction LDQ PV,xxx($yy) */
381 sra t0,t0,16 /* isolate offset */
383 aaddu t0,t0,mptr /* compute update address via method pointer*/
384 ast v0,0(t0) /* save new method address there */
386 move pv,v0 /* move method address into pv */
388 jr pv /* and call method. The method returns */
389 /* directly to the caller (ra). */
391 L_asm_call_jit_compiler_exception:
393 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
396 jal builtin_asm_get_exceptionptrptr
402 ald xptr,0(v0) /* get the exception pointer */
403 ast zero,0(v0) /* clear the exception pointer */
409 move a0,zero /* fill in the correct stacktrace */
410 aaddiu a1,sp,2*8 /* pass sp of parent Java function */
411 move a2,ra /* pass ra to parent Java function */
412 move a3,a2 /* xpc is the same as ra */
413 jal stacktrace_extern_fillInStackTrace
419 aaddiu xpc,ra,-4 /* faulting address is return adress - 4 */
420 b asm_handle_nat_exception
422 .end asm_call_jit_compiler
425 /********************* function asm_handle_exception ***************************
427 * This function handles an exception. It does not use the usual calling *
428 * conventions. The exception pointer is passed in REG_ITMP1 and the *
429 * pc from the exception raising position is passed in REG_ITMP2. It searches *
430 * the local exception table for a handler. If no one is found, it unwinds *
431 * stacks and continues searching the callers. *
433 * void asm_handle_exception (exceptionptr, exceptionpc); *
435 *******************************************************************************/
437 .ent asm_handle_nat_exception
439 asm_handle_nat_exception:
440 lw t0,0(ra) /* load instruction LDA PV,xxx(RA) */
442 sra t0,t0,16 /* isolate offset */
443 aaddu pv,t0,ra /* compute update address */
445 .aent asm_handle_exception
447 asm_handle_exception:
448 aaddiu sp,sp,-14*8 /* allocate stack */
450 sd v0,0*8(sp) /* save possible used registers */
451 sd t0,1*8(sp) /* also registers used by trace_exception */
465 addu t3,zero,1 /* set no unwind flag */
467 aaddiu sp,sp,-6*8 /* allocate stack */
468 sd xptr,0*8(sp) /* save used registers */
475 ald a1,MethodPointer(pv)
480 jal builtin_trace_exception /* trace_exception(xptr,methodptr) */
482 ld xptr,0*8(sp) /* restore used register */
487 aaddiu sp,sp,6*8 /* deallocate stack */
489 lw t0,ExTableSize(pv) /* t0 = exception table size */
490 beqz t0,empty_table /* if empty table skip */
491 aaddiu t1,pv,ExTableStart /* t1 = start of exception table */
494 ald t2,ExStartPC(t1) /* t2 = exception start pc */
495 sle t2,t2,xpc /* t2 = (startpc <= xpc) */
496 beqz t2,ex_table_cont /* if (false) continue */
497 ald t2,ExEndPC(t1) /* t2 = exception end pc */
498 slt t2,xpc,t2 /* t2 = (xpc < endpc) */
499 beqz t2,ex_table_cont /* if (false) continue */
500 ald a1,ExCatchType(t1) /* arg1 = exception catch type */
501 beqz a1,ex_handle_it /* NULL catches everything */
503 lw itmp3,offclassloaded(a1)
504 bnez itmp3,L_class_loaded
506 aaddiu sp,sp,-8*8 /* allocate stack */
507 sd t0,0*8(sp) /* save used register */
517 jal load_class_bootstrap
519 ld t0,0*8(sp) /* restore used register */
527 aaddiu sp,sp,8*8 /* deallocate stack */
530 lw itmp3,offclasslinked(a1)
531 aaddiu sp,sp,-8*8 /* allocate stack */
533 bnez itmp3,L_class_linked
535 sd t0,0*8(sp) /* save used register */
546 ld t0,0*8(sp) /* restore used register */
558 ald a0,offobjvftbl(xptr) /* a0 = vftblptr(xptr) */
559 ald a1,offclassvftbl(a1) /* a1 = vftblptr(catchtype) class (not obj) */
560 lw a0,offbaseval(a0) /* a0 = baseval(xptr) */
561 lw v0,offbaseval(a1) /* a2 = baseval(catchtype) */
562 lw a1,offdiffval(a1) /* a1 = diffval(catchtype) */
564 subu a0,a0,v0 /* a0 = baseval(xptr) - baseval(catchtype) */
565 sltu v0,a1,a0 /* v0 = xptr is instanceof catchtype */
566 aaddiu sp,sp,8*8 /* deallocate stack */
567 bnez v0,ex_table_cont /* if (false) continue */
570 ald xpc,ExHandlerPC(t1) /* xpc = exception handler pc */
572 beqz t3,ex_jump /* if (!(no stack unwinding) skip */
574 ld v0,0*8(sp) /* restore possible used registers */
575 ld t0,1*8(sp) /* also registers used by trace_exception */
589 aaddiu sp,sp,14*8 /* deallocate stack */
592 jr xpc /* jump to the handler */
595 aaddiu t1,t1,ExEntrySize /* next exception table entry */
596 addiu t0,t0,-1 /* decrement entry counter */
597 bgtz t0,ex_table_loop /* if (t0 > 0) next entry */
600 beqz t3,ex_already_cleared /* if here the first time, then */
601 aaddiu sp,sp,14*8 /* deallocate stack and */
602 move t3,zero /* clear the no unwind flag */
604 lw t0,IsSync(pv) /* t0 = SyncOffset */
605 beqz t0,no_monitor_exit /* if zero no monitorexit */
607 #if defined(USE_THREADS)
608 aaddu t0,sp,t0 /* add stackptr to Offset */
609 ald a0,-8(t0) /* load monitorexit pointer */
611 aaddiu sp,sp,-8*8 /* allocate stack */
612 sd t0,0*8(sp) /* save used register */
620 jal builtin_monitorexit /* builtin_monitorexit(objectptr) */
622 ld t0,0*8(sp) /* restore used register */
629 aaddiu sp,sp,8*8 /* deallocate stack */
633 lw t0,FrameSize(pv) /* t0 = frame size */
634 aaddu sp,sp,t0 /* unwind stack */
635 move t0,sp /* t0 = pointer to save area */
636 lw t1,IsLeaf(pv) /* t1 = is leaf procedure */
637 bnez t1,ex_no_restore /* if (leaf) skip */
638 ld ra,-8(t0) /* restore ra */
639 aaddiu t0,t0,-8 /* t0-- */
641 move xpc,ra /* the new xpc is ra */
642 lw t1,IntSave(pv) /* t1 = saved int register count */
643 ala t2,ex_int2 /* t2 = current pc */
644 sll t1,t1,2 /* t1 = register count * 4 */
645 asubu t2,t2,t1 /* t2 = ex_int_sav - 4 * register count */
646 jr t2 /* jump to save position */
656 sll t1,t1,1 /* t1 = register count * 4 * 2 */
657 asubu t0,t0,t1 /* t0 = t0 - 8 * register count */
659 lw t1,FltSave(pv) /* t1 = saved flt register count */
660 ala t2,ex_flt2 /* t2 = current pc */
661 sll t1,t1,2 /* t1 = register count * 4 */
662 asubu t2,t2,t1 /* t2 = ex_int_sav - 4 * register count */
663 jr t2 /* jump to save position */
669 lw t0,0(ra) /* load instruction LDA PV,xxx(RA) */
671 sra t0,t0,16 /* isolate offset */
672 aaddu pv,t0,ra /* compute update address */
675 .end asm_handle_nat_exception
678 /* asm_wrapper_patcher *********************************************************
683 32 return address into JIT code (patch position)
684 24 pointer to virtual java_objectheader
685 16 machine code (which is patched back later)
686 8 unresolved class/method/field reference
687 0 patcher function pointer to call
689 *******************************************************************************/
691 .ent asm_wrapper_patcher
694 aaddiu sp,sp,-((16+21+4)*8+sizestackframeinfo) /* create stack frame */
696 SAVE_ARGUMENT_REGISTERS(0) /* save 8 int/8 float argument registers */
697 SAVE_TEMPORARY_REGISTERS(16) /* save 5 int/16 float temporary registers */
699 ast itmp1,(16+21+0)*8(sp) /* save itmp1 */
700 ast itmp2,(16+21+1)*8(sp) /* save itmp2 */
701 ast ra,(16+21+2)*8(sp) /* save method return address (for leafs) */
702 ast pv,(16+21+3)*8(sp) /* save pv of calling java function */
705 aaddiu a0,sp,(16+21+4)*8 /* create stackframe info */
706 move a1,pv /* pass java pv */
707 aaddiu a2,sp,((5+16+21+4)*8+sizestackframeinfo) /* pass java sp */
708 move a3,ra /* this is correct for leafs */
709 ald a4,((4+16+21+4)*8+sizestackframeinfo)(sp) /* pass xpc */
710 jal stacktrace_create_extern_stackframeinfo
713 aaddiu a0,sp,((0+16+21+4)*8+sizestackframeinfo) /* pass sp */
714 ald itmp3,((0+16+21+4)*8+sizestackframeinfo)(sp) /* get function pointer */
715 ald itmp1,(16+21+3)*8(sp) /* save pv to the position of fp */
716 ast itmp1,((0+16+21+4)*8+sizestackframeinfo)(sp)
718 ast v0,((0+16+21+4)*8+sizestackframeinfo)(sp) /* save return value */
721 aaddiu a0,sp,(16+21+4)*8 /* remove stackframe info */
722 jal stacktrace_remove_stackframeinfo
725 RESTORE_ARGUMENT_REGISTERS(0) /* restore 8 int/8 float argument registers */
726 RESTORE_TEMPORARY_REGISTERS(16) /* restore 5 int/16 float temporary reg. */
728 ald itmp1,(16+21+0)*8(sp) /* restore itmp1 */
729 ald itmp2,(16+21+1)*8(sp) /* restore itmp2 */
730 ald ra,(16+21+2)*8(sp) /* restore method return address (for leafs)*/
731 ald pv,(16+21+3)*8(sp) /* restore pv of calling java function */
733 ald v0,((0+16+21+4)*8+sizestackframeinfo)(sp) /* restore return value */
735 ald itmp3,((4+16+21+4)*8+sizestackframeinfo)(sp) /* get ra to jit code*/
736 aaddiu sp,sp,((5+16+21+4)*8+sizestackframeinfo) /* remove stack frame */
738 beqz v0,L_asm_wrapper_patcher_exception
740 jr itmp3 /* jump to new patched code */
742 L_asm_wrapper_patcher_exception:
743 move xpc,itmp3 /* return address into JIT code is xpc */
745 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
750 jal builtin_asm_get_exceptionptrptr
758 ld xptr,0(v0) /* get the exception pointer */
759 sd zero,0(v0) /* clear the exception pointer */
760 b asm_handle_exception
762 .end asm_wrapper_patcher
765 /******************* function asm_initialize_thread_stack **********************
767 * u1* asm_initialize_thread_stack (void *func, u1 *stack); *
769 * initialize a thread stack *
771 *******************************************************************************/
773 .ent asm_initialize_thread_stack
775 asm_initialize_thread_stack:
776 aaddiu a1,a1,-14*8 /* allocate save area */
777 sd zero, 0*8(a1) /* s0 initalize thread area */
778 sd zero, 1*8(a1) /* s1 */
779 sd zero, 2*8(a1) /* s2 */
780 sd zero, 3*8(a1) /* s3 */
781 sd zero, 4*8(a1) /* s4 */
782 sd zero, 5*8(a1) /* s5 */
783 sd zero, 6*8(a1) /* s6 */
784 sd zero, 7*8(a1) /* s7 */
785 sd zero, 8*8(a1) /* s8 */
786 sd zero, 9*8(a1) /* fs0 */
787 sd zero,10*8(a1) /* fs1 */
788 sd zero,11*8(a1) /* fs2 */
789 sd zero,12*8(a1) /* fs3 */
794 .end asm_initialize_thread_stack
797 /******************* function asm_perform_threadswitch *************************
799 * void asm_perform_threadswitch (u1 **from, u1 **to, u1 **stackTop); *
801 * performs a threadswitch *
803 *******************************************************************************/
805 .ent asm_perform_threadswitch
807 asm_perform_threadswitch:
808 aaddiu sp,sp,-14*8 /* allocate new stack */
809 sd s0, 0*8(sp) /* save saved registers of old thread */
823 ast sp,0(a0) /* save old stack pointer */
824 ast sp,0(a2) /* stackTop = old stack pointer */
825 ald sp,0(a1) /* load new stack pointer */
826 ld s0, 0*8(sp) /* load saved registers of new thread */
840 aaddiu sp,sp,14*8 /* deallocate new stack */
844 .end asm_perform_threadswitch
847 /********************* function asm_switchstackandcall *************************
849 * void asm_switchstackandcall (void *stack, void *func, void **stacktopsave); *
851 * Switches to a new stack, calls a function and switches back. *
852 * a0 new stack pointer *
853 * a1 function pointer *
854 * a2 pointer to variable where stack top should be stored *
856 *******************************************************************************/
858 .ent asm_switchstackandcall
860 asm_switchstackandcall:
861 aaddiu a0,a0,-16 /* allocate new stack */
862 sd ra,0(a0) /* save return address on new stack */
863 sd sp,8(a0) /* save old stack pointer on new stack */
864 sd sp,0(a2) /* save old stack pointer to variable */
865 move sp,a0 /* switch to new stack */
869 jalr itmp3 /* and call function */
871 ld ra,0(sp) /* load return address */
872 ld sp,8(sp) /* switch to old stack */
876 .end asm_switchstackandcall
879 .ent asm_getclassvalues_atomic
881 asm_getclassvalues_atomic:
888 sw t0,offcast_super_baseval(a2)
889 sw t1,offcast_super_diffval(a2)
890 sw t2,offcast_sub_baseval(a2)
893 .end asm_getclassvalues_atomic
897 asm_criticalsections:
898 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
901 .dword _crit_restart1
904 .dword _crit_restart2
911 .ent compare_and_swap
924 .end compare_and_swap
928 * These are local overrides for various environment variables in Emacs.
929 * Please do not remove this and leave it at the end of the file, where
930 * Emacs will automagically detect them.
931 * ---------------------------------------------------------------------
934 * indent-tabs-mode: t