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"
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_classcast_check ********************************************************
446 Emit a ClassCastException check.
448 *******************************************************************************/
450 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
452 if (INSTRUCTION_MUST_CHECK(iptr)) {
464 vm_abort("emit_classcast_check: unknown condition %d", condition);
466 M_ALD_INTERN(s1, REG_ZERO, EXCEPTION_HARDWARE_CLASSCAST);
471 /* emit_nullpointer_check ******************************************************
473 Emit a NullPointerException check.
475 *******************************************************************************/
477 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
479 if (INSTRUCTION_MUST_CHECK(iptr)) {
482 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_NULLPOINTER);
487 /* emit_exception_check ********************************************************
489 Emit an Exception check.
491 *******************************************************************************/
493 void emit_exception_check(codegendata *cd, instruction *iptr)
495 if (INSTRUCTION_MUST_CHECK(iptr)) {
498 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_EXCEPTION);
503 /* emit_trap *******************************************************************
505 Emit a trap instruction and return the original machine code.
507 *******************************************************************************/
509 uint32_t emit_trap(codegendata *cd)
513 /* Get machine code which is patched back in later. The
514 trap is 1 instruction word long. */
516 mcode = *((u4 *) cd->mcodeptr);
518 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_PATCHER);
524 /* emit_verbosecall_enter ******************************************************
526 Generates the code for the call trace.
528 *******************************************************************************/
530 void emit_verbosecall_enter(jitdata *jd)
542 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
545 /* get required compiler data */
553 /* mark trace code */
558 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
559 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + ARG_CNT + TMP_CNT) * 8));
561 M_CLR(REG_ITMP1); /* prepare a "zero" register */
563 /* save argument registers */
565 for (i = 0; i < md->paramcount; i++) {
566 if (!md->params[i].inmemory) {
567 s = md->params[i].regoff;
568 d = LA_SIZE + (1 + i) * 8;
570 switch (md->paramtypes[i].type) {
573 M_IST(REG_ITMP1, REG_SP, d); /* high-bits are zero */
574 M_IST(s, REG_SP, d + 4);
580 M_IST(REG_ITMP1, REG_SP, d); /* high-bits are zero */
581 M_FST(s, REG_SP, d + 4);
590 /* load arguments as longs */
594 for (i = 0; i < md->paramcount && i < TRACE_ARGS_NUM; i++) {
595 s = LA_SIZE + (1 + i) * 8;
596 x = PACK_REGS(abi_registers_integer_argument[d + 1],
597 abi_registers_integer_argument[d]);
604 /* put methodinfo pointer as last argument on the stack */
606 disp = dseg_add_address(cd, m);
607 M_ALD(REG_ITMP1, REG_PV, disp);
608 #if defined(__DARWIN__)
609 M_AST(REG_ITMP1, REG_SP, LA_SIZE + TRACE_ARGS_NUM * 8);
611 M_AST(REG_ITMP1, REG_SP, LA_SIZE);
613 disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
614 M_ALD(REG_ITMP2, REG_PV, disp);
618 /* restore argument registers */
620 for (i = 0; i < md->paramcount; i++) {
621 if (!md->params[i].inmemory) {
622 s = LA_SIZE + (1 + i) * 8;
623 d = md->params[i].regoff;
625 switch (md->paramtypes[i].type) {
628 M_ILD(d, REG_SP, s + 4); /* get low-bits */
634 M_FLD(d, REG_SP, s + 4); /* get low-bits */
643 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + ARG_CNT + TMP_CNT) * 8 + LA_LR_OFFSET);
645 M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + ARG_CNT + TMP_CNT) * 8);
647 /* mark trace code */
650 #endif /* !defined(NDEBUG) */
654 /* emit_verbosecall_exit *******************************************************
656 Generates the code for the call trace.
658 void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
660 *******************************************************************************/
662 void emit_verbosecall_exit(jitdata *jd)
671 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
674 /* get required compiler data */
682 /* mark trace code */
687 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
688 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4));
690 /* save return registers */
692 M_LST(REG_RESULT_PACKED, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 0) * 4);
693 M_DST(REG_FRESULT, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 2) * 4);
695 /* keep this order */
696 switch (md->returntype.type) {
699 M_INTMOVE(REG_RESULT, REG_A1);
704 M_LNGMOVE(REG_RESULT_PACKED, REG_A0_A1_PACKED);
708 M_FLTMOVE(REG_FRESULT, REG_FA0);
709 M_FLTMOVE(REG_FRESULT, REG_FA1);
711 disp = dseg_add_address(cd, m);
712 M_ALD(REG_A2, REG_PV, disp);
714 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
715 M_ALD(REG_ITMP2, REG_PV, disp);
719 /* restore return registers */
721 M_LLD(REG_RESULT_PACKED, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 0) * 4);
722 M_DLD(REG_FRESULT, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 2) * 4);
724 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4 + LA_LR_OFFSET);
726 M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4);
728 /* mark trace code */
731 #endif /* !defined(NDEBUG) */
736 * These are local overrides for various environment variables in Emacs.
737 * Please do not remove this and leave it at the end of the file, where
738 * Emacs will automagically detect them.
739 * ---------------------------------------------------------------------
742 * indent-tabs-mode: t
746 * vim:noexpandtab:sw=4:ts=4: