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/jit/builtin.hpp"
39 #include "vm/options.h"
41 #include "vm/jit/abi.h"
42 #include "vm/jit/abi-asm.h"
43 #include "vm/jit/asmpart.h"
44 #include "vm/jit/dseg.h"
45 #include "vm/jit/emit-common.hpp"
46 #include "vm/jit/jit.hpp"
47 #include "vm/jit/replace.hpp"
49 #include "vm/jit/sparc64/solaris/macro_rename.h"
51 /* how to leaf optimization in the emitted stubs?? */
52 #define REG_PV REG_PV_CALLEE
55 /* emit_load *******************************************************************
57 Emits a possible load of an operand.
59 *******************************************************************************/
61 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
67 /* get required compiler data */
71 if (src->flags & INMEMORY) {
72 COUNT_READ_SPILLS(src)
74 disp = JITSTACK + src->vv.regoff;
81 M_LDX(tempreg, REG_SP, disp);
85 M_DLD(tempreg, REG_SP, disp);
88 vm_abort("emit_load: unknown type %d", src->type);
101 /* emit_store ******************************************************************
103 Emits a possible store to variable.
105 *******************************************************************************/
107 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
112 /* get required compiler data */
116 if (dst->flags & INMEMORY) {
117 COUNT_WRITE_SPILLS(dst)
119 disp = JITSTACK + dst->vv.regoff;
126 M_STX(d, REG_SP, disp);
130 M_DST(d, REG_SP, disp);
133 vm_abort("emit_store: unknown type %d", dst->type);
140 /* emit_copy *******************************************************************
142 Generates a register/memory to register/memory copy.
144 *******************************************************************************/
146 void emit_copy(jitdata *jd, instruction *iptr)
153 /* get required compiler data */
157 /* get source and destination variables */
159 src = VAROP(iptr->s1);
160 dst = VAROP(iptr->dst);
162 if ((src->vv.regoff != dst->vv.regoff) ||
163 ((src->flags ^ dst->flags) & INMEMORY)) {
165 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
166 /* emit nothing, as the value won't be used anyway */
170 /* If one of the variables resides in memory, we can eliminate
171 the register move from/to the temporary register with the
172 order of getting the destination register and the load. */
174 if (IS_INMEMORY(src->flags)) {
175 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
176 s1 = emit_load(jd, iptr, src, d);
179 s1 = emit_load(jd, iptr, src, REG_IFTMP);
180 d = codegen_reg_of_var(iptr->opc, dst, s1);
195 vm_abort("emit_copy: unknown type %d", src->type);
200 emit_store(jd, iptr, dst, d);
205 /* emit_iconst *****************************************************************
209 *******************************************************************************/
211 void emit_iconst(codegendata *cd, s4 d, s4 value)
215 if ((value >= -4096) && (value <= 4095)) {
216 M_XOR_IMM(REG_ZERO, value, d);
218 disp = dseg_add_s4(cd, value);
219 M_ILD(d, REG_PV_CALLEE, disp);
224 /* emit_lconst *****************************************************************
228 *******************************************************************************/
230 void emit_lconst(codegendata *cd, s4 d, s8 value)
234 if ((value >= -4096) && (value <= 4095)) {
235 M_XOR_IMM(REG_ZERO, value, d);
237 disp = dseg_add_s8(cd, value);
238 M_LDX(d, REG_PV_CALLEE, disp);
242 /* emit_branch *****************************************************************
244 Emits the code for conditional and unconditional branchs.
246 *******************************************************************************/
248 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
252 /* calculate the different displacements */
254 branchdisp = disp >> 2;
256 /* check which branch to generate */
258 if (condition == BRANCH_UNCONDITIONAL) {
259 /* check displacement for overflow (19-bit)*/
261 if ((branchdisp < (s4) 0xfffc0000) || (branchdisp > (s4) 0x003ffff)) {
262 /* if the long-branches flag isn't set yet, do it */
264 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
265 cd->flags |= (CODEGENDATA_FLAG_ERROR |
266 CODEGENDATA_FLAG_LONGBRANCHES);
269 vm_abort("emit_branch: emit unconditional long-branch code");
276 else if (reg == -1) {
277 /* branch on condition codes */
279 /* check displacement for overflow (19-bit)*/
281 if ((branchdisp < (s4) 0xfffc0000) || (branchdisp > (s4) 0x003ffff)) {
282 /* if the long-branches flag isn't set yet, do it */
284 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
285 log_println("setting error");
286 cd->flags |= (CODEGENDATA_FLAG_ERROR |
287 CODEGENDATA_FLAG_LONGBRANCHES);
290 vm_abort("emit_branch: emit long-branch on cc code");
293 /* check whether to branch on 64-bit condition code */
294 if (BRANCH_CHECKS_XCC(opt)) {
321 vm_abort("emit_branch: unknown condition %d", condition);
354 vm_abort("emit_branch: unknown condition %d", condition);
363 /* branch on register */
365 /* check displacement for overflow (16-bit) */
367 if ((branchdisp < (s4) 0xffff8000) || (branchdisp > (s4) 0x0007fff)) {
368 /* if the long-branches flag isn't set yet, do it */
370 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
371 log_println("setting error");
372 cd->flags |= (CODEGENDATA_FLAG_ERROR |
373 CODEGENDATA_FLAG_LONGBRANCHES);
376 vm_abort("emit_branch: emit long-branch on reg code");
381 M_BEQZ(reg, branchdisp);
384 M_BNEZ(reg, branchdisp);
387 M_BLTZ(reg, branchdisp);
390 M_BGEZ(reg, branchdisp);
393 M_BGTZ(reg, branchdisp);
396 M_BLEZ(reg, branchdisp);
399 vm_abort("emit_branch: unknown condition %d", condition);
409 /* emit_bxx_xcc*****************************************************************
411 Wrappers for branches on 64-bit condition codes (SPARC specific).
413 *******************************************************************************/
415 void emit_beq_xcc(codegendata *cd, basicblock *target)
417 emit_bcc(cd, target, BRANCH_EQ, BRANCH_OPT_XCC);
420 void emit_bne_xcc(codegendata *cd, basicblock *target)
422 emit_bcc(cd, target, BRANCH_NE, BRANCH_OPT_XCC);
425 void emit_blt_xcc(codegendata *cd, basicblock *target)
427 emit_bcc(cd, target, BRANCH_LT, BRANCH_OPT_XCC);
430 void emit_bge_xcc(codegendata *cd, basicblock *target)
432 emit_bcc(cd, target, BRANCH_GE, BRANCH_OPT_XCC);
435 void emit_bgt_xcc(codegendata *cd, basicblock *target)
437 emit_bcc(cd, target, BRANCH_GT, BRANCH_OPT_XCC);
440 void emit_ble_xcc(codegendata *cd, basicblock *target)
442 emit_bcc(cd, target, BRANCH_LE, BRANCH_OPT_XCC);
449 /* emit_arithmetic_check *******************************************************
451 Emit an ArithmeticException check.
453 *******************************************************************************/
455 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
457 if (INSTRUCTION_MUST_CHECK(iptr)) {
460 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_ArithmeticException);
465 /* emit_arrayindexoutofbounds_check ********************************************
467 Emit an ArrayIndexOutOfBoundsException check.
469 *******************************************************************************/
471 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
473 if (INSTRUCTION_MUST_CHECK(iptr)) {
474 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
475 M_CMP(s2, REG_ITMP3);
478 M_ALD_INTERN(s2, REG_ZERO, TRAP_ArrayIndexOutOfBoundsException);
483 /* emit_arraystore_check *******************************************************
485 Emit an ArrayStoreException check.
487 *******************************************************************************/
489 void emit_arraystore_check(codegendata *cd, instruction *iptr)
491 if (INSTRUCTION_MUST_CHECK(iptr)) {
492 M_BNEZ(REG_RESULT_CALLER, 3);
494 M_ALD_INTERN(REG_RESULT_CALLER, REG_ZERO, TRAP_ArrayStoreException);
499 /* emit_classcast_check ********************************************************
501 Emit a ClassCastException check.
503 *******************************************************************************/
505 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
507 /* XXX: use 64-bit or 32-bit compares??? */
509 if (INSTRUCTION_MUST_CHECK(iptr)) {
524 vm_abort("emit_classcast_check: unknown condition %d", condition);
528 M_ALD_INTERN(s1, REG_ZERO, TRAP_ClassCastException);
533 /* emit_nullpointer_check ******************************************************
535 Emit a NullPointerException check.
537 *******************************************************************************/
539 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
541 if (INSTRUCTION_MUST_CHECK(iptr)) {
544 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_NullPointerException);
549 /* emit_exception_check ********************************************************
551 Emit an Exception check.
553 *******************************************************************************/
555 void emit_exception_check(codegendata *cd, instruction *iptr)
557 if (INSTRUCTION_MUST_CHECK(iptr)) {
558 M_BNEZ(REG_RESULT_CALLER, 3);
560 M_ALD_INTERN(REG_RESULT_CALLER, REG_ZERO, TRAP_CHECK_EXCEPTION);
565 /* emit_trap *******************************************************************
567 Emit a trap instruction and return the original machine code.
569 *******************************************************************************/
571 uint32_t emit_trap(codegendata *cd)
575 /* Get machine code which is patched back in later. The
576 trap is 1 instruction word long. */
578 mcode = *((uint32_t *) cd->mcodeptr);
580 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_PATCHER);
586 /* emit_patcher_stubs **********************************************************
588 Generates the code for the patcher stubs.
590 *******************************************************************************/
592 void emit_patcher_stubs(jitdata *jd)
602 /* get required compiler data */
606 /* generate code patching stub call code */
610 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
611 /* check code segment size */
615 /* Get machine code which is patched back in later. The
616 call is 2 instruction words long. */
618 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
620 /* We use 2 loads here as an unaligned 8-byte read on 64-bit
621 SPARC causes a SIGSEGV */
623 mcode[0] = ((u4 *) tmpmcodeptr)[0];
624 mcode[1] = ((u4 *) tmpmcodeptr)[1];
626 /* Patch in the call to call the following code (done at
629 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
630 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
632 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) );
634 if ((disp < (s4) 0xfffc0000) || (disp > (s4) 0x003ffff)) {
635 vm_abort("Jump offset is out of range: %d > +/-%d",
643 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
645 /* extend stack frame for wrapper data */
647 M_ASUB_IMM(REG_SP, 6 * 8, REG_SP);
649 /* calculate return address and move it onto the stack */
651 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
652 M_AST(REG_ITMP3, REG_SP, JITSTACK + 5 * 8);
654 /* move pointer to java_objectheader onto stack */
656 #if defined(ENABLE_THREADS)
657 /* create a virtual java_objectheader */
659 (void) dseg_add_unique_address(cd, NULL); /* flcword */
660 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
661 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
663 M_LDA(REG_ITMP3, REG_PV, disp);
664 M_AST(REG_ITMP3, REG_SP, JITSTACK + 4 * 8);
669 /* move machine code onto stack */
671 disp = dseg_add_s4(cd, mcode[0]);
672 M_ILD(REG_ITMP3, REG_PV, disp);
673 M_IST(REG_ITMP3, REG_SP, JITSTACK + 3 * 8);
675 disp = dseg_add_s4(cd, mcode[1]);
676 M_ILD(REG_ITMP3, REG_PV, disp);
677 M_IST(REG_ITMP3, REG_SP, JITSTACK + 3 * 8 + 4);
679 /* move class/method/field reference onto stack */
681 disp = dseg_add_address(cd, pref->ref);
682 M_ALD(REG_ITMP3, REG_PV, disp);
683 M_AST(REG_ITMP3, REG_SP, JITSTACK + 2 * 8);
685 /* move data segment displacement onto stack */
687 disp = dseg_add_s4(cd, pref->disp);
688 M_ILD(REG_ITMP3, REG_PV, disp);
689 M_IST(REG_ITMP3, REG_SP, JITSTACK + 1 * 8);
691 /* move patcher function pointer onto stack */
693 disp = dseg_add_functionptr(cd, pref->patcher);
694 M_ALD(REG_ITMP3, REG_PV, disp);
695 M_AST(REG_ITMP3, REG_SP, JITSTACK + 0 * 8);
697 if (targetdisp == 0) {
698 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
700 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
701 M_ALD(REG_ITMP3, REG_PV, disp);
702 M_JMP(REG_ZERO, REG_ITMP3, REG_ZERO);
706 disp = (((u4 *) cd->mcodebase) + targetdisp) -
707 (((u4 *) cd->mcodeptr));
716 /* emit_verbosecall_enter ******************************************************
718 Generates the code for the call trace.
720 *******************************************************************************/
723 void emit_verbosecall_enter(jitdata *jd)
733 /* get required compiler data */
741 /* mark trace code */
746 stackslots = 1 + FLT_ARG_CNT;
747 ALIGN_STACK_SLOTS(stackslots);
749 M_LDA(REG_SP, REG_SP, -(stackslots * 8));
751 /* save float argument registers */
753 for (i = 0; i < FLT_ARG_CNT; i++)
754 M_DST(abi_registers_float_argument[i], REG_SP, JITSTACK + (1 + i) * 8);
756 /* save temporary registers for leaf methods */
757 /* XXX no leaf optimization yet
758 if (code_is_leafmethod(code)) {
759 for (i = 0; i < INT_TMP_CNT; i++)
760 M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
762 for (i = 0; i < FLT_TMP_CNT; i++)
763 M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
766 /* load int/float arguments into integer argument registers */
768 for (i = 0; i < md->paramcount && i < INT_NATARG_CNT; i++) {
769 t = md->paramtypes[i].type;
771 /* all available argument registers used, which adds a little complexity */
773 if (IS_INT_LNG_TYPE(t)) {
774 if (i < INT_ARG_CNT) {
775 M_INTMOVE(REG_WINDOW_TRANSPOSE(abi_registers_integer_argument[i]),
776 abi_registers_integer_argument[i]);
780 M_LDX(REG_OUT5, REG_FP, JITSTACK);
784 if (i < FLT_ARG_CNT) {
786 /* reg -> mem -> reg */
788 if (IS_2_WORD_TYPE(t)) {
789 M_DST(abi_registers_float_argument[i], REG_SP, JITSTACK);
790 M_LDX(abi_registers_integer_argument[i], REG_SP, JITSTACK);
793 M_FST(abi_registers_float_argument[i], REG_SP, JITSTACK);
794 M_ILD(abi_registers_integer_argument[i], REG_SP, JITSTACK);
802 if (IS_2_WORD_TYPE(t)) {
803 M_LDX(REG_OUT5, REG_FP, JITSTACK);
806 M_ILD(REG_OUT5, REG_FP, JITSTACK);
813 /* method info pointer is passed via stack */
814 disp = dseg_add_address(cd, m);
815 M_ALD(REG_ITMP1, REG_PV_CALLEE, disp);
816 M_AST(REG_ITMP1, REG_SP, CSTACK);
817 disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
818 M_ALD(REG_ITMP1, REG_PV_CALLEE, disp);
819 M_JMP(REG_RA_CALLER, REG_ITMP1, REG_ZERO);
822 /* restore float argument registers */
824 for (i = 0; i < FLT_ARG_CNT; i++)
825 M_DLD(abi_registers_float_argument[i], REG_SP, JITSTACK + (1 + i) * 8);
827 /* restore temporary registers for leaf methods */
828 /* XXX no leaf optimization yet
829 if (code_is_leafmethod(code)) {
830 for (i = 0; i < INT_TMP_CNT; i++)
831 M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
833 for (i = 0; i < FLT_TMP_CNT; i++)
834 M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
837 M_LDA(REG_SP, REG_SP, stackslots * 8);
839 /* mark trace code */
843 #endif /* !defined(NDEBUG) */
846 /* emit_verbosecall_exit *******************************************************
848 Generates the code for the call trace.
850 *******************************************************************************/
853 void emit_verbosecall_exit(jitdata *jd)
860 /* get required compiler data */
866 /* mark trace code */
870 /* XXX jit-c-call (keep stack aligned)*/
871 M_LDA(REG_SP, REG_SP, -(2 * 8));
873 M_DST(REG_FRESULT, REG_SP, JITSTACK);
875 M_MOV(REG_RESULT_CALLEE, REG_OUT0);
876 M_DMOV(REG_FRESULT, 1); /* logical dreg 1 => f2 */
877 M_FMOV(REG_FRESULT, 2); /* logical freg 2 => f5 */
879 disp = dseg_add_functionptr(cd, m);
880 M_ALD(REG_OUT3, REG_PV_CALLEE, disp);
882 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
883 M_ALD(REG_ITMP3, REG_PV_CALLEE, disp);
884 M_JMP(REG_RA_CALLER, REG_ITMP3, REG_ZERO);
887 M_DLD(REG_FRESULT, REG_SP, JITSTACK);
889 M_LDA(REG_SP, REG_SP, 2 * 8);
891 /* mark trace code */
895 #endif /* !defined(NDEBUG) */
899 * These are local overrides for various environment variables in Emacs.
900 * Please do not remove this and leave it at the end of the file, where
901 * Emacs will automagically detect them.
902 * ---------------------------------------------------------------------
905 * indent-tabs-mode: t
909 * vim:noexpandtab:sw=4:ts=4: