1 /* -*- mode: asm; tab-width: 4 -*- */
2 /****************************** asmpart.c **************************************
4 * is an assembly language file, but called .c to fake the preprocessor. *
5 * It contains the Java-C interface functions for Alpha processors. *
7 * Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst *
9 * See file COPYRIGHT for information on usage and disclaimer of warranties *
11 * Authors: Andreas Krall EMAIL: cacao@complang.tuwien.ac.at *
13 * Last Change: 1998/11/23 *
15 *******************************************************************************/
108 #define aaddiu daddiu
115 #define MethodPointer -8
116 #define FrameSize -12
121 #define ExTableSize -32
122 #define ExTableStart -32
124 #define ExEntrySize -32
127 #define ExHandlerPC -24
128 #define ExCatchType -32
135 /********************* exported functions and variables ***********************/
137 .globl has_no_x_instr_set
138 .globl synchronize_caches
139 .globl asm_calljavamethod
140 .globl asm_call_jit_compiler
141 .globl asm_dumpregistersandcall
142 .globl asm_handle_exception
143 .globl asm_handle_nat_exception
144 .globl asm_builtin_checkarraycast
145 .globl asm_builtin_aastore
146 .globl asm_builtin_monitorenter
147 .globl asm_builtin_monitorexit
148 .globl asm_builtin_idiv
149 .globl asm_builtin_irem
150 .globl asm_builtin_ldiv
151 .globl asm_builtin_lrem
152 .globl asm_perform_threadswitch
153 .globl asm_initialize_thread_stack
154 .globl asm_switchstackandcall
157 /*************************** imported functions *******************************/
160 .globl builtin_monitorexit
161 .globl builtin_throw_exception
162 .globl builtin_trace_exception
163 .globl class_java_lang_Object
166 /*********************** function has_no_x_instr_set ***************************
168 * determines if the byte support instruction set (21164a and higher) *
171 *******************************************************************************/
173 .ent has_no_x_instr_set
176 move v0,zero /* result code 0 (not used for MIPS) */
179 .end has_no_x_instr_set
182 /********************* function synchronize_caches ****************************/
184 .ent synchronize_caches
187 /* li a0,BCACHE */ /* flush both caches */
188 /* li v0,SYS_cacheflush */ /* Syscall number for cacheflush() */
189 /* syscall */ /* call cacheflush() */
192 .end synchronize_caches
197 /********************* function asm_calljavamethod *****************************
199 * This function calls a Java-method (which possibly needs compilation) *
200 * with up to 4 address parameters. *
202 * This functions calls the JIT-compiler which eventually translates the *
203 * method into machine code. *
205 * A possibly throwed exception will be returned to the caller as function *
206 * return value, so the java method cannot return a fucntion value (this *
207 * function usually calls 'main' and '<clinit>' which do not return a *
211 * javaobject_header *asm_calljavamethod (methodinfo *m, *
212 * void *arg1, void *arg2, void *arg3, void *arg4); *
214 *******************************************************************************/
216 .ent asm_calljavamethod
220 .ascii "calljavamethod\0\0"
223 .dword 0 /* catch type all */
224 .dword calljava_xhandler /* handler pc */
225 .dword calljava_xhandler /* end pc */
226 .dword asm_calljavamethod /* start pc */
227 .word 1 /* extable size */
228 .word 0 /* fltsave */
229 .word 0 /* intsave */
232 .word 80 /* frame size */
233 .dword 0 /* method pointer (pointer to name) */
237 aaddiu sp,sp,-10*8 /* allocate stack space */
238 sd ra,0(sp) /* save return address */
242 sd pv,3*8(sp) /* procedure vector */
248 sdc1 fss0,4*8(sp) /* save non JavaABI saved flt registers */
254 sd a0,2*8(sp) /* save method pointer for compiler */
255 aaddiu itmp1,sp,16 /* pass pointer to methodptr via itmp1 */
257 move a0,a1 /* pass the remaining parameters */
262 ala mptr,asm_call_jit_compiler/* fake virtual function call (2 instr) */
263 ast mptr,1*8(sp) /* store function address */
264 move mptr,sp /* set method pointer */
268 ald pv,1*8(mptr) /* method call as in Java */
269 jalr pv /* call JIT compiler */
271 aaddiu pv,ra,-23*4 /* recompute procedure vector */
272 move v0,zero /* clear return value (exception ptr) */
278 ld ra,0(sp) /* restore return address */
279 ld pv,3*8(sp) /* restore procedure vector */
281 ldc1 fss0,4*8(sp) /* restore non JavaABI saved flt regs */
287 aaddiu sp,sp,10*8 /* free stack space */
293 jal builtin_throw_exception
296 .end asm_calljavamethod
301 /****************** function asm_call_jit_compiler *****************************
303 * invokes the compiler for untranslated JavaVM methods. *
305 * Register REG_ITEMP1 contains a pointer to the method info structure *
306 * (prepared by createcompilerstub). Using the return address in R31 and the *
307 * offset in the LDA instruction or using the value in methodptr R25 the *
308 * patching address for storing the method address can be computed: *
310 * method address was either loaded using *
311 * M_ALD (REG_PV, REG_PV, a) ; invokestatic/special ($28) *
312 * M_JSR (REG_RA, REG_PV); *
314 * M_LDA (REG_PV, REG_RA, val) *
316 * M_ALD (REG_PV, REG_METHODPTR, m) ; invokevirtual/interface ($25) *
317 * M_JSR (REG_RA, REG_PV); *
319 * in the static case the method pointer can be computed using the *
320 * return address and the lda function following the jmp instruction *
322 *******************************************************************************/
325 .ent asm_call_jit_compiler
326 asm_call_jit_compiler:
328 lw t0,-12(ra) /* load instruction LD PV,xxx($y) */
329 srl t0,t0,21 /* shift right register number $y */
330 and t0,t0,31 /* isolate register number */
331 addiu t0,t0,-mptrreg /* test for REG_METHODPTR */
334 lw t0,0(ra) /* load instruction LDA PV,xxx(RA) */
336 sra t0,t0,16 /* isolate offset */
337 aaddu mptr,t0,ra /* compute update address */
341 aaddiu sp,sp,-18*8 /* allocate stack space */
342 sd a0,0*8(sp) /* save all argument registers */
343 sd a1,1*8(sp) /* they could be used by method */
358 sd mptr,16*8(sp) /* save method pointer */
359 sd ra,17*8(sp) /* save return address */
361 ald a0,0(itmp1) /* pass 'methodinfo' pointer to */
362 jal jit_compile /* jit compiler */
364 ld a0,0*8(sp) /* restore argument registers */
365 ld a1,1*8(sp) /* they could be used by method */
380 ld mptr,16*8(sp) /* restore method pointer */
381 ld ra,17*8(sp) /* restore return address */
382 aaddiu sp,sp,18*8 /* deallocate stack area */
384 lw t0,-12(ra) /* load instruction LDQ PV,xxx($yy) */
386 sra t0,t0,16 /* isolate offset */
388 aaddu t0,t0,mptr /* compute update address via method pointer*/
389 ast v0,0(t0) /* save new method address there */
391 move pv,v0 /* move method address into pv */
393 jr pv /* and call method. The method returns */
394 /* directly to the caller (ra). */
396 .end asm_call_jit_compiler
399 /****************** function asm_dumpregistersandcall **************************
401 * This funtion saves all callee saved (address) registers and calls the *
402 * function which is passed as parameter. *
404 * This function is needed by the garbage collector, which needs to access *
405 * all registers which are stored on the stack. Unused registers are *
406 * cleared to avoid interferances with the GC. *
408 * void asm_dumpregistersandcall (functionptr f); *
410 *******************************************************************************/
412 .ent asm_dumpregistersandcall
413 asm_dumpregistersandcall:
414 aaddiu sp,sp,-10*8 /* allocate stack */
415 sd ra,0(sp) /* save return address */
417 sd s0,1*8(sp) /* save all callee saved registers */
428 jalr itmp3 /* and call function */
430 ld ra,0(sp) /* restore return address */
431 aaddiu sp,sp,10*8 /* deallocate stack */
434 .end asm_dumpregistersandcall
437 /********************* function asm_handle_exception ***************************
439 * This function handles an exception. It does not use the usual calling *
440 * conventions. The exception pointer is passed in REG_ITMP1 and the *
441 * pc from the exception raising position is passed in REG_ITMP2. It searches *
442 * the local exception table for a handler. If no one is found, it unwinds *
443 * stacks and continues searching the callers. *
445 * void asm_handle_exception (exceptionptr, exceptionpc); *
447 *******************************************************************************/
449 .ent asm_handle_nat_exception
450 asm_handle_nat_exception:
452 lw t0,0(ra) /* load instruction LDA PV,xxx(RA) */
454 sra t0,t0,16 /* isolate offset */
455 aaddu pv,t0,ra /* compute update address */
457 .aent asm_handle_exception
458 asm_handle_exception:
460 aaddiu sp,sp,-14*8 /* allocate stack */
461 sd v0,0*8(sp) /* save possible used registers */
462 sd t0,1*8(sp) /* also registers used by trace_exception */
476 addu t3,zero,1 /* set no unwind flag */
478 aaddiu sp,sp,-6*8 /* allocate stack */
479 sd xptr,0*8(sp) /* save used registers */
486 ald a1,MethodPointer(pv)
489 jal builtin_trace_exception /* trace_exception(xptr,methodptr) */
491 ld xptr,0*8(sp) /* restore used register */
496 aaddiu sp,sp,6*8 /* deallocate stack */
498 lw t0,ExTableSize(pv) /* t0 = exception table size */
499 beqz t0,empty_table /* if empty table skip */
500 aaddiu t1,pv,ExTableStart /* t1 = start of exception table */
503 ald t2,ExStartPC(t1) /* t2 = exception start pc */
504 slt t2,xpc,t2 /* t2 = (xpc < startpc) */
505 bnez t2,ex_table_cont /* if (true) continue */
506 ald t2,ExEndPC(t1) /* t2 = exception end pc */
507 slt t2,xpc,t2 /* t2 = (xpc < endpc) */
508 beqz t2,ex_table_cont /* if (false) continue */
509 ald a1,ExCatchType(t1) /* arg1 = exception catch type */
510 beqz a1,ex_handle_it /* NULL catches everything */
512 ald a0,offobjvftbl(xptr) /* a0 = vftblptr(xptr) */
513 ald a1,offobjvftbl(a1) /* a1 = vftblptr(catchtype) class (not obj) */
514 lw a0,offbaseval(a0) /* a0 = baseval(xptr) */
515 lw v0,offbaseval(a1) /* a2 = baseval(catchtype) */
516 lw a1,offdiffval(a1) /* a1 = diffval(catchtype) */
517 subu a0,a0,v0 /* a0 = baseval(xptr) - baseval(catchtype) */
518 sltu v0,a1,a0 /* v0 = xptr is instanceof catchtype */
519 bnez v0,ex_table_cont /* if (false) continue */
523 ald xpc,ExHandlerPC(t1) /* xpc = exception handler pc */
525 beqz t3,ex_jump /* if (!(no stack unwinding) skip */
527 ld v0,0*8(sp) /* restore possible used registers */
528 ld t0,1*8(sp) /* also registers used by trace_exception */
542 aaddiu sp,sp,14*8 /* deallocate stack */
545 jr xpc /* jump to the handler */
548 aaddiu t1,t1,ExEntrySize /* next exception table entry */
549 addiu t0,t0,-1 /* decrement entry counter */
550 bgtz t0,ex_table_loop /* if (t0 > 0) next entry */
553 beqz t3,ex_already_cleared /* if here the first time, then */
554 aaddiu sp,sp,14*8 /* deallocate stack and */
555 move t3,zero /* clear the no unwind flag */
557 lw t0,IsSync(pv) /* t0 = SyncOffset */
558 beqz t0,no_monitor_exit /* if zero no monitorexit */
559 aaddu t0,sp,t0 /* add stackptr to Offset */
560 ald a0,-8(t0) /* load monitorexit pointer */
562 aaddiu sp,sp,-8*8 /* allocate stack */
563 sd t0,0*8(sp) /* save used register */
571 jal builtin_monitorexit /* builtin_monitorexit(objectptr) */
573 ld t0,0*8(sp) /* restore used register */
580 aaddiu sp,sp,8*8 /* deallocate stack */
583 lw t0,FrameSize(pv) /* t0 = frame size */
584 aaddu sp,sp,t0 /* unwind stack */
585 move t0,sp /* t0 = pointer to save area */
586 lw t1,IsLeaf(pv) /* t1 = is leaf procedure */
587 bnez t1,ex_no_restore /* if (leaf) skip */
588 ld ra,-8(t0) /* restore ra */
589 aaddiu t0,t0,-8 /* t0-- */
591 move xpc,ra /* the new xpc is ra */
592 lw t1,IntSave(pv) /* t1 = saved int register count */
593 ala t2,ex_int2 /* t2 = current pc */
594 sll t1,t1,2 /* t1 = register count * 4 */
595 asubu t2,t2,t1 /* t2 = ex_int_sav - 4 * register count */
596 jr t2 /* jump to save position */
606 sll t1,t1,1 /* t1 = register count * 4 * 2 */
607 asubu t0,t0,t1 /* t0 = t0 - 8 * register count */
609 lw t1,FltSave(pv) /* t1 = saved flt register count */
610 ala t2,ex_flt2 /* t2 = current pc */
611 sll t1,t1,2 /* t1 = register count * 4 */
612 asubu t2,t2,t1 /* t2 = ex_int_sav - 4 * register count */
613 jr t2 /* jump to save position */
619 lw t0,0(ra) /* load instruction LDA PV,xxx(RA) */
621 sra t0,t0,16 /* isolate offset */
622 aaddu pv,t0,ra /* compute update address */
625 .end asm_handle_nat_exception
628 /********************* function asm_builtin_monitorenter ***********************
630 * Does null check and calls monitorenter or throws an exception *
632 *******************************************************************************/
634 .ent asm_builtin_monitorenter
635 asm_builtin_monitorenter:
637 beqz a0,nb_monitorenter /* if (null) throw exception */
638 ala v0,builtin_monitorenter /* else call builtin_monitorenter */
642 ald xptr,proto_java_lang_NullPointerException
643 aaddiu xpc,ra,-4 /* faulting address is return adress - 4*/
644 b asm_handle_nat_exception
645 .end asm_builtin_monitorenter
648 /********************* function asm_builtin_monitorexit ************************
650 * Does null check and calls monitorexit or throws an exception *
652 *******************************************************************************/
654 .ent asm_builtin_monitorexit
655 asm_builtin_monitorexit:
657 beqz a0,nb_monitorexit /* if (null) throw exception */
658 ala v0,builtin_monitorexit /* else call builtin_monitorexit */
662 ald xptr,proto_java_lang_NullPointerException
663 aaddiu xpc,ra,-4 /* faulting address is return adress - 4*/
664 b asm_handle_nat_exception
665 .end asm_builtin_monitorexit
668 /************************ function asm_builtin_idiv ****************************
670 * Does null check and calls idiv or throws an exception *
672 *******************************************************************************/
674 .ent asm_builtin_idiv
677 beqz a1,nb_idiv /* if (null) throw exception */
678 ala v0,builtin_idiv /* else call builtin_idiv */
682 ald xptr,proto_java_lang_ArithmeticException
683 aaddiu xpc,ra,-4 /* faulting address is return adress - 4*/
684 b asm_handle_nat_exception
685 .end asm_builtin_idiv
688 /************************ function asm_builtin_ldiv ****************************
690 * Does null check and calls ldiv or throws an exception *
692 *******************************************************************************/
694 .ent asm_builtin_ldiv
697 beqz a1,nb_ldiv /* if (null) throw exception */
698 ala v0,builtin_ldiv /* else call builtin_ldiv */
702 ald xptr,proto_java_lang_ArithmeticException
703 aaddiu xpc,ra,-4 /* faulting address is return adress - 4*/
704 b asm_handle_nat_exception
705 .end asm_builtin_ldiv
708 /************************ function asm_builtin_irem ****************************
710 * Does null check and calls irem or throws an exception *
712 *******************************************************************************/
714 .ent asm_builtin_irem
717 beqz a1,nb_irem /* if (null) throw exception */
718 ala v0,builtin_irem /* else call builtin_irem */
722 ald xptr,proto_java_lang_ArithmeticException
723 aaddiu xpc,ra,-4 /* faulting address is return adress - 4*/
724 b asm_handle_nat_exception
725 .end asm_builtin_irem
728 /************************ function asm_builtin_lrem ****************************
730 * Does null check and calls lrem or throws an exception *
732 *******************************************************************************/
734 .ent asm_builtin_lrem
737 beqz a1,nb_lrem /* if (null) throw exception */
738 ala v0,builtin_lrem /* else call builtin_lrem */
742 ald xptr,proto_java_lang_ArithmeticException
743 aaddiu xpc,ra,-4 /* faulting address is return adress - 4*/
744 b asm_handle_nat_exception
745 .end asm_builtin_lrem
748 /******************* function asm_builtin_checkarraycast ***********************
750 * Does the cast check and eventually throws an exception *
752 *******************************************************************************/
754 .ent asm_builtin_checkarraycast
755 asm_builtin_checkarraycast:
757 aaddiu sp,sp,-16 /* allocate stack space */
758 sd ra,0(sp) /* save return address */
759 sd a0,8(sp) /* save object pointer */
760 jal builtin_checkarraycast /* builtin_checkarraycast */
761 beqz v0,nb_carray_throw /* if (false) throw exception */
762 ld ra,0(sp) /* restore return address */
763 ld v0,8(sp) /* return object pointer */
764 aaddiu sp,sp,16 /* deallocate stack */
768 ald xptr,proto_java_lang_ClassCastException
769 ld ra,0(sp) /* restore return address */
770 aaddiu sp,sp,16 /* free stack space */
771 aaddiu xpc,ra,-4 /* faulting address is return adress - 4*/
772 b asm_handle_nat_exception
773 .end asm_builtin_checkarraycast
776 /******************* function asm_builtin_aastore ******************************
778 * Does the cast check and eventually throws an exception *
779 * a0 = arrayref, a1 = index, a2 = value *
781 *******************************************************************************/
783 .ent asm_builtin_aastore
786 beqz a0,nb_aastore_null /* if null pointer throw exception */
787 lw t0,offarraysize(a0) /* load size */
788 aaddiu sp,sp,-32 /* allocate stack space */
789 sd ra,0(sp) /* save return address */
790 asll t1,a1,ashift /* add index*8 to arrayref */
791 aaddu t1,a0,t1 /* add index * ashift to arrayref */
792 sltu t0,a1,t0 /* do bound check */
793 beqz t0,nb_aastore_bound /* if out of bounds throw exception */
794 move a1,a2 /* object is second argument */
795 sd t1,8(sp) /* save store position */
796 sd a1,16(sp) /* save object */
797 jal builtin_canstore /* builtin_canstore(arrayref,object) */
798 ld ra,0(sp) /* restore return address */
799 ld a0,8(sp) /* restore store position */
800 ld a1,16(sp) /* restore object */
801 aaddiu sp,sp,32 /* free stack space */
802 beqz v0,nb_aastore_throw /* if (false) throw exception */
803 ast a1,offobjarrdata(a0) /* store objectptr in array */
807 ald xptr,proto_java_lang_NullPointerException
808 move xpc,ra /* faulting address is return adress */
809 b asm_handle_nat_exception
812 ald xptr,proto_java_lang_ArrayIndexOutOfBoundsException
813 aaddiu sp,sp,32 /* free stack space */
814 move xpc,ra /* faulting address is return adress */
815 b asm_handle_nat_exception
818 ald xptr,proto_java_lang_ArrayStoreException
819 move xpc,ra /* faulting address is return adress */
820 b asm_handle_nat_exception
822 .end asm_builtin_aastore
825 /******************* function asm_initialize_thread_stack **********************
827 * u1* asm_initialize_thread_stack (void *func, u1 *stack); *
829 * initialize a thread stack *
831 *******************************************************************************/
833 .ent asm_initialize_thread_stack
834 asm_initialize_thread_stack:
836 aaddiu a1,a1,-14*8 /* allocate save area */
837 sd zero, 0*8(a1) /* s0 initalize thread area */
838 sd zero, 1*8(a1) /* s1 */
839 sd zero, 2*8(a1) /* s2 */
840 sd zero, 3*8(a1) /* s3 */
841 sd zero, 4*8(a1) /* s4 */
842 sd zero, 5*8(a1) /* s5 */
843 sd zero, 6*8(a1) /* s6 */
844 sd zero, 7*8(a1) /* s7 */
845 sd zero, 8*8(a1) /* s8 */
846 sd zero, 9*8(a1) /* fs0 */
847 sd zero,10*8(a1) /* fs1 */
848 sd zero,11*8(a1) /* fs2 */
849 sd zero,12*8(a1) /* fs3 */
853 .end asm_initialize_thread_stack
856 /******************* function asm_perform_threadswitch *************************
858 * void asm_perform_threadswitch (u1 **from, u1 **to, u1 **stackTop); *
860 * performs a threadswitch *
862 *******************************************************************************/
864 .ent asm_perform_threadswitch
865 asm_perform_threadswitch:
867 aaddiu sp,sp,-14*8 /* allocate new stack */
868 sd s0, 0*8(sp) /* save saved registers of old thread */
882 ast sp,0(a0) /* save old stack pointer */
883 ast sp,0(a2) /* stackTop = old stack pointer */
884 ald sp,0(a1) /* load new stack pointer */
885 ld s0, 0*8(sp) /* load saved registers of new thread */
899 aaddiu sp,sp,14*8 /* deallocate new stack */
901 .end asm_perform_threadswitch
904 /********************* function asm_switchstackandcall *************************
906 * void asm_switchstackandcall (void *stack, void *func, void **stacktopsave); *
908 * Switches to a new stack, calls a function and switches back. *
909 * a0 new stack pointer *
910 * a1 function pointer *
911 * a2 pointer to variable where stack top should be stored *
913 *******************************************************************************/
916 .ent asm_switchstackandcall
917 asm_switchstackandcall:
918 aaddiu a0,a0,-16 /* allocate new stack */
919 sd ra,0(a0) /* save return address on new stack */
920 sd sp,8(a0) /* save old stack pointer on new stack */
921 sd sp,0(a2) /* save old stack pointer to variable */
922 move sp,a0 /* switch to new stack */
925 jalr itmp3 /* and call function */
927 ld ra,0(sp) /* load return address */
928 ld sp,8(sp) /* switch to old stack */
932 .end asm_switchstackandcall