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 Contact: cacao@cacaojvm.org
27 Authors: Christian Thalinger
29 $Id: emit.c 4398 2006-01-31 23:43:08Z twisti $
42 #include "vm/jit/arm/codegen.h"
44 #if defined(ENABLE_THREADS)
45 # include "threads/native/lock.h"
48 #include "vm/builtin.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 (IS_FLT_DBL_TYPE(src->type)) {
79 #if !defined(ENABLE_SOFTFLOAT)
80 if (IS_2_WORD_TYPE(src->type))
81 M_DLD(tempreg, REG_SP, disp);
83 M_FLD(tempreg, REG_SP, disp);
89 if (IS_2_WORD_TYPE(src->type))
90 M_LLD(tempreg, REG_SP, disp);
92 M_ILD(tempreg, REG_SP, disp);
104 /* emit_load_low ***************************************************************
106 Emits a possible load of the low 32-bits of a long source operand.
108 *******************************************************************************/
110 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
116 assert(src->type == TYPE_LNG);
118 /* get required compiler data */
122 if (src->flags & INMEMORY) {
125 disp = src->vv.regoff * 4;
127 #if defined(__ARMEL__)
128 M_ILD(tempreg, REG_SP, disp);
130 M_ILD(tempreg, REG_SP, disp + 4);
136 reg = GET_LOW_REG(src->vv.regoff);
142 /* emit_load_high **************************************************************
144 Emits a possible load of the high 32-bits of a long source operand.
146 *******************************************************************************/
148 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
154 assert(src->type == TYPE_LNG);
156 /* get required compiler data */
160 if (src->flags & INMEMORY) {
163 disp = src->vv.regoff * 4;
165 #if defined(__ARMEL__)
166 M_ILD(tempreg, REG_SP, disp + 4);
168 M_ILD(tempreg, REG_SP, disp);
174 reg = GET_HIGH_REG(src->vv.regoff);
180 /* emit_store ******************************************************************
182 Emits a possible store to a variable.
184 *******************************************************************************/
186 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
191 /* get required compiler data */
195 if (dst->flags & INMEMORY) {
198 disp = dst->vv.regoff * 4;
200 #if !defined(ENABLE_SOFTFLOAT)
201 if (IS_FLT_DBL_TYPE(dst->type)) {
202 if (IS_2_WORD_TYPE(dst->type))
203 M_DST(d, REG_SP, disp);
205 M_FST(d, REG_SP, disp);
208 if (IS_2_WORD_TYPE(dst->type))
209 M_LST(d, REG_SP, disp);
211 M_IST(d, REG_SP, disp);
214 if (IS_2_WORD_TYPE(dst->type))
215 M_LST(d, REG_SP, disp);
217 M_IST(d, REG_SP, disp);
220 else if (IS_LNG_TYPE(dst->type)) {
221 #if defined(__ARMEL__)
222 if (GET_HIGH_REG(dst->vv.regoff) == REG_SPLIT)
223 M_IST_INTERN(GET_HIGH_REG(d), REG_SP, 0 * 4);
225 if (GET_LOW_REG(dst->vv.regoff) == REG_SPLIT)
226 M_IST_INTERN(GET_LOW_REG(d), REG_SP, 0 * 4);
232 /* emit_copy *******************************************************************
236 *******************************************************************************/
238 void emit_copy(jitdata *jd, instruction *iptr, varinfo *src, varinfo *dst)
244 /* get required compiler data */
249 /* XXX dummy call, removed me!!! */
250 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP1);
252 if ((src->vv.regoff != dst->vv.regoff) ||
253 ((src->flags ^ dst->flags) & INMEMORY)) {
255 /* If one of the variables resides in memory, we can eliminate
256 the register move from/to the temporary register with the
257 order of getting the destination register and the load. */
259 if (IS_INMEMORY(src->flags)) {
260 #if !defined(ENABLE_SOFTFLOAT)
261 if (IS_FLT_DBL_TYPE(src->type))
262 d = codegen_reg_of_var(iptr->opc, dst, REG_FTMP1);
266 if (IS_2_WORD_TYPE(src->type))
267 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
269 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP1);
272 s1 = emit_load(jd, iptr, src, d);
275 #if !defined(ENABLE_SOFTFLOAT)
276 if (IS_FLT_DBL_TYPE(src->type))
277 s1 = emit_load(jd, iptr, src, REG_FTMP1);
281 if (IS_2_WORD_TYPE(src->type))
282 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
284 s1 = emit_load(jd, iptr, src, REG_ITMP1);
287 d = codegen_reg_of_var(iptr->opc, dst, s1);
291 #if !defined(ENABLE_SOFTFLOAT)
292 if (IS_FLT_DBL_TYPE(src->type)) {
293 if (IS_2_WORD_TYPE(src->type))
299 if (IS_2_WORD_TYPE(src->type))
302 /* XXX grrrr, wrong direction! */
306 if (IS_2_WORD_TYPE(src->type))
309 /* XXX grrrr, wrong direction! */
314 emit_store(jd, iptr, dst, d);
319 /* emit_iconst *****************************************************************
323 *******************************************************************************/
325 void emit_iconst(codegendata *cd, s4 d, s4 value)
332 disp = dseg_add_s4(cd, value);
333 M_DSEG_LOAD(d, disp);
338 /* emit_nullpointer_check ******************************************************
340 Emit a NullPointerException check.
342 *******************************************************************************/
344 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
346 if (INSTRUCTION_MUST_CHECK(iptr)) {
349 codegen_add_nullpointerexception_ref(cd);
354 /* emit_arrayindexoutofbounds_check ********************************************
356 Emit a ArrayIndexOutOfBoundsException check.
358 *******************************************************************************/
360 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
362 if (INSTRUCTION_MUST_CHECK(iptr)) {
363 M_ILD_INTERN(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
364 M_CMP(s2, REG_ITMP3);
366 codegen_add_arrayindexoutofboundsexception_ref(cd, s2);
371 /* emit_exception_stubs ********************************************************
373 Generates the code for the exception stubs.
375 *******************************************************************************/
377 void emit_exception_stubs(jitdata *jd)
385 /* get required compiler data */
390 /* generate exception stubs */
394 for (eref = cd->exceptionrefs; eref != NULL; eref = eref->next) {
395 gen_resolvebranch(cd->mcodebase + eref->branchpos,
396 eref->branchpos, cd->mcodeptr - cd->mcodebase);
400 /* Check if the exception is an
401 ArrayIndexOutOfBoundsException. If so, move index register
405 M_MOV(REG_ITMP1, eref->reg);
407 /* calcuate exception address */
409 assert((eref->branchpos - 4) % 4 == 0);
410 M_ADD_IMM_EXT_MUL4(REG_ITMP2_XPC, REG_IP, (eref->branchpos - 4) / 4);
412 /* move function to call into REG_ITMP3 */
414 disp = dseg_add_functionptr(cd, eref->function);
415 M_DSEG_LOAD(REG_ITMP3, disp);
417 if (targetdisp == 0) {
418 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
420 M_MOV(rd->argintregs[0], REG_IP);
421 M_MOV(rd->argintregs[1], REG_SP);
423 if (jd->isleafmethod)
424 M_MOV(rd->argintregs[2], REG_LR);
426 M_LDR(rd->argintregs[2], REG_SP,
427 cd->stackframesize * 4 - SIZEOF_VOID_P);
429 M_MOV(rd->argintregs[3], REG_ITMP2_XPC);
432 /* TODO: we only need to save LR in leaf methods */
434 M_STMFD(BITMASK_ARGS | 1<<REG_IP | 1<<REG_LR, REG_SP);
436 /* move a3 to stack */
438 M_STR_UPDATE(REG_ITMP1, REG_SP, -4);
440 /* do the exception call */
442 M_MOV(REG_LR, REG_PC);
443 M_MOV(REG_PC, REG_ITMP3);
445 M_ADD_IMM(REG_SP, REG_SP, 4);
447 /* result of stacktrace is our XPTR */
449 M_MOV(REG_ITMP1_XPTR, REG_RESULT);
451 /* restore registers */
453 M_LDMFD(BITMASK_ARGS | 1<<REG_IP | 1<<REG_LR, REG_SP);
455 disp = dseg_add_functionptr(cd, asm_handle_exception);
456 M_DSEG_LOAD(REG_ITMP3, disp);
457 M_MOV(REG_PC, REG_ITMP3);
460 disp = (((u4 *) cd->mcodebase) + targetdisp) -
461 (((u4 *) cd->mcodeptr) + 2);
469 /* emit_patcher_stubs **********************************************************
471 Generates the code for the patcher stubs.
473 *******************************************************************************/
475 void emit_patcher_stubs(jitdata *jd)
485 /* get required compiler data */
489 /* generate patcher stub call code */
493 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
494 /* check code segment size */
498 /* Get machine code which is patched back in later. The
499 call is 1 instruction word long. */
501 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
503 mcode = *((u4 *) tmpmcodeptr);
505 /* Patch in the call to call the following code (done at
508 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
509 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
511 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 2);
514 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
516 /* create stack frame */
518 M_SUB_IMM(REG_SP, REG_SP, 7 * 4);
520 /* save itmp3 onto stack */
522 M_STR_INTERN(REG_ITMP3, REG_SP, 6 * 4);
524 /* calculate return address and move it onto stack */
525 /* ATTENTION: we can not use BL to branch to patcher stub, */
526 /* ATTENTION: because we need to preserve LR for leaf methods */
528 disp = (s4) (((u4 *) cd->mcodeptr) - (((u4 *) tmpmcodeptr) + 1) + 2);
530 M_SUB_IMM_EXT_MUL4(REG_ITMP3, REG_PC, disp);
531 M_STR_INTERN(REG_ITMP3, REG_SP, 4 * 4);
533 /* move pointer to java_objectheader onto stack */
535 #if defined(ENABLE_THREADS)
536 /* order reversed because of data segment layout */
538 (void) dseg_add_unique_address(cd, NULL); /* flcword */
539 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
540 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
542 M_SUB_IMM_EXT_MUL4(REG_ITMP3, REG_IP, -disp / 4);
543 M_STR_INTERN(REG_ITMP3, REG_SP, 3 * 4);
545 M_EOR(REG_ITMP3, REG_ITMP3, REG_ITMP3);
546 M_STR_INTERN(REG_ITMP3, REG_SP, 3 * 4);
549 /* move machine code onto stack */
551 disp = dseg_add_unique_s4(cd, mcode);
552 M_DSEG_LOAD(REG_ITMP3, disp);
553 M_STR_INTERN(REG_ITMP3, REG_SP, 2 * 4);
555 /* move class/method/field reference onto stack */
557 disp = dseg_add_unique_address(cd, pref->ref);
558 M_DSEG_LOAD(REG_ITMP3, disp);
559 M_STR_INTERN(REG_ITMP3, REG_SP, 1 * 4);
561 /* move data segment displacement onto stack */
563 disp = dseg_add_unique_s4(cd, pref->disp);
564 M_DSEG_LOAD(REG_ITMP3, disp);
565 M_STR_INTERN(REG_ITMP3, REG_SP, 5 * 4);
567 /* move patcher function pointer onto stack */
569 disp = dseg_add_functionptr(cd, pref->patcher);
570 M_DSEG_LOAD(REG_ITMP3, disp);
571 M_STR_INTERN(REG_ITMP3, REG_SP, 0 * 4);
573 /* finally call the patcher via asm_patcher_wrapper */
574 /* ATTENTION: don't use REG_IP here, because some patchers need it */
576 if (targetdisp == 0) {
577 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
579 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
580 /*M_DSEG_BRANCH_NOLINK(REG_PC, REG_IP, a);*/
581 /* TODO: this is only a hack */
582 M_DSEG_LOAD(REG_ITMP3, disp);
583 M_MOV(REG_PC, REG_ITMP3);
586 disp = (((u4 *) cd->mcodebase) + targetdisp) -
587 (((u4 *) cd->mcodeptr) + 2);
595 /* emit_replacement_stubs ******************************************************
597 Generates the code for the replacement stubs.
599 *******************************************************************************/
601 #if defined(ENABLE_REPLACEMENT)
602 void emit_replacement_stubs(jitdata *jd)
611 /* get required compiler data */
616 #endif /* defined(ENABLE_REPLACEMENT) */
619 /* emit_verbosecall_enter ******************************************************
621 Generates the code for the call trace.
623 *******************************************************************************/
626 void emit_verbosecall_enter(jitdata *jd)
636 /* get required compiler data */
644 /* stackframesize is changed below */
646 stackframesize = cd->stackframesize;
648 /* mark trace code */
652 /* save argument registers to stack (including LR and IP) */
653 M_STMFD(BITMASK_ARGS | (1<<REG_LR) | (1<<REG_IP), REG_SP);
654 M_SUB_IMM(REG_SP, REG_SP, (2 + 2 + 1) * 4); /* space for a3, a4 and m */
656 stackframesize += 6 + 2 + 2 + 1;
658 /* prepare args for tracer */
660 i = md->paramcount - 1;
665 for (; i >= 0; i--) {
666 t = md->paramtypes[i].type;
668 /* load argument into register (s1) and make it of TYPE_LNG */
670 if (!md->params[i].inmemory) {
671 s1 = md->params[i].regoff;
673 if (!IS_2_WORD_TYPE(t)) {
674 M_MOV_IMM(REG_ITMP1, 0);
675 s1 = PACK_REGS(s1, REG_ITMP1);
678 SPLIT_OPEN(t, s1, REG_ITMP1);
679 SPLIT_LOAD(t, s1, stackframesize);
683 s1 = md->params[i].regoff + stackframesize;
685 if (IS_2_WORD_TYPE(t))
686 M_LLD(REG_ITMP12_PACKED, REG_SP, s1 * 4);
688 M_ILD(REG_ITMP1, REG_SP, s1 * 4);
691 /* place argument for tracer */
694 #if defined(__ARMEL__)
695 s2 = PACK_REGS(rd->argintregs[i * 2], rd->argintregs[i * 2 + 1]);
696 #else /* defined(__ARMEB__) */
697 s2 = PACK_REGS(rd->argintregs[i * 2 + 1], rd->argintregs[i * 2]);
703 M_LST(s1, REG_SP, s2 * 4);
707 /* prepare methodinfo pointer for tracer */
709 disp = dseg_add_address(cd, m);
710 M_DSEG_LOAD(REG_ITMP1, disp);
711 M_STR_INTERN(REG_ITMP1, REG_SP, 16);
713 /* call tracer here (we use a long branch) */
715 M_LONGBRANCH(builtin_trace_args);
717 /* restore argument registers from stack */
719 M_ADD_IMM(REG_SP, REG_SP, (2 + 2 + 1) * 4); /* free argument stack */
720 M_LDMFD(BITMASK_ARGS | (1<<REG_LR) | (1<<REG_IP), REG_SP);
722 /* mark trace code */
726 #endif /* !defined(NDEBUG) */
729 /* emit_verbosecall_exit *******************************************************
731 Generates the code for the call trace.
733 *******************************************************************************/
736 void emit_verbosecall_exit(jitdata *jd)
745 /* get required compiler data */
753 /* mark trace code */
757 M_STMFD(BITMASK_RESULT | (1<<REG_LR) | (1<<REG_IP), REG_SP);
758 M_SUB_IMM(REG_SP, REG_SP, (1 + 1) * 4); /* space for d[high reg] and f */
760 #if defined(__ARMEL__)
761 s1 = PACK_REGS(rd->argintregs[1], rd->argintregs[2]);
762 #else /* defined(__ARMEB__) */
763 s1 = PACK_REGS(rd->argintregs[2], rd->argintregs[1]);
766 switch (md->returntype.type) {
769 M_INTMOVE(REG_RESULT, GET_LOW_REG(s1));
770 M_MOV_IMM(GET_HIGH_REG(s1), 0);
774 M_LNGMOVE(REG_RESULT_PACKED, s1);
778 M_IST(REG_RESULT, REG_SP, 1 * 4);
782 s1 = rd->argintregs[3];
783 M_INTMOVE(REG_RESULT, s1);
784 M_IST(REG_RESULT2, REG_SP, 0 * 4);
788 disp = dseg_add_address(cd, m);
789 M_DSEG_LOAD(rd->argintregs[0], disp);
790 M_LONGBRANCH(builtin_displaymethodstop);
792 M_ADD_IMM(REG_SP, REG_SP, (1 + 1) * 4); /* free argument stack */
793 M_LDMFD(BITMASK_RESULT | (1<<REG_LR) | (1<<REG_IP), REG_SP);
795 /* mark trace code */
799 #endif /* !defined(NDEBUG) */
803 * These are local overrides for various environment variables in Emacs.
804 * Please do not remove this and leave it at the end of the file, where
805 * Emacs will automagically detect them.
806 * ---------------------------------------------------------------------
809 * indent-tabs-mode: t
813 * vim:noexpandtab:sw=4:ts=4: