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/patcher-common.h"
53 #include "vm/jit/replace.h"
55 #include "toolbox/logging.h" /* XXX for debugging only */
58 /* emit_load *******************************************************************
60 Emits a possible load of an operand.
62 *******************************************************************************/
64 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
70 /* get required compiler data */
74 if (src->flags & INMEMORY) {
77 disp = src->vv.regoff;
79 #if defined(ENABLE_SOFTFLOAT)
84 M_ILD(tempreg, REG_SP, disp);
88 M_LLD(tempreg, REG_SP, disp);
91 vm_abort("emit_load: unknown type %d", src->type);
97 M_ILD(tempreg, REG_SP, disp);
100 M_LLD(tempreg, REG_SP, disp);
103 M_FLD(tempreg, REG_SP, disp);
106 M_DLD(tempreg, REG_SP, disp);
109 vm_abort("emit_load: unknown type %d", src->type);
116 reg = src->vv.regoff;
122 /* emit_load_low ***************************************************************
124 Emits a possible load of the low 32-bits of a long source operand.
126 *******************************************************************************/
128 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
134 assert(src->type == TYPE_LNG);
136 /* get required compiler data */
140 if (src->flags & INMEMORY) {
143 disp = src->vv.regoff;
145 #if defined(__ARMEL__)
146 M_ILD(tempreg, REG_SP, disp);
148 M_ILD(tempreg, REG_SP, disp + 4);
154 reg = GET_LOW_REG(src->vv.regoff);
160 /* emit_load_high **************************************************************
162 Emits a possible load of the high 32-bits of a long source operand.
164 *******************************************************************************/
166 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
172 assert(src->type == TYPE_LNG);
174 /* get required compiler data */
178 if (src->flags & INMEMORY) {
181 disp = src->vv.regoff;
183 #if defined(__ARMEL__)
184 M_ILD(tempreg, REG_SP, disp + 4);
186 M_ILD(tempreg, REG_SP, disp);
192 reg = GET_HIGH_REG(src->vv.regoff);
198 /* emit_store ******************************************************************
200 Emits a possible store to a variable.
202 *******************************************************************************/
204 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
209 /* get required compiler data */
213 if (dst->flags & INMEMORY) {
216 disp = dst->vv.regoff;
218 #if defined(ENABLE_SOFTFLOAT)
223 M_IST(d, REG_SP, disp);
227 M_LST(d, REG_SP, disp);
230 vm_abort("emit_store: unknown type %d", dst->type);
236 M_IST(d, REG_SP, disp);
239 M_LST(d, REG_SP, disp);
242 M_FST(d, REG_SP, disp);
245 M_DST(d, REG_SP, disp);
248 vm_abort("emit_store: unknown type %d", dst->type);
255 /* emit_copy *******************************************************************
257 Generates a register/memory to register/memory copy.
259 *******************************************************************************/
261 void emit_copy(jitdata *jd, instruction *iptr)
268 /* get required compiler data */
272 /* get source and destination variables */
274 src = VAROP(iptr->s1);
275 dst = VAROP(iptr->dst);
277 /* XXX dummy call, removed me!!! */
278 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP1);
280 if ((src->vv.regoff != dst->vv.regoff) ||
281 ((src->flags ^ dst->flags) & INMEMORY)) {
283 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
284 /* emit nothing, as the value won't be used anyway */
288 /* If one of the variables resides in memory, we can eliminate
289 the register move from/to the temporary register with the
290 order of getting the destination register and the load. */
292 if (IS_INMEMORY(src->flags)) {
293 #if !defined(ENABLE_SOFTFLOAT)
294 if (IS_FLT_DBL_TYPE(src->type))
295 d = codegen_reg_of_var(iptr->opc, dst, REG_FTMP1);
299 if (IS_2_WORD_TYPE(src->type))
300 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
302 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP1);
305 s1 = emit_load(jd, iptr, src, d);
308 #if !defined(ENABLE_SOFTFLOAT)
309 if (IS_FLT_DBL_TYPE(src->type))
310 s1 = emit_load(jd, iptr, src, REG_FTMP1);
314 if (IS_2_WORD_TYPE(src->type))
315 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
317 s1 = emit_load(jd, iptr, src, REG_ITMP1);
320 d = codegen_reg_of_var(iptr->opc, dst, s1);
324 #if defined(ENABLE_SOFTFLOAT)
329 /* XXX grrrr, wrong direction! */
334 /* XXX grrrr, wrong direction! */
335 M_MOV(GET_LOW_REG(d), GET_LOW_REG(s1));
336 M_MOV(GET_HIGH_REG(d), GET_HIGH_REG(s1));
339 vm_abort("emit_copy: unknown type %d", src->type);
345 /* XXX grrrr, wrong direction! */
349 /* XXX grrrr, wrong direction! */
350 M_MOV(GET_LOW_REG(d), GET_LOW_REG(s1));
351 M_MOV(GET_HIGH_REG(d), GET_HIGH_REG(s1));
360 vm_abort("emit_copy: unknown type %d", src->type);
365 emit_store(jd, iptr, dst, d);
370 /* emit_iconst *****************************************************************
374 *******************************************************************************/
376 void emit_iconst(codegendata *cd, s4 d, s4 value)
383 disp = dseg_add_s4(cd, value);
384 M_DSEG_LOAD(d, disp);
389 /* emit_branch *****************************************************************
391 Emits the code for conditional and unconditional branchs.
393 *******************************************************************************/
395 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
400 /* calculate the different displacements */
402 checkdisp = (disp - 8);
403 branchdisp = (disp - 8) >> 2;
405 /* check which branch to generate */
407 if (condition == BRANCH_UNCONDITIONAL) {
408 /* check displacement for overflow */
410 if ((checkdisp < (s4) 0xff000000) || (checkdisp > (s4) 0x00ffffff)) {
411 /* if the long-branches flag isn't set yet, do it */
413 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
414 cd->flags |= (CODEGENDATA_FLAG_ERROR |
415 CODEGENDATA_FLAG_LONGBRANCHES);
418 vm_abort("emit_branch: emit unconditional long-branch code");
425 /* and displacement for overflow */
427 if ((checkdisp < (s4) 0xff000000) || (checkdisp > (s4) 0x00ffffff)) {
428 /* if the long-branches flag isn't set yet, do it */
430 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
431 cd->flags |= (CODEGENDATA_FLAG_ERROR |
432 CODEGENDATA_FLAG_LONGBRANCHES);
435 vm_abort("emit_branch: emit conditional long-branch code");
461 vm_abort("emit_branch: unknown condition %d", condition);
468 /* emit_arithmetic_check *******************************************************
470 Emit an ArithmeticException check.
472 *******************************************************************************/
474 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
476 if (INSTRUCTION_MUST_CHECK(iptr)) {
479 M_TRAPEQ(0, EXCEPTION_HARDWARE_ARITHMETIC);
484 /* emit_nullpointer_check ******************************************************
486 Emit a NullPointerException check.
488 *******************************************************************************/
490 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
492 if (INSTRUCTION_MUST_CHECK(iptr)) {
494 M_TRAPEQ(0, EXCEPTION_HARDWARE_NULLPOINTER);
498 void emit_nullpointer_check_force(codegendata *cd, instruction *iptr, s4 reg)
501 M_TRAPEQ(0, EXCEPTION_HARDWARE_NULLPOINTER);
505 /* emit_arrayindexoutofbounds_check ********************************************
507 Emit a ArrayIndexOutOfBoundsException check.
509 *******************************************************************************/
511 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
513 if (INSTRUCTION_MUST_CHECK(iptr)) {
514 M_ILD_INTERN(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
515 M_CMP(s2, REG_ITMP3);
516 M_TRAPHS(s2, EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS);
521 /* emit_classcast_check ********************************************************
523 Emit a ClassCastException check.
525 *******************************************************************************/
527 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
529 if (INSTRUCTION_MUST_CHECK(iptr)) {
532 M_TRAPEQ(s1, EXCEPTION_HARDWARE_CLASSCAST);
536 M_TRAPLE(s1, EXCEPTION_HARDWARE_CLASSCAST);
540 M_TRAPHI(s1, EXCEPTION_HARDWARE_CLASSCAST);
544 vm_abort("emit_classcast_check: unknown condition %d", condition);
549 /* emit_exception_check ********************************************************
551 Emit an Exception check.
553 *******************************************************************************/
555 void emit_exception_check(codegendata *cd, instruction *iptr)
557 if (INSTRUCTION_MUST_CHECK(iptr)) {
558 M_TST(REG_RESULT, REG_RESULT);
559 M_TRAPEQ(0, EXCEPTION_HARDWARE_EXCEPTION);
564 /* emit_patcher_traps **********************************************************
566 Generates the code for the patcher traps.
568 *******************************************************************************/
570 void emit_patcher_traps(jitdata *jd)
578 /* get required compiler data */
583 /* generate patcher traps code */
585 for (pr = list_first_unsynced(code->patchers); pr != NULL; pr = list_next_unsynced(code->patchers, pr)) {
587 /* Get machine code which is patched back in later. The
588 trap is 1 instruction word long. */
590 tmpmcodeptr = (u1 *) (cd->mcodebase + pr->mpc);
592 pr->mcode = *((u4 *) tmpmcodeptr);
594 /* Patch in the trap to call the signal handler (done at
597 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
598 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
600 M_TRAP(0, EXCEPTION_HARDWARE_PATCHER);
602 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
607 /* emit_verbosecall_enter ******************************************************
609 Generates the code for the call trace.
611 *******************************************************************************/
614 void emit_verbosecall_enter(jitdata *jd)
624 /* get required compiler data */
632 /* stackframesize is changed below */
634 stackframesize = cd->stackframesize;
636 /* mark trace code */
640 /* Save argument registers to stack (including LR and PV). Keep
641 stack 8-byte aligned. */
643 M_STMFD(BITMASK_ARGS | (1<<REG_LR) | (1<<REG_PV), REG_SP);
644 M_SUB_IMM(REG_SP, REG_SP, (2 + 2 + 1 + 1) * 4); /* space for a3, a4 and m */
646 stackframesize += (6 + 2 + 2 + 1 + 1) * 4;
648 /* prepare args for tracer */
650 i = md->paramcount - 1;
655 for (; i >= 0; i--) {
656 t = md->paramtypes[i].type;
658 /* load argument into register (s1) and make it of TYPE_LNG */
660 if (!md->params[i].inmemory) {
661 s1 = md->params[i].regoff;
663 if (!IS_2_WORD_TYPE(t)) {
664 M_MOV_IMM(REG_ITMP1, 0);
665 s1 = PACK_REGS(s1, REG_ITMP1);
669 s1 = REG_ITMP12_PACKED;
670 s2 = md->params[i].regoff + stackframesize;
672 if (IS_2_WORD_TYPE(t))
673 M_LLD(s1, REG_SP, s2);
675 M_ILD(GET_LOW_REG(s1), REG_SP, s2);
676 M_MOV_IMM(GET_HIGH_REG(s1), 0);
680 /* place argument for tracer */
683 #if defined(__ARMEL__)
684 s2 = PACK_REGS(abi_registers_integer_argument[i * 2],
685 abi_registers_integer_argument[i * 2 + 1]);
686 #else /* defined(__ARMEB__) */
687 s2 = PACK_REGS(abi_registers_integer_argument[i * 2 + 1],
688 abi_registers_integer_argument[i * 2]);
694 M_LST(s1, REG_SP, s2 * 4);
698 /* prepare methodinfo pointer for tracer */
700 disp = dseg_add_address(cd, m);
701 M_DSEG_LOAD(REG_ITMP1, disp);
702 M_STR_INTERN(REG_ITMP1, REG_SP, 16);
704 /* call tracer here (we use a long branch) */
706 M_LONGBRANCH(builtin_verbosecall_enter);
708 /* Restore argument registers from stack. Keep stack 8-byte
711 M_ADD_IMM(REG_SP, REG_SP, (2 + 2 + 1 + 1) * 4); /* free argument stack */
712 M_LDMFD(BITMASK_ARGS | (1<<REG_LR) | (1<<REG_PV), REG_SP);
714 /* mark trace code */
718 #endif /* !defined(NDEBUG) */
721 /* emit_verbosecall_exit *******************************************************
723 Generates the code for the call trace.
725 void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
727 *******************************************************************************/
730 void emit_verbosecall_exit(jitdata *jd)
738 /* get required compiler data */
746 /* mark trace code */
750 /* Keep stack 8-byte aligned. */
752 M_STMFD(BITMASK_RESULT | (1<<REG_LR) | (1<<REG_PV), REG_SP);
753 M_SUB_IMM(REG_SP, REG_SP, (1 + 1) * 4); /* space for f and m */
755 switch (md->returntype.type) {
758 M_INTMOVE(REG_RESULT, GET_LOW_REG(REG_A0_A1_PACKED));
759 M_MOV_IMM(GET_HIGH_REG(REG_A0_A1_PACKED), 0);
763 M_LNGMOVE(REG_RESULT_PACKED, REG_A0_A1_PACKED);
767 M_IST(REG_RESULT, REG_SP, 0 * 4);
771 M_LNGMOVE(REG_RESULT_PACKED, REG_A2_A3_PACKED);
775 disp = dseg_add_address(cd, m);
776 M_DSEG_LOAD(REG_ITMP1, disp);
777 M_AST(REG_ITMP1, REG_SP, 1 * 4);
778 M_LONGBRANCH(builtin_verbosecall_exit);
780 /* Keep stack 8-byte aligned. */
782 M_ADD_IMM(REG_SP, REG_SP, (1 + 1) * 4); /* free argument stack */
783 M_LDMFD(BITMASK_RESULT | (1<<REG_LR) | (1<<REG_PV), REG_SP);
785 /* mark trace code */
789 #endif /* !defined(NDEBUG) */
793 * These are local overrides for various environment variables in Emacs.
794 * Please do not remove this and leave it at the end of the file, where
795 * Emacs will automagically detect them.
796 * ---------------------------------------------------------------------
799 * indent-tabs-mode: t
803 * vim:noexpandtab:sw=4:ts=4: