1 /* src/vm/jit/alpha/emit.c - Alpha 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: emit.c 4398 2006-01-31 23:43:08Z twisti $
41 #include "vm/jit/alpha/codegen.h"
43 #if defined(ENABLE_THREADS)
44 # include "threads/native/lock.h"
47 #include "vm/builtin.h"
48 #include "vm/jit/abi-asm.h"
49 #include "vm/jit/asmpart.h"
50 #include "vm/jit/dseg.h"
51 #include "vm/jit/emit.h"
52 #include "vm/jit/jit.h"
53 #include "vm/jit/replace.h"
56 /* code generation functions **************************************************/
58 /* emit_load *******************************************************************
60 Emits a possible load of an operand.
62 *******************************************************************************/
64 s4 emit_load(jitdata *jd, instruction *iptr, stackptr src, s4 tempreg)
69 /* get required compiler data */
73 if (src->flags & INMEMORY) {
76 if (IS_FLT_DBL_TYPE(src->type))
77 M_DLD(tempreg, REG_SP, src->regoff * 8);
79 M_LLD(tempreg, REG_SP, src->regoff * 8);
90 /* emit_load_s1 ****************************************************************
92 Emits a possible load of the first source operand.
94 *******************************************************************************/
96 s4 emit_load_s1(jitdata *jd, instruction *iptr, s4 tempreg)
100 r = emit_load(jd, iptr, iptr->s1.var, tempreg);
106 /* emit_load_s2 ****************************************************************
108 Emits a possible load of the second source operand.
110 *******************************************************************************/
112 s4 emit_load_s2(jitdata *jd, instruction *iptr, s4 tempreg)
116 r = emit_load(jd, iptr, iptr->sx.s23.s2.var, tempreg);
122 /* emit_load_s3 ****************************************************************
124 Emits a possible load of the third source operand.
126 *******************************************************************************/
128 s4 emit_load_s3(jitdata *jd, instruction *iptr, s4 tempreg)
132 r = emit_load(jd, iptr, iptr->sx.s23.s3.var, tempreg);
138 /* emit_store ******************************************************************
140 Emit a possible store for the given variable.
142 *******************************************************************************/
144 void emit_store(jitdata *jd, instruction *iptr, stackptr dst, s4 d)
148 /* get required compiler data */
152 if (dst->flags & INMEMORY) {
155 if (IS_FLT_DBL_TYPE(dst->type))
156 M_DST(d, REG_SP, dst->regoff * 8);
158 M_LST(d, REG_SP, dst->regoff * 8);
163 /* emit_store_dst **************************************************************
165 Emit a possible store for the destination operand.
167 *******************************************************************************/
169 void emit_store_dst(jitdata *jd, instruction *iptr, s4 d)
171 emit_store(jd, iptr, iptr->dst.var, d);
175 /* emit_copy *******************************************************************
177 Generates a register/memory to register/memory copy.
179 *******************************************************************************/
181 void emit_copy(jitdata *jd, instruction *iptr, stackptr src, stackptr dst)
187 /* get required compiler data */
192 if ((src->regoff != dst->regoff) ||
193 ((src->flags ^ dst->flags) & INMEMORY)) {
195 /* If one of the variables resides in memory, we can eliminate
196 the register move from/to the temporary register with the
197 order of getting the destination register and the load. */
199 if (IS_INMEMORY(src->flags)) {
200 d = codegen_reg_of_var(rd, iptr->opc, dst, REG_IFTMP);
201 s1 = emit_load(jd, iptr, src, d);
204 s1 = emit_load(jd, iptr, src, REG_IFTMP);
205 d = codegen_reg_of_var(rd, iptr->opc, dst, s1);
208 if (IS_FLT_DBL_TYPE(src->type))
213 emit_store(jd, iptr, dst, d);
218 /* emit_iconst *****************************************************************
222 *******************************************************************************/
224 void emit_iconst(codegendata *cd, s4 d, s4 value)
228 if ((value >= -32768) && (value <= 32767))
229 M_LDA_INTERN(d, REG_ZERO, value);
231 disp = dseg_adds4(cd, value);
232 M_ILD(d, REG_PV, disp);
237 /* emit_lconst *****************************************************************
241 *******************************************************************************/
243 void emit_lconst(codegendata *cd, s4 d, s8 value)
247 if ((value >= -32768) && (value <= 32767))
248 M_LDA_INTERN(d, REG_ZERO, value);
250 disp = dseg_adds8(cd, value);
251 M_LLD(d, REG_PV, disp);
256 /* emit_exception_stubs ********************************************************
258 Generates the code for the exception stubs.
260 *******************************************************************************/
262 void emit_exception_stubs(jitdata *jd)
270 /* get required compiler data */
275 /* generate exception stubs */
279 for (eref = cd->exceptionrefs; eref != NULL; eref = eref->next) {
280 gen_resolvebranch(cd->mcodebase + eref->branchpos,
281 eref->branchpos, cd->mcodeptr - cd->mcodebase);
285 /* move index register into REG_ITMP1 */
287 /* Check if the exception is an
288 ArrayIndexOutOfBoundsException. If so, move index register
292 M_MOV(eref->reg, rd->argintregs[4]);
294 /* calcuate exception address */
296 M_LDA(rd->argintregs[3], REG_PV, eref->branchpos - 4);
298 /* move function to call into REG_ITMP3 */
300 disp = dseg_add_functionptr(cd, eref->function);
301 M_ALD(REG_ITMP3, REG_PV, disp);
303 if (targetdisp == 0) {
304 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
306 M_MOV(REG_PV, rd->argintregs[0]);
307 M_MOV(REG_SP, rd->argintregs[1]);
309 if (jd->isleafmethod)
310 M_MOV(REG_RA, rd->argintregs[2]);
312 M_ALD(rd->argintregs[2],
313 REG_SP, cd->stackframesize * 8 - SIZEOF_VOID_P);
315 M_LDA(REG_SP, REG_SP, -2 * 8);
316 M_AST(rd->argintregs[3], REG_SP, 0 * 8); /* store XPC */
318 if (jd->isleafmethod)
319 M_AST(REG_RA, REG_SP, 1 * 8);
321 M_MOV(REG_ITMP3, REG_PV);
322 M_JSR(REG_RA, REG_PV);
323 disp = (s4) (cd->mcodeptr - cd->mcodebase);
324 M_LDA(REG_PV, REG_RA, -disp);
326 M_MOV(REG_RESULT, REG_ITMP1_XPTR);
328 if (jd->isleafmethod)
329 M_ALD(REG_RA, REG_SP, 1 * 8);
331 M_ALD(REG_ITMP2_XPC, REG_SP, 0 * 8);
332 M_LDA(REG_SP, REG_SP, 2 * 8);
334 disp = dseg_add_functionptr(cd, asm_handle_exception);
335 M_ALD(REG_ITMP3, REG_PV, disp);
336 M_JMP(REG_ZERO, REG_ITMP3);
339 disp = (((u4 *) cd->mcodebase) + targetdisp) -
340 (((u4 *) cd->mcodeptr) + 1);
348 /* emit_patcher_stubs **********************************************************
350 Generates the code for the patcher stubs.
352 *******************************************************************************/
354 void emit_patcher_stubs(jitdata *jd)
364 /* get required compiler data */
368 /* generate code patching stub call code */
372 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
373 /* check code segment size */
377 /* Get machine code which is patched back in later. The
378 call is 1 instruction word long. */
380 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
382 mcode = *((u4 *) tmpmcodeptr);
384 /* Patch in the call to call the following code (done at
387 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
388 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
390 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
391 M_BSR(REG_ITMP3, disp);
393 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
395 /* create stack frame */
397 M_LSUB_IMM(REG_SP, 6 * 8, REG_SP);
399 /* move return address onto stack */
401 M_AST(REG_ITMP3, REG_SP, 5 * 8);
403 /* move pointer to java_objectheader onto stack */
405 #if defined(ENABLE_THREADS)
406 /* create a virtual java_objectheader */
408 (void) dseg_add_unique_address(cd, NULL); /* flcword */
409 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
410 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
412 M_LDA(REG_ITMP3, REG_PV, disp);
413 M_AST(REG_ITMP3, REG_SP, 4 * 8);
418 /* move machine code onto stack */
420 disp = dseg_add_s4(cd, mcode);
421 M_ILD(REG_ITMP3, REG_PV, disp);
422 M_IST(REG_ITMP3, REG_SP, 3 * 8);
424 /* move class/method/field reference onto stack */
426 disp = dseg_add_address(cd, pref->ref);
427 M_ALD(REG_ITMP3, REG_PV, disp);
428 M_AST(REG_ITMP3, REG_SP, 2 * 8);
430 /* move data segment displacement onto stack */
432 disp = dseg_add_s4(cd, pref->disp);
433 M_ILD(REG_ITMP3, REG_PV, disp);
434 M_IST(REG_ITMP3, REG_SP, 1 * 8);
436 /* move patcher function pointer onto stack */
438 disp = dseg_add_functionptr(cd, pref->patcher);
439 M_ALD(REG_ITMP3, REG_PV, disp);
440 M_AST(REG_ITMP3, REG_SP, 0 * 8);
442 if (targetdisp == 0) {
443 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
445 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
446 M_ALD(REG_ITMP3, REG_PV, disp);
447 M_JMP(REG_ZERO, REG_ITMP3);
450 disp = (((u4 *) cd->mcodebase) + targetdisp) -
451 (((u4 *) cd->mcodeptr) + 1);
459 /* emit_replacement_stubs ******************************************************
461 Generates the code for the replacement stubs.
463 *******************************************************************************/
465 void emit_replacement_stubs(jitdata *jd)
474 /* get required compiler data */
479 rplp = code->rplpoints;
481 for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
482 /* check code segment size */
486 /* note start of stub code */
488 rplp->outcode = (u1 *) (ptrint) (cd->mcodeptr - cd->mcodebase);
490 /* make machine code for patching */
492 savedmcodeptr = cd->mcodeptr;
493 cd->mcodeptr = (u1 *) &(rplp->mcode);
495 disp = (ptrint) ((s4 *) rplp->outcode - (s4 *) rplp->pc) - 1;
498 cd->mcodeptr = savedmcodeptr;
500 /* create stack frame - 16-byte aligned */
502 M_LSUB_IMM(REG_SP, 2 * 8, REG_SP);
504 /* push address of `rplpoint` struct */
506 disp = dseg_add_address(cd, rplp);
507 M_ALD(REG_ITMP3, REG_PV, disp);
508 M_AST(REG_ITMP3, REG_SP, 0 * 8);
510 /* jump to replacement function */
512 disp = dseg_add_functionptr(cd, asm_replacement_out);
513 M_ALD(REG_ITMP3, REG_PV, disp);
514 M_JMP(REG_ZERO, REG_ITMP3);
519 /* emit_verbosecall_enter ******************************************************
521 Generates the code for the call trace.
523 *******************************************************************************/
526 void emit_verbosecall_enter(jitdata *jd)
535 /* get required compiler data */
543 /* mark trace code */
547 M_LDA(REG_SP, REG_SP, -((ARG_CNT + TMP_CNT + 2) * 8));
548 M_AST(REG_RA, REG_SP, 1 * 8);
550 /* save argument registers */
552 for (i = 0; i < INT_ARG_CNT; i++)
553 M_LST(rd->argintregs[i], REG_SP, (2 + i) * 8);
555 for (i = 0; i < FLT_ARG_CNT; i++)
556 M_DST(rd->argintregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
558 /* save temporary registers for leaf methods */
560 if (jd->isleafmethod) {
561 for (i = 0; i < INT_TMP_CNT; i++)
562 M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
564 for (i = 0; i < FLT_TMP_CNT; i++)
565 M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
568 /* load float arguments into integer registers */
570 for (i = 0; i < md->paramcount && i < INT_ARG_CNT; i++) {
571 t = md->paramtypes[i].type;
573 if (IS_FLT_DBL_TYPE(t)) {
574 if (IS_2_WORD_TYPE(t)) {
575 M_DST(rd->argfltregs[i], REG_SP, 0 * 8);
576 M_LLD(rd->argintregs[i], REG_SP, 0 * 8);
579 M_FST(rd->argfltregs[i], REG_SP, 0 * 8);
580 M_ILD(rd->argintregs[i], REG_SP, 0 * 8);
585 disp = dseg_add_address(cd, m);
586 M_ALD(REG_ITMP1, REG_PV, disp);
587 M_AST(REG_ITMP1, REG_SP, 0 * 8);
588 disp = dseg_add_functionptr(cd, builtin_trace_args);
589 M_ALD(REG_PV, REG_PV, disp);
590 M_JSR(REG_RA, REG_PV);
591 disp = (s4) (cd->mcodeptr - cd->mcodebase);
592 M_LDA(REG_PV, REG_RA, -disp);
593 M_ALD(REG_RA, REG_SP, 1 * 8);
595 /* restore argument registers */
597 for (i = 0; i < INT_ARG_CNT; i++)
598 M_LLD(rd->argintregs[i], REG_SP, (2 + i) * 8);
600 for (i = 0; i < FLT_ARG_CNT; i++)
601 M_DLD(rd->argintregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
603 /* restore temporary registers for leaf methods */
605 if (jd->isleafmethod) {
606 for (i = 0; i < INT_TMP_CNT; i++)
607 M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
609 for (i = 0; i < FLT_TMP_CNT; i++)
610 M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
613 M_LDA(REG_SP, REG_SP, (ARG_CNT + TMP_CNT + 2) * 8);
615 /* mark trace code */
619 #endif /* !defined(NDEBUG) */
622 /* emit_verbosecall_exit *******************************************************
624 Generates the code for the call trace.
626 *******************************************************************************/
629 void emit_verbosecall_exit(jitdata *jd)
636 /* get required compiler data */
642 /* mark trace code */
646 M_LDA(REG_SP, REG_SP, -4 * 8);
647 M_AST(REG_RA, REG_SP, 0 * 8);
649 M_LST(REG_RESULT, REG_SP, 1 * 8);
650 M_DST(REG_FRESULT, REG_SP, 2 * 8);
652 disp = dseg_add_address(cd, m);
653 M_ALD(rd->argintregs[0], REG_PV, disp);
655 M_MOV(REG_RESULT, rd->argintregs[1]);
656 M_FLTMOVE(REG_FRESULT, rd->argfltregs[2]);
657 M_FLTMOVE(REG_FRESULT, rd->argfltregs[3]);
659 disp = dseg_add_functionptr(cd, builtin_displaymethodstop);
660 M_ALD(REG_PV, REG_PV, disp);
661 M_JSR(REG_RA, REG_PV);
662 disp = (cd->mcodeptr - cd->mcodebase);
663 M_LDA(REG_PV, REG_RA, -disp);
665 M_DLD(REG_FRESULT, REG_SP, 2 * 8);
666 M_LLD(REG_RESULT, REG_SP, 1 * 8);
668 M_ALD(REG_RA, REG_SP, 0 * 8);
669 M_LDA(REG_SP, REG_SP, 4 * 8);
671 /* mark trace code */
675 #endif /* !defined(NDEBUG) */
679 * These are local overrides for various environment variables in Emacs.
680 * Please do not remove this and leave it at the end of the file, where
681 * Emacs will automagically detect them.
682 * ---------------------------------------------------------------------
685 * indent-tabs-mode: t
689 * vim:noexpandtab:sw=4:ts=4: