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;
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;
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;
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;
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);
254 /* emit_copy *******************************************************************
256 Generates a register/memory to register/memory copy.
258 *******************************************************************************/
260 void emit_copy(jitdata *jd, instruction *iptr)
267 /* get required compiler data */
271 /* get source and destination variables */
273 src = VAROP(iptr->s1);
274 dst = VAROP(iptr->dst);
276 /* XXX dummy call, removed me!!! */
277 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP1);
279 if ((src->vv.regoff != dst->vv.regoff) ||
280 ((src->flags ^ dst->flags) & INMEMORY)) {
282 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
283 /* emit nothing, as the value won't be used anyway */
287 /* If one of the variables resides in memory, we can eliminate
288 the register move from/to the temporary register with the
289 order of getting the destination register and the load. */
291 if (IS_INMEMORY(src->flags)) {
292 #if !defined(ENABLE_SOFTFLOAT)
293 if (IS_FLT_DBL_TYPE(src->type))
294 d = codegen_reg_of_var(iptr->opc, dst, REG_FTMP1);
298 if (IS_2_WORD_TYPE(src->type))
299 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
301 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP1);
304 s1 = emit_load(jd, iptr, src, d);
307 #if !defined(ENABLE_SOFTFLOAT)
308 if (IS_FLT_DBL_TYPE(src->type))
309 s1 = emit_load(jd, iptr, src, REG_FTMP1);
313 if (IS_2_WORD_TYPE(src->type))
314 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
316 s1 = emit_load(jd, iptr, src, REG_ITMP1);
319 d = codegen_reg_of_var(iptr->opc, dst, s1);
323 #if defined(ENABLE_SOFTFLOAT)
328 /* XXX grrrr, wrong direction! */
333 /* XXX grrrr, wrong direction! */
334 M_MOV(GET_LOW_REG(d), GET_LOW_REG(s1));
335 M_MOV(GET_HIGH_REG(d), GET_HIGH_REG(s1));
338 vm_abort("emit_copy: unknown type %d", src->type);
344 /* XXX grrrr, wrong direction! */
348 /* XXX grrrr, wrong direction! */
349 M_MOV(GET_LOW_REG(d), GET_LOW_REG(s1));
350 M_MOV(GET_HIGH_REG(d), GET_HIGH_REG(s1));
359 vm_abort("emit_copy: unknown type %d", src->type);
364 emit_store(jd, iptr, dst, d);
369 /* emit_iconst *****************************************************************
373 *******************************************************************************/
375 void emit_iconst(codegendata *cd, s4 d, s4 value)
382 disp = dseg_add_s4(cd, value);
383 M_DSEG_LOAD(d, disp);
388 /* emit_branch *****************************************************************
390 Emits the code for conditional and unconditional branchs.
392 *******************************************************************************/
394 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
399 /* calculate the different displacements */
401 checkdisp = (disp - 8);
402 branchdisp = (disp - 8) >> 2;
404 /* check which branch to generate */
406 if (condition == BRANCH_UNCONDITIONAL) {
407 /* check displacement for overflow */
409 if ((checkdisp < (s4) 0xff000000) || (checkdisp > (s4) 0x00ffffff)) {
410 /* if the long-branches flag isn't set yet, do it */
412 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
413 cd->flags |= (CODEGENDATA_FLAG_ERROR |
414 CODEGENDATA_FLAG_LONGBRANCHES);
417 vm_abort("emit_branch: emit unconditional long-branch code");
424 /* and displacement for overflow */
426 if ((checkdisp < (s4) 0xff000000) || (checkdisp > (s4) 0x00ffffff)) {
427 /* if the long-branches flag isn't set yet, do it */
429 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
430 cd->flags |= (CODEGENDATA_FLAG_ERROR |
431 CODEGENDATA_FLAG_LONGBRANCHES);
434 vm_abort("emit_branch: emit conditional long-branch code");
460 vm_abort("emit_branch: unknown condition %d", condition);
467 /* emit_arithmetic_check *******************************************************
469 Emit an ArithmeticException check.
471 *******************************************************************************/
473 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
475 if (INSTRUCTION_MUST_CHECK(iptr)) {
478 M_TRAPEQ(0, EXCEPTION_HARDWARE_ARITHMETIC);
483 /* emit_nullpointer_check ******************************************************
485 Emit a NullPointerException check.
487 *******************************************************************************/
489 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
491 if (INSTRUCTION_MUST_CHECK(iptr)) {
493 M_TRAPEQ(0, EXCEPTION_HARDWARE_NULLPOINTER);
497 void emit_nullpointer_check_force(codegendata *cd, instruction *iptr, s4 reg)
500 M_TRAPEQ(0, EXCEPTION_HARDWARE_NULLPOINTER);
504 /* emit_arrayindexoutofbounds_check ********************************************
506 Emit a ArrayIndexOutOfBoundsException check.
508 *******************************************************************************/
510 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
512 if (INSTRUCTION_MUST_CHECK(iptr)) {
513 M_ILD_INTERN(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
514 M_CMP(s2, REG_ITMP3);
515 M_TRAPHS(s2, EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS);
520 /* emit_classcast_check ********************************************************
522 Emit a ClassCastException check.
524 *******************************************************************************/
526 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
528 if (INSTRUCTION_MUST_CHECK(iptr)) {
531 M_TRAPEQ(s1, EXCEPTION_HARDWARE_CLASSCAST);
535 M_TRAPLE(s1, EXCEPTION_HARDWARE_CLASSCAST);
539 M_TRAPHI(s1, EXCEPTION_HARDWARE_CLASSCAST);
543 vm_abort("emit_classcast_check: unknown condition %d", condition);
548 /* emit_exception_check ********************************************************
550 Emit an Exception check.
552 *******************************************************************************/
554 void emit_exception_check(codegendata *cd, instruction *iptr)
556 if (INSTRUCTION_MUST_CHECK(iptr)) {
557 M_TST(REG_RESULT, REG_RESULT);
558 M_TRAPEQ(0, EXCEPTION_HARDWARE_EXCEPTION);
563 /* emit_patcher_stubs **********************************************************
565 Generates the code for the patcher stubs.
567 *******************************************************************************/
569 void emit_patcher_stubs(jitdata *jd)
579 /* get required compiler data */
583 /* generate patcher stub call code */
587 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
588 /* check code segment size */
592 /* Get machine code which is patched back in later. The
593 call is 1 instruction word long. */
595 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
597 mcode = *((u4 *) tmpmcodeptr);
599 /* Patch in the call to call the following code (done at
602 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
603 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
605 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 2);
608 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
610 /* create stack frame (align stack to 8-byte) */
612 M_SUB_IMM(REG_SP, REG_SP, 8 * 4);
614 /* save itmp3 onto stack */
616 M_STR_INTERN(REG_ITMP3, REG_SP, 6 * 4);
618 /* calculate return address and move it onto stack */
619 /* ATTENTION: we can not use BL to branch to patcher stub, */
620 /* ATTENTION: because we need to preserve LR for leaf methods */
622 disp = (s4) (((u4 *) cd->mcodeptr) - (((u4 *) tmpmcodeptr) + 1) + 2);
624 M_SUB_IMM_EXT_MUL4(REG_ITMP3, REG_PC, disp);
625 M_STR_INTERN(REG_ITMP3, REG_SP, 4 * 4);
627 /* move pointer to java_objectheader onto stack */
629 #if defined(ENABLE_THREADS)
630 /* order reversed because of data segment layout */
632 (void) dseg_add_unique_address(cd, NULL); /* flcword */
633 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
634 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
636 M_SUB_IMM_EXT_MUL4(REG_ITMP3, REG_PV, -disp / 4);
637 M_STR_INTERN(REG_ITMP3, REG_SP, 3 * 4);
639 M_EOR(REG_ITMP3, REG_ITMP3, REG_ITMP3);
640 M_STR_INTERN(REG_ITMP3, REG_SP, 3 * 4);
643 /* move machine code onto stack */
645 disp = dseg_add_unique_s4(cd, mcode);
646 M_DSEG_LOAD(REG_ITMP3, disp);
647 M_STR_INTERN(REG_ITMP3, REG_SP, 2 * 4);
649 /* move class/method/field reference onto stack */
651 disp = dseg_add_unique_address(cd, pref->ref);
652 M_DSEG_LOAD(REG_ITMP3, disp);
653 M_STR_INTERN(REG_ITMP3, REG_SP, 1 * 4);
655 /* move data segment displacement onto stack */
657 disp = dseg_add_unique_s4(cd, pref->disp);
658 M_DSEG_LOAD(REG_ITMP3, disp);
659 M_STR_INTERN(REG_ITMP3, REG_SP, 5 * 4);
661 /* move patcher function pointer onto stack */
663 disp = dseg_add_functionptr(cd, pref->patcher);
664 M_DSEG_LOAD(REG_ITMP3, disp);
665 M_STR_INTERN(REG_ITMP3, REG_SP, 0 * 4);
667 /* finally call the patcher via asm_patcher_wrapper */
668 /* ATTENTION: don't use REG_PV here, because some patchers need it */
670 if (targetdisp == 0) {
671 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
673 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
674 /*M_DSEG_BRANCH_NOLINK(REG_PC, REG_PV, a);*/
675 /* TODO: this is only a hack */
676 M_DSEG_LOAD(REG_ITMP3, disp);
677 M_MOV(REG_PC, REG_ITMP3);
680 disp = (((u4 *) cd->mcodebase) + targetdisp) -
681 (((u4 *) cd->mcodeptr) + 2);
689 /* emit_replacement_stubs ******************************************************
691 Generates the code for the replacement stubs.
693 *******************************************************************************/
695 #if defined(ENABLE_REPLACEMENT)
696 void emit_replacement_stubs(jitdata *jd)
705 /* get required compiler data */
710 #endif /* defined(ENABLE_REPLACEMENT) */
713 /* emit_verbosecall_enter ******************************************************
715 Generates the code for the call trace.
717 *******************************************************************************/
720 void emit_verbosecall_enter(jitdata *jd)
730 /* get required compiler data */
738 /* stackframesize is changed below */
740 stackframesize = cd->stackframesize;
742 /* mark trace code */
746 /* Save argument registers to stack (including LR and PV). Keep
747 stack 8-byte aligned. */
749 M_STMFD(BITMASK_ARGS | (1<<REG_LR) | (1<<REG_PV), REG_SP);
750 M_SUB_IMM(REG_SP, REG_SP, (2 + 2 + 1 + 1) * 4); /* space for a3, a4 and m */
752 stackframesize += (6 + 2 + 2 + 1 + 1) * 4;
754 /* prepare args for tracer */
756 i = md->paramcount - 1;
761 for (; i >= 0; i--) {
762 t = md->paramtypes[i].type;
764 /* load argument into register (s1) and make it of TYPE_LNG */
766 if (!md->params[i].inmemory) {
767 s1 = md->params[i].regoff;
769 if (!IS_2_WORD_TYPE(t)) {
770 M_MOV_IMM(REG_ITMP1, 0);
771 s1 = PACK_REGS(s1, REG_ITMP1);
775 s1 = REG_ITMP12_PACKED;
776 s2 = md->params[i].regoff + stackframesize;
778 if (IS_2_WORD_TYPE(t))
779 M_LLD(s1, REG_SP, s2);
781 M_ILD(GET_LOW_REG(s1), REG_SP, s2);
782 M_MOV_IMM(GET_HIGH_REG(s1), 0);
786 /* place argument for tracer */
789 #if defined(__ARMEL__)
790 s2 = PACK_REGS(abi_registers_integer_argument[i * 2],
791 abi_registers_integer_argument[i * 2 + 1]);
792 #else /* defined(__ARMEB__) */
793 s2 = PACK_REGS(abi_registers_integer_argument[i * 2 + 1],
794 abi_registers_integer_argument[i * 2]);
800 M_LST(s1, REG_SP, s2 * 4);
804 /* prepare methodinfo pointer for tracer */
806 disp = dseg_add_address(cd, m);
807 M_DSEG_LOAD(REG_ITMP1, disp);
808 M_STR_INTERN(REG_ITMP1, REG_SP, 16);
810 /* call tracer here (we use a long branch) */
812 M_LONGBRANCH(builtin_verbosecall_enter);
814 /* Restore argument registers from stack. Keep stack 8-byte
817 M_ADD_IMM(REG_SP, REG_SP, (2 + 2 + 1 + 1) * 4); /* free argument stack */
818 M_LDMFD(BITMASK_ARGS | (1<<REG_LR) | (1<<REG_PV), REG_SP);
820 /* mark trace code */
824 #endif /* !defined(NDEBUG) */
827 /* emit_verbosecall_exit *******************************************************
829 Generates the code for the call trace.
831 void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
833 *******************************************************************************/
836 void emit_verbosecall_exit(jitdata *jd)
844 /* get required compiler data */
852 /* mark trace code */
856 /* Keep stack 8-byte aligned. */
858 M_STMFD(BITMASK_RESULT | (1<<REG_LR) | (1<<REG_PV), REG_SP);
859 M_SUB_IMM(REG_SP, REG_SP, (1 + 1) * 4); /* space for f and m */
861 switch (md->returntype.type) {
864 M_INTMOVE(REG_RESULT, GET_LOW_REG(REG_A0_A1_PACKED));
865 M_MOV_IMM(GET_HIGH_REG(REG_A0_A1_PACKED), 0);
869 M_LNGMOVE(REG_RESULT_PACKED, REG_A0_A1_PACKED);
873 M_IST(REG_RESULT, REG_SP, 0 * 4);
877 M_LNGMOVE(REG_RESULT_PACKED, REG_A2_A3_PACKED);
881 disp = dseg_add_address(cd, m);
882 M_DSEG_LOAD(REG_ITMP1, disp);
883 M_AST(REG_ITMP1, REG_SP, 1 * 4);
884 M_LONGBRANCH(builtin_verbosecall_exit);
886 /* Keep stack 8-byte aligned. */
888 M_ADD_IMM(REG_SP, REG_SP, (1 + 1) * 4); /* free argument stack */
889 M_LDMFD(BITMASK_RESULT | (1<<REG_LR) | (1<<REG_PV), REG_SP);
891 /* mark trace code */
895 #endif /* !defined(NDEBUG) */
899 * These are local overrides for various environment variables in Emacs.
900 * Please do not remove this and leave it at the end of the file, where
901 * Emacs will automagically detect them.
902 * ---------------------------------------------------------------------
905 * indent-tabs-mode: t
909 * vim:noexpandtab:sw=4:ts=4: