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.h"
35 #include "vm/builtin.h"
36 #include "vm/exceptions.h"
38 #include "vm/jit/asmpart.h"
39 #include "vm/jit/emit-common.h"
40 #include "vm/jit/trace.hpp"
41 #include "vm/jit/trap.h"
44 /* emit_mov_imm_reg **************************************************************************
46 * Loads an immededat operand into an integer data register
48 ********************************************************************************************/
49 void emit_mov_imm_reg (codegendata *cd, s4 imm, s4 dreg)
51 /* FIXME: -1 can be used as byte form 0xff, but this ifs cascade is plain wrong it seems */
53 if ( (imm & 0x0000007F) == imm) {
55 *((s2*)cd->mcodeptr) = 0x7000 | (dreg << 9) | imm; /* MOVEQ.L */
57 } else if ((imm & 0x00007FFF) == imm) {
59 OPWORD( ((7<<6) | (dreg << 3) | 5), 7, 4); /* MVS.W */
60 *((s2*)cd->mcodeptr) = (s2)imm;
64 OPWORD( ((2<<6) | (dreg << 3) | 0), 7, 4);
65 *((s4*)cd->mcodeptr) = (s4)imm;
71 /* emit_copy *******************************************************************
73 Generates a register/memory to register/memory copy.
75 *******************************************************************************/
77 void emit_copy(jitdata *jd, instruction *iptr)
84 /* get required compiler data */
88 /* get source and destination variables */
90 src = VAROP(iptr->s1);
91 dst = VAROP(iptr->dst);
93 if ((src->vv.regoff != dst->vv.regoff) ||
94 (IS_INMEMORY(src->flags ^ dst->flags))) {
96 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
97 /* emit nothing, as the value won't be used anyway */
101 /* If one of the variables resides in memory, we can eliminate
102 the register move from/to the temporary register with the
103 order of getting the destination register and the load. */
105 if (IS_INMEMORY(src->flags)) {
106 if (IS_LNG_TYPE(src->type))
107 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
109 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
111 s1 = emit_load(jd, iptr, src, d);
114 if (IS_LNG_TYPE(src->type))
115 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
117 s1 = emit_load(jd, iptr, src, REG_IFTMP);
119 d = codegen_reg_of_var(iptr->opc, dst, s1);
124 case TYPE_INT: M_INTMOVE(s1, d); break;
125 case TYPE_ADR: M_ADRMOVE(s1, d); break;
126 case TYPE_LNG: M_LNGMOVE(s1, d); break;
127 #if !defined(ENABLE_SOFTFLOAT)
128 case TYPE_FLT: M_FLTMOVE(s1, d); break;
129 case TYPE_DBL: M_DBLMOVE(s1, d); break;
131 case TYPE_FLT: M_INTMOVE(s1, d); break;
132 case TYPE_DBL: M_LNGMOVE(s1, d); break;
135 vm_abort("emit_copy: unknown type %d", src->type);
139 emit_store(jd, iptr, dst, d);
144 /* emit_store ******************************************************************
146 Emits a possible store of the destination operand.
148 *******************************************************************************/
150 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
154 /* get required compiler data */
158 if (IS_INMEMORY(dst->flags)) {
162 #if defined(ENABLE_SOFTFLOAT)
166 M_LST(d, REG_SP, dst->vv.regoff);
168 #if defined(ENABLE_SOFTFLOAT)
172 M_IST(d, REG_SP, dst->vv.regoff);
175 M_AST(d, REG_SP, dst->vv.regoff);
177 #if !defined(ENABLE_SOFTFLOAT)
179 M_DST(d, REG_SP, dst->vv.regoff);
182 M_FST(d, REG_SP, dst->vv.regoff);
186 vm_abort("emit_store: unknown type %d", dst->type);
192 /* emit_load *******************************************************************
194 Emits a possible load of an operand.
196 *******************************************************************************/
198 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
204 /* get required compiler data */
208 if (IS_INMEMORY(src->flags)) {
211 disp = src->vv.regoff;
214 #if defined(ENABLE_SOFTFLOAT)
218 M_ILD(tempreg, REG_SP, disp);
220 #if defined(ENABLE_SOFTFLOAT)
224 M_LLD(tempreg, REG_SP, disp);
227 M_ALD(tempreg, REG_SP, disp);
229 #if !defined(ENABLE_SOFTFLOAT)
231 M_FLD(tempreg, REG_SP, disp);
234 M_DLD(tempreg, REG_SP, disp);
238 vm_abort("emit_load: unknown type %d", src->type);
241 if (IS_FLT_DBL_TYPE(src->type)) {
242 if (IS_2_WORD_TYPE(src->type)) {
243 M_DLD(tempreg, REG_SP, disp);
245 M_FLD(tempreg, REG_SP, disp);
248 if (IS_2_WORD_TYPE(src->type)) {
249 M_LLD(tempreg, REG_SP, disp);
251 M_ILD(tempreg, REG_SP, disp);
259 reg = src->vv.regoff;
264 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
270 #if !defined(ENABLE_SOFTFLOAT)
271 assert(src->type == TYPE_LNG);
273 assert(src->type == TYPE_LNG || src->type == TYPE_DBL);
276 /* get required compiler data */
279 if (IS_INMEMORY(src->flags)) {
282 disp = src->vv.regoff;
283 M_ILD(tempreg, REG_SP, disp + 4);
286 reg = GET_LOW_REG(src->vv.regoff);
290 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
296 #if !defined(ENABLE_SOFTFLOAT)
297 assert(src->type == TYPE_LNG);
299 assert(src->type == TYPE_LNG || src->type == TYPE_DBL);
301 /* get required compiler data */
304 if (IS_INMEMORY(src->flags)) {
306 disp = src->vv.regoff;
307 M_ILD(tempreg, REG_SP, disp);
310 reg = GET_HIGH_REG(src->vv.regoff);
314 /* emit_branch *****************************************************************
316 Emits the code for conditional and unconditional branchs.
318 *******************************************************************************/
319 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
321 /* calculate the different displacements */
322 /* PC is a at branch instruction + 2 */
323 /* coditional and uncondition branching work the same way */
324 /* short branches have signed 16 bit offset */
325 /* long branches are signed 32 bit */
326 /* the 8 bit offset branching instructions are not used */
330 /* check displacement for overflow */
331 if ((disp & 0x0000FFFF) != disp) {
332 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
333 cd->flags |= (CODEGENDATA_FLAG_ERROR | CODEGENDATA_FLAG_LONGBRANCHES);
337 /* check which branch to generate */
339 if (condition == BRANCH_UNCONDITIONAL) {
340 if (CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
346 if (CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
374 vm_abort("emit_branch: unknown condition %d", condition);
403 vm_abort("emit_branch: unknown condition %d", condition);
412 * Trace functions. Implement -verbose:call flag
413 * code marked by real NOP, but performance is no matter when using -verbose:call :)
415 void emit_verbosecall_enter(jitdata* jd)
422 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
425 /* get required compiler data */
431 /* mark trace code */
438 M_AMOV(REG_SP, REG_A0); /* simpyfy stack offset calculation */
440 #if !defined(ENABLE_SOFTFLOAT)
441 M_AADD_IMM(-8*2, REG_SP);
442 M_FSTORE(REG_F0, REG_SP, 8);
443 M_FSTORE(REG_F1, REG_SP, 0);
446 M_AADD_IMM(4*4 + 4, REG_A0);
447 M_APUSH(REG_A0); /* third arg is original argument stack */
448 M_IPUSH_IMM(0); /* second arg is number of argument registers (=0) */
449 M_IPUSH_IMM(m); /* first arg is methodpointer */
451 M_JSR_IMM(trace_java_call_enter);
452 /* pop arguments off stack */
453 M_AADD_IMM(3*4, REG_SP);
455 #if !defined(ENABLE_SOFTFLOAT)
456 M_FSTORE(REG_F1, REG_SP, 0);
457 M_FSTORE(REG_F0, REG_SP, 8);
458 M_AADD_IMM(8*2, REG_SP);
468 void emit_verbosecall_exit(jitdata* jd)
475 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
478 /* get required compiler data */
484 /* void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m); */
486 /* void trace_java_call_exit(methodinfo *m, uint64_t *return_regs) */
488 /* mark trace code */
491 /* to store result on stack */
492 M_AADD_IMM(-8, REG_SP); /* create space for array */
494 switch (md->returntype.type) {
497 #if defined(ENABLE_SOFTFLOAT)
500 M_IST(REG_D0, REG_SP, 0);
504 #if defined(ENABLE_SOFTFLOAT)
507 M_LST(REG_D1, REG_SP, 0);
510 #if !defined(ENABLE_SOFTFLOAT)
511 case TYPE_FLT: /* FIXME */
512 case TYPE_DBL: /* FIXME */
515 case TYPE_VOID: /* nothing */
522 M_APUSH(REG_SP); /* push address of array */
523 M_IPUSH_IMM(m); /* push methodinfo */
524 M_JSR_IMM(trace_java_call_exit);
526 M_AADD_IMM(8, REG_SP); /* remove args */
527 /* poping result registers from stack */
528 switch (md->returntype.type) {
531 #if defined(ENABLE_SOFTFLOAT)
534 M_ILD(REG_D0, REG_SP, 0);
538 #if defined(ENABLE_SOFTFLOAT)
541 M_LLD(REG_D1, REG_SP, 0);
544 #if !defined(ENABLE_SOFTFLOAT)
545 case TYPE_FLT: /* FIXME */
546 case TYPE_DBL: /* FIXME */
549 case TYPE_VOID: /* nothing */
556 M_AADD_IMM(8, REG_SP); /* remove space for array */
562 /* emit_classcast_check ********************************************************
564 Emit a ClassCastException check.
566 *******************************************************************************/
568 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
570 if (INSTRUCTION_MUST_CHECK(iptr)) {
585 vm_abort("emit_classcast_check: unknown condition %d", condition);
587 M_TRAP_SETREGISTER(s1);
588 M_TRAP(TRAP_ClassCastException);
592 /* emit_arrayindexoutofbounds_check ********************************************
594 Emit a ArrayIndexOutOfBoundsException check.
596 *******************************************************************************/
597 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
599 if (INSTRUCTION_MUST_CHECK(iptr)) {
600 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
601 M_ICMP(s2, REG_ITMP3);
603 M_TRAP_SETREGISTER(s2);
604 M_TRAP(TRAP_ArrayIndexOutOfBoundsException);
609 /* emit_arraystore_check *******************************************************
611 Emit an ArrayStoreException check.
613 *******************************************************************************/
615 void emit_arraystore_check(codegendata *cd, instruction *iptr)
617 if (INSTRUCTION_MUST_CHECK(iptr)) {
620 M_TRAP(TRAP_ArrayStoreException);
625 /* emit_nullpointer_check ******************************************************
627 Emit a NullPointerException check.
629 *******************************************************************************/
631 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
633 if (INSTRUCTION_MUST_CHECK(iptr)) {
634 /* XXX: this check is copied to call monitor_enter
635 * invocation at the beginning of codegen.c */
638 M_TRAP(TRAP_NullPointerException);
642 /* emit_arithmetic_check *******************************************************
644 Emit an ArithmeticException check.
646 *******************************************************************************/
648 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
650 if (INSTRUCTION_MUST_CHECK(iptr)) {
653 M_TRAP(TRAP_ArithmeticException);
657 /* emit_exception_check_ireg **************************************************
659 Emit an Exception check. Teste register is integer REG_RESULT
661 *******************************************************************************/
662 void emit_exception_check(codegendata *cd, instruction *iptr)
664 if (INSTRUCTION_MUST_CHECK(iptr)) {
667 M_TRAP(TRAP_CHECK_EXCEPTION);
671 /* emit_trap_compiler **********************************************************
673 Emit a trap instruction which calls the JIT compiler.
675 *******************************************************************************/
677 void emit_trap_compiler(codegendata *cd)
679 M_TRAP_SETREGISTER(REG_METHODPTR);
680 M_TRAP(TRAP_COMPILER);
684 /* emit_trap *******************************************************************
686 Emit a trap instruction and return the original machine code.
688 *******************************************************************************/
690 uint32_t emit_trap(codegendata *cd)
694 /* Get machine code which is patched back in later. The
695 trap is 2 bytes long. */
697 mcode = *((uint32_t *) cd->mcodeptr);
699 M_TRAP(TRAP_PATCHER);
707 * These are local overrides for various environment variables in Emacs.
708 * Please do not remove this and leave it at the end of the file, where
709 * Emacs will automagically detect them.
710 * ---------------------------------------------------------------------
713 * indent-tabs-mode: t
717 * vim:noexpandtab:sw=4:ts=4: