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
25 $Id: arch.h 5330 2006-09-05 18:43:12Z edwin $
35 #include "vm/jit/emit-common.h"
36 #include "vm/exceptions.h"
37 #include "vm/jit/asmpart.h"
38 #include "vm/builtin.h"
40 #include "mm/memory.h"
42 #include "threads/lock-common.h"
47 /* emit_mov_imm_reg **************************************************************************
49 * Loads an immededat operand into an integer data register
51 ********************************************************************************************/
52 void emit_mov_imm_reg (codegendata *cd, s4 imm, s4 dreg)
54 /* FIXME: -1 can be used as byte form 0xff, but this ifs cascade is plain wrong it seems */
56 if ( (imm & 0x0000007F) == imm) {
58 *((s2*)cd->mcodeptr) = 0x7000 | (dreg << 9) | imm; /* MOVEQ.L */
60 } else if ((imm & 0x00007FFF) == imm) {
62 OPWORD( ((7<<6) | (dreg << 3) | 5), 7, 4); /* MVS.W */
63 *((s2*)cd->mcodeptr) = (s2)imm;
67 OPWORD( ((2<<6) | (dreg << 3) | 0), 7, 4);
68 *((s4*)cd->mcodeptr) = (s4)imm;
74 /* emit_copy *******************************************************************
76 Generates a register/memory to register/memory copy.
78 *******************************************************************************/
80 void emit_copy(jitdata *jd, instruction *iptr)
87 /* get required compiler data */
91 /* get source and destination variables */
93 src = VAROP(iptr->s1);
94 dst = VAROP(iptr->dst);
96 if ((src->vv.regoff != dst->vv.regoff) ||
97 (IS_INMEMORY(src->flags ^ dst->flags))) {
99 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
100 /* emit nothing, as the value won't be used anyway */
104 /* If one of the variables resides in memory, we can eliminate
105 the register move from/to the temporary register with the
106 order of getting the destination register and the load. */
108 if (IS_INMEMORY(src->flags)) {
109 if (IS_LNG_TYPE(src->type))
110 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
112 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
114 s1 = emit_load(jd, iptr, src, d);
117 if (IS_LNG_TYPE(src->type))
118 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
120 s1 = emit_load(jd, iptr, src, REG_IFTMP);
122 d = codegen_reg_of_var(iptr->opc, dst, s1);
127 case TYPE_INT: M_INTMOVE(s1, d); break;
128 case TYPE_ADR: M_ADRMOVE(s1, d); break;
129 case TYPE_LNG: M_LNGMOVE(s1, d); break;
130 #if !defined(ENABLE_SOFTFLOAT)
131 case TYPE_FLT: M_FLTMOVE(s1, d); break;
132 case TYPE_DBL: M_DBLMOVE(s1, d); break;
134 case TYPE_FLT: M_INTMOVE(s1, d); break;
135 case TYPE_DBL: M_LNGMOVE(s1, d); break;
138 vm_abort("emit_copy: unknown type %d", src->type);
142 emit_store(jd, iptr, dst, d);
147 /* emit_store ******************************************************************
149 Emits a possible store of the destination operand.
151 *******************************************************************************/
153 inline void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
157 /* get required compiler data */
161 if (IS_INMEMORY(dst->flags)) {
165 #if defined(ENABLE_SOFTFLOAT)
169 M_LST(d, REG_SP, dst->vv.regoff * 4);
171 #if defined(ENABLE_SOFTFLOAT)
175 M_IST(d, REG_SP, dst->vv.regoff * 4);
178 M_AST(d, REG_SP, dst->vv.regoff * 4);
180 #if !defined(ENABLE_SOFTFLOAT)
182 M_DST(d, REG_SP, dst->vv.regoff * 4);
185 M_FST(d, REG_SP, dst->vv.regoff * 4);
189 vm_abort("emit_store: unknown type %d", dst->type);
195 /* emit_load *******************************************************************
197 Emits a possible load of an operand.
199 *******************************************************************************/
201 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
207 /* get required compiler data */
211 if (IS_INMEMORY(src->flags)) {
214 disp = src->vv.regoff * 4;
217 #if defined(ENABLE_SOFTFLOAT)
221 M_ILD(tempreg, REG_SP, disp);
223 #if defined(ENABLE_SOFTFLOAT)
227 M_LLD(tempreg, REG_SP, disp);
230 M_ALD(tempreg, REG_SP, disp);
232 #if !defined(ENABLE_SOFTFLOAT)
234 M_FLD(tempreg, REG_SP, disp);
237 M_DLD(tempreg, REG_SP, disp);
241 vm_abort("emit_load: unknown type %d", src->type);
244 if (IS_FLT_DBL_TYPE(src->type)) {
245 if (IS_2_WORD_TYPE(src->type)) {
246 M_DLD(tempreg, REG_SP, disp);
248 M_FLD(tempreg, REG_SP, disp);
251 if (IS_2_WORD_TYPE(src->type)) {
252 M_LLD(tempreg, REG_SP, disp);
254 M_ILD(tempreg, REG_SP, disp);
262 reg = src->vv.regoff;
268 /* emit_patcher_stubs **********************************************************
270 Generates the code for the patcher stubs.
272 *******************************************************************************/
273 void emit_patcher_stubs(jitdata *jd)
283 /* get required compiler data */
287 /* generate code patching stub call code */
291 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
292 /* check code segment size */
296 /* Get machine code which is patched back in later. A
297 `bsr.l' is 6 bytes long. */
299 savedmcodeptr = cd->mcodebase + pref->branchpos;
300 mcode = *((u8 *) savedmcodeptr);
302 /* patch in `bsr.l' to call the following code */
304 tmpmcodeptr = cd->mcodeptr; /* save current mcodeptr */
305 cd->mcodeptr = savedmcodeptr; /* set mcodeptr to patch position */
307 M_BSR_IMM(tmpmcodeptr - (savedmcodeptr + PATCHER_CALL_SIZE) + 4);
309 cd->mcodeptr = tmpmcodeptr; /* restore the current mcodeptr */
311 /* save REG_ITMP3, restored in asm_patcher_wrapper */
314 /* move pointer to java_objectheader onto stack */
316 #if defined(ENABLE_THREADS)
317 (void) dseg_add_unique_address(cd, NULL); /* flcword */
318 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
319 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
321 M_IMOV_IMM32(0, REG_ITMP3);
323 M_IADD_IMM(disp, REG_ITMP3);
329 /* push move machine code bytes and classinfo pointer */
331 M_IPUSH_IMM(mcode >> 32);
333 M_IPUSH_IMM(pref->ref);
334 M_IPUSH_IMM(pref->patcher);
336 M_JMP_IMM(asm_patcher_wrapper);
339 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
345 #if !defined(ENABLE_SOFTFLOAT)
346 assert(src->type == TYPE_LNG);
348 assert(src->type == TYPE_LNG || src->type == TYPE_DBL);
351 /* get required compiler data */
354 if (IS_INMEMORY(src->flags)) {
357 disp = src->vv.regoff * 4;
358 M_ILD(tempreg, REG_SP, disp + 4);
361 reg = GET_LOW_REG(src->vv.regoff);
365 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
371 #if !defined(ENABLE_SOFTFLOAT)
372 assert(src->type == TYPE_LNG);
374 assert(src->type == TYPE_LNG || src->type == TYPE_DBL);
376 /* get required compiler data */
379 if (IS_INMEMORY(src->flags)) {
381 disp = src->vv.regoff * 4;
382 M_ILD(tempreg, REG_SP, disp);
385 reg = GET_HIGH_REG(src->vv.regoff);
389 /* emit_branch *****************************************************************
391 Emits the code for conditional and unconditional branchs.
393 *******************************************************************************/
394 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
396 /* calculate the different displacements */
397 /* PC is a at branch instruction + 2 */
398 /* coditional and uncondition branching work the same way */
399 /* short branches have signed 16 bit offset */
400 /* long branches are signed 32 bit */
401 /* the 8 bit offset branching instructions are not used */
405 /* check displacement for overflow */
406 if ((disp & 0x0000FFFF) != disp) {
407 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
408 cd->flags |= (CODEGENDATA_FLAG_ERROR | CODEGENDATA_FLAG_LONGBRANCHES);
412 /* check which branch to generate */
414 if (condition == BRANCH_UNCONDITIONAL) {
415 if (CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
421 if (CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
449 vm_abort("emit_branch: unknown condition %d", condition);
478 vm_abort("emit_branch: unknown condition %d", condition);
487 * Trace functions. Implement -verbose:call flag
488 * code marked by real NOP, but performance is no matter when using -verbose:call :)
490 void emit_verbosecall_enter(jitdata* jd)
499 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
502 /* get required compiler data */
508 /* mark trace code */
516 #if !defined(ENABLE_SOFTFLOAT)
517 M_AADD_IMM(-8*2, REG_SP);
518 M_FSTORE(REG_F0, REG_SP, 8);
519 M_FSTORE(REG_F1, REG_SP, 0);
521 disp = 4*4 + 8*2 + 4; /* points to old argument stack initially */
525 /* builtin_verbosecall_enter takes all args as s8 type */
526 /* TRACE_ARGS_NUM is the number of args the builtin_verbosecall_enter expects */
530 /* travel up stack to the first argument of the function which needs to be copied */
531 for (i=0; (i < md->paramcount) && (i < TRACE_ARGS_NUM); i++) {
533 if (IS_2_WORD_TYPE(md->paramtypes[i].type)) {
538 /* disp now points to the first arg which gets copied to the trace stack, relative to REG_SP! */
539 for (i=TRACE_ARGS_NUM-1; i>=0; --i) {
540 if (i < md->paramcount) {
541 /* traced function has such an argument */
542 t = md->paramtypes[i].type;
544 if (IS_2_WORD_TYPE(t)) {
545 /* copy from original argument stack */
546 M_ILD(REG_ITMP1, REG_SP, disp);
548 M_ILD(REG_ITMP1, REG_SP, disp);
551 /* displacment is increased as 4 byte on original stack but 8 byte on trace stack */
552 M_ILD(REG_ITMP1, REG_SP, disp);
558 /* function has no arg here, push nothing and adapt displacement */
564 M_JSR_IMM(builtin_verbosecall_enter);
565 /* pop arguments off stack */
566 M_AADD_IMM(TRACE_ARGS_NUM*8+4, REG_SP);
568 #if !defined(ENABLE_SOFTFLOAT)
569 M_FSTORE(REG_F1, REG_SP, 0);
570 M_FSTORE(REG_F0, REG_SP, 8);
571 M_AADD_IMM(8*2, REG_SP);
581 void emit_verbosecall_exit(jitdata* jd)
588 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
591 /* get required compiler data */
597 /* void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m); */
600 /* mark trace code */
603 #if !defined(ENABLE_SOFTFLOAT)
604 M_AADD_IMM(-8, REG_SP);
605 M_FSTORE(REG_F1, REG_SP, 0);
608 M_IPUSH_IMM(m); /* push methodinfo */
610 #if !defined(ENABLE_SOFTFLOAT)
611 M_AADD_IMM(-3*4, REG_SP);
612 M_FST(REG_D0, REG_SP, 8);
613 M_DST(REG_D0, REG_SP, 0);
621 M_IPUSH(GET_HIGH_REG(REG_RESULT_PACKED))
622 M_IPUSH(GET_LOW_REG(REG_RESULT_PACKED)) /* push long result */
624 M_JSR_IMM(builtin_verbosecall_exit);
626 /* poping result registers from stack */
627 M_IPOP(GET_LOW_REG(REG_RESULT_PACKED))
628 M_IPOP(GET_HIGH_REG(REG_RESULT_PACKED))
630 #if !defined(ENABLE_SOFTFLOAT)
631 M_DLD(REG_D0, REG_SP, 0)
632 M_FLD(REG_D0, REG_SP, 8)
634 M_AADD_IMM(3*4 + 4, REG_SP);
636 #if !defined(ENABLE_SOFTFLOAT)
637 M_FLOAD(REG_F1, REG_SP, 0)
638 M_AADD_IMM(8, REG_SP);
645 /* emit_classcast_check ********************************************************
647 Emit a ClassCastException check.
649 *******************************************************************************/
651 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
653 if (INSTRUCTION_MUST_CHECK(iptr)) {
668 vm_abort("emit_classcast_check: unknown condition %d", condition);
670 M_TRAP_SETREGISTER(s1);
671 M_TRAP(EXCEPTION_HARDWARE_CLASSCAST);
675 /* emit_arrayindexoutofbounds_check ********************************************
677 Emit a ArrayIndexOutOfBoundsException check.
679 *******************************************************************************/
680 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
682 if (INSTRUCTION_MUST_CHECK(iptr)) {
683 M_ILD(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
684 M_ICMP(s2, REG_ITMP3);
686 M_TRAP_SETREGISTER(s2);
687 M_TRAP(EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS);
691 /* emit_nullpointer_check ******************************************************
693 Emit a NullPointerException check.
695 *******************************************************************************/
696 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
698 if (INSTRUCTION_MUST_CHECK(iptr)) {
699 /* XXX: this check is copied to call monitor_enter
700 * invocation at the beginning of codegen.c */
703 M_TRAP(M68K_EXCEPTION_HARDWARE_NULLPOINTER);
707 /* emit_arithmetic_check *******************************************************
709 Emit an ArithmeticException check.
711 *******************************************************************************/
713 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
715 if (INSTRUCTION_MUST_CHECK(iptr)) {
718 M_TRAP(EXCEPTION_HARDWARE_ARITHMETIC);
722 /* emit_exception_check_ireg **************************************************
724 Emit an Exception check. Teste register is integer REG_RESULT
726 *******************************************************************************/
727 void emit_exception_check(codegendata *cd, instruction *iptr)
729 if (INSTRUCTION_MUST_CHECK(iptr)) {
732 /*M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_EXCEPTION);*/
733 M_TRAP(EXCEPTION_HARDWARE_EXCEPTION);
739 * These are local overrides for various environment variables in Emacs.
740 * Please do not remove this and leave it at the end of the file, where
741 * Emacs will automagically detect them.
742 * ---------------------------------------------------------------------
745 * indent-tabs-mode: t
749 * vim:noexpandtab:sw=4:ts=4: