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_s1 ****************************************************************
62 Emits a possible load of the first source operand.
64 *******************************************************************************/
66 s4 emit_load_s1(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);
97 /* emit_load_s2 ****************************************************************
99 Emits a possible load of the second source operand.
101 *******************************************************************************/
103 s4 emit_load_s2(jitdata *jd, instruction *iptr, stackptr src, s4 tempreg)
109 /* get required compiler data */
113 if (src->flags & INMEMORY) {
116 disp = src->regoff * 8;
118 if (IS_FLT_DBL_TYPE(src->type)) {
119 if (IS_2_WORD_TYPE(src->type))
120 M_DLD(tempreg, REG_SP, disp);
122 M_FLD(tempreg, REG_SP, disp);
124 M_LLD(tempreg, REG_SP, disp);
134 /* emit_load_s3 ****************************************************************
136 Emits a possible load of the third source operand.
138 *******************************************************************************/
140 s4 emit_load_s3(jitdata *jd, instruction *iptr, stackptr src, s4 tempreg)
146 /* get required compiler data */
150 if (src->flags & INMEMORY) {
153 disp = src->regoff * 8;
155 if (IS_FLT_DBL_TYPE(src->type)) {
156 if (IS_2_WORD_TYPE(src->type))
157 M_DLD(tempreg, REG_SP, disp);
159 M_FLD(tempreg, REG_SP, disp);
161 M_LLD(tempreg, REG_SP, disp);
171 /* emit_store ******************************************************************
175 *******************************************************************************/
177 void emit_store(jitdata *jd, instruction *iptr, stackptr dst, s4 d)
182 /* get required compiler data */
186 if (dst->flags & INMEMORY) {
189 disp = dst->regoff * 8;
191 if (IS_FLT_DBL_TYPE(dst->type)) {
192 if (IS_2_WORD_TYPE(dst->type))
193 M_DST(d, REG_SP, disp);
195 M_FST(d, REG_SP, disp);
197 M_LST(d, REG_SP, disp);
202 /* emit_copy *******************************************************************
206 *******************************************************************************/
208 void emit_copy(jitdata *jd, instruction *iptr, stackptr src, stackptr dst)
214 /* get required compiler data */
219 if ((src->regoff != dst->regoff) ||
220 ((src->flags ^ dst->flags) & INMEMORY)) {
221 d = codegen_reg_of_var(rd, iptr->opc, dst, REG_IFTMP);
222 s1 = emit_load_s1(jd, iptr, src, d);
225 if (IS_FLT_DBL_TYPE(src->type)) {
226 if (IS_2_WORD_TYPE(src->type))
234 emit_store(jd, iptr, dst, d);
239 /* emit_iconst *****************************************************************
243 *******************************************************************************/
245 void emit_iconst(codegendata *cd, s4 d, s4 value)
249 if ((value >= -32768) && (value <= 32767))
250 M_IADD_IMM(REG_ZERO, value, d);
251 else if ((value >= 0) && (value <= 0xffff))
252 M_OR_IMM(REG_ZERO, value, d);
254 disp = dseg_adds4(cd, value);
255 M_ILD(d, REG_PV, disp);
260 /* emit_lconst *****************************************************************
264 *******************************************************************************/
266 void emit_lconst(codegendata *cd, s4 d, s8 value)
270 if ((value >= -32768) && (value <= 32767))
271 M_LADD_IMM(REG_ZERO, value, d);
272 else if ((value >= 0) && (value <= 0xffff))
273 M_OR_IMM(REG_ZERO, value, d);
275 disp = dseg_adds8(cd, value);
276 M_LLD(d, REG_PV, disp);
281 /* emit_exception_stubs ********************************************************
283 Generates the code for the exception stubs.
285 *******************************************************************************/
287 void emit_exception_stubs(jitdata *jd)
295 /* get required compiler data */
300 /* generate exception stubs */
304 for (eref = cd->exceptionrefs; eref != NULL; eref = eref->next) {
305 gen_resolvebranch(cd->mcodebase + eref->branchpos,
306 eref->branchpos, cd->mcodeptr - cd->mcodebase);
310 /* Check if the exception is an
311 ArrayIndexOutOfBoundsException. If so, move index register
315 M_MOV(eref->reg, REG_ITMP1);
317 /* calcuate exception address */
319 M_LDA(REG_ITMP2_XPC, REG_PV, eref->branchpos - 4);
321 /* move function to call into REG_ITMP3 */
323 disp = dseg_addaddress(cd, eref->function);
324 M_ALD(REG_ITMP3, REG_PV, disp);
326 if (targetdisp == 0) {
327 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
329 M_MOV(REG_PV, rd->argintregs[0]);
330 M_MOV(REG_SP, rd->argintregs[1]);
332 if (jd->isleafmethod)
333 M_MOV(REG_RA, rd->argintregs[2]);
335 M_ALD(rd->argintregs[2],
336 REG_SP, cd->stackframesize * 8 - SIZEOF_VOID_P);
338 M_MOV(REG_ITMP2_XPC, rd->argintregs[3]);
339 M_MOV(REG_ITMP1, rd->argintregs[4]);
341 M_ASUB_IMM(REG_SP, 2 * 8, REG_SP);
342 M_AST(REG_ITMP2_XPC, REG_SP, 0 * 8);
344 if (jd->isleafmethod)
345 M_AST(REG_RA, REG_SP, 1 * 8);
347 M_JSR(REG_RA, REG_ITMP3);
349 M_MOV(REG_RESULT, REG_ITMP1_XPTR);
351 if (jd->isleafmethod)
352 M_ALD(REG_RA, REG_SP, 1 * 8);
354 M_ALD(REG_ITMP2_XPC, REG_SP, 0 * 8);
355 M_AADD_IMM(REG_SP, 2 * 8, REG_SP);
357 disp = dseg_addaddress(cd, asm_handle_exception);
358 M_ALD(REG_ITMP3, REG_PV, disp);
363 disp = (((u4 *) cd->mcodebase) + targetdisp) -
364 (((u4 *) cd->mcodeptr) + 1);
373 /* emit_patcher_stubs **********************************************************
375 Generates the code for the patcher stubs.
377 *******************************************************************************/
379 void emit_patcher_stubs(jitdata *jd)
389 /* get required compiler data */
393 /* generate code patching stub call code */
397 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
398 /* check code segment size */
402 /* Get machine code which is patched back in later. The
403 call is 2 instruction words long. */
405 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
407 /* We use 2 loads here as an unaligned 8-byte read on 64-bit
408 MIPS causes a SIGSEGV and using the same code for both
409 architectures is much better. */
411 mcode[0] = ((u4 *) tmpmcodeptr)[0];
412 mcode[1] = ((u4 *) tmpmcodeptr)[1];
414 /* Patch in the call to call the following code (done at
417 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
418 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
420 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
422 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff)) {
424 new_internalerror("Jump offset is out of range: %d > +/-%d",
432 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
434 /* create stack frame */
436 M_ASUB_IMM(REG_SP, 6 * 8, REG_SP);
438 /* calculate return address and move it onto the stack */
440 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
441 M_AST(REG_ITMP3, REG_SP, 5 * 8);
443 /* move pointer to java_objectheader onto stack */
445 #if defined(ENABLE_THREADS)
446 /* create a virtual java_objectheader */
448 (void) dseg_addaddress(cd, NULL); /* flcword */
449 (void) dseg_addaddress(cd, lock_get_initial_lock_word());
450 disp = dseg_addaddress(cd, NULL); /* vftbl */
452 M_LDA(REG_ITMP3, REG_PV, disp);
453 M_AST(REG_ITMP3, REG_SP, 4 * 8);
458 /* move machine code onto stack */
460 disp = dseg_adds4(cd, mcode[0]);
461 M_ILD(REG_ITMP3, REG_PV, disp);
462 M_IST(REG_ITMP3, REG_SP, 3 * 8);
464 disp = dseg_adds4(cd, mcode[1]);
465 M_ILD(REG_ITMP3, REG_PV, disp);
466 M_IST(REG_ITMP3, REG_SP, 3 * 8 + 4);
468 /* move class/method/field reference onto stack */
470 disp = dseg_addaddress(cd, pref->ref);
471 M_ALD(REG_ITMP3, REG_PV, disp);
472 M_AST(REG_ITMP3, REG_SP, 2 * 8);
474 /* move data segment displacement onto stack */
476 disp = dseg_adds4(cd, pref->disp);
477 M_ILD(REG_ITMP3, REG_PV, disp);
478 M_IST(REG_ITMP3, REG_SP, 1 * 8);
480 /* move patcher function pointer onto stack */
482 disp = dseg_addaddress(cd, pref->patcher);
483 M_ALD(REG_ITMP3, REG_PV, disp);
484 M_AST(REG_ITMP3, REG_SP, 0 * 8);
486 if (targetdisp == 0) {
487 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
489 disp = dseg_addaddress(cd, asm_patcher_wrapper);
490 M_ALD(REG_ITMP3, REG_PV, disp);
495 disp = (((u4 *) cd->mcodebase) + targetdisp) -
496 (((u4 *) cd->mcodeptr) + 1);
505 /* emit_replacement_stubs ******************************************************
507 Generates the code for the replacement stubs.
509 *******************************************************************************/
511 void emit_replacement_stubs(jitdata *jd)
520 /* get required compiler data */
525 rplp = code->rplpoints;
527 for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
528 /* check code segment size */
532 /* note start of stub code */
534 rplp->outcode = (u1 *) (ptrint) (cd->mcodeptr - cd->mcodebase);
536 /* make machine code for patching */
538 savedmcodeptr = cd->mcodeptr;
539 cd->mcodeptr = (u1 *) &(rplp->mcode);
541 disp = (ptrint) ((s4 *) rplp->outcode - (s4 *) rplp->pc) - 1;
543 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff)) {
545 new_internalerror("Jump offset is out of range: %d > +/-%d",
551 M_NOP; /* delay slot */
553 cd->mcodeptr = savedmcodeptr;
555 /* create stack frame - 16-byte aligned */
557 M_ASUB_IMM(REG_SP, 2 * 8, REG_SP);
559 /* push address of `rplpoint` struct */
561 disp = dseg_addaddress(cd, rplp);
562 M_ALD(REG_ITMP3, REG_PV, disp);
563 M_AST(REG_ITMP3, REG_SP, 0 * 8);
565 /* jump to replacement function */
567 disp = dseg_addaddress(cd, asm_replacement_out);
568 M_ALD(REG_ITMP3, REG_PV, disp);
570 M_NOP; /* delay slot */
575 /* emit_verbosecall_enter ******************************************************
577 Generates the code for the call trace.
579 *******************************************************************************/
582 void emit_verbosecall_enter(jitdata *jd)
591 /* get required compiler data */
599 /* mark trace code */
603 M_LDA(REG_SP, REG_SP, -(2 + ARG_CNT + TMP_CNT) * 8);
604 M_AST(REG_RA, REG_SP, 1 * 8);
606 /* save argument registers */
608 for (i = 0; i < INT_ARG_CNT; i++)
609 M_LST(rd->argintregs[i], REG_SP, (2 + i) * 8);
611 for (i = 0; i < FLT_ARG_CNT; i++)
612 M_DST(rd->argfltregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
614 /* save temporary registers for leaf methods */
616 if (jd->isleafmethod) {
617 for (i = 0; i < INT_TMP_CNT; i++)
618 M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
620 for (i = 0; i < FLT_TMP_CNT; i++)
621 M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
624 /* load float arguments into integer registers */
626 for (i = 0; i < md->paramcount && i < INT_ARG_CNT; i++) {
627 t = md->paramtypes[i].type;
629 if (IS_FLT_DBL_TYPE(t)) {
630 if (IS_2_WORD_TYPE(t)) {
631 M_DST(rd->argfltregs[i], REG_SP, 0 * 8);
632 M_LLD(rd->argintregs[i], REG_SP, 0 * 8);
635 M_FST(rd->argfltregs[i], REG_SP, 0 * 8);
636 M_ILD(rd->argintregs[i], REG_SP, 0 * 8);
641 disp = dseg_addaddress(cd, m);
642 M_ALD(REG_ITMP1, REG_PV, disp);
643 M_AST(REG_ITMP1, REG_SP, 0 * 8);
644 disp = dseg_addaddress(cd, builtin_trace_args);
645 M_ALD(REG_ITMP3, REG_PV, disp);
646 M_JSR(REG_RA, REG_ITMP3);
649 /* restore argument registers */
651 for (i = 0; i < INT_ARG_CNT; i++)
652 M_LLD(rd->argintregs[i], REG_SP, (2 + i) * 8);
654 for (i = 0; i < FLT_ARG_CNT; i++)
655 M_DLD(rd->argfltregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
657 /* restore temporary registers for leaf methods */
659 if (jd->isleafmethod) {
660 for (i = 0; i < INT_TMP_CNT; i++)
661 M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
663 for (i = 0; i < FLT_TMP_CNT; i++)
664 M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
667 M_ALD(REG_RA, REG_SP, 1 * 8);
668 M_LDA(REG_SP, REG_SP, (2 + ARG_CNT + TMP_CNT) * 8);
670 /* mark trace code */
674 #endif /* !defined(NDEBUG) */
677 /* emit_verbosecall_exit *******************************************************
679 Generates the code for the call trace.
681 *******************************************************************************/
684 void emit_verbosecall_exit(jitdata *jd)
691 /* get required compiler data */
697 /* mark trace code */
701 M_LDA(REG_SP, REG_SP, -4 * 8); /* keep stack 16-byte aligned */
702 M_LST(REG_RA, REG_SP, 0 * 8);
704 M_LST(REG_RESULT, REG_SP, 1 * 8);
705 M_DST(REG_FRESULT, REG_SP, 2 * 8);
707 disp = dseg_addaddress(cd, m);
708 M_ALD(rd->argintregs[0], REG_PV, disp);
710 M_MOV(REG_RESULT, rd->argintregs[1]);
711 M_DMOV(REG_FRESULT, rd->argfltregs[2]);
712 M_FMOV(REG_FRESULT, rd->argfltregs[3]);
714 disp = dseg_addaddress(cd, builtin_displaymethodstop);
715 M_ALD(REG_ITMP3, REG_PV, disp);
716 M_JSR(REG_RA, REG_ITMP3);
719 M_DLD(REG_FRESULT, REG_SP, 2 * 8);
720 M_LLD(REG_RESULT, REG_SP, 1 * 8);
722 M_LLD(REG_RA, REG_SP, 0 * 8);
723 M_LDA(REG_SP, REG_SP, 4 * 8);
725 /* mark trace code */
729 #endif /* !defined(NDEBUG) */
733 * These are local overrides for various environment variables in Emacs.
734 * Please do not remove this and leave it at the end of the file, where
735 * Emacs will automagically detect them.
736 * ---------------------------------------------------------------------
739 * indent-tabs-mode: t
743 * vim:noexpandtab:sw=4:ts=4: