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);
88 M_LLD(tempreg, REG_SP, disp);
98 /* emit_load_s1 ****************************************************************
100 Emits a possible load of the first source operand.
102 *******************************************************************************/
104 s4 emit_load_s1(jitdata *jd, instruction *iptr, s4 tempreg)
111 reg = emit_load(jd, iptr, src, tempreg);
117 /* emit_load_s2 ****************************************************************
119 Emits a possible load of the second source operand.
121 *******************************************************************************/
123 s4 emit_load_s2(jitdata *jd, instruction *iptr, s4 tempreg)
128 src = iptr->sx.s23.s2.var;
130 reg = emit_load(jd, iptr, src, tempreg);
136 /* emit_load_s3 ****************************************************************
138 Emits a possible load of the third source operand.
140 *******************************************************************************/
142 s4 emit_load_s3(jitdata *jd, instruction *iptr, s4 tempreg)
147 src = iptr->sx.s23.s3.var;
149 reg = emit_load(jd, iptr, src, tempreg);
155 /* emit_store ******************************************************************
157 Emits a possible store to variable.
159 *******************************************************************************/
161 void emit_store(jitdata *jd, instruction *iptr, stackptr dst, s4 d)
166 /* get required compiler data */
170 if (dst->flags & INMEMORY) {
173 disp = dst->regoff * 8;
175 if (IS_FLT_DBL_TYPE(dst->type)) {
176 if (IS_2_WORD_TYPE(dst->type))
177 M_DST(d, REG_SP, disp);
179 M_FST(d, REG_SP, disp);
181 M_LST(d, REG_SP, disp);
185 /* emit_store_dst **************************************************************
187 Emits a possible store to the destination operand of an instruction.
189 *******************************************************************************/
191 void emit_store_dst(jitdata *jd, instruction *iptr, s4 d)
193 emit_store(jd, iptr, iptr->dst.var, d);
197 /* emit_copy *******************************************************************
199 Generates a register/memory to register/memory copy.
201 *******************************************************************************/
203 void emit_copy(jitdata *jd, instruction *iptr, stackptr src, stackptr dst)
209 /* get required compiler data */
214 if ((src->regoff != dst->regoff) ||
215 ((src->flags ^ dst->flags) & INMEMORY)) {
217 /* If one of the variables resides in memory, we can eliminate
218 the register move from/to the temporary register with the
219 order of getting the destination register and the load. */
221 if (IS_INMEMORY(src->flags)) {
222 d = codegen_reg_of_var(rd, iptr->opc, dst, REG_IFTMP);
223 s1 = emit_load(jd, iptr, src, d);
226 s1 = emit_load(jd, iptr, src, REG_IFTMP);
227 d = codegen_reg_of_var(rd, iptr->opc, dst, s1);
231 if (IS_FLT_DBL_TYPE(src->type)) {
232 if (IS_2_WORD_TYPE(src->type))
241 emit_store(jd, iptr, dst, d);
246 /* emit_iconst *****************************************************************
250 *******************************************************************************/
252 void emit_iconst(codegendata *cd, s4 d, s4 value)
256 if ((value >= -32768) && (value <= 32767))
257 M_IADD_IMM(REG_ZERO, value, d);
258 else if ((value >= 0) && (value <= 0xffff))
259 M_OR_IMM(REG_ZERO, value, d);
261 disp = dseg_adds4(cd, value);
262 M_ILD(d, REG_PV, disp);
267 /* emit_lconst *****************************************************************
271 *******************************************************************************/
273 void emit_lconst(codegendata *cd, s4 d, s8 value)
277 if ((value >= -32768) && (value <= 32767))
278 M_LADD_IMM(REG_ZERO, value, d);
279 else if ((value >= 0) && (value <= 0xffff))
280 M_OR_IMM(REG_ZERO, value, d);
282 disp = dseg_adds8(cd, value);
283 M_LLD(d, REG_PV, disp);
288 /* emit_exception_stubs ********************************************************
290 Generates the code for the exception stubs.
292 *******************************************************************************/
294 void emit_exception_stubs(jitdata *jd)
302 /* get required compiler data */
307 /* generate exception stubs */
311 for (eref = cd->exceptionrefs; eref != NULL; eref = eref->next) {
312 gen_resolvebranch(cd->mcodebase + eref->branchpos,
313 eref->branchpos, cd->mcodeptr - cd->mcodebase);
317 /* Check if the exception is an
318 ArrayIndexOutOfBoundsException. If so, move index register
322 M_MOV(eref->reg, REG_ITMP1);
324 /* calcuate exception address */
326 M_LDA(REG_ITMP2_XPC, REG_PV, eref->branchpos - 4);
328 /* move function to call into REG_ITMP3 */
330 disp = dseg_addaddress(cd, eref->function);
331 M_ALD(REG_ITMP3, REG_PV, disp);
333 if (targetdisp == 0) {
334 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
336 M_MOV(REG_PV, rd->argintregs[0]);
337 M_MOV(REG_SP, rd->argintregs[1]);
339 if (jd->isleafmethod)
340 M_MOV(REG_RA, rd->argintregs[2]);
342 M_ALD(rd->argintregs[2],
343 REG_SP, cd->stackframesize * 8 - SIZEOF_VOID_P);
345 M_MOV(REG_ITMP2_XPC, rd->argintregs[3]);
346 M_MOV(REG_ITMP1, rd->argintregs[4]);
348 M_ASUB_IMM(REG_SP, 2 * 8, REG_SP);
349 M_AST(REG_ITMP2_XPC, REG_SP, 0 * 8);
351 if (jd->isleafmethod)
352 M_AST(REG_RA, REG_SP, 1 * 8);
354 M_JSR(REG_RA, REG_ITMP3);
356 M_MOV(REG_RESULT, REG_ITMP1_XPTR);
358 if (jd->isleafmethod)
359 M_ALD(REG_RA, REG_SP, 1 * 8);
361 M_ALD(REG_ITMP2_XPC, REG_SP, 0 * 8);
362 M_AADD_IMM(REG_SP, 2 * 8, REG_SP);
364 disp = dseg_addaddress(cd, asm_handle_exception);
365 M_ALD(REG_ITMP3, REG_PV, disp);
370 disp = (((u4 *) cd->mcodebase) + targetdisp) -
371 (((u4 *) cd->mcodeptr) + 1);
380 /* emit_patcher_stubs **********************************************************
382 Generates the code for the patcher stubs.
384 *******************************************************************************/
386 void emit_patcher_stubs(jitdata *jd)
396 /* get required compiler data */
400 /* generate code patching stub call code */
404 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
405 /* check code segment size */
409 /* Get machine code which is patched back in later. The
410 call is 2 instruction words long. */
412 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
414 /* We use 2 loads here as an unaligned 8-byte read on 64-bit
415 MIPS causes a SIGSEGV and using the same code for both
416 architectures is much better. */
418 mcode[0] = ((u4 *) tmpmcodeptr)[0];
419 mcode[1] = ((u4 *) tmpmcodeptr)[1];
421 /* Patch in the call to call the following code (done at
424 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
425 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
427 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
429 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff)) {
431 new_internalerror("Jump offset is out of range: %d > +/-%d",
439 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
441 /* create stack frame */
443 M_ASUB_IMM(REG_SP, 6 * 8, REG_SP);
445 /* calculate return address and move it onto the stack */
447 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
448 M_AST(REG_ITMP3, REG_SP, 5 * 8);
450 /* move pointer to java_objectheader onto stack */
452 #if defined(ENABLE_THREADS)
453 /* create a virtual java_objectheader */
455 (void) dseg_addaddress(cd, NULL); /* flcword */
456 (void) dseg_addaddress(cd, lock_get_initial_lock_word());
457 disp = dseg_addaddress(cd, NULL); /* vftbl */
459 M_LDA(REG_ITMP3, REG_PV, disp);
460 M_AST(REG_ITMP3, REG_SP, 4 * 8);
465 /* move machine code onto stack */
467 disp = dseg_adds4(cd, mcode[0]);
468 M_ILD(REG_ITMP3, REG_PV, disp);
469 M_IST(REG_ITMP3, REG_SP, 3 * 8);
471 disp = dseg_adds4(cd, mcode[1]);
472 M_ILD(REG_ITMP3, REG_PV, disp);
473 M_IST(REG_ITMP3, REG_SP, 3 * 8 + 4);
475 /* move class/method/field reference onto stack */
477 disp = dseg_addaddress(cd, pref->ref);
478 M_ALD(REG_ITMP3, REG_PV, disp);
479 M_AST(REG_ITMP3, REG_SP, 2 * 8);
481 /* move data segment displacement onto stack */
483 disp = dseg_adds4(cd, pref->disp);
484 M_ILD(REG_ITMP3, REG_PV, disp);
485 M_IST(REG_ITMP3, REG_SP, 1 * 8);
487 /* move patcher function pointer onto stack */
489 disp = dseg_addaddress(cd, pref->patcher);
490 M_ALD(REG_ITMP3, REG_PV, disp);
491 M_AST(REG_ITMP3, REG_SP, 0 * 8);
493 if (targetdisp == 0) {
494 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
496 disp = dseg_addaddress(cd, asm_patcher_wrapper);
497 M_ALD(REG_ITMP3, REG_PV, disp);
502 disp = (((u4 *) cd->mcodebase) + targetdisp) -
503 (((u4 *) cd->mcodeptr) + 1);
512 /* emit_replacement_stubs ******************************************************
514 Generates the code for the replacement stubs.
516 *******************************************************************************/
518 void emit_replacement_stubs(jitdata *jd)
527 /* get required compiler data */
532 rplp = code->rplpoints;
534 for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
535 /* check code segment size */
539 /* note start of stub code */
541 rplp->outcode = (u1 *) (ptrint) (cd->mcodeptr - cd->mcodebase);
543 /* make machine code for patching */
545 savedmcodeptr = cd->mcodeptr;
546 cd->mcodeptr = (u1 *) &(rplp->mcode);
548 disp = (ptrint) ((s4 *) rplp->outcode - (s4 *) rplp->pc) - 1;
550 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff)) {
552 new_internalerror("Jump offset is out of range: %d > +/-%d",
558 M_NOP; /* delay slot */
560 cd->mcodeptr = savedmcodeptr;
562 /* create stack frame - 16-byte aligned */
564 M_ASUB_IMM(REG_SP, 2 * 8, REG_SP);
566 /* push address of `rplpoint` struct */
568 disp = dseg_addaddress(cd, rplp);
569 M_ALD(REG_ITMP3, REG_PV, disp);
570 M_AST(REG_ITMP3, REG_SP, 0 * 8);
572 /* jump to replacement function */
574 disp = dseg_addaddress(cd, asm_replacement_out);
575 M_ALD(REG_ITMP3, REG_PV, disp);
577 M_NOP; /* delay slot */
582 /* emit_verbosecall_enter ******************************************************
584 Generates the code for the call trace.
586 *******************************************************************************/
589 void emit_verbosecall_enter(jitdata *jd)
598 /* get required compiler data */
606 /* mark trace code */
610 M_LDA(REG_SP, REG_SP, -(2 + ARG_CNT + TMP_CNT) * 8);
611 M_AST(REG_RA, REG_SP, 1 * 8);
613 /* save argument registers */
615 for (i = 0; i < INT_ARG_CNT; i++)
616 M_LST(rd->argintregs[i], REG_SP, (2 + i) * 8);
618 for (i = 0; i < FLT_ARG_CNT; i++)
619 M_DST(rd->argfltregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
621 /* save temporary registers for leaf methods */
623 if (jd->isleafmethod) {
624 for (i = 0; i < INT_TMP_CNT; i++)
625 M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
627 for (i = 0; i < FLT_TMP_CNT; i++)
628 M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
631 /* load float arguments into integer registers */
633 for (i = 0; i < md->paramcount && i < INT_ARG_CNT; i++) {
634 t = md->paramtypes[i].type;
636 if (IS_FLT_DBL_TYPE(t)) {
637 if (IS_2_WORD_TYPE(t)) {
638 M_DST(rd->argfltregs[i], REG_SP, 0 * 8);
639 M_LLD(rd->argintregs[i], REG_SP, 0 * 8);
642 M_FST(rd->argfltregs[i], REG_SP, 0 * 8);
643 M_ILD(rd->argintregs[i], REG_SP, 0 * 8);
648 disp = dseg_addaddress(cd, m);
649 M_ALD(REG_ITMP1, REG_PV, disp);
650 M_AST(REG_ITMP1, REG_SP, 0 * 8);
651 disp = dseg_addaddress(cd, builtin_trace_args);
652 M_ALD(REG_ITMP3, REG_PV, disp);
653 M_JSR(REG_RA, REG_ITMP3);
656 /* restore argument registers */
658 for (i = 0; i < INT_ARG_CNT; i++)
659 M_LLD(rd->argintregs[i], REG_SP, (2 + i) * 8);
661 for (i = 0; i < FLT_ARG_CNT; i++)
662 M_DLD(rd->argfltregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
664 /* restore temporary registers for leaf methods */
666 if (jd->isleafmethod) {
667 for (i = 0; i < INT_TMP_CNT; i++)
668 M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
670 for (i = 0; i < FLT_TMP_CNT; i++)
671 M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
674 M_ALD(REG_RA, REG_SP, 1 * 8);
675 M_LDA(REG_SP, REG_SP, (2 + ARG_CNT + TMP_CNT) * 8);
677 /* mark trace code */
681 #endif /* !defined(NDEBUG) */
684 /* emit_verbosecall_exit *******************************************************
686 Generates the code for the call trace.
688 *******************************************************************************/
691 void emit_verbosecall_exit(jitdata *jd)
698 /* get required compiler data */
704 /* mark trace code */
708 M_LDA(REG_SP, REG_SP, -4 * 8); /* keep stack 16-byte aligned */
709 M_LST(REG_RA, REG_SP, 0 * 8);
711 M_LST(REG_RESULT, REG_SP, 1 * 8);
712 M_DST(REG_FRESULT, REG_SP, 2 * 8);
714 disp = dseg_addaddress(cd, m);
715 M_ALD(rd->argintregs[0], REG_PV, disp);
717 M_MOV(REG_RESULT, rd->argintregs[1]);
718 M_DMOV(REG_FRESULT, rd->argfltregs[2]);
719 M_FMOV(REG_FRESULT, rd->argfltregs[3]);
721 disp = dseg_addaddress(cd, builtin_displaymethodstop);
722 M_ALD(REG_ITMP3, REG_PV, disp);
723 M_JSR(REG_RA, REG_ITMP3);
726 M_DLD(REG_FRESULT, REG_SP, 2 * 8);
727 M_LLD(REG_RESULT, REG_SP, 1 * 8);
729 M_LLD(REG_RA, REG_SP, 0 * 8);
730 M_LDA(REG_SP, REG_SP, 4 * 8);
732 /* mark trace code */
736 #endif /* !defined(NDEBUG) */
740 * These are local overrides for various environment variables in Emacs.
741 * Please do not remove this and leave it at the end of the file, where
742 * Emacs will automagically detect them.
743 * ---------------------------------------------------------------------
746 * indent-tabs-mode: t
750 * vim:noexpandtab:sw=4:ts=4: