1 /* src/vm/jit/m68k/emit.c
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
30 #include "vm/jit/m68k/codegen.h"
31 #include "vm/jit/m68k/emit.h"
33 #include "mm/memory.hpp"
35 #include "vm/jit/builtin.hpp"
37 #include "vm/jit/asmpart.h"
38 #include "vm/jit/emit-common.hpp"
39 #include "vm/jit/trace.hpp"
40 #include "vm/jit/trap.hpp"
43 /* emit_mov_imm_reg **************************************************************************
45 * Loads an immededat operand into an integer data register
47 ********************************************************************************************/
48 void emit_mov_imm_reg (codegendata *cd, s4 imm, s4 dreg)
50 /* FIXME: -1 can be used as byte form 0xff, but this ifs cascade is plain wrong it seems */
52 if ( (imm & 0x0000007F) == imm) {
54 *((s2*)cd->mcodeptr) = 0x7000 | (dreg << 9) | imm; /* MOVEQ.L */
56 } else if ((imm & 0x00007FFF) == imm) {
58 OPWORD( ((7<<6) | (dreg << 3) | 5), 7, 4); /* MVS.W */
59 *((s2*)cd->mcodeptr) = (s2)imm;
63 OPWORD( ((2<<6) | (dreg << 3) | 0), 7, 4);
64 *((s4*)cd->mcodeptr) = (s4)imm;
70 /* emit_copy *******************************************************************
72 Generates a register/memory to register/memory copy.
74 *******************************************************************************/
76 void emit_copy(jitdata *jd, instruction *iptr)
83 /* get required compiler data */
87 /* get source and destination variables */
89 src = VAROP(iptr->s1);
90 dst = VAROP(iptr->dst);
92 if ((src->vv.regoff != dst->vv.regoff) ||
93 (IS_INMEMORY(src->flags ^ dst->flags))) {
95 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
96 /* emit nothing, as the value won't be used anyway */
100 /* If one of the variables resides in memory, we can eliminate
101 the register move from/to the temporary register with the
102 order of getting the destination register and the load. */
104 if (IS_INMEMORY(src->flags)) {
105 if (IS_LNG_TYPE(src->type))
106 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
108 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
110 s1 = emit_load(jd, iptr, src, d);
113 if (IS_LNG_TYPE(src->type))
114 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
116 s1 = emit_load(jd, iptr, src, REG_IFTMP);
118 d = codegen_reg_of_var(iptr->opc, dst, s1);
123 case TYPE_INT: M_INTMOVE(s1, d); break;
124 case TYPE_ADR: M_ADRMOVE(s1, d); break;
125 case TYPE_LNG: M_LNGMOVE(s1, d); break;
126 #if !defined(ENABLE_SOFTFLOAT)
127 case TYPE_FLT: M_FLTMOVE(s1, d); break;
128 case TYPE_DBL: M_DBLMOVE(s1, d); break;
130 case TYPE_FLT: M_INTMOVE(s1, d); break;
131 case TYPE_DBL: M_LNGMOVE(s1, d); break;
134 vm_abort("emit_copy: unknown type %d", src->type);
138 emit_store(jd, iptr, dst, d);
143 /* emit_store ******************************************************************
145 Emits a possible store of the destination operand.
147 *******************************************************************************/
149 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
153 /* get required compiler data */
157 if (IS_INMEMORY(dst->flags)) {
161 #if defined(ENABLE_SOFTFLOAT)
165 M_LST(d, REG_SP, dst->vv.regoff);
167 #if defined(ENABLE_SOFTFLOAT)
171 M_IST(d, REG_SP, dst->vv.regoff);
174 M_AST(d, REG_SP, dst->vv.regoff);
176 #if !defined(ENABLE_SOFTFLOAT)
178 M_DST(d, REG_SP, dst->vv.regoff);
181 M_FST(d, REG_SP, dst->vv.regoff);
185 vm_abort("emit_store: unknown type %d", dst->type);
191 /* emit_load *******************************************************************
193 Emits a possible load of an operand.
195 *******************************************************************************/
197 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
203 /* get required compiler data */
207 if (IS_INMEMORY(src->flags)) {
210 disp = src->vv.regoff;
213 #if defined(ENABLE_SOFTFLOAT)
217 M_ILD(tempreg, REG_SP, disp);
219 #if defined(ENABLE_SOFTFLOAT)
223 M_LLD(tempreg, REG_SP, disp);
226 M_ALD(tempreg, REG_SP, disp);
228 #if !defined(ENABLE_SOFTFLOAT)
230 M_FLD(tempreg, REG_SP, disp);
233 M_DLD(tempreg, REG_SP, disp);
237 vm_abort("emit_load: unknown type %d", src->type);
240 if (IS_FLT_DBL_TYPE(src->type)) {
241 if (IS_2_WORD_TYPE(src->type)) {
242 M_DLD(tempreg, REG_SP, disp);
244 M_FLD(tempreg, REG_SP, disp);
247 if (IS_2_WORD_TYPE(src->type)) {
248 M_LLD(tempreg, REG_SP, disp);
250 M_ILD(tempreg, REG_SP, disp);
258 reg = src->vv.regoff;
263 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
269 #if !defined(ENABLE_SOFTFLOAT)
270 assert(src->type == TYPE_LNG);
272 assert(src->type == TYPE_LNG || src->type == TYPE_DBL);
275 /* get required compiler data */
278 if (IS_INMEMORY(src->flags)) {
281 disp = src->vv.regoff;
282 M_ILD(tempreg, REG_SP, disp + 4);
285 reg = GET_LOW_REG(src->vv.regoff);
289 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
295 #if !defined(ENABLE_SOFTFLOAT)
296 assert(src->type == TYPE_LNG);
298 assert(src->type == TYPE_LNG || src->type == TYPE_DBL);
300 /* get required compiler data */
303 if (IS_INMEMORY(src->flags)) {
305 disp = src->vv.regoff;
306 M_ILD(tempreg, REG_SP, disp);
309 reg = GET_HIGH_REG(src->vv.regoff);
316 * Emits code updating the condition register by comparing one integer
317 * register to an immediate integer value.
319 void emit_icmp_imm(codegendata* cd, int reg, int32_t value)
321 M_ICMP_IMM(value, reg);
325 /* emit_branch *****************************************************************
327 Emits the code for conditional and unconditional branchs.
329 *******************************************************************************/
330 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
332 /* calculate the different displacements */
333 /* PC is a at branch instruction + 2 */
334 /* coditional and uncondition branching work the same way */
335 /* short branches have signed 16 bit offset */
336 /* long branches are signed 32 bit */
337 /* the 8 bit offset branching instructions are not used */
341 /* check displacement for overflow */
342 if ((disp & 0x0000FFFF) != disp) {
343 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
344 cd->flags |= (CODEGENDATA_FLAG_ERROR | CODEGENDATA_FLAG_LONGBRANCHES);
348 /* check which branch to generate */
350 if (condition == BRANCH_UNCONDITIONAL) {
351 if (CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
357 if (CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
385 vm_abort("emit_branch: unknown condition %d", condition);
414 vm_abort("emit_branch: unknown condition %d", condition);
423 * Trace functions. Implement -verbose:call flag
424 * code marked by real NOP, but performance is no matter when using -verbose:call :)
426 void emit_verbosecall_enter(jitdata* jd)
433 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
436 /* get required compiler data */
442 /* mark trace code */
449 M_AMOV(REG_SP, REG_A0); /* simpyfy stack offset calculation */
451 #if !defined(ENABLE_SOFTFLOAT)
452 M_AADD_IMM(-8*2, REG_SP);
453 M_FSTORE(REG_F0, REG_SP, 8);
454 M_FSTORE(REG_F1, REG_SP, 0);
457 M_AADD_IMM(4*4 + 4, REG_A0);
458 M_APUSH(REG_A0); /* third arg is original argument stack */
459 M_IPUSH_IMM(0); /* second arg is number of argument registers (=0) */
460 M_IPUSH_IMM(m); /* first arg is methodpointer */
462 M_JSR_IMM(trace_java_call_enter);
463 /* pop arguments off stack */
464 M_AADD_IMM(3*4, REG_SP);
466 #if !defined(ENABLE_SOFTFLOAT)
467 M_FSTORE(REG_F1, REG_SP, 0);
468 M_FSTORE(REG_F0, REG_SP, 8);
469 M_AADD_IMM(8*2, REG_SP);
479 void emit_verbosecall_exit(jitdata* jd)
486 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
489 /* get required compiler data */
495 /* void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m); */
497 /* void trace_java_call_exit(methodinfo *m, uint64_t *return_regs) */
499 /* mark trace code */
502 /* to store result on stack */
503 M_AADD_IMM(-8, REG_SP); /* create space for array */
505 switch (md->returntype.type) {
508 #if defined(ENABLE_SOFTFLOAT)
511 M_IST(REG_D0, REG_SP, 0);
515 #if defined(ENABLE_SOFTFLOAT)
518 M_LST(REG_D1, REG_SP, 0);
521 #if !defined(ENABLE_SOFTFLOAT)
522 case TYPE_FLT: /* FIXME */
523 case TYPE_DBL: /* FIXME */
526 case TYPE_VOID: /* nothing */
533 M_APUSH(REG_SP); /* push address of array */
534 M_IPUSH_IMM(m); /* push methodinfo */
535 M_JSR_IMM(trace_java_call_exit);
537 M_AADD_IMM(8, REG_SP); /* remove args */
538 /* poping result registers from stack */
539 switch (md->returntype.type) {
542 #if defined(ENABLE_SOFTFLOAT)
545 M_ILD(REG_D0, REG_SP, 0);
549 #if defined(ENABLE_SOFTFLOAT)
552 M_LLD(REG_D1, REG_SP, 0);
555 #if !defined(ENABLE_SOFTFLOAT)
556 case TYPE_FLT: /* FIXME */
557 case TYPE_DBL: /* FIXME */
560 case TYPE_VOID: /* nothing */
567 M_AADD_IMM(8, REG_SP); /* remove space for array */
573 /* emit_classcast_check ********************************************************
575 Emit a ClassCastException check.
577 *******************************************************************************/
579 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
581 if (INSTRUCTION_MUST_CHECK(iptr)) {
596 vm_abort("emit_classcast_check: unknown condition %d", condition);
598 M_TRAP_SETREGISTER(s1);
599 M_TRAP(TRAP_ClassCastException);
603 /* emit_arrayindexoutofbounds_check ********************************************
605 Emit a ArrayIndexOutOfBoundsException check.
607 *******************************************************************************/
608 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
610 if (INSTRUCTION_MUST_CHECK(iptr)) {
611 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
612 M_ICMP(s2, REG_ITMP3);
614 M_TRAP_SETREGISTER(s2);
615 M_TRAP(TRAP_ArrayIndexOutOfBoundsException);
620 /* emit_arraystore_check *******************************************************
622 Emit an ArrayStoreException check.
624 *******************************************************************************/
626 void emit_arraystore_check(codegendata *cd, instruction *iptr)
628 if (INSTRUCTION_MUST_CHECK(iptr)) {
631 M_TRAP(TRAP_ArrayStoreException);
636 /* emit_nullpointer_check ******************************************************
638 Emit a NullPointerException check.
640 *******************************************************************************/
642 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
644 if (INSTRUCTION_MUST_CHECK(iptr)) {
645 /* XXX: this check is copied to call monitor_enter
646 * invocation at the beginning of codegen.c */
649 M_TRAP(TRAP_NullPointerException);
653 /* emit_arithmetic_check *******************************************************
655 Emit an ArithmeticException check.
657 *******************************************************************************/
659 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
661 if (INSTRUCTION_MUST_CHECK(iptr)) {
664 M_TRAP(TRAP_ArithmeticException);
668 /* emit_exception_check_ireg **************************************************
670 Emit an Exception check. Teste register is integer REG_RESULT
672 *******************************************************************************/
673 void emit_exception_check(codegendata *cd, instruction *iptr)
675 if (INSTRUCTION_MUST_CHECK(iptr)) {
678 M_TRAP(TRAP_CHECK_EXCEPTION);
682 /* emit_trap_compiler **********************************************************
684 Emit a trap instruction which calls the JIT compiler.
686 *******************************************************************************/
688 void emit_trap_compiler(codegendata *cd)
690 M_TRAP_SETREGISTER(REG_METHODPTR);
691 M_TRAP(TRAP_COMPILER);
695 /* emit_trap *******************************************************************
697 Emit a trap instruction and return the original machine code.
699 *******************************************************************************/
701 uint32_t emit_trap(codegendata *cd)
705 /* Get machine code which is patched back in later. The
706 trap is 2 bytes long. */
708 mcode = *((uint32_t *) cd->mcodeptr);
710 M_TRAP(TRAP_PATCHER);
717 * Emit code to recompute the procedure vector.
719 void emit_recompute_pv(codegendata *cd)
721 // This is a nop, because we do not use a procedure vector.
726 * Generates synchronization code to enter a monitor.
728 #if defined(ENABLE_THREADS)
729 void emit_monitor_enter(jitdata* jd, int32_t syncslot_offset)
731 // Get required compiler data.
732 methodinfo* m = jd->m;
733 codegendata* cd = jd->cd;
735 if (m->flags & ACC_STATIC) {
736 M_AMOV_IMM((&m->clazz->object.header), REG_ATMP1);
738 /* for non-static case the first arg is the object */
739 M_ALD(REG_ATMP1, REG_SP, cd->stackframesize + 4);
742 M_TRAP(TRAP_NullPointerException);
745 M_AST(REG_ATMP1, REG_SP, syncslot_offset);
746 M_AST(REG_ATMP1, REG_SP, 0 * 4);
747 M_JSR_IMM(LOCK_monitor_enter);
753 * Generates synchronization code to leave a monitor.
755 #if defined(ENABLE_THREADS)
756 void emit_monitor_exit(jitdata* jd, int32_t syncslot_offset)
758 // Get required compiler data.
759 methodinfo* m = jd->m;
760 codegendata* cd = jd->cd;
762 M_ILD(REG_ITMP3, REG_SP, syncslot_offset);
764 /* we need to save the proper return value */
765 /* we do not care for the long -> doubel convert space here */
767 methoddesc* md = m->parseddesc;
769 switch (md->returntype.type) {
770 #if defined(ENABLE_SOFTFLOAT)
774 M_LST(REG_RESULT_PACKED, REG_SP, syncslot_offset + 8);
776 #if defined(ENABLE_SOFTFLOAT)
781 M_IST(REG_RESULT , REG_SP, syncslot_offset + 8);
783 #if !defined(ENABLE_SOFTFLOAT)
785 M_FST(REG_FRESULT, REG_SP, syncslot_offset + 8);
788 M_DST(REG_FRESULT, REG_SP, syncslot_offset + 8);
793 M_IST(REG_ITMP3, REG_SP, 0 * 4);
794 M_JSR_IMM(LOCK_monitor_exit);
796 /* and now restore the proper return value */
798 switch (md->returntype.type) {
800 #if defined(ENABLE_SOFTFLOAT)
804 M_LLD(REG_RESULT_PACKED, REG_SP, syncslot_offset + 8);
806 #if defined(ENABLE_SOFTFLOAT)
811 M_ILD(REG_RESULT , REG_SP, syncslot_offset + 8);
813 #if !defined(ENABLE_SOFTFLOAT)
815 M_FLD(REG_FRESULT, REG_SP, syncslot_offset + 8);
818 M_DLD(REG_FRESULT, REG_SP, syncslot_offset + 8);
827 * These are local overrides for various environment variables in Emacs.
828 * Please do not remove this and leave it at the end of the file, where
829 * Emacs will automagically detect them.
830 * ---------------------------------------------------------------------
833 * indent-tabs-mode: t
837 * vim:noexpandtab:sw=4:ts=4: