1 /* src/vm/jit/powerpc/emit.c - PowerPC code emitter functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
35 #include "vm/jit/powerpc/codegen.h"
37 #include "mm/memory.hpp"
39 #include "threads/lock.hpp"
41 #include "vm/options.h"
43 #include "vm/jit/abi.h"
44 #include "vm/jit/asmpart.h"
45 #include "vm/jit/codegen-common.hpp"
46 #include "vm/jit/dseg.h"
47 #include "vm/jit/emit-common.hpp"
48 #include "vm/jit/jit.hpp"
49 #include "vm/jit/replace.hpp"
50 #include "vm/jit/trace.hpp"
51 #include "vm/jit/trap.hpp"
54 /* emit_load *******************************************************************
56 Emits a possible load of an operand.
58 *******************************************************************************/
60 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
66 /* get required compiler data */
70 if (IS_INMEMORY(src->flags)) {
73 disp = src->vv.regoff;
78 M_ILD(tempreg, REG_SP, disp);
81 M_LLD(tempreg, REG_SP, disp);
85 M_DLD(tempreg, REG_SP, disp);
88 vm_abort("emit_load: unknown type %d", src->type);
100 /* emit_load_low ***************************************************************
102 Emits a possible load of the low 32-bits of an operand.
104 *******************************************************************************/
106 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
112 assert(src->type == TYPE_LNG);
114 /* get required compiler data */
118 if (IS_INMEMORY(src->flags)) {
121 disp = src->vv.regoff;
123 M_ILD(tempreg, REG_SP, disp + 4);
128 reg = GET_LOW_REG(src->vv.regoff);
134 /* emit_load_high **************************************************************
136 Emits a possible load of the high 32-bits of an operand.
138 *******************************************************************************/
140 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
146 assert(src->type == TYPE_LNG);
148 /* get required compiler data */
152 if (IS_INMEMORY(src->flags)) {
155 disp = src->vv.regoff;
157 M_ILD(tempreg, REG_SP, disp);
162 reg = GET_HIGH_REG(src->vv.regoff);
168 /* emit_store ******************************************************************
170 Emit a possible store for the given variable.
172 *******************************************************************************/
174 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
179 /* get required compiler data */
183 if (IS_INMEMORY(dst->flags)) {
186 disp = dst->vv.regoff;
191 M_IST(d, REG_SP, disp);
194 M_LST(d, REG_SP, disp);
198 M_DST(d, REG_SP, disp);
201 vm_abort("emit_store: unknown type %d", dst->type);
207 /* emit_copy *******************************************************************
209 Generates a register/memory to register/memory copy.
211 *******************************************************************************/
213 void emit_copy(jitdata *jd, instruction *iptr)
220 /* get required compiler data */
224 /* get source and destination variables */
226 src = VAROP(iptr->s1);
227 dst = VAROP(iptr->dst);
229 if ((src->vv.regoff != dst->vv.regoff) ||
230 (IS_INMEMORY(src->flags ^ dst->flags))) {
232 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
233 /* emit nothing, as the value won't be used anyway */
237 /* If one of the variables resides in memory, we can eliminate
238 the register move from/to the temporary register with the
239 order of getting the destination register and the load. */
241 if (IS_INMEMORY(src->flags)) {
242 if (IS_LNG_TYPE(src->type))
243 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
245 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
247 s1 = emit_load(jd, iptr, src, d);
250 if (IS_LNG_TYPE(src->type))
251 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
253 s1 = emit_load(jd, iptr, src, REG_IFTMP);
255 d = codegen_reg_of_var(iptr->opc, dst, s1);
265 M_MOV(GET_LOW_REG(s1), GET_LOW_REG(d));
266 M_MOV(GET_HIGH_REG(s1), GET_HIGH_REG(d));
273 vm_abort("emit_copy: unknown type %d", src->type);
277 emit_store(jd, iptr, dst, d);
282 /* emit_iconst *****************************************************************
286 *******************************************************************************/
288 void emit_iconst(codegendata *cd, s4 d, s4 value)
292 if ((value >= -32768) && (value <= 32767))
293 M_LDA_INTERN(d, REG_ZERO, value);
295 disp = dseg_add_s4(cd, value);
296 M_ILD(d, REG_PV, disp);
302 * Emits code updating the condition register by comparing one integer
303 * register to an immediate integer value.
305 void emit_icmp_imm(codegendata* cd, int reg, int32_t value)
309 if ((value >= -32768) && (value <= 32767)) {
313 assert(reg != REG_ITMP2);
314 disp = dseg_add_s4(cd, value);
315 M_ILD(REG_ITMP2, REG_PV, disp);
316 M_CMP(reg, REG_ITMP2);
321 /* emit_branch *****************************************************************
323 Emits the code for conditional and unconditional branchs.
325 *******************************************************************************/
327 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
332 /* calculate the different displacements */
334 checkdisp = disp + 4;
335 branchdisp = (disp - 4) >> 2;
337 /* check which branch to generate */
339 if (condition == BRANCH_UNCONDITIONAL) {
340 /* check displacement for overflow */
342 if ((checkdisp < (s4) 0xfe000000) || (checkdisp > (s4) 0x01fffffc)) {
343 /* if the long-branches flag isn't set yet, do it */
345 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
346 cd->flags |= (CODEGENDATA_FLAG_ERROR |
347 CODEGENDATA_FLAG_LONGBRANCHES);
350 vm_abort("emit_branch: emit unconditional long-branch code");
357 /* and displacement for overflow */
359 if ((checkdisp < (s4) 0xffff8000) || (checkdisp > (s4) 0x00007fff)) {
360 /* if the long-branches flag isn't set yet, do it */
362 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
363 cd->flags |= (CODEGENDATA_FLAG_ERROR |
364 CODEGENDATA_FLAG_LONGBRANCHES);
367 // Subtract 1 instruction from the displacement as the
368 // actual branch is the second instruction.
369 checkdisp = checkdisp - 4;
370 branchdisp = branchdisp - 1;
372 if ((checkdisp < (int32_t) 0xfe000000) || (checkdisp > (int32_t) 0x01fffffc)) {
373 vm_abort("emit_branch: emit conditional long-branch code");
402 vm_abort("emit_branch: long BRANCH_NAN");
405 vm_abort("emit_branch: unknown condition %d", condition);
433 vm_abort("emit_branch: unknown condition %d", condition);
440 /* emit_arithmetic_check *******************************************************
442 Emit an ArithmeticException check.
444 *******************************************************************************/
446 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
448 if (INSTRUCTION_MUST_CHECK(iptr)) {
451 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_ArithmeticException);
456 /* emit_arrayindexoutofbounds_check ********************************************
458 Emit a ArrayIndexOutOfBoundsException check.
460 *******************************************************************************/
462 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
464 if (INSTRUCTION_MUST_CHECK(iptr)) {
465 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
466 M_TRAPGEU(s2, REG_ITMP3);
471 /* emit_arraystore_check *******************************************************
473 Emit an ArrayStoreException check.
475 *******************************************************************************/
477 void emit_arraystore_check(codegendata *cd, instruction *iptr)
479 if (INSTRUCTION_MUST_CHECK(iptr)) {
482 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_ArrayStoreException);
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 if (INSTRUCTION_MUST_CHECK(iptr)) {
510 vm_abort("emit_classcast_check: unknown condition %d", condition);
512 M_ALD_INTERN(s1, REG_ZERO, TRAP_ClassCastException);
517 /* emit_nullpointer_check ******************************************************
519 Emit a NullPointerException check.
521 *******************************************************************************/
523 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
525 if (INSTRUCTION_MUST_CHECK(iptr)) {
528 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_NullPointerException);
533 /* emit_exception_check ********************************************************
535 Emit an Exception check.
537 *******************************************************************************/
539 void emit_exception_check(codegendata *cd, instruction *iptr)
541 if (INSTRUCTION_MUST_CHECK(iptr)) {
544 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_CHECK_EXCEPTION);
549 /* emit_trap_compiler **********************************************************
551 Emit a trap instruction which calls the JIT compiler.
553 *******************************************************************************/
555 void emit_trap_compiler(codegendata *cd)
557 M_ALD_INTERN(REG_METHODPTR, REG_ZERO, TRAP_COMPILER);
561 /* emit_trap *******************************************************************
563 Emit a trap instruction and return the original machine code.
565 *******************************************************************************/
567 uint32_t emit_trap(codegendata *cd)
569 // Get machine code which is patched back in later. The rap is 1
570 // instruction word long.
571 uint32_t mcode = *((uint32_t*) cd->mcodeptr);
580 * Emit code to recompute the procedure vector.
582 void emit_recompute_pv(codegendata *cd)
584 int32_t disp = (int32_t) (cd->mcodeptr - cd->mcodebase);
587 M_LDA(REG_PV, REG_ITMP1, -disp);
592 * Generates synchronization code to enter a monitor.
594 #if defined(ENABLE_THREADS)
595 void emit_monitor_enter(jitdata* jd, int32_t syncslot_offset)
600 // Get required compiler data.
601 methodinfo* m = jd->m;
602 codegendata* cd = jd->cd;
604 # if !defined(NDEBUG)
605 if (JITDATA_HAS_FLAG_VERBOSECALL(jd)) {
606 M_AADD_IMM(REG_SP, -((LA_SIZE_IN_POINTERS + ARG_CNT) * 8), REG_SP);
608 for (p = 0; p < INT_ARG_CNT; p++)
609 M_IST(abi_registers_integer_argument[p], REG_SP, LA_SIZE + p * 8);
611 for (p = 0; p < FLT_ARG_CNT; p++)
612 M_DST(abi_registers_float_argument[p], REG_SP, LA_SIZE + (INT_ARG_CNT + p) * 8);
614 syncslot_offset += (LA_SIZE_IN_POINTERS + ARG_CNT) * 8;
618 disp = dseg_add_functionptr(cd, LOCK_monitor_enter);
619 M_ALD(REG_ITMP3, REG_PV, disp);
622 /* get or test the lock object */
624 if (m->flags & ACC_STATIC) {
625 disp = dseg_add_address(cd, &m->clazz->object.header);
626 M_ALD(REG_A0, REG_PV, disp);
631 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_NullPointerException);
634 M_AST(REG_A0, REG_SP, syncslot_offset);
637 # if !defined(NDEBUG)
638 if (JITDATA_HAS_FLAG_VERBOSECALL(jd)) {
639 for (p = 0; p < INT_ARG_CNT; p++)
640 M_ILD(abi_registers_integer_argument[p], REG_SP, LA_SIZE + p * 8);
642 for (p = 0; p < FLT_ARG_CNT; p++)
643 M_DLD(abi_registers_float_argument[p], REG_SP, LA_SIZE + (INT_ARG_CNT + p) * 8);
645 M_AADD_IMM(REG_SP, (LA_SIZE_IN_POINTERS + ARG_CNT) * 8, REG_SP);
653 * Generates synchronization code to leave a monitor.
655 #if defined(ENABLE_THREADS)
656 void emit_monitor_exit(jitdata* jd, int32_t syncslot_offset)
660 // Get required compiler data.
661 methodinfo* m = jd->m;
662 codegendata* cd = jd->cd;
664 disp = dseg_add_functionptr(cd, LOCK_monitor_exit);
665 M_ALD(REG_ITMP3, REG_PV, disp);
668 /* we need to save the proper return value */
670 methoddesc* md = m->parseddesc;
672 switch (md->returntype.type) {
674 M_IST(REG_RESULT2, REG_SP, syncslot_offset + 8);
678 M_IST(REG_RESULT , REG_SP, syncslot_offset + 4);
682 M_DST(REG_FRESULT, REG_SP, syncslot_offset + 4);
686 M_ALD(REG_A0, REG_SP, syncslot_offset);
689 /* and now restore the proper return value */
691 switch (md->returntype.type) {
693 M_ILD(REG_RESULT2, REG_SP, syncslot_offset + 8);
697 M_ILD(REG_RESULT , REG_SP, syncslot_offset + 4);
701 M_DLD(REG_FRESULT, REG_SP, syncslot_offset + 4);
709 * Emit profiling code for method frequency counting.
711 #if defined(ENABLE_PROFILING)
712 void emit_profile_method(codegendata* cd, codeinfo* code)
714 M_ALD(REG_ITMP1, REG_PV, CodeinfoPointer);
715 M_ALD(REG_ITMP2, REG_ITMP1, OFFSET(codeinfo, frequency));
716 M_IADD_IMM(REG_ITMP2, 1, REG_ITMP2);
717 M_AST(REG_ITMP2, REG_ITMP1, OFFSET(codeinfo, frequency));
723 * Emit profiling code for basicblock frequency counting.
725 #if defined(ENABLE_PROFILING)
726 void emit_profile_basicblock(codegendata* cd, codeinfo* code, basicblock* bptr)
728 int32_t disp = dseg_add_address(cd, code->bbfrequency);
730 M_ALD(REG_ITMP2, REG_PV, disp);
731 M_ALD(REG_ITMP3, REG_ITMP2, bptr->nr * 4);
732 M_IADD_IMM(REG_ITMP3, 1, REG_ITMP3);
733 M_AST(REG_ITMP3, REG_ITMP2, bptr->nr * 4);
738 /* emit_verbosecall_enter ******************************************************
740 Generates the code for the call trace.
742 *******************************************************************************/
744 void emit_verbosecall_enter(jitdata *jd)
755 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
758 /* get required compiler data */
766 /* mark trace code */
770 /* On Darwin we need to allocate an additional 3*4 bytes of stack
771 for the arguments to trace_java_call_enter, we make it 2*8. */
774 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
775 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8));
777 /* save argument registers */
779 for (i = 0; i < md->paramcount; i++) {
780 if (!md->params[i].inmemory) {
781 s = md->params[i].regoff;
782 d = LA_SIZE + (i + 2) * 8;
784 switch (md->paramtypes[i].type) {
802 /* pass methodinfo and pointers to the tracer function */
804 disp = dseg_add_address(cd, m);
805 M_ALD(REG_A0, REG_PV, disp);
806 M_AADD_IMM(REG_SP, LA_SIZE + 2 * 8, REG_A1);
807 M_AADD_IMM(REG_SP, LA_SIZE + (2 + ARG_CNT + TMP_CNT + cd->stackframesize) * 8, REG_A2);
809 disp = dseg_add_functionptr(cd, trace_java_call_enter);
810 M_ALD(REG_ITMP2, REG_PV, disp);
814 /* restore argument registers */
816 for (i = 0; i < md->paramcount; i++) {
817 if (!md->params[i].inmemory) {
818 s = LA_SIZE + (i + 2) * 8;
819 d = md->params[i].regoff;
821 switch (md->paramtypes[i].type) {
839 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8 + LA_LR_OFFSET);
841 M_LDA(REG_SP, REG_SP, LA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8);
843 /* mark trace code */
846 #endif /* !defined(NDEBUG) */
850 /* emit_verbosecall_exit *******************************************************
852 Generates the code for the call trace.
854 *******************************************************************************/
856 void emit_verbosecall_exit(jitdata *jd)
865 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
868 /* get required compiler data */
876 /* mark trace code */
880 /* On Darwin we need to allocate an additional 2*4 bytes of stack
881 for the arguments to trace_java_call_exit, we make it 1*8. */
884 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
885 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + 1) * 8));
887 /* save return value */
889 switch (md->returntype.type) {
892 M_IST(REG_RESULT, REG_SP, LA_SIZE + 1 * 8);
895 M_LST(REG_RESULT_PACKED, REG_SP, LA_SIZE + 1 * 8);
898 M_FST(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
901 M_DST(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
907 disp = dseg_add_address(cd, m);
908 M_ALD(REG_A0, REG_PV, disp);
909 M_AADD_IMM(REG_SP, LA_SIZE + 1 * 8, REG_A1);
911 disp = dseg_add_functionptr(cd, trace_java_call_exit);
912 M_ALD(REG_ITMP2, REG_PV, disp);
916 /* restore return value */
918 switch (md->returntype.type) {
921 M_ILD(REG_RESULT, REG_SP, LA_SIZE + 1 * 8);
924 M_LLD(REG_RESULT_PACKED, REG_SP, LA_SIZE + 1 * 8);
927 M_FLD(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
930 M_DLD(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
936 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + 1) * 8 + LA_LR_OFFSET);
938 M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + 1) * 8);
940 /* mark trace code */
943 #endif /* !defined(NDEBUG) */
948 * These are local overrides for various environment variables in Emacs.
949 * Please do not remove this and leave it at the end of the file, where
950 * Emacs will automagically detect them.
951 * ---------------------------------------------------------------------
954 * indent-tabs-mode: t
958 * vim:noexpandtab:sw=4:ts=4: