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);
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)) {
490 vm_abort("emit_classcast_check: unknown condition %d", condition);
492 M_ALD_INTERN(s1, REG_ZERO, TRAP_ClassCastException);
497 /* emit_nullpointer_check ******************************************************
499 Emit a NullPointerException check.
501 *******************************************************************************/
503 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
505 if (INSTRUCTION_MUST_CHECK(iptr)) {
508 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_NullPointerException);
513 /* emit_exception_check ********************************************************
515 Emit an Exception check.
517 *******************************************************************************/
519 void emit_exception_check(codegendata *cd, instruction *iptr)
521 if (INSTRUCTION_MUST_CHECK(iptr)) {
524 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_CHECK_EXCEPTION);
529 /* emit_trap_compiler **********************************************************
531 Emit a trap instruction which calls the JIT compiler.
533 *******************************************************************************/
535 void emit_trap_compiler(codegendata *cd)
537 M_ALD_INTERN(REG_METHODPTR, REG_ZERO, TRAP_COMPILER);
541 /* emit_trap *******************************************************************
543 Emit a trap instruction and return the original machine code.
545 *******************************************************************************/
547 uint32_t emit_trap(codegendata *cd)
549 // Get machine code which is patched back in later. The rap is 1
550 // instruction word long.
551 uint32_t mcode = *((uint32_t*) cd->mcodeptr);
559 /* emit_verbosecall_enter ******************************************************
561 Generates the code for the call trace.
563 *******************************************************************************/
565 void emit_verbosecall_enter(jitdata *jd)
576 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
579 /* get required compiler data */
587 /* mark trace code */
591 /* On Darwin we need to allocate an additional 3*4 bytes of stack
592 for the arguments to trace_java_call_enter, we make it 2*8. */
595 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
596 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8));
598 /* save argument registers */
600 for (i = 0; i < md->paramcount; i++) {
601 if (!md->params[i].inmemory) {
602 s = md->params[i].regoff;
603 d = LA_SIZE + (i + 2) * 8;
605 switch (md->paramtypes[i].type) {
623 /* pass methodinfo and pointers to the tracer function */
625 disp = dseg_add_address(cd, m);
626 M_ALD(REG_A0, REG_PV, disp);
627 M_AADD_IMM(REG_SP, LA_SIZE + 2 * 8, REG_A1);
628 M_AADD_IMM(REG_SP, LA_SIZE + (2 + ARG_CNT + TMP_CNT + cd->stackframesize) * 8, REG_A2);
630 disp = dseg_add_functionptr(cd, trace_java_call_enter);
631 M_ALD(REG_ITMP2, REG_PV, disp);
635 /* restore argument registers */
637 for (i = 0; i < md->paramcount; i++) {
638 if (!md->params[i].inmemory) {
639 s = LA_SIZE + (i + 2) * 8;
640 d = md->params[i].regoff;
642 switch (md->paramtypes[i].type) {
660 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8 + LA_LR_OFFSET);
662 M_LDA(REG_SP, REG_SP, LA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8);
664 /* mark trace code */
667 #endif /* !defined(NDEBUG) */
671 /* emit_verbosecall_exit *******************************************************
673 Generates the code for the call trace.
675 *******************************************************************************/
677 void emit_verbosecall_exit(jitdata *jd)
686 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
689 /* get required compiler data */
697 /* mark trace code */
701 /* On Darwin we need to allocate an additional 2*4 bytes of stack
702 for the arguments to trace_java_call_exit, we make it 1*8. */
705 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
706 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + 1) * 8));
708 /* save return value */
710 switch (md->returntype.type) {
713 M_IST(REG_RESULT, REG_SP, LA_SIZE + 1 * 8);
716 M_LST(REG_RESULT_PACKED, REG_SP, LA_SIZE + 1 * 8);
719 M_FST(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
722 M_DST(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
728 disp = dseg_add_address(cd, m);
729 M_ALD(REG_A0, REG_PV, disp);
730 M_AADD_IMM(REG_SP, LA_SIZE + 1 * 8, REG_A1);
732 disp = dseg_add_functionptr(cd, trace_java_call_exit);
733 M_ALD(REG_ITMP2, REG_PV, disp);
737 /* restore return value */
739 switch (md->returntype.type) {
742 M_ILD(REG_RESULT, REG_SP, LA_SIZE + 1 * 8);
745 M_LLD(REG_RESULT_PACKED, REG_SP, LA_SIZE + 1 * 8);
748 M_FLD(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
751 M_DLD(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
757 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + 1) * 8 + LA_LR_OFFSET);
759 M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + 1) * 8);
761 /* mark trace code */
764 #endif /* !defined(NDEBUG) */
769 * These are local overrides for various environment variables in Emacs.
770 * Please do not remove this and leave it at the end of the file, where
771 * Emacs will automagically detect them.
772 * ---------------------------------------------------------------------
775 * indent-tabs-mode: t
779 * vim:noexpandtab:sw=4:ts=4: