1 /* src/vm/jit/arm/emit.c - Arm 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 4398 2006-01-31 23:43:08Z twisti $
38 #include "vm/jit/arm/codegen.h"
40 #include "mm/memory.h"
42 #include "threads/lock-common.h"
44 #include "vm/builtin.h"
45 #include "vm/exceptions.h"
46 #include "vm/global.h"
48 #include "vm/jit/abi.h"
49 #include "vm/jit/asmpart.h"
50 #include "vm/jit/emit-common.h"
51 #include "vm/jit/jit.h"
52 #include "vm/jit/replace.h"
54 #include "toolbox/logging.h" /* XXX for debugging only */
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 (src->flags & INMEMORY) {
76 disp = src->vv.regoff * 4;
78 #if defined(ENABLE_SOFTFLOAT)
83 M_ILD(tempreg, REG_SP, disp);
87 M_LLD(tempreg, REG_SP, disp);
90 vm_abort("emit_load: unknown type %d", src->type);
96 M_ILD(tempreg, REG_SP, disp);
99 M_LLD(tempreg, REG_SP, disp);
102 M_FLD(tempreg, REG_SP, disp);
105 M_DLD(tempreg, REG_SP, disp);
108 vm_abort("emit_load: unknown type %d", src->type);
115 reg = src->vv.regoff;
121 /* emit_load_low ***************************************************************
123 Emits a possible load of the low 32-bits of a long source operand.
125 *******************************************************************************/
127 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
133 assert(src->type == TYPE_LNG);
135 /* get required compiler data */
139 if (src->flags & INMEMORY) {
142 disp = src->vv.regoff * 4;
144 #if defined(__ARMEL__)
145 M_ILD(tempreg, REG_SP, disp);
147 M_ILD(tempreg, REG_SP, disp + 4);
153 reg = GET_LOW_REG(src->vv.regoff);
159 /* emit_load_high **************************************************************
161 Emits a possible load of the high 32-bits of a long source operand.
163 *******************************************************************************/
165 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
171 assert(src->type == TYPE_LNG);
173 /* get required compiler data */
177 if (src->flags & INMEMORY) {
180 disp = src->vv.regoff * 4;
182 #if defined(__ARMEL__)
183 M_ILD(tempreg, REG_SP, disp + 4);
185 M_ILD(tempreg, REG_SP, disp);
191 reg = GET_HIGH_REG(src->vv.regoff);
197 /* emit_store ******************************************************************
199 Emits a possible store to a variable.
201 *******************************************************************************/
203 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
208 /* get required compiler data */
212 if (dst->flags & INMEMORY) {
215 disp = dst->vv.regoff * 4;
217 #if defined(ENABLE_SOFTFLOAT)
222 M_IST(d, REG_SP, disp);
226 M_LST(d, REG_SP, disp);
229 vm_abort("emit_store: unknown type %d", dst->type);
235 M_IST(d, REG_SP, disp);
238 M_LST(d, REG_SP, disp);
241 M_FST(d, REG_SP, disp);
244 M_DST(d, REG_SP, disp);
247 vm_abort("emit_store: unknown type %d", dst->type);
251 else if (IS_LNG_TYPE(dst->type)) {
252 #if defined(__ARMEL__)
253 assert(GET_HIGH_REG(dst->vv.regoff) != REG_SPLIT);
255 assert(GET_LOW_REG(dst->vv.regoff) != REG_SPLIT);
261 /* emit_copy *******************************************************************
263 Generates a register/memory to register/memory copy.
265 *******************************************************************************/
267 void emit_copy(jitdata *jd, instruction *iptr)
274 /* get required compiler data */
278 /* get source and destination variables */
280 src = VAROP(iptr->s1);
281 dst = VAROP(iptr->dst);
283 /* XXX dummy call, removed me!!! */
284 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP1);
286 if ((src->vv.regoff != dst->vv.regoff) ||
287 ((src->flags ^ dst->flags) & INMEMORY)) {
289 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
290 /* emit nothing, as the value won't be used anyway */
294 /* If one of the variables resides in memory, we can eliminate
295 the register move from/to the temporary register with the
296 order of getting the destination register and the load. */
298 if (IS_INMEMORY(src->flags)) {
299 #if !defined(ENABLE_SOFTFLOAT)
300 if (IS_FLT_DBL_TYPE(src->type))
301 d = codegen_reg_of_var(iptr->opc, dst, REG_FTMP1);
305 if (IS_2_WORD_TYPE(src->type))
306 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
308 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP1);
311 s1 = emit_load(jd, iptr, src, d);
314 #if !defined(ENABLE_SOFTFLOAT)
315 if (IS_FLT_DBL_TYPE(src->type))
316 s1 = emit_load(jd, iptr, src, REG_FTMP1);
320 if (IS_2_WORD_TYPE(src->type))
321 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
323 s1 = emit_load(jd, iptr, src, REG_ITMP1);
326 d = codegen_reg_of_var(iptr->opc, dst, s1);
330 #if defined(ENABLE_SOFTFLOAT)
335 /* XXX grrrr, wrong direction! */
340 /* XXX grrrr, wrong direction! */
341 M_MOV(GET_LOW_REG(d), GET_LOW_REG(s1));
342 M_MOV(GET_HIGH_REG(d), GET_HIGH_REG(s1));
345 vm_abort("emit_copy: unknown type %d", src->type);
351 /* XXX grrrr, wrong direction! */
355 /* XXX grrrr, wrong direction! */
356 M_MOV(GET_LOW_REG(d), GET_LOW_REG(s1));
357 M_MOV(GET_HIGH_REG(d), GET_HIGH_REG(s1));
366 vm_abort("emit_copy: unknown type %d", src->type);
371 emit_store(jd, iptr, dst, d);
376 /* emit_iconst *****************************************************************
380 *******************************************************************************/
382 void emit_iconst(codegendata *cd, s4 d, s4 value)
389 disp = dseg_add_s4(cd, value);
390 M_DSEG_LOAD(d, disp);
395 /* emit_branch *****************************************************************
397 Emits the code for conditional and unconditional branchs.
399 *******************************************************************************/
401 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
406 /* calculate the different displacements */
408 checkdisp = (disp - 8);
409 branchdisp = (disp - 8) >> 2;
411 /* check which branch to generate */
413 if (condition == BRANCH_UNCONDITIONAL) {
414 /* check displacement for overflow */
416 if ((checkdisp < (s4) 0xff000000) || (checkdisp > (s4) 0x00ffffff)) {
417 /* if the long-branches flag isn't set yet, do it */
419 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
420 cd->flags |= (CODEGENDATA_FLAG_ERROR |
421 CODEGENDATA_FLAG_LONGBRANCHES);
424 vm_abort("emit_branch: emit unconditional long-branch code");
431 /* and displacement for overflow */
433 if ((checkdisp < (s4) 0xff000000) || (checkdisp > (s4) 0x00ffffff)) {
434 /* if the long-branches flag isn't set yet, do it */
436 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
437 cd->flags |= (CODEGENDATA_FLAG_ERROR |
438 CODEGENDATA_FLAG_LONGBRANCHES);
441 vm_abort("emit_branch: emit conditional long-branch code");
467 vm_abort("emit_branch: unknown condition %d", condition);
474 /* emit_arithmetic_check *******************************************************
476 Emit an ArithmeticException check.
478 *******************************************************************************/
480 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
482 if (INSTRUCTION_MUST_CHECK(iptr)) {
485 M_TRAPEQ(0, EXCEPTION_HARDWARE_ARITHMETIC);
490 /* emit_nullpointer_check ******************************************************
492 Emit a NullPointerException check.
494 *******************************************************************************/
496 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
498 if (INSTRUCTION_MUST_CHECK(iptr)) {
500 M_TRAPEQ(0, EXCEPTION_HARDWARE_NULLPOINTER);
504 void emit_nullpointer_check_force(codegendata *cd, instruction *iptr, s4 reg)
507 M_TRAPEQ(0, EXCEPTION_HARDWARE_NULLPOINTER);
511 /* emit_arrayindexoutofbounds_check ********************************************
513 Emit a ArrayIndexOutOfBoundsException check.
515 *******************************************************************************/
517 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
519 if (INSTRUCTION_MUST_CHECK(iptr)) {
520 M_ILD_INTERN(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
521 M_CMP(s2, REG_ITMP3);
522 M_TRAPHS(s2, EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS);
527 /* emit_classcast_check ********************************************************
529 Emit a ClassCastException check.
531 *******************************************************************************/
533 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
535 if (INSTRUCTION_MUST_CHECK(iptr)) {
538 M_TRAPEQ(s1, EXCEPTION_HARDWARE_CLASSCAST);
542 M_TRAPLE(s1, EXCEPTION_HARDWARE_CLASSCAST);
546 M_TRAPHI(s1, EXCEPTION_HARDWARE_CLASSCAST);
550 vm_abort("emit_classcast_check: unknown condition %d", condition);
555 /* emit_exception_check ********************************************************
557 Emit an Exception check.
559 *******************************************************************************/
561 void emit_exception_check(codegendata *cd, instruction *iptr)
563 if (INSTRUCTION_MUST_CHECK(iptr)) {
564 M_TST(REG_RESULT, REG_RESULT);
565 M_TRAPEQ(0, EXCEPTION_HARDWARE_EXCEPTION);
570 /* emit_patcher_stubs **********************************************************
572 Generates the code for the patcher stubs.
574 *******************************************************************************/
576 void emit_patcher_stubs(jitdata *jd)
586 /* get required compiler data */
590 /* generate patcher stub call code */
594 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
595 /* check code segment size */
599 /* Get machine code which is patched back in later. The
600 call is 1 instruction word long. */
602 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
604 mcode = *((u4 *) tmpmcodeptr);
606 /* Patch in the call to call the following code (done at
609 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
610 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
612 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 2);
615 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
617 /* create stack frame (align stack to 8-byte) */
619 M_SUB_IMM(REG_SP, REG_SP, 8 * 4);
621 /* save itmp3 onto stack */
623 M_STR_INTERN(REG_ITMP3, REG_SP, 6 * 4);
625 /* calculate return address and move it onto stack */
626 /* ATTENTION: we can not use BL to branch to patcher stub, */
627 /* ATTENTION: because we need to preserve LR for leaf methods */
629 disp = (s4) (((u4 *) cd->mcodeptr) - (((u4 *) tmpmcodeptr) + 1) + 2);
631 M_SUB_IMM_EXT_MUL4(REG_ITMP3, REG_PC, disp);
632 M_STR_INTERN(REG_ITMP3, REG_SP, 4 * 4);
634 /* move pointer to java_objectheader onto stack */
636 #if defined(ENABLE_THREADS)
637 /* order reversed because of data segment layout */
639 (void) dseg_add_unique_address(cd, NULL); /* flcword */
640 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
641 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
643 M_SUB_IMM_EXT_MUL4(REG_ITMP3, REG_PV, -disp / 4);
644 M_STR_INTERN(REG_ITMP3, REG_SP, 3 * 4);
646 M_EOR(REG_ITMP3, REG_ITMP3, REG_ITMP3);
647 M_STR_INTERN(REG_ITMP3, REG_SP, 3 * 4);
650 /* move machine code onto stack */
652 disp = dseg_add_unique_s4(cd, mcode);
653 M_DSEG_LOAD(REG_ITMP3, disp);
654 M_STR_INTERN(REG_ITMP3, REG_SP, 2 * 4);
656 /* move class/method/field reference onto stack */
658 disp = dseg_add_unique_address(cd, pref->ref);
659 M_DSEG_LOAD(REG_ITMP3, disp);
660 M_STR_INTERN(REG_ITMP3, REG_SP, 1 * 4);
662 /* move data segment displacement onto stack */
664 disp = dseg_add_unique_s4(cd, pref->disp);
665 M_DSEG_LOAD(REG_ITMP3, disp);
666 M_STR_INTERN(REG_ITMP3, REG_SP, 5 * 4);
668 /* move patcher function pointer onto stack */
670 disp = dseg_add_functionptr(cd, pref->patcher);
671 M_DSEG_LOAD(REG_ITMP3, disp);
672 M_STR_INTERN(REG_ITMP3, REG_SP, 0 * 4);
674 /* finally call the patcher via asm_patcher_wrapper */
675 /* ATTENTION: don't use REG_PV here, because some patchers need it */
677 if (targetdisp == 0) {
678 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
680 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
681 /*M_DSEG_BRANCH_NOLINK(REG_PC, REG_PV, a);*/
682 /* TODO: this is only a hack */
683 M_DSEG_LOAD(REG_ITMP3, disp);
684 M_MOV(REG_PC, REG_ITMP3);
687 disp = (((u4 *) cd->mcodebase) + targetdisp) -
688 (((u4 *) cd->mcodeptr) + 2);
696 /* emit_replacement_stubs ******************************************************
698 Generates the code for the replacement stubs.
700 *******************************************************************************/
702 #if defined(ENABLE_REPLACEMENT)
703 void emit_replacement_stubs(jitdata *jd)
712 /* get required compiler data */
717 #endif /* defined(ENABLE_REPLACEMENT) */
720 /* emit_verbosecall_enter ******************************************************
722 Generates the code for the call trace.
724 *******************************************************************************/
727 void emit_verbosecall_enter(jitdata *jd)
737 /* get required compiler data */
745 /* stackframesize is changed below */
747 stackframesize = cd->stackframesize;
749 /* mark trace code */
753 /* Save argument registers to stack (including LR and PV). Keep
754 stack 8-byte aligned. */
756 M_STMFD(BITMASK_ARGS | (1<<REG_LR) | (1<<REG_PV), REG_SP);
757 M_SUB_IMM(REG_SP, REG_SP, (2 + 2 + 1 + 1) * 4); /* space for a3, a4 and m */
759 stackframesize += 6 + 2 + 2 + 1 + 1;
761 /* prepare args for tracer */
763 i = md->paramcount - 1;
768 for (; i >= 0; i--) {
769 t = md->paramtypes[i].type;
771 /* load argument into register (s1) and make it of TYPE_LNG */
773 if (!md->params[i].inmemory) {
774 s1 = md->params[i].regoff;
776 if (!IS_2_WORD_TYPE(t)) {
777 M_MOV_IMM(REG_ITMP1, 0);
778 s1 = PACK_REGS(s1, REG_ITMP1);
781 SPLIT_OPEN(t, s1, REG_ITMP1);
782 SPLIT_LOAD(t, s1, stackframesize);
786 s1 = REG_ITMP12_PACKED;
787 s2 = md->params[i].regoff + stackframesize;
789 if (IS_2_WORD_TYPE(t))
790 M_LLD(s1, REG_SP, s2 * 4);
792 M_ILD(GET_LOW_REG(s1), REG_SP, s2 * 4);
793 M_MOV_IMM(GET_HIGH_REG(s1), 0);
797 /* place argument for tracer */
800 #if defined(__ARMEL__)
801 s2 = PACK_REGS(abi_registers_integer_argument[i * 2],
802 abi_registers_integer_argument[i * 2 + 1]);
803 #else /* defined(__ARMEB__) */
804 s2 = PACK_REGS(abi_registers_integer_argument[i * 2 + 1],
805 abi_registers_integer_argument[i * 2]);
811 M_LST(s1, REG_SP, s2 * 4);
815 /* prepare methodinfo pointer for tracer */
817 disp = dseg_add_address(cd, m);
818 M_DSEG_LOAD(REG_ITMP1, disp);
819 M_STR_INTERN(REG_ITMP1, REG_SP, 16);
821 /* call tracer here (we use a long branch) */
823 M_LONGBRANCH(builtin_verbosecall_enter);
825 /* Restore argument registers from stack. Keep stack 8-byte
828 M_ADD_IMM(REG_SP, REG_SP, (2 + 2 + 1 + 1) * 4); /* free argument stack */
829 M_LDMFD(BITMASK_ARGS | (1<<REG_LR) | (1<<REG_PV), REG_SP);
831 /* mark trace code */
835 #endif /* !defined(NDEBUG) */
838 /* emit_verbosecall_exit *******************************************************
840 Generates the code for the call trace.
842 void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
844 *******************************************************************************/
847 void emit_verbosecall_exit(jitdata *jd)
855 /* get required compiler data */
863 /* mark trace code */
867 /* Keep stack 8-byte aligned. */
869 M_STMFD(BITMASK_RESULT | (1<<REG_LR) | (1<<REG_PV), REG_SP);
870 M_SUB_IMM(REG_SP, REG_SP, (1 + 1) * 4); /* space for f and m */
872 switch (md->returntype.type) {
875 M_INTMOVE(REG_RESULT, GET_LOW_REG(REG_A0_A1_PACKED));
876 M_MOV_IMM(GET_HIGH_REG(REG_A0_A1_PACKED), 0);
880 M_LNGMOVE(REG_RESULT_PACKED, REG_A0_A1_PACKED);
884 M_IST(REG_RESULT, REG_SP, 0 * 4);
888 M_LNGMOVE(REG_RESULT_PACKED, REG_A2_A3_PACKED);
892 disp = dseg_add_address(cd, m);
893 M_DSEG_LOAD(REG_ITMP1, disp);
894 M_AST(REG_ITMP1, REG_SP, 1 * 4);
895 M_LONGBRANCH(builtin_verbosecall_exit);
897 /* Keep stack 8-byte aligned. */
899 M_ADD_IMM(REG_SP, REG_SP, (1 + 1) * 4); /* free argument stack */
900 M_LDMFD(BITMASK_RESULT | (1<<REG_LR) | (1<<REG_PV), REG_SP);
902 /* mark trace code */
906 #endif /* !defined(NDEBUG) */
910 * These are local overrides for various environment variables in Emacs.
911 * Please do not remove this and leave it at the end of the file, where
912 * Emacs will automagically detect them.
913 * ---------------------------------------------------------------------
916 * indent-tabs-mode: t
920 * vim:noexpandtab:sw=4:ts=4: