1 /* src/vm/jit/alpha/emit.c - Alpha code emitter functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
34 #include "vm/jit/alpha/codegen.h"
36 #include "mm/memory.h"
38 #include "threads/lock-common.h"
40 #include "vm/options.h"
42 #include "vm/jit/abi.h"
43 #include "vm/jit/abi-asm.h"
44 #include "vm/jit/asmpart.h"
45 #include "vm/jit/dseg.h"
46 #include "vm/jit/emit-common.h"
47 #include "vm/jit/jit.hpp"
48 #include "vm/jit/patcher-common.h"
49 #include "vm/jit/replace.h"
50 #include "vm/jit/trace.hpp"
51 #include "vm/jit/trap.h"
54 /* emit_load *******************************************************************
56 Emits a possible load of an operand.
58 *******************************************************************************/
60 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
66 /* get required compiler data */
70 if (IS_INMEMORY(src->flags)) {
73 disp = src->vv.regoff;
79 M_LLD(tempreg, REG_SP, disp);
83 M_DLD(tempreg, REG_SP, disp);
86 vm_abort("emit_load: unknown type %d", src->type);
98 /* emit_store ******************************************************************
100 Emit a possible store for the given variable.
102 *******************************************************************************/
104 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
109 /* get required compiler data */
113 if (IS_INMEMORY(dst->flags)) {
116 disp = dst->vv.regoff;
122 M_LST(d, REG_SP, disp);
126 M_DST(d, REG_SP, disp);
129 vm_abort("emit_store: unknown type %d", dst->type);
135 /* emit_copy *******************************************************************
137 Generates a register/memory to register/memory copy.
139 *******************************************************************************/
141 void emit_copy(jitdata *jd, instruction *iptr)
148 /* get required compiler data */
152 /* get source and destination variables */
154 src = VAROP(iptr->s1);
155 dst = VAROP(iptr->dst);
157 if ((src->vv.regoff != dst->vv.regoff) ||
158 ((src->flags ^ dst->flags) & INMEMORY)) {
160 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
161 /* emit nothing, as the value won't be used anyway */
165 /* If one of the variables resides in memory, we can eliminate
166 the register move from/to the temporary register with the
167 order of getting the destination register and the load. */
169 if (IS_INMEMORY(src->flags)) {
170 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
171 s1 = emit_load(jd, iptr, src, d);
174 s1 = emit_load(jd, iptr, src, REG_IFTMP);
175 d = codegen_reg_of_var(iptr->opc, dst, s1);
190 vm_abort("emit_copy: unknown type %d", src->type);
194 emit_store(jd, iptr, dst, d);
199 /* emit_iconst *****************************************************************
203 *******************************************************************************/
205 void emit_iconst(codegendata *cd, s4 d, s4 value)
209 if ((value >= -32768) && (value <= 32767))
210 M_LDA_INTERN(d, REG_ZERO, value);
212 disp = dseg_add_s4(cd, value);
213 M_ILD(d, REG_PV, disp);
218 /* emit_lconst *****************************************************************
222 *******************************************************************************/
224 void emit_lconst(codegendata *cd, s4 d, s8 value)
228 if ((value >= -32768) && (value <= 32767))
229 M_LDA_INTERN(d, REG_ZERO, value);
231 disp = dseg_add_s8(cd, value);
232 M_LLD(d, REG_PV, disp);
237 /* emit_branch *****************************************************************
239 Emits the code for conditional and unconditional branchs.
241 *******************************************************************************/
243 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
248 /* calculate the different displacements */
250 checkdisp = (disp - 4);
251 branchdisp = (disp - 4) >> 2;
253 /* check which branch to generate */
255 if (condition == BRANCH_UNCONDITIONAL) {
256 /* check displacement for overflow */
258 if ((checkdisp < (s4) 0xffe00000) || (checkdisp > (s4) 0x001fffff)) {
259 /* if the long-branches flag isn't set yet, do it */
261 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
262 log_println("setting error");
263 cd->flags |= (CODEGENDATA_FLAG_ERROR |
264 CODEGENDATA_FLAG_LONGBRANCHES);
267 vm_abort("emit_branch: emit unconditional long-branch code");
274 /* and displacement for overflow */
276 if ((checkdisp < (s4) 0xffe00000) || (checkdisp > (s4) 0x001fffff)) {
277 /* if the long-branches flag isn't set yet, do it */
279 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
280 log_println("setting error");
281 cd->flags |= (CODEGENDATA_FLAG_ERROR |
282 CODEGENDATA_FLAG_LONGBRANCHES);
285 vm_abort("emit_branch: emit conditional long-branch code");
290 M_BEQZ(reg, branchdisp);
293 M_BNEZ(reg, branchdisp);
296 M_BLTZ(reg, branchdisp);
299 M_BGEZ(reg, branchdisp);
302 M_BGTZ(reg, branchdisp);
305 M_BLEZ(reg, branchdisp);
308 vm_abort("emit_branch: unknown condition %d", condition);
315 /* emit_arithmetic_check *******************************************************
317 Emit an ArithmeticException check.
319 *******************************************************************************/
321 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
323 if (INSTRUCTION_MUST_CHECK(iptr)) {
325 /* Destination register must not be REG_ZERO, because then no
326 SIGSEGV is thrown. */
327 M_ALD_INTERN(reg, REG_ZERO, TRAP_ArithmeticException);
332 /* emit_arrayindexoutofbounds_check ********************************************
334 Emit an ArrayIndexOutOfBoundsException check.
336 *******************************************************************************/
338 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
340 if (INSTRUCTION_MUST_CHECK(iptr)) {
341 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
342 M_CMPULT(s2, REG_ITMP3, REG_ITMP3);
343 M_BNEZ(REG_ITMP3, 1);
344 M_ALD_INTERN(s2, REG_ZERO, TRAP_ArrayIndexOutOfBoundsException);
349 /* emit_arraystore_check *******************************************************
351 Emit an ArrayStoreException check.
353 *******************************************************************************/
355 void emit_arraystore_check(codegendata *cd, instruction *iptr)
357 if (INSTRUCTION_MUST_CHECK(iptr)) {
358 M_BNEZ(REG_RESULT, 1);
359 /* Destination register must not be REG_ZERO, because then no
360 SIGSEGV is thrown. */
361 M_ALD_INTERN(REG_RESULT, REG_ZERO, TRAP_ArrayStoreException);
366 /* emit_classcast_check ********************************************************
368 Emit a ClassCastException check.
370 *******************************************************************************/
372 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
374 if (INSTRUCTION_MUST_CHECK(iptr)) {
383 vm_abort("emit_classcast_check: unknown condition %d", condition);
385 M_ALD_INTERN(s1, REG_ZERO, TRAP_ClassCastException);
390 /* emit_nullpointer_check ******************************************************
392 Emit a NullPointerException check.
394 *******************************************************************************/
396 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
398 if (INSTRUCTION_MUST_CHECK(iptr)) {
400 /* Destination register must not be REG_ZERO, because then no
401 SIGSEGV is thrown. */
402 M_ALD_INTERN(reg, REG_ZERO, TRAP_NullPointerException);
407 /* emit_exception_check ********************************************************
409 Emit an Exception check.
411 *******************************************************************************/
413 void emit_exception_check(codegendata *cd, instruction *iptr)
415 if (INSTRUCTION_MUST_CHECK(iptr)) {
416 M_BNEZ(REG_RESULT, 1);
417 /* Destination register must not be REG_ZERO, because then no
418 SIGSEGV is thrown. */
419 M_ALD_INTERN(REG_RESULT, REG_ZERO, TRAP_CHECK_EXCEPTION);
424 /* emit_trap_compiler **********************************************************
426 Emit a trap instruction which calls the JIT compiler.
428 *******************************************************************************/
430 void emit_trap_compiler(codegendata *cd)
432 M_ALD_INTERN(REG_METHODPTR, REG_ZERO, TRAP_COMPILER);
436 /* emit_trap *******************************************************************
438 Emit a trap instruction and return the original machine code.
440 *******************************************************************************/
442 uint32_t emit_trap(codegendata *cd)
446 /* Get machine code which is patched back in later. The
447 trap is 1 instruction word long. */
449 mcode = *((uint32_t*) cd->mcodeptr);
451 // Generate a SIGILL.
458 /* emit_verbosecall_enter ******************************************************
460 Generates the code for the call trace.
462 *******************************************************************************/
465 void emit_verbosecall_enter(jitdata *jd)
472 int32_t stackframesize;
476 /* get required compiler data */
485 /* mark trace code */
489 stackframesize = ARG_CNT + TMP_CNT + md->paramcount + 1;
491 M_LDA(REG_SP, REG_SP, -(stackframesize * 8));
492 M_AST(REG_RA, REG_SP, 0 * 8);
494 /* save all argument and temporary registers for leaf methods */
496 if (code_is_leafmethod(code)) {
497 j = 1 + md->paramcount;
499 for (i = 0; i < INT_ARG_CNT; i++, j++)
500 M_LST(abi_registers_integer_argument[i], REG_SP, j * 8);
502 for (i = 0; i < FLT_ARG_CNT; i++, j++)
503 M_DST(abi_registers_float_argument[i], REG_SP, j * 8);
505 for (i = 0; i < INT_TMP_CNT; i++, j++)
506 M_LST(rd->tmpintregs[i], REG_SP, j * 8);
508 for (i = 0; i < FLT_TMP_CNT; i++, j++)
509 M_DST(rd->tmpfltregs[i], REG_SP, j * 8);
512 /* save argument registers */
514 for (i = 0; i < md->paramcount; i++) {
515 if (!md->params[i].inmemory) {
516 s = md->params[i].regoff;
518 switch (md->paramtypes[i].type) {
522 M_LST(s, REG_SP, (1 + i) * 8);
526 M_DST(s, REG_SP, (1 + i) * 8);
532 disp = dseg_add_address(cd, m);
533 M_ALD(REG_A0, REG_PV, disp);
534 M_AADD_IMM(REG_SP, 1 * 8, REG_A1);
535 M_LDA(REG_A2, REG_SP, stackframesize * 8 + cd->stackframesize * 8);
537 disp = dseg_add_functionptr(cd, trace_java_call_enter);
538 M_ALD(REG_PV, REG_PV, disp);
539 M_JSR(REG_RA, REG_PV);
540 disp = (s4) (cd->mcodeptr - cd->mcodebase);
541 M_LDA(REG_PV, REG_RA, -disp);
542 M_ALD(REG_RA, REG_SP, 0 * 8);
544 /* restore argument registers */
546 for (i = 0; i < md->paramcount; i++) {
547 if (!md->params[i].inmemory) {
548 s = md->params[i].regoff;
550 switch (md->paramtypes[i].type) {
554 M_LLD(s, REG_SP, (1 + i) * 8);
558 M_DLD(s, REG_SP, (1 + i) * 8);
564 /* restore all argument and temporary registers for leaf methods */
566 if (code_is_leafmethod(code)) {
567 j = 1 + md->paramcount;
569 for (i = 0; i < INT_ARG_CNT; i++, j++)
570 M_LLD(abi_registers_integer_argument[i], REG_SP, j * 8);
572 for (i = 0; i < FLT_ARG_CNT; i++, j++)
573 M_DLD(abi_registers_float_argument[i], REG_SP, j * 8);
575 for (i = 0; i < INT_TMP_CNT; i++, j++)
576 M_LLD(rd->tmpintregs[i], REG_SP, j * 8);
578 for (i = 0; i < FLT_TMP_CNT; i++, j++)
579 M_DLD(rd->tmpfltregs[i], REG_SP, j * 8);
582 M_LDA(REG_SP, REG_SP, stackframesize * 8);
584 /* mark trace code */
588 #endif /* !defined(NDEBUG) */
591 /* emit_verbosecall_exit *******************************************************
593 Generates the code for the call trace.
595 *******************************************************************************/
598 void emit_verbosecall_exit(jitdata *jd)
606 /* get required compiler data */
614 /* mark trace code */
618 M_ASUB_IMM(REG_SP, 2 * 8, REG_SP);
619 M_AST(REG_RA, REG_SP, 0 * 8);
621 /* save return value */
623 switch (md->returntype.type) {
627 M_LST(REG_RESULT, REG_SP, 1 * 8);
631 M_DST(REG_FRESULT, REG_SP, 1 * 8);
635 disp = dseg_add_address(cd, m);
636 M_ALD(REG_A0, REG_PV, disp);
637 M_AADD_IMM(REG_SP, 1 * 8, REG_A1);
639 disp = dseg_add_functionptr(cd, trace_java_call_exit);
640 M_ALD(REG_PV, REG_PV, disp);
641 M_JSR(REG_RA, REG_PV);
642 disp = (cd->mcodeptr - cd->mcodebase);
643 M_LDA(REG_PV, REG_RA, -disp);
645 /* restore return value */
647 switch (md->returntype.type) {
651 M_LLD(REG_RESULT, REG_SP, 1 * 8);
655 M_DLD(REG_FRESULT, REG_SP, 1 * 8);
659 M_ALD(REG_RA, REG_SP, 0 * 8);
660 M_AADD_IMM(REG_SP, 2 * 8, REG_SP);
662 /* mark trace code */
666 #endif /* !defined(NDEBUG) */
670 * These are local overrides for various environment variables in Emacs.
671 * Please do not remove this and leave it at the end of the file, where
672 * Emacs will automagically detect them.
673 * ---------------------------------------------------------------------
676 * indent-tabs-mode: t
680 * vim:noexpandtab:sw=4:ts=4: