1 /* src/vm/jit/powerpc/emit.c - PowerPC code emitter functions
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
37 #include "vm/jit/powerpc/codegen.h"
39 #include "mm/memory.h"
41 #include "threads/lock-common.h"
43 #include "vm/builtin.h"
44 #include "vm/exceptions.h"
46 #include "vm/jit/abi.h"
47 #include "vm/jit/asmpart.h"
48 #include "vm/jit/codegen-common.h"
49 #include "vm/jit/dseg.h"
50 #include "vm/jit/emit-common.h"
51 #include "vm/jit/jit.h"
52 #include "vm/jit/replace.h"
53 #include "vm/jit/trace.h"
55 #include "vmcore/options.h"
58 /* emit_load *******************************************************************
60 Emits a possible load of an operand.
62 *******************************************************************************/
64 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
70 /* get required compiler data */
74 if (IS_INMEMORY(src->flags)) {
77 disp = src->vv.regoff;
82 M_ILD(tempreg, REG_SP, disp);
85 M_LLD(tempreg, REG_SP, disp);
89 M_DLD(tempreg, REG_SP, disp);
92 vm_abort("emit_load: unknown type %d", src->type);
104 /* emit_load_low ***************************************************************
106 Emits a possible load of the low 32-bits of an operand.
108 *******************************************************************************/
110 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
116 assert(src->type == TYPE_LNG);
118 /* get required compiler data */
122 if (IS_INMEMORY(src->flags)) {
125 disp = src->vv.regoff;
127 M_ILD(tempreg, REG_SP, disp + 4);
132 reg = GET_LOW_REG(src->vv.regoff);
138 /* emit_load_high **************************************************************
140 Emits a possible load of the high 32-bits of an operand.
142 *******************************************************************************/
144 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
150 assert(src->type == TYPE_LNG);
152 /* get required compiler data */
156 if (IS_INMEMORY(src->flags)) {
159 disp = src->vv.regoff;
161 M_ILD(tempreg, REG_SP, disp);
166 reg = GET_HIGH_REG(src->vv.regoff);
172 /* emit_store ******************************************************************
174 Emit a possible store for the given variable.
176 *******************************************************************************/
178 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
183 /* get required compiler data */
187 if (IS_INMEMORY(dst->flags)) {
190 disp = dst->vv.regoff;
195 M_IST(d, REG_SP, disp);
198 M_LST(d, REG_SP, disp);
202 M_DST(d, REG_SP, disp);
205 vm_abort("emit_store: unknown type %d", dst->type);
211 /* emit_copy *******************************************************************
213 Generates a register/memory to register/memory copy.
215 *******************************************************************************/
217 void emit_copy(jitdata *jd, instruction *iptr)
224 /* get required compiler data */
228 /* get source and destination variables */
230 src = VAROP(iptr->s1);
231 dst = VAROP(iptr->dst);
233 if ((src->vv.regoff != dst->vv.regoff) ||
234 (IS_INMEMORY(src->flags ^ dst->flags))) {
236 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
237 /* emit nothing, as the value won't be used anyway */
241 /* If one of the variables resides in memory, we can eliminate
242 the register move from/to the temporary register with the
243 order of getting the destination register and the load. */
245 if (IS_INMEMORY(src->flags)) {
246 if (IS_LNG_TYPE(src->type))
247 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
249 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
251 s1 = emit_load(jd, iptr, src, d);
254 if (IS_LNG_TYPE(src->type))
255 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
257 s1 = emit_load(jd, iptr, src, REG_IFTMP);
259 d = codegen_reg_of_var(iptr->opc, dst, s1);
269 M_MOV(GET_LOW_REG(s1), GET_LOW_REG(d));
270 M_MOV(GET_HIGH_REG(s1), GET_HIGH_REG(d));
277 vm_abort("emit_copy: unknown type %d", src->type);
281 emit_store(jd, iptr, dst, d);
286 /* emit_iconst *****************************************************************
290 *******************************************************************************/
292 void emit_iconst(codegendata *cd, s4 d, s4 value)
296 if ((value >= -32768) && (value <= 32767))
297 M_LDA_INTERN(d, REG_ZERO, value);
299 disp = dseg_add_s4(cd, value);
300 M_ILD(d, REG_PV, disp);
305 /* emit_branch *****************************************************************
307 Emits the code for conditional and unconditional branchs.
309 *******************************************************************************/
311 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
316 /* calculate the different displacements */
318 checkdisp = disp + 4;
319 branchdisp = (disp - 4) >> 2;
321 /* check which branch to generate */
323 if (condition == BRANCH_UNCONDITIONAL) {
324 /* check displacement for overflow */
326 if ((checkdisp < (s4) 0xfe000000) || (checkdisp > (s4) 0x01fffffc)) {
327 /* if the long-branches flag isn't set yet, do it */
329 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
330 cd->flags |= (CODEGENDATA_FLAG_ERROR |
331 CODEGENDATA_FLAG_LONGBRANCHES);
334 vm_abort("emit_branch: emit unconditional long-branch code");
341 /* and displacement for overflow */
343 if ((checkdisp < (s4) 0xffff8000) || (checkdisp > (s4) 0x00007fff)) {
344 /* if the long-branches flag isn't set yet, do it */
346 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
347 cd->flags |= (CODEGENDATA_FLAG_ERROR |
348 CODEGENDATA_FLAG_LONGBRANCHES);
377 vm_abort("emit_branch: long BRANCH_NAN");
380 vm_abort("emit_branch: unknown condition %d", condition);
407 vm_abort("emit_branch: unknown condition %d", condition);
414 /* emit_arithmetic_check *******************************************************
416 Emit an ArithmeticException check.
418 *******************************************************************************/
420 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
422 if (INSTRUCTION_MUST_CHECK(iptr)) {
425 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_ARITHMETIC);
430 /* emit_arrayindexoutofbounds_check ********************************************
432 Emit a ArrayIndexOutOfBoundsException check.
434 *******************************************************************************/
436 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
438 if (INSTRUCTION_MUST_CHECK(iptr)) {
439 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
440 M_TRAPGEU(s2, REG_ITMP3);
445 /* emit_arraystore_check *******************************************************
447 Emit an ArrayStoreException check.
449 *******************************************************************************/
451 void emit_arraystore_check(codegendata *cd, instruction *iptr)
453 if (INSTRUCTION_MUST_CHECK(iptr)) {
456 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_ARRAYSTORE);
461 /* emit_classcast_check ********************************************************
463 Emit a ClassCastException check.
465 *******************************************************************************/
467 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
469 if (INSTRUCTION_MUST_CHECK(iptr)) {
481 vm_abort("emit_classcast_check: unknown condition %d", condition);
483 M_ALD_INTERN(s1, REG_ZERO, EXCEPTION_HARDWARE_CLASSCAST);
488 /* emit_nullpointer_check ******************************************************
490 Emit a NullPointerException check.
492 *******************************************************************************/
494 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
496 if (INSTRUCTION_MUST_CHECK(iptr)) {
499 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_NULLPOINTER);
504 /* emit_exception_check ********************************************************
506 Emit an Exception check.
508 *******************************************************************************/
510 void emit_exception_check(codegendata *cd, instruction *iptr)
512 if (INSTRUCTION_MUST_CHECK(iptr)) {
515 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_EXCEPTION);
520 /* emit_trap *******************************************************************
522 Emit a trap instruction and return the original machine code.
524 *******************************************************************************/
526 uint32_t emit_trap(codegendata *cd)
530 /* Get machine code which is patched back in later. The
531 trap is 1 instruction word long. */
533 mcode = *((u4 *) cd->mcodeptr);
535 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_PATCHER);
541 /* emit_verbosecall_enter ******************************************************
543 Generates the code for the call trace.
545 *******************************************************************************/
547 void emit_verbosecall_enter(jitdata *jd)
558 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
561 /* get required compiler data */
569 /* mark trace code */
573 /* On Darwin we need to allocate an additional 3*4 bytes of stack
574 for the arguments to trace_java_call_enter, we make it 2*8. */
577 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
578 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8));
580 /* save argument registers */
582 for (i = 0; i < md->paramcount; i++) {
583 if (!md->params[i].inmemory) {
584 s = md->params[i].regoff;
585 d = LA_SIZE + (i + 2) * 8;
587 switch (md->paramtypes[i].type) {
605 /* pass methodinfo and pointers to the tracer function */
607 disp = dseg_add_address(cd, m);
608 M_ALD(REG_A0, REG_PV, disp);
609 M_AADD_IMM(REG_SP, LA_SIZE + 2 * 8, REG_A1);
610 M_AADD_IMM(REG_SP, LA_SIZE + (2 + ARG_CNT + TMP_CNT + cd->stackframesize) * 8, REG_A2);
612 disp = dseg_add_functionptr(cd, trace_java_call_enter);
613 M_ALD(REG_ITMP2, REG_PV, disp);
617 /* restore argument registers */
619 for (i = 0; i < md->paramcount; i++) {
620 if (!md->params[i].inmemory) {
621 s = LA_SIZE + (i + 2) * 8;
622 d = md->params[i].regoff;
624 switch (md->paramtypes[i].type) {
642 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8 + LA_LR_OFFSET);
644 M_LDA(REG_SP, REG_SP, LA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8);
646 /* mark trace code */
649 #endif /* !defined(NDEBUG) */
653 /* emit_verbosecall_exit *******************************************************
655 Generates the code for the call trace.
657 void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
659 *******************************************************************************/
661 void emit_verbosecall_exit(jitdata *jd)
670 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
673 /* get required compiler data */
681 /* mark trace code */
685 /* On Darwin we need to allocate an additional 2*4 bytes of stack
686 for the arguments to trace_java_call_exit, we make it 1*8. */
689 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
690 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + 1) * 8));
692 /* save return value */
694 switch (md->returntype.type) {
697 M_IST(REG_RESULT, REG_SP, LA_SIZE + 1 * 8);
700 M_LST(REG_RESULT_PACKED, REG_SP, LA_SIZE + 1 * 8);
703 M_FST(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
706 M_DST(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
712 disp = dseg_add_address(cd, m);
713 M_ALD(REG_A0, REG_PV, disp);
714 M_AADD_IMM(REG_SP, LA_SIZE + 1 * 8, REG_A1);
716 disp = dseg_add_functionptr(cd, trace_java_call_exit);
717 M_ALD(REG_ITMP2, REG_PV, disp);
721 /* restore return value */
723 switch (md->returntype.type) {
726 M_ILD(REG_RESULT, REG_SP, LA_SIZE + 1 * 8);
729 M_LLD(REG_RESULT_PACKED, REG_SP, LA_SIZE + 1 * 8);
732 M_FLD(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
735 M_DLD(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
741 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + 1) * 8 + LA_LR_OFFSET);
743 M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + 1) * 8);
745 /* mark trace code */
748 #endif /* !defined(NDEBUG) */
753 * These are local overrides for various environment variables in Emacs.
754 * Please do not remove this and leave it at the end of the file, where
755 * Emacs will automagically detect them.
756 * ---------------------------------------------------------------------
759 * indent-tabs-mode: t
763 * vim:noexpandtab:sw=4:ts=4: