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.hpp"
39 #include "vm/jit/builtin.hpp"
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.hpp"
47 #include "vm/jit/jit.hpp"
48 #include "vm/jit/patcher-common.hpp"
49 #include "vm/jit/replace.hpp"
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)
381 // Calculate the displacements.
382 int32_t checkdisp = (disp - 4);
383 int32_t branchdisp = (disp - 4) >> 2;
385 /* check which branch to generate */
387 if (condition == BRANCH_UNCONDITIONAL) {
388 // Check displacement for overflow.
389 if (opt_AlwaysEmitLongBranches || ((checkdisp < (int32_t) 0xffff8000) || (checkdisp > (int32_t) 0x00007fff))) {
390 /* if the long-branches flag isn't set yet, do it */
392 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
393 cd->flags |= (CODEGENDATA_FLAG_ERROR |
394 CODEGENDATA_FLAG_LONGBRANCHES);
397 // Calculate the offset relative to PV.
398 int32_t currentrpc = cd->mcodeptr - cd->mcodebase;
399 int32_t offset = currentrpc + disp;
402 assert(offset % 4 == 0);
404 // Do the long-branch.
405 M_LUI(REG_ITMP3, offset >> 16);
406 M_OR_IMM(REG_ITMP3, offset, REG_ITMP3);
407 M_AADD(REG_PV, REG_ITMP3, REG_ITMP3);
410 M_NOP; // This nop is to have 6 instructions (see BRANCH_NOPS).
418 // Check displacement for overflow.
419 if (opt_AlwaysEmitLongBranches || ((checkdisp < (int32_t) 0xffff8000) || (checkdisp > (int32_t) 0x00007fff))) {
420 /* if the long-branches flag isn't set yet, do it */
422 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
423 cd->flags |= (CODEGENDATA_FLAG_ERROR |
424 CODEGENDATA_FLAG_LONGBRANCHES);
427 // Calculate the offset relative to PV before we generate
429 int32_t currentrpc = cd->mcodeptr - cd->mcodebase;
430 int32_t offset = currentrpc + disp;
433 assert(offset % 4 == 0);
437 M_BNE(GET_HIGH_REG(reg), GET_LOW_REG(reg), 5);
440 M_BEQ(GET_HIGH_REG(reg), GET_LOW_REG(reg), 5);
455 vm_abort("emit_branch: unknown condition %d", condition);
458 // The actual branch code which is over-jumped. NOTE: We
459 // don't use a branch delay slot for the conditional
462 // Do the long-branch.
463 M_LUI(REG_ITMP3, offset >> 16);
464 M_OR_IMM(REG_ITMP3, offset, REG_ITMP3);
465 M_AADD(REG_PV, REG_ITMP3, REG_ITMP3);
472 M_BEQ(GET_HIGH_REG(reg), GET_LOW_REG(reg), branchdisp);
475 M_BNE(GET_HIGH_REG(reg), GET_LOW_REG(reg), branchdisp);
478 M_BLTZ(reg, branchdisp);
481 M_BGEZ(reg, branchdisp);
484 M_BGTZ(reg, branchdisp);
487 M_BLEZ(reg, branchdisp);
490 vm_abort("emit_branch: unknown condition %d", condition);
500 /* emit_arithmetic_check *******************************************************
502 Emit an ArithmeticException check.
504 *******************************************************************************/
506 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
508 if (INSTRUCTION_MUST_CHECK(iptr)) {
511 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_ArithmeticException);
516 /* emit_arrayindexoutofbounds_check ********************************************
518 Emit an ArrayIndexOutOfBoundsException check.
520 *******************************************************************************/
522 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
524 if (INSTRUCTION_MUST_CHECK(iptr)) {
525 M_ILD_INTERN(REG_ITMP3, s1, OFFSET(java_array_t, size));
526 M_CMPULT(s2, REG_ITMP3, REG_ITMP3);
527 M_BNEZ(REG_ITMP3, 2);
529 M_ALD_INTERN(s2, REG_ZERO, TRAP_ArrayIndexOutOfBoundsException);
534 /* emit_arraystore_check *******************************************************
536 Emit an ArrayStoreException check.
538 *******************************************************************************/
540 void emit_arraystore_check(codegendata *cd, instruction *iptr)
542 if (INSTRUCTION_MUST_CHECK(iptr)) {
543 M_BNEZ(REG_RESULT, 2);
545 M_ALD_INTERN(REG_RESULT, REG_ZERO, TRAP_ArrayStoreException);
550 /* emit_classcast_check ********************************************************
552 Emit a ClassCastException check.
554 *******************************************************************************/
556 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
558 if (INSTRUCTION_MUST_CHECK(iptr)) {
573 vm_abort("emit_classcast_check: unknown condition %d", condition);
577 M_ALD_INTERN(s1, REG_ZERO, TRAP_ClassCastException);
582 /* emit_nullpointer_check ******************************************************
584 Emit a NullPointerException check.
586 *******************************************************************************/
588 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
590 if (INSTRUCTION_MUST_CHECK(iptr)) {
593 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_NullPointerException);
598 /* emit_exception_check ********************************************************
600 Emit an Exception check.
602 *******************************************************************************/
604 void emit_exception_check(codegendata *cd, instruction *iptr)
606 if (INSTRUCTION_MUST_CHECK(iptr)) {
607 M_BNEZ(REG_RESULT, 2);
609 M_ALD_INTERN(REG_RESULT, REG_ZERO, TRAP_CHECK_EXCEPTION);
614 /* emit_trap_compiler **********************************************************
616 Emit a trap instruction which calls the JIT compiler.
618 *******************************************************************************/
620 void emit_trap_compiler(codegendata *cd)
622 M_ALD_INTERN(REG_METHODPTR, REG_ZERO, TRAP_COMPILER);
626 /* emit_trap *******************************************************************
628 Emit a trap instruction and return the original machine code.
630 *******************************************************************************/
632 uint32_t emit_trap(codegendata *cd)
634 // Get machine code which is patched back in later. The trap is 1
635 // instruction word long.
636 uint32_t mcode = *((uint32_t*) cd->mcodeptr);
644 /* emit_verbosecall_enter ******************************************************
646 Generates the code for the call trace.
648 *******************************************************************************/
651 void emit_verbosecall_enter(jitdata *jd)
661 /* get required compiler data */
670 /* mark trace code */
674 M_LDA(REG_SP, REG_SP, -(PA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8));
675 M_AST(REG_RA, REG_SP, PA_SIZE + 1 * 8);
677 /* save argument registers (we store the registers as address
678 types, so it's correct for MIPS32 too) */
680 for (i = 0; i < INT_ARG_CNT; i++)
681 M_AST(abi_registers_integer_argument[i], REG_SP, PA_SIZE + (2 + i) * 8);
683 for (i = 0; i < FLT_ARG_CNT; i++)
684 M_DST(abi_registers_float_argument[i], REG_SP, PA_SIZE + (2 + INT_ARG_CNT + i) * 8);
686 /* save temporary registers for leaf methods */
688 if (code_is_leafmethod(code)) {
689 for (i = 0; i < INT_TMP_CNT; i++)
690 M_AST(rd->tmpintregs[i], REG_SP, PA_SIZE + (2 + ARG_CNT + i) * 8);
692 for (i = 0; i < FLT_TMP_CNT; i++)
693 M_DST(rd->tmpfltregs[i], REG_SP, PA_SIZE + (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
696 /* Load float arguments into integer registers. MIPS32 has less
697 float argument registers than integer ones, we need to check
700 for (i = 0; i < md->paramcount && i < INT_ARG_CNT && i < FLT_ARG_CNT; i++) {
701 t = md->paramtypes[i].type;
703 if (IS_FLT_DBL_TYPE(t)) {
704 if (IS_2_WORD_TYPE(t)) {
705 M_DST(abi_registers_float_argument[i], REG_SP, 0 * 8);
706 M_LLD(abi_registers_integer_argument[i], REG_SP, 0 * 8);
709 M_FST(abi_registers_float_argument[i], REG_SP, 0 * 8);
710 M_ILD(abi_registers_integer_argument[i], REG_SP, 0 * 8);
715 #if SIZEOF_VOID_P == 4
716 for (i = 0, j = 0; i < md->paramcount && i < TRACE_ARGS_NUM; i++) {
717 t = md->paramtypes[i].type;
719 if (IS_INT_LNG_TYPE(t)) {
720 if (IS_2_WORD_TYPE(t)) {
721 M_ILD(abi_registers_integer_argument[j], REG_SP, PA_SIZE + (2 + i) * 8);
722 M_ILD(abi_registers_integer_argument[j + 1], REG_SP, PA_SIZE + (2 + i) * 8 + 4);
725 # if WORDS_BIGENDIAN == 1
726 M_MOV(REG_ZERO, abi_registers_integer_argument[j]);
727 M_ILD(abi_registers_integer_argument[j + 1], REG_SP, PA_SIZE + (2 + i) * 8);
729 M_ILD(abi_registers_integer_argument[j], REG_SP, PA_SIZE + (2 + i) * 8);
730 M_MOV(REG_ZERO, abi_registers_integer_argument[j + 1]);
738 disp = dseg_add_address(cd, m);
739 M_ALD(REG_ITMP1, REG_PV, disp);
740 M_AST(REG_ITMP1, REG_SP, PA_SIZE + 0 * 8);
741 disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
742 M_ALD(REG_ITMP3, REG_PV, disp);
743 M_JSR(REG_RA, REG_ITMP3);
746 /* restore argument registers */
748 for (i = 0; i < INT_ARG_CNT; i++)
749 M_ALD(abi_registers_integer_argument[i], REG_SP, PA_SIZE + (2 + i) * 8);
751 for (i = 0; i < FLT_ARG_CNT; i++)
752 M_DLD(abi_registers_float_argument[i], REG_SP, PA_SIZE + (2 + INT_ARG_CNT + i) * 8);
754 /* restore temporary registers for leaf methods */
756 if (code_is_leafmethod(code)) {
757 for (i = 0; i < INT_TMP_CNT; i++)
758 M_ALD(rd->tmpintregs[i], REG_SP, PA_SIZE + (2 + ARG_CNT + i) * 8);
760 for (i = 0; i < FLT_TMP_CNT; i++)
761 M_DLD(rd->tmpfltregs[i], REG_SP, PA_SIZE + (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
764 M_ALD(REG_RA, REG_SP, PA_SIZE + 1 * 8);
765 M_LDA(REG_SP, REG_SP, PA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8);
767 /* mark trace code */
771 #endif /* !defined(NDEBUG) */
774 /* emit_verbosecall_exit *******************************************************
776 Generates the code for the call trace.
778 void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
780 *******************************************************************************/
783 void emit_verbosecall_exit(jitdata *jd)
791 /* get required compiler data */
799 /* mark trace code */
803 #if SIZEOF_VOID_P == 8
804 M_ASUB_IMM(REG_SP, 4 * 8, REG_SP); /* keep stack 16-byte aligned */
805 M_AST(REG_RA, REG_SP, 0 * 8);
807 M_LST(REG_RESULT, REG_SP, 1 * 8);
808 M_DST(REG_FRESULT, REG_SP, 2 * 8);
810 M_MOV(REG_RESULT, REG_A0);
811 M_DMOV(REG_FRESULT, REG_FA1);
812 M_FMOV(REG_FRESULT, REG_FA2);
814 disp = dseg_add_address(cd, m);
815 M_ALD(REG_A4, REG_PV, disp);
817 M_ASUB_IMM(REG_SP, (8*4 + 4 * 8), REG_SP);
818 M_AST(REG_RA, REG_SP, 8*4 + 0 * 8);
820 M_LST(REG_RESULT_PACKED, REG_SP, 8*4 + 1 * 8);
821 M_DST(REG_FRESULT, REG_SP, 8*4 + 2 * 8);
823 switch (md->returntype.type) {
825 M_LNGMOVE(REG_RESULT_PACKED, REG_A0_A1_PACKED);
829 # if WORDS_BIGENDIAN == 1
830 M_MOV(REG_ZERO, REG_A0);
831 M_MOV(REG_RESULT, REG_A1);
833 M_MOV(REG_RESULT, REG_A0);
834 M_MOV(REG_ZERO, REG_A1);
838 M_LLD(REG_A2_A3_PACKED, REG_SP, 8*4 + 2 * 8);
839 M_FST(REG_FRESULT, REG_SP, 4*4 + 0 * 4);
841 disp = dseg_add_address(cd, m);
842 M_ALD(REG_ITMP1, REG_PV, disp);
843 M_AST(REG_ITMP1, REG_SP, 4*4 + 1 * 4);
846 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
847 M_ALD(REG_ITMP3, REG_PV, disp);
848 M_JSR(REG_RA, REG_ITMP3);
851 #if SIZEOF_VOID_P == 8
852 M_DLD(REG_FRESULT, REG_SP, 2 * 8);
853 M_LLD(REG_RESULT, REG_SP, 1 * 8);
855 M_ALD(REG_RA, REG_SP, 0 * 8);
856 M_AADD_IMM(REG_SP, 4 * 8, REG_SP);
858 M_DLD(REG_FRESULT, REG_SP, 8*4 + 2 * 8);
859 M_LLD(REG_RESULT_PACKED, REG_SP, 8*4 + 1 * 8);
861 M_ALD(REG_RA, REG_SP, 8*4 + 0 * 8);
862 M_AADD_IMM(REG_SP, 8*4 + 4 * 8, REG_SP);
865 /* mark trace code */
869 #endif /* !defined(NDEBUG) */
873 * These are local overrides for various environment variables in Emacs.
874 * Please do not remove this and leave it at the end of the file, where
875 * Emacs will automagically detect them.
876 * ---------------------------------------------------------------------
879 * indent-tabs-mode: t
883 * vim:noexpandtab:sw=4:ts=4: