1 /* src/vm/jit/m68k/emit.c
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
33 #include "vm/jit/emit-common.h"
34 #include "vm/exceptions.h"
35 #include "vm/jit/asmpart.h"
36 #include "vm/builtin.h"
38 #include "mm/memory.h"
40 #include "threads/lock-common.h"
45 /* emit_mov_imm_reg **************************************************************************
47 * Loads an immededat operand into an integer data register
49 ********************************************************************************************/
50 void emit_mov_imm_reg (codegendata *cd, s4 imm, s4 dreg)
52 /* FIXME: -1 can be used as byte form 0xff, but this ifs cascade is plain wrong it seems */
54 if ( (imm & 0x0000007F) == imm) {
56 *((s2*)cd->mcodeptr) = 0x7000 | (dreg << 9) | imm; /* MOVEQ.L */
58 } else if ((imm & 0x00007FFF) == imm) {
60 OPWORD( ((7<<6) | (dreg << 3) | 5), 7, 4); /* MVS.W */
61 *((s2*)cd->mcodeptr) = (s2)imm;
65 OPWORD( ((2<<6) | (dreg << 3) | 0), 7, 4);
66 *((s4*)cd->mcodeptr) = (s4)imm;
72 /* emit_copy *******************************************************************
74 Generates a register/memory to register/memory copy.
76 *******************************************************************************/
78 void emit_copy(jitdata *jd, instruction *iptr)
85 /* get required compiler data */
89 /* get source and destination variables */
91 src = VAROP(iptr->s1);
92 dst = VAROP(iptr->dst);
94 if ((src->vv.regoff != dst->vv.regoff) ||
95 (IS_INMEMORY(src->flags ^ dst->flags))) {
97 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
98 /* emit nothing, as the value won't be used anyway */
102 /* If one of the variables resides in memory, we can eliminate
103 the register move from/to the temporary register with the
104 order of getting the destination register and the load. */
106 if (IS_INMEMORY(src->flags)) {
107 if (IS_LNG_TYPE(src->type))
108 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
110 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
112 s1 = emit_load(jd, iptr, src, d);
115 if (IS_LNG_TYPE(src->type))
116 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
118 s1 = emit_load(jd, iptr, src, REG_IFTMP);
120 d = codegen_reg_of_var(iptr->opc, dst, s1);
125 case TYPE_INT: M_INTMOVE(s1, d); break;
126 case TYPE_ADR: M_ADRMOVE(s1, d); break;
127 case TYPE_LNG: M_LNGMOVE(s1, d); break;
128 #if !defined(ENABLE_SOFTFLOAT)
129 case TYPE_FLT: M_FLTMOVE(s1, d); break;
130 case TYPE_DBL: M_DBLMOVE(s1, d); break;
132 case TYPE_FLT: M_INTMOVE(s1, d); break;
133 case TYPE_DBL: M_LNGMOVE(s1, d); break;
136 vm_abort("emit_copy: unknown type %d", src->type);
140 emit_store(jd, iptr, dst, d);
145 /* emit_store ******************************************************************
147 Emits a possible store of the destination operand.
149 *******************************************************************************/
151 inline void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
155 /* get required compiler data */
159 if (IS_INMEMORY(dst->flags)) {
163 #if defined(ENABLE_SOFTFLOAT)
167 M_LST(d, REG_SP, dst->vv.regoff);
169 #if defined(ENABLE_SOFTFLOAT)
173 M_IST(d, REG_SP, dst->vv.regoff);
176 M_AST(d, REG_SP, dst->vv.regoff);
178 #if !defined(ENABLE_SOFTFLOAT)
180 M_DST(d, REG_SP, dst->vv.regoff);
183 M_FST(d, REG_SP, dst->vv.regoff);
187 vm_abort("emit_store: unknown type %d", dst->type);
193 /* emit_load *******************************************************************
195 Emits a possible load of an operand.
197 *******************************************************************************/
199 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
205 /* get required compiler data */
209 if (IS_INMEMORY(src->flags)) {
212 disp = src->vv.regoff;
215 #if defined(ENABLE_SOFTFLOAT)
219 M_ILD(tempreg, REG_SP, disp);
221 #if defined(ENABLE_SOFTFLOAT)
225 M_LLD(tempreg, REG_SP, disp);
228 M_ALD(tempreg, REG_SP, disp);
230 #if !defined(ENABLE_SOFTFLOAT)
232 M_FLD(tempreg, REG_SP, disp);
235 M_DLD(tempreg, REG_SP, disp);
239 vm_abort("emit_load: unknown type %d", src->type);
242 if (IS_FLT_DBL_TYPE(src->type)) {
243 if (IS_2_WORD_TYPE(src->type)) {
244 M_DLD(tempreg, REG_SP, disp);
246 M_FLD(tempreg, REG_SP, disp);
249 if (IS_2_WORD_TYPE(src->type)) {
250 M_LLD(tempreg, REG_SP, disp);
252 M_ILD(tempreg, REG_SP, disp);
260 reg = src->vv.regoff;
265 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
271 #if !defined(ENABLE_SOFTFLOAT)
272 assert(src->type == TYPE_LNG);
274 assert(src->type == TYPE_LNG || src->type == TYPE_DBL);
277 /* get required compiler data */
280 if (IS_INMEMORY(src->flags)) {
283 disp = src->vv.regoff;
284 M_ILD(tempreg, REG_SP, disp + 4);
287 reg = GET_LOW_REG(src->vv.regoff);
291 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
297 #if !defined(ENABLE_SOFTFLOAT)
298 assert(src->type == TYPE_LNG);
300 assert(src->type == TYPE_LNG || src->type == TYPE_DBL);
302 /* get required compiler data */
305 if (IS_INMEMORY(src->flags)) {
307 disp = src->vv.regoff;
308 M_ILD(tempreg, REG_SP, disp);
311 reg = GET_HIGH_REG(src->vv.regoff);
315 /* emit_branch *****************************************************************
317 Emits the code for conditional and unconditional branchs.
319 *******************************************************************************/
320 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
322 /* calculate the different displacements */
323 /* PC is a at branch instruction + 2 */
324 /* coditional and uncondition branching work the same way */
325 /* short branches have signed 16 bit offset */
326 /* long branches are signed 32 bit */
327 /* the 8 bit offset branching instructions are not used */
331 /* check displacement for overflow */
332 if ((disp & 0x0000FFFF) != disp) {
333 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
334 cd->flags |= (CODEGENDATA_FLAG_ERROR | CODEGENDATA_FLAG_LONGBRANCHES);
338 /* check which branch to generate */
340 if (condition == BRANCH_UNCONDITIONAL) {
341 if (CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
347 if (CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
375 vm_abort("emit_branch: unknown condition %d", condition);
404 vm_abort("emit_branch: unknown condition %d", condition);
413 * Trace functions. Implement -verbose:call flag
414 * code marked by real NOP, but performance is no matter when using -verbose:call :)
416 void emit_verbosecall_enter(jitdata* jd)
425 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
428 /* get required compiler data */
434 /* mark trace code */
442 #if !defined(ENABLE_SOFTFLOAT)
443 M_AADD_IMM(-8*2, REG_SP);
444 M_FSTORE(REG_F0, REG_SP, 8);
445 M_FSTORE(REG_F1, REG_SP, 0);
447 disp = 4*4 + 8*2 + 4; /* points to old argument stack initially */
451 /* builtin_verbosecall_enter takes all args as s8 type */
452 /* TRACE_ARGS_NUM is the number of args the builtin_verbosecall_enter expects */
456 /* travel up stack to the first argument of the function which needs to be copied */
457 for (i=0; (i < md->paramcount) && (i < TRACE_ARGS_NUM); i++) {
461 /* disp now points to the first arg which gets copied to the trace stack, relative to REG_SP! */
462 for (i=TRACE_ARGS_NUM-1; i>=0; --i) {
463 if (i < md->paramcount) {
464 /* traced function has such an argument */
465 t = md->paramtypes[i].type;
467 /* copy from original argument stack */
468 M_ILD(REG_ITMP1, REG_SP, disp);
469 M_ILD(REG_ITMP2, REG_SP, disp-4);
473 /* function has no arg here, push nothing and adapt displacement */
479 M_JSR_IMM(builtin_verbosecall_enter);
480 /* pop arguments off stack */
481 M_AADD_IMM(TRACE_ARGS_NUM*8+4, REG_SP);
483 #if !defined(ENABLE_SOFTFLOAT)
484 M_FSTORE(REG_F1, REG_SP, 0);
485 M_FSTORE(REG_F0, REG_SP, 8);
486 M_AADD_IMM(8*2, REG_SP);
496 void emit_verbosecall_exit(jitdata* jd)
503 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
506 /* get required compiler data */
512 /* void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m); */
515 /* mark trace code */
518 #if !defined(ENABLE_SOFTFLOAT)
519 M_AADD_IMM(-8, REG_SP);
520 M_FSTORE(REG_D1, REG_SP, 0);
523 M_IPUSH_IMM(m); /* push methodinfo */
525 #if !defined(ENABLE_SOFTFLOAT)
526 M_AADD_IMM(-3*4, REG_SP);
527 M_FST(REG_D0, REG_SP, 8);
528 M_DST(REG_D0, REG_SP, 0);
536 M_IPUSH(GET_HIGH_REG(REG_RESULT_PACKED))
537 M_IPUSH(GET_LOW_REG(REG_RESULT_PACKED)) /* push long result */
539 M_JSR_IMM(builtin_verbosecall_exit);
541 /* poping result registers from stack */
542 M_IPOP(GET_LOW_REG(REG_RESULT_PACKED))
543 M_IPOP(GET_HIGH_REG(REG_RESULT_PACKED))
545 #if !defined(ENABLE_SOFTFLOAT)
546 M_DLD(REG_D0, REG_SP, 0)
547 M_FLD(REG_D0, REG_SP, 8)
549 M_AADD_IMM(3*4 + 4, REG_SP);
551 #if !defined(ENABLE_SOFTFLOAT)
552 M_FLOAD(REG_D1, REG_SP, 0)
553 M_AADD_IMM(8, REG_SP);
560 /* emit_classcast_check ********************************************************
562 Emit a ClassCastException check.
564 *******************************************************************************/
566 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
568 if (INSTRUCTION_MUST_CHECK(iptr)) {
583 vm_abort("emit_classcast_check: unknown condition %d", condition);
585 M_TRAP_SETREGISTER(s1);
586 M_TRAP(EXCEPTION_HARDWARE_CLASSCAST);
590 /* emit_arrayindexoutofbounds_check ********************************************
592 Emit a ArrayIndexOutOfBoundsException check.
594 *******************************************************************************/
595 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
597 if (INSTRUCTION_MUST_CHECK(iptr)) {
598 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
599 M_ICMP(s2, REG_ITMP3);
601 M_TRAP_SETREGISTER(s2);
602 M_TRAP(EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS);
607 /* emit_arraystore_check *******************************************************
609 Emit an ArrayStoreException check.
611 *******************************************************************************/
613 void emit_arraystore_check(codegendata *cd, instruction *iptr)
615 if (INSTRUCTION_MUST_CHECK(iptr)) {
618 /*M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_ARRAYSTORE);*/
619 M_TRAP(EXCEPTION_HARDWARE_ARRAYSTORE);
624 /* emit_nullpointer_check ******************************************************
626 Emit a NullPointerException check.
628 *******************************************************************************/
629 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
631 if (INSTRUCTION_MUST_CHECK(iptr)) {
632 /* XXX: this check is copied to call monitor_enter
633 * invocation at the beginning of codegen.c */
636 M_TRAP(M68K_EXCEPTION_HARDWARE_NULLPOINTER);
640 /* emit_arithmetic_check *******************************************************
642 Emit an ArithmeticException check.
644 *******************************************************************************/
646 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
648 if (INSTRUCTION_MUST_CHECK(iptr)) {
651 M_TRAP(EXCEPTION_HARDWARE_ARITHMETIC);
655 /* emit_exception_check_ireg **************************************************
657 Emit an Exception check. Teste register is integer REG_RESULT
659 *******************************************************************************/
660 void emit_exception_check(codegendata *cd, instruction *iptr)
662 if (INSTRUCTION_MUST_CHECK(iptr)) {
665 /*M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_EXCEPTION);*/
666 M_TRAP(EXCEPTION_HARDWARE_EXCEPTION);
672 * These are local overrides for various environment variables in Emacs.
673 * Please do not remove this and leave it at the end of the file, where
674 * Emacs will automagically detect them.
675 * ---------------------------------------------------------------------
678 * indent-tabs-mode: t
682 * vim:noexpandtab:sw=4:ts=4: