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
37 #include "vm/jit/arm/codegen.h"
39 #include "mm/memory.h"
41 #include "threads/lock-common.h"
43 #include "vm/builtin.h"
44 #include "vm/exceptions.h"
45 #include "vm/global.h"
47 #include "vm/jit/abi.h"
48 #include "vm/jit/asmpart.h"
49 #include "vm/jit/emit-common.h"
50 #include "vm/jit/jit.h"
51 #include "vm/jit/patcher-common.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_array_t, 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_trap *******************************************************************
565 Emit a trap instruction and return the original machine code.
567 *******************************************************************************/
569 uint32_t emit_trap(codegendata *cd)
573 /* Get machine code which is patched back in later. The
574 trap is 1 instruction word long. */
576 mcode = *((u4 *) cd->mcodeptr);
578 M_TRAP(0, EXCEPTION_HARDWARE_PATCHER);
584 /* emit_verbosecall_enter ******************************************************
586 Generates the code for the call trace.
588 *******************************************************************************/
591 void emit_verbosecall_enter(jitdata *jd)
601 /* get required compiler data */
609 /* stackframesize is changed below */
611 stackframesize = cd->stackframesize;
613 /* mark trace code */
617 /* Save argument registers to stack (including LR and PV). Keep
618 stack 8-byte aligned. */
620 M_STMFD(BITMASK_ARGS | (1<<REG_LR) | (1<<REG_PV), REG_SP);
621 M_SUB_IMM(REG_SP, REG_SP, (2 + 2 + 1 + 1) * 4); /* space for a3, a4 and m */
623 stackframesize += (6 + 2 + 2 + 1 + 1) * 4;
625 /* prepare args for tracer */
627 i = md->paramcount - 1;
632 for (; i >= 0; i--) {
633 t = md->paramtypes[i].type;
635 /* load argument into register (s1) and make it of TYPE_LNG */
637 if (!md->params[i].inmemory) {
638 s1 = md->params[i].regoff;
640 if (!IS_2_WORD_TYPE(t)) {
641 M_MOV_IMM(REG_ITMP1, 0);
642 s1 = PACK_REGS(s1, REG_ITMP1);
646 s1 = REG_ITMP12_PACKED;
647 s2 = md->params[i].regoff + stackframesize;
649 if (IS_2_WORD_TYPE(t))
650 M_LLD(s1, REG_SP, s2);
652 M_ILD(GET_LOW_REG(s1), REG_SP, s2);
653 M_MOV_IMM(GET_HIGH_REG(s1), 0);
657 /* place argument for tracer */
660 #if defined(__ARMEL__)
661 s2 = PACK_REGS(abi_registers_integer_argument[i * 2],
662 abi_registers_integer_argument[i * 2 + 1]);
663 #else /* defined(__ARMEB__) */
664 s2 = PACK_REGS(abi_registers_integer_argument[i * 2 + 1],
665 abi_registers_integer_argument[i * 2]);
671 M_LST(s1, REG_SP, s2 * 4);
675 /* prepare methodinfo pointer for tracer */
677 disp = dseg_add_address(cd, m);
678 M_DSEG_LOAD(REG_ITMP1, disp);
679 M_STR_INTERN(REG_ITMP1, REG_SP, 16);
681 /* call tracer here (we use a long branch) */
683 M_LONGBRANCH(builtin_verbosecall_enter);
685 /* Restore argument registers from stack. Keep stack 8-byte
688 M_ADD_IMM(REG_SP, REG_SP, (2 + 2 + 1 + 1) * 4); /* free argument stack */
689 M_LDMFD(BITMASK_ARGS | (1<<REG_LR) | (1<<REG_PV), REG_SP);
691 /* mark trace code */
695 #endif /* !defined(NDEBUG) */
698 /* emit_verbosecall_exit *******************************************************
700 Generates the code for the call trace.
702 void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
704 *******************************************************************************/
707 void emit_verbosecall_exit(jitdata *jd)
715 /* get required compiler data */
723 /* mark trace code */
727 /* Keep stack 8-byte aligned. */
729 M_STMFD(BITMASK_RESULT | (1<<REG_LR) | (1<<REG_PV), REG_SP);
730 M_SUB_IMM(REG_SP, REG_SP, (1 + 1) * 4); /* space for f and m */
732 switch (md->returntype.type) {
735 M_INTMOVE(REG_RESULT, GET_LOW_REG(REG_A0_A1_PACKED));
736 M_MOV_IMM(GET_HIGH_REG(REG_A0_A1_PACKED), 0);
740 M_LNGMOVE(REG_RESULT_PACKED, REG_A0_A1_PACKED);
744 M_IST(REG_RESULT, REG_SP, 0 * 4);
748 M_LNGMOVE(REG_RESULT_PACKED, REG_A2_A3_PACKED);
752 disp = dseg_add_address(cd, m);
753 M_DSEG_LOAD(REG_ITMP1, disp);
754 M_AST(REG_ITMP1, REG_SP, 1 * 4);
755 M_LONGBRANCH(builtin_verbosecall_exit);
757 /* Keep stack 8-byte aligned. */
759 M_ADD_IMM(REG_SP, REG_SP, (1 + 1) * 4); /* free argument stack */
760 M_LDMFD(BITMASK_RESULT | (1<<REG_LR) | (1<<REG_PV), REG_SP);
762 /* mark trace code */
766 #endif /* !defined(NDEBUG) */
770 * These are local overrides for various environment variables in Emacs.
771 * Please do not remove this and leave it at the end of the file, where
772 * Emacs will automagically detect them.
773 * ---------------------------------------------------------------------
776 * indent-tabs-mode: t
780 * vim:noexpandtab:sw=4:ts=4: