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"
37 #include "vm/jit/trace.h"
39 #include "mm/memory.h"
41 #include "threads/lock-common.h"
46 /* emit_mov_imm_reg **************************************************************************
48 * Loads an immededat operand into an integer data register
50 ********************************************************************************************/
51 void emit_mov_imm_reg (codegendata *cd, s4 imm, s4 dreg)
53 /* FIXME: -1 can be used as byte form 0xff, but this ifs cascade is plain wrong it seems */
55 if ( (imm & 0x0000007F) == imm) {
57 *((s2*)cd->mcodeptr) = 0x7000 | (dreg << 9) | imm; /* MOVEQ.L */
59 } else if ((imm & 0x00007FFF) == imm) {
61 OPWORD( ((7<<6) | (dreg << 3) | 5), 7, 4); /* MVS.W */
62 *((s2*)cd->mcodeptr) = (s2)imm;
66 OPWORD( ((2<<6) | (dreg << 3) | 0), 7, 4);
67 *((s4*)cd->mcodeptr) = (s4)imm;
73 /* emit_copy *******************************************************************
75 Generates a register/memory to register/memory copy.
77 *******************************************************************************/
79 void emit_copy(jitdata *jd, instruction *iptr)
86 /* get required compiler data */
90 /* get source and destination variables */
92 src = VAROP(iptr->s1);
93 dst = VAROP(iptr->dst);
95 if ((src->vv.regoff != dst->vv.regoff) ||
96 (IS_INMEMORY(src->flags ^ dst->flags))) {
98 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
99 /* emit nothing, as the value won't be used anyway */
103 /* If one of the variables resides in memory, we can eliminate
104 the register move from/to the temporary register with the
105 order of getting the destination register and the load. */
107 if (IS_INMEMORY(src->flags)) {
108 if (IS_LNG_TYPE(src->type))
109 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
111 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
113 s1 = emit_load(jd, iptr, src, d);
116 if (IS_LNG_TYPE(src->type))
117 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
119 s1 = emit_load(jd, iptr, src, REG_IFTMP);
121 d = codegen_reg_of_var(iptr->opc, dst, s1);
126 case TYPE_INT: M_INTMOVE(s1, d); break;
127 case TYPE_ADR: M_ADRMOVE(s1, d); break;
128 case TYPE_LNG: M_LNGMOVE(s1, d); break;
129 #if !defined(ENABLE_SOFTFLOAT)
130 case TYPE_FLT: M_FLTMOVE(s1, d); break;
131 case TYPE_DBL: M_DBLMOVE(s1, d); break;
133 case TYPE_FLT: M_INTMOVE(s1, d); break;
134 case TYPE_DBL: M_LNGMOVE(s1, d); break;
137 vm_abort("emit_copy: unknown type %d", src->type);
141 emit_store(jd, iptr, dst, d);
146 /* emit_store ******************************************************************
148 Emits a possible store of the destination operand.
150 *******************************************************************************/
152 inline void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
156 /* get required compiler data */
160 if (IS_INMEMORY(dst->flags)) {
164 #if defined(ENABLE_SOFTFLOAT)
168 M_LST(d, REG_SP, dst->vv.regoff);
170 #if defined(ENABLE_SOFTFLOAT)
174 M_IST(d, REG_SP, dst->vv.regoff);
177 M_AST(d, REG_SP, dst->vv.regoff);
179 #if !defined(ENABLE_SOFTFLOAT)
181 M_DST(d, REG_SP, dst->vv.regoff);
184 M_FST(d, REG_SP, dst->vv.regoff);
188 vm_abort("emit_store: unknown type %d", dst->type);
194 /* emit_load *******************************************************************
196 Emits a possible load of an operand.
198 *******************************************************************************/
200 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
206 /* get required compiler data */
210 if (IS_INMEMORY(src->flags)) {
213 disp = src->vv.regoff;
216 #if defined(ENABLE_SOFTFLOAT)
220 M_ILD(tempreg, REG_SP, disp);
222 #if defined(ENABLE_SOFTFLOAT)
226 M_LLD(tempreg, REG_SP, disp);
229 M_ALD(tempreg, REG_SP, disp);
231 #if !defined(ENABLE_SOFTFLOAT)
233 M_FLD(tempreg, REG_SP, disp);
236 M_DLD(tempreg, REG_SP, disp);
240 vm_abort("emit_load: unknown type %d", src->type);
243 if (IS_FLT_DBL_TYPE(src->type)) {
244 if (IS_2_WORD_TYPE(src->type)) {
245 M_DLD(tempreg, REG_SP, disp);
247 M_FLD(tempreg, REG_SP, disp);
250 if (IS_2_WORD_TYPE(src->type)) {
251 M_LLD(tempreg, REG_SP, disp);
253 M_ILD(tempreg, REG_SP, disp);
261 reg = src->vv.regoff;
266 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
272 #if !defined(ENABLE_SOFTFLOAT)
273 assert(src->type == TYPE_LNG);
275 assert(src->type == TYPE_LNG || src->type == TYPE_DBL);
278 /* get required compiler data */
281 if (IS_INMEMORY(src->flags)) {
284 disp = src->vv.regoff;
285 M_ILD(tempreg, REG_SP, disp + 4);
288 reg = GET_LOW_REG(src->vv.regoff);
292 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
298 #if !defined(ENABLE_SOFTFLOAT)
299 assert(src->type == TYPE_LNG);
301 assert(src->type == TYPE_LNG || src->type == TYPE_DBL);
303 /* get required compiler data */
306 if (IS_INMEMORY(src->flags)) {
308 disp = src->vv.regoff;
309 M_ILD(tempreg, REG_SP, disp);
312 reg = GET_HIGH_REG(src->vv.regoff);
316 /* emit_branch *****************************************************************
318 Emits the code for conditional and unconditional branchs.
320 *******************************************************************************/
321 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
323 /* calculate the different displacements */
324 /* PC is a at branch instruction + 2 */
325 /* coditional and uncondition branching work the same way */
326 /* short branches have signed 16 bit offset */
327 /* long branches are signed 32 bit */
328 /* the 8 bit offset branching instructions are not used */
332 /* check displacement for overflow */
333 if ((disp & 0x0000FFFF) != disp) {
334 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
335 cd->flags |= (CODEGENDATA_FLAG_ERROR | CODEGENDATA_FLAG_LONGBRANCHES);
339 /* check which branch to generate */
341 if (condition == BRANCH_UNCONDITIONAL) {
342 if (CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
348 if (CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
376 vm_abort("emit_branch: unknown condition %d", condition);
405 vm_abort("emit_branch: unknown condition %d", condition);
414 * Trace functions. Implement -verbose:call flag
415 * code marked by real NOP, but performance is no matter when using -verbose:call :)
417 void emit_verbosecall_enter(jitdata* jd)
424 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
427 /* get required compiler data */
433 /* mark trace code */
440 M_AMOV(REG_SP, REG_A0); /* simpyfy stack offset calculation */
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);
448 M_AADD_IMM(4*4 + 4, REG_A0);
449 M_APUSH(REG_A0); /* third arg is original argument stack */
450 M_IPUSH_IMM(0); /* second arg is number of argument registers (=0) */
451 M_IPUSH_IMM(m); /* first arg is methodpointer */
453 M_JSR_IMM(trace_java_call_enter);
454 /* pop arguments off stack */
455 M_AADD_IMM(3*4, REG_SP);
457 #if !defined(ENABLE_SOFTFLOAT)
458 M_FSTORE(REG_F1, REG_SP, 0);
459 M_FSTORE(REG_F0, REG_SP, 8);
460 M_AADD_IMM(8*2, REG_SP);
470 void emit_verbosecall_exit(jitdata* jd)
477 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
480 /* get required compiler data */
486 /* void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m); */
488 /* void trace_java_call_exit(methodinfo *m, uint64_t *return_regs) */
490 /* mark trace code */
493 /* to store result on stack */
494 M_AADD_IMM(-8, REG_SP); /* create space for array */
496 switch (md->returntype.type) {
499 #if defined(ENABLE_SOFTFLOAT)
502 M_IST(REG_D0, REG_SP, 0);
506 #if defined(ENABLE_SOFTFLOAT)
509 M_LST(REG_D1, REG_SP, 0);
512 #if !defined(ENABLE_SOFTFLOAT)
513 case TYPE_FLT: /* FIXME */
514 case TYPE_DBL: /* FIXME */
517 case TYPE_VOID: /* nothing */
524 M_APUSH(REG_SP); /* push address of array */
525 M_IPUSH_IMM(m); /* push methodinfo */
526 M_JSR_IMM(trace_java_call_exit);
528 M_AADD_IMM(8, REG_SP); /* remove args */
529 /* poping result registers from stack */
530 switch (md->returntype.type) {
533 #if defined(ENABLE_SOFTFLOAT)
536 M_ILD(REG_D0, REG_SP, 0);
540 #if defined(ENABLE_SOFTFLOAT)
543 M_LLD(REG_D1, REG_SP, 0);
546 #if !defined(ENABLE_SOFTFLOAT)
547 case TYPE_FLT: /* FIXME */
548 case TYPE_DBL: /* FIXME */
551 case TYPE_VOID: /* nothing */
558 M_AADD_IMM(8, REG_SP); /* remove space for array */
564 /* emit_classcast_check ********************************************************
566 Emit a ClassCastException check.
568 *******************************************************************************/
570 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
572 if (INSTRUCTION_MUST_CHECK(iptr)) {
587 vm_abort("emit_classcast_check: unknown condition %d", condition);
589 M_TRAP_SETREGISTER(s1);
590 M_TRAP(EXCEPTION_HARDWARE_CLASSCAST);
594 /* emit_arrayindexoutofbounds_check ********************************************
596 Emit a ArrayIndexOutOfBoundsException check.
598 *******************************************************************************/
599 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
601 if (INSTRUCTION_MUST_CHECK(iptr)) {
602 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
603 M_ICMP(s2, REG_ITMP3);
605 M_TRAP_SETREGISTER(s2);
606 M_TRAP(EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS);
611 /* emit_arraystore_check *******************************************************
613 Emit an ArrayStoreException check.
615 *******************************************************************************/
617 void emit_arraystore_check(codegendata *cd, instruction *iptr)
619 if (INSTRUCTION_MUST_CHECK(iptr)) {
622 /*M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_ARRAYSTORE);*/
623 M_TRAP(EXCEPTION_HARDWARE_ARRAYSTORE);
628 /* emit_nullpointer_check ******************************************************
630 Emit a NullPointerException check.
632 *******************************************************************************/
633 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
635 if (INSTRUCTION_MUST_CHECK(iptr)) {
636 /* XXX: this check is copied to call monitor_enter
637 * invocation at the beginning of codegen.c */
640 M_TRAP(M68K_EXCEPTION_HARDWARE_NULLPOINTER);
644 /* emit_arithmetic_check *******************************************************
646 Emit an ArithmeticException check.
648 *******************************************************************************/
650 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
652 if (INSTRUCTION_MUST_CHECK(iptr)) {
655 M_TRAP(EXCEPTION_HARDWARE_ARITHMETIC);
659 /* emit_exception_check_ireg **************************************************
661 Emit an Exception check. Teste register is integer REG_RESULT
663 *******************************************************************************/
664 void emit_exception_check(codegendata *cd, instruction *iptr)
666 if (INSTRUCTION_MUST_CHECK(iptr)) {
669 /*M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_EXCEPTION);*/
670 M_TRAP(EXCEPTION_HARDWARE_EXCEPTION);
674 /* emit_trap_compiler **********************************************************
676 Emit a trap instruction which calls the JIT compiler.
678 *******************************************************************************/
680 void emit_trap_compiler(codegendata *cd)
682 M_TRAP_SETREGISTER(REG_METHODPTR);
683 M_TRAP(EXCEPTION_HARDWARE_COMPILER);
687 /* emit_trap *******************************************************************
689 Emit a trap instruction and return the original machine code.
691 *******************************************************************************/
693 uint32_t emit_trap(codegendata *cd)
697 /* Get machine code which is patched back in later. The
698 trap is 2 bytes long. */
700 mcode = *((uint32_t *) cd->mcodeptr);
702 M_TRAP(EXCEPTION_HARDWARE_PATCHER);
710 * These are local overrides for various environment variables in Emacs.
711 * Please do not remove this and leave it at the end of the file, where
712 * Emacs will automagically detect them.
713 * ---------------------------------------------------------------------
716 * indent-tabs-mode: t
720 * vim:noexpandtab:sw=4:ts=4: