1 /* src/vm/jit/mips/emit.c - MIPS code emitter functions
3 Copyright (C) 1996-2005, 2006 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
25 Contact: cacao@cacaojvm.org
27 Authors: Christian Thalinger
31 $Id: emitfuncs.c 4398 2006-01-31 23:43:08Z twisti $
42 #include "vm/jit/mips/codegen.h"
44 #if defined(ENABLE_THREADS)
45 # include "threads/native/lock.h"
48 #include "vm/exceptions.h"
49 #include "vm/options.h"
50 #include "vm/stringlocal.h" /* XXX for gen_resolvebranch */
51 #include "vm/jit/abi-asm.h"
52 #include "vm/jit/asmpart.h"
53 #include "vm/jit/dseg.h"
54 #include "vm/jit/emit-common.h"
55 #include "vm/jit/jit.h"
56 #include "vm/jit/replace.h"
59 /* emit_load *******************************************************************
61 Emits a possible load of an operand.
63 *******************************************************************************/
65 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
71 /* get required compiler data */
75 if (src->flags & INMEMORY) {
78 disp = src->vv.regoff * 8;
80 if (IS_FLT_DBL_TYPE(src->type)) {
81 if (IS_2_WORD_TYPE(src->type))
82 M_DLD(tempreg, REG_SP, disp);
84 M_FLD(tempreg, REG_SP, disp);
87 M_LLD(tempreg, REG_SP, disp);
98 /* emit_store ******************************************************************
100 Emits a possible store to variable.
102 *******************************************************************************/
104 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
109 /* get required compiler data */
113 if (dst->flags & INMEMORY) {
116 disp = dst->vv.regoff * 8;
118 if (IS_FLT_DBL_TYPE(dst->type)) {
119 if (IS_2_WORD_TYPE(dst->type))
120 M_DST(d, REG_SP, disp);
122 M_FST(d, REG_SP, disp);
125 M_LST(d, REG_SP, disp);
130 /* emit_copy *******************************************************************
132 Generates a register/memory to register/memory copy.
134 *******************************************************************************/
136 void emit_copy(jitdata *jd, instruction *iptr, varinfo *src, varinfo *dst)
141 /* get required compiler data */
145 if ((src->vv.regoff != dst->vv.regoff) ||
146 ((src->flags ^ dst->flags) & INMEMORY)) {
148 /* If one of the variables resides in memory, we can eliminate
149 the register move from/to the temporary register with the
150 order of getting the destination register and the load. */
152 if (IS_INMEMORY(src->flags)) {
153 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
154 s1 = emit_load(jd, iptr, src, d);
157 s1 = emit_load(jd, iptr, src, REG_IFTMP);
158 d = codegen_reg_of_var(iptr->opc, dst, s1);
162 if (IS_FLT_DBL_TYPE(src->type)) {
163 if (IS_2_WORD_TYPE(src->type))
172 emit_store(jd, iptr, dst, d);
177 /* emit_iconst *****************************************************************
181 *******************************************************************************/
183 void emit_iconst(codegendata *cd, s4 d, s4 value)
187 if ((value >= -32768) && (value <= 32767))
188 M_IADD_IMM(REG_ZERO, value, d);
189 else if ((value >= 0) && (value <= 0xffff))
190 M_OR_IMM(REG_ZERO, value, d);
192 disp = dseg_add_s4(cd, value);
193 M_ILD(d, REG_PV, disp);
198 /* emit_lconst *****************************************************************
202 *******************************************************************************/
204 void emit_lconst(codegendata *cd, s4 d, s8 value)
208 if ((value >= -32768) && (value <= 32767))
209 M_LADD_IMM(REG_ZERO, value, d);
210 else if ((value >= 0) && (value <= 0xffff))
211 M_OR_IMM(REG_ZERO, value, d);
213 disp = dseg_add_s8(cd, value);
214 M_LLD(d, REG_PV, disp);
219 /* emit_arithmetic_check *******************************************************
221 Emit an ArithmeticException check.
223 *******************************************************************************/
225 void emit_arithmetic_check(codegendata *cd, s4 reg)
229 codegen_add_arithmeticexception_ref(cd);
236 M_OR_IMM(REG_ITMP3, 0, REG_ITMP3);
237 codegen_add_arithmeticexception_ref(cd);
238 M_AADD(REG_PV, REG_ITMP3, REG_ITMP3);
245 /* emit_arrayindexoutofbounds_check ********************************************
247 Emit an ArrayIndexOutOfBoundsException check.
249 *******************************************************************************/
251 void emit_arrayindexoutofbounds_check(codegendata *cd, s4 s1, s4 s2)
254 M_ILD(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
255 M_CMPULT(s2, REG_ITMP3, REG_ITMP3);
258 M_BEQZ(REG_ITMP3, 0);
259 codegen_add_arrayindexoutofboundsexception_ref(cd, s2);
262 M_BNEZ(REG_ITMP3, 6);
266 M_OR_IMM(REG_ITMP3, 0, REG_ITMP3);
267 codegen_add_arrayindexoutofboundsexception_ref(cd, s2);
268 M_AADD(REG_PV, REG_ITMP3, REG_ITMP3);
276 /* emit_arraystore_check *******************************************************
278 Emit an ArrayStoreException check.
280 *******************************************************************************/
282 void emit_arraystore_check(codegendata *cd, s4 reg)
286 codegen_add_arraystoreexception_ref(cd);
293 M_OR_IMM(REG_ITMP3, 0, REG_ITMP3);
294 codegen_add_arraystoreexception_ref(cd);
295 M_AADD(REG_PV, REG_ITMP3, REG_ITMP3);
302 /* emit_classcast_check ********************************************************
304 Emit a ClassCastException check.
306 *******************************************************************************/
308 void emit_classcast_check(codegendata *cd, s4 condition, s4 reg, s4 s1)
312 codegen_add_classcastexception_ref(cd, s1);
329 vm_abort("emit_classcast_check: condition %d not found", condition);
335 M_OR_IMM(REG_ITMP3, 0, REG_ITMP3);
336 codegen_add_classcastexception_ref(cd, s1);
337 M_AADD(REG_PV, REG_ITMP3, REG_ITMP3);
344 /* emit_nullpointer_check ******************************************************
346 Emit a NullPointerException check.
348 *******************************************************************************/
350 void emit_nullpointer_check(codegendata *cd, s4 reg)
355 codegen_add_nullpointerexception_ref(cd);
362 M_OR_IMM(REG_ITMP3, 0, REG_ITMP3);
363 codegen_add_nullpointerexception_ref(cd);
364 M_AADD(REG_PV, REG_ITMP3, REG_ITMP3);
372 /* emit_exception_check ********************************************************
374 Emit an Exception check.
376 *******************************************************************************/
378 void emit_exception_check(codegendata *cd)
381 M_BEQZ(REG_RESULT, 0);
382 codegen_add_fillinstacktrace_ref(cd);
385 M_BNEZ(REG_RESULT, 6);
389 M_OR_IMM(REG_ITMP3, 0, REG_ITMP3);
390 codegen_add_fillinstacktrace_ref(cd);
391 M_AADD(REG_PV, REG_ITMP3, REG_ITMP3);
398 /* emit_exception_stubs ********************************************************
400 Generates the code for the exception stubs.
402 *******************************************************************************/
404 void emit_exception_stubs(jitdata *jd)
414 /* get required compiler data */
419 /* generate exception stubs */
423 for (er = cd->exceptionrefs; er != NULL; er = er->next) {
424 /* back-patch the branch to this exception code */
426 branchmpc = er->branchpos;
427 targetmpc = cd->mcodeptr - cd->mcodebase;
429 md_codegen_patch_branch(cd, branchmpc, targetmpc);
433 /* Check if the exception is an
434 ArrayIndexOutOfBoundsException. If so, move index register
438 M_MOV(er->reg, REG_ITMP1);
440 /* calcuate exception address */
442 M_LDA(REG_ITMP2_XPC, REG_PV, er->branchpos - 4);
444 /* move function to call into REG_ITMP3 */
446 disp = dseg_add_functionptr(cd, er->function);
447 M_ALD(REG_ITMP3, REG_PV, disp);
449 if (targetdisp == 0) {
450 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
452 M_MOV(REG_PV, REG_A0);
453 M_MOV(REG_SP, REG_A1);
455 if (jd->isleafmethod)
456 M_MOV(REG_RA, REG_A2);
458 M_ALD(REG_A2, REG_SP, (cd->stackframesize - 1) * 8);
460 M_MOV(REG_ITMP2_XPC, REG_A3);
461 M_MOV(REG_ITMP1, REG_A4);
463 M_ASUB_IMM(REG_SP, 2 * 8, REG_SP);
464 M_AST(REG_ITMP2_XPC, REG_SP, 0 * 8);
466 if (jd->isleafmethod)
467 M_AST(REG_RA, REG_SP, 1 * 8);
469 M_JSR(REG_RA, REG_ITMP3);
471 M_MOV(REG_RESULT, REG_ITMP1_XPTR);
473 if (jd->isleafmethod)
474 M_ALD(REG_RA, REG_SP, 1 * 8);
476 M_ALD(REG_ITMP2_XPC, REG_SP, 0 * 8);
477 M_AADD_IMM(REG_SP, 2 * 8, REG_SP);
479 disp = dseg_add_functionptr(cd, asm_handle_exception);
480 M_ALD(REG_ITMP3, REG_PV, disp);
485 disp = (((u4 *) cd->mcodebase) + targetdisp) -
486 (((u4 *) cd->mcodeptr) + 1);
495 /* emit_patcher_stubs **********************************************************
497 Generates the code for the patcher stubs.
499 *******************************************************************************/
501 void emit_patcher_stubs(jitdata *jd)
511 /* get required compiler data */
515 /* generate code patching stub call code */
519 /* for (pr = list_first_unsynced(cd->patchrefs); pr != NULL; */
520 /* pr = list_next_unsynced(cd->patchrefs, pr)) { */
521 for (pr = cd->patchrefs; pr != NULL; pr = pr->next) {
522 /* check code segment size */
526 /* Get machine code which is patched back in later. The
527 call is 2 instruction words long. */
529 tmpmcodeptr = (u1 *) (cd->mcodebase + pr->branchpos);
531 /* We use 2 loads here as an unaligned 8-byte read on 64-bit
532 MIPS causes a SIGSEGV and using the same code for both
533 architectures is much better. */
535 mcode[0] = ((u4 *) tmpmcodeptr)[0];
536 mcode[1] = ((u4 *) tmpmcodeptr)[1];
538 /* Patch in the call to call the following code (done at
541 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
542 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
544 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
546 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff)) {
548 new_internalerror("Jump offset is out of range: %d > +/-%d",
556 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
558 /* create stack frame */
560 M_ASUB_IMM(REG_SP, 6 * 8, REG_SP);
562 /* calculate return address and move it onto the stack */
564 M_LDA(REG_ITMP3, REG_PV, pr->branchpos);
565 M_AST(REG_ITMP3, REG_SP, 5 * 8);
567 /* move pointer to java_objectheader onto stack */
569 #if defined(ENABLE_THREADS)
570 /* create a virtual java_objectheader */
572 (void) dseg_add_unique_address(cd, NULL); /* flcword */
573 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
574 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
576 M_LDA(REG_ITMP3, REG_PV, disp);
577 M_AST(REG_ITMP3, REG_SP, 4 * 8);
582 /* move machine code onto stack */
584 disp = dseg_add_s4(cd, mcode[0]);
585 M_ILD(REG_ITMP3, REG_PV, disp);
586 M_IST(REG_ITMP3, REG_SP, 3 * 8);
588 disp = dseg_add_s4(cd, mcode[1]);
589 M_ILD(REG_ITMP3, REG_PV, disp);
590 M_IST(REG_ITMP3, REG_SP, 3 * 8 + 4);
592 /* move class/method/field reference onto stack */
594 disp = dseg_add_address(cd, pr->ref);
595 M_ALD(REG_ITMP3, REG_PV, disp);
596 M_AST(REG_ITMP3, REG_SP, 2 * 8);
598 /* move data segment displacement onto stack */
600 disp = dseg_add_s4(cd, pr->disp);
601 M_ILD(REG_ITMP3, REG_PV, disp);
602 M_IST(REG_ITMP3, REG_SP, 1 * 8);
604 /* move patcher function pointer onto stack */
606 disp = dseg_add_address(cd, pr->patcher);
607 M_ALD(REG_ITMP3, REG_PV, disp);
608 M_AST(REG_ITMP3, REG_SP, 0 * 8);
610 if (targetdisp == 0) {
611 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
613 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
614 M_ALD(REG_ITMP3, REG_PV, disp);
619 disp = (((u4 *) cd->mcodebase) + targetdisp) -
620 (((u4 *) cd->mcodeptr) + 1);
629 /* emit_replacement_stubs ******************************************************
631 Generates the code for the replacement stubs.
633 *******************************************************************************/
635 void emit_replacement_stubs(jitdata *jd)
644 /* get required compiler data */
649 rplp = code->rplpoints;
651 for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
652 /* check code segment size */
656 /* note start of stub code */
658 rplp->outcode = (u1 *) (ptrint) (cd->mcodeptr - cd->mcodebase);
660 /* make machine code for patching */
662 savedmcodeptr = cd->mcodeptr;
663 cd->mcodeptr = (u1 *) &(rplp->mcode);
665 disp = (ptrint) ((s4 *) rplp->outcode - (s4 *) rplp->pc) - 1;
667 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff)) {
669 new_internalerror("Jump offset is out of range: %d > +/-%d",
675 M_NOP; /* delay slot */
677 cd->mcodeptr = savedmcodeptr;
679 /* create stack frame - 16-byte aligned */
681 M_ASUB_IMM(REG_SP, 2 * 8, REG_SP);
683 /* push address of `rplpoint` struct */
685 disp = dseg_add_address(cd, rplp);
686 M_ALD(REG_ITMP3, REG_PV, disp);
687 M_AST(REG_ITMP3, REG_SP, 0 * 8);
689 /* jump to replacement function */
691 disp = dseg_add_functionptr(cd, asm_replacement_out);
692 M_ALD(REG_ITMP3, REG_PV, disp);
694 M_NOP; /* delay slot */
699 /* emit_verbosecall_enter ******************************************************
701 Generates the code for the call trace.
703 *******************************************************************************/
706 void emit_verbosecall_enter(jitdata *jd)
715 /* get required compiler data */
723 /* mark trace code */
727 M_LDA(REG_SP, REG_SP, -(2 + ARG_CNT + TMP_CNT) * 8);
728 M_AST(REG_RA, REG_SP, 1 * 8);
730 /* save argument registers */
732 for (i = 0; i < INT_ARG_CNT; i++)
733 M_LST(rd->argintregs[i], REG_SP, (2 + i) * 8);
735 for (i = 0; i < FLT_ARG_CNT; i++)
736 M_DST(rd->argfltregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
738 /* save temporary registers for leaf methods */
740 if (jd->isleafmethod) {
741 for (i = 0; i < INT_TMP_CNT; i++)
742 M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
744 for (i = 0; i < FLT_TMP_CNT; i++)
745 M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
748 /* load float arguments into integer registers */
750 for (i = 0; i < md->paramcount && i < INT_ARG_CNT; i++) {
751 t = md->paramtypes[i].type;
753 if (IS_FLT_DBL_TYPE(t)) {
754 if (IS_2_WORD_TYPE(t)) {
755 M_DST(rd->argfltregs[i], REG_SP, 0 * 8);
756 M_LLD(rd->argintregs[i], REG_SP, 0 * 8);
759 M_FST(rd->argfltregs[i], REG_SP, 0 * 8);
760 M_ILD(rd->argintregs[i], REG_SP, 0 * 8);
765 disp = dseg_add_address(cd, m);
766 M_ALD(REG_ITMP1, REG_PV, disp);
767 M_AST(REG_ITMP1, REG_SP, 0 * 8);
768 disp = dseg_add_functionptr(cd, builtin_trace_args);
769 M_ALD(REG_ITMP3, REG_PV, disp);
770 M_JSR(REG_RA, REG_ITMP3);
773 /* restore argument registers */
775 for (i = 0; i < INT_ARG_CNT; i++)
776 M_LLD(rd->argintregs[i], REG_SP, (2 + i) * 8);
778 for (i = 0; i < FLT_ARG_CNT; i++)
779 M_DLD(rd->argfltregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
781 /* restore temporary registers for leaf methods */
783 if (jd->isleafmethod) {
784 for (i = 0; i < INT_TMP_CNT; i++)
785 M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
787 for (i = 0; i < FLT_TMP_CNT; i++)
788 M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
791 M_ALD(REG_RA, REG_SP, 1 * 8);
792 M_LDA(REG_SP, REG_SP, (2 + ARG_CNT + TMP_CNT) * 8);
794 /* mark trace code */
798 #endif /* !defined(NDEBUG) */
801 /* emit_verbosecall_exit *******************************************************
803 Generates the code for the call trace.
805 *******************************************************************************/
808 void emit_verbosecall_exit(jitdata *jd)
815 /* get required compiler data */
821 /* mark trace code */
825 M_LDA(REG_SP, REG_SP, -4 * 8); /* keep stack 16-byte aligned */
826 M_LST(REG_RA, REG_SP, 0 * 8);
828 M_LST(REG_RESULT, REG_SP, 1 * 8);
829 M_DST(REG_FRESULT, REG_SP, 2 * 8);
831 disp = dseg_add_address(cd, m);
832 M_ALD(rd->argintregs[0], REG_PV, disp);
834 M_MOV(REG_RESULT, rd->argintregs[1]);
835 M_DMOV(REG_FRESULT, rd->argfltregs[2]);
836 M_FMOV(REG_FRESULT, rd->argfltregs[3]);
838 disp = dseg_add_functionptr(cd, builtin_displaymethodstop);
839 M_ALD(REG_ITMP3, REG_PV, disp);
840 M_JSR(REG_RA, REG_ITMP3);
843 M_DLD(REG_FRESULT, REG_SP, 2 * 8);
844 M_LLD(REG_RESULT, REG_SP, 1 * 8);
846 M_LLD(REG_RA, REG_SP, 0 * 8);
847 M_LDA(REG_SP, REG_SP, 4 * 8);
849 /* mark trace code */
853 #endif /* !defined(NDEBUG) */
857 * These are local overrides for various environment variables in Emacs.
858 * Please do not remove this and leave it at the end of the file, where
859 * Emacs will automagically detect them.
860 * ---------------------------------------------------------------------
863 * indent-tabs-mode: t
867 * vim:noexpandtab:sw=4:ts=4: