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 /* 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) {
78 disp = JITSTACK + src->vv.regoff * 8;
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) {
123 disp = JITSTACK + dst->vv.regoff * 8;
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, EXCEPTION_HARDWARE_ARITHMETIC);
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_arrayheader, size));
479 M_CMP(s2, REG_ITMP3);
482 M_ALD_INTERN(s2, REG_ZERO, EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS);
487 /* emit_classcast_check ********************************************************
489 Emit a ClassCastException check.
491 *******************************************************************************/
493 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
495 /* XXX: use 64-bit or 32-bit compares??? */
497 if (INSTRUCTION_MUST_CHECK(iptr)) {
512 vm_abort("emit_classcast_check: unknown condition %d", condition);
516 M_ALD_INTERN(s1, REG_ZERO, EXCEPTION_HARDWARE_CLASSCAST);
521 /* emit_nullpointer_check ******************************************************
523 Emit a NullPointerException check.
525 *******************************************************************************/
527 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
529 if (INSTRUCTION_MUST_CHECK(iptr)) {
532 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_NULLPOINTER);
537 /* emit_exception_check ********************************************************
539 Emit an Exception check.
541 *******************************************************************************/
543 void emit_exception_check(codegendata *cd, instruction *iptr)
545 if (INSTRUCTION_MUST_CHECK(iptr)) {
546 M_BNEZ(REG_RESULT_CALLER, 3);
548 M_ALD_INTERN(REG_RESULT_CALLER, REG_ZERO, EXCEPTION_HARDWARE_EXCEPTION);
553 /* emit_patcher_stubs **********************************************************
555 Generates the code for the patcher stubs.
557 *******************************************************************************/
559 void emit_patcher_stubs(jitdata *jd)
569 /* get required compiler data */
573 /* generate code patching stub call code */
577 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
578 /* check code segment size */
582 /* Get machine code which is patched back in later. The
583 call is 2 instruction words long. */
585 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
587 /* We use 2 loads here as an unaligned 8-byte read on 64-bit
588 SPARC causes a SIGSEGV */
590 mcode[0] = ((u4 *) tmpmcodeptr)[0];
591 mcode[1] = ((u4 *) tmpmcodeptr)[1];
593 /* Patch in the call to call the following code (done at
596 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
597 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
599 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) );
601 if ((disp < (s4) 0xfffc0000) || (disp > (s4) 0x003ffff)) {
602 vm_abort("Jump offset is out of range: %d > +/-%d",
610 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
612 /* extend stack frame for wrapper data */
614 M_ASUB_IMM(REG_SP, 6 * 8, REG_SP);
616 /* calculate return address and move it onto the stack */
618 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
619 M_AST(REG_ITMP3, REG_SP, JITSTACK + 5 * 8);
621 /* move pointer to java_objectheader onto stack */
623 #if defined(ENABLE_THREADS)
624 /* create a virtual java_objectheader */
626 (void) dseg_add_unique_address(cd, NULL); /* flcword */
627 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
628 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
630 M_LDA(REG_ITMP3, REG_PV, disp);
631 M_AST(REG_ITMP3, REG_SP, JITSTACK + 4 * 8);
636 /* move machine code onto stack */
638 disp = dseg_add_s4(cd, mcode[0]);
639 M_ILD(REG_ITMP3, REG_PV, disp);
640 M_IST(REG_ITMP3, REG_SP, JITSTACK + 3 * 8);
642 disp = dseg_add_s4(cd, mcode[1]);
643 M_ILD(REG_ITMP3, REG_PV, disp);
644 M_IST(REG_ITMP3, REG_SP, JITSTACK + 3 * 8 + 4);
646 /* move class/method/field reference onto stack */
648 disp = dseg_add_address(cd, pref->ref);
649 M_ALD(REG_ITMP3, REG_PV, disp);
650 M_AST(REG_ITMP3, REG_SP, JITSTACK + 2 * 8);
652 /* move data segment displacement onto stack */
654 disp = dseg_add_s4(cd, pref->disp);
655 M_ILD(REG_ITMP3, REG_PV, disp);
656 M_IST(REG_ITMP3, REG_SP, JITSTACK + 1 * 8);
658 /* move patcher function pointer onto stack */
660 disp = dseg_add_functionptr(cd, pref->patcher);
661 M_ALD(REG_ITMP3, REG_PV, disp);
662 M_AST(REG_ITMP3, REG_SP, JITSTACK + 0 * 8);
664 if (targetdisp == 0) {
665 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
667 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
668 M_ALD(REG_ITMP3, REG_PV, disp);
669 M_JMP(REG_ZERO, REG_ITMP3, REG_ZERO);
673 disp = (((u4 *) cd->mcodebase) + targetdisp) -
674 (((u4 *) cd->mcodeptr));
683 /* emit_replacement_stubs ******************************************************
685 Generates the code for the replacement stubs.
687 *******************************************************************************/
689 #if defined(ENABLE_REPLACEMENT)
690 void emit_replacement_stubs(jitdata *jd)
693 #endif /* defined(ENABLE_REPLACEMENT) */
695 /* emit_verbosecall_enter ******************************************************
697 Generates the code for the call trace.
699 *******************************************************************************/
702 void emit_verbosecall_enter(jitdata *jd)
711 /* get required compiler data */
719 /* mark trace code */
724 M_LDA(REG_SP, REG_SP, -(1 + FLT_ARG_CNT) * 8);
726 /* save float argument registers */
728 for (i = 0; i < FLT_ARG_CNT; i++)
729 M_DST(abi_registers_float_argument[i], REG_SP, JITSTACK + (1 + i) * 8);
731 /* save temporary registers for leaf methods */
732 /* XXX no leaf optimization yet
733 if (jd->isleafmethod) {
734 for (i = 0; i < INT_TMP_CNT; i++)
735 M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
737 for (i = 0; i < FLT_TMP_CNT; i++)
738 M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
741 /* load int/float arguments into integer argument registers */
743 for (i = 0; i < md->paramcount && i < INT_NATARG_CNT; i++) {
744 t = md->paramtypes[i].type;
746 /* all available argument registers used, which adds a little complexity */
748 if (IS_INT_LNG_TYPE(t)) {
749 if (i < INT_ARG_CNT) {
750 M_INTMOVE(REG_WINDOW_TRANSPOSE(abi_registers_integer_argument[i]),
751 abi_registers_integer_argument[i]);
755 M_LDX(REG_OUT5, REG_FP, JITSTACK);
759 if (i < FLT_ARG_CNT) {
761 /* reg -> mem -> reg */
763 if (IS_2_WORD_TYPE(t)) {
764 M_DST(abi_registers_float_argument[i], REG_SP, JITSTACK);
765 M_LDX(abi_registers_integer_argument[i], REG_SP, JITSTACK);
768 M_FST(abi_registers_float_argument[i], REG_SP, JITSTACK);
769 M_ILD(abi_registers_integer_argument[i], REG_SP, JITSTACK);
777 if (IS_2_WORD_TYPE(t)) {
778 M_LDX(REG_OUT5, REG_FP, JITSTACK);
781 M_ILD(REG_OUT5, REG_FP, JITSTACK);
788 /* method info pointer is passed via stack */
789 disp = dseg_add_address(cd, m);
790 M_ALD(REG_ITMP1, REG_PV_CALLEE, disp);
791 M_AST(REG_ITMP1, REG_SP, CSTACK);
792 disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
793 M_ALD(REG_ITMP1, REG_PV_CALLEE, disp);
794 M_JMP(REG_RA_CALLER, REG_ITMP1, REG_ZERO);
797 /* restore float argument registers */
799 for (i = 0; i < FLT_ARG_CNT; i++)
800 M_DLD(abi_registers_float_argument[i], REG_SP, JITSTACK + (1 + i) * 8);
802 /* restore temporary registers for leaf methods */
803 /* XXX no leaf optimization yet
804 if (jd->isleafmethod) {
805 for (i = 0; i < INT_TMP_CNT; i++)
806 M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
808 for (i = 0; i < FLT_TMP_CNT; i++)
809 M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
812 M_LDA(REG_SP, REG_SP, (1 + FLT_ARG_CNT) * 8);
814 /* mark trace code */
818 #endif /* !defined(NDEBUG) */
821 /* emit_verbosecall_exit *******************************************************
823 Generates the code for the call trace.
825 *******************************************************************************/
828 void emit_verbosecall_exit(jitdata *jd)
835 /* get required compiler data */
841 /* mark trace code */
846 M_LDA(REG_SP, REG_SP, -(1 * 8));
848 M_DST(REG_FRESULT, REG_SP, JITSTACK);
850 M_MOV(REG_RESULT_CALLEE, REG_OUT0);
851 M_DMOV(REG_FRESULT, 1); /* logical dreg 1 => f2 */
852 M_FMOV(REG_FRESULT, 2); /* logical freg 2 => f5 */
854 disp = dseg_add_functionptr(cd, m);
855 M_ALD(REG_OUT3, REG_PV_CALLEE, disp);
857 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
858 M_ALD(REG_ITMP3, REG_PV_CALLEE, disp);
859 M_JMP(REG_RA_CALLER, REG_ITMP3, REG_ZERO);
862 M_DLD(REG_FRESULT, REG_SP, JITSTACK);
864 M_LDA(REG_SP, REG_SP, 1 * 8);
866 /* mark trace code */
870 #endif /* !defined(NDEBUG) */
874 * These are local overrides for various environment variables in Emacs.
875 * Please do not remove this and leave it at the end of the file, where
876 * Emacs will automagically detect them.
877 * ---------------------------------------------------------------------
880 * indent-tabs-mode: t
884 * vim:noexpandtab:sw=4:ts=4: