1 /* src/vm/jit/mips/emit.c - MIPS 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
34 #include "vm/jit/mips/codegen.h"
35 #include "vm/jit/mips/md-abi.h"
37 #include "mm/memory.h"
39 #include "threads/lock-common.h"
41 #include "vm/builtin.h"
42 #include "vm/exceptions.h"
43 #include "vm/stringlocal.h" /* XXX for gen_resolvebranch */
45 #include "vm/jit/abi.h"
46 #include "vm/jit/abi-asm.h"
47 #include "vm/jit/asmpart.h"
48 #include "vm/jit/dseg.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 "vmcore/options.h"
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;
79 #if SIZEOF_VOID_P == 8
83 M_LLD(tempreg, REG_SP, disp);
88 M_ILD(tempreg, REG_SP, disp);
91 M_LLD(tempreg, REG_SP, disp);
95 M_FLD(tempreg, REG_SP, disp);
98 M_DLD(tempreg, REG_SP, disp);
101 vm_abort("emit_load: unknown type %d", src->type);
107 reg = src->vv.regoff;
113 /* emit_load_low ***************************************************************
115 Emits a possible load of the low 32-bits of an operand.
117 *******************************************************************************/
119 #if SIZEOF_VOID_P == 4
120 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
126 assert(src->type == TYPE_LNG);
128 /* get required compiler data */
132 if (src->flags & INMEMORY) {
135 disp = src->vv.regoff;
137 #if WORDS_BIGENDIAN == 1
138 M_ILD(tempreg, REG_SP, disp + 4);
140 M_ILD(tempreg, REG_SP, disp);
146 reg = GET_LOW_REG(src->vv.regoff);
150 #endif /* SIZEOF_VOID_P == 4 */
153 /* emit_load_high **************************************************************
155 Emits a possible load of the high 32-bits of an operand.
157 *******************************************************************************/
159 #if SIZEOF_VOID_P == 4
160 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
166 assert(src->type == TYPE_LNG);
168 /* get required compiler data */
172 if (src->flags & INMEMORY) {
175 disp = src->vv.regoff;
177 #if WORDS_BIGENDIAN == 1
178 M_ILD(tempreg, REG_SP, disp);
180 M_ILD(tempreg, REG_SP, disp + 4);
186 reg = GET_HIGH_REG(src->vv.regoff);
190 #endif /* SIZEOF_VOID_P == 4 */
193 /* emit_store ******************************************************************
195 Emits a possible store to variable.
197 *******************************************************************************/
199 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
204 /* get required compiler data */
208 if (dst->flags & INMEMORY) {
211 disp = dst->vv.regoff;
214 #if SIZEOF_VOID_P == 8
218 M_LST(d, REG_SP, disp);
223 M_IST(d, REG_SP, disp);
226 M_LST(d, REG_SP, disp);
230 M_FST(d, REG_SP, disp);
233 M_DST(d, REG_SP, disp);
236 vm_abort("emit_store: unknown type %d", dst->type);
242 /* emit_copy *******************************************************************
244 Generates a register/memory to register/memory copy.
246 *******************************************************************************/
248 void emit_copy(jitdata *jd, instruction *iptr)
255 /* get required compiler data */
259 /* get source and destination variables */
261 src = VAROP(iptr->s1);
262 dst = VAROP(iptr->dst);
264 if ((src->vv.regoff != dst->vv.regoff) ||
265 ((src->flags ^ dst->flags) & INMEMORY)) {
267 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
268 /* emit nothing, as the value won't be used anyway */
272 /* If one of the variables resides in memory, we can eliminate
273 the register move from/to the temporary register with the
274 order of getting the destination register and the load. */
276 if (IS_INMEMORY(src->flags)) {
277 #if SIZEOF_VOID_P == 4
278 if (IS_2_WORD_TYPE(src->type))
279 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
282 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
283 s1 = emit_load(jd, iptr, src, d);
286 s1 = emit_load(jd, iptr, src, REG_IFTMP);
287 #if SIZEOF_VOID_P == 4
288 if (IS_2_WORD_TYPE(src->type))
289 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
292 d = codegen_reg_of_var(iptr->opc, dst, s1);
297 #if SIZEOF_VOID_P == 8
319 vm_abort("emit_copy: unknown type %d", dst->type);
323 emit_store(jd, iptr, dst, d);
328 /* emit_iconst *****************************************************************
332 *******************************************************************************/
334 void emit_iconst(codegendata *cd, s4 d, s4 value)
338 if ((value >= -32768) && (value <= 32767))
339 M_IADD_IMM(REG_ZERO, value, d);
340 else if ((value >= 0) && (value <= 0xffff))
341 M_OR_IMM(REG_ZERO, value, d);
343 disp = dseg_add_s4(cd, value);
344 M_ILD(d, REG_PV, disp);
349 /* emit_lconst *****************************************************************
353 *******************************************************************************/
355 void emit_lconst(codegendata *cd, s4 d, s8 value)
359 #if SIZEOF_VOID_P == 8
360 if ((value >= -32768) && (value <= 32767))
361 M_LADD_IMM(REG_ZERO, value, d);
362 else if ((value >= 0) && (value <= 0xffff))
363 M_OR_IMM(REG_ZERO, value, d);
365 disp = dseg_add_s8(cd, value);
366 M_LLD(d, REG_PV, disp);
369 disp = dseg_add_s8(cd, value);
370 M_LLD(d, REG_PV, disp);
375 /* emit_branch *****************************************************************
377 Emits the code for conditional and unconditional branchs.
379 NOTE: The reg argument may contain two packed registers.
381 *******************************************************************************/
383 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
388 /* calculate the different displacements */
390 checkdisp = (disp - 4);
391 branchdisp = (disp - 4) >> 2;
393 /* check which branch to generate */
395 if (condition == BRANCH_UNCONDITIONAL) {
396 /* check displacement for overflow */
398 if ((checkdisp < (s4) 0xffff8000) || (checkdisp > (s4) 0x00007fff)) {
399 /* if the long-branches flag isn't set yet, do it */
401 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
402 cd->flags |= (CODEGENDATA_FLAG_ERROR |
403 CODEGENDATA_FLAG_LONGBRANCHES);
406 vm_abort("emit_branch: emit unconditional long-branch code");
414 /* and displacement for overflow */
416 if ((checkdisp < (s4) 0xffff8000) || (checkdisp > (s4) 0x00007fff)) {
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);
426 M_BNE(GET_HIGH_REG(reg), GET_LOW_REG(reg), 5);
429 M_BEQ(GET_HIGH_REG(reg), GET_LOW_REG(reg), 5);
444 vm_abort("emit_branch: unknown condition %d", condition);
447 /* The actual branch code which is over-jumped (NOTE: we
448 don't use a branch delay slot here). */
450 M_LUI(REG_ITMP3, branchdisp >> 16);
451 M_OR_IMM(REG_ITMP3, branchdisp, REG_ITMP3);
452 M_AADD(REG_PV, REG_ITMP3, REG_ITMP3);
460 M_BEQ(GET_HIGH_REG(reg), GET_LOW_REG(reg), branchdisp);
463 M_BNE(GET_HIGH_REG(reg), GET_LOW_REG(reg), branchdisp);
466 M_BLTZ(reg, branchdisp);
469 M_BGEZ(reg, branchdisp);
472 M_BGTZ(reg, branchdisp);
475 M_BLEZ(reg, branchdisp);
478 vm_abort("emit_branch: unknown condition %d", condition);
488 /* emit_arithmetic_check *******************************************************
490 Emit an ArithmeticException check.
492 *******************************************************************************/
494 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
496 if (INSTRUCTION_MUST_CHECK(iptr)) {
499 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_ARITHMETIC);
504 /* emit_arrayindexoutofbounds_check ********************************************
506 Emit an 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_CMPULT(s2, REG_ITMP3, REG_ITMP3);
515 M_BNEZ(REG_ITMP3, 2);
517 M_ALD_INTERN(s2, REG_ZERO, EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS);
522 /* emit_classcast_check ********************************************************
524 Emit a ClassCastException check.
526 *******************************************************************************/
528 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
530 if (INSTRUCTION_MUST_CHECK(iptr)) {
545 vm_abort("emit_classcast_check: unknown condition %d", condition);
549 M_ALD_INTERN(s1, REG_ZERO, EXCEPTION_HARDWARE_CLASSCAST);
554 /* emit_nullpointer_check ******************************************************
556 Emit a NullPointerException check.
558 *******************************************************************************/
560 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
562 if (INSTRUCTION_MUST_CHECK(iptr)) {
565 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_NULLPOINTER);
570 /* emit_exception_check ********************************************************
572 Emit an Exception check.
574 *******************************************************************************/
576 void emit_exception_check(codegendata *cd, instruction *iptr)
578 if (INSTRUCTION_MUST_CHECK(iptr)) {
579 M_BNEZ(REG_RESULT, 2);
581 M_ALD_INTERN(REG_RESULT, REG_ZERO, EXCEPTION_HARDWARE_EXCEPTION);
586 /* emit_trap *******************************************************************
588 Emit a trap instruction and return the original machine code.
590 *******************************************************************************/
592 uint32_t emit_trap(codegendata *cd)
596 /* Get machine code which is patched back in later. The
597 trap is 1 instruction word long. */
599 mcode = *((u4 *) cd->mcodeptr);
601 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_PATCHER);
607 /* emit_verbosecall_enter ******************************************************
609 Generates the code for the call trace.
611 *******************************************************************************/
614 void emit_verbosecall_enter(jitdata *jd)
623 /* get required compiler data */
631 /* mark trace code */
635 M_LDA(REG_SP, REG_SP, -(PA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8));
636 M_AST(REG_RA, REG_SP, PA_SIZE + 1 * 8);
638 /* save argument registers (we store the registers as address
639 types, so it's correct for MIPS32 too) */
641 for (i = 0; i < INT_ARG_CNT; i++)
642 M_AST(abi_registers_integer_argument[i], REG_SP, PA_SIZE + (2 + i) * 8);
644 for (i = 0; i < FLT_ARG_CNT; i++)
645 M_DST(abi_registers_float_argument[i], REG_SP, PA_SIZE + (2 + INT_ARG_CNT + i) * 8);
647 /* save temporary registers for leaf methods */
649 if (jd->isleafmethod) {
650 for (i = 0; i < INT_TMP_CNT; i++)
651 M_AST(rd->tmpintregs[i], REG_SP, PA_SIZE + (2 + ARG_CNT + i) * 8);
653 for (i = 0; i < FLT_TMP_CNT; i++)
654 M_DST(rd->tmpfltregs[i], REG_SP, PA_SIZE + (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
657 /* Load float arguments into integer registers. MIPS32 has less
658 float argument registers than integer ones, we need to check
661 for (i = 0; i < md->paramcount && i < INT_ARG_CNT && i < FLT_ARG_CNT; i++) {
662 t = md->paramtypes[i].type;
664 if (IS_FLT_DBL_TYPE(t)) {
665 if (IS_2_WORD_TYPE(t)) {
666 M_DST(abi_registers_float_argument[i], REG_SP, 0 * 8);
667 M_LLD(abi_registers_integer_argument[i], REG_SP, 0 * 8);
670 M_FST(abi_registers_float_argument[i], REG_SP, 0 * 8);
671 M_ILD(abi_registers_integer_argument[i], REG_SP, 0 * 8);
676 #if SIZEOF_VOID_P == 4
677 for (i = 0, j = 0; i < md->paramcount && i < TRACE_ARGS_NUM; i++) {
678 t = md->paramtypes[i].type;
680 if (IS_INT_LNG_TYPE(t)) {
681 if (IS_2_WORD_TYPE(t)) {
682 M_ILD(abi_registers_integer_argument[j], REG_SP, PA_SIZE + (2 + i) * 8);
683 M_ILD(abi_registers_integer_argument[j + 1], REG_SP, PA_SIZE + (2 + i) * 8 + 4);
686 # if WORDS_BIGENDIAN == 1
687 M_MOV(REG_ZERO, abi_registers_integer_argument[j]);
688 M_ILD(abi_registers_integer_argument[j + 1], REG_SP, PA_SIZE + (2 + i) * 8);
690 M_ILD(abi_registers_integer_argument[j], REG_SP, PA_SIZE + (2 + i) * 8);
691 M_MOV(REG_ZERO, abi_registers_integer_argument[j + 1]);
699 disp = dseg_add_address(cd, m);
700 M_ALD(REG_ITMP1, REG_PV, disp);
701 M_AST(REG_ITMP1, REG_SP, PA_SIZE + 0 * 8);
702 disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
703 M_ALD(REG_ITMP3, REG_PV, disp);
704 M_JSR(REG_RA, REG_ITMP3);
707 /* restore argument registers */
709 for (i = 0; i < INT_ARG_CNT; i++)
710 M_ALD(abi_registers_integer_argument[i], REG_SP, PA_SIZE + (2 + i) * 8);
712 for (i = 0; i < FLT_ARG_CNT; i++)
713 M_DLD(abi_registers_float_argument[i], REG_SP, PA_SIZE + (2 + INT_ARG_CNT + i) * 8);
715 /* restore temporary registers for leaf methods */
717 if (jd->isleafmethod) {
718 for (i = 0; i < INT_TMP_CNT; i++)
719 M_ALD(rd->tmpintregs[i], REG_SP, PA_SIZE + (2 + ARG_CNT + i) * 8);
721 for (i = 0; i < FLT_TMP_CNT; i++)
722 M_DLD(rd->tmpfltregs[i], REG_SP, PA_SIZE + (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
725 M_ALD(REG_RA, REG_SP, PA_SIZE + 1 * 8);
726 M_LDA(REG_SP, REG_SP, PA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8);
728 /* mark trace code */
732 #endif /* !defined(NDEBUG) */
735 /* emit_verbosecall_exit *******************************************************
737 Generates the code for the call trace.
739 void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
741 *******************************************************************************/
744 void emit_verbosecall_exit(jitdata *jd)
752 /* get required compiler data */
760 /* mark trace code */
764 #if SIZEOF_VOID_P == 8
765 M_ASUB_IMM(REG_SP, 4 * 8, REG_SP); /* keep stack 16-byte aligned */
766 M_AST(REG_RA, REG_SP, 0 * 8);
768 M_LST(REG_RESULT, REG_SP, 1 * 8);
769 M_DST(REG_FRESULT, REG_SP, 2 * 8);
771 M_MOV(REG_RESULT, REG_A0);
772 M_DMOV(REG_FRESULT, REG_FA1);
773 M_FMOV(REG_FRESULT, REG_FA2);
775 disp = dseg_add_address(cd, m);
776 M_ALD(REG_A4, REG_PV, disp);
778 M_ASUB_IMM(REG_SP, (8*4 + 4 * 8), REG_SP);
779 M_AST(REG_RA, REG_SP, 8*4 + 0 * 8);
781 M_LST(REG_RESULT_PACKED, REG_SP, 8*4 + 1 * 8);
782 M_DST(REG_FRESULT, REG_SP, 8*4 + 2 * 8);
784 switch (md->returntype.type) {
786 M_LNGMOVE(REG_RESULT_PACKED, REG_A0_A1_PACKED);
790 # if WORDS_BIGENDIAN == 1
791 M_MOV(REG_ZERO, REG_A0);
792 M_MOV(REG_RESULT, REG_A1);
794 M_MOV(REG_RESULT, REG_A0);
795 M_MOV(REG_ZERO, REG_A1);
799 M_LLD(REG_A2_A3_PACKED, REG_SP, 8*4 + 2 * 8);
800 M_FST(REG_FRESULT, REG_SP, 4*4 + 0 * 4);
802 disp = dseg_add_address(cd, m);
803 M_ALD(REG_ITMP1, REG_PV, disp);
804 M_AST(REG_ITMP1, REG_SP, 4*4 + 1 * 4);
807 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
808 M_ALD(REG_ITMP3, REG_PV, disp);
809 M_JSR(REG_RA, REG_ITMP3);
812 #if SIZEOF_VOID_P == 8
813 M_DLD(REG_FRESULT, REG_SP, 2 * 8);
814 M_LLD(REG_RESULT, REG_SP, 1 * 8);
816 M_ALD(REG_RA, REG_SP, 0 * 8);
817 M_AADD_IMM(REG_SP, 4 * 8, REG_SP);
819 M_DLD(REG_FRESULT, REG_SP, 8*4 + 2 * 8);
820 M_LLD(REG_RESULT_PACKED, REG_SP, 8*4 + 1 * 8);
822 M_ALD(REG_RA, REG_SP, 8*4 + 0 * 8);
823 M_AADD_IMM(REG_SP, 8*4 + 4 * 8, REG_SP);
826 /* mark trace code */
830 #endif /* !defined(NDEBUG) */
834 * These are local overrides for various environment variables in Emacs.
835 * Please do not remove this and leave it at the end of the file, where
836 * Emacs will automagically detect them.
837 * ---------------------------------------------------------------------
840 * indent-tabs-mode: t
844 * vim:noexpandtab:sw=4:ts=4: