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 8318 2007-08-16 10:05:34Z 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);
90 M_DLD(tempreg, REG_SP, disp);
93 vm_abort("emit_load: unknown type %d", src->type);
105 /* emit_load_low ***************************************************************
107 Emits a possible load of the low 32-bits of an operand.
109 *******************************************************************************/
111 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
117 assert(src->type == TYPE_LNG);
119 /* get required compiler data */
123 if (IS_INMEMORY(src->flags)) {
126 disp = src->vv.regoff;
128 M_ILD(tempreg, REG_SP, disp + 4);
133 reg = GET_LOW_REG(src->vv.regoff);
139 /* emit_load_high **************************************************************
141 Emits a possible load of the high 32-bits of an operand.
143 *******************************************************************************/
145 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
151 assert(src->type == TYPE_LNG);
153 /* get required compiler data */
157 if (IS_INMEMORY(src->flags)) {
160 disp = src->vv.regoff;
162 M_ILD(tempreg, REG_SP, disp);
167 reg = GET_HIGH_REG(src->vv.regoff);
173 /* emit_store ******************************************************************
175 Emit a possible store for the given variable.
177 *******************************************************************************/
179 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
184 /* get required compiler data */
188 if (IS_INMEMORY(dst->flags)) {
191 disp = dst->vv.regoff;
196 M_IST(d, REG_SP, disp);
199 M_LST(d, REG_SP, disp);
203 M_DST(d, REG_SP, disp);
206 vm_abort("emit_store: unknown type %d", dst->type);
212 /* emit_copy *******************************************************************
214 Generates a register/memory to register/memory copy.
216 *******************************************************************************/
218 void emit_copy(jitdata *jd, instruction *iptr)
225 /* get required compiler data */
229 /* get source and destination variables */
231 src = VAROP(iptr->s1);
232 dst = VAROP(iptr->dst);
234 if ((src->vv.regoff != dst->vv.regoff) ||
235 (IS_INMEMORY(src->flags ^ dst->flags))) {
237 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
238 /* emit nothing, as the value won't be used anyway */
242 /* If one of the variables resides in memory, we can eliminate
243 the register move from/to the temporary register with the
244 order of getting the destination register and the load. */
246 if (IS_INMEMORY(src->flags)) {
247 if (IS_LNG_TYPE(src->type))
248 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
250 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
252 s1 = emit_load(jd, iptr, src, d);
255 if (IS_LNG_TYPE(src->type))
256 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
258 s1 = emit_load(jd, iptr, src, REG_IFTMP);
260 d = codegen_reg_of_var(iptr->opc, dst, s1);
270 M_MOV(GET_LOW_REG(s1), GET_LOW_REG(d));
271 M_MOV(GET_HIGH_REG(s1), GET_HIGH_REG(d));
278 vm_abort("emit_copy: unknown type %d", src->type);
282 emit_store(jd, iptr, dst, d);
287 /* emit_iconst *****************************************************************
291 *******************************************************************************/
293 void emit_iconst(codegendata *cd, s4 d, s4 value)
297 if ((value >= -32768) && (value <= 32767))
298 M_LDA_INTERN(d, REG_ZERO, value);
300 disp = dseg_add_s4(cd, value);
301 M_ILD(d, REG_PV, disp);
306 /* emit_branch *****************************************************************
308 Emits the code for conditional and unconditional branchs.
310 *******************************************************************************/
312 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
317 /* calculate the different displacements */
319 checkdisp = disp + 4;
320 branchdisp = (disp - 4) >> 2;
322 /* check which branch to generate */
324 if (condition == BRANCH_UNCONDITIONAL) {
325 /* check displacement for overflow */
327 if ((checkdisp < (s4) 0xfe000000) || (checkdisp > (s4) 0x01fffffc)) {
328 /* if the long-branches flag isn't set yet, do it */
330 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
331 cd->flags |= (CODEGENDATA_FLAG_ERROR |
332 CODEGENDATA_FLAG_LONGBRANCHES);
335 vm_abort("emit_branch: emit unconditional long-branch code");
342 /* and displacement for overflow */
344 if ((checkdisp < (s4) 0xffff8000) || (checkdisp > (s4) 0x00007fff)) {
345 /* if the long-branches flag isn't set yet, do it */
347 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
348 cd->flags |= (CODEGENDATA_FLAG_ERROR |
349 CODEGENDATA_FLAG_LONGBRANCHES);
378 vm_abort("emit_branch: long BRANCH_NAN");
381 vm_abort("emit_branch: unknown condition %d", condition);
408 vm_abort("emit_branch: unknown condition %d", condition);
415 /* emit_arithmetic_check *******************************************************
417 Emit an ArithmeticException check.
419 *******************************************************************************/
421 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
423 if (INSTRUCTION_MUST_CHECK(iptr)) {
426 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_ARITHMETIC);
431 /* emit_arrayindexoutofbounds_check ********************************************
433 Emit a ArrayIndexOutOfBoundsException check.
435 *******************************************************************************/
437 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
439 if (INSTRUCTION_MUST_CHECK(iptr)) {
440 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
441 M_TRAPGEU(s2, REG_ITMP3);
446 /* emit_classcast_check ********************************************************
448 Emit a ClassCastException check.
450 *******************************************************************************/
452 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
454 if (INSTRUCTION_MUST_CHECK(iptr)) {
466 vm_abort("emit_classcast_check: unknown condition %d", condition);
468 M_ALD_INTERN(s1, REG_ZERO, EXCEPTION_HARDWARE_CLASSCAST);
473 /* emit_nullpointer_check ******************************************************
475 Emit a NullPointerException check.
477 *******************************************************************************/
479 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
481 if (INSTRUCTION_MUST_CHECK(iptr)) {
484 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_NULLPOINTER);
489 /* emit_exception_check ********************************************************
491 Emit an Exception check.
493 *******************************************************************************/
495 void emit_exception_check(codegendata *cd, instruction *iptr)
497 if (INSTRUCTION_MUST_CHECK(iptr)) {
500 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_EXCEPTION);
505 /* emit_trap *******************************************************************
507 Emit a trap instruction and return the original machine code.
509 *******************************************************************************/
511 uint32_t emit_trap(codegendata *cd)
515 /* Get machine code which is patched back in later. The
516 trap is 1 instruction word long. */
518 mcode = *((u4 *) cd->mcodeptr);
520 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_PATCHER);
526 /* emit_verbosecall_enter ******************************************************
528 Generates the code for the call trace.
530 *******************************************************************************/
532 void emit_verbosecall_enter(jitdata *jd)
544 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
547 /* get required compiler data */
555 /* mark trace code */
560 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
561 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + ARG_CNT + TMP_CNT) * 8));
563 M_CLR(REG_ITMP1); /* prepare a "zero" register */
565 /* save argument registers */
567 for (i = 0; i < md->paramcount; i++) {
568 if (!md->params[i].inmemory) {
569 s = md->params[i].regoff;
570 d = LA_SIZE + (1 + i) * 8;
572 switch (md->paramtypes[i].type) {
575 M_IST(REG_ITMP1, REG_SP, d); /* high-bits are zero */
576 M_IST(s, REG_SP, d + 4);
582 M_IST(REG_ITMP1, REG_SP, d); /* high-bits are zero */
583 M_FST(s, REG_SP, d + 4);
592 /* load arguments as longs */
596 for (i = 0; i < md->paramcount && i < TRACE_ARGS_NUM; i++) {
597 s = LA_SIZE + (1 + i) * 8;
598 x = PACK_REGS(abi_registers_integer_argument[d + 1],
599 abi_registers_integer_argument[d]);
606 /* put methodinfo pointer as last argument on the stack */
608 disp = dseg_add_address(cd, m);
609 M_ALD(REG_ITMP1, REG_PV, disp);
610 #if defined(__DARWIN__)
611 M_AST(REG_ITMP1, REG_SP, LA_SIZE + TRACE_ARGS_NUM * 8);
613 M_AST(REG_ITMP1, REG_SP, LA_SIZE);
615 disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
616 M_ALD(REG_ITMP2, REG_PV, disp);
620 /* restore argument registers */
622 for (i = 0; i < md->paramcount; i++) {
623 if (!md->params[i].inmemory) {
624 s = LA_SIZE + (1 + i) * 8;
625 d = md->params[i].regoff;
627 switch (md->paramtypes[i].type) {
630 M_ILD(d, REG_SP, s + 4); /* get low-bits */
636 M_FLD(d, REG_SP, s + 4); /* get low-bits */
645 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + ARG_CNT + TMP_CNT) * 8 + LA_LR_OFFSET);
647 M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + ARG_CNT + TMP_CNT) * 8);
649 /* mark trace code */
652 #endif /* !defined(NDEBUG) */
656 /* emit_verbosecall_exit *******************************************************
658 Generates the code for the call trace.
660 void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
662 *******************************************************************************/
664 void emit_verbosecall_exit(jitdata *jd)
673 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
676 /* get required compiler data */
684 /* mark trace code */
689 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
690 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4));
692 /* save return registers */
694 M_LST(REG_RESULT_PACKED, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 0) * 4);
695 M_DST(REG_FRESULT, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 2) * 4);
697 /* keep this order */
698 switch (md->returntype.type) {
701 M_INTMOVE(REG_RESULT, REG_A1);
706 M_LNGMOVE(REG_RESULT_PACKED, REG_A0_A1_PACKED);
710 M_FLTMOVE(REG_FRESULT, REG_FA0);
711 M_FLTMOVE(REG_FRESULT, REG_FA1);
713 disp = dseg_add_address(cd, m);
714 M_ALD(REG_A2, REG_PV, disp);
716 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
717 M_ALD(REG_ITMP2, REG_PV, disp);
721 /* restore return registers */
723 M_LLD(REG_RESULT_PACKED, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 0) * 4);
724 M_DLD(REG_FRESULT, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 2) * 4);
726 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4 + LA_LR_OFFSET);
728 M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4);
730 /* mark trace code */
733 #endif /* !defined(NDEBUG) */
738 * These are local overrides for various environment variables in Emacs.
739 * Please do not remove this and leave it at the end of the file, where
740 * Emacs will automagically detect them.
741 * ---------------------------------------------------------------------
744 * indent-tabs-mode: t
748 * vim:noexpandtab:sw=4:ts=4: