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.h"
39 #include "threads/lock-common.h"
41 #include "vm/exceptions.h"
43 #include "vm/jit/abi.h"
44 #include "vm/jit/asmpart.h"
45 #include "vm/jit/codegen-common.h"
46 #include "vm/jit/dseg.h"
47 #include "vm/jit/emit-common.h"
48 #include "vm/jit/jit.h"
49 #include "vm/jit/replace.h"
50 #include "vm/jit/trace.h"
51 #include "vm/jit/trap.h"
53 #include "vmcore/options.h"
56 /* emit_load *******************************************************************
58 Emits a possible load of an operand.
60 *******************************************************************************/
62 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
68 /* get required compiler data */
72 if (IS_INMEMORY(src->flags)) {
75 disp = src->vv.regoff;
80 M_ILD(tempreg, REG_SP, disp);
83 M_LLD(tempreg, REG_SP, disp);
87 M_DLD(tempreg, REG_SP, disp);
90 vm_abort("emit_load: unknown type %d", src->type);
102 /* emit_load_low ***************************************************************
104 Emits a possible load of the low 32-bits of an operand.
106 *******************************************************************************/
108 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
114 assert(src->type == TYPE_LNG);
116 /* get required compiler data */
120 if (IS_INMEMORY(src->flags)) {
123 disp = src->vv.regoff;
125 M_ILD(tempreg, REG_SP, disp + 4);
130 reg = GET_LOW_REG(src->vv.regoff);
136 /* emit_load_high **************************************************************
138 Emits a possible load of the high 32-bits of an operand.
140 *******************************************************************************/
142 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
148 assert(src->type == TYPE_LNG);
150 /* get required compiler data */
154 if (IS_INMEMORY(src->flags)) {
157 disp = src->vv.regoff;
159 M_ILD(tempreg, REG_SP, disp);
164 reg = GET_HIGH_REG(src->vv.regoff);
170 /* emit_store ******************************************************************
172 Emit a possible store for the given variable.
174 *******************************************************************************/
176 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
181 /* get required compiler data */
185 if (IS_INMEMORY(dst->flags)) {
188 disp = dst->vv.regoff;
193 M_IST(d, REG_SP, disp);
196 M_LST(d, REG_SP, disp);
200 M_DST(d, REG_SP, disp);
203 vm_abort("emit_store: unknown type %d", dst->type);
209 /* emit_copy *******************************************************************
211 Generates a register/memory to register/memory copy.
213 *******************************************************************************/
215 void emit_copy(jitdata *jd, instruction *iptr)
222 /* get required compiler data */
226 /* get source and destination variables */
228 src = VAROP(iptr->s1);
229 dst = VAROP(iptr->dst);
231 if ((src->vv.regoff != dst->vv.regoff) ||
232 (IS_INMEMORY(src->flags ^ dst->flags))) {
234 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
235 /* emit nothing, as the value won't be used anyway */
239 /* If one of the variables resides in memory, we can eliminate
240 the register move from/to the temporary register with the
241 order of getting the destination register and the load. */
243 if (IS_INMEMORY(src->flags)) {
244 if (IS_LNG_TYPE(src->type))
245 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
247 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
249 s1 = emit_load(jd, iptr, src, d);
252 if (IS_LNG_TYPE(src->type))
253 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
255 s1 = emit_load(jd, iptr, src, REG_IFTMP);
257 d = codegen_reg_of_var(iptr->opc, dst, s1);
267 M_MOV(GET_LOW_REG(s1), GET_LOW_REG(d));
268 M_MOV(GET_HIGH_REG(s1), GET_HIGH_REG(d));
275 vm_abort("emit_copy: unknown type %d", src->type);
279 emit_store(jd, iptr, dst, d);
284 /* emit_iconst *****************************************************************
288 *******************************************************************************/
290 void emit_iconst(codegendata *cd, s4 d, s4 value)
294 if ((value >= -32768) && (value <= 32767))
295 M_LDA_INTERN(d, REG_ZERO, value);
297 disp = dseg_add_s4(cd, value);
298 M_ILD(d, REG_PV, disp);
303 /* emit_branch *****************************************************************
305 Emits the code for conditional and unconditional branchs.
307 *******************************************************************************/
309 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
314 /* calculate the different displacements */
316 checkdisp = disp + 4;
317 branchdisp = (disp - 4) >> 2;
319 /* check which branch to generate */
321 if (condition == BRANCH_UNCONDITIONAL) {
322 /* check displacement for overflow */
324 if ((checkdisp < (s4) 0xfe000000) || (checkdisp > (s4) 0x01fffffc)) {
325 /* if the long-branches flag isn't set yet, do it */
327 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
328 cd->flags |= (CODEGENDATA_FLAG_ERROR |
329 CODEGENDATA_FLAG_LONGBRANCHES);
332 vm_abort("emit_branch: emit unconditional long-branch code");
339 /* and displacement for overflow */
341 if ((checkdisp < (s4) 0xffff8000) || (checkdisp > (s4) 0x00007fff)) {
342 /* if the long-branches flag isn't set yet, do it */
344 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
345 cd->flags |= (CODEGENDATA_FLAG_ERROR |
346 CODEGENDATA_FLAG_LONGBRANCHES);
375 vm_abort("emit_branch: long BRANCH_NAN");
378 vm_abort("emit_branch: unknown condition %d", condition);
405 vm_abort("emit_branch: unknown condition %d", condition);
412 /* emit_arithmetic_check *******************************************************
414 Emit an ArithmeticException check.
416 *******************************************************************************/
418 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
420 if (INSTRUCTION_MUST_CHECK(iptr)) {
423 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_ArithmeticException);
428 /* emit_arrayindexoutofbounds_check ********************************************
430 Emit a ArrayIndexOutOfBoundsException check.
432 *******************************************************************************/
434 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
436 if (INSTRUCTION_MUST_CHECK(iptr)) {
437 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
438 M_TRAPGEU(s2, REG_ITMP3);
443 /* emit_arraystore_check *******************************************************
445 Emit an ArrayStoreException check.
447 *******************************************************************************/
449 void emit_arraystore_check(codegendata *cd, instruction *iptr)
451 if (INSTRUCTION_MUST_CHECK(iptr)) {
454 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_ArrayStoreException);
459 /* emit_classcast_check ********************************************************
461 Emit a ClassCastException check.
463 *******************************************************************************/
465 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
467 if (INSTRUCTION_MUST_CHECK(iptr)) {
479 vm_abort("emit_classcast_check: unknown condition %d", condition);
481 M_ALD_INTERN(s1, REG_ZERO, TRAP_ClassCastException);
486 /* emit_nullpointer_check ******************************************************
488 Emit a NullPointerException check.
490 *******************************************************************************/
492 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
494 if (INSTRUCTION_MUST_CHECK(iptr)) {
497 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_NullPointerException);
502 /* emit_exception_check ********************************************************
504 Emit an Exception check.
506 *******************************************************************************/
508 void emit_exception_check(codegendata *cd, instruction *iptr)
510 if (INSTRUCTION_MUST_CHECK(iptr)) {
513 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_CHECK_EXCEPTION);
518 /* emit_trap_compiler **********************************************************
520 Emit a trap instruction which calls the JIT compiler.
522 *******************************************************************************/
524 void emit_trap_compiler(codegendata *cd)
526 M_ALD_INTERN(REG_METHODPTR, REG_ZERO, TRAP_COMPILER);
530 /* emit_trap *******************************************************************
532 Emit a trap instruction and return the original machine code.
534 *******************************************************************************/
536 uint32_t emit_trap(codegendata *cd)
540 /* Get machine code which is patched back in later. The
541 trap is 1 instruction word long. */
543 mcode = *((uint32_t *) cd->mcodeptr);
545 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_PATCHER);
551 /* emit_verbosecall_enter ******************************************************
553 Generates the code for the call trace.
555 *******************************************************************************/
557 void emit_verbosecall_enter(jitdata *jd)
568 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
571 /* get required compiler data */
579 /* mark trace code */
583 /* On Darwin we need to allocate an additional 3*4 bytes of stack
584 for the arguments to trace_java_call_enter, we make it 2*8. */
587 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
588 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8));
590 /* save argument registers */
592 for (i = 0; i < md->paramcount; i++) {
593 if (!md->params[i].inmemory) {
594 s = md->params[i].regoff;
595 d = LA_SIZE + (i + 2) * 8;
597 switch (md->paramtypes[i].type) {
615 /* pass methodinfo and pointers to the tracer function */
617 disp = dseg_add_address(cd, m);
618 M_ALD(REG_A0, REG_PV, disp);
619 M_AADD_IMM(REG_SP, LA_SIZE + 2 * 8, REG_A1);
620 M_AADD_IMM(REG_SP, LA_SIZE + (2 + ARG_CNT + TMP_CNT + cd->stackframesize) * 8, REG_A2);
622 disp = dseg_add_functionptr(cd, trace_java_call_enter);
623 M_ALD(REG_ITMP2, REG_PV, disp);
627 /* restore argument registers */
629 for (i = 0; i < md->paramcount; i++) {
630 if (!md->params[i].inmemory) {
631 s = LA_SIZE + (i + 2) * 8;
632 d = md->params[i].regoff;
634 switch (md->paramtypes[i].type) {
652 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8 + LA_LR_OFFSET);
654 M_LDA(REG_SP, REG_SP, LA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8);
656 /* mark trace code */
659 #endif /* !defined(NDEBUG) */
663 /* emit_verbosecall_exit *******************************************************
665 Generates the code for the call trace.
667 *******************************************************************************/
669 void emit_verbosecall_exit(jitdata *jd)
678 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
681 /* get required compiler data */
689 /* mark trace code */
693 /* On Darwin we need to allocate an additional 2*4 bytes of stack
694 for the arguments to trace_java_call_exit, we make it 1*8. */
697 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
698 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + 1) * 8));
700 /* save return value */
702 switch (md->returntype.type) {
705 M_IST(REG_RESULT, REG_SP, LA_SIZE + 1 * 8);
708 M_LST(REG_RESULT_PACKED, REG_SP, LA_SIZE + 1 * 8);
711 M_FST(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
714 M_DST(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
720 disp = dseg_add_address(cd, m);
721 M_ALD(REG_A0, REG_PV, disp);
722 M_AADD_IMM(REG_SP, LA_SIZE + 1 * 8, REG_A1);
724 disp = dseg_add_functionptr(cd, trace_java_call_exit);
725 M_ALD(REG_ITMP2, REG_PV, disp);
729 /* restore return value */
731 switch (md->returntype.type) {
734 M_ILD(REG_RESULT, REG_SP, LA_SIZE + 1 * 8);
737 M_LLD(REG_RESULT_PACKED, REG_SP, LA_SIZE + 1 * 8);
740 M_FLD(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
743 M_DLD(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
749 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + 1) * 8 + LA_LR_OFFSET);
751 M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + 1) * 8);
753 /* mark trace code */
756 #endif /* !defined(NDEBUG) */
761 * These are local overrides for various environment variables in Emacs.
762 * Please do not remove this and leave it at the end of the file, where
763 * Emacs will automagically detect them.
764 * ---------------------------------------------------------------------
767 * indent-tabs-mode: t
771 * vim:noexpandtab:sw=4:ts=4: