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
25 $Id: emit.c 8216 2007-07-19 13:51:21Z michi $
39 #include "vm/jit/powerpc/codegen.h"
41 #include "mm/memory.h"
43 #include "threads/lock-common.h"
45 #include "vm/builtin.h"
46 #include "vm/exceptions.h"
48 #include "vm/jit/abi.h"
49 #include "vm/jit/asmpart.h"
50 #include "vm/jit/codegen-common.h"
51 #include "vm/jit/dseg.h"
52 #include "vm/jit/emit-common.h"
53 #include "vm/jit/jit.h"
54 #include "vm/jit/patcher-common.h"
55 #include "vm/jit/replace.h"
57 #include "vmcore/options.h"
60 /* emit_load *******************************************************************
62 Emits a possible load of an operand.
64 *******************************************************************************/
66 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
72 /* get required compiler data */
76 if (IS_INMEMORY(src->flags)) {
79 disp = src->vv.regoff;
84 M_ILD(tempreg, REG_SP, disp);
87 M_LLD(tempreg, REG_SP, disp);
90 M_FLD(tempreg, REG_SP, disp);
93 M_DLD(tempreg, REG_SP, disp);
96 vm_abort("emit_load: unknown type %d", src->type);
102 reg = src->vv.regoff;
108 /* emit_load_low ***************************************************************
110 Emits a possible load of the low 32-bits of an operand.
112 *******************************************************************************/
114 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
120 assert(src->type == TYPE_LNG);
122 /* get required compiler data */
126 if (IS_INMEMORY(src->flags)) {
129 disp = src->vv.regoff;
131 M_ILD(tempreg, REG_SP, disp + 4);
136 reg = GET_LOW_REG(src->vv.regoff);
142 /* emit_load_high **************************************************************
144 Emits a possible load of the high 32-bits of an operand.
146 *******************************************************************************/
148 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
154 assert(src->type == TYPE_LNG);
156 /* get required compiler data */
160 if (IS_INMEMORY(src->flags)) {
163 disp = src->vv.regoff;
165 M_ILD(tempreg, REG_SP, disp);
170 reg = GET_HIGH_REG(src->vv.regoff);
176 /* emit_store ******************************************************************
178 Emit a possible store for the given variable.
180 *******************************************************************************/
182 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
187 /* get required compiler data */
191 if (IS_INMEMORY(dst->flags)) {
194 disp = dst->vv.regoff;
199 M_IST(d, REG_SP, disp);
202 M_LST(d, REG_SP, disp);
205 M_FST(d, REG_SP, disp);
208 M_DST(d, REG_SP, disp);
211 vm_abort("emit_store: unknown type %d", dst->type);
217 /* emit_copy *******************************************************************
219 Generates a register/memory to register/memory copy.
221 *******************************************************************************/
223 void emit_copy(jitdata *jd, instruction *iptr)
230 /* get required compiler data */
234 /* get source and destination variables */
236 src = VAROP(iptr->s1);
237 dst = VAROP(iptr->dst);
239 if ((src->vv.regoff != dst->vv.regoff) ||
240 (IS_INMEMORY(src->flags ^ dst->flags))) {
242 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
243 /* emit nothing, as the value won't be used anyway */
247 /* If one of the variables resides in memory, we can eliminate
248 the register move from/to the temporary register with the
249 order of getting the destination register and the load. */
251 if (IS_INMEMORY(src->flags)) {
252 if (IS_LNG_TYPE(src->type))
253 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
255 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
257 s1 = emit_load(jd, iptr, src, d);
260 if (IS_LNG_TYPE(src->type))
261 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
263 s1 = emit_load(jd, iptr, src, REG_IFTMP);
265 d = codegen_reg_of_var(iptr->opc, dst, s1);
275 M_MOV(GET_LOW_REG(s1), GET_LOW_REG(d));
276 M_MOV(GET_HIGH_REG(s1), GET_HIGH_REG(d));
283 vm_abort("emit_copy: unknown type %d", src->type);
287 emit_store(jd, iptr, dst, d);
292 /* emit_iconst *****************************************************************
296 *******************************************************************************/
298 void emit_iconst(codegendata *cd, s4 d, s4 value)
302 if ((value >= -32768) && (value <= 32767))
303 M_LDA_INTERN(d, REG_ZERO, value);
305 disp = dseg_add_s4(cd, value);
306 M_ILD(d, REG_PV, disp);
311 /* emit_branch *****************************************************************
313 Emits the code for conditional and unconditional branchs.
315 *******************************************************************************/
317 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
322 /* calculate the different displacements */
324 checkdisp = disp + 4;
325 branchdisp = (disp - 4) >> 2;
327 /* check which branch to generate */
329 if (condition == BRANCH_UNCONDITIONAL) {
330 /* check displacement for overflow */
332 if ((checkdisp < (s4) 0xfe000000) || (checkdisp > (s4) 0x01fffffc)) {
333 /* if the long-branches flag isn't set yet, do it */
335 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
336 cd->flags |= (CODEGENDATA_FLAG_ERROR |
337 CODEGENDATA_FLAG_LONGBRANCHES);
340 vm_abort("emit_branch: emit unconditional long-branch code");
347 /* and displacement for overflow */
349 if ((checkdisp < (s4) 0xffff8000) || (checkdisp > (s4) 0x00007fff)) {
350 /* if the long-branches flag isn't set yet, do it */
352 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
353 cd->flags |= (CODEGENDATA_FLAG_ERROR |
354 CODEGENDATA_FLAG_LONGBRANCHES);
383 vm_abort("emit_branch: long BRANCH_NAN");
386 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, EXCEPTION_HARDWARE_ARITHMETIC);
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_arrayheader, size));
446 M_TRAPGEU(s2, REG_ITMP3);
451 /* emit_classcast_check ********************************************************
453 Emit a ClassCastException check.
455 *******************************************************************************/
457 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
459 if (INSTRUCTION_MUST_CHECK(iptr)) {
471 vm_abort("emit_classcast_check: unknown condition %d", condition);
473 M_ALD_INTERN(s1, REG_ZERO, EXCEPTION_HARDWARE_CLASSCAST);
478 /* emit_nullpointer_check ******************************************************
480 Emit a NullPointerException check.
482 *******************************************************************************/
484 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
486 if (INSTRUCTION_MUST_CHECK(iptr)) {
489 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_NULLPOINTER);
494 /* emit_exception_check ********************************************************
496 Emit an Exception check.
498 *******************************************************************************/
500 void emit_exception_check(codegendata *cd, instruction *iptr)
502 if (INSTRUCTION_MUST_CHECK(iptr)) {
505 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_EXCEPTION);
510 /* emit_patcher_traps **********************************************************
512 Generates the code for the patcher stubs.
514 *******************************************************************************/
516 void emit_patcher_traps(jitdata *jd)
524 /* get required compiler data */
529 /* generate code patching stub call code */
531 for (pr = list_first_unsynced(code->patchers); pr != NULL; pr = list_next_unsynced(code->patchers, pr)) {
533 /* Get machine code which is patched back in later. The
534 trap is 1 instruction word long. */
536 tmpmcodeptr = (u1 *) (cd->mcodebase + pr->mpc);
538 pr->mcode = *((u4 *) tmpmcodeptr);
540 /* Patch in the trap to call the signal handler (done at
543 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
544 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
546 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_PATCHER);
548 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
553 /* emit_verbosecall_enter ******************************************************
555 Generates the code for the call trace.
557 *******************************************************************************/
559 void emit_verbosecall_enter(jitdata *jd)
571 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
574 /* get required compiler data */
582 /* mark trace code */
587 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
588 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + ARG_CNT + TMP_CNT) * 8));
590 M_CLR(REG_ITMP1); /* prepare a "zero" register */
592 /* save argument registers */
594 for (i = 0; i < md->paramcount; i++) {
595 if (!md->params[i].inmemory) {
596 s = md->params[i].regoff;
597 d = LA_SIZE + (1 + i) * 8;
599 switch (md->paramtypes[i].type) {
602 M_IST(REG_ITMP1, REG_SP, d); /* high-bits are zero */
603 M_IST(s, REG_SP, d + 4);
609 M_IST(REG_ITMP1, REG_SP, d); /* high-bits are zero */
610 M_FST(s, REG_SP, d + 4);
619 /* load arguments as longs */
623 for (i = 0; i < md->paramcount && i < TRACE_ARGS_NUM; i++) {
624 s = LA_SIZE + (1 + i) * 8;
625 x = PACK_REGS(abi_registers_integer_argument[d + 1],
626 abi_registers_integer_argument[d]);
633 /* put methodinfo pointer as last argument on the stack */
635 disp = dseg_add_address(cd, m);
636 M_ALD(REG_ITMP1, REG_PV, disp);
637 #if defined(__DARWIN__)
638 M_AST(REG_ITMP1, REG_SP, LA_SIZE + TRACE_ARGS_NUM * 8);
640 M_AST(REG_ITMP1, REG_SP, LA_SIZE);
642 disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
643 M_ALD(REG_ITMP2, REG_PV, disp);
647 /* restore argument registers */
649 for (i = 0; i < md->paramcount; i++) {
650 if (!md->params[i].inmemory) {
651 s = LA_SIZE + (1 + i) * 8;
652 d = md->params[i].regoff;
654 switch (md->paramtypes[i].type) {
657 M_ILD(d, REG_SP, s + 4); /* get low-bits */
663 M_FLD(d, REG_SP, s + 4); /* get low-bits */
672 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + ARG_CNT + TMP_CNT) * 8 + LA_LR_OFFSET);
674 M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + ARG_CNT + TMP_CNT) * 8);
676 /* mark trace code */
679 #endif /* !defined(NDEBUG) */
683 /* emit_verbosecall_exit *******************************************************
685 Generates the code for the call trace.
687 void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
689 *******************************************************************************/
691 void emit_verbosecall_exit(jitdata *jd)
700 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
703 /* get required compiler data */
711 /* mark trace code */
716 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
717 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4));
719 /* save return registers */
721 M_LST(REG_RESULT_PACKED, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 0) * 4);
722 M_DST(REG_FRESULT, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 2) * 4);
724 /* keep this order */
725 switch (md->returntype.type) {
728 M_INTMOVE(REG_RESULT, REG_A1);
733 M_LNGMOVE(REG_RESULT_PACKED, REG_A0_A1_PACKED);
737 M_FLTMOVE(REG_FRESULT, REG_FA0);
738 M_FLTMOVE(REG_FRESULT, REG_FA1);
740 disp = dseg_add_address(cd, m);
741 M_ALD(REG_A2, REG_PV, disp);
743 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
744 M_ALD(REG_ITMP2, REG_PV, disp);
748 /* restore return registers */
750 M_LLD(REG_RESULT_PACKED, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 0) * 4);
751 M_DLD(REG_FRESULT, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 2) * 4);
753 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4 + LA_LR_OFFSET);
755 M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4);
757 /* mark trace code */
760 #endif /* !defined(NDEBUG) */
765 * These are local overrides for various environment variables in Emacs.
766 * Please do not remove this and leave it at the end of the file, where
767 * Emacs will automagically detect them.
768 * ---------------------------------------------------------------------
771 * indent-tabs-mode: t
775 * vim:noexpandtab:sw=4:ts=4: