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/exceptions.h"
45 #include "vm/jit/abi.h"
46 #include "vm/jit/asmpart.h"
47 #include "vm/jit/codegen-common.h"
48 #include "vm/jit/dseg.h"
49 #include "vm/jit/emit-common.h"
50 #include "vm/jit/jit.h"
51 #include "vm/jit/replace.h"
52 #include "vm/jit/trace.h"
54 #include "vmcore/options.h"
57 /* emit_load *******************************************************************
59 Emits a possible load of an operand.
61 *******************************************************************************/
63 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
69 /* get required compiler data */
73 if (IS_INMEMORY(src->flags)) {
76 disp = src->vv.regoff;
81 M_ILD(tempreg, REG_SP, disp);
84 M_LLD(tempreg, REG_SP, disp);
88 M_DLD(tempreg, REG_SP, disp);
91 vm_abort("emit_load: unknown type %d", src->type);
103 /* emit_load_low ***************************************************************
105 Emits a possible load of the low 32-bits of an operand.
107 *******************************************************************************/
109 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
115 assert(src->type == TYPE_LNG);
117 /* get required compiler data */
121 if (IS_INMEMORY(src->flags)) {
124 disp = src->vv.regoff;
126 M_ILD(tempreg, REG_SP, disp + 4);
131 reg = GET_LOW_REG(src->vv.regoff);
137 /* emit_load_high **************************************************************
139 Emits a possible load of the high 32-bits of an operand.
141 *******************************************************************************/
143 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
149 assert(src->type == TYPE_LNG);
151 /* get required compiler data */
155 if (IS_INMEMORY(src->flags)) {
158 disp = src->vv.regoff;
160 M_ILD(tempreg, REG_SP, disp);
165 reg = GET_HIGH_REG(src->vv.regoff);
171 /* emit_store ******************************************************************
173 Emit a possible store for the given variable.
175 *******************************************************************************/
177 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
182 /* get required compiler data */
186 if (IS_INMEMORY(dst->flags)) {
189 disp = dst->vv.regoff;
194 M_IST(d, REG_SP, disp);
197 M_LST(d, REG_SP, disp);
201 M_DST(d, REG_SP, disp);
204 vm_abort("emit_store: unknown type %d", dst->type);
210 /* emit_copy *******************************************************************
212 Generates a register/memory to register/memory copy.
214 *******************************************************************************/
216 void emit_copy(jitdata *jd, instruction *iptr)
223 /* get required compiler data */
227 /* get source and destination variables */
229 src = VAROP(iptr->s1);
230 dst = VAROP(iptr->dst);
232 if ((src->vv.regoff != dst->vv.regoff) ||
233 (IS_INMEMORY(src->flags ^ dst->flags))) {
235 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
236 /* emit nothing, as the value won't be used anyway */
240 /* If one of the variables resides in memory, we can eliminate
241 the register move from/to the temporary register with the
242 order of getting the destination register and the load. */
244 if (IS_INMEMORY(src->flags)) {
245 if (IS_LNG_TYPE(src->type))
246 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
248 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
250 s1 = emit_load(jd, iptr, src, d);
253 if (IS_LNG_TYPE(src->type))
254 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
256 s1 = emit_load(jd, iptr, src, REG_IFTMP);
258 d = codegen_reg_of_var(iptr->opc, dst, s1);
268 M_MOV(GET_LOW_REG(s1), GET_LOW_REG(d));
269 M_MOV(GET_HIGH_REG(s1), GET_HIGH_REG(d));
276 vm_abort("emit_copy: unknown type %d", src->type);
280 emit_store(jd, iptr, dst, d);
285 /* emit_iconst *****************************************************************
289 *******************************************************************************/
291 void emit_iconst(codegendata *cd, s4 d, s4 value)
295 if ((value >= -32768) && (value <= 32767))
296 M_LDA_INTERN(d, REG_ZERO, value);
298 disp = dseg_add_s4(cd, value);
299 M_ILD(d, REG_PV, disp);
304 /* emit_branch *****************************************************************
306 Emits the code for conditional and unconditional branchs.
308 *******************************************************************************/
310 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
315 /* calculate the different displacements */
317 checkdisp = disp + 4;
318 branchdisp = (disp - 4) >> 2;
320 /* check which branch to generate */
322 if (condition == BRANCH_UNCONDITIONAL) {
323 /* check displacement for overflow */
325 if ((checkdisp < (s4) 0xfe000000) || (checkdisp > (s4) 0x01fffffc)) {
326 /* if the long-branches flag isn't set yet, do it */
328 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
329 cd->flags |= (CODEGENDATA_FLAG_ERROR |
330 CODEGENDATA_FLAG_LONGBRANCHES);
333 vm_abort("emit_branch: emit unconditional long-branch code");
340 /* and displacement for overflow */
342 if ((checkdisp < (s4) 0xffff8000) || (checkdisp > (s4) 0x00007fff)) {
343 /* if the long-branches flag isn't set yet, do it */
345 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
346 cd->flags |= (CODEGENDATA_FLAG_ERROR |
347 CODEGENDATA_FLAG_LONGBRANCHES);
376 vm_abort("emit_branch: long BRANCH_NAN");
379 vm_abort("emit_branch: unknown condition %d", condition);
406 vm_abort("emit_branch: unknown condition %d", condition);
413 /* emit_arithmetic_check *******************************************************
415 Emit an ArithmeticException check.
417 *******************************************************************************/
419 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
421 if (INSTRUCTION_MUST_CHECK(iptr)) {
424 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_ARITHMETIC);
429 /* emit_arrayindexoutofbounds_check ********************************************
431 Emit a ArrayIndexOutOfBoundsException check.
433 *******************************************************************************/
435 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
437 if (INSTRUCTION_MUST_CHECK(iptr)) {
438 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
439 M_TRAPGEU(s2, REG_ITMP3);
444 /* emit_arraystore_check *******************************************************
446 Emit an ArrayStoreException check.
448 *******************************************************************************/
450 void emit_arraystore_check(codegendata *cd, instruction *iptr)
452 if (INSTRUCTION_MUST_CHECK(iptr)) {
455 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_ARRAYSTORE);
460 /* emit_classcast_check ********************************************************
462 Emit a ClassCastException check.
464 *******************************************************************************/
466 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
468 if (INSTRUCTION_MUST_CHECK(iptr)) {
480 vm_abort("emit_classcast_check: unknown condition %d", condition);
482 M_ALD_INTERN(s1, REG_ZERO, EXCEPTION_HARDWARE_CLASSCAST);
487 /* emit_nullpointer_check ******************************************************
489 Emit a NullPointerException check.
491 *******************************************************************************/
493 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
495 if (INSTRUCTION_MUST_CHECK(iptr)) {
498 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_NULLPOINTER);
503 /* emit_exception_check ********************************************************
505 Emit an Exception check.
507 *******************************************************************************/
509 void emit_exception_check(codegendata *cd, instruction *iptr)
511 if (INSTRUCTION_MUST_CHECK(iptr)) {
514 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_EXCEPTION);
519 /* emit_trap_compiler **********************************************************
521 Emit a trap instruction which calls the JIT compiler.
523 *******************************************************************************/
525 void emit_trap_compiler(codegendata *cd)
527 M_ALD_INTERN(REG_METHODPTR, REG_ZERO, EXCEPTION_HARDWARE_COMPILER);
531 /* emit_trap *******************************************************************
533 Emit a trap instruction and return the original machine code.
535 *******************************************************************************/
537 uint32_t emit_trap(codegendata *cd)
541 /* Get machine code which is patched back in later. The
542 trap is 1 instruction word long. */
544 mcode = *((u4 *) cd->mcodeptr);
546 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_PATCHER);
552 /* emit_verbosecall_enter ******************************************************
554 Generates the code for the call trace.
556 *******************************************************************************/
558 void emit_verbosecall_enter(jitdata *jd)
569 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
572 /* get required compiler data */
580 /* mark trace code */
584 /* On Darwin we need to allocate an additional 3*4 bytes of stack
585 for the arguments to trace_java_call_enter, we make it 2*8. */
588 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
589 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8));
591 /* save argument registers */
593 for (i = 0; i < md->paramcount; i++) {
594 if (!md->params[i].inmemory) {
595 s = md->params[i].regoff;
596 d = LA_SIZE + (i + 2) * 8;
598 switch (md->paramtypes[i].type) {
616 /* pass methodinfo and pointers to the tracer function */
618 disp = dseg_add_address(cd, m);
619 M_ALD(REG_A0, REG_PV, disp);
620 M_AADD_IMM(REG_SP, LA_SIZE + 2 * 8, REG_A1);
621 M_AADD_IMM(REG_SP, LA_SIZE + (2 + ARG_CNT + TMP_CNT + cd->stackframesize) * 8, REG_A2);
623 disp = dseg_add_functionptr(cd, trace_java_call_enter);
624 M_ALD(REG_ITMP2, REG_PV, disp);
628 /* restore argument registers */
630 for (i = 0; i < md->paramcount; i++) {
631 if (!md->params[i].inmemory) {
632 s = LA_SIZE + (i + 2) * 8;
633 d = md->params[i].regoff;
635 switch (md->paramtypes[i].type) {
653 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8 + LA_LR_OFFSET);
655 M_LDA(REG_SP, REG_SP, LA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8);
657 /* mark trace code */
660 #endif /* !defined(NDEBUG) */
664 /* emit_verbosecall_exit *******************************************************
666 Generates the code for the call trace.
668 *******************************************************************************/
670 void emit_verbosecall_exit(jitdata *jd)
679 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
682 /* get required compiler data */
690 /* mark trace code */
694 /* On Darwin we need to allocate an additional 2*4 bytes of stack
695 for the arguments to trace_java_call_exit, we make it 1*8. */
698 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
699 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + 1) * 8));
701 /* save return value */
703 switch (md->returntype.type) {
706 M_IST(REG_RESULT, REG_SP, LA_SIZE + 1 * 8);
709 M_LST(REG_RESULT_PACKED, REG_SP, LA_SIZE + 1 * 8);
712 M_FST(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
715 M_DST(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
721 disp = dseg_add_address(cd, m);
722 M_ALD(REG_A0, REG_PV, disp);
723 M_AADD_IMM(REG_SP, LA_SIZE + 1 * 8, REG_A1);
725 disp = dseg_add_functionptr(cd, trace_java_call_exit);
726 M_ALD(REG_ITMP2, REG_PV, disp);
730 /* restore return value */
732 switch (md->returntype.type) {
735 M_ILD(REG_RESULT, REG_SP, LA_SIZE + 1 * 8);
738 M_LLD(REG_RESULT_PACKED, REG_SP, LA_SIZE + 1 * 8);
741 M_FLD(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
744 M_DLD(REG_FRESULT, REG_SP, LA_SIZE + 1 * 8);
750 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + 1) * 8 + LA_LR_OFFSET);
752 M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + 1) * 8);
754 /* mark trace code */
757 #endif /* !defined(NDEBUG) */
762 * These are local overrides for various environment variables in Emacs.
763 * Please do not remove this and leave it at the end of the file, where
764 * Emacs will automagically detect them.
765 * ---------------------------------------------------------------------
768 * indent-tabs-mode: t
772 * vim:noexpandtab:sw=4:ts=4: