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_arrayheader, 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_patcher_stubs **********************************************************
557 Generates the code for the patcher stubs.
559 *******************************************************************************/
561 void emit_patcher_stubs(jitdata *jd)
571 /* get required compiler data */
575 /* generate code patching stub call code */
579 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
580 /* check code segment size */
584 /* Get machine code which is patched back in later. The
585 call is 2 instruction words long. */
587 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
589 /* We use 2 loads here as an unaligned 8-byte read on 64-bit
590 SPARC causes a SIGSEGV */
592 mcode[0] = ((u4 *) tmpmcodeptr)[0];
593 mcode[1] = ((u4 *) tmpmcodeptr)[1];
595 /* Patch in the call to call the following code (done at
598 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
599 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
601 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) );
603 if ((disp < (s4) 0xfffc0000) || (disp > (s4) 0x003ffff)) {
604 vm_abort("Jump offset is out of range: %d > +/-%d",
612 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
614 /* extend stack frame for wrapper data */
616 M_ASUB_IMM(REG_SP, 6 * 8, REG_SP);
618 /* calculate return address and move it onto the stack */
620 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
621 M_AST(REG_ITMP3, REG_SP, JITSTACK + 5 * 8);
623 /* move pointer to java_objectheader onto stack */
625 #if defined(ENABLE_THREADS)
626 /* create a virtual java_objectheader */
628 (void) dseg_add_unique_address(cd, NULL); /* flcword */
629 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
630 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
632 M_LDA(REG_ITMP3, REG_PV, disp);
633 M_AST(REG_ITMP3, REG_SP, JITSTACK + 4 * 8);
638 /* move machine code onto stack */
640 disp = dseg_add_s4(cd, mcode[0]);
641 M_ILD(REG_ITMP3, REG_PV, disp);
642 M_IST(REG_ITMP3, REG_SP, JITSTACK + 3 * 8);
644 disp = dseg_add_s4(cd, mcode[1]);
645 M_ILD(REG_ITMP3, REG_PV, disp);
646 M_IST(REG_ITMP3, REG_SP, JITSTACK + 3 * 8 + 4);
648 /* move class/method/field reference onto stack */
650 disp = dseg_add_address(cd, pref->ref);
651 M_ALD(REG_ITMP3, REG_PV, disp);
652 M_AST(REG_ITMP3, REG_SP, JITSTACK + 2 * 8);
654 /* move data segment displacement onto stack */
656 disp = dseg_add_s4(cd, pref->disp);
657 M_ILD(REG_ITMP3, REG_PV, disp);
658 M_IST(REG_ITMP3, REG_SP, JITSTACK + 1 * 8);
660 /* move patcher function pointer onto stack */
662 disp = dseg_add_functionptr(cd, pref->patcher);
663 M_ALD(REG_ITMP3, REG_PV, disp);
664 M_AST(REG_ITMP3, REG_SP, JITSTACK + 0 * 8);
666 if (targetdisp == 0) {
667 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
669 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
670 M_ALD(REG_ITMP3, REG_PV, disp);
671 M_JMP(REG_ZERO, REG_ITMP3, REG_ZERO);
675 disp = (((u4 *) cd->mcodebase) + targetdisp) -
676 (((u4 *) cd->mcodeptr));
685 /* emit_verbosecall_enter ******************************************************
687 Generates the code for the call trace.
689 *******************************************************************************/
692 void emit_verbosecall_enter(jitdata *jd)
702 /* get required compiler data */
710 /* mark trace code */
715 stackslots = 1 + FLT_ARG_CNT;
716 ALIGN_STACK_SLOTS(stackslots);
718 M_LDA(REG_SP, REG_SP, -(stackslots * 8));
720 /* save float argument registers */
722 for (i = 0; i < FLT_ARG_CNT; i++)
723 M_DST(abi_registers_float_argument[i], REG_SP, JITSTACK + (1 + i) * 8);
725 /* save temporary registers for leaf methods */
726 /* XXX no leaf optimization yet
727 if (jd->isleafmethod) {
728 for (i = 0; i < INT_TMP_CNT; i++)
729 M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
731 for (i = 0; i < FLT_TMP_CNT; i++)
732 M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
735 /* load int/float arguments into integer argument registers */
737 for (i = 0; i < md->paramcount && i < INT_NATARG_CNT; i++) {
738 t = md->paramtypes[i].type;
740 /* all available argument registers used, which adds a little complexity */
742 if (IS_INT_LNG_TYPE(t)) {
743 if (i < INT_ARG_CNT) {
744 M_INTMOVE(REG_WINDOW_TRANSPOSE(abi_registers_integer_argument[i]),
745 abi_registers_integer_argument[i]);
749 M_LDX(REG_OUT5, REG_FP, JITSTACK);
753 if (i < FLT_ARG_CNT) {
755 /* reg -> mem -> reg */
757 if (IS_2_WORD_TYPE(t)) {
758 M_DST(abi_registers_float_argument[i], REG_SP, JITSTACK);
759 M_LDX(abi_registers_integer_argument[i], REG_SP, JITSTACK);
762 M_FST(abi_registers_float_argument[i], REG_SP, JITSTACK);
763 M_ILD(abi_registers_integer_argument[i], REG_SP, JITSTACK);
771 if (IS_2_WORD_TYPE(t)) {
772 M_LDX(REG_OUT5, REG_FP, JITSTACK);
775 M_ILD(REG_OUT5, REG_FP, JITSTACK);
782 /* method info pointer is passed via stack */
783 disp = dseg_add_address(cd, m);
784 M_ALD(REG_ITMP1, REG_PV_CALLEE, disp);
785 M_AST(REG_ITMP1, REG_SP, CSTACK);
786 disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
787 M_ALD(REG_ITMP1, REG_PV_CALLEE, disp);
788 M_JMP(REG_RA_CALLER, REG_ITMP1, REG_ZERO);
791 /* restore float argument registers */
793 for (i = 0; i < FLT_ARG_CNT; i++)
794 M_DLD(abi_registers_float_argument[i], REG_SP, JITSTACK + (1 + i) * 8);
796 /* restore temporary registers for leaf methods */
797 /* XXX no leaf optimization yet
798 if (jd->isleafmethod) {
799 for (i = 0; i < INT_TMP_CNT; i++)
800 M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
802 for (i = 0; i < FLT_TMP_CNT; i++)
803 M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
806 M_LDA(REG_SP, REG_SP, stackslots * 8);
808 /* mark trace code */
812 #endif /* !defined(NDEBUG) */
815 /* emit_verbosecall_exit *******************************************************
817 Generates the code for the call trace.
819 *******************************************************************************/
822 void emit_verbosecall_exit(jitdata *jd)
829 /* get required compiler data */
835 /* mark trace code */
839 /* XXX jit-c-call (keep stack aligned)*/
840 M_LDA(REG_SP, REG_SP, -(2 * 8));
842 M_DST(REG_FRESULT, REG_SP, JITSTACK);
844 M_MOV(REG_RESULT_CALLEE, REG_OUT0);
845 M_DMOV(REG_FRESULT, 1); /* logical dreg 1 => f2 */
846 M_FMOV(REG_FRESULT, 2); /* logical freg 2 => f5 */
848 disp = dseg_add_functionptr(cd, m);
849 M_ALD(REG_OUT3, REG_PV_CALLEE, disp);
851 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
852 M_ALD(REG_ITMP3, REG_PV_CALLEE, disp);
853 M_JMP(REG_RA_CALLER, REG_ITMP3, REG_ZERO);
856 M_DLD(REG_FRESULT, REG_SP, JITSTACK);
858 M_LDA(REG_SP, REG_SP, 2 * 8);
860 /* mark trace code */
864 #endif /* !defined(NDEBUG) */
868 * These are local overrides for various environment variables in Emacs.
869 * Please do not remove this and leave it at the end of the file, where
870 * Emacs will automagically detect them.
871 * ---------------------------------------------------------------------
874 * indent-tabs-mode: t
878 * vim:noexpandtab:sw=4:ts=4: