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/options.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.hpp"
49 #include "vm/jit/replace.h"
50 #include "vm/jit/trace.hpp"
51 #include "vm/jit/trap.h"
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);
301 /* emit_branch *****************************************************************
303 Emits the code for conditional and unconditional branchs.
305 *******************************************************************************/
307 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
312 /* calculate the different displacements */
314 checkdisp = disp + 4;
315 branchdisp = (disp - 4) >> 2;
317 /* check which branch to generate */
319 if (condition == BRANCH_UNCONDITIONAL) {
320 /* check displacement for overflow */
322 if ((checkdisp < (s4) 0xfe000000) || (checkdisp > (s4) 0x01fffffc)) {
323 /* if the long-branches flag isn't set yet, do it */
325 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
326 cd->flags |= (CODEGENDATA_FLAG_ERROR |
327 CODEGENDATA_FLAG_LONGBRANCHES);
330 vm_abort("emit_branch: emit unconditional long-branch code");
337 /* and displacement for overflow */
339 if ((checkdisp < (s4) 0xffff8000) || (checkdisp > (s4) 0x00007fff)) {
340 /* if the long-branches flag isn't set yet, do it */
342 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
343 cd->flags |= (CODEGENDATA_FLAG_ERROR |
344 CODEGENDATA_FLAG_LONGBRANCHES);
373 vm_abort("emit_branch: long BRANCH_NAN");
376 vm_abort("emit_branch: unknown condition %d", condition);
403 vm_abort("emit_branch: unknown condition %d", condition);
410 /* emit_arithmetic_check *******************************************************
412 Emit an ArithmeticException check.
414 *******************************************************************************/
416 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
418 if (INSTRUCTION_MUST_CHECK(iptr)) {
421 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_ArithmeticException);
426 /* emit_arrayindexoutofbounds_check ********************************************
428 Emit a ArrayIndexOutOfBoundsException check.
430 *******************************************************************************/
432 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
434 if (INSTRUCTION_MUST_CHECK(iptr)) {
435 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
436 M_TRAPGEU(s2, REG_ITMP3);
441 /* emit_arraystore_check *******************************************************
443 Emit an ArrayStoreException check.
445 *******************************************************************************/
447 void emit_arraystore_check(codegendata *cd, instruction *iptr)
449 if (INSTRUCTION_MUST_CHECK(iptr)) {
452 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_ArrayStoreException);
457 /* emit_classcast_check ********************************************************
459 Emit a ClassCastException check.
461 *******************************************************************************/
463 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
465 if (INSTRUCTION_MUST_CHECK(iptr)) {
477 vm_abort("emit_classcast_check: unknown condition %d", condition);
479 M_ALD_INTERN(s1, REG_ZERO, TRAP_ClassCastException);
484 /* emit_nullpointer_check ******************************************************
486 Emit a NullPointerException check.
488 *******************************************************************************/
490 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
492 if (INSTRUCTION_MUST_CHECK(iptr)) {
495 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_NullPointerException);
500 /* emit_exception_check ********************************************************
502 Emit an Exception check.
504 *******************************************************************************/
506 void emit_exception_check(codegendata *cd, instruction *iptr)
508 if (INSTRUCTION_MUST_CHECK(iptr)) {
511 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_CHECK_EXCEPTION);
516 /* emit_trap_compiler **********************************************************
518 Emit a trap instruction which calls the JIT compiler.
520 *******************************************************************************/
522 void emit_trap_compiler(codegendata *cd)
524 M_ALD_INTERN(REG_METHODPTR, REG_ZERO, TRAP_COMPILER);
528 /* emit_trap *******************************************************************
530 Emit a trap instruction and return the original machine code.
532 *******************************************************************************/
534 uint32_t emit_trap(codegendata *cd)
538 /* Get machine code which is patched back in later. The
539 trap is 1 instruction word long. */
541 mcode = *((uint32_t *) cd->mcodeptr);
543 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_PATCHER);
549 /* emit_verbosecall_enter ******************************************************
551 Generates the code for the call trace.
553 *******************************************************************************/
555 void emit_verbosecall_enter(jitdata *jd)
566 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
569 /* get required compiler data */
577 /* mark trace code */
581 /* On Darwin we need to allocate an additional 3*4 bytes of stack
582 for the arguments to trace_java_call_enter, we make it 2*8. */
585 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
586 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8));
588 /* save argument registers */
590 for (i = 0; i < md->paramcount; i++) {
591 if (!md->params[i].inmemory) {
592 s = md->params[i].regoff;
593 d = LA_SIZE + (i + 2) * 8;
595 switch (md->paramtypes[i].type) {
613 /* pass methodinfo and pointers to the tracer function */
615 disp = dseg_add_address(cd, m);
616 M_ALD(REG_A0, REG_PV, disp);
617 M_AADD_IMM(REG_SP, LA_SIZE + 2 * 8, REG_A1);
618 M_AADD_IMM(REG_SP, LA_SIZE + (2 + ARG_CNT + TMP_CNT + cd->stackframesize) * 8, REG_A2);
620 disp = dseg_add_functionptr(cd, trace_java_call_enter);
621 M_ALD(REG_ITMP2, REG_PV, disp);
625 /* restore argument registers */
627 for (i = 0; i < md->paramcount; i++) {
628 if (!md->params[i].inmemory) {
629 s = LA_SIZE + (i + 2) * 8;
630 d = md->params[i].regoff;
632 switch (md->paramtypes[i].type) {
650 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8 + LA_LR_OFFSET);
652 M_LDA(REG_SP, REG_SP, LA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8);
654 /* mark trace code */
657 #endif /* !defined(NDEBUG) */
661 /* emit_verbosecall_exit *******************************************************
663 Generates the code for the call trace.
665 *******************************************************************************/
667 void emit_verbosecall_exit(jitdata *jd)
676 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
679 /* get required compiler data */
687 /* mark trace code */
691 /* On Darwin we need to allocate an additional 2*4 bytes of stack
692 for the arguments to trace_java_call_exit, we make it 1*8. */
695 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
696 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + 1) * 8));
698 /* save return value */
700 switch (md->returntype.type) {
703 M_IST(REG_RESULT, REG_SP, LA_SIZE + 1 * 8);
706 M_LST(REG_RESULT_PACKED, REG_SP, LA_SIZE + 1 * 8);
709 M_FST(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
712 M_DST(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
718 disp = dseg_add_address(cd, m);
719 M_ALD(REG_A0, REG_PV, disp);
720 M_AADD_IMM(REG_SP, LA_SIZE + 1 * 8, REG_A1);
722 disp = dseg_add_functionptr(cd, trace_java_call_exit);
723 M_ALD(REG_ITMP2, REG_PV, disp);
727 /* restore return value */
729 switch (md->returntype.type) {
732 M_ILD(REG_RESULT, REG_SP, LA_SIZE + 1 * 8);
735 M_LLD(REG_RESULT_PACKED, REG_SP, LA_SIZE + 1 * 8);
738 M_FLD(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
741 M_DLD(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
747 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + 1) * 8 + LA_LR_OFFSET);
749 M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + 1) * 8);
751 /* mark trace code */
754 #endif /* !defined(NDEBUG) */
759 * These are local overrides for various environment variables in Emacs.
760 * Please do not remove this and leave it at the end of the file, where
761 * Emacs will automagically detect them.
762 * ---------------------------------------------------------------------
765 * indent-tabs-mode: t
769 * vim:noexpandtab:sw=4:ts=4: