1 /* src/vm/jit/sparc64/emit.c - SPARC code emitter functions
3 Copyright (C) 1996-2005, 2006, 2007 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
34 #include "vm/jit/sparc64/codegen.h"
35 #include "vm/jit/sparc64/md-abi.h"
36 #include "vm/jit/sparc64/emit.h"
38 #include "mm/memory.h"
40 #include "vm/exceptions.h"
41 #include "vm/stringlocal.h" /* XXX for gen_resolvebranch */
42 #include "vm/jit/abi.h"
43 #include "vm/jit/abi-asm.h"
44 #include "vm/jit/asmpart.h"
45 #include "vm/builtin.h"
46 #include "vm/jit/dseg.h"
47 #include "vm/jit/emit-common.h"
48 #include "vm/jit/jit.h"
49 #include "vm/jit/replace.h"
51 #include "vmcore/options.h"
53 #include "vm/jit/sparc64/solaris/macro_rename.h"
55 /* how to leaf optimization in the emitted stubs?? */
56 #define REG_PV REG_PV_CALLEE
59 /* emit_load *******************************************************************
61 Emits a possible load of an operand.
63 *******************************************************************************/
65 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
71 /* get required compiler data */
75 if (src->flags & INMEMORY) {
76 COUNT_READ_SPILLS(src)
78 disp = JITSTACK + src->vv.regoff;
85 M_LDX(tempreg, REG_SP, disp);
89 M_DLD(tempreg, REG_SP, disp);
92 vm_abort("emit_load: unknown type %d", src->type);
105 /* emit_store ******************************************************************
107 Emits a possible store to variable.
109 *******************************************************************************/
111 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
116 /* get required compiler data */
120 if (dst->flags & INMEMORY) {
121 COUNT_WRITE_SPILLS(dst)
123 disp = JITSTACK + dst->vv.regoff;
130 M_STX(d, REG_SP, disp);
134 M_DST(d, REG_SP, disp);
137 vm_abort("emit_store: unknown type %d", dst->type);
144 /* emit_copy *******************************************************************
146 Generates a register/memory to register/memory copy.
148 *******************************************************************************/
150 void emit_copy(jitdata *jd, instruction *iptr)
157 /* get required compiler data */
161 /* get source and destination variables */
163 src = VAROP(iptr->s1);
164 dst = VAROP(iptr->dst);
166 if ((src->vv.regoff != dst->vv.regoff) ||
167 ((src->flags ^ dst->flags) & INMEMORY)) {
169 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
170 /* emit nothing, as the value won't be used anyway */
174 /* If one of the variables resides in memory, we can eliminate
175 the register move from/to the temporary register with the
176 order of getting the destination register and the load. */
178 if (IS_INMEMORY(src->flags)) {
179 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
180 s1 = emit_load(jd, iptr, src, d);
183 s1 = emit_load(jd, iptr, src, REG_IFTMP);
184 d = codegen_reg_of_var(iptr->opc, dst, s1);
199 vm_abort("emit_copy: unknown type %d", src->type);
204 emit_store(jd, iptr, dst, d);
209 /* emit_iconst *****************************************************************
213 *******************************************************************************/
215 void emit_iconst(codegendata *cd, s4 d, s4 value)
219 if ((value >= -4096) && (value <= 4095)) {
220 M_XOR_IMM(REG_ZERO, value, d);
222 disp = dseg_add_s4(cd, value);
223 M_ILD(d, REG_PV_CALLEE, disp);
228 /* emit_lconst *****************************************************************
232 *******************************************************************************/
234 void emit_lconst(codegendata *cd, s4 d, s8 value)
238 if ((value >= -4096) && (value <= 4095)) {
239 M_XOR_IMM(REG_ZERO, value, d);
241 disp = dseg_add_s8(cd, value);
242 M_LDX(d, REG_PV_CALLEE, disp);
246 /* emit_branch *****************************************************************
248 Emits the code for conditional and unconditional branchs.
250 *******************************************************************************/
252 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
256 /* calculate the different displacements */
258 branchdisp = disp >> 2;
260 /* check which branch to generate */
262 if (condition == BRANCH_UNCONDITIONAL) {
263 /* check displacement for overflow (19-bit)*/
265 if ((branchdisp < (s4) 0xfffc0000) || (branchdisp > (s4) 0x003ffff)) {
266 /* if the long-branches flag isn't set yet, do it */
268 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
269 cd->flags |= (CODEGENDATA_FLAG_ERROR |
270 CODEGENDATA_FLAG_LONGBRANCHES);
273 vm_abort("emit_branch: emit unconditional long-branch code");
280 else if (reg == -1) {
281 /* branch on condition codes */
283 /* check displacement for overflow (19-bit)*/
285 if ((branchdisp < (s4) 0xfffc0000) || (branchdisp > (s4) 0x003ffff)) {
286 /* if the long-branches flag isn't set yet, do it */
288 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
289 log_println("setting error");
290 cd->flags |= (CODEGENDATA_FLAG_ERROR |
291 CODEGENDATA_FLAG_LONGBRANCHES);
294 vm_abort("emit_branch: emit long-branch on cc code");
297 /* check whether to branch on 64-bit condition code */
298 if (BRANCH_CHECKS_XCC(opt)) {
325 vm_abort("emit_branch: unknown condition %d", condition);
358 vm_abort("emit_branch: unknown condition %d", condition);
367 /* branch on register */
369 /* check displacement for overflow (16-bit) */
371 if ((branchdisp < (s4) 0xffff8000) || (branchdisp > (s4) 0x0007fff)) {
372 /* if the long-branches flag isn't set yet, do it */
374 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
375 log_println("setting error");
376 cd->flags |= (CODEGENDATA_FLAG_ERROR |
377 CODEGENDATA_FLAG_LONGBRANCHES);
380 vm_abort("emit_branch: emit long-branch on reg code");
385 M_BEQZ(reg, branchdisp);
388 M_BNEZ(reg, branchdisp);
391 M_BLTZ(reg, branchdisp);
394 M_BGEZ(reg, branchdisp);
397 M_BGTZ(reg, branchdisp);
400 M_BLEZ(reg, branchdisp);
403 vm_abort("emit_branch: unknown condition %d", condition);
413 /* emit_bxx_xcc*****************************************************************
415 Wrappers for branches on 64-bit condition codes (SPARC specific).
417 *******************************************************************************/
419 void emit_beq_xcc(codegendata *cd, basicblock *target)
421 emit_bcc(cd, target, BRANCH_EQ, BRANCH_OPT_XCC);
424 void emit_bne_xcc(codegendata *cd, basicblock *target)
426 emit_bcc(cd, target, BRANCH_NE, BRANCH_OPT_XCC);
429 void emit_blt_xcc(codegendata *cd, basicblock *target)
431 emit_bcc(cd, target, BRANCH_LT, BRANCH_OPT_XCC);
434 void emit_bge_xcc(codegendata *cd, basicblock *target)
436 emit_bcc(cd, target, BRANCH_GE, BRANCH_OPT_XCC);
439 void emit_bgt_xcc(codegendata *cd, basicblock *target)
441 emit_bcc(cd, target, BRANCH_GT, BRANCH_OPT_XCC);
444 void emit_ble_xcc(codegendata *cd, basicblock *target)
446 emit_bcc(cd, target, BRANCH_LE, BRANCH_OPT_XCC);
453 /* emit_arithmetic_check *******************************************************
455 Emit an ArithmeticException check.
457 *******************************************************************************/
459 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
461 if (INSTRUCTION_MUST_CHECK(iptr)) {
464 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_ArithmeticException);
469 /* emit_arrayindexoutofbounds_check ********************************************
471 Emit an ArrayIndexOutOfBoundsException check.
473 *******************************************************************************/
475 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
477 if (INSTRUCTION_MUST_CHECK(iptr)) {
478 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
479 M_CMP(s2, REG_ITMP3);
482 M_ALD_INTERN(s2, REG_ZERO, TRAP_ArrayIndexOutOfBoundsException);
487 /* emit_arraystore_check *******************************************************
489 Emit an ArrayStoreException check.
491 *******************************************************************************/
493 void emit_arraystore_check(codegendata *cd, instruction *iptr)
495 if (INSTRUCTION_MUST_CHECK(iptr)) {
496 M_BNEZ(REG_RESULT_CALLER, 3);
498 M_ALD_INTERN(REG_RESULT_CALLER, REG_ZERO, TRAP_ArrayStoreException);
503 /* emit_classcast_check ********************************************************
505 Emit a ClassCastException check.
507 *******************************************************************************/
509 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
511 /* XXX: use 64-bit or 32-bit compares??? */
513 if (INSTRUCTION_MUST_CHECK(iptr)) {
528 vm_abort("emit_classcast_check: unknown condition %d", condition);
532 M_ALD_INTERN(s1, REG_ZERO, TRAP_ClassCastException);
537 /* emit_nullpointer_check ******************************************************
539 Emit a NullPointerException check.
541 *******************************************************************************/
543 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
545 if (INSTRUCTION_MUST_CHECK(iptr)) {
548 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_NullPointerException);
553 /* emit_exception_check ********************************************************
555 Emit an Exception check.
557 *******************************************************************************/
559 void emit_exception_check(codegendata *cd, instruction *iptr)
561 if (INSTRUCTION_MUST_CHECK(iptr)) {
562 M_BNEZ(REG_RESULT_CALLER, 3);
564 M_ALD_INTERN(REG_RESULT_CALLER, REG_ZERO, TRAP_CHECK_EXCEPTION);
569 /* emit_trap *******************************************************************
571 Emit a trap instruction and return the original machine code.
573 *******************************************************************************/
575 uint32_t emit_trap(codegendata *cd)
579 /* Get machine code which is patched back in later. The
580 trap is 1 instruction word long. */
582 mcode = *((uint32_t *) cd->mcodeptr);
584 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_PATCHER);
590 /* emit_patcher_stubs **********************************************************
592 Generates the code for the patcher stubs.
594 *******************************************************************************/
596 void emit_patcher_stubs(jitdata *jd)
606 /* get required compiler data */
610 /* generate code patching stub call code */
614 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
615 /* check code segment size */
619 /* Get machine code which is patched back in later. The
620 call is 2 instruction words long. */
622 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
624 /* We use 2 loads here as an unaligned 8-byte read on 64-bit
625 SPARC causes a SIGSEGV */
627 mcode[0] = ((u4 *) tmpmcodeptr)[0];
628 mcode[1] = ((u4 *) tmpmcodeptr)[1];
630 /* Patch in the call to call the following code (done at
633 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
634 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
636 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) );
638 if ((disp < (s4) 0xfffc0000) || (disp > (s4) 0x003ffff)) {
639 vm_abort("Jump offset is out of range: %d > +/-%d",
647 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
649 /* extend stack frame for wrapper data */
651 M_ASUB_IMM(REG_SP, 6 * 8, REG_SP);
653 /* calculate return address and move it onto the stack */
655 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
656 M_AST(REG_ITMP3, REG_SP, JITSTACK + 5 * 8);
658 /* move pointer to java_objectheader onto stack */
660 #if defined(ENABLE_THREADS)
661 /* create a virtual java_objectheader */
663 (void) dseg_add_unique_address(cd, NULL); /* flcword */
664 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
665 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
667 M_LDA(REG_ITMP3, REG_PV, disp);
668 M_AST(REG_ITMP3, REG_SP, JITSTACK + 4 * 8);
673 /* move machine code onto stack */
675 disp = dseg_add_s4(cd, mcode[0]);
676 M_ILD(REG_ITMP3, REG_PV, disp);
677 M_IST(REG_ITMP3, REG_SP, JITSTACK + 3 * 8);
679 disp = dseg_add_s4(cd, mcode[1]);
680 M_ILD(REG_ITMP3, REG_PV, disp);
681 M_IST(REG_ITMP3, REG_SP, JITSTACK + 3 * 8 + 4);
683 /* move class/method/field reference onto stack */
685 disp = dseg_add_address(cd, pref->ref);
686 M_ALD(REG_ITMP3, REG_PV, disp);
687 M_AST(REG_ITMP3, REG_SP, JITSTACK + 2 * 8);
689 /* move data segment displacement onto stack */
691 disp = dseg_add_s4(cd, pref->disp);
692 M_ILD(REG_ITMP3, REG_PV, disp);
693 M_IST(REG_ITMP3, REG_SP, JITSTACK + 1 * 8);
695 /* move patcher function pointer onto stack */
697 disp = dseg_add_functionptr(cd, pref->patcher);
698 M_ALD(REG_ITMP3, REG_PV, disp);
699 M_AST(REG_ITMP3, REG_SP, JITSTACK + 0 * 8);
701 if (targetdisp == 0) {
702 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
704 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
705 M_ALD(REG_ITMP3, REG_PV, disp);
706 M_JMP(REG_ZERO, REG_ITMP3, REG_ZERO);
710 disp = (((u4 *) cd->mcodebase) + targetdisp) -
711 (((u4 *) cd->mcodeptr));
720 /* emit_verbosecall_enter ******************************************************
722 Generates the code for the call trace.
724 *******************************************************************************/
727 void emit_verbosecall_enter(jitdata *jd)
737 /* get required compiler data */
745 /* mark trace code */
750 stackslots = 1 + FLT_ARG_CNT;
751 ALIGN_STACK_SLOTS(stackslots);
753 M_LDA(REG_SP, REG_SP, -(stackslots * 8));
755 /* save float argument registers */
757 for (i = 0; i < FLT_ARG_CNT; i++)
758 M_DST(abi_registers_float_argument[i], REG_SP, JITSTACK + (1 + i) * 8);
760 /* save temporary registers for leaf methods */
761 /* XXX no leaf optimization yet
762 if (code_is_leafmethod(code)) {
763 for (i = 0; i < INT_TMP_CNT; i++)
764 M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
766 for (i = 0; i < FLT_TMP_CNT; i++)
767 M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
770 /* load int/float arguments into integer argument registers */
772 for (i = 0; i < md->paramcount && i < INT_NATARG_CNT; i++) {
773 t = md->paramtypes[i].type;
775 /* all available argument registers used, which adds a little complexity */
777 if (IS_INT_LNG_TYPE(t)) {
778 if (i < INT_ARG_CNT) {
779 M_INTMOVE(REG_WINDOW_TRANSPOSE(abi_registers_integer_argument[i]),
780 abi_registers_integer_argument[i]);
784 M_LDX(REG_OUT5, REG_FP, JITSTACK);
788 if (i < FLT_ARG_CNT) {
790 /* reg -> mem -> reg */
792 if (IS_2_WORD_TYPE(t)) {
793 M_DST(abi_registers_float_argument[i], REG_SP, JITSTACK);
794 M_LDX(abi_registers_integer_argument[i], REG_SP, JITSTACK);
797 M_FST(abi_registers_float_argument[i], REG_SP, JITSTACK);
798 M_ILD(abi_registers_integer_argument[i], REG_SP, JITSTACK);
806 if (IS_2_WORD_TYPE(t)) {
807 M_LDX(REG_OUT5, REG_FP, JITSTACK);
810 M_ILD(REG_OUT5, REG_FP, JITSTACK);
817 /* method info pointer is passed via stack */
818 disp = dseg_add_address(cd, m);
819 M_ALD(REG_ITMP1, REG_PV_CALLEE, disp);
820 M_AST(REG_ITMP1, REG_SP, CSTACK);
821 disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
822 M_ALD(REG_ITMP1, REG_PV_CALLEE, disp);
823 M_JMP(REG_RA_CALLER, REG_ITMP1, REG_ZERO);
826 /* restore float argument registers */
828 for (i = 0; i < FLT_ARG_CNT; i++)
829 M_DLD(abi_registers_float_argument[i], REG_SP, JITSTACK + (1 + i) * 8);
831 /* restore temporary registers for leaf methods */
832 /* XXX no leaf optimization yet
833 if (code_is_leafmethod(code)) {
834 for (i = 0; i < INT_TMP_CNT; i++)
835 M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
837 for (i = 0; i < FLT_TMP_CNT; i++)
838 M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
841 M_LDA(REG_SP, REG_SP, stackslots * 8);
843 /* mark trace code */
847 #endif /* !defined(NDEBUG) */
850 /* emit_verbosecall_exit *******************************************************
852 Generates the code for the call trace.
854 *******************************************************************************/
857 void emit_verbosecall_exit(jitdata *jd)
864 /* get required compiler data */
870 /* mark trace code */
874 /* XXX jit-c-call (keep stack aligned)*/
875 M_LDA(REG_SP, REG_SP, -(2 * 8));
877 M_DST(REG_FRESULT, REG_SP, JITSTACK);
879 M_MOV(REG_RESULT_CALLEE, REG_OUT0);
880 M_DMOV(REG_FRESULT, 1); /* logical dreg 1 => f2 */
881 M_FMOV(REG_FRESULT, 2); /* logical freg 2 => f5 */
883 disp = dseg_add_functionptr(cd, m);
884 M_ALD(REG_OUT3, REG_PV_CALLEE, disp);
886 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
887 M_ALD(REG_ITMP3, REG_PV_CALLEE, disp);
888 M_JMP(REG_RA_CALLER, REG_ITMP3, REG_ZERO);
891 M_DLD(REG_FRESULT, REG_SP, JITSTACK);
893 M_LDA(REG_SP, REG_SP, 2 * 8);
895 /* mark trace code */
899 #endif /* !defined(NDEBUG) */
903 * These are local overrides for various environment variables in Emacs.
904 * Please do not remove this and leave it at the end of the file, where
905 * Emacs will automagically detect them.
906 * ---------------------------------------------------------------------
909 * indent-tabs-mode: t
913 * vim:noexpandtab:sw=4:ts=4: