1 /* src/vm/jit/sparc64/emit.c - SPARC code emitter functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 #include "vm/jit/sparc64/codegen.h"
33 #include "vm/jit/sparc64/md-abi.h"
34 #include "vm/jit/sparc64/emit.h"
36 #include "mm/memory.h"
38 #include "vm/stringlocal.h" /* XXX for gen_resolvebranch */
39 #include "vm/jit/abi.h"
40 #include "vm/jit/abi-asm.h"
41 #include "vm/jit/asmpart.h"
42 #include "vm/builtin.h"
43 #include "vm/jit/dseg.h"
44 #include "vm/jit/emit-common.h"
45 #include "vm/jit/jit.h"
46 #include "vm/jit/replace.h"
48 #include "vmcore/options.h"
50 #include "vm/jit/sparc64/solaris/macro_rename.h"
52 /* how to leaf optimization in the emitted stubs?? */
53 #define REG_PV REG_PV_CALLEE
56 /* emit_load *******************************************************************
58 Emits a possible load of an operand.
60 *******************************************************************************/
62 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
68 /* get required compiler data */
72 if (src->flags & INMEMORY) {
73 COUNT_READ_SPILLS(src)
75 disp = JITSTACK + src->vv.regoff;
82 M_LDX(tempreg, REG_SP, disp);
86 M_DLD(tempreg, REG_SP, disp);
89 vm_abort("emit_load: unknown type %d", src->type);
102 /* emit_store ******************************************************************
104 Emits a possible store to variable.
106 *******************************************************************************/
108 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
113 /* get required compiler data */
117 if (dst->flags & INMEMORY) {
118 COUNT_WRITE_SPILLS(dst)
120 disp = JITSTACK + dst->vv.regoff;
127 M_STX(d, REG_SP, disp);
131 M_DST(d, REG_SP, disp);
134 vm_abort("emit_store: unknown type %d", dst->type);
141 /* emit_copy *******************************************************************
143 Generates a register/memory to register/memory copy.
145 *******************************************************************************/
147 void emit_copy(jitdata *jd, instruction *iptr)
154 /* get required compiler data */
158 /* get source and destination variables */
160 src = VAROP(iptr->s1);
161 dst = VAROP(iptr->dst);
163 if ((src->vv.regoff != dst->vv.regoff) ||
164 ((src->flags ^ dst->flags) & INMEMORY)) {
166 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
167 /* emit nothing, as the value won't be used anyway */
171 /* If one of the variables resides in memory, we can eliminate
172 the register move from/to the temporary register with the
173 order of getting the destination register and the load. */
175 if (IS_INMEMORY(src->flags)) {
176 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
177 s1 = emit_load(jd, iptr, src, d);
180 s1 = emit_load(jd, iptr, src, REG_IFTMP);
181 d = codegen_reg_of_var(iptr->opc, dst, s1);
196 vm_abort("emit_copy: unknown type %d", src->type);
201 emit_store(jd, iptr, dst, d);
206 /* emit_iconst *****************************************************************
210 *******************************************************************************/
212 void emit_iconst(codegendata *cd, s4 d, s4 value)
216 if ((value >= -4096) && (value <= 4095)) {
217 M_XOR_IMM(REG_ZERO, value, d);
219 disp = dseg_add_s4(cd, value);
220 M_ILD(d, REG_PV_CALLEE, disp);
225 /* emit_lconst *****************************************************************
229 *******************************************************************************/
231 void emit_lconst(codegendata *cd, s4 d, s8 value)
235 if ((value >= -4096) && (value <= 4095)) {
236 M_XOR_IMM(REG_ZERO, value, d);
238 disp = dseg_add_s8(cd, value);
239 M_LDX(d, REG_PV_CALLEE, disp);
243 /* emit_branch *****************************************************************
245 Emits the code for conditional and unconditional branchs.
247 *******************************************************************************/
249 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
253 /* calculate the different displacements */
255 branchdisp = disp >> 2;
257 /* check which branch to generate */
259 if (condition == BRANCH_UNCONDITIONAL) {
260 /* check displacement for overflow (19-bit)*/
262 if ((branchdisp < (s4) 0xfffc0000) || (branchdisp > (s4) 0x003ffff)) {
263 /* if the long-branches flag isn't set yet, do it */
265 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
266 cd->flags |= (CODEGENDATA_FLAG_ERROR |
267 CODEGENDATA_FLAG_LONGBRANCHES);
270 vm_abort("emit_branch: emit unconditional long-branch code");
277 else if (reg == -1) {
278 /* branch on condition codes */
280 /* check displacement for overflow (19-bit)*/
282 if ((branchdisp < (s4) 0xfffc0000) || (branchdisp > (s4) 0x003ffff)) {
283 /* if the long-branches flag isn't set yet, do it */
285 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
286 log_println("setting error");
287 cd->flags |= (CODEGENDATA_FLAG_ERROR |
288 CODEGENDATA_FLAG_LONGBRANCHES);
291 vm_abort("emit_branch: emit long-branch on cc code");
294 /* check whether to branch on 64-bit condition code */
295 if (BRANCH_CHECKS_XCC(opt)) {
322 vm_abort("emit_branch: unknown condition %d", condition);
355 vm_abort("emit_branch: unknown condition %d", condition);
364 /* branch on register */
366 /* check displacement for overflow (16-bit) */
368 if ((branchdisp < (s4) 0xffff8000) || (branchdisp > (s4) 0x0007fff)) {
369 /* if the long-branches flag isn't set yet, do it */
371 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
372 log_println("setting error");
373 cd->flags |= (CODEGENDATA_FLAG_ERROR |
374 CODEGENDATA_FLAG_LONGBRANCHES);
377 vm_abort("emit_branch: emit long-branch on reg code");
382 M_BEQZ(reg, branchdisp);
385 M_BNEZ(reg, branchdisp);
388 M_BLTZ(reg, branchdisp);
391 M_BGEZ(reg, branchdisp);
394 M_BGTZ(reg, branchdisp);
397 M_BLEZ(reg, branchdisp);
400 vm_abort("emit_branch: unknown condition %d", condition);
410 /* emit_bxx_xcc*****************************************************************
412 Wrappers for branches on 64-bit condition codes (SPARC specific).
414 *******************************************************************************/
416 void emit_beq_xcc(codegendata *cd, basicblock *target)
418 emit_bcc(cd, target, BRANCH_EQ, BRANCH_OPT_XCC);
421 void emit_bne_xcc(codegendata *cd, basicblock *target)
423 emit_bcc(cd, target, BRANCH_NE, BRANCH_OPT_XCC);
426 void emit_blt_xcc(codegendata *cd, basicblock *target)
428 emit_bcc(cd, target, BRANCH_LT, BRANCH_OPT_XCC);
431 void emit_bge_xcc(codegendata *cd, basicblock *target)
433 emit_bcc(cd, target, BRANCH_GE, BRANCH_OPT_XCC);
436 void emit_bgt_xcc(codegendata *cd, basicblock *target)
438 emit_bcc(cd, target, BRANCH_GT, BRANCH_OPT_XCC);
441 void emit_ble_xcc(codegendata *cd, basicblock *target)
443 emit_bcc(cd, target, BRANCH_LE, BRANCH_OPT_XCC);
450 /* emit_arithmetic_check *******************************************************
452 Emit an ArithmeticException check.
454 *******************************************************************************/
456 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
458 if (INSTRUCTION_MUST_CHECK(iptr)) {
461 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_ArithmeticException);
466 /* emit_arrayindexoutofbounds_check ********************************************
468 Emit an ArrayIndexOutOfBoundsException check.
470 *******************************************************************************/
472 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
474 if (INSTRUCTION_MUST_CHECK(iptr)) {
475 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
476 M_CMP(s2, REG_ITMP3);
479 M_ALD_INTERN(s2, REG_ZERO, TRAP_ArrayIndexOutOfBoundsException);
484 /* emit_arraystore_check *******************************************************
486 Emit an ArrayStoreException check.
488 *******************************************************************************/
490 void emit_arraystore_check(codegendata *cd, instruction *iptr)
492 if (INSTRUCTION_MUST_CHECK(iptr)) {
493 M_BNEZ(REG_RESULT_CALLER, 3);
495 M_ALD_INTERN(REG_RESULT_CALLER, REG_ZERO, TRAP_ArrayStoreException);
500 /* emit_classcast_check ********************************************************
502 Emit a ClassCastException check.
504 *******************************************************************************/
506 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
508 /* XXX: use 64-bit or 32-bit compares??? */
510 if (INSTRUCTION_MUST_CHECK(iptr)) {
525 vm_abort("emit_classcast_check: unknown condition %d", condition);
529 M_ALD_INTERN(s1, REG_ZERO, TRAP_ClassCastException);
534 /* emit_nullpointer_check ******************************************************
536 Emit a NullPointerException check.
538 *******************************************************************************/
540 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
542 if (INSTRUCTION_MUST_CHECK(iptr)) {
545 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_NullPointerException);
550 /* emit_exception_check ********************************************************
552 Emit an Exception check.
554 *******************************************************************************/
556 void emit_exception_check(codegendata *cd, instruction *iptr)
558 if (INSTRUCTION_MUST_CHECK(iptr)) {
559 M_BNEZ(REG_RESULT_CALLER, 3);
561 M_ALD_INTERN(REG_RESULT_CALLER, REG_ZERO, TRAP_CHECK_EXCEPTION);
566 /* emit_trap *******************************************************************
568 Emit a trap instruction and return the original machine code.
570 *******************************************************************************/
572 uint32_t emit_trap(codegendata *cd)
576 /* Get machine code which is patched back in later. The
577 trap is 1 instruction word long. */
579 mcode = *((uint32_t *) cd->mcodeptr);
581 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_PATCHER);
587 /* emit_patcher_stubs **********************************************************
589 Generates the code for the patcher stubs.
591 *******************************************************************************/
593 void emit_patcher_stubs(jitdata *jd)
603 /* get required compiler data */
607 /* generate code patching stub call code */
611 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
612 /* check code segment size */
616 /* Get machine code which is patched back in later. The
617 call is 2 instruction words long. */
619 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
621 /* We use 2 loads here as an unaligned 8-byte read on 64-bit
622 SPARC causes a SIGSEGV */
624 mcode[0] = ((u4 *) tmpmcodeptr)[0];
625 mcode[1] = ((u4 *) tmpmcodeptr)[1];
627 /* Patch in the call to call the following code (done at
630 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
631 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
633 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) );
635 if ((disp < (s4) 0xfffc0000) || (disp > (s4) 0x003ffff)) {
636 vm_abort("Jump offset is out of range: %d > +/-%d",
644 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
646 /* extend stack frame for wrapper data */
648 M_ASUB_IMM(REG_SP, 6 * 8, REG_SP);
650 /* calculate return address and move it onto the stack */
652 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
653 M_AST(REG_ITMP3, REG_SP, JITSTACK + 5 * 8);
655 /* move pointer to java_objectheader onto stack */
657 #if defined(ENABLE_THREADS)
658 /* create a virtual java_objectheader */
660 (void) dseg_add_unique_address(cd, NULL); /* flcword */
661 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
662 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
664 M_LDA(REG_ITMP3, REG_PV, disp);
665 M_AST(REG_ITMP3, REG_SP, JITSTACK + 4 * 8);
670 /* move machine code onto stack */
672 disp = dseg_add_s4(cd, mcode[0]);
673 M_ILD(REG_ITMP3, REG_PV, disp);
674 M_IST(REG_ITMP3, REG_SP, JITSTACK + 3 * 8);
676 disp = dseg_add_s4(cd, mcode[1]);
677 M_ILD(REG_ITMP3, REG_PV, disp);
678 M_IST(REG_ITMP3, REG_SP, JITSTACK + 3 * 8 + 4);
680 /* move class/method/field reference onto stack */
682 disp = dseg_add_address(cd, pref->ref);
683 M_ALD(REG_ITMP3, REG_PV, disp);
684 M_AST(REG_ITMP3, REG_SP, JITSTACK + 2 * 8);
686 /* move data segment displacement onto stack */
688 disp = dseg_add_s4(cd, pref->disp);
689 M_ILD(REG_ITMP3, REG_PV, disp);
690 M_IST(REG_ITMP3, REG_SP, JITSTACK + 1 * 8);
692 /* move patcher function pointer onto stack */
694 disp = dseg_add_functionptr(cd, pref->patcher);
695 M_ALD(REG_ITMP3, REG_PV, disp);
696 M_AST(REG_ITMP3, REG_SP, JITSTACK + 0 * 8);
698 if (targetdisp == 0) {
699 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
701 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
702 M_ALD(REG_ITMP3, REG_PV, disp);
703 M_JMP(REG_ZERO, REG_ITMP3, REG_ZERO);
707 disp = (((u4 *) cd->mcodebase) + targetdisp) -
708 (((u4 *) cd->mcodeptr));
717 /* emit_verbosecall_enter ******************************************************
719 Generates the code for the call trace.
721 *******************************************************************************/
724 void emit_verbosecall_enter(jitdata *jd)
734 /* get required compiler data */
742 /* mark trace code */
747 stackslots = 1 + FLT_ARG_CNT;
748 ALIGN_STACK_SLOTS(stackslots);
750 M_LDA(REG_SP, REG_SP, -(stackslots * 8));
752 /* save float argument registers */
754 for (i = 0; i < FLT_ARG_CNT; i++)
755 M_DST(abi_registers_float_argument[i], REG_SP, JITSTACK + (1 + i) * 8);
757 /* save temporary registers for leaf methods */
758 /* XXX no leaf optimization yet
759 if (code_is_leafmethod(code)) {
760 for (i = 0; i < INT_TMP_CNT; i++)
761 M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
763 for (i = 0; i < FLT_TMP_CNT; i++)
764 M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
767 /* load int/float arguments into integer argument registers */
769 for (i = 0; i < md->paramcount && i < INT_NATARG_CNT; i++) {
770 t = md->paramtypes[i].type;
772 /* all available argument registers used, which adds a little complexity */
774 if (IS_INT_LNG_TYPE(t)) {
775 if (i < INT_ARG_CNT) {
776 M_INTMOVE(REG_WINDOW_TRANSPOSE(abi_registers_integer_argument[i]),
777 abi_registers_integer_argument[i]);
781 M_LDX(REG_OUT5, REG_FP, JITSTACK);
785 if (i < FLT_ARG_CNT) {
787 /* reg -> mem -> reg */
789 if (IS_2_WORD_TYPE(t)) {
790 M_DST(abi_registers_float_argument[i], REG_SP, JITSTACK);
791 M_LDX(abi_registers_integer_argument[i], REG_SP, JITSTACK);
794 M_FST(abi_registers_float_argument[i], REG_SP, JITSTACK);
795 M_ILD(abi_registers_integer_argument[i], REG_SP, JITSTACK);
803 if (IS_2_WORD_TYPE(t)) {
804 M_LDX(REG_OUT5, REG_FP, JITSTACK);
807 M_ILD(REG_OUT5, REG_FP, JITSTACK);
814 /* method info pointer is passed via stack */
815 disp = dseg_add_address(cd, m);
816 M_ALD(REG_ITMP1, REG_PV_CALLEE, disp);
817 M_AST(REG_ITMP1, REG_SP, CSTACK);
818 disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
819 M_ALD(REG_ITMP1, REG_PV_CALLEE, disp);
820 M_JMP(REG_RA_CALLER, REG_ITMP1, REG_ZERO);
823 /* restore float argument registers */
825 for (i = 0; i < FLT_ARG_CNT; i++)
826 M_DLD(abi_registers_float_argument[i], REG_SP, JITSTACK + (1 + i) * 8);
828 /* restore temporary registers for leaf methods */
829 /* XXX no leaf optimization yet
830 if (code_is_leafmethod(code)) {
831 for (i = 0; i < INT_TMP_CNT; i++)
832 M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
834 for (i = 0; i < FLT_TMP_CNT; i++)
835 M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
838 M_LDA(REG_SP, REG_SP, stackslots * 8);
840 /* mark trace code */
844 #endif /* !defined(NDEBUG) */
847 /* emit_verbosecall_exit *******************************************************
849 Generates the code for the call trace.
851 *******************************************************************************/
854 void emit_verbosecall_exit(jitdata *jd)
861 /* get required compiler data */
867 /* mark trace code */
871 /* XXX jit-c-call (keep stack aligned)*/
872 M_LDA(REG_SP, REG_SP, -(2 * 8));
874 M_DST(REG_FRESULT, REG_SP, JITSTACK);
876 M_MOV(REG_RESULT_CALLEE, REG_OUT0);
877 M_DMOV(REG_FRESULT, 1); /* logical dreg 1 => f2 */
878 M_FMOV(REG_FRESULT, 2); /* logical freg 2 => f5 */
880 disp = dseg_add_functionptr(cd, m);
881 M_ALD(REG_OUT3, REG_PV_CALLEE, disp);
883 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
884 M_ALD(REG_ITMP3, REG_PV_CALLEE, disp);
885 M_JMP(REG_RA_CALLER, REG_ITMP3, REG_ZERO);
888 M_DLD(REG_FRESULT, REG_SP, JITSTACK);
890 M_LDA(REG_SP, REG_SP, 2 * 8);
892 /* mark trace code */
896 #endif /* !defined(NDEBUG) */
900 * These are local overrides for various environment variables in Emacs.
901 * Please do not remove this and leave it at the end of the file, where
902 * Emacs will automagically detect them.
903 * ---------------------------------------------------------------------
906 * indent-tabs-mode: t
910 * vim:noexpandtab:sw=4:ts=4: