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-common.h"
54 #include "vm/jit/jit.h"
55 #include "vm/jit/replace.h"
58 /* emit_load *******************************************************************
60 Emits a possible load of an operand.
62 *******************************************************************************/
64 s4 emit_load(jitdata *jd, instruction *iptr, stackptr src, s4 tempreg)
70 /* get required compiler data */
74 if (src->flags & INMEMORY) {
77 disp = src->regoff * 8;
79 if (IS_FLT_DBL_TYPE(src->type)) {
80 if (IS_2_WORD_TYPE(src->type))
81 M_DLD(tempreg, REG_SP, disp);
83 M_FLD(tempreg, REG_SP, disp);
86 M_LLD(tempreg, REG_SP, disp);
97 /* emit_store ******************************************************************
99 Emits a possible store to variable.
101 *******************************************************************************/
103 void emit_store(jitdata *jd, instruction *iptr, stackptr dst, s4 d)
108 /* get required compiler data */
112 if (dst->flags & INMEMORY) {
115 disp = dst->regoff * 8;
117 if (IS_FLT_DBL_TYPE(dst->type)) {
118 if (IS_2_WORD_TYPE(dst->type))
119 M_DST(d, REG_SP, disp);
121 M_FST(d, REG_SP, disp);
124 M_LST(d, REG_SP, disp);
129 /* emit_copy *******************************************************************
131 Generates a register/memory to register/memory copy.
133 *******************************************************************************/
135 void emit_copy(jitdata *jd, instruction *iptr, stackptr src, stackptr dst)
141 /* get required compiler data */
146 if ((src->regoff != dst->regoff) ||
147 ((src->flags ^ dst->flags) & INMEMORY)) {
149 /* If one of the variables resides in memory, we can eliminate
150 the register move from/to the temporary register with the
151 order of getting the destination register and the load. */
153 if (IS_INMEMORY(src->flags)) {
154 d = codegen_reg_of_var(rd, iptr->opc, dst, REG_IFTMP);
155 s1 = emit_load(jd, iptr, src, d);
158 s1 = emit_load(jd, iptr, src, REG_IFTMP);
159 d = codegen_reg_of_var(rd, iptr->opc, dst, s1);
163 if (IS_FLT_DBL_TYPE(src->type)) {
164 if (IS_2_WORD_TYPE(src->type))
173 emit_store(jd, iptr, dst, d);
178 /* emit_iconst *****************************************************************
182 *******************************************************************************/
184 void emit_iconst(codegendata *cd, s4 d, s4 value)
188 if ((value >= -32768) && (value <= 32767))
189 M_IADD_IMM(REG_ZERO, value, d);
190 else if ((value >= 0) && (value <= 0xffff))
191 M_OR_IMM(REG_ZERO, value, d);
193 disp = dseg_adds4(cd, value);
194 M_ILD(d, REG_PV, disp);
199 /* emit_lconst *****************************************************************
203 *******************************************************************************/
205 void emit_lconst(codegendata *cd, s4 d, s8 value)
209 if ((value >= -32768) && (value <= 32767))
210 M_LADD_IMM(REG_ZERO, value, d);
211 else if ((value >= 0) && (value <= 0xffff))
212 M_OR_IMM(REG_ZERO, value, d);
214 disp = dseg_adds8(cd, value);
215 M_LLD(d, REG_PV, disp);
220 /* emit_exception_stubs ********************************************************
222 Generates the code for the exception stubs.
224 *******************************************************************************/
226 void emit_exception_stubs(jitdata *jd)
234 /* get required compiler data */
239 /* generate exception stubs */
243 for (eref = cd->exceptionrefs; eref != NULL; eref = eref->next) {
244 gen_resolvebranch(cd->mcodebase + eref->branchpos,
245 eref->branchpos, cd->mcodeptr - cd->mcodebase);
249 /* Check if the exception is an
250 ArrayIndexOutOfBoundsException. If so, move index register
254 M_MOV(eref->reg, REG_ITMP1);
256 /* calcuate exception address */
258 M_LDA(REG_ITMP2_XPC, REG_PV, eref->branchpos - 4);
260 /* move function to call into REG_ITMP3 */
262 disp = dseg_addaddress(cd, eref->function);
263 M_ALD(REG_ITMP3, REG_PV, disp);
265 if (targetdisp == 0) {
266 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
268 M_MOV(REG_PV, rd->argintregs[0]);
269 M_MOV(REG_SP, rd->argintregs[1]);
271 if (jd->isleafmethod)
272 M_MOV(REG_RA, rd->argintregs[2]);
274 M_ALD(rd->argintregs[2],
275 REG_SP, cd->stackframesize * 8 - SIZEOF_VOID_P);
277 M_MOV(REG_ITMP2_XPC, rd->argintregs[3]);
278 M_MOV(REG_ITMP1, rd->argintregs[4]);
280 M_ASUB_IMM(REG_SP, 2 * 8, REG_SP);
281 M_AST(REG_ITMP2_XPC, REG_SP, 0 * 8);
283 if (jd->isleafmethod)
284 M_AST(REG_RA, REG_SP, 1 * 8);
286 M_JSR(REG_RA, REG_ITMP3);
288 M_MOV(REG_RESULT, REG_ITMP1_XPTR);
290 if (jd->isleafmethod)
291 M_ALD(REG_RA, REG_SP, 1 * 8);
293 M_ALD(REG_ITMP2_XPC, REG_SP, 0 * 8);
294 M_AADD_IMM(REG_SP, 2 * 8, REG_SP);
296 disp = dseg_addaddress(cd, asm_handle_exception);
297 M_ALD(REG_ITMP3, REG_PV, disp);
302 disp = (((u4 *) cd->mcodebase) + targetdisp) -
303 (((u4 *) cd->mcodeptr) + 1);
312 /* emit_patcher_stubs **********************************************************
314 Generates the code for the patcher stubs.
316 *******************************************************************************/
318 void emit_patcher_stubs(jitdata *jd)
328 /* get required compiler data */
332 /* generate code patching stub call code */
336 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
337 /* check code segment size */
341 /* Get machine code which is patched back in later. The
342 call is 2 instruction words long. */
344 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
346 /* We use 2 loads here as an unaligned 8-byte read on 64-bit
347 MIPS causes a SIGSEGV and using the same code for both
348 architectures is much better. */
350 mcode[0] = ((u4 *) tmpmcodeptr)[0];
351 mcode[1] = ((u4 *) tmpmcodeptr)[1];
353 /* Patch in the call to call the following code (done at
356 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
357 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
359 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
361 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff)) {
363 new_internalerror("Jump offset is out of range: %d > +/-%d",
371 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
373 /* create stack frame */
375 M_ASUB_IMM(REG_SP, 6 * 8, REG_SP);
377 /* calculate return address and move it onto the stack */
379 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
380 M_AST(REG_ITMP3, REG_SP, 5 * 8);
382 /* move pointer to java_objectheader onto stack */
384 #if defined(ENABLE_THREADS)
385 /* create a virtual java_objectheader */
387 (void) dseg_addaddress(cd, NULL); /* flcword */
388 (void) dseg_addaddress(cd, lock_get_initial_lock_word());
389 disp = dseg_addaddress(cd, NULL); /* vftbl */
391 M_LDA(REG_ITMP3, REG_PV, disp);
392 M_AST(REG_ITMP3, REG_SP, 4 * 8);
397 /* move machine code onto stack */
399 disp = dseg_adds4(cd, mcode[0]);
400 M_ILD(REG_ITMP3, REG_PV, disp);
401 M_IST(REG_ITMP3, REG_SP, 3 * 8);
403 disp = dseg_adds4(cd, mcode[1]);
404 M_ILD(REG_ITMP3, REG_PV, disp);
405 M_IST(REG_ITMP3, REG_SP, 3 * 8 + 4);
407 /* move class/method/field reference onto stack */
409 disp = dseg_addaddress(cd, pref->ref);
410 M_ALD(REG_ITMP3, REG_PV, disp);
411 M_AST(REG_ITMP3, REG_SP, 2 * 8);
413 /* move data segment displacement onto stack */
415 disp = dseg_adds4(cd, pref->disp);
416 M_ILD(REG_ITMP3, REG_PV, disp);
417 M_IST(REG_ITMP3, REG_SP, 1 * 8);
419 /* move patcher function pointer onto stack */
421 disp = dseg_addaddress(cd, pref->patcher);
422 M_ALD(REG_ITMP3, REG_PV, disp);
423 M_AST(REG_ITMP3, REG_SP, 0 * 8);
425 if (targetdisp == 0) {
426 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
428 disp = dseg_addaddress(cd, asm_patcher_wrapper);
429 M_ALD(REG_ITMP3, REG_PV, disp);
434 disp = (((u4 *) cd->mcodebase) + targetdisp) -
435 (((u4 *) cd->mcodeptr) + 1);
444 /* emit_replacement_stubs ******************************************************
446 Generates the code for the replacement stubs.
448 *******************************************************************************/
450 void emit_replacement_stubs(jitdata *jd)
459 /* get required compiler data */
464 rplp = code->rplpoints;
466 for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
467 /* check code segment size */
471 /* note start of stub code */
473 rplp->outcode = (u1 *) (ptrint) (cd->mcodeptr - cd->mcodebase);
475 /* make machine code for patching */
477 savedmcodeptr = cd->mcodeptr;
478 cd->mcodeptr = (u1 *) &(rplp->mcode);
480 disp = (ptrint) ((s4 *) rplp->outcode - (s4 *) rplp->pc) - 1;
482 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff)) {
484 new_internalerror("Jump offset is out of range: %d > +/-%d",
490 M_NOP; /* delay slot */
492 cd->mcodeptr = savedmcodeptr;
494 /* create stack frame - 16-byte aligned */
496 M_ASUB_IMM(REG_SP, 2 * 8, REG_SP);
498 /* push address of `rplpoint` struct */
500 disp = dseg_addaddress(cd, rplp);
501 M_ALD(REG_ITMP3, REG_PV, disp);
502 M_AST(REG_ITMP3, REG_SP, 0 * 8);
504 /* jump to replacement function */
506 disp = dseg_addaddress(cd, asm_replacement_out);
507 M_ALD(REG_ITMP3, REG_PV, disp);
509 M_NOP; /* delay slot */
514 /* emit_verbosecall_enter ******************************************************
516 Generates the code for the call trace.
518 *******************************************************************************/
521 void emit_verbosecall_enter(jitdata *jd)
530 /* get required compiler data */
538 /* mark trace code */
542 M_LDA(REG_SP, REG_SP, -(2 + ARG_CNT + TMP_CNT) * 8);
543 M_AST(REG_RA, REG_SP, 1 * 8);
545 /* save argument registers */
547 for (i = 0; i < INT_ARG_CNT; i++)
548 M_LST(rd->argintregs[i], REG_SP, (2 + i) * 8);
550 for (i = 0; i < FLT_ARG_CNT; i++)
551 M_DST(rd->argfltregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
553 /* save temporary registers for leaf methods */
555 if (jd->isleafmethod) {
556 for (i = 0; i < INT_TMP_CNT; i++)
557 M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
559 for (i = 0; i < FLT_TMP_CNT; i++)
560 M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
563 /* load float arguments into integer registers */
565 for (i = 0; i < md->paramcount && i < INT_ARG_CNT; i++) {
566 t = md->paramtypes[i].type;
568 if (IS_FLT_DBL_TYPE(t)) {
569 if (IS_2_WORD_TYPE(t)) {
570 M_DST(rd->argfltregs[i], REG_SP, 0 * 8);
571 M_LLD(rd->argintregs[i], REG_SP, 0 * 8);
574 M_FST(rd->argfltregs[i], REG_SP, 0 * 8);
575 M_ILD(rd->argintregs[i], REG_SP, 0 * 8);
580 disp = dseg_addaddress(cd, m);
581 M_ALD(REG_ITMP1, REG_PV, disp);
582 M_AST(REG_ITMP1, REG_SP, 0 * 8);
583 disp = dseg_addaddress(cd, builtin_trace_args);
584 M_ALD(REG_ITMP3, REG_PV, disp);
585 M_JSR(REG_RA, REG_ITMP3);
588 /* restore argument registers */
590 for (i = 0; i < INT_ARG_CNT; i++)
591 M_LLD(rd->argintregs[i], REG_SP, (2 + i) * 8);
593 for (i = 0; i < FLT_ARG_CNT; i++)
594 M_DLD(rd->argfltregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
596 /* restore temporary registers for leaf methods */
598 if (jd->isleafmethod) {
599 for (i = 0; i < INT_TMP_CNT; i++)
600 M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
602 for (i = 0; i < FLT_TMP_CNT; i++)
603 M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
606 M_ALD(REG_RA, REG_SP, 1 * 8);
607 M_LDA(REG_SP, REG_SP, (2 + ARG_CNT + TMP_CNT) * 8);
609 /* mark trace code */
613 #endif /* !defined(NDEBUG) */
616 /* emit_verbosecall_exit *******************************************************
618 Generates the code for the call trace.
620 *******************************************************************************/
623 void emit_verbosecall_exit(jitdata *jd)
630 /* get required compiler data */
636 /* mark trace code */
640 M_LDA(REG_SP, REG_SP, -4 * 8); /* keep stack 16-byte aligned */
641 M_LST(REG_RA, REG_SP, 0 * 8);
643 M_LST(REG_RESULT, REG_SP, 1 * 8);
644 M_DST(REG_FRESULT, REG_SP, 2 * 8);
646 disp = dseg_addaddress(cd, m);
647 M_ALD(rd->argintregs[0], REG_PV, disp);
649 M_MOV(REG_RESULT, rd->argintregs[1]);
650 M_DMOV(REG_FRESULT, rd->argfltregs[2]);
651 M_FMOV(REG_FRESULT, rd->argfltregs[3]);
653 disp = dseg_addaddress(cd, builtin_displaymethodstop);
654 M_ALD(REG_ITMP3, REG_PV, disp);
655 M_JSR(REG_RA, REG_ITMP3);
658 M_DLD(REG_FRESULT, REG_SP, 2 * 8);
659 M_LLD(REG_RESULT, REG_SP, 1 * 8);
661 M_LLD(REG_RA, REG_SP, 0 * 8);
662 M_LDA(REG_SP, REG_SP, 4 * 8);
664 /* mark trace code */
668 #endif /* !defined(NDEBUG) */
672 * These are local overrides for various environment variables in Emacs.
673 * Please do not remove this and leave it at the end of the file, where
674 * Emacs will automagically detect them.
675 * ---------------------------------------------------------------------
678 * indent-tabs-mode: t
682 * vim:noexpandtab:sw=4:ts=4: