1 /* src/vm/jit/alpha/emit.c - Alpha code emitter functions
3 Copyright (C) 1996-2005, 2006, 2007 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 $Id: emit.c 4398 2006-01-31 23:43:08Z twisti $
37 #include "vm/jit/alpha/codegen.h"
39 #include "mm/memory.h"
41 #if defined(ENABLE_THREADS)
42 # include "threads/native/lock.h"
45 #include "vm/builtin.h"
46 #include "vm/exceptions.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-common.h"
52 #include "vm/jit/jit.h"
53 #include "vm/jit/replace.h"
55 #include "vmcore/options.h"
58 /* emit_load *******************************************************************
60 Emits a possible load of an operand.
62 *******************************************************************************/
64 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
70 /* get required compiler data */
74 if (IS_INMEMORY(src->flags)) {
77 disp = src->vv.regoff * 8;
83 M_LLD(tempreg, REG_SP, disp);
87 M_DLD(tempreg, REG_SP, disp);
90 vm_abort("emit_load: unknown type %d", src->type);
102 /* emit_store ******************************************************************
104 Emit a possible store for the given variable.
106 *******************************************************************************/
108 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
113 /* get required compiler data */
117 if (IS_INMEMORY(dst->flags)) {
120 disp = dst->vv.regoff * 8;
126 M_LST(d, REG_SP, disp);
130 M_DST(d, REG_SP, disp);
133 vm_abort("emit_store: unknown type %d", dst->type);
139 /* emit_copy *******************************************************************
141 Generates a register/memory to register/memory copy.
143 *******************************************************************************/
145 void emit_copy(jitdata *jd, instruction *iptr, varinfo *src, varinfo *dst)
150 /* get required compiler data */
154 if ((src->vv.regoff != dst->vv.regoff) ||
155 ((src->flags ^ dst->flags) & INMEMORY)) {
157 /* If one of the variables resides in memory, we can eliminate
158 the register move from/to the temporary register with the
159 order of getting the destination register and the load. */
161 if (IS_INMEMORY(src->flags)) {
162 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
163 s1 = emit_load(jd, iptr, src, d);
166 s1 = emit_load(jd, iptr, src, REG_IFTMP);
167 d = codegen_reg_of_var(iptr->opc, dst, s1);
182 vm_abort("emit_copy: unknown type %d", dst->type);
186 emit_store(jd, iptr, dst, d);
191 /* emit_iconst *****************************************************************
195 *******************************************************************************/
197 void emit_iconst(codegendata *cd, s4 d, s4 value)
201 if ((value >= -32768) && (value <= 32767))
202 M_LDA_INTERN(d, REG_ZERO, value);
204 disp = dseg_add_s4(cd, value);
205 M_ILD(d, REG_PV, disp);
210 /* emit_lconst *****************************************************************
214 *******************************************************************************/
216 void emit_lconst(codegendata *cd, s4 d, s8 value)
220 if ((value >= -32768) && (value <= 32767))
221 M_LDA_INTERN(d, REG_ZERO, value);
223 disp = dseg_add_s8(cd, value);
224 M_LLD(d, REG_PV, disp);
229 /* emit_branch *****************************************************************
231 Emits the code for conditional and unconditional branchs.
233 *******************************************************************************/
235 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
240 /* calculate the different displacements */
242 checkdisp = (disp - 4);
243 branchdisp = (disp - 4) >> 2;
245 /* check which branch to generate */
247 if (condition == BRANCH_UNCONDITIONAL) {
248 /* check displacement for overflow */
250 if ((checkdisp < (s4) 0xffe00000) || (checkdisp > (s4) 0x001fffff)) {
251 /* if the long-branches flag isn't set yet, do it */
253 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
254 log_println("setting error");
255 cd->flags |= (CODEGENDATA_FLAG_ERROR |
256 CODEGENDATA_FLAG_LONGBRANCHES);
259 vm_abort("emit_branch: emit unconditional long-branch code");
266 /* and displacement for overflow */
268 if ((checkdisp < (s4) 0xffe00000) || (checkdisp > (s4) 0x001fffff)) {
269 /* if the long-branches flag isn't set yet, do it */
271 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
272 log_println("setting error");
273 cd->flags |= (CODEGENDATA_FLAG_ERROR |
274 CODEGENDATA_FLAG_LONGBRANCHES);
277 vm_abort("emit_branch: emit conditional long-branch code");
282 M_BEQZ(reg, branchdisp);
285 M_BNEZ(reg, branchdisp);
288 M_BLTZ(reg, branchdisp);
291 M_BGEZ(reg, branchdisp);
294 M_BGTZ(reg, branchdisp);
297 M_BLEZ(reg, branchdisp);
300 vm_abort("emit_branch: unknown condition %d", condition);
307 /* emit_arithmetic_check *******************************************************
309 Emit an ArithmeticException check.
311 *******************************************************************************/
313 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
315 if (INSTRUCTION_MUST_CHECK(iptr)) {
317 /* Destination register must not be REG_ZERO, because then no
318 SIGSEGV is thrown. */
319 M_ALD_INTERN(reg, REG_ZERO, EXCEPTION_HARDWARE_ARITHMETIC);
324 /* emit_arrayindexoutofbounds_check ********************************************
326 Emit an ArrayIndexOutOfBoundsException check.
328 *******************************************************************************/
330 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
332 if (INSTRUCTION_MUST_CHECK(iptr)) {
333 M_ILD(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
334 M_CMPULT(s2, REG_ITMP3, REG_ITMP3);
335 M_BNEZ(REG_ITMP3, 1);
336 M_ALD_INTERN(s2, REG_ZERO, EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS);
341 /* emit_classcast_check ********************************************************
343 Emit a ClassCastException check.
345 *******************************************************************************/
347 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
349 if (INSTRUCTION_MUST_CHECK(iptr)) {
358 vm_abort("emit_classcast_check: unknown condition %d", condition);
360 M_ALD_INTERN(s1, REG_ZERO, EXCEPTION_HARDWARE_CLASSCAST);
365 /* emit_nullpointer_check ******************************************************
367 Emit a NullPointerException check.
369 *******************************************************************************/
371 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
373 if (INSTRUCTION_MUST_CHECK(iptr)) {
375 /* Destination register must not be REG_ZERO, because then no
376 SIGSEGV is thrown. */
377 M_ALD_INTERN(reg, REG_ZERO, EXCEPTION_HARDWARE_NULLPOINTER);
382 /* emit_exception_check ********************************************************
384 Emit an Exception check.
386 *******************************************************************************/
388 void emit_exception_check(codegendata *cd, instruction *iptr)
390 if (INSTRUCTION_MUST_CHECK(iptr)) {
391 M_BNEZ(REG_RESULT, 1);
392 /* Destination register must not be REG_ZERO, because then no
393 SIGSEGV is thrown. */
394 M_ALD_INTERN(REG_RESULT, REG_ZERO, EXCEPTION_HARDWARE_EXCEPTION);
399 /* emit_patcher_stubs **********************************************************
401 Generates the code for the patcher stubs.
403 *******************************************************************************/
405 void emit_patcher_stubs(jitdata *jd)
415 /* get required compiler data */
419 /* generate code patching stub call code */
423 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
424 /* check code segment size */
428 /* Get machine code which is patched back in later. The
429 call is 1 instruction word long. */
431 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
433 mcode = *((u4 *) tmpmcodeptr);
435 /* Patch in the call to call the following code (done at
438 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
439 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
441 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
442 M_BSR(REG_ITMP3, disp);
444 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
446 /* create stack frame */
448 M_LSUB_IMM(REG_SP, 6 * 8, REG_SP);
450 /* move return address onto stack */
452 M_AST(REG_ITMP3, REG_SP, 5 * 8);
454 /* move pointer to java_objectheader onto stack */
456 #if defined(ENABLE_THREADS)
457 /* create a virtual java_objectheader */
459 (void) dseg_add_unique_address(cd, NULL); /* flcword */
460 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
461 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
463 M_LDA(REG_ITMP3, REG_PV, disp);
464 M_AST(REG_ITMP3, REG_SP, 4 * 8);
469 /* move machine code onto stack */
471 disp = dseg_add_s4(cd, mcode);
472 M_ILD(REG_ITMP3, REG_PV, disp);
473 M_IST(REG_ITMP3, REG_SP, 3 * 8);
475 /* move class/method/field reference onto stack */
477 disp = dseg_add_address(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_add_s4(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_add_functionptr(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_add_functionptr(cd, asm_patcher_wrapper);
497 M_ALD(REG_ITMP3, REG_PV, disp);
498 M_JMP(REG_ZERO, REG_ITMP3);
501 disp = (((u4 *) cd->mcodebase) + targetdisp) -
502 (((u4 *) cd->mcodeptr) + 1);
510 /* emit_replacement_stubs ******************************************************
512 Generates the code for the replacement stubs.
514 *******************************************************************************/
516 #if defined(ENABLE_REPLACEMENT)
517 void emit_replacement_stubs(jitdata *jd)
528 /* get required compiler data */
533 rplp = code->rplpoints;
535 /* store beginning of replacement stubs */
537 code->replacementstubs = (u1*) (cd->mcodeptr - cd->mcodebase);
539 for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
540 /* do not generate stubs for non-trappable points */
542 if (rplp->flags & RPLPOINT_FLAG_NOTRAP)
545 /* check code segment size */
550 savedmcodeptr = cd->mcodeptr;
553 /* create stack frame - 16-byte aligned */
555 M_LSUB_IMM(REG_SP, 2 * 8, REG_SP);
557 /* push address of `rplpoint` struct */
559 disp = dseg_add_address(cd, rplp);
560 M_ALD(REG_ITMP3, REG_PV, disp);
561 M_AST(REG_ITMP3, REG_SP, 0 * 8);
563 /* jump to replacement function */
565 disp = dseg_add_functionptr(cd, asm_replacement_out);
566 M_ALD(REG_ITMP3, REG_PV, disp);
567 M_JMP(REG_ZERO, REG_ITMP3);
569 assert((cd->mcodeptr - savedmcodeptr) == 4*REPLACEMENT_STUB_SIZE);
572 #endif /* defined(ENABLE_REPLACEMENT) */
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, -((ARG_CNT + TMP_CNT + 2) * 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->argintregs[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_add_address(cd, m);
642 M_ALD(REG_ITMP1, REG_PV, disp);
643 M_AST(REG_ITMP1, REG_SP, 0 * 8);
644 disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
645 M_ALD(REG_PV, REG_PV, disp);
646 M_JSR(REG_RA, REG_PV);
647 disp = (s4) (cd->mcodeptr - cd->mcodebase);
648 M_LDA(REG_PV, REG_RA, -disp);
649 M_ALD(REG_RA, REG_SP, 1 * 8);
651 /* restore argument registers */
653 for (i = 0; i < INT_ARG_CNT; i++)
654 M_LLD(rd->argintregs[i], REG_SP, (2 + i) * 8);
656 for (i = 0; i < FLT_ARG_CNT; i++)
657 M_DLD(rd->argintregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
659 /* restore temporary registers for leaf methods */
661 if (jd->isleafmethod) {
662 for (i = 0; i < INT_TMP_CNT; i++)
663 M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
665 for (i = 0; i < FLT_TMP_CNT; i++)
666 M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
669 M_LDA(REG_SP, REG_SP, (ARG_CNT + TMP_CNT + 2) * 8);
671 /* mark trace code */
675 #endif /* !defined(NDEBUG) */
678 /* emit_verbosecall_exit *******************************************************
680 Generates the code for the call trace.
682 void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
684 *******************************************************************************/
687 void emit_verbosecall_exit(jitdata *jd)
694 /* get required compiler data */
700 /* mark trace code */
704 M_ASUB_IMM(REG_SP, 4 * 8, REG_SP);
705 M_AST(REG_RA, REG_SP, 0 * 8);
707 M_LST(REG_RESULT, REG_SP, 1 * 8);
708 M_DST(REG_FRESULT, REG_SP, 2 * 8);
710 M_MOV(REG_RESULT, REG_A0);
711 M_FMOV(REG_FRESULT, REG_FA1);
712 M_FMOV(REG_FRESULT, REG_FA2);
714 disp = dseg_add_address(cd, m);
715 M_ALD(REG_A3, REG_PV, disp);
717 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
718 M_ALD(REG_PV, REG_PV, disp);
719 M_JSR(REG_RA, REG_PV);
720 disp = (cd->mcodeptr - cd->mcodebase);
721 M_LDA(REG_PV, REG_RA, -disp);
723 M_DLD(REG_FRESULT, REG_SP, 2 * 8);
724 M_LLD(REG_RESULT, REG_SP, 1 * 8);
726 M_ALD(REG_RA, REG_SP, 0 * 8);
727 M_AADD_IMM(REG_SP, 4 * 8, REG_SP);
729 /* mark trace code */
733 #endif /* !defined(NDEBUG) */
737 * These are local overrides for various environment variables in Emacs.
738 * Please do not remove this and leave it at the end of the file, where
739 * Emacs will automagically detect them.
740 * ---------------------------------------------------------------------
743 * indent-tabs-mode: t
747 * vim:noexpandtab:sw=4:ts=4: