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/stringlocal.h" /* XXX for gen_resolvebranch */
50 #include "vm/jit/abi-asm.h"
51 #include "vm/jit/asmpart.h"
52 #include "vm/jit/dseg.h"
53 #include "vm/jit/emit.h"
54 #include "vm/jit/jit.h"
55 #include "vm/jit/replace.h"
58 /* code generation functions **************************************************/
60 /* emit_load *******************************************************************
62 Emits a possible load of an operand.
64 *******************************************************************************/
66 s4 emit_load(jitdata *jd, instruction *iptr, stackptr src, s4 tempreg)
72 /* get required compiler data */
76 if (src->flags & INMEMORY) {
79 disp = src->regoff * 8;
81 if (IS_FLT_DBL_TYPE(src->type)) {
82 if (IS_2_WORD_TYPE(src->type))
83 M_DLD(tempreg, REG_SP, disp);
85 M_FLD(tempreg, REG_SP, disp);
87 M_LLD(tempreg, REG_SP, disp);
96 /* emit_load_s1 ****************************************************************
98 Emits a possible load of the first source operand.
100 *******************************************************************************/
102 s4 emit_load_s1(jitdata *jd, instruction *iptr, s4 tempreg)
109 reg = emit_load(jd, iptr, src, tempreg);
115 /* emit_load_s2 ****************************************************************
117 Emits a possible load of the second source operand.
119 *******************************************************************************/
121 s4 emit_load_s2(jitdata *jd, instruction *iptr, s4 tempreg)
126 src = iptr->sx.s23.s2.var;
128 reg = emit_load(jd, iptr, src, tempreg);
134 /* emit_load_s3 ****************************************************************
136 Emits a possible load of the third source operand.
138 *******************************************************************************/
140 s4 emit_load_s3(jitdata *jd, instruction *iptr, s4 tempreg)
145 src = iptr->sx.s23.s3.var;
147 reg = emit_load(jd, iptr, src, tempreg);
153 /* emit_store ******************************************************************
155 Emits a possible store to variable.
157 *******************************************************************************/
159 void emit_store(jitdata *jd, instruction *iptr, stackptr dst, s4 d)
164 /* get required compiler data */
168 if (dst->flags & INMEMORY) {
171 disp = dst->regoff * 8;
173 if (IS_FLT_DBL_TYPE(dst->type)) {
174 if (IS_2_WORD_TYPE(dst->type))
175 M_DST(d, REG_SP, disp);
177 M_FST(d, REG_SP, disp);
179 M_LST(d, REG_SP, disp);
183 /* emit_store_dst **************************************************************
185 Emits a possible store to the destination operand of an instruction.
187 *******************************************************************************/
189 void emit_store_dst(jitdata *jd, instruction *iptr, s4 d)
191 emit_store(jd, iptr, iptr->dst.var, d);
195 /* emit_copy *******************************************************************
199 *******************************************************************************/
201 void emit_copy(jitdata *jd, instruction *iptr, stackptr src, stackptr dst)
207 /* get required compiler data */
215 src = iptr->sx.s23.s3.var;
217 reg = emit_load(jd, iptr, src, tempreg);
220 if ((src->regoff != dst->regoff) ||
221 ((src->flags ^ dst->flags) & INMEMORY)) {
222 d = codegen_reg_of_var(rd, iptr->opc, dst, REG_IFTMP);
223 s1 = emit_load_s1(jd, iptr, src, d);
226 if (IS_FLT_DBL_TYPE(src->type)) {
227 if (IS_2_WORD_TYPE(src->type))
235 emit_store(jd, iptr, dst, d);
240 /* emit_iconst *****************************************************************
244 *******************************************************************************/
246 void emit_iconst(codegendata *cd, s4 d, s4 value)
250 if ((value >= -32768) && (value <= 32767))
251 M_IADD_IMM(REG_ZERO, value, d);
252 else if ((value >= 0) && (value <= 0xffff))
253 M_OR_IMM(REG_ZERO, value, d);
255 disp = dseg_adds4(cd, value);
256 M_ILD(d, REG_PV, disp);
261 /* emit_lconst *****************************************************************
265 *******************************************************************************/
267 void emit_lconst(codegendata *cd, s4 d, s8 value)
271 if ((value >= -32768) && (value <= 32767))
272 M_LADD_IMM(REG_ZERO, value, d);
273 else if ((value >= 0) && (value <= 0xffff))
274 M_OR_IMM(REG_ZERO, value, d);
276 disp = dseg_adds8(cd, value);
277 M_LLD(d, REG_PV, disp);
282 /* emit_exception_stubs ********************************************************
284 Generates the code for the exception stubs.
286 *******************************************************************************/
288 void emit_exception_stubs(jitdata *jd)
296 /* get required compiler data */
301 /* generate exception stubs */
305 for (eref = cd->exceptionrefs; eref != NULL; eref = eref->next) {
306 gen_resolvebranch(cd->mcodebase + eref->branchpos,
307 eref->branchpos, cd->mcodeptr - cd->mcodebase);
311 /* Check if the exception is an
312 ArrayIndexOutOfBoundsException. If so, move index register
316 M_MOV(eref->reg, REG_ITMP1);
318 /* calcuate exception address */
320 M_LDA(REG_ITMP2_XPC, REG_PV, eref->branchpos - 4);
322 /* move function to call into REG_ITMP3 */
324 disp = dseg_addaddress(cd, eref->function);
325 M_ALD(REG_ITMP3, REG_PV, disp);
327 if (targetdisp == 0) {
328 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
330 M_MOV(REG_PV, rd->argintregs[0]);
331 M_MOV(REG_SP, rd->argintregs[1]);
333 if (jd->isleafmethod)
334 M_MOV(REG_RA, rd->argintregs[2]);
336 M_ALD(rd->argintregs[2],
337 REG_SP, cd->stackframesize * 8 - SIZEOF_VOID_P);
339 M_MOV(REG_ITMP2_XPC, rd->argintregs[3]);
340 M_MOV(REG_ITMP1, rd->argintregs[4]);
342 M_ASUB_IMM(REG_SP, 2 * 8, REG_SP);
343 M_AST(REG_ITMP2_XPC, REG_SP, 0 * 8);
345 if (jd->isleafmethod)
346 M_AST(REG_RA, REG_SP, 1 * 8);
348 M_JSR(REG_RA, REG_ITMP3);
350 M_MOV(REG_RESULT, REG_ITMP1_XPTR);
352 if (jd->isleafmethod)
353 M_ALD(REG_RA, REG_SP, 1 * 8);
355 M_ALD(REG_ITMP2_XPC, REG_SP, 0 * 8);
356 M_AADD_IMM(REG_SP, 2 * 8, REG_SP);
358 disp = dseg_addaddress(cd, asm_handle_exception);
359 M_ALD(REG_ITMP3, REG_PV, disp);
364 disp = (((u4 *) cd->mcodebase) + targetdisp) -
365 (((u4 *) cd->mcodeptr) + 1);
374 /* emit_patcher_stubs **********************************************************
376 Generates the code for the patcher stubs.
378 *******************************************************************************/
380 void emit_patcher_stubs(jitdata *jd)
390 /* get required compiler data */
394 /* generate code patching stub call code */
398 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
399 /* check code segment size */
403 /* Get machine code which is patched back in later. The
404 call is 2 instruction words long. */
406 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
408 /* We use 2 loads here as an unaligned 8-byte read on 64-bit
409 MIPS causes a SIGSEGV and using the same code for both
410 architectures is much better. */
412 mcode[0] = ((u4 *) tmpmcodeptr)[0];
413 mcode[1] = ((u4 *) tmpmcodeptr)[1];
415 /* Patch in the call to call the following code (done at
418 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
419 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
421 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
423 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff)) {
425 new_internalerror("Jump offset is out of range: %d > +/-%d",
433 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
435 /* create stack frame */
437 M_ASUB_IMM(REG_SP, 6 * 8, REG_SP);
439 /* calculate return address and move it onto the stack */
441 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
442 M_AST(REG_ITMP3, REG_SP, 5 * 8);
444 /* move pointer to java_objectheader onto stack */
446 #if defined(ENABLE_THREADS)
447 /* create a virtual java_objectheader */
449 (void) dseg_addaddress(cd, NULL); /* flcword */
450 (void) dseg_addaddress(cd, lock_get_initial_lock_word());
451 disp = dseg_addaddress(cd, NULL); /* vftbl */
453 M_LDA(REG_ITMP3, REG_PV, disp);
454 M_AST(REG_ITMP3, REG_SP, 4 * 8);
459 /* move machine code onto stack */
461 disp = dseg_adds4(cd, mcode[0]);
462 M_ILD(REG_ITMP3, REG_PV, disp);
463 M_IST(REG_ITMP3, REG_SP, 3 * 8);
465 disp = dseg_adds4(cd, mcode[1]);
466 M_ILD(REG_ITMP3, REG_PV, disp);
467 M_IST(REG_ITMP3, REG_SP, 3 * 8 + 4);
469 /* move class/method/field reference onto stack */
471 disp = dseg_addaddress(cd, pref->ref);
472 M_ALD(REG_ITMP3, REG_PV, disp);
473 M_AST(REG_ITMP3, REG_SP, 2 * 8);
475 /* move data segment displacement onto stack */
477 disp = dseg_adds4(cd, pref->disp);
478 M_ILD(REG_ITMP3, REG_PV, disp);
479 M_IST(REG_ITMP3, REG_SP, 1 * 8);
481 /* move patcher function pointer onto stack */
483 disp = dseg_addaddress(cd, pref->patcher);
484 M_ALD(REG_ITMP3, REG_PV, disp);
485 M_AST(REG_ITMP3, REG_SP, 0 * 8);
487 if (targetdisp == 0) {
488 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
490 disp = dseg_addaddress(cd, asm_patcher_wrapper);
491 M_ALD(REG_ITMP3, REG_PV, disp);
496 disp = (((u4 *) cd->mcodebase) + targetdisp) -
497 (((u4 *) cd->mcodeptr) + 1);
506 /* emit_replacement_stubs ******************************************************
508 Generates the code for the replacement stubs.
510 *******************************************************************************/
512 void emit_replacement_stubs(jitdata *jd)
521 /* get required compiler data */
526 rplp = code->rplpoints;
528 for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
529 /* check code segment size */
533 /* note start of stub code */
535 rplp->outcode = (u1 *) (ptrint) (cd->mcodeptr - cd->mcodebase);
537 /* make machine code for patching */
539 savedmcodeptr = cd->mcodeptr;
540 cd->mcodeptr = (u1 *) &(rplp->mcode);
542 disp = (ptrint) ((s4 *) rplp->outcode - (s4 *) rplp->pc) - 1;
544 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff)) {
546 new_internalerror("Jump offset is out of range: %d > +/-%d",
552 M_NOP; /* delay slot */
554 cd->mcodeptr = savedmcodeptr;
556 /* create stack frame - 16-byte aligned */
558 M_ASUB_IMM(REG_SP, 2 * 8, REG_SP);
560 /* push address of `rplpoint` struct */
562 disp = dseg_addaddress(cd, rplp);
563 M_ALD(REG_ITMP3, REG_PV, disp);
564 M_AST(REG_ITMP3, REG_SP, 0 * 8);
566 /* jump to replacement function */
568 disp = dseg_addaddress(cd, asm_replacement_out);
569 M_ALD(REG_ITMP3, REG_PV, disp);
571 M_NOP; /* delay slot */
576 /* emit_verbosecall_enter ******************************************************
578 Generates the code for the call trace.
580 *******************************************************************************/
583 void emit_verbosecall_enter(jitdata *jd)
592 /* get required compiler data */
600 /* mark trace code */
604 M_LDA(REG_SP, REG_SP, -(2 + ARG_CNT + TMP_CNT) * 8);
605 M_AST(REG_RA, REG_SP, 1 * 8);
607 /* save argument registers */
609 for (i = 0; i < INT_ARG_CNT; i++)
610 M_LST(rd->argintregs[i], REG_SP, (2 + i) * 8);
612 for (i = 0; i < FLT_ARG_CNT; i++)
613 M_DST(rd->argfltregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
615 /* save temporary registers for leaf methods */
617 if (jd->isleafmethod) {
618 for (i = 0; i < INT_TMP_CNT; i++)
619 M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
621 for (i = 0; i < FLT_TMP_CNT; i++)
622 M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
625 /* load float arguments into integer registers */
627 for (i = 0; i < md->paramcount && i < INT_ARG_CNT; i++) {
628 t = md->paramtypes[i].type;
630 if (IS_FLT_DBL_TYPE(t)) {
631 if (IS_2_WORD_TYPE(t)) {
632 M_DST(rd->argfltregs[i], REG_SP, 0 * 8);
633 M_LLD(rd->argintregs[i], REG_SP, 0 * 8);
636 M_FST(rd->argfltregs[i], REG_SP, 0 * 8);
637 M_ILD(rd->argintregs[i], REG_SP, 0 * 8);
642 disp = dseg_addaddress(cd, m);
643 M_ALD(REG_ITMP1, REG_PV, disp);
644 M_AST(REG_ITMP1, REG_SP, 0 * 8);
645 disp = dseg_addaddress(cd, builtin_trace_args);
646 M_ALD(REG_ITMP3, REG_PV, disp);
647 M_JSR(REG_RA, REG_ITMP3);
650 /* restore argument registers */
652 for (i = 0; i < INT_ARG_CNT; i++)
653 M_LLD(rd->argintregs[i], REG_SP, (2 + i) * 8);
655 for (i = 0; i < FLT_ARG_CNT; i++)
656 M_DLD(rd->argfltregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
658 /* restore temporary registers for leaf methods */
660 if (jd->isleafmethod) {
661 for (i = 0; i < INT_TMP_CNT; i++)
662 M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
664 for (i = 0; i < FLT_TMP_CNT; i++)
665 M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
668 M_ALD(REG_RA, REG_SP, 1 * 8);
669 M_LDA(REG_SP, REG_SP, (2 + ARG_CNT + TMP_CNT) * 8);
671 /* mark trace code */
675 #endif /* !defined(NDEBUG) */
678 /* emit_verbosecall_exit *******************************************************
680 Generates the code for the call trace.
682 *******************************************************************************/
685 void emit_verbosecall_exit(jitdata *jd)
692 /* get required compiler data */
698 /* mark trace code */
702 M_LDA(REG_SP, REG_SP, -4 * 8); /* keep stack 16-byte aligned */
703 M_LST(REG_RA, REG_SP, 0 * 8);
705 M_LST(REG_RESULT, REG_SP, 1 * 8);
706 M_DST(REG_FRESULT, REG_SP, 2 * 8);
708 disp = dseg_addaddress(cd, m);
709 M_ALD(rd->argintregs[0], REG_PV, disp);
711 M_MOV(REG_RESULT, rd->argintregs[1]);
712 M_DMOV(REG_FRESULT, rd->argfltregs[2]);
713 M_FMOV(REG_FRESULT, rd->argfltregs[3]);
715 disp = dseg_addaddress(cd, builtin_displaymethodstop);
716 M_ALD(REG_ITMP3, REG_PV, disp);
717 M_JSR(REG_RA, REG_ITMP3);
720 M_DLD(REG_FRESULT, REG_SP, 2 * 8);
721 M_LLD(REG_RESULT, REG_SP, 1 * 8);
723 M_LLD(REG_RA, REG_SP, 0 * 8);
724 M_LDA(REG_SP, REG_SP, 4 * 8);
726 /* mark trace code */
730 #endif /* !defined(NDEBUG) */
734 * These are local overrides for various environment variables in Emacs.
735 * Please do not remove this and leave it at the end of the file, where
736 * Emacs will automagically detect them.
737 * ---------------------------------------------------------------------
740 * indent-tabs-mode: t
744 * vim:noexpandtab:sw=4:ts=4: