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 8260 2007-08-06 12:19:01Z 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/replace.h"
56 #include "vmcore/options.h"
59 /* emit_load *******************************************************************
61 Emits a possible load of an operand.
63 *******************************************************************************/
65 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
71 /* get required compiler data */
75 if (IS_INMEMORY(src->flags)) {
78 disp = src->vv.regoff;
83 M_ILD(tempreg, REG_SP, disp);
86 M_LLD(tempreg, REG_SP, disp);
89 M_FLD(tempreg, REG_SP, disp);
92 M_DLD(tempreg, REG_SP, disp);
95 vm_abort("emit_load: unknown type %d", src->type);
101 reg = src->vv.regoff;
107 /* emit_load_low ***************************************************************
109 Emits a possible load of the low 32-bits of an operand.
111 *******************************************************************************/
113 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
119 assert(src->type == TYPE_LNG);
121 /* get required compiler data */
125 if (IS_INMEMORY(src->flags)) {
128 disp = src->vv.regoff;
130 M_ILD(tempreg, REG_SP, disp + 4);
135 reg = GET_LOW_REG(src->vv.regoff);
141 /* emit_load_high **************************************************************
143 Emits a possible load of the high 32-bits of an operand.
145 *******************************************************************************/
147 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
153 assert(src->type == TYPE_LNG);
155 /* get required compiler data */
159 if (IS_INMEMORY(src->flags)) {
162 disp = src->vv.regoff;
164 M_ILD(tempreg, REG_SP, disp);
169 reg = GET_HIGH_REG(src->vv.regoff);
175 /* emit_store ******************************************************************
177 Emit a possible store for the given variable.
179 *******************************************************************************/
181 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
186 /* get required compiler data */
190 if (IS_INMEMORY(dst->flags)) {
193 disp = dst->vv.regoff;
198 M_IST(d, REG_SP, disp);
201 M_LST(d, REG_SP, disp);
204 M_FST(d, REG_SP, disp);
207 M_DST(d, REG_SP, disp);
210 vm_abort("emit_store: unknown type %d", dst->type);
216 /* emit_copy *******************************************************************
218 Generates a register/memory to register/memory copy.
220 *******************************************************************************/
222 void emit_copy(jitdata *jd, instruction *iptr)
229 /* get required compiler data */
233 /* get source and destination variables */
235 src = VAROP(iptr->s1);
236 dst = VAROP(iptr->dst);
238 if ((src->vv.regoff != dst->vv.regoff) ||
239 (IS_INMEMORY(src->flags ^ dst->flags))) {
241 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
242 /* emit nothing, as the value won't be used anyway */
246 /* If one of the variables resides in memory, we can eliminate
247 the register move from/to the temporary register with the
248 order of getting the destination register and the load. */
250 if (IS_INMEMORY(src->flags)) {
251 if (IS_LNG_TYPE(src->type))
252 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
254 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
256 s1 = emit_load(jd, iptr, src, d);
259 if (IS_LNG_TYPE(src->type))
260 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
262 s1 = emit_load(jd, iptr, src, REG_IFTMP);
264 d = codegen_reg_of_var(iptr->opc, dst, s1);
274 M_MOV(GET_LOW_REG(s1), GET_LOW_REG(d));
275 M_MOV(GET_HIGH_REG(s1), GET_HIGH_REG(d));
282 vm_abort("emit_copy: unknown type %d", src->type);
286 emit_store(jd, iptr, dst, d);
291 /* emit_iconst *****************************************************************
295 *******************************************************************************/
297 void emit_iconst(codegendata *cd, s4 d, s4 value)
301 if ((value >= -32768) && (value <= 32767))
302 M_LDA_INTERN(d, REG_ZERO, value);
304 disp = dseg_add_s4(cd, value);
305 M_ILD(d, REG_PV, disp);
310 /* emit_branch *****************************************************************
312 Emits the code for conditional and unconditional branchs.
314 *******************************************************************************/
316 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
321 /* calculate the different displacements */
323 checkdisp = disp + 4;
324 branchdisp = (disp - 4) >> 2;
326 /* check which branch to generate */
328 if (condition == BRANCH_UNCONDITIONAL) {
329 /* check displacement for overflow */
331 if ((checkdisp < (s4) 0xfe000000) || (checkdisp > (s4) 0x01fffffc)) {
332 /* if the long-branches flag isn't set yet, do it */
334 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
335 cd->flags |= (CODEGENDATA_FLAG_ERROR |
336 CODEGENDATA_FLAG_LONGBRANCHES);
339 vm_abort("emit_branch: emit unconditional long-branch code");
346 /* and displacement for overflow */
348 if ((checkdisp < (s4) 0xffff8000) || (checkdisp > (s4) 0x00007fff)) {
349 /* if the long-branches flag isn't set yet, do it */
351 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
352 cd->flags |= (CODEGENDATA_FLAG_ERROR |
353 CODEGENDATA_FLAG_LONGBRANCHES);
382 vm_abort("emit_branch: long BRANCH_NAN");
385 vm_abort("emit_branch: unknown condition %d", condition);
412 vm_abort("emit_branch: unknown condition %d", condition);
419 /* emit_arithmetic_check *******************************************************
421 Emit an ArithmeticException check.
423 *******************************************************************************/
425 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
427 if (INSTRUCTION_MUST_CHECK(iptr)) {
430 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_ARITHMETIC);
435 /* emit_arrayindexoutofbounds_check ********************************************
437 Emit a ArrayIndexOutOfBoundsException check.
439 *******************************************************************************/
441 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
443 if (INSTRUCTION_MUST_CHECK(iptr)) {
444 M_ILD(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
445 M_TRAPGEU(s2, REG_ITMP3);
450 /* emit_classcast_check ********************************************************
452 Emit a ClassCastException check.
454 *******************************************************************************/
456 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
458 if (INSTRUCTION_MUST_CHECK(iptr)) {
470 vm_abort("emit_classcast_check: unknown condition %d", condition);
472 M_ALD_INTERN(s1, REG_ZERO, EXCEPTION_HARDWARE_CLASSCAST);
477 /* emit_nullpointer_check ******************************************************
479 Emit a NullPointerException check.
481 *******************************************************************************/
483 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
485 if (INSTRUCTION_MUST_CHECK(iptr)) {
488 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_NULLPOINTER);
493 /* emit_exception_check ********************************************************
495 Emit an Exception check.
497 *******************************************************************************/
499 void emit_exception_check(codegendata *cd, instruction *iptr)
501 if (INSTRUCTION_MUST_CHECK(iptr)) {
504 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_EXCEPTION);
509 /* emit_trap *******************************************************************
511 Emit a trap instruction and return the original machine code.
513 *******************************************************************************/
515 uint32_t emit_trap(codegendata *cd)
519 /* Get machine code which is patched back in later. The
520 trap is 1 instruction word long. */
522 mcode = *((u4 *) cd->mcodeptr);
524 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_PATCHER);
530 /* emit_verbosecall_enter ******************************************************
532 Generates the code for the call trace.
534 *******************************************************************************/
536 void emit_verbosecall_enter(jitdata *jd)
548 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
551 /* get required compiler data */
559 /* mark trace code */
564 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
565 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + ARG_CNT + TMP_CNT) * 8));
567 M_CLR(REG_ITMP1); /* prepare a "zero" register */
569 /* save argument registers */
571 for (i = 0; i < md->paramcount; i++) {
572 if (!md->params[i].inmemory) {
573 s = md->params[i].regoff;
574 d = LA_SIZE + (1 + i) * 8;
576 switch (md->paramtypes[i].type) {
579 M_IST(REG_ITMP1, REG_SP, d); /* high-bits are zero */
580 M_IST(s, REG_SP, d + 4);
586 M_IST(REG_ITMP1, REG_SP, d); /* high-bits are zero */
587 M_FST(s, REG_SP, d + 4);
596 /* load arguments as longs */
600 for (i = 0; i < md->paramcount && i < TRACE_ARGS_NUM; i++) {
601 s = LA_SIZE + (1 + i) * 8;
602 x = PACK_REGS(abi_registers_integer_argument[d + 1],
603 abi_registers_integer_argument[d]);
610 /* put methodinfo pointer as last argument on the stack */
612 disp = dseg_add_address(cd, m);
613 M_ALD(REG_ITMP1, REG_PV, disp);
614 #if defined(__DARWIN__)
615 M_AST(REG_ITMP1, REG_SP, LA_SIZE + TRACE_ARGS_NUM * 8);
617 M_AST(REG_ITMP1, REG_SP, LA_SIZE);
619 disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
620 M_ALD(REG_ITMP2, REG_PV, disp);
624 /* restore argument registers */
626 for (i = 0; i < md->paramcount; i++) {
627 if (!md->params[i].inmemory) {
628 s = LA_SIZE + (1 + i) * 8;
629 d = md->params[i].regoff;
631 switch (md->paramtypes[i].type) {
634 M_ILD(d, REG_SP, s + 4); /* get low-bits */
640 M_FLD(d, REG_SP, s + 4); /* get low-bits */
649 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + ARG_CNT + TMP_CNT) * 8 + LA_LR_OFFSET);
651 M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + ARG_CNT + TMP_CNT) * 8);
653 /* mark trace code */
656 #endif /* !defined(NDEBUG) */
660 /* emit_verbosecall_exit *******************************************************
662 Generates the code for the call trace.
664 void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
666 *******************************************************************************/
668 void emit_verbosecall_exit(jitdata *jd)
677 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
680 /* get required compiler data */
688 /* mark trace code */
693 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
694 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4));
696 /* save return registers */
698 M_LST(REG_RESULT_PACKED, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 0) * 4);
699 M_DST(REG_FRESULT, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 2) * 4);
701 /* keep this order */
702 switch (md->returntype.type) {
705 M_INTMOVE(REG_RESULT, REG_A1);
710 M_LNGMOVE(REG_RESULT_PACKED, REG_A0_A1_PACKED);
714 M_FLTMOVE(REG_FRESULT, REG_FA0);
715 M_FLTMOVE(REG_FRESULT, REG_FA1);
717 disp = dseg_add_address(cd, m);
718 M_ALD(REG_A2, REG_PV, disp);
720 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
721 M_ALD(REG_ITMP2, REG_PV, disp);
725 /* restore return registers */
727 M_LLD(REG_RESULT_PACKED, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 0) * 4);
728 M_DLD(REG_FRESULT, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 2) * 4);
730 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4 + LA_LR_OFFSET);
732 M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4);
734 /* mark trace code */
737 #endif /* !defined(NDEBUG) */
742 * These are local overrides for various environment variables in Emacs.
743 * Please do not remove this and leave it at the end of the file, where
744 * Emacs will automagically detect them.
745 * ---------------------------------------------------------------------
748 * indent-tabs-mode: t
752 * vim:noexpandtab:sw=4:ts=4: