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.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);
347 // Subtract 1 instruction from the displacement as the
348 // actual branch is the second instruction.
349 checkdisp = checkdisp - 4;
350 branchdisp = branchdisp - 1;
352 if ((checkdisp < (int32_t) 0xfe000000) || (checkdisp > (int32_t) 0x01fffffc)) {
353 vm_abort("emit_branch: emit conditional long-branch code");
382 vm_abort("emit_branch: long BRANCH_NAN");
385 vm_abort("emit_branch: unknown condition %d", condition);
413 vm_abort("emit_branch: unknown condition %d", condition);
420 /* emit_arithmetic_check *******************************************************
422 Emit an ArithmeticException check.
424 *******************************************************************************/
426 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
428 if (INSTRUCTION_MUST_CHECK(iptr)) {
431 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_ArithmeticException);
436 /* emit_arrayindexoutofbounds_check ********************************************
438 Emit a ArrayIndexOutOfBoundsException check.
440 *******************************************************************************/
442 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
444 if (INSTRUCTION_MUST_CHECK(iptr)) {
445 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
446 M_TRAPGEU(s2, REG_ITMP3);
451 /* emit_arraystore_check *******************************************************
453 Emit an ArrayStoreException check.
455 *******************************************************************************/
457 void emit_arraystore_check(codegendata *cd, instruction *iptr)
459 if (INSTRUCTION_MUST_CHECK(iptr)) {
462 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_ArrayStoreException);
467 /* emit_classcast_check ********************************************************
469 Emit a ClassCastException check.
471 *******************************************************************************/
473 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
475 if (INSTRUCTION_MUST_CHECK(iptr)) {
487 vm_abort("emit_classcast_check: unknown condition %d", condition);
489 M_ALD_INTERN(s1, REG_ZERO, TRAP_ClassCastException);
494 /* emit_nullpointer_check ******************************************************
496 Emit a NullPointerException check.
498 *******************************************************************************/
500 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
502 if (INSTRUCTION_MUST_CHECK(iptr)) {
505 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_NullPointerException);
510 /* emit_exception_check ********************************************************
512 Emit an Exception check.
514 *******************************************************************************/
516 void emit_exception_check(codegendata *cd, instruction *iptr)
518 if (INSTRUCTION_MUST_CHECK(iptr)) {
521 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_CHECK_EXCEPTION);
526 /* emit_trap_compiler **********************************************************
528 Emit a trap instruction which calls the JIT compiler.
530 *******************************************************************************/
532 void emit_trap_compiler(codegendata *cd)
534 M_ALD_INTERN(REG_METHODPTR, REG_ZERO, TRAP_COMPILER);
538 /* emit_trap *******************************************************************
540 Emit a trap instruction and return the original machine code.
542 *******************************************************************************/
544 uint32_t emit_trap(codegendata *cd)
546 // Get machine code which is patched back in later. The rap is 1
547 // instruction word long.
548 uint32_t mcode = *((uint32_t*) cd->mcodeptr);
556 /* emit_verbosecall_enter ******************************************************
558 Generates the code for the call trace.
560 *******************************************************************************/
562 void emit_verbosecall_enter(jitdata *jd)
573 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
576 /* get required compiler data */
584 /* mark trace code */
588 /* On Darwin we need to allocate an additional 3*4 bytes of stack
589 for the arguments to trace_java_call_enter, we make it 2*8. */
592 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
593 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8));
595 /* save argument registers */
597 for (i = 0; i < md->paramcount; i++) {
598 if (!md->params[i].inmemory) {
599 s = md->params[i].regoff;
600 d = LA_SIZE + (i + 2) * 8;
602 switch (md->paramtypes[i].type) {
620 /* pass methodinfo and pointers to the tracer function */
622 disp = dseg_add_address(cd, m);
623 M_ALD(REG_A0, REG_PV, disp);
624 M_AADD_IMM(REG_SP, LA_SIZE + 2 * 8, REG_A1);
625 M_AADD_IMM(REG_SP, LA_SIZE + (2 + ARG_CNT + TMP_CNT + cd->stackframesize) * 8, REG_A2);
627 disp = dseg_add_functionptr(cd, trace_java_call_enter);
628 M_ALD(REG_ITMP2, REG_PV, disp);
632 /* restore argument registers */
634 for (i = 0; i < md->paramcount; i++) {
635 if (!md->params[i].inmemory) {
636 s = LA_SIZE + (i + 2) * 8;
637 d = md->params[i].regoff;
639 switch (md->paramtypes[i].type) {
657 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8 + LA_LR_OFFSET);
659 M_LDA(REG_SP, REG_SP, LA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8);
661 /* mark trace code */
664 #endif /* !defined(NDEBUG) */
668 /* emit_verbosecall_exit *******************************************************
670 Generates the code for the call trace.
672 *******************************************************************************/
674 void emit_verbosecall_exit(jitdata *jd)
683 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
686 /* get required compiler data */
694 /* mark trace code */
698 /* On Darwin we need to allocate an additional 2*4 bytes of stack
699 for the arguments to trace_java_call_exit, we make it 1*8. */
702 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
703 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + 1) * 8));
705 /* save return value */
707 switch (md->returntype.type) {
710 M_IST(REG_RESULT, REG_SP, LA_SIZE + 1 * 8);
713 M_LST(REG_RESULT_PACKED, REG_SP, LA_SIZE + 1 * 8);
716 M_FST(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
719 M_DST(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
725 disp = dseg_add_address(cd, m);
726 M_ALD(REG_A0, REG_PV, disp);
727 M_AADD_IMM(REG_SP, LA_SIZE + 1 * 8, REG_A1);
729 disp = dseg_add_functionptr(cd, trace_java_call_exit);
730 M_ALD(REG_ITMP2, REG_PV, disp);
734 /* restore return value */
736 switch (md->returntype.type) {
739 M_ILD(REG_RESULT, REG_SP, LA_SIZE + 1 * 8);
742 M_LLD(REG_RESULT_PACKED, REG_SP, LA_SIZE + 1 * 8);
745 M_FLD(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
748 M_DLD(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
754 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + 1) * 8 + LA_LR_OFFSET);
756 M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + 1) * 8);
758 /* mark trace code */
761 #endif /* !defined(NDEBUG) */
766 * These are local overrides for various environment variables in Emacs.
767 * Please do not remove this and leave it at the end of the file, where
768 * Emacs will automagically detect them.
769 * ---------------------------------------------------------------------
772 * indent-tabs-mode: t
776 * vim:noexpandtab:sw=4:ts=4: