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/options.h"
42 #include "vm/jit/abi.h"
43 #include "vm/jit/abi-asm.h"
44 #include "vm/jit/asmpart.h"
45 #include "vm/jit/dseg.h"
46 #include "vm/jit/emit-common.h"
47 #include "vm/jit/jit.h"
48 #include "vm/jit/patcher-common.h"
49 #include "vm/jit/replace.h"
50 #include "vm/jit/trap.h"
53 /* emit_load *******************************************************************
55 Emits a possible load of an operand.
57 *******************************************************************************/
59 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
65 /* get required compiler data */
69 if (src->flags & INMEMORY) {
72 disp = src->vv.regoff;
75 #if SIZEOF_VOID_P == 8
79 M_LLD(tempreg, REG_SP, disp);
84 M_ILD(tempreg, REG_SP, disp);
87 M_LLD(tempreg, REG_SP, disp);
91 M_FLD(tempreg, REG_SP, disp);
94 M_DLD(tempreg, REG_SP, disp);
97 vm_abort("emit_load: unknown type %d", src->type);
103 reg = src->vv.regoff;
109 /* emit_load_low ***************************************************************
111 Emits a possible load of the low 32-bits of an operand.
113 *******************************************************************************/
115 #if SIZEOF_VOID_P == 4
116 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
122 assert(src->type == TYPE_LNG);
124 /* get required compiler data */
128 if (src->flags & INMEMORY) {
131 disp = src->vv.regoff;
133 #if WORDS_BIGENDIAN == 1
134 M_ILD(tempreg, REG_SP, disp + 4);
136 M_ILD(tempreg, REG_SP, disp);
142 reg = GET_LOW_REG(src->vv.regoff);
146 #endif /* SIZEOF_VOID_P == 4 */
149 /* emit_load_high **************************************************************
151 Emits a possible load of the high 32-bits of an operand.
153 *******************************************************************************/
155 #if SIZEOF_VOID_P == 4
156 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
162 assert(src->type == TYPE_LNG);
164 /* get required compiler data */
168 if (src->flags & INMEMORY) {
171 disp = src->vv.regoff;
173 #if WORDS_BIGENDIAN == 1
174 M_ILD(tempreg, REG_SP, disp);
176 M_ILD(tempreg, REG_SP, disp + 4);
182 reg = GET_HIGH_REG(src->vv.regoff);
186 #endif /* SIZEOF_VOID_P == 4 */
189 /* emit_store ******************************************************************
191 Emits a possible store to variable.
193 *******************************************************************************/
195 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
200 /* get required compiler data */
204 if (dst->flags & INMEMORY) {
207 disp = dst->vv.regoff;
210 #if SIZEOF_VOID_P == 8
214 M_LST(d, REG_SP, disp);
219 M_IST(d, REG_SP, disp);
222 M_LST(d, REG_SP, disp);
226 M_FST(d, REG_SP, disp);
229 M_DST(d, REG_SP, disp);
232 vm_abort("emit_store: unknown type %d", dst->type);
238 /* emit_copy *******************************************************************
240 Generates a register/memory to register/memory copy.
242 *******************************************************************************/
244 void emit_copy(jitdata *jd, instruction *iptr)
251 /* get required compiler data */
255 /* get source and destination variables */
257 src = VAROP(iptr->s1);
258 dst = VAROP(iptr->dst);
260 if ((src->vv.regoff != dst->vv.regoff) ||
261 ((src->flags ^ dst->flags) & INMEMORY)) {
263 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
264 /* emit nothing, as the value won't be used anyway */
268 /* If one of the variables resides in memory, we can eliminate
269 the register move from/to the temporary register with the
270 order of getting the destination register and the load. */
272 if (IS_INMEMORY(src->flags)) {
273 #if SIZEOF_VOID_P == 4
274 if (IS_2_WORD_TYPE(src->type))
275 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
278 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
279 s1 = emit_load(jd, iptr, src, d);
282 s1 = emit_load(jd, iptr, src, REG_IFTMP);
283 #if SIZEOF_VOID_P == 4
284 if (IS_2_WORD_TYPE(src->type))
285 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
288 d = codegen_reg_of_var(iptr->opc, dst, s1);
293 #if SIZEOF_VOID_P == 8
315 vm_abort("emit_copy: unknown type %d", dst->type);
319 emit_store(jd, iptr, dst, d);
324 /* emit_iconst *****************************************************************
328 *******************************************************************************/
330 void emit_iconst(codegendata *cd, s4 d, s4 value)
334 if ((value >= -32768) && (value <= 32767))
335 M_IADD_IMM(REG_ZERO, value, d);
336 else if ((value >= 0) && (value <= 0xffff))
337 M_OR_IMM(REG_ZERO, value, d);
339 disp = dseg_add_s4(cd, value);
340 M_ILD(d, REG_PV, disp);
345 /* emit_lconst *****************************************************************
349 *******************************************************************************/
351 void emit_lconst(codegendata *cd, s4 d, s8 value)
355 #if SIZEOF_VOID_P == 8
356 if ((value >= -32768) && (value <= 32767))
357 M_LADD_IMM(REG_ZERO, value, d);
358 else if ((value >= 0) && (value <= 0xffff))
359 M_OR_IMM(REG_ZERO, value, d);
361 disp = dseg_add_s8(cd, value);
362 M_LLD(d, REG_PV, disp);
365 disp = dseg_add_s8(cd, value);
366 M_LLD(d, REG_PV, disp);
371 /* emit_branch *****************************************************************
373 Emits the code for conditional and unconditional branchs.
375 NOTE: The reg argument may contain two packed registers.
377 *******************************************************************************/
379 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
384 /* calculate the different displacements */
386 checkdisp = (disp - 4);
387 branchdisp = (disp - 4) >> 2;
389 /* check which branch to generate */
391 if (condition == BRANCH_UNCONDITIONAL) {
392 /* check displacement for overflow */
394 if ((checkdisp < (s4) 0xffff8000) || (checkdisp > (s4) 0x00007fff)) {
395 /* if the long-branches flag isn't set yet, do it */
397 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
398 cd->flags |= (CODEGENDATA_FLAG_ERROR |
399 CODEGENDATA_FLAG_LONGBRANCHES);
402 vm_abort("emit_branch: emit unconditional long-branch code");
410 /* and displacement for overflow */
412 if ((checkdisp < (s4) 0xffff8000) || (checkdisp > (s4) 0x00007fff)) {
413 /* if the long-branches flag isn't set yet, do it */
415 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
416 cd->flags |= (CODEGENDATA_FLAG_ERROR |
417 CODEGENDATA_FLAG_LONGBRANCHES);
422 M_BNE(GET_HIGH_REG(reg), GET_LOW_REG(reg), 5);
425 M_BEQ(GET_HIGH_REG(reg), GET_LOW_REG(reg), 5);
440 vm_abort("emit_branch: unknown condition %d", condition);
443 /* The actual branch code which is over-jumped (NOTE: we
444 don't use a branch delay slot here). */
446 M_LUI(REG_ITMP3, branchdisp >> 16);
447 M_OR_IMM(REG_ITMP3, branchdisp, REG_ITMP3);
448 M_AADD(REG_PV, REG_ITMP3, REG_ITMP3);
456 M_BEQ(GET_HIGH_REG(reg), GET_LOW_REG(reg), branchdisp);
459 M_BNE(GET_HIGH_REG(reg), GET_LOW_REG(reg), branchdisp);
462 M_BLTZ(reg, branchdisp);
465 M_BGEZ(reg, branchdisp);
468 M_BGTZ(reg, branchdisp);
471 M_BLEZ(reg, branchdisp);
474 vm_abort("emit_branch: unknown condition %d", condition);
484 /* emit_arithmetic_check *******************************************************
486 Emit an ArithmeticException check.
488 *******************************************************************************/
490 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
492 if (INSTRUCTION_MUST_CHECK(iptr)) {
495 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_ArithmeticException);
500 /* emit_arrayindexoutofbounds_check ********************************************
502 Emit an ArrayIndexOutOfBoundsException check.
504 *******************************************************************************/
506 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
508 if (INSTRUCTION_MUST_CHECK(iptr)) {
509 M_ILD_INTERN(REG_ITMP3, s1, OFFSET(java_array_t, size));
510 M_CMPULT(s2, REG_ITMP3, REG_ITMP3);
511 M_BNEZ(REG_ITMP3, 2);
513 M_ALD_INTERN(s2, REG_ZERO, TRAP_ArrayIndexOutOfBoundsException);
518 /* emit_arraystore_check *******************************************************
520 Emit an ArrayStoreException check.
522 *******************************************************************************/
524 void emit_arraystore_check(codegendata *cd, instruction *iptr)
526 if (INSTRUCTION_MUST_CHECK(iptr)) {
527 M_BNEZ(REG_RESULT, 2);
529 M_ALD_INTERN(REG_RESULT, REG_ZERO, TRAP_ArrayStoreException);
534 /* emit_classcast_check ********************************************************
536 Emit a ClassCastException check.
538 *******************************************************************************/
540 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
542 if (INSTRUCTION_MUST_CHECK(iptr)) {
557 vm_abort("emit_classcast_check: unknown condition %d", condition);
561 M_ALD_INTERN(s1, REG_ZERO, TRAP_ClassCastException);
566 /* emit_nullpointer_check ******************************************************
568 Emit a NullPointerException check.
570 *******************************************************************************/
572 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
574 if (INSTRUCTION_MUST_CHECK(iptr)) {
577 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_NullPointerException);
582 /* emit_exception_check ********************************************************
584 Emit an Exception check.
586 *******************************************************************************/
588 void emit_exception_check(codegendata *cd, instruction *iptr)
590 if (INSTRUCTION_MUST_CHECK(iptr)) {
591 M_BNEZ(REG_RESULT, 2);
593 M_ALD_INTERN(REG_RESULT, REG_ZERO, TRAP_CHECK_EXCEPTION);
598 /* emit_trap_compiler **********************************************************
600 Emit a trap instruction which calls the JIT compiler.
602 *******************************************************************************/
604 void emit_trap_compiler(codegendata *cd)
606 M_ALD_INTERN(REG_METHODPTR, REG_ZERO, TRAP_COMPILER);
610 /* emit_trap *******************************************************************
612 Emit a trap instruction and return the original machine code.
614 *******************************************************************************/
616 uint32_t emit_trap(codegendata *cd)
620 /* Get machine code which is patched back in later. The
621 trap is 1 instruction word long. */
623 mcode = *((uint32_t *) cd->mcodeptr);
625 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_PATCHER);
631 /* emit_verbosecall_enter ******************************************************
633 Generates the code for the call trace.
635 *******************************************************************************/
638 void emit_verbosecall_enter(jitdata *jd)
648 /* get required compiler data */
657 /* mark trace code */
661 M_LDA(REG_SP, REG_SP, -(PA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8));
662 M_AST(REG_RA, REG_SP, PA_SIZE + 1 * 8);
664 /* save argument registers (we store the registers as address
665 types, so it's correct for MIPS32 too) */
667 for (i = 0; i < INT_ARG_CNT; i++)
668 M_AST(abi_registers_integer_argument[i], REG_SP, PA_SIZE + (2 + i) * 8);
670 for (i = 0; i < FLT_ARG_CNT; i++)
671 M_DST(abi_registers_float_argument[i], REG_SP, PA_SIZE + (2 + INT_ARG_CNT + i) * 8);
673 /* save temporary registers for leaf methods */
675 if (code_is_leafmethod(code)) {
676 for (i = 0; i < INT_TMP_CNT; i++)
677 M_AST(rd->tmpintregs[i], REG_SP, PA_SIZE + (2 + ARG_CNT + i) * 8);
679 for (i = 0; i < FLT_TMP_CNT; i++)
680 M_DST(rd->tmpfltregs[i], REG_SP, PA_SIZE + (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
683 /* Load float arguments into integer registers. MIPS32 has less
684 float argument registers than integer ones, we need to check
687 for (i = 0; i < md->paramcount && i < INT_ARG_CNT && i < FLT_ARG_CNT; i++) {
688 t = md->paramtypes[i].type;
690 if (IS_FLT_DBL_TYPE(t)) {
691 if (IS_2_WORD_TYPE(t)) {
692 M_DST(abi_registers_float_argument[i], REG_SP, 0 * 8);
693 M_LLD(abi_registers_integer_argument[i], REG_SP, 0 * 8);
696 M_FST(abi_registers_float_argument[i], REG_SP, 0 * 8);
697 M_ILD(abi_registers_integer_argument[i], REG_SP, 0 * 8);
702 #if SIZEOF_VOID_P == 4
703 for (i = 0, j = 0; i < md->paramcount && i < TRACE_ARGS_NUM; i++) {
704 t = md->paramtypes[i].type;
706 if (IS_INT_LNG_TYPE(t)) {
707 if (IS_2_WORD_TYPE(t)) {
708 M_ILD(abi_registers_integer_argument[j], REG_SP, PA_SIZE + (2 + i) * 8);
709 M_ILD(abi_registers_integer_argument[j + 1], REG_SP, PA_SIZE + (2 + i) * 8 + 4);
712 # if WORDS_BIGENDIAN == 1
713 M_MOV(REG_ZERO, abi_registers_integer_argument[j]);
714 M_ILD(abi_registers_integer_argument[j + 1], REG_SP, PA_SIZE + (2 + i) * 8);
716 M_ILD(abi_registers_integer_argument[j], REG_SP, PA_SIZE + (2 + i) * 8);
717 M_MOV(REG_ZERO, abi_registers_integer_argument[j + 1]);
725 disp = dseg_add_address(cd, m);
726 M_ALD(REG_ITMP1, REG_PV, disp);
727 M_AST(REG_ITMP1, REG_SP, PA_SIZE + 0 * 8);
728 disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
729 M_ALD(REG_ITMP3, REG_PV, disp);
730 M_JSR(REG_RA, REG_ITMP3);
733 /* restore argument registers */
735 for (i = 0; i < INT_ARG_CNT; i++)
736 M_ALD(abi_registers_integer_argument[i], REG_SP, PA_SIZE + (2 + i) * 8);
738 for (i = 0; i < FLT_ARG_CNT; i++)
739 M_DLD(abi_registers_float_argument[i], REG_SP, PA_SIZE + (2 + INT_ARG_CNT + i) * 8);
741 /* restore temporary registers for leaf methods */
743 if (code_is_leafmethod(code)) {
744 for (i = 0; i < INT_TMP_CNT; i++)
745 M_ALD(rd->tmpintregs[i], REG_SP, PA_SIZE + (2 + ARG_CNT + i) * 8);
747 for (i = 0; i < FLT_TMP_CNT; i++)
748 M_DLD(rd->tmpfltregs[i], REG_SP, PA_SIZE + (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
751 M_ALD(REG_RA, REG_SP, PA_SIZE + 1 * 8);
752 M_LDA(REG_SP, REG_SP, PA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8);
754 /* mark trace code */
758 #endif /* !defined(NDEBUG) */
761 /* emit_verbosecall_exit *******************************************************
763 Generates the code for the call trace.
765 void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
767 *******************************************************************************/
770 void emit_verbosecall_exit(jitdata *jd)
778 /* get required compiler data */
786 /* mark trace code */
790 #if SIZEOF_VOID_P == 8
791 M_ASUB_IMM(REG_SP, 4 * 8, REG_SP); /* keep stack 16-byte aligned */
792 M_AST(REG_RA, REG_SP, 0 * 8);
794 M_LST(REG_RESULT, REG_SP, 1 * 8);
795 M_DST(REG_FRESULT, REG_SP, 2 * 8);
797 M_MOV(REG_RESULT, REG_A0);
798 M_DMOV(REG_FRESULT, REG_FA1);
799 M_FMOV(REG_FRESULT, REG_FA2);
801 disp = dseg_add_address(cd, m);
802 M_ALD(REG_A4, REG_PV, disp);
804 M_ASUB_IMM(REG_SP, (8*4 + 4 * 8), REG_SP);
805 M_AST(REG_RA, REG_SP, 8*4 + 0 * 8);
807 M_LST(REG_RESULT_PACKED, REG_SP, 8*4 + 1 * 8);
808 M_DST(REG_FRESULT, REG_SP, 8*4 + 2 * 8);
810 switch (md->returntype.type) {
812 M_LNGMOVE(REG_RESULT_PACKED, REG_A0_A1_PACKED);
816 # if WORDS_BIGENDIAN == 1
817 M_MOV(REG_ZERO, REG_A0);
818 M_MOV(REG_RESULT, REG_A1);
820 M_MOV(REG_RESULT, REG_A0);
821 M_MOV(REG_ZERO, REG_A1);
825 M_LLD(REG_A2_A3_PACKED, REG_SP, 8*4 + 2 * 8);
826 M_FST(REG_FRESULT, REG_SP, 4*4 + 0 * 4);
828 disp = dseg_add_address(cd, m);
829 M_ALD(REG_ITMP1, REG_PV, disp);
830 M_AST(REG_ITMP1, REG_SP, 4*4 + 1 * 4);
833 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
834 M_ALD(REG_ITMP3, REG_PV, disp);
835 M_JSR(REG_RA, REG_ITMP3);
838 #if SIZEOF_VOID_P == 8
839 M_DLD(REG_FRESULT, REG_SP, 2 * 8);
840 M_LLD(REG_RESULT, REG_SP, 1 * 8);
842 M_ALD(REG_RA, REG_SP, 0 * 8);
843 M_AADD_IMM(REG_SP, 4 * 8, REG_SP);
845 M_DLD(REG_FRESULT, REG_SP, 8*4 + 2 * 8);
846 M_LLD(REG_RESULT_PACKED, REG_SP, 8*4 + 1 * 8);
848 M_ALD(REG_RA, REG_SP, 8*4 + 0 * 8);
849 M_AADD_IMM(REG_SP, 8*4 + 4 * 8, REG_SP);
852 /* mark trace code */
856 #endif /* !defined(NDEBUG) */
860 * These are local overrides for various environment variables in Emacs.
861 * Please do not remove this and leave it at the end of the file, where
862 * Emacs will automagically detect them.
863 * ---------------------------------------------------------------------
866 * indent-tabs-mode: t
870 * vim:noexpandtab:sw=4:ts=4: