1 /* src/vm/jit/mips/asmpart.S - Java-C interface functions for MIPS
3 Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
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., 51 Franklin Street, Fifth Floor, Boston, MA
25 Contact: cacao@cacaojvm.org
27 Authors: Andreas Krall
29 Changes: Christian Thalinger
31 $Id: asmpart.S 4640 2006-03-16 17:24:18Z twisti $
38 #include "vm/jit/mips/md-abi.h"
39 #include "vm/jit/mips/md-asm.h"
40 #include "vm/jit/mips/offsets.h"
42 #include "vm/jit/abi.h"
43 #include "vm/jit/methodheader.h"
50 /* export functions ***********************************************************/
52 .globl asm_vm_call_method
53 .globl asm_vm_call_method_int
54 .globl asm_vm_call_method_long
55 .globl asm_vm_call_method_float
56 .globl asm_vm_call_method_double
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_vm_call_method
96 .dword 0 /* catch type all */
97 .dword calljava_xhandler2 /* handler pc */
98 .dword calljava_xhandler2 /* end pc */
99 .dword asm_vm_call_method /* start pc */
100 .word 1 /* extable size */
101 .word 0 /* 4-byte ALIGNMENT PADDING */
102 .dword 0 /* line number table start */
103 .dword 0 /* line number table size */
104 .word 0 /* 4-byte ALIGNMENT PADDING */
105 .word 0 /* fltsave */
106 .word 0 /* intsave */
109 .word 0 /* frame size */
110 .dword 0 /* method pointer (pointer to name) */
113 asm_vm_call_method_int:
114 asm_vm_call_method_long:
115 asm_vm_call_method_float:
116 asm_vm_call_method_double:
117 .set noreorder /* XXX we need to recompute pv */
119 aaddiu sp,sp,-12*8 /* allocate stack space (only 11 needed)*/
120 ast ra,0(sp) /* save return address */
122 bal L_asm_vm_call_method_compute_pv
123 ast pv,1*8(sp) /* procedure vector */
124 L_asm_vm_call_method_compute_pv:
128 sdc1 fss0,5*8(sp) /* save non JavaABI saved flt registers */
135 ast a0,4*8(sp) /* save method pointer for compiler */
139 blez s7,calljava_argsloaded
142 ald a0,offvmargdata(t0)
143 ldc1 fa0,offvmargdata(t0)
145 blez s7,calljava_argsloaded
148 ald a1,offvmargdata+sizevmarg*1(t0)
149 ldc1 fa1,offvmargdata+sizevmarg*1(t0)
151 blez s7,calljava_argsloaded
154 ald a2,offvmargdata+sizevmarg*2(t0)
155 ldc1 fa2,offvmargdata+sizevmarg*2(t0)
157 blez s7,calljava_argsloaded
160 ald a3,offvmargdata+sizevmarg*3(t0)
161 ldc1 fa3,offvmargdata+sizevmarg*3(t0)
163 blez s7,calljava_argsloaded
166 ald a4,offvmargdata+sizevmarg*4(t0)
167 ldc1 fa4,offvmargdata+sizevmarg*4(t0)
169 blez s7,calljava_argsloaded
172 ald a5,offvmargdata+sizevmarg*5(t0)
173 ldc1 fa5,offvmargdata+sizevmarg*5(t0)
175 blez s7,calljava_argsloaded
178 ald a6,offvmargdata+sizevmarg*6(t0)
179 ldc1 fa6,offvmargdata+sizevmarg*6(t0)
181 blez s7,calljava_argsloaded
184 ald a7,offvmargdata+sizevmarg*7(t0)
185 ldc1 fa7,offvmargdata+sizevmarg*7(t0)
189 move t8,sp /* save stack pointer */
190 blez s7,calljava_nocopy
198 ald t3,offvmargdata+sizevmarg*8(t0)
203 bnez t1,calljava_copyloop
207 ald itmp1,4*8(t8) /* pass method pointer via itmp1 */
209 ala mptr,asm_call_jit_compiler/* fake virtual function call (2 instr) */
210 ast mptr,2*8(t8) /* store function address */
211 ala mptr,1*8(t8) /* set method pointer */
213 ald pv,1*8(mptr) /* method call as in Java */
214 jalr pv /* call JIT compiler */
216 L_asm_vm_call_method_recompute_pv:
217 /* aaddiu pv,ra,(asm_vm_call_method - L_asm_vm_call_method_recompute_pv)*/
218 aaddiu pv,ra,-76*4 /* recompute procedure vector */
220 .set reorder /* XXX we need to recompute pv */
223 ald ra,0(sp) /* restore return address */
224 ald pv,8(sp) /* restore procedure vector */
227 ldc1 fss0,5*8(sp) /* restore non JavaABI saved flt regs */
234 aaddiu sp,sp,12*8 /* free stack space */
241 jal builtin_throw_exception
244 .end asm_vm_call_method
247 /****************** function asm_call_jit_compiler *****************************
249 * invokes the compiler for untranslated JavaVM methods. *
251 * Register REG_ITEMP1 contains a pointer to the method info structure *
252 * (prepared by createcompilerstub). Using the return address in R31 and the *
253 * offset in the LDA instruction or using the value in methodptr R25 the *
254 * patching address for storing the method address can be computed: *
256 * method address was either loaded using *
257 * M_ALD (REG_PV, REG_PV, a) ; invokestatic/special ($28) *
258 * M_JSR (REG_RA, REG_PV); *
260 * M_LDA (REG_PV, REG_RA, val) *
262 * M_ALD (REG_PV, REG_METHODPTR, m) ; invokevirtual/interface ($25) *
263 * M_JSR (REG_RA, REG_PV); *
265 * in the static case the method pointer can be computed using the *
266 * return address and the lda function following the jmp instruction *
268 *******************************************************************************/
271 .ent asm_call_jit_compiler
273 asm_call_jit_compiler:
274 aaddiu sp,sp,-(20*8+sizestackframeinfo) /* allocate stack space */
276 SAVE_ARGUMENT_REGISTERS(0)
278 ast mptr,16*8(sp) /* save method pointer */
279 ast ra,17*8(sp) /* save return address */
280 ast itmp1,18*8(sp) /* save methodinfo pointer */
282 aaddiu a0,sp,20*8 /* create stackframe info */
283 move a1,zero /* we don't have pv handy */
284 aaddiu a2,sp,(20*8+sizestackframeinfo) /* pass java sp */
285 ald a3,17*8(sp) /* pass java ra */
286 move a4,a3 /* xpc is equal to ra */
287 jal stacktrace_create_extern_stackframeinfo
289 ald a0,18*8(sp) /* pass methodinfo pointer */
290 jal jit_compile /* jit compiler */
291 ast v0,18*8(sp) /* save return value */
293 aaddiu a0,sp,20*8 /* remove stackframe info */
294 jal stacktrace_remove_stackframeinfo
296 ald a0,17*8(sp) /* pass return address */
297 aaddiu a1,sp,20*8 /* pass stackframeinfo (for PV) */
298 ald a2,16*8(sp) /* pass method pointer */
299 jal md_assembler_get_patch_address /* get address of patch position */
300 move t0,v0 /* move offset to t0 for later use */
302 RESTORE_ARGUMENT_REGISTERS(0)
304 ald ra,17*8(sp) /* restore return address */
305 ald v0,18*8(sp) /* restore return value */
306 aaddiu sp,sp,20*8+sizestackframeinfo /* deallocate stack area */
308 beqz v0,L_asm_call_jit_compiler_exception
310 ast v0,0(t0) /* store new method address */
311 move pv,v0 /* move method address into pv */
312 jr pv /* and call method. The method returns */
313 /* directly to the caller (ra). */
315 L_asm_call_jit_compiler_exception:
316 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
319 jal builtin_asm_get_exceptionptrptr
325 ald xptr,0(v0) /* get the exception pointer */
326 ast zero,0(v0) /* clear the exception pointer */
328 aaddiu xpc,ra,-4 /* faulting address is return adress - 4 */
329 b asm_handle_nat_exception
331 .end asm_call_jit_compiler
334 /* asm_handle_exception ********************************************************
336 This function handles an exception. It does not use the usual calling
337 conventions. The exception pointer is passed in REG_ITMP1 and the
338 pc from the exception raising position is passed in REG_ITMP2. It searches
339 the local exception table for a handler. If no one is found, it unwinds
340 stacks and continues searching the callers.
342 *******************************************************************************/
344 .ent asm_handle_nat_exception
346 asm_handle_nat_exception:
347 L_asm_handle_exception_stack_loop:
348 aaddiu sp,sp,-6*8 /* allocate stack */
349 ast xptr,0*8(sp) /* save exception pointer */
350 ast xpc,1*8(sp) /* save exception pc */
351 ast ra,3*8(sp) /* save return address */
352 ast zero,4*8(sp) /* save maybe-leaf flag (cleared) */
354 move a0,ra /* pass return address */
355 jal md_codegen_findmethod /* get PV from RA */
356 ast v0,2*8(sp) /* save data segment pointer */
358 ald a0,0*8(sp) /* pass exception pointer */
359 ald a1,1*8(sp) /* pass exception pc */
360 move a2,v0 /* pass data segment pointer */
361 aaddiu a3,sp,6*8 /* pass Java stack pointer */
363 b L_asm_handle_exception_continue
365 .aent asm_handle_exception
367 asm_handle_exception:
368 aaddiu sp,sp,-(ARG_CNT+TMP_CNT)*8 /* create maybe-leaf stackframe */
370 SAVE_ARGUMENT_REGISTERS(0) /* we save arg and temp registers in */
371 SAVE_TEMPORARY_REGISTERS(ARG_CNT) /* case this is a leaf method */
373 aaddiu sp,sp,-6*8 /* allocate stack */
374 ast xptr,0*8(sp) /* save exception pointer */
375 ast xpc,1*8(sp) /* save exception pc */
376 ast pv,2*8(sp) /* save data segment pointer */
377 ast ra,3*8(sp) /* save return address */
378 addu t0,zero,1 /* set maybe-leaf flag */
379 ast t0,4*8(sp) /* save maybe-leaf flag */
381 move a0,xptr /* pass exception pointer */
382 move a1,xpc /* pass exception pc */
383 move a2,pv /* pass data segment pointer */
384 aaddiu a3,sp,(ARG_CNT+TMP_CNT+6)*8 /* pass Java stack pointer */
386 L_asm_handle_exception_continue:
387 jal exceptions_handle_exception
389 beqz v0,L_asm_handle_exception_not_catched
391 move xpc,v0 /* move handlerpc into xpc */
392 ald xptr,0*8(sp) /* restore exception pointer */
393 ald pv,2*8(sp) /* restore data segment pointer */
394 ald ra,3*8(sp) /* restore return address */
395 ald t0,4*8(sp) /* get maybe-leaf flag */
396 aaddiu sp,sp,6*8 /* free stackframe */
398 beqz t0,L_asm_handle_exception_no_leaf
400 RESTORE_ARGUMENT_REGISTERS(0) /* if this is a leaf method, we have */
401 RESTORE_TEMPORARY_REGISTERS(ARG_CNT)/* to restore arg and temp registers */
403 aaddiu sp,sp,(ARG_CNT+TMP_CNT)*8 /* remove maybe-leaf stackframe */
405 L_asm_handle_exception_no_leaf:
406 jr xpc /* jump to the handler */
408 L_asm_handle_exception_not_catched:
409 ald xptr,0*8(sp) /* restore exception pointer */
410 ald pv,2*8(sp) /* restore data segment pointer */
411 ald ra,3*8(sp) /* restore return address */
412 ald t0,4*8(sp) /* get maybe-leaf flag */
413 aaddiu sp,sp,6*8 /* free stackframe */
415 beqz t0,L_asm_handle_exception_no_leaf_stack
417 aaddiu sp,sp,(ARG_CNT+TMP_CNT)*8 /* remove maybe-leaf stackframe */
418 move t0,zero /* clear the maybe-leaf flag */
420 L_asm_handle_exception_no_leaf_stack:
421 lw t1,FrameSize(pv) /* get frame size */
422 aaddu t1,sp,t1 /* pointer to save area */
424 lw t2,IsLeaf(pv) /* is leaf procedure */
425 bnez t2,L_asm_handle_exception_no_ra_restore
427 ald ra,-1*8(t1) /* restore ra */
428 aaddiu t1,t1,-8 /* t1-- */
430 L_asm_handle_exception_no_ra_restore:
431 move xpc,ra /* the new xpc is ra */
432 lw t2,IntSave(pv) /* t1 = saved int register count */
433 ala t3,ex_int2 /* t3 = current pc */
434 sll t2,t2,2 /* t2 = register count * 4 */
435 asubu t3,t3,t2 /* t3 = IntSave - 4 * register count */
436 jr t3 /* jump to save position */
447 sll t2,t2,1 /* t2 = register count * 4 * 2 */
448 asubu t1,t1,t2 /* t1 = t0 - 8 * register count */
450 lw t2,FltSave(pv) /* t2 = saved flt register count */
451 ala t3,ex_flt2 /* t3 = current pc */
452 sll t2,t2,2 /* t2 = register count * 4 */
453 asubu t3,t3,t2 /* t3 = ex_int_sav - 4 * register count */
454 jr t3 /* jump to save position */
462 lw t1,FrameSize(pv) /* get frame size */
463 aaddu sp,sp,t1 /* unwind stack */
464 b L_asm_handle_exception_stack_loop
466 .end asm_handle_nat_exception
469 /* asm_wrapper_patcher *********************************************************
474 40 return address into JIT code (patch position)
475 32 pointer to virtual java_objectheader
476 24 machine code (which is patched back later)
477 16 unresolved class/method/field reference
478 8 data segment displacement from load instructions
479 0 patcher function pointer to call
481 *******************************************************************************/
483 .ent asm_wrapper_patcher
486 aaddiu sp,sp,-((2+16+22+4)*8+sizestackframeinfo) /* create stack frame */
488 SAVE_RETURN_REGISTERS(0) /* save 1 int/1 float return registers */
489 SAVE_ARGUMENT_REGISTERS(2) /* save 8 int/8 float argument registers */
490 SAVE_TEMPORARY_REGISTERS(18) /* save 5 int/16 float temporary registers */
492 ast itmp1,(2+16+22+0)*8(sp) /* save itmp1 */
493 ast itmp2,(2+16+22+1)*8(sp) /* save itmp2 */
494 ast ra,(2+16+22+2)*8(sp) /* save method return address (for leafs) */
495 ast pv,(2+16+22+3)*8(sp) /* save pv of calling java function */
497 aaddiu a0,sp,(2+16+22+4)*8 /* create stackframe info */
498 move a1,pv /* pass java pv */
499 aaddiu a2,sp,((6+2+16+22+4)*8+sizestackframeinfo) /* pass java sp */
500 move a3,ra /* this is correct for leafs */
501 ald a4,((5+2+16+22+4)*8+sizestackframeinfo)(sp) /* pass xpc */
502 jal stacktrace_create_extern_stackframeinfo
504 aaddiu a0,sp,((0+2+16+22+4)*8+sizestackframeinfo) /* pass sp */
505 ald itmp3,((0+2+16+22+4)*8+sizestackframeinfo)(sp) /* get function */
506 ald itmp1,(2+16+22+3)*8(sp) /* save pv to the position of fp */
507 ast itmp1,((0+2+16+22+4)*8+sizestackframeinfo)(sp)
509 ast v0,((0+2+16+22+4)*8+sizestackframeinfo)(sp) /* save return value */
511 aaddiu a0,sp,(2+16+22+4)*8 /* remove stackframe info */
512 jal stacktrace_remove_stackframeinfo
514 RESTORE_RETURN_REGISTERS(0) /* restore 1 int/1 float return registers */
515 RESTORE_ARGUMENT_REGISTERS(2) /* restore 8 int/8 float argument registers */
516 RESTORE_TEMPORARY_REGISTERS(18) /* restore 5 int/16 float temporary reg. */
518 ald itmp1,(2+16+22+0)*8(sp) /* restore itmp1 */
519 ald itmp2,(2+16+22+1)*8(sp) /* restore itmp2 */
520 ald ra,(2+16+22+2)*8(sp) /* restore method return address (for leafs)*/
521 ald pv,(2+16+22+3)*8(sp) /* restore pv of calling java function */
523 ald itmp3,((0+2+16+22+4)*8+sizestackframeinfo)(sp) /* get return value*/
524 beqz itmp3,L_asm_wrapper_patcher_exception
526 ald itmp3,((5+2+16+22+4)*8+sizestackframeinfo)(sp) /* get RA to JIT */
527 aaddiu sp,sp,((6+2+16+22+4)*8+sizestackframeinfo) /* remove stack frame */
529 jr itmp3 /* jump to new patched code */
531 L_asm_wrapper_patcher_exception:
532 ald xpc,((5+2+16+22+4)*8+sizestackframeinfo)(sp) /* RA to JIT is xpc */
533 aaddiu sp,sp,((6+2+16+22+4)*8+sizestackframeinfo) /* remove stack frame */
535 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
540 jal builtin_asm_get_exceptionptrptr
548 ld xptr,0(v0) /* get the exception pointer */
549 sd zero,0(v0) /* clear the exception pointer */
550 b asm_handle_exception
552 .end asm_wrapper_patcher
555 /******************* function asm_initialize_thread_stack **********************
557 * u1* asm_initialize_thread_stack (void *func, u1 *stack); *
559 * initialize a thread stack *
561 *******************************************************************************/
563 .ent asm_initialize_thread_stack
565 asm_initialize_thread_stack:
566 aaddiu a1,a1,-14*8 /* allocate save area */
567 sd zero, 0*8(a1) /* s0 initalize thread area */
568 sd zero, 1*8(a1) /* s1 */
569 sd zero, 2*8(a1) /* s2 */
570 sd zero, 3*8(a1) /* s3 */
571 sd zero, 4*8(a1) /* s4 */
572 sd zero, 5*8(a1) /* s5 */
573 sd zero, 6*8(a1) /* s6 */
574 sd zero, 7*8(a1) /* s7 */
575 sd zero, 8*8(a1) /* s8 */
576 sd zero, 9*8(a1) /* fs0 */
577 sd zero,10*8(a1) /* fs1 */
578 sd zero,11*8(a1) /* fs2 */
579 sd zero,12*8(a1) /* fs3 */
584 .end asm_initialize_thread_stack
587 /******************* function asm_perform_threadswitch *************************
589 * void asm_perform_threadswitch (u1 **from, u1 **to, u1 **stackTop); *
591 * performs a threadswitch *
593 *******************************************************************************/
595 .ent asm_perform_threadswitch
597 asm_perform_threadswitch:
598 aaddiu sp,sp,-14*8 /* allocate new stack */
599 sd s0, 0*8(sp) /* save saved registers of old thread */
613 ast sp,0(a0) /* save old stack pointer */
614 ast sp,0(a2) /* stackTop = old stack pointer */
615 ald sp,0(a1) /* load new stack pointer */
616 ld s0, 0*8(sp) /* load saved registers of new thread */
630 aaddiu sp,sp,14*8 /* deallocate new stack */
634 .end asm_perform_threadswitch
637 /********************* function asm_switchstackandcall *************************
639 * void asm_switchstackandcall (void *stack, void *func, void **stacktopsave); *
641 * Switches to a new stack, calls a function and switches back. *
642 * a0 new stack pointer *
643 * a1 function pointer *
644 * a2 pointer to variable where stack top should be stored *
646 *******************************************************************************/
648 .ent asm_switchstackandcall
650 asm_switchstackandcall:
651 aaddiu a0,a0,-16 /* allocate new stack */
652 sd ra,0(a0) /* save return address on new stack */
653 sd sp,8(a0) /* save old stack pointer on new stack */
654 sd sp,0(a2) /* save old stack pointer to variable */
655 move sp,a0 /* switch to new stack */
659 jalr itmp3 /* and call function */
661 ld ra,0(sp) /* load return address */
662 ld sp,8(sp) /* switch to old stack */
666 .end asm_switchstackandcall
669 .ent asm_getclassvalues_atomic
671 asm_getclassvalues_atomic:
678 sw t0,offcast_super_baseval(a2)
679 sw t1,offcast_super_diffval(a2)
680 sw t2,offcast_sub_baseval(a2)
683 .end asm_getclassvalues_atomic
687 asm_criticalsections:
688 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
698 .ent compare_and_swap
711 .end compare_and_swap
714 /* Disable exec-stacks, required for Gentoo ***********************************/
716 #if defined(__GCC__) && defined(__ELF__)
717 .section .note.GNU-stack,"",@progbits
722 * These are local overrides for various environment variables in Emacs.
723 * Please do not remove this and leave it at the end of the file, where
724 * Emacs will automagically detect them.
725 * ---------------------------------------------------------------------
728 * indent-tabs-mode: t