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.hpp"
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/trace.hpp"
51 #include "vm/jit/trap.hpp"
54 /* emit_load *******************************************************************
56 Emits a possible load of an operand.
58 *******************************************************************************/
60 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
66 /* get required compiler data */
70 if (src->flags & INMEMORY) {
73 disp = src->vv.regoff;
76 #if SIZEOF_VOID_P == 8
80 M_LLD(tempreg, REG_SP, disp);
85 M_ILD(tempreg, REG_SP, disp);
88 M_LLD(tempreg, REG_SP, disp);
92 M_FLD(tempreg, REG_SP, disp);
95 M_DLD(tempreg, REG_SP, disp);
98 vm_abort("emit_load: unknown type %d", src->type);
104 reg = src->vv.regoff;
110 /* emit_load_low ***************************************************************
112 Emits a possible load of the low 32-bits of an operand.
114 *******************************************************************************/
116 #if SIZEOF_VOID_P == 4
117 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
123 assert(src->type == TYPE_LNG);
125 /* get required compiler data */
129 if (src->flags & INMEMORY) {
132 disp = src->vv.regoff;
134 #if WORDS_BIGENDIAN == 1
135 M_ILD(tempreg, REG_SP, disp + 4);
137 M_ILD(tempreg, REG_SP, disp);
143 reg = GET_LOW_REG(src->vv.regoff);
147 #endif /* SIZEOF_VOID_P == 4 */
150 /* emit_load_high **************************************************************
152 Emits a possible load of the high 32-bits of an operand.
154 *******************************************************************************/
156 #if SIZEOF_VOID_P == 4
157 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
163 assert(src->type == TYPE_LNG);
165 /* get required compiler data */
169 if (src->flags & INMEMORY) {
172 disp = src->vv.regoff;
174 #if WORDS_BIGENDIAN == 1
175 M_ILD(tempreg, REG_SP, disp);
177 M_ILD(tempreg, REG_SP, disp + 4);
183 reg = GET_HIGH_REG(src->vv.regoff);
187 #endif /* SIZEOF_VOID_P == 4 */
190 /* emit_store ******************************************************************
192 Emits a possible store to variable.
194 *******************************************************************************/
196 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
201 /* get required compiler data */
205 if (dst->flags & INMEMORY) {
208 disp = dst->vv.regoff;
211 #if SIZEOF_VOID_P == 8
215 M_LST(d, REG_SP, disp);
220 M_IST(d, REG_SP, disp);
223 M_LST(d, REG_SP, disp);
227 M_FST(d, REG_SP, disp);
230 M_DST(d, REG_SP, disp);
233 vm_abort("emit_store: unknown type %d", dst->type);
239 /* emit_copy *******************************************************************
241 Generates a register/memory to register/memory copy.
243 *******************************************************************************/
245 void emit_copy(jitdata *jd, instruction *iptr)
252 /* get required compiler data */
256 /* get source and destination variables */
258 src = VAROP(iptr->s1);
259 dst = VAROP(iptr->dst);
261 if ((src->vv.regoff != dst->vv.regoff) ||
262 ((src->flags ^ dst->flags) & INMEMORY)) {
264 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
265 /* emit nothing, as the value won't be used anyway */
269 /* If one of the variables resides in memory, we can eliminate
270 the register move from/to the temporary register with the
271 order of getting the destination register and the load. */
273 if (IS_INMEMORY(src->flags)) {
274 #if SIZEOF_VOID_P == 4
275 if (IS_2_WORD_TYPE(src->type))
276 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
279 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
280 s1 = emit_load(jd, iptr, src, d);
283 s1 = emit_load(jd, iptr, src, REG_IFTMP);
284 #if SIZEOF_VOID_P == 4
285 if (IS_2_WORD_TYPE(src->type))
286 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
289 d = codegen_reg_of_var(iptr->opc, dst, s1);
294 #if SIZEOF_VOID_P == 8
316 vm_abort("emit_copy: unknown type %d", dst->type);
320 emit_store(jd, iptr, dst, d);
325 /* emit_iconst *****************************************************************
329 *******************************************************************************/
331 void emit_iconst(codegendata *cd, s4 d, s4 value)
335 if ((value >= -32768) && (value <= 32767))
336 M_IADD_IMM(REG_ZERO, value, d);
337 else if ((value >= 0) && (value <= 0xffff))
338 M_OR_IMM(REG_ZERO, value, d);
340 disp = dseg_add_s4(cd, value);
341 M_ILD(d, REG_PV, disp);
346 /* emit_lconst *****************************************************************
350 *******************************************************************************/
352 void emit_lconst(codegendata *cd, s4 d, s8 value)
356 #if SIZEOF_VOID_P == 8
357 if ((value >= -32768) && (value <= 32767))
358 M_LADD_IMM(REG_ZERO, value, d);
359 else if ((value >= 0) && (value <= 0xffff))
360 M_OR_IMM(REG_ZERO, value, d);
362 disp = dseg_add_s8(cd, value);
363 M_LLD(d, REG_PV, disp);
366 disp = dseg_add_s8(cd, value);
367 M_LLD(d, REG_PV, disp);
372 /* emit_branch *****************************************************************
374 Emits the code for conditional and unconditional branchs.
376 NOTE: The reg argument may contain two packed registers.
378 *******************************************************************************/
380 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
382 // Calculate the displacements.
383 int32_t checkdisp = (disp - 4);
384 int32_t branchdisp = (disp - 4) >> 2;
386 /* check which branch to generate */
388 if (condition == BRANCH_UNCONDITIONAL) {
389 // Check displacement for overflow.
390 if (opt_AlwaysEmitLongBranches || ((checkdisp < (int32_t) 0xffff8000) || (checkdisp > (int32_t) 0x00007fff))) {
391 /* if the long-branches flag isn't set yet, do it */
393 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
394 cd->flags |= (CODEGENDATA_FLAG_ERROR |
395 CODEGENDATA_FLAG_LONGBRANCHES);
398 // Calculate the offset relative to PV.
399 int32_t currentrpc = cd->mcodeptr - cd->mcodebase;
400 int32_t offset = currentrpc + disp;
403 assert(offset % 4 == 0);
405 // Do the long-branch.
406 M_LUI(REG_ITMP3, offset >> 16);
407 M_OR_IMM(REG_ITMP3, offset, REG_ITMP3);
408 M_AADD(REG_PV, REG_ITMP3, REG_ITMP3);
411 M_NOP; // This nop is to have 6 instructions (see BRANCH_NOPS).
419 // Check displacement for overflow.
420 if (opt_AlwaysEmitLongBranches || ((checkdisp < (int32_t) 0xffff8000) || (checkdisp > (int32_t) 0x00007fff))) {
421 /* if the long-branches flag isn't set yet, do it */
423 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
424 cd->flags |= (CODEGENDATA_FLAG_ERROR |
425 CODEGENDATA_FLAG_LONGBRANCHES);
428 // Calculate the offset relative to PV before we generate
430 int32_t currentrpc = cd->mcodeptr - cd->mcodebase;
431 int32_t offset = currentrpc + disp;
434 assert(offset % 4 == 0);
438 M_BNE(GET_HIGH_REG(reg), GET_LOW_REG(reg), 5);
441 M_BEQ(GET_HIGH_REG(reg), GET_LOW_REG(reg), 5);
456 vm_abort("emit_branch: unknown condition %d", condition);
459 // The actual branch code which is over-jumped. NOTE: We
460 // don't use a branch delay slot for the conditional
463 // Do the long-branch.
464 M_LUI(REG_ITMP3, offset >> 16);
465 M_OR_IMM(REG_ITMP3, offset, REG_ITMP3);
466 M_AADD(REG_PV, REG_ITMP3, REG_ITMP3);
473 M_BEQ(GET_HIGH_REG(reg), GET_LOW_REG(reg), branchdisp);
476 M_BNE(GET_HIGH_REG(reg), GET_LOW_REG(reg), branchdisp);
479 M_BLTZ(reg, branchdisp);
482 M_BGEZ(reg, branchdisp);
485 M_BGTZ(reg, branchdisp);
488 M_BLEZ(reg, branchdisp);
491 vm_abort("emit_branch: unknown condition %d", condition);
501 /* emit_arithmetic_check *******************************************************
503 Emit an ArithmeticException check.
505 *******************************************************************************/
507 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
509 if (INSTRUCTION_MUST_CHECK(iptr)) {
512 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_ArithmeticException);
517 /* emit_arrayindexoutofbounds_check ********************************************
519 Emit an ArrayIndexOutOfBoundsException check.
521 *******************************************************************************/
523 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
525 if (INSTRUCTION_MUST_CHECK(iptr)) {
526 M_ILD_INTERN(REG_ITMP3, s1, OFFSET(java_array_t, size));
527 M_CMPULT(s2, REG_ITMP3, REG_ITMP3);
528 M_BNEZ(REG_ITMP3, 2);
530 M_ALD_INTERN(s2, REG_ZERO, TRAP_ArrayIndexOutOfBoundsException);
535 /* emit_arraystore_check *******************************************************
537 Emit an ArrayStoreException check.
539 *******************************************************************************/
541 void emit_arraystore_check(codegendata *cd, instruction *iptr)
543 if (INSTRUCTION_MUST_CHECK(iptr)) {
544 M_BNEZ(REG_RESULT, 2);
546 M_ALD_INTERN(REG_RESULT, REG_ZERO, TRAP_ArrayStoreException);
551 /* emit_classcast_check ********************************************************
553 Emit a ClassCastException check.
555 *******************************************************************************/
557 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
559 if (INSTRUCTION_MUST_CHECK(iptr)) {
574 vm_abort("emit_classcast_check: unknown condition %d", condition);
578 M_ALD_INTERN(s1, REG_ZERO, TRAP_ClassCastException);
583 /* emit_nullpointer_check ******************************************************
585 Emit a NullPointerException check.
587 *******************************************************************************/
589 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
591 if (INSTRUCTION_MUST_CHECK(iptr)) {
594 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_NullPointerException);
599 /* emit_exception_check ********************************************************
601 Emit an Exception check.
603 *******************************************************************************/
605 void emit_exception_check(codegendata *cd, instruction *iptr)
607 if (INSTRUCTION_MUST_CHECK(iptr)) {
608 M_BNEZ(REG_RESULT, 2);
610 M_ALD_INTERN(REG_RESULT, REG_ZERO, TRAP_CHECK_EXCEPTION);
615 /* emit_trap_compiler **********************************************************
617 Emit a trap instruction which calls the JIT compiler.
619 *******************************************************************************/
621 void emit_trap_compiler(codegendata *cd)
623 M_ALD_INTERN(REG_METHODPTR, REG_ZERO, TRAP_COMPILER);
627 /* emit_trap *******************************************************************
629 Emit a trap instruction and return the original machine code.
631 *******************************************************************************/
633 uint32_t emit_trap(codegendata *cd)
635 // Get machine code which is patched back in later. The trap is 1
636 // instruction word long.
637 uint32_t mcode = *((uint32_t*) cd->mcodeptr);
645 /* emit_verbosecall_enter ******************************************************
647 Generates the code for the call trace.
649 *******************************************************************************/
652 void emit_verbosecall_enter(jitdata *jd)
662 /* get required compiler data */
671 /* mark trace code */
675 /* keep stack 16-byte aligned */
677 M_LDA(REG_SP, REG_SP, -(PA_SIZE + (md->paramcount + 2 + TMP_CNT) * 8));
678 M_AST(REG_RA, REG_SP, PA_SIZE + md->paramcount * 8);
680 /* save argument registers (we store the registers as address
681 types, so it's correct for MIPS32 too) */
683 for (i = 0; i < md->paramcount; i++) {
684 if (!md->params[i].inmemory) {
685 s = md->params[i].regoff;
686 switch (md->paramtypes[i].type) {
689 M_AST(s, REG_SP, PA_SIZE + i * 8);
692 M_LST(s, REG_SP, PA_SIZE + i * 8);
695 M_FST(s, REG_SP, PA_SIZE + i * 8);
698 M_DST(s, REG_SP, PA_SIZE + i * 8);
704 /* save temporary registers for leaf methods */
706 if (code_is_leafmethod(code)) {
707 for (i = 0; i < INT_TMP_CNT; i++)
708 M_AST(rd->tmpintregs[i], REG_SP, PA_SIZE + (md->paramcount + 2 + i) * 8);
710 for (i = 0; i < FLT_TMP_CNT; i++)
711 M_DST(rd->tmpfltregs[i], REG_SP, PA_SIZE + (md->paramcount + 2 + INT_TMP_CNT + i) * 8);
714 disp = dseg_add_address(cd, m);
715 M_ALD(REG_A0, REG_PV, disp);
716 M_LDA(REG_A1, REG_SP, PA_SIZE);
717 M_LDA(REG_A2, REG_SP, PA_SIZE + (md->paramcount + 2 + TMP_CNT) * 8 + cd->stackframesize * 8);
718 disp = dseg_add_functionptr(cd, trace_java_call_enter);
719 M_ALD(REG_ITMP3, REG_PV, disp);
720 M_JSR(REG_RA, REG_ITMP3);
723 /* restore argument registers */
725 for (i = 0; i < md->paramcount; i++) {
726 if (!md->params[i].inmemory) {
727 s = md->params[i].regoff;
728 switch (md->paramtypes[i].type) {
731 M_ALD(s, REG_SP, PA_SIZE + i * 8);
734 M_LLD(s, REG_SP, PA_SIZE + i * 8);
737 M_FLD(s, REG_SP, PA_SIZE + i * 8);
740 M_DLD(s, REG_SP, PA_SIZE + i * 8);
746 /* restore temporary registers for leaf methods */
748 if (code_is_leafmethod(code)) {
749 for (i = 0; i < INT_TMP_CNT; i++)
750 M_ALD(rd->tmpintregs[i], REG_SP, PA_SIZE + (md->paramcount + 2 + i) * 8);
752 for (i = 0; i < FLT_TMP_CNT; i++)
753 M_DLD(rd->tmpfltregs[i], REG_SP, PA_SIZE + (md->paramcount + 2 + INT_TMP_CNT + i) * 8);
756 /* keep stack 16-byte aligned */
758 M_ALD(REG_RA, REG_SP, PA_SIZE + md->paramcount * 8);
759 M_LDA(REG_SP, REG_SP, PA_SIZE + (md->paramcount + 2 + TMP_CNT) * 8);
761 /* mark trace code */
765 #endif /* !defined(NDEBUG) */
768 /* emit_verbosecall_exit *******************************************************
770 Generates the code for the call trace.
772 *******************************************************************************/
775 void emit_verbosecall_exit(jitdata *jd)
783 /* get required compiler data */
791 /* mark trace code */
795 /* keep stack 16-byte aligned */
797 #if SIZEOF_VOID_P == 8
798 assert(0); // XXX: Revisit this code for MIPS64!
800 M_ASUB_IMM(REG_SP, PA_SIZE + 2 * 8, REG_SP);
801 M_AST(REG_RA, REG_SP, PA_SIZE + 1 * 8);
803 /* save return value */
805 switch (md->returntype.type) {
808 M_AST(REG_RESULT, REG_SP, PA_SIZE + 0 * 8);
811 #if SIZEOF_VOID_P == 8
812 M_LST(REG_RESULT, REG_SP, PA_SIZE + 0 * 8);
814 M_LST(REG_RESULT_PACKED, REG_SP, PA_SIZE + 0 * 8);
818 M_FST(REG_FRESULT, REG_SP, PA_SIZE + 0 * 8);
821 M_DST(REG_FRESULT, REG_SP, PA_SIZE + 0 * 8);
824 disp = dseg_add_address(cd, m);
825 M_ALD(REG_A0, REG_PV, disp);
826 M_AADD_IMM(REG_SP, PA_SIZE, REG_A1);
827 disp = dseg_add_functionptr(cd, trace_java_call_exit);
828 M_ALD(REG_ITMP3, REG_PV, disp);
829 M_JSR(REG_RA, REG_ITMP3);
832 /* restore return value */
834 switch (md->returntype.type) {
837 M_ALD(REG_RESULT, REG_SP, PA_SIZE + 0 * 8);
840 #if SIZEOF_VOID_P == 8
841 M_LLD(REG_RESULT, REG_SP, PA_SIZE + 0 * 8);
843 M_LLD(REG_RESULT_PACKED, REG_SP, PA_SIZE + 0 * 8);
847 M_FLD(REG_FRESULT, REG_SP, PA_SIZE + 0 * 8);
850 M_DLD(REG_FRESULT, REG_SP, PA_SIZE + 0 * 8);
853 /* keep stack 16-byte aligned */
855 M_ALD(REG_RA, REG_SP, PA_SIZE + 1 * 8);
856 M_AADD_IMM(REG_SP, PA_SIZE + 2 * 8, REG_SP);
858 /* mark trace code */
862 #endif /* !defined(NDEBUG) */
866 * These are local overrides for various environment variables in Emacs.
867 * Please do not remove this and leave it at the end of the file, where
868 * Emacs will automagically detect them.
869 * ---------------------------------------------------------------------
872 * indent-tabs-mode: t
876 * vim:noexpandtab:sw=4:ts=4: