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 8211 2007-07-18 19:52:23Z michi $
37 #include "vm/jit/alpha/codegen.h"
39 #include "mm/memory.h"
41 #include "threads/lock-common.h"
43 #include "vm/builtin.h"
44 #include "vm/exceptions.h"
46 #include "vm/jit/abi.h"
47 #include "vm/jit/abi-asm.h"
48 #include "vm/jit/asmpart.h"
49 #include "vm/jit/dseg.h"
50 #include "vm/jit/emit-common.h"
51 #include "vm/jit/jit.h"
52 #include "vm/jit/patcher-common.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;
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;
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)
152 /* get required compiler data */
156 /* get source and destination variables */
158 src = VAROP(iptr->s1);
159 dst = VAROP(iptr->dst);
161 if ((src->vv.regoff != dst->vv.regoff) ||
162 ((src->flags ^ dst->flags) & INMEMORY)) {
164 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
165 /* emit nothing, as the value won't be used anyway */
169 /* If one of the variables resides in memory, we can eliminate
170 the register move from/to the temporary register with the
171 order of getting the destination register and the load. */
173 if (IS_INMEMORY(src->flags)) {
174 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
175 s1 = emit_load(jd, iptr, src, d);
178 s1 = emit_load(jd, iptr, src, REG_IFTMP);
179 d = codegen_reg_of_var(iptr->opc, dst, s1);
194 vm_abort("emit_copy: unknown type %d", src->type);
198 emit_store(jd, iptr, dst, d);
203 /* emit_iconst *****************************************************************
207 *******************************************************************************/
209 void emit_iconst(codegendata *cd, s4 d, s4 value)
213 if ((value >= -32768) && (value <= 32767))
214 M_LDA_INTERN(d, REG_ZERO, value);
216 disp = dseg_add_s4(cd, value);
217 M_ILD(d, REG_PV, disp);
222 /* emit_lconst *****************************************************************
226 *******************************************************************************/
228 void emit_lconst(codegendata *cd, s4 d, s8 value)
232 if ((value >= -32768) && (value <= 32767))
233 M_LDA_INTERN(d, REG_ZERO, value);
235 disp = dseg_add_s8(cd, value);
236 M_LLD(d, REG_PV, disp);
241 /* emit_branch *****************************************************************
243 Emits the code for conditional and unconditional branchs.
245 *******************************************************************************/
247 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
252 /* calculate the different displacements */
254 checkdisp = (disp - 4);
255 branchdisp = (disp - 4) >> 2;
257 /* check which branch to generate */
259 if (condition == BRANCH_UNCONDITIONAL) {
260 /* check displacement for overflow */
262 if ((checkdisp < (s4) 0xffe00000) || (checkdisp > (s4) 0x001fffff)) {
263 /* if the long-branches flag isn't set yet, do it */
265 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
266 log_println("setting error");
267 cd->flags |= (CODEGENDATA_FLAG_ERROR |
268 CODEGENDATA_FLAG_LONGBRANCHES);
271 vm_abort("emit_branch: emit unconditional long-branch code");
278 /* and displacement for overflow */
280 if ((checkdisp < (s4) 0xffe00000) || (checkdisp > (s4) 0x001fffff)) {
281 /* if the long-branches flag isn't set yet, do it */
283 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
284 log_println("setting error");
285 cd->flags |= (CODEGENDATA_FLAG_ERROR |
286 CODEGENDATA_FLAG_LONGBRANCHES);
289 vm_abort("emit_branch: emit conditional long-branch code");
294 M_BEQZ(reg, branchdisp);
297 M_BNEZ(reg, branchdisp);
300 M_BLTZ(reg, branchdisp);
303 M_BGEZ(reg, branchdisp);
306 M_BGTZ(reg, branchdisp);
309 M_BLEZ(reg, branchdisp);
312 vm_abort("emit_branch: unknown condition %d", condition);
319 /* emit_arithmetic_check *******************************************************
321 Emit an ArithmeticException check.
323 *******************************************************************************/
325 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
327 if (INSTRUCTION_MUST_CHECK(iptr)) {
329 /* Destination register must not be REG_ZERO, because then no
330 SIGSEGV is thrown. */
331 M_ALD_INTERN(reg, REG_ZERO, EXCEPTION_HARDWARE_ARITHMETIC);
336 /* emit_arrayindexoutofbounds_check ********************************************
338 Emit an ArrayIndexOutOfBoundsException check.
340 *******************************************************************************/
342 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
344 if (INSTRUCTION_MUST_CHECK(iptr)) {
345 M_ILD(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
346 M_CMPULT(s2, REG_ITMP3, REG_ITMP3);
347 M_BNEZ(REG_ITMP3, 1);
348 M_ALD_INTERN(s2, REG_ZERO, EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS);
353 /* emit_classcast_check ********************************************************
355 Emit a ClassCastException check.
357 *******************************************************************************/
359 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
361 if (INSTRUCTION_MUST_CHECK(iptr)) {
370 vm_abort("emit_classcast_check: unknown condition %d", condition);
372 M_ALD_INTERN(s1, REG_ZERO, EXCEPTION_HARDWARE_CLASSCAST);
377 /* emit_nullpointer_check ******************************************************
379 Emit a NullPointerException check.
381 *******************************************************************************/
383 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
385 if (INSTRUCTION_MUST_CHECK(iptr)) {
387 /* Destination register must not be REG_ZERO, because then no
388 SIGSEGV is thrown. */
389 M_ALD_INTERN(reg, REG_ZERO, EXCEPTION_HARDWARE_NULLPOINTER);
394 /* emit_exception_check ********************************************************
396 Emit an Exception check.
398 *******************************************************************************/
400 void emit_exception_check(codegendata *cd, instruction *iptr)
402 if (INSTRUCTION_MUST_CHECK(iptr)) {
403 M_BNEZ(REG_RESULT, 1);
404 /* Destination register must not be REG_ZERO, because then no
405 SIGSEGV is thrown. */
406 M_ALD_INTERN(REG_RESULT, REG_ZERO, EXCEPTION_HARDWARE_EXCEPTION);
411 /* emit_patcher_traps **********************************************************
413 Generates the code for the patcher traps.
415 *******************************************************************************/
417 void emit_patcher_traps(jitdata *jd)
425 /* get required compiler data */
430 /* generate patcher traps code */
432 for (pr = list_first_unsynced(code->patchers); pr != NULL; pr = list_next_unsynced(code->patchers, pr)) {
434 /* Get machine code which is patched back in later. The
435 trap is 1 instruction word long. */
437 tmpmcodeptr = (u1 *) (cd->mcodebase + pr->mpc);
438 pr->mcode = *((u4 *) tmpmcodeptr);
440 /* Patch in the trap to call the signal handler (done at
443 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
444 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
446 M_ALD_INTERN(REG_RESULT, REG_ZERO, EXCEPTION_HARDWARE_PATCHER);
448 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
453 /* emit_verbosecall_enter ******************************************************
455 Generates the code for the call trace.
457 *******************************************************************************/
460 void emit_verbosecall_enter(jitdata *jd)
469 /* get required compiler data */
477 /* mark trace code */
481 M_LDA(REG_SP, REG_SP, -((ARG_CNT + TMP_CNT + 2) * 8));
482 M_AST(REG_RA, REG_SP, 1 * 8);
484 /* save argument registers */
486 for (i = 0; i < INT_ARG_CNT; i++)
487 M_LST(abi_registers_integer_argument[i], REG_SP, (2 + i) * 8);
489 for (i = 0; i < FLT_ARG_CNT; i++)
490 M_DST(abi_registers_float_argument[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
492 /* save temporary registers for leaf methods */
494 if (jd->isleafmethod) {
495 for (i = 0; i < INT_TMP_CNT; i++)
496 M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
498 for (i = 0; i < FLT_TMP_CNT; i++)
499 M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
502 /* load float arguments into integer registers */
504 for (i = 0; i < md->paramcount && i < INT_ARG_CNT; i++) {
505 t = md->paramtypes[i].type;
507 if (IS_FLT_DBL_TYPE(t)) {
508 if (IS_2_WORD_TYPE(t)) {
509 M_DST(abi_registers_float_argument[i], REG_SP, 0 * 8);
510 M_LLD(abi_registers_integer_argument[i], REG_SP, 0 * 8);
513 M_FST(abi_registers_float_argument[i], REG_SP, 0 * 8);
514 M_ILD(abi_registers_integer_argument[i], REG_SP, 0 * 8);
519 disp = dseg_add_address(cd, m);
520 M_ALD(REG_ITMP1, REG_PV, disp);
521 M_AST(REG_ITMP1, REG_SP, 0 * 8);
522 disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
523 M_ALD(REG_PV, REG_PV, disp);
524 M_JSR(REG_RA, REG_PV);
525 disp = (s4) (cd->mcodeptr - cd->mcodebase);
526 M_LDA(REG_PV, REG_RA, -disp);
527 M_ALD(REG_RA, REG_SP, 1 * 8);
529 /* restore argument registers */
531 for (i = 0; i < INT_ARG_CNT; i++)
532 M_LLD(abi_registers_integer_argument[i], REG_SP, (2 + i) * 8);
534 for (i = 0; i < FLT_ARG_CNT; i++)
535 M_DLD(abi_registers_float_argument[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
537 /* restore temporary registers for leaf methods */
539 if (jd->isleafmethod) {
540 for (i = 0; i < INT_TMP_CNT; i++)
541 M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
543 for (i = 0; i < FLT_TMP_CNT; i++)
544 M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
547 M_LDA(REG_SP, REG_SP, (ARG_CNT + TMP_CNT + 2) * 8);
549 /* mark trace code */
553 #endif /* !defined(NDEBUG) */
556 /* emit_verbosecall_exit *******************************************************
558 Generates the code for the call trace.
560 void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
562 *******************************************************************************/
565 void emit_verbosecall_exit(jitdata *jd)
572 /* get required compiler data */
578 /* mark trace code */
582 M_ASUB_IMM(REG_SP, 4 * 8, REG_SP);
583 M_AST(REG_RA, REG_SP, 0 * 8);
585 M_LST(REG_RESULT, REG_SP, 1 * 8);
586 M_DST(REG_FRESULT, REG_SP, 2 * 8);
588 M_MOV(REG_RESULT, REG_A0);
589 M_FMOV(REG_FRESULT, REG_FA1);
590 M_FMOV(REG_FRESULT, REG_FA2);
592 disp = dseg_add_address(cd, m);
593 M_ALD(REG_A3, REG_PV, disp);
595 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
596 M_ALD(REG_PV, REG_PV, disp);
597 M_JSR(REG_RA, REG_PV);
598 disp = (cd->mcodeptr - cd->mcodebase);
599 M_LDA(REG_PV, REG_RA, -disp);
601 M_DLD(REG_FRESULT, REG_SP, 2 * 8);
602 M_LLD(REG_RESULT, REG_SP, 1 * 8);
604 M_ALD(REG_RA, REG_SP, 0 * 8);
605 M_AADD_IMM(REG_SP, 4 * 8, REG_SP);
607 /* mark trace code */
611 #endif /* !defined(NDEBUG) */
615 * These are local overrides for various environment variables in Emacs.
616 * Please do not remove this and leave it at the end of the file, where
617 * Emacs will automagically detect them.
618 * ---------------------------------------------------------------------
621 * indent-tabs-mode: t
625 * vim:noexpandtab:sw=4:ts=4: