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
25 $Id: emit.c 4398 2006-01-31 23:43:08Z twisti $
36 #include "vm/jit/sparc64/codegen.h"
37 #include "vm/jit/sparc64/md-abi.h"
38 #include "vm/jit/sparc64/emit.h"
40 #include "mm/memory.h"
42 #include "vm/exceptions.h"
43 #include "vm/stringlocal.h" /* XXX for gen_resolvebranch */
44 #include "vm/jit/abi.h"
45 #include "vm/jit/abi-asm.h"
46 #include "vm/jit/asmpart.h"
47 #include "vm/builtin.h"
48 #include "vm/jit/dseg.h"
49 #include "vm/jit/emit-common.h"
50 #include "vm/jit/jit.h"
51 #include "vm/jit/replace.h"
53 #include "vmcore/options.h"
55 #include "vm/jit/sparc64/solaris/macro_rename.h"
57 /* how to leaf optimization in the emitted stubs?? */
58 #define REG_PV REG_PV_CALLEE
61 /* emit_load *******************************************************************
63 Emits a possible load of an operand.
65 *******************************************************************************/
67 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
73 /* get required compiler data */
77 if (src->flags & INMEMORY) {
78 COUNT_READ_SPILLS(src)
80 disp = JITSTACK + src->vv.regoff;
87 M_LDX(tempreg, REG_SP, disp);
91 M_DLD(tempreg, REG_SP, disp);
94 vm_abort("emit_load: unknown type %d", src->type);
101 reg = src->vv.regoff;
107 /* emit_store ******************************************************************
109 Emits a possible store to variable.
111 *******************************************************************************/
113 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
118 /* get required compiler data */
122 if (dst->flags & INMEMORY) {
123 COUNT_WRITE_SPILLS(dst)
125 disp = JITSTACK + dst->vv.regoff;
132 M_STX(d, REG_SP, disp);
136 M_DST(d, REG_SP, disp);
139 vm_abort("emit_store: unknown type %d", dst->type);
146 /* emit_copy *******************************************************************
148 Generates a register/memory to register/memory copy.
150 *******************************************************************************/
152 void emit_copy(jitdata *jd, instruction *iptr)
159 /* get required compiler data */
163 /* get source and destination variables */
165 src = VAROP(iptr->s1);
166 dst = VAROP(iptr->dst);
168 if ((src->vv.regoff != dst->vv.regoff) ||
169 ((src->flags ^ dst->flags) & INMEMORY)) {
171 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
172 /* emit nothing, as the value won't be used anyway */
176 /* If one of the variables resides in memory, we can eliminate
177 the register move from/to the temporary register with the
178 order of getting the destination register and the load. */
180 if (IS_INMEMORY(src->flags)) {
181 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
182 s1 = emit_load(jd, iptr, src, d);
185 s1 = emit_load(jd, iptr, src, REG_IFTMP);
186 d = codegen_reg_of_var(iptr->opc, dst, s1);
201 vm_abort("emit_copy: unknown type %d", src->type);
206 emit_store(jd, iptr, dst, d);
211 /* emit_iconst *****************************************************************
215 *******************************************************************************/
217 void emit_iconst(codegendata *cd, s4 d, s4 value)
221 if ((value >= -4096) && (value <= 4095)) {
222 M_XOR_IMM(REG_ZERO, value, d);
224 disp = dseg_add_s4(cd, value);
225 M_ILD(d, REG_PV_CALLEE, disp);
230 /* emit_lconst *****************************************************************
234 *******************************************************************************/
236 void emit_lconst(codegendata *cd, s4 d, s8 value)
240 if ((value >= -4096) && (value <= 4095)) {
241 M_XOR_IMM(REG_ZERO, value, d);
243 disp = dseg_add_s8(cd, value);
244 M_LDX(d, REG_PV_CALLEE, disp);
248 /* emit_branch *****************************************************************
250 Emits the code for conditional and unconditional branchs.
252 *******************************************************************************/
254 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
258 /* calculate the different displacements */
260 branchdisp = disp >> 2;
262 /* check which branch to generate */
264 if (condition == BRANCH_UNCONDITIONAL) {
265 /* check displacement for overflow (19-bit)*/
267 if ((branchdisp < (s4) 0xfffc0000) || (branchdisp > (s4) 0x003ffff)) {
268 /* if the long-branches flag isn't set yet, do it */
270 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
271 cd->flags |= (CODEGENDATA_FLAG_ERROR |
272 CODEGENDATA_FLAG_LONGBRANCHES);
275 vm_abort("emit_branch: emit unconditional long-branch code");
282 else if (reg == -1) {
283 /* branch on condition codes */
285 /* check displacement for overflow (19-bit)*/
287 if ((branchdisp < (s4) 0xfffc0000) || (branchdisp > (s4) 0x003ffff)) {
288 /* if the long-branches flag isn't set yet, do it */
290 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
291 log_println("setting error");
292 cd->flags |= (CODEGENDATA_FLAG_ERROR |
293 CODEGENDATA_FLAG_LONGBRANCHES);
296 vm_abort("emit_branch: emit long-branch on cc code");
299 /* check whether to branch on 64-bit condition code */
300 if (BRANCH_CHECKS_XCC(opt)) {
327 vm_abort("emit_branch: unknown condition %d", condition);
360 vm_abort("emit_branch: unknown condition %d", condition);
369 /* branch on register */
371 /* check displacement for overflow (16-bit) */
373 if ((branchdisp < (s4) 0xffff8000) || (branchdisp > (s4) 0x0007fff)) {
374 /* if the long-branches flag isn't set yet, do it */
376 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
377 log_println("setting error");
378 cd->flags |= (CODEGENDATA_FLAG_ERROR |
379 CODEGENDATA_FLAG_LONGBRANCHES);
382 vm_abort("emit_branch: emit long-branch on reg code");
387 M_BEQZ(reg, branchdisp);
390 M_BNEZ(reg, branchdisp);
393 M_BLTZ(reg, branchdisp);
396 M_BGEZ(reg, branchdisp);
399 M_BGTZ(reg, branchdisp);
402 M_BLEZ(reg, branchdisp);
405 vm_abort("emit_branch: unknown condition %d", condition);
415 /* emit_bxx_xcc*****************************************************************
417 Wrappers for branches on 64-bit condition codes (SPARC specific).
419 *******************************************************************************/
421 void emit_beq_xcc(codegendata *cd, basicblock *target)
423 emit_bcc(cd, target, BRANCH_EQ, BRANCH_OPT_XCC);
426 void emit_bne_xcc(codegendata *cd, basicblock *target)
428 emit_bcc(cd, target, BRANCH_NE, BRANCH_OPT_XCC);
431 void emit_blt_xcc(codegendata *cd, basicblock *target)
433 emit_bcc(cd, target, BRANCH_LT, BRANCH_OPT_XCC);
436 void emit_bge_xcc(codegendata *cd, basicblock *target)
438 emit_bcc(cd, target, BRANCH_GE, BRANCH_OPT_XCC);
441 void emit_bgt_xcc(codegendata *cd, basicblock *target)
443 emit_bcc(cd, target, BRANCH_GT, BRANCH_OPT_XCC);
446 void emit_ble_xcc(codegendata *cd, basicblock *target)
448 emit_bcc(cd, target, BRANCH_LE, BRANCH_OPT_XCC);
455 /* emit_arithmetic_check *******************************************************
457 Emit an ArithmeticException check.
459 *******************************************************************************/
461 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
463 if (INSTRUCTION_MUST_CHECK(iptr)) {
466 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_ARITHMETIC);
471 /* emit_arrayindexoutofbounds_check ********************************************
473 Emit an ArrayIndexOutOfBoundsException check.
475 *******************************************************************************/
477 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
479 if (INSTRUCTION_MUST_CHECK(iptr)) {
480 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
481 M_CMP(s2, REG_ITMP3);
484 M_ALD_INTERN(s2, REG_ZERO, EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS);
489 /* emit_classcast_check ********************************************************
491 Emit a ClassCastException check.
493 *******************************************************************************/
495 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
497 /* XXX: use 64-bit or 32-bit compares??? */
499 if (INSTRUCTION_MUST_CHECK(iptr)) {
514 vm_abort("emit_classcast_check: unknown condition %d", condition);
518 M_ALD_INTERN(s1, REG_ZERO, EXCEPTION_HARDWARE_CLASSCAST);
523 /* emit_nullpointer_check ******************************************************
525 Emit a NullPointerException check.
527 *******************************************************************************/
529 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
531 if (INSTRUCTION_MUST_CHECK(iptr)) {
534 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_NULLPOINTER);
539 /* emit_exception_check ********************************************************
541 Emit an Exception check.
543 *******************************************************************************/
545 void emit_exception_check(codegendata *cd, instruction *iptr)
547 if (INSTRUCTION_MUST_CHECK(iptr)) {
548 M_BNEZ(REG_RESULT_CALLER, 3);
550 M_ALD_INTERN(REG_RESULT_CALLER, REG_ZERO, EXCEPTION_HARDWARE_EXCEPTION);
555 /* emit_trap *******************************************************************
557 Emit a trap instruction and return the original machine code.
559 *******************************************************************************/
561 uint32_t emit_trap(codegendata *cd)
565 /* Get machine code which is patched back in later. The
566 trap is 1 instruction word long. */
568 mcode = *((u4 *) cd->mcodeptr);
570 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_PATCHER);
576 /* emit_patcher_stubs **********************************************************
578 Generates the code for the patcher stubs.
580 *******************************************************************************/
582 void emit_patcher_stubs(jitdata *jd)
592 /* get required compiler data */
596 /* generate code patching stub call code */
600 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
601 /* check code segment size */
605 /* Get machine code which is patched back in later. The
606 call is 2 instruction words long. */
608 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
610 /* We use 2 loads here as an unaligned 8-byte read on 64-bit
611 SPARC causes a SIGSEGV */
613 mcode[0] = ((u4 *) tmpmcodeptr)[0];
614 mcode[1] = ((u4 *) tmpmcodeptr)[1];
616 /* Patch in the call to call the following code (done at
619 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
620 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
622 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) );
624 if ((disp < (s4) 0xfffc0000) || (disp > (s4) 0x003ffff)) {
625 vm_abort("Jump offset is out of range: %d > +/-%d",
633 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
635 /* extend stack frame for wrapper data */
637 M_ASUB_IMM(REG_SP, 6 * 8, REG_SP);
639 /* calculate return address and move it onto the stack */
641 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
642 M_AST(REG_ITMP3, REG_SP, JITSTACK + 5 * 8);
644 /* move pointer to java_objectheader onto stack */
646 #if defined(ENABLE_THREADS)
647 /* create a virtual java_objectheader */
649 (void) dseg_add_unique_address(cd, NULL); /* flcword */
650 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
651 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
653 M_LDA(REG_ITMP3, REG_PV, disp);
654 M_AST(REG_ITMP3, REG_SP, JITSTACK + 4 * 8);
659 /* move machine code onto stack */
661 disp = dseg_add_s4(cd, mcode[0]);
662 M_ILD(REG_ITMP3, REG_PV, disp);
663 M_IST(REG_ITMP3, REG_SP, JITSTACK + 3 * 8);
665 disp = dseg_add_s4(cd, mcode[1]);
666 M_ILD(REG_ITMP3, REG_PV, disp);
667 M_IST(REG_ITMP3, REG_SP, JITSTACK + 3 * 8 + 4);
669 /* move class/method/field reference onto stack */
671 disp = dseg_add_address(cd, pref->ref);
672 M_ALD(REG_ITMP3, REG_PV, disp);
673 M_AST(REG_ITMP3, REG_SP, JITSTACK + 2 * 8);
675 /* move data segment displacement onto stack */
677 disp = dseg_add_s4(cd, pref->disp);
678 M_ILD(REG_ITMP3, REG_PV, disp);
679 M_IST(REG_ITMP3, REG_SP, JITSTACK + 1 * 8);
681 /* move patcher function pointer onto stack */
683 disp = dseg_add_functionptr(cd, pref->patcher);
684 M_ALD(REG_ITMP3, REG_PV, disp);
685 M_AST(REG_ITMP3, REG_SP, JITSTACK + 0 * 8);
687 if (targetdisp == 0) {
688 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
690 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
691 M_ALD(REG_ITMP3, REG_PV, disp);
692 M_JMP(REG_ZERO, REG_ITMP3, REG_ZERO);
696 disp = (((u4 *) cd->mcodebase) + targetdisp) -
697 (((u4 *) cd->mcodeptr));
706 /* emit_verbosecall_enter ******************************************************
708 Generates the code for the call trace.
710 *******************************************************************************/
713 void emit_verbosecall_enter(jitdata *jd)
723 /* get required compiler data */
731 /* mark trace code */
736 stackslots = 1 + FLT_ARG_CNT;
737 ALIGN_STACK_SLOTS(stackslots);
739 M_LDA(REG_SP, REG_SP, -(stackslots * 8));
741 /* save float argument registers */
743 for (i = 0; i < FLT_ARG_CNT; i++)
744 M_DST(abi_registers_float_argument[i], REG_SP, JITSTACK + (1 + i) * 8);
746 /* save temporary registers for leaf methods */
747 /* XXX no leaf optimization yet
748 if (jd->isleafmethod) {
749 for (i = 0; i < INT_TMP_CNT; i++)
750 M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
752 for (i = 0; i < FLT_TMP_CNT; i++)
753 M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
756 /* load int/float arguments into integer argument registers */
758 for (i = 0; i < md->paramcount && i < INT_NATARG_CNT; i++) {
759 t = md->paramtypes[i].type;
761 /* all available argument registers used, which adds a little complexity */
763 if (IS_INT_LNG_TYPE(t)) {
764 if (i < INT_ARG_CNT) {
765 M_INTMOVE(REG_WINDOW_TRANSPOSE(abi_registers_integer_argument[i]),
766 abi_registers_integer_argument[i]);
770 M_LDX(REG_OUT5, REG_FP, JITSTACK);
774 if (i < FLT_ARG_CNT) {
776 /* reg -> mem -> reg */
778 if (IS_2_WORD_TYPE(t)) {
779 M_DST(abi_registers_float_argument[i], REG_SP, JITSTACK);
780 M_LDX(abi_registers_integer_argument[i], REG_SP, JITSTACK);
783 M_FST(abi_registers_float_argument[i], REG_SP, JITSTACK);
784 M_ILD(abi_registers_integer_argument[i], REG_SP, JITSTACK);
792 if (IS_2_WORD_TYPE(t)) {
793 M_LDX(REG_OUT5, REG_FP, JITSTACK);
796 M_ILD(REG_OUT5, REG_FP, JITSTACK);
803 /* method info pointer is passed via stack */
804 disp = dseg_add_address(cd, m);
805 M_ALD(REG_ITMP1, REG_PV_CALLEE, disp);
806 M_AST(REG_ITMP1, REG_SP, CSTACK);
807 disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
808 M_ALD(REG_ITMP1, REG_PV_CALLEE, disp);
809 M_JMP(REG_RA_CALLER, REG_ITMP1, REG_ZERO);
812 /* restore float argument registers */
814 for (i = 0; i < FLT_ARG_CNT; i++)
815 M_DLD(abi_registers_float_argument[i], REG_SP, JITSTACK + (1 + i) * 8);
817 /* restore temporary registers for leaf methods */
818 /* XXX no leaf optimization yet
819 if (jd->isleafmethod) {
820 for (i = 0; i < INT_TMP_CNT; i++)
821 M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
823 for (i = 0; i < FLT_TMP_CNT; i++)
824 M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
827 M_LDA(REG_SP, REG_SP, stackslots * 8);
829 /* mark trace code */
833 #endif /* !defined(NDEBUG) */
836 /* emit_verbosecall_exit *******************************************************
838 Generates the code for the call trace.
840 *******************************************************************************/
843 void emit_verbosecall_exit(jitdata *jd)
850 /* get required compiler data */
856 /* mark trace code */
860 /* XXX jit-c-call (keep stack aligned)*/
861 M_LDA(REG_SP, REG_SP, -(2 * 8));
863 M_DST(REG_FRESULT, REG_SP, JITSTACK);
865 M_MOV(REG_RESULT_CALLEE, REG_OUT0);
866 M_DMOV(REG_FRESULT, 1); /* logical dreg 1 => f2 */
867 M_FMOV(REG_FRESULT, 2); /* logical freg 2 => f5 */
869 disp = dseg_add_functionptr(cd, m);
870 M_ALD(REG_OUT3, REG_PV_CALLEE, disp);
872 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
873 M_ALD(REG_ITMP3, REG_PV_CALLEE, disp);
874 M_JMP(REG_RA_CALLER, REG_ITMP3, REG_ZERO);
877 M_DLD(REG_FRESULT, REG_SP, JITSTACK);
879 M_LDA(REG_SP, REG_SP, 2 * 8);
881 /* mark trace code */
885 #endif /* !defined(NDEBUG) */
889 * These are local overrides for various environment variables in Emacs.
890 * Please do not remove this and leave it at the end of the file, where
891 * Emacs will automagically detect them.
892 * ---------------------------------------------------------------------
895 * indent-tabs-mode: t
899 * vim:noexpandtab:sw=4:ts=4: