1 /* src/vm/jit/powerpc/emit.c - PowerPC 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 8211 2007-07-18 19:52:23Z michi $
39 #include "vm/jit/powerpc/codegen.h"
41 #include "mm/memory.h"
43 #include "threads/lock-common.h"
45 #include "vm/builtin.h"
46 #include "vm/exceptions.h"
48 #include "vm/jit/abi.h"
49 #include "vm/jit/asmpart.h"
50 #include "vm/jit/codegen-common.h"
51 #include "vm/jit/dseg.h"
52 #include "vm/jit/emit-common.h"
53 #include "vm/jit/jit.h"
54 #include "vm/jit/replace.h"
56 #include "vmcore/options.h"
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 (IS_INMEMORY(src->flags)) {
78 disp = src->vv.regoff;
83 M_ILD(tempreg, REG_SP, disp);
86 M_LLD(tempreg, REG_SP, disp);
89 M_FLD(tempreg, REG_SP, disp);
92 M_DLD(tempreg, REG_SP, disp);
95 vm_abort("emit_load: unknown type %d", src->type);
101 reg = src->vv.regoff;
107 /* emit_load_low ***************************************************************
109 Emits a possible load of the low 32-bits of an operand.
111 *******************************************************************************/
113 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
119 assert(src->type == TYPE_LNG);
121 /* get required compiler data */
125 if (IS_INMEMORY(src->flags)) {
128 disp = src->vv.regoff;
130 M_ILD(tempreg, REG_SP, disp + 4);
135 reg = GET_LOW_REG(src->vv.regoff);
141 /* emit_load_high **************************************************************
143 Emits a possible load of the high 32-bits of an operand.
145 *******************************************************************************/
147 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
153 assert(src->type == TYPE_LNG);
155 /* get required compiler data */
159 if (IS_INMEMORY(src->flags)) {
162 disp = src->vv.regoff;
164 M_ILD(tempreg, REG_SP, disp);
169 reg = GET_HIGH_REG(src->vv.regoff);
175 /* emit_store ******************************************************************
177 Emit a possible store for the given variable.
179 *******************************************************************************/
181 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
186 /* get required compiler data */
190 if (IS_INMEMORY(dst->flags)) {
193 disp = dst->vv.regoff;
198 M_IST(d, REG_SP, disp);
201 M_LST(d, REG_SP, disp);
204 M_FST(d, REG_SP, disp);
207 M_DST(d, REG_SP, disp);
210 vm_abort("emit_store: unknown type %d", dst->type);
216 /* emit_copy *******************************************************************
218 Generates a register/memory to register/memory copy.
220 *******************************************************************************/
222 void emit_copy(jitdata *jd, instruction *iptr)
229 /* get required compiler data */
233 /* get source and destination variables */
235 src = VAROP(iptr->s1);
236 dst = VAROP(iptr->dst);
238 if ((src->vv.regoff != dst->vv.regoff) ||
239 (IS_INMEMORY(src->flags ^ dst->flags))) {
241 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
242 /* emit nothing, as the value won't be used anyway */
246 /* If one of the variables resides in memory, we can eliminate
247 the register move from/to the temporary register with the
248 order of getting the destination register and the load. */
250 if (IS_INMEMORY(src->flags)) {
251 if (IS_LNG_TYPE(src->type))
252 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
254 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
256 s1 = emit_load(jd, iptr, src, d);
259 if (IS_LNG_TYPE(src->type))
260 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
262 s1 = emit_load(jd, iptr, src, REG_IFTMP);
264 d = codegen_reg_of_var(iptr->opc, dst, s1);
274 M_MOV(GET_LOW_REG(s1), GET_LOW_REG(d));
275 M_MOV(GET_HIGH_REG(s1), GET_HIGH_REG(d));
282 vm_abort("emit_copy: unknown type %d", src->type);
286 emit_store(jd, iptr, dst, d);
291 /* emit_iconst *****************************************************************
295 *******************************************************************************/
297 void emit_iconst(codegendata *cd, s4 d, s4 value)
301 if ((value >= -32768) && (value <= 32767))
302 M_LDA_INTERN(d, REG_ZERO, value);
304 disp = dseg_add_s4(cd, value);
305 M_ILD(d, REG_PV, disp);
310 /* emit_branch *****************************************************************
312 Emits the code for conditional and unconditional branchs.
314 *******************************************************************************/
316 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
321 /* calculate the different displacements */
323 checkdisp = disp + 4;
324 branchdisp = (disp - 4) >> 2;
326 /* check which branch to generate */
328 if (condition == BRANCH_UNCONDITIONAL) {
329 /* check displacement for overflow */
331 if ((checkdisp < (s4) 0xfe000000) || (checkdisp > (s4) 0x01fffffc)) {
332 /* if the long-branches flag isn't set yet, do it */
334 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
335 cd->flags |= (CODEGENDATA_FLAG_ERROR |
336 CODEGENDATA_FLAG_LONGBRANCHES);
339 vm_abort("emit_branch: emit unconditional long-branch code");
346 /* and displacement for overflow */
348 if ((checkdisp < (s4) 0xffff8000) || (checkdisp > (s4) 0x00007fff)) {
349 /* if the long-branches flag isn't set yet, do it */
351 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
352 cd->flags |= (CODEGENDATA_FLAG_ERROR |
353 CODEGENDATA_FLAG_LONGBRANCHES);
382 vm_abort("emit_branch: long BRANCH_NAN");
385 vm_abort("emit_branch: unknown condition %d", condition);
412 vm_abort("emit_branch: unknown condition %d", condition);
419 /* emit_arithmetic_check *******************************************************
421 Emit an ArithmeticException check.
423 *******************************************************************************/
425 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
427 if (INSTRUCTION_MUST_CHECK(iptr)) {
430 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_ARITHMETIC);
435 /* emit_arrayindexoutofbounds_check ********************************************
437 Emit a ArrayIndexOutOfBoundsException check.
439 *******************************************************************************/
441 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
443 if (INSTRUCTION_MUST_CHECK(iptr)) {
444 M_ILD(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
445 M_TRAPGEU(s2, REG_ITMP3);
450 /* emit_classcast_check ********************************************************
452 Emit a ClassCastException check.
454 *******************************************************************************/
456 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
458 if (INSTRUCTION_MUST_CHECK(iptr)) {
470 vm_abort("emit_classcast_check: unknown condition %d", condition);
472 M_ALD_INTERN(s1, REG_ZERO, EXCEPTION_HARDWARE_CLASSCAST);
477 /* emit_nullpointer_check ******************************************************
479 Emit a NullPointerException check.
481 *******************************************************************************/
483 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
485 if (INSTRUCTION_MUST_CHECK(iptr)) {
488 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_NULLPOINTER);
493 /* emit_exception_check ********************************************************
495 Emit an Exception check.
497 *******************************************************************************/
499 void emit_exception_check(codegendata *cd, instruction *iptr)
501 if (INSTRUCTION_MUST_CHECK(iptr)) {
504 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_EXCEPTION);
509 /* emit_patcher_stubs **********************************************************
511 Generates the code for the patcher stubs.
513 *******************************************************************************/
515 void emit_patcher_stubs(jitdata *jd)
525 /* get required compiler data */
529 /* generate code patching stub call code */
533 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
534 /* check code segment size */
538 /* Get machine code which is patched back in later. The
539 call is 1 instruction word long. */
541 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
543 mcode = *((u4 *) tmpmcodeptr);
545 /* Patch in the call to call the following code (done at
548 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
549 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
551 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
554 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
556 /* create stack frame - keep stack 16-byte aligned */
558 M_AADD_IMM(REG_SP, -8 * 4, REG_SP);
560 /* calculate return address and move it onto the stack */
562 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
563 M_AST_INTERN(REG_ITMP3, REG_SP, 5 * 4);
565 /* move pointer to java_objectheader onto stack */
567 #if defined(ENABLE_THREADS)
568 /* order reversed because of data segment layout */
570 (void) dseg_add_unique_address(cd, NULL); /* flcword */
571 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
572 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
574 M_LDA(REG_ITMP3, REG_PV, disp);
575 M_AST_INTERN(REG_ITMP3, REG_SP, 4 * 4);
580 /* move machine code onto stack */
582 disp = dseg_add_s4(cd, mcode);
583 M_ILD(REG_ITMP3, REG_PV, disp);
584 M_IST_INTERN(REG_ITMP3, REG_SP, 3 * 4);
586 /* move class/method/field reference onto stack */
588 disp = dseg_add_address(cd, pref->ref);
589 M_ALD(REG_ITMP3, REG_PV, disp);
590 M_AST_INTERN(REG_ITMP3, REG_SP, 2 * 4);
592 /* move data segment displacement onto stack */
594 disp = dseg_add_s4(cd, pref->disp);
595 M_ILD(REG_ITMP3, REG_PV, disp);
596 M_IST_INTERN(REG_ITMP3, REG_SP, 1 * 4);
598 /* move patcher function pointer onto stack */
600 disp = dseg_add_functionptr(cd, pref->patcher);
601 M_ALD(REG_ITMP3, REG_PV, disp);
602 M_AST_INTERN(REG_ITMP3, REG_SP, 0 * 4);
604 if (targetdisp == 0) {
605 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
607 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
608 M_ALD(REG_ITMP3, REG_PV, disp);
613 disp = (((u4 *) cd->mcodebase) + targetdisp) -
614 (((u4 *) cd->mcodeptr) + 1);
621 /* emit_verbosecall_enter ******************************************************
623 Generates the code for the call trace.
625 *******************************************************************************/
627 void emit_verbosecall_enter(jitdata *jd)
639 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
642 /* get required compiler data */
650 /* mark trace code */
655 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
656 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + ARG_CNT + TMP_CNT) * 8));
658 M_CLR(REG_ITMP1); /* prepare a "zero" register */
660 /* save argument registers */
662 for (i = 0; i < md->paramcount; i++) {
663 if (!md->params[i].inmemory) {
664 s = md->params[i].regoff;
665 d = LA_SIZE + (1 + i) * 8;
667 switch (md->paramtypes[i].type) {
670 M_IST(REG_ITMP1, REG_SP, d); /* high-bits are zero */
671 M_IST(s, REG_SP, d + 4);
677 M_IST(REG_ITMP1, REG_SP, d); /* high-bits are zero */
678 M_FST(s, REG_SP, d + 4);
687 /* load arguments as longs */
691 for (i = 0; i < md->paramcount && i < TRACE_ARGS_NUM; i++) {
692 s = LA_SIZE + (1 + i) * 8;
693 x = PACK_REGS(abi_registers_integer_argument[d + 1],
694 abi_registers_integer_argument[d]);
701 /* put methodinfo pointer as last argument on the stack */
703 disp = dseg_add_address(cd, m);
704 M_ALD(REG_ITMP1, REG_PV, disp);
705 #if defined(__DARWIN__)
706 M_AST(REG_ITMP1, REG_SP, LA_SIZE + TRACE_ARGS_NUM * 8);
708 M_AST(REG_ITMP1, REG_SP, LA_SIZE);
710 disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
711 M_ALD(REG_ITMP2, REG_PV, disp);
715 /* restore argument registers */
717 for (i = 0; i < md->paramcount; i++) {
718 if (!md->params[i].inmemory) {
719 s = LA_SIZE + (1 + i) * 8;
720 d = md->params[i].regoff;
722 switch (md->paramtypes[i].type) {
725 M_ILD(d, REG_SP, s + 4); /* get low-bits */
731 M_FLD(d, REG_SP, s + 4); /* get low-bits */
740 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + ARG_CNT + TMP_CNT) * 8 + LA_LR_OFFSET);
742 M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + ARG_CNT + TMP_CNT) * 8);
744 /* mark trace code */
747 #endif /* !defined(NDEBUG) */
751 /* emit_verbosecall_exit *******************************************************
753 Generates the code for the call trace.
755 void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
757 *******************************************************************************/
759 void emit_verbosecall_exit(jitdata *jd)
768 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
771 /* get required compiler data */
779 /* mark trace code */
784 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
785 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4));
787 /* save return registers */
789 M_LST(REG_RESULT_PACKED, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 0) * 4);
790 M_DST(REG_FRESULT, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 2) * 4);
792 /* keep this order */
793 switch (md->returntype.type) {
796 M_INTMOVE(REG_RESULT, REG_A1);
801 M_LNGMOVE(REG_RESULT_PACKED, REG_A0_A1_PACKED);
805 M_FLTMOVE(REG_FRESULT, REG_FA0);
806 M_FLTMOVE(REG_FRESULT, REG_FA1);
808 disp = dseg_add_address(cd, m);
809 M_ALD(REG_A2, REG_PV, disp);
811 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
812 M_ALD(REG_ITMP2, REG_PV, disp);
816 /* restore return registers */
818 M_LLD(REG_RESULT_PACKED, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 0) * 4);
819 M_DLD(REG_FRESULT, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 2) * 4);
821 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4 + LA_LR_OFFSET);
823 M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4);
825 /* mark trace code */
828 #endif /* !defined(NDEBUG) */
833 * These are local overrides for various environment variables in Emacs.
834 * Please do not remove this and leave it at the end of the file, where
835 * Emacs will automagically detect them.
836 * ---------------------------------------------------------------------
839 * indent-tabs-mode: t
843 * vim:noexpandtab:sw=4:ts=4: