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/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.h"
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);
313 /* emit_branch *****************************************************************
315 Emits the code for conditional and unconditional branchs.
317 *******************************************************************************/
318 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
320 /* calculate the different displacements */
321 /* PC is a at branch instruction + 2 */
322 /* coditional and uncondition branching work the same way */
323 /* short branches have signed 16 bit offset */
324 /* long branches are signed 32 bit */
325 /* the 8 bit offset branching instructions are not used */
329 /* check displacement for overflow */
330 if ((disp & 0x0000FFFF) != disp) {
331 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
332 cd->flags |= (CODEGENDATA_FLAG_ERROR | CODEGENDATA_FLAG_LONGBRANCHES);
336 /* check which branch to generate */
338 if (condition == BRANCH_UNCONDITIONAL) {
339 if (CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
345 if (CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
373 vm_abort("emit_branch: unknown condition %d", condition);
402 vm_abort("emit_branch: unknown condition %d", condition);
411 * Trace functions. Implement -verbose:call flag
412 * code marked by real NOP, but performance is no matter when using -verbose:call :)
414 void emit_verbosecall_enter(jitdata* jd)
421 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
424 /* get required compiler data */
430 /* mark trace code */
437 M_AMOV(REG_SP, REG_A0); /* simpyfy stack offset calculation */
439 #if !defined(ENABLE_SOFTFLOAT)
440 M_AADD_IMM(-8*2, REG_SP);
441 M_FSTORE(REG_F0, REG_SP, 8);
442 M_FSTORE(REG_F1, REG_SP, 0);
445 M_AADD_IMM(4*4 + 4, REG_A0);
446 M_APUSH(REG_A0); /* third arg is original argument stack */
447 M_IPUSH_IMM(0); /* second arg is number of argument registers (=0) */
448 M_IPUSH_IMM(m); /* first arg is methodpointer */
450 M_JSR_IMM(trace_java_call_enter);
451 /* pop arguments off stack */
452 M_AADD_IMM(3*4, REG_SP);
454 #if !defined(ENABLE_SOFTFLOAT)
455 M_FSTORE(REG_F1, REG_SP, 0);
456 M_FSTORE(REG_F0, REG_SP, 8);
457 M_AADD_IMM(8*2, REG_SP);
467 void emit_verbosecall_exit(jitdata* jd)
474 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
477 /* get required compiler data */
483 /* void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m); */
485 /* void trace_java_call_exit(methodinfo *m, uint64_t *return_regs) */
487 /* mark trace code */
490 /* to store result on stack */
491 M_AADD_IMM(-8, REG_SP); /* create space for array */
493 switch (md->returntype.type) {
496 #if defined(ENABLE_SOFTFLOAT)
499 M_IST(REG_D0, REG_SP, 0);
503 #if defined(ENABLE_SOFTFLOAT)
506 M_LST(REG_D1, REG_SP, 0);
509 #if !defined(ENABLE_SOFTFLOAT)
510 case TYPE_FLT: /* FIXME */
511 case TYPE_DBL: /* FIXME */
514 case TYPE_VOID: /* nothing */
521 M_APUSH(REG_SP); /* push address of array */
522 M_IPUSH_IMM(m); /* push methodinfo */
523 M_JSR_IMM(trace_java_call_exit);
525 M_AADD_IMM(8, REG_SP); /* remove args */
526 /* poping result registers from stack */
527 switch (md->returntype.type) {
530 #if defined(ENABLE_SOFTFLOAT)
533 M_ILD(REG_D0, REG_SP, 0);
537 #if defined(ENABLE_SOFTFLOAT)
540 M_LLD(REG_D1, REG_SP, 0);
543 #if !defined(ENABLE_SOFTFLOAT)
544 case TYPE_FLT: /* FIXME */
545 case TYPE_DBL: /* FIXME */
548 case TYPE_VOID: /* nothing */
555 M_AADD_IMM(8, REG_SP); /* remove space for array */
561 /* emit_classcast_check ********************************************************
563 Emit a ClassCastException check.
565 *******************************************************************************/
567 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
569 if (INSTRUCTION_MUST_CHECK(iptr)) {
584 vm_abort("emit_classcast_check: unknown condition %d", condition);
586 M_TRAP_SETREGISTER(s1);
587 M_TRAP(TRAP_ClassCastException);
591 /* emit_arrayindexoutofbounds_check ********************************************
593 Emit a ArrayIndexOutOfBoundsException check.
595 *******************************************************************************/
596 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
598 if (INSTRUCTION_MUST_CHECK(iptr)) {
599 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
600 M_ICMP(s2, REG_ITMP3);
602 M_TRAP_SETREGISTER(s2);
603 M_TRAP(TRAP_ArrayIndexOutOfBoundsException);
608 /* emit_arraystore_check *******************************************************
610 Emit an ArrayStoreException check.
612 *******************************************************************************/
614 void emit_arraystore_check(codegendata *cd, instruction *iptr)
616 if (INSTRUCTION_MUST_CHECK(iptr)) {
619 M_TRAP(TRAP_ArrayStoreException);
624 /* emit_nullpointer_check ******************************************************
626 Emit a NullPointerException check.
628 *******************************************************************************/
630 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
632 if (INSTRUCTION_MUST_CHECK(iptr)) {
633 /* XXX: this check is copied to call monitor_enter
634 * invocation at the beginning of codegen.c */
637 M_TRAP(TRAP_NullPointerException);
641 /* emit_arithmetic_check *******************************************************
643 Emit an ArithmeticException check.
645 *******************************************************************************/
647 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
649 if (INSTRUCTION_MUST_CHECK(iptr)) {
652 M_TRAP(TRAP_ArithmeticException);
656 /* emit_exception_check_ireg **************************************************
658 Emit an Exception check. Teste register is integer REG_RESULT
660 *******************************************************************************/
661 void emit_exception_check(codegendata *cd, instruction *iptr)
663 if (INSTRUCTION_MUST_CHECK(iptr)) {
666 M_TRAP(TRAP_CHECK_EXCEPTION);
670 /* emit_trap_compiler **********************************************************
672 Emit a trap instruction which calls the JIT compiler.
674 *******************************************************************************/
676 void emit_trap_compiler(codegendata *cd)
678 M_TRAP_SETREGISTER(REG_METHODPTR);
679 M_TRAP(TRAP_COMPILER);
683 /* emit_trap *******************************************************************
685 Emit a trap instruction and return the original machine code.
687 *******************************************************************************/
689 uint32_t emit_trap(codegendata *cd)
693 /* Get machine code which is patched back in later. The
694 trap is 2 bytes long. */
696 mcode = *((uint32_t *) cd->mcodeptr);
698 M_TRAP(TRAP_PATCHER);
706 * These are local overrides for various environment variables in Emacs.
707 * Please do not remove this and leave it at the end of the file, where
708 * Emacs will automagically detect them.
709 * ---------------------------------------------------------------------
712 * indent-tabs-mode: t
716 * vim:noexpandtab:sw=4:ts=4: