1 /* src/vm/jit/mips/emit.c - MIPS code emitter functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 #include "vm/jit/mips/codegen.h"
33 #include "vm/jit/mips/md-abi.h"
35 #include "mm/memory.h"
37 #include "threads/lock-common.h"
39 #include "vm/builtin.h"
40 #include "vm/exceptions.h"
41 #include "vm/stringlocal.h" /* XXX for gen_resolvebranch */
43 #include "vm/jit/abi.h"
44 #include "vm/jit/abi-asm.h"
45 #include "vm/jit/asmpart.h"
46 #include "vm/jit/dseg.h"
47 #include "vm/jit/emit-common.h"
48 #include "vm/jit/jit.h"
49 #include "vm/jit/patcher-common.h"
50 #include "vm/jit/replace.h"
51 #include "vm/jit/trap.h"
53 #include "vmcore/options.h"
56 /* emit_load *******************************************************************
58 Emits a possible load of an operand.
60 *******************************************************************************/
62 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
68 /* get required compiler data */
72 if (src->flags & INMEMORY) {
75 disp = src->vv.regoff;
78 #if SIZEOF_VOID_P == 8
82 M_LLD(tempreg, REG_SP, disp);
87 M_ILD(tempreg, REG_SP, disp);
90 M_LLD(tempreg, REG_SP, disp);
94 M_FLD(tempreg, REG_SP, disp);
97 M_DLD(tempreg, REG_SP, disp);
100 vm_abort("emit_load: unknown type %d", src->type);
106 reg = src->vv.regoff;
112 /* emit_load_low ***************************************************************
114 Emits a possible load of the low 32-bits of an operand.
116 *******************************************************************************/
118 #if SIZEOF_VOID_P == 4
119 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
125 assert(src->type == TYPE_LNG);
127 /* get required compiler data */
131 if (src->flags & INMEMORY) {
134 disp = src->vv.regoff;
136 #if WORDS_BIGENDIAN == 1
137 M_ILD(tempreg, REG_SP, disp + 4);
139 M_ILD(tempreg, REG_SP, disp);
145 reg = GET_LOW_REG(src->vv.regoff);
149 #endif /* SIZEOF_VOID_P == 4 */
152 /* emit_load_high **************************************************************
154 Emits a possible load of the high 32-bits of an operand.
156 *******************************************************************************/
158 #if SIZEOF_VOID_P == 4
159 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
165 assert(src->type == TYPE_LNG);
167 /* get required compiler data */
171 if (src->flags & INMEMORY) {
174 disp = src->vv.regoff;
176 #if WORDS_BIGENDIAN == 1
177 M_ILD(tempreg, REG_SP, disp);
179 M_ILD(tempreg, REG_SP, disp + 4);
185 reg = GET_HIGH_REG(src->vv.regoff);
189 #endif /* SIZEOF_VOID_P == 4 */
192 /* emit_store ******************************************************************
194 Emits a possible store to variable.
196 *******************************************************************************/
198 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
203 /* get required compiler data */
207 if (dst->flags & INMEMORY) {
210 disp = dst->vv.regoff;
213 #if SIZEOF_VOID_P == 8
217 M_LST(d, REG_SP, disp);
222 M_IST(d, REG_SP, disp);
225 M_LST(d, REG_SP, disp);
229 M_FST(d, REG_SP, disp);
232 M_DST(d, REG_SP, disp);
235 vm_abort("emit_store: unknown type %d", dst->type);
241 /* emit_copy *******************************************************************
243 Generates a register/memory to register/memory copy.
245 *******************************************************************************/
247 void emit_copy(jitdata *jd, instruction *iptr)
254 /* get required compiler data */
258 /* get source and destination variables */
260 src = VAROP(iptr->s1);
261 dst = VAROP(iptr->dst);
263 if ((src->vv.regoff != dst->vv.regoff) ||
264 ((src->flags ^ dst->flags) & INMEMORY)) {
266 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
267 /* emit nothing, as the value won't be used anyway */
271 /* If one of the variables resides in memory, we can eliminate
272 the register move from/to the temporary register with the
273 order of getting the destination register and the load. */
275 if (IS_INMEMORY(src->flags)) {
276 #if SIZEOF_VOID_P == 4
277 if (IS_2_WORD_TYPE(src->type))
278 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
281 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
282 s1 = emit_load(jd, iptr, src, d);
285 s1 = emit_load(jd, iptr, src, REG_IFTMP);
286 #if SIZEOF_VOID_P == 4
287 if (IS_2_WORD_TYPE(src->type))
288 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
291 d = codegen_reg_of_var(iptr->opc, dst, s1);
296 #if SIZEOF_VOID_P == 8
318 vm_abort("emit_copy: unknown type %d", dst->type);
322 emit_store(jd, iptr, dst, d);
327 /* emit_iconst *****************************************************************
331 *******************************************************************************/
333 void emit_iconst(codegendata *cd, s4 d, s4 value)
337 if ((value >= -32768) && (value <= 32767))
338 M_IADD_IMM(REG_ZERO, value, d);
339 else if ((value >= 0) && (value <= 0xffff))
340 M_OR_IMM(REG_ZERO, value, d);
342 disp = dseg_add_s4(cd, value);
343 M_ILD(d, REG_PV, disp);
348 /* emit_lconst *****************************************************************
352 *******************************************************************************/
354 void emit_lconst(codegendata *cd, s4 d, s8 value)
358 #if SIZEOF_VOID_P == 8
359 if ((value >= -32768) && (value <= 32767))
360 M_LADD_IMM(REG_ZERO, value, d);
361 else if ((value >= 0) && (value <= 0xffff))
362 M_OR_IMM(REG_ZERO, value, d);
364 disp = dseg_add_s8(cd, value);
365 M_LLD(d, REG_PV, disp);
368 disp = dseg_add_s8(cd, value);
369 M_LLD(d, REG_PV, disp);
374 /* emit_branch *****************************************************************
376 Emits the code for conditional and unconditional branchs.
378 NOTE: The reg argument may contain two packed registers.
380 *******************************************************************************/
382 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
387 /* calculate the different displacements */
389 checkdisp = (disp - 4);
390 branchdisp = (disp - 4) >> 2;
392 /* check which branch to generate */
394 if (condition == BRANCH_UNCONDITIONAL) {
395 /* check displacement for overflow */
397 if ((checkdisp < (s4) 0xffff8000) || (checkdisp > (s4) 0x00007fff)) {
398 /* if the long-branches flag isn't set yet, do it */
400 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
401 cd->flags |= (CODEGENDATA_FLAG_ERROR |
402 CODEGENDATA_FLAG_LONGBRANCHES);
405 vm_abort("emit_branch: emit unconditional long-branch code");
413 /* and displacement for overflow */
415 if ((checkdisp < (s4) 0xffff8000) || (checkdisp > (s4) 0x00007fff)) {
416 /* if the long-branches flag isn't set yet, do it */
418 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
419 cd->flags |= (CODEGENDATA_FLAG_ERROR |
420 CODEGENDATA_FLAG_LONGBRANCHES);
425 M_BNE(GET_HIGH_REG(reg), GET_LOW_REG(reg), 5);
428 M_BEQ(GET_HIGH_REG(reg), GET_LOW_REG(reg), 5);
443 vm_abort("emit_branch: unknown condition %d", condition);
446 /* The actual branch code which is over-jumped (NOTE: we
447 don't use a branch delay slot here). */
449 M_LUI(REG_ITMP3, branchdisp >> 16);
450 M_OR_IMM(REG_ITMP3, branchdisp, REG_ITMP3);
451 M_AADD(REG_PV, REG_ITMP3, REG_ITMP3);
459 M_BEQ(GET_HIGH_REG(reg), GET_LOW_REG(reg), branchdisp);
462 M_BNE(GET_HIGH_REG(reg), GET_LOW_REG(reg), branchdisp);
465 M_BLTZ(reg, branchdisp);
468 M_BGEZ(reg, branchdisp);
471 M_BGTZ(reg, branchdisp);
474 M_BLEZ(reg, branchdisp);
477 vm_abort("emit_branch: unknown condition %d", condition);
487 /* emit_arithmetic_check *******************************************************
489 Emit an ArithmeticException check.
491 *******************************************************************************/
493 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
495 if (INSTRUCTION_MUST_CHECK(iptr)) {
498 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_ArithmeticException);
503 /* emit_arrayindexoutofbounds_check ********************************************
505 Emit an ArrayIndexOutOfBoundsException check.
507 *******************************************************************************/
509 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
511 if (INSTRUCTION_MUST_CHECK(iptr)) {
512 M_ILD_INTERN(REG_ITMP3, s1, OFFSET(java_array_t, size));
513 M_CMPULT(s2, REG_ITMP3, REG_ITMP3);
514 M_BNEZ(REG_ITMP3, 2);
516 M_ALD_INTERN(s2, REG_ZERO, TRAP_ArrayIndexOutOfBoundsException);
521 /* emit_arraystore_check *******************************************************
523 Emit an ArrayStoreException check.
525 *******************************************************************************/
527 void emit_arraystore_check(codegendata *cd, instruction *iptr)
529 if (INSTRUCTION_MUST_CHECK(iptr)) {
530 M_BNEZ(REG_RESULT, 2);
532 M_ALD_INTERN(REG_RESULT, REG_ZERO, TRAP_ArrayStoreException);
537 /* emit_classcast_check ********************************************************
539 Emit a ClassCastException check.
541 *******************************************************************************/
543 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
545 if (INSTRUCTION_MUST_CHECK(iptr)) {
560 vm_abort("emit_classcast_check: unknown condition %d", condition);
564 M_ALD_INTERN(s1, REG_ZERO, TRAP_ClassCastException);
569 /* emit_nullpointer_check ******************************************************
571 Emit a NullPointerException check.
573 *******************************************************************************/
575 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
577 if (INSTRUCTION_MUST_CHECK(iptr)) {
580 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_NullPointerException);
585 /* emit_exception_check ********************************************************
587 Emit an Exception check.
589 *******************************************************************************/
591 void emit_exception_check(codegendata *cd, instruction *iptr)
593 if (INSTRUCTION_MUST_CHECK(iptr)) {
594 M_BNEZ(REG_RESULT, 2);
596 M_ALD_INTERN(REG_RESULT, REG_ZERO, TRAP_CHECK_EXCEPTION);
601 /* emit_trap_compiler **********************************************************
603 Emit a trap instruction which calls the JIT compiler.
605 *******************************************************************************/
607 void emit_trap_compiler(codegendata *cd)
609 M_ALD_INTERN(REG_METHODPTR, REG_ZERO, TRAP_COMPILER);
613 /* emit_trap *******************************************************************
615 Emit a trap instruction and return the original machine code.
617 *******************************************************************************/
619 uint32_t emit_trap(codegendata *cd)
623 /* Get machine code which is patched back in later. The
624 trap is 1 instruction word long. */
626 mcode = *((uint32_t *) cd->mcodeptr);
628 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_PATCHER);
634 /* emit_verbosecall_enter ******************************************************
636 Generates the code for the call trace.
638 *******************************************************************************/
641 void emit_verbosecall_enter(jitdata *jd)
651 /* get required compiler data */
660 /* mark trace code */
664 M_LDA(REG_SP, REG_SP, -(PA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8));
665 M_AST(REG_RA, REG_SP, PA_SIZE + 1 * 8);
667 /* save argument registers (we store the registers as address
668 types, so it's correct for MIPS32 too) */
670 for (i = 0; i < INT_ARG_CNT; i++)
671 M_AST(abi_registers_integer_argument[i], REG_SP, PA_SIZE + (2 + i) * 8);
673 for (i = 0; i < FLT_ARG_CNT; i++)
674 M_DST(abi_registers_float_argument[i], REG_SP, PA_SIZE + (2 + INT_ARG_CNT + i) * 8);
676 /* save temporary registers for leaf methods */
678 if (code_is_leafmethod(code)) {
679 for (i = 0; i < INT_TMP_CNT; i++)
680 M_AST(rd->tmpintregs[i], REG_SP, PA_SIZE + (2 + ARG_CNT + i) * 8);
682 for (i = 0; i < FLT_TMP_CNT; i++)
683 M_DST(rd->tmpfltregs[i], REG_SP, PA_SIZE + (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
686 /* Load float arguments into integer registers. MIPS32 has less
687 float argument registers than integer ones, we need to check
690 for (i = 0; i < md->paramcount && i < INT_ARG_CNT && i < FLT_ARG_CNT; i++) {
691 t = md->paramtypes[i].type;
693 if (IS_FLT_DBL_TYPE(t)) {
694 if (IS_2_WORD_TYPE(t)) {
695 M_DST(abi_registers_float_argument[i], REG_SP, 0 * 8);
696 M_LLD(abi_registers_integer_argument[i], REG_SP, 0 * 8);
699 M_FST(abi_registers_float_argument[i], REG_SP, 0 * 8);
700 M_ILD(abi_registers_integer_argument[i], REG_SP, 0 * 8);
705 #if SIZEOF_VOID_P == 4
706 for (i = 0, j = 0; i < md->paramcount && i < TRACE_ARGS_NUM; i++) {
707 t = md->paramtypes[i].type;
709 if (IS_INT_LNG_TYPE(t)) {
710 if (IS_2_WORD_TYPE(t)) {
711 M_ILD(abi_registers_integer_argument[j], REG_SP, PA_SIZE + (2 + i) * 8);
712 M_ILD(abi_registers_integer_argument[j + 1], REG_SP, PA_SIZE + (2 + i) * 8 + 4);
715 # if WORDS_BIGENDIAN == 1
716 M_MOV(REG_ZERO, abi_registers_integer_argument[j]);
717 M_ILD(abi_registers_integer_argument[j + 1], REG_SP, PA_SIZE + (2 + i) * 8);
719 M_ILD(abi_registers_integer_argument[j], REG_SP, PA_SIZE + (2 + i) * 8);
720 M_MOV(REG_ZERO, abi_registers_integer_argument[j + 1]);
728 disp = dseg_add_address(cd, m);
729 M_ALD(REG_ITMP1, REG_PV, disp);
730 M_AST(REG_ITMP1, REG_SP, PA_SIZE + 0 * 8);
731 disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
732 M_ALD(REG_ITMP3, REG_PV, disp);
733 M_JSR(REG_RA, REG_ITMP3);
736 /* restore argument registers */
738 for (i = 0; i < INT_ARG_CNT; i++)
739 M_ALD(abi_registers_integer_argument[i], REG_SP, PA_SIZE + (2 + i) * 8);
741 for (i = 0; i < FLT_ARG_CNT; i++)
742 M_DLD(abi_registers_float_argument[i], REG_SP, PA_SIZE + (2 + INT_ARG_CNT + i) * 8);
744 /* restore temporary registers for leaf methods */
746 if (code_is_leafmethod(code)) {
747 for (i = 0; i < INT_TMP_CNT; i++)
748 M_ALD(rd->tmpintregs[i], REG_SP, PA_SIZE + (2 + ARG_CNT + i) * 8);
750 for (i = 0; i < FLT_TMP_CNT; i++)
751 M_DLD(rd->tmpfltregs[i], REG_SP, PA_SIZE + (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
754 M_ALD(REG_RA, REG_SP, PA_SIZE + 1 * 8);
755 M_LDA(REG_SP, REG_SP, PA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8);
757 /* mark trace code */
761 #endif /* !defined(NDEBUG) */
764 /* emit_verbosecall_exit *******************************************************
766 Generates the code for the call trace.
768 void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
770 *******************************************************************************/
773 void emit_verbosecall_exit(jitdata *jd)
781 /* get required compiler data */
789 /* mark trace code */
793 #if SIZEOF_VOID_P == 8
794 M_ASUB_IMM(REG_SP, 4 * 8, REG_SP); /* keep stack 16-byte aligned */
795 M_AST(REG_RA, REG_SP, 0 * 8);
797 M_LST(REG_RESULT, REG_SP, 1 * 8);
798 M_DST(REG_FRESULT, REG_SP, 2 * 8);
800 M_MOV(REG_RESULT, REG_A0);
801 M_DMOV(REG_FRESULT, REG_FA1);
802 M_FMOV(REG_FRESULT, REG_FA2);
804 disp = dseg_add_address(cd, m);
805 M_ALD(REG_A4, REG_PV, disp);
807 M_ASUB_IMM(REG_SP, (8*4 + 4 * 8), REG_SP);
808 M_AST(REG_RA, REG_SP, 8*4 + 0 * 8);
810 M_LST(REG_RESULT_PACKED, REG_SP, 8*4 + 1 * 8);
811 M_DST(REG_FRESULT, REG_SP, 8*4 + 2 * 8);
813 switch (md->returntype.type) {
815 M_LNGMOVE(REG_RESULT_PACKED, REG_A0_A1_PACKED);
819 # if WORDS_BIGENDIAN == 1
820 M_MOV(REG_ZERO, REG_A0);
821 M_MOV(REG_RESULT, REG_A1);
823 M_MOV(REG_RESULT, REG_A0);
824 M_MOV(REG_ZERO, REG_A1);
828 M_LLD(REG_A2_A3_PACKED, REG_SP, 8*4 + 2 * 8);
829 M_FST(REG_FRESULT, REG_SP, 4*4 + 0 * 4);
831 disp = dseg_add_address(cd, m);
832 M_ALD(REG_ITMP1, REG_PV, disp);
833 M_AST(REG_ITMP1, REG_SP, 4*4 + 1 * 4);
836 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
837 M_ALD(REG_ITMP3, REG_PV, disp);
838 M_JSR(REG_RA, REG_ITMP3);
841 #if SIZEOF_VOID_P == 8
842 M_DLD(REG_FRESULT, REG_SP, 2 * 8);
843 M_LLD(REG_RESULT, REG_SP, 1 * 8);
845 M_ALD(REG_RA, REG_SP, 0 * 8);
846 M_AADD_IMM(REG_SP, 4 * 8, REG_SP);
848 M_DLD(REG_FRESULT, REG_SP, 8*4 + 2 * 8);
849 M_LLD(REG_RESULT_PACKED, REG_SP, 8*4 + 1 * 8);
851 M_ALD(REG_RA, REG_SP, 8*4 + 0 * 8);
852 M_AADD_IMM(REG_SP, 8*4 + 4 * 8, REG_SP);
855 /* mark trace code */
859 #endif /* !defined(NDEBUG) */
863 * These are local overrides for various environment variables in Emacs.
864 * Please do not remove this and leave it at the end of the file, where
865 * Emacs will automagically detect them.
866 * ---------------------------------------------------------------------
869 * indent-tabs-mode: t
873 * vim:noexpandtab:sw=4:ts=4: