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/asmpart.h"
51 #include "vm/jit/dseg.h"
52 #include "vm/jit/emit.h"
53 #include "vm/jit/jit.h"
54 #include "vm/jit/replace.h"
57 /* code generation functions **************************************************/
59 /* emit_load_s1 ****************************************************************
61 Emits a possible load of the first source operand.
63 *******************************************************************************/
65 s4 emit_load_s1(jitdata *jd, instruction *iptr, stackptr src, s4 tempreg)
71 /* get required compiler data */
75 if (src->flags & INMEMORY) {
78 disp = src->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);
86 M_LLD(tempreg, REG_SP, disp);
96 /* emit_load_s2 ****************************************************************
98 Emits a possible load of the second source operand.
100 *******************************************************************************/
102 s4 emit_load_s2(jitdata *jd, instruction *iptr, stackptr src, s4 tempreg)
108 /* get required compiler data */
112 if (src->flags & INMEMORY) {
115 disp = src->regoff * 8;
117 if (IS_FLT_DBL_TYPE(src->type)) {
118 if (IS_2_WORD_TYPE(src->type))
119 M_DLD(tempreg, REG_SP, disp);
121 M_FLD(tempreg, REG_SP, disp);
123 M_LLD(tempreg, REG_SP, disp);
133 /* emit_load_s3 ****************************************************************
135 Emits a possible load of the third source operand.
137 *******************************************************************************/
139 s4 emit_load_s3(jitdata *jd, instruction *iptr, stackptr src, s4 tempreg)
145 /* get required compiler data */
149 if (src->flags & INMEMORY) {
152 disp = src->regoff * 8;
154 if (IS_FLT_DBL_TYPE(src->type)) {
155 if (IS_2_WORD_TYPE(src->type))
156 M_DLD(tempreg, REG_SP, disp);
158 M_FLD(tempreg, REG_SP, disp);
160 M_LLD(tempreg, REG_SP, disp);
170 /* emit_store ******************************************************************
174 *******************************************************************************/
176 void emit_store(jitdata *jd, instruction *iptr, stackptr dst, s4 d)
181 /* get required compiler data */
185 if (dst->flags & INMEMORY) {
188 disp = dst->regoff * 8;
190 if (IS_FLT_DBL_TYPE(dst->type)) {
191 if (IS_2_WORD_TYPE(dst->type))
192 M_DST(d, REG_SP, disp);
194 M_FST(d, REG_SP, disp);
196 M_LST(d, REG_SP, disp);
201 /* emit_copy *******************************************************************
205 *******************************************************************************/
207 void emit_copy(jitdata *jd, instruction *iptr, stackptr src, stackptr dst)
213 /* get required compiler data */
218 if ((src->regoff != dst->regoff) ||
219 ((src->flags ^ dst->flags) & INMEMORY)) {
220 d = codegen_reg_of_var(rd, iptr->opc, dst, REG_IFTMP);
221 s1 = emit_load_s1(jd, iptr, src, d);
224 if (IS_FLT_DBL_TYPE(src->type)) {
225 if (IS_2_WORD_TYPE(src->type))
233 emit_store(jd, iptr, dst, d);
238 /* emit_iconst *****************************************************************
242 *******************************************************************************/
244 void emit_iconst(codegendata *cd, s4 d, s4 value)
248 if ((value >= -32768) && (value <= 32767))
249 M_IADD_IMM(REG_ZERO, value, d);
250 else if ((value >= 0) && (value <= 0xffff))
251 M_OR_IMM(REG_ZERO, value, d);
253 disp = dseg_adds4(cd, value);
254 M_ILD(d, REG_PV, disp);
259 /* emit_lconst *****************************************************************
263 *******************************************************************************/
265 void emit_lconst(codegendata *cd, s4 d, s8 value)
269 if ((value >= -32768) && (value <= 32767))
270 M_LADD_IMM(REG_ZERO, value, d);
271 else if ((value >= 0) && (value <= 0xffff))
272 M_OR_IMM(REG_ZERO, value, d);
274 disp = dseg_adds8(cd, value);
275 M_LLD(d, REG_PV, disp);
280 /* emit_exception_stubs ********************************************************
282 Generates the code for the exception stubs.
284 *******************************************************************************/
286 void emit_exception_stubs(jitdata *jd)
294 /* get required compiler data */
299 /* generate exception stubs */
303 for (eref = cd->exceptionrefs; eref != NULL; eref = eref->next) {
304 gen_resolvebranch(cd->mcodebase + eref->branchpos,
305 eref->branchpos, cd->mcodeptr - cd->mcodebase);
309 /* Check if the exception is an
310 ArrayIndexOutOfBoundsException. If so, move index register
314 M_MOV(eref->reg, REG_ITMP1);
316 /* calcuate exception address */
318 M_LDA(REG_ITMP2_XPC, REG_PV, eref->branchpos - 4);
320 /* move function to call into REG_ITMP3 */
322 disp = dseg_addaddress(cd, eref->function);
323 M_ALD(REG_ITMP3, REG_PV, disp);
325 if (targetdisp == 0) {
326 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
328 M_MOV(REG_PV, rd->argintregs[0]);
329 M_MOV(REG_SP, rd->argintregs[1]);
331 if (jd->isleafmethod)
332 M_MOV(REG_RA, rd->argintregs[2]);
334 M_ALD(rd->argintregs[2],
335 REG_SP, cd->stackframesize * 8 - SIZEOF_VOID_P);
337 M_MOV(REG_ITMP2_XPC, rd->argintregs[3]);
338 M_MOV(REG_ITMP1, rd->argintregs[4]);
340 M_ASUB_IMM(REG_SP, 2 * 8, REG_SP);
341 M_AST(REG_ITMP2_XPC, REG_SP, 0 * 8);
343 if (jd->isleafmethod)
344 M_AST(REG_RA, REG_SP, 1 * 8);
346 M_JSR(REG_RA, REG_ITMP3);
348 M_MOV(REG_RESULT, REG_ITMP1_XPTR);
350 if (jd->isleafmethod)
351 M_ALD(REG_RA, REG_SP, 1 * 8);
353 M_ALD(REG_ITMP2_XPC, REG_SP, 0 * 8);
354 M_AADD_IMM(REG_SP, 2 * 8, REG_SP);
356 disp = dseg_addaddress(cd, asm_handle_exception);
357 M_ALD(REG_ITMP3, REG_PV, disp);
362 disp = (((u4 *) cd->mcodebase) + targetdisp) -
363 (((u4 *) cd->mcodeptr) + 1);
371 /* emit_patcher_stubs **********************************************************
373 Generates the code for the patcher stubs.
375 *******************************************************************************/
377 void emit_patcher_stubs(jitdata *jd)
387 /* get required compiler data */
391 /* generate code patching stub call code */
395 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
396 /* check code segment size */
400 /* Get machine code which is patched back in later. The
401 call is 2 instruction words long. */
403 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
405 /* We need to split this, because an unaligned 8 byte read
408 mcode = ((u8) ((u4 *) tmpmcodeptr)[1] << 32) +
409 ((u4 *) tmpmcodeptr)[0];
411 /* Patch in the call to call the following code (done at
414 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
415 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
417 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
419 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff)) {
421 new_internalerror("Jump offset is out of range: %d > +/-%d",
429 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
431 /* create stack frame */
433 M_ASUB_IMM(REG_SP, 6 * 8, REG_SP);
435 /* calculate return address and move it onto the stack */
437 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
438 M_AST(REG_ITMP3, REG_SP, 5 * 8);
440 /* move pointer to java_objectheader onto stack */
442 #if defined(ENABLE_THREADS)
443 /* create a virtual java_objectheader */
445 (void) dseg_addaddress(cd, NULL); /* flcword */
446 (void) dseg_addaddress(cd, lock_get_initial_lock_word());
447 disp = dseg_addaddress(cd, NULL); /* vftbl */
449 M_LDA(REG_ITMP3, REG_PV, disp);
450 M_AST(REG_ITMP3, REG_SP, 4 * 8);
455 /* move machine code onto stack */
457 disp = dseg_adds8(cd, mcode);
458 M_LLD(REG_ITMP3, REG_PV, disp);
459 M_LST(REG_ITMP3, REG_SP, 3 * 8);
461 /* move class/method/field reference onto stack */
463 disp = dseg_addaddress(cd, pref->ref);
464 M_ALD(REG_ITMP3, REG_PV, disp);
465 M_AST(REG_ITMP3, REG_SP, 2 * 8);
467 /* move data segment displacement onto stack */
469 disp = dseg_adds4(cd, pref->disp);
470 M_ILD(REG_ITMP3, REG_PV, disp);
471 M_IST(REG_ITMP3, REG_SP, 1 * 8);
473 /* move patcher function pointer onto stack */
475 disp = dseg_addaddress(cd, pref->patcher);
476 M_ALD(REG_ITMP3, REG_PV, disp);
477 M_AST(REG_ITMP3, REG_SP, 0 * 8);
479 if (targetdisp == 0) {
480 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
482 disp = dseg_addaddress(cd, asm_patcher_wrapper);
483 M_ALD(REG_ITMP3, REG_PV, disp);
488 disp = (((u4 *) cd->mcodebase) + targetdisp) -
489 (((u4 *) cd->mcodeptr) + 1);
497 /* emit_replacement_stubs ******************************************************
499 Generates the code for the replacement stubs.
501 *******************************************************************************/
503 void emit_replacement_stubs(jitdata *jd)
512 /* get required compiler data */
517 rplp = code->rplpoints;
519 for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
520 /* check code segment size */
524 /* note start of stub code */
526 rplp->outcode = (u1 *) (ptrint) (cd->mcodeptr - cd->mcodebase);
528 /* make machine code for patching */
530 savedmcodeptr = cd->mcodeptr;
531 cd->mcodeptr = (u1 *) &(rplp->mcode);
533 disp = (ptrint) ((s4 *) rplp->outcode - (s4 *) rplp->pc) - 1;
535 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff)) {
537 new_internalerror("Jump offset is out of range: %d > +/-%d",
543 M_NOP; /* delay slot */
545 cd->mcodeptr = savedmcodeptr;
547 /* create stack frame - 16-byte aligned */
549 M_ASUB_IMM(REG_SP, 2 * 8, REG_SP);
551 /* push address of `rplpoint` struct */
553 disp = dseg_addaddress(cd, rplp);
554 M_ALD(REG_ITMP3, REG_PV, disp);
555 M_AST(REG_ITMP3, REG_SP, 0 * 8);
557 /* jump to replacement function */
559 disp = dseg_addaddress(cd, asm_replacement_out);
560 M_ALD(REG_ITMP3, REG_PV, disp);
562 M_NOP; /* delay slot */
568 * These are local overrides for various environment variables in Emacs.
569 * Please do not remove this and leave it at the end of the file, where
570 * Emacs will automagically detect them.
571 * ---------------------------------------------------------------------
574 * indent-tabs-mode: t
578 * vim:noexpandtab:sw=4:ts=4: