1 /* src/vm/jit/s390/emit.c - s390 code emitter functions
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/s390/codegen.h"
31 #include "vm/jit/s390/emit.h"
32 #include "vm/jit/s390/md-abi.h"
34 #include "mm/memory.h"
36 #include "threads/lock.hpp"
38 #include "vm/jit/builtin.hpp"
39 #include "vm/global.h"
41 #include "vm/options.h"
43 #include "vm/jit/abi.h"
44 #include "vm/jit/abi-asm.h"
45 #include "vm/jit/asmpart.h"
46 #include "vm/jit/codegen-common.hpp"
47 #include "vm/jit/emit-common.hpp"
48 #include "vm/jit/jit.hpp"
49 #include "vm/jit/patcher-common.hpp"
50 #include "vm/jit/replace.hpp"
51 #include "vm/jit/trace.hpp"
52 #include "vm/jit/trap.h"
55 /* emit_load *******************************************************************
57 Emits a possible load of an operand.
59 *******************************************************************************/
61 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
67 /* get required compiler data */
71 if (IS_INMEMORY(src->flags)) {
74 disp = src->vv.regoff;
76 if (IS_FLT_DBL_TYPE(src->type)) {
77 if (IS_2_WORD_TYPE(src->type))
78 M_DLD(tempreg, REG_SP, disp);
80 M_FLD(tempreg, REG_SP, disp);
83 if (IS_2_WORD_TYPE(src->type))
84 M_LLD(tempreg, REG_SP, disp);
86 M_ILD(tempreg, REG_SP, disp);
98 /* emit_store ******************************************************************
100 This function generates the code to store the result of an
101 operation back into a spilled pseudo-variable. If the
102 pseudo-variable has not been spilled in the first place, this
103 function will generate nothing.
105 *******************************************************************************/
107 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
111 /* get required compiler data */
115 if (IS_INMEMORY(dst->flags)) {
118 if (IS_FLT_DBL_TYPE(dst->type)) {
119 if (IS_2_WORD_TYPE(dst->type))
120 M_DST(d, REG_SP, dst->vv.regoff);
122 M_FST(d, REG_SP, dst->vv.regoff);
125 if (IS_2_WORD_TYPE(dst->type))
126 M_LST(d, REG_SP, dst->vv.regoff);
128 M_IST(d, REG_SP, dst->vv.regoff);
134 /* emit_copy *******************************************************************
136 Generates a register/memory to register/memory copy.
138 *******************************************************************************/
140 void emit_copy(jitdata *jd, instruction *iptr)
147 /* get required compiler data */
151 /* get source and destination variables */
153 src = VAROP(iptr->s1);
154 dst = VAROP(iptr->dst);
156 if ((src->vv.regoff != dst->vv.regoff) ||
157 ((src->flags ^ dst->flags) & INMEMORY)) {
159 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
160 /* emit nothing, as the value won't be used anyway */
164 if (IS_INMEMORY(src->flags) && IS_INMEMORY(dst->flags)) {
165 if (IS_2_WORD_TYPE(src->type)) {
166 N_MVC(dst->vv.regoff, 8, REG_SP, src->vv.regoff, REG_SP);
168 N_MVC(dst->vv.regoff, 4, REG_SP, src->vv.regoff, REG_SP);
172 /* If one of the variables resides in memory, we can eliminate
173 the register move from/to the temporary register with the
174 order of getting the destination register and the load. */
176 if (IS_INMEMORY(src->flags)) {
177 if (IS_FLT_DBL_TYPE(dst->type)) {
178 d = codegen_reg_of_var(iptr->opc, dst, REG_FTMP1);
180 if (IS_2_WORD_TYPE(dst->type)) {
181 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
183 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP1);
186 s1 = emit_load(jd, iptr, src, d);
189 if (IS_FLT_DBL_TYPE(src->type)) {
190 s1 = emit_load(jd, iptr, src, REG_FTMP1);
192 if (IS_2_WORD_TYPE(src->type)) {
193 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
195 s1 = emit_load(jd, iptr, src, REG_ITMP1);
198 d = codegen_reg_of_var(iptr->opc, dst, s1);
202 if (IS_FLT_DBL_TYPE(src->type)) {
205 if (IS_2_WORD_TYPE(src->type)) {
213 emit_store(jd, iptr, dst, d);
218 /* emit_trap *******************************************************************
220 Emit a trap instruction and return the original machine code.
222 *******************************************************************************/
224 uint32_t emit_trap(codegendata *cd)
228 /* Get machine code which is patched back in later. The
229 trap is 2 bytes long. */
231 mcode = *((u2 *) cd->mcodeptr);
239 /* emit_verbosecall_enter ******************************************************
241 Generates the code for the call trace.
243 *******************************************************************************/
246 void emit_verbosecall_enter(jitdata *jd)
261 /* mark trace code */
265 /* allocate stack frame */
267 stackframesize = 96 + (md->paramcount * 8);
269 /* for leaf methods we need to store unused argument and temporary registers */
271 if (code_is_leafmethod(code)) {
272 stackframesize += (ARG_CNT + TMP_CNT) * 8;
275 /* allocate stack frame */
277 M_ASUB_IMM(stackframesize, REG_SP);
279 /* store argument registers in array */
283 for (i = 0; i < md->paramcount; i++) {
284 if (! md->params[i].inmemory) {
285 s = md->params[i].regoff;
286 switch (md->paramtypes[i].type) {
289 M_IST(s, REG_SP, off);
292 M_LST(s, REG_SP, off);
295 M_FST(s, REG_SP, off);
298 M_DST(s, REG_SP, off);
305 /* save unused (currently all) argument registers for leaf methods */
306 /* save temporary registers for leaf methods */
308 if (code_is_leafmethod(code)) {
310 for (i = 0; i < INT_ARG_CNT; ++i, off += 8) {
311 M_IST(abi_registers_integer_argument[i], REG_SP, off);
314 for (i = 0; i < FLT_ARG_CNT; ++i, off += 8) {
315 M_DST(abi_registers_float_argument[i], REG_SP, off);
318 for (i = 0; i < INT_TMP_CNT; ++i, off += 8) {
319 M_IST(abi_registers_integer_temporary[i], REG_SP, off);
322 for (i = 0; i < FLT_TMP_CNT; ++i, off += 8) {
323 M_DST(abi_registers_float_temporary[i], REG_SP, off);
327 /* load arguments for trace_java_call_enter */
331 disp = dseg_add_address(cd, m);
332 M_ALD_DSEG(REG_A0, disp);
333 /* pointer to argument registers array */
334 M_LDA(REG_A1, REG_SP, 96);
335 /* pointer to on stack arguments */
336 M_LDA(REG_A2, REG_SP, stackframesize + (cd->stackframesize * 8));
338 /* call trace_java_call_enter */
340 disp = dseg_add_functionptr(cd, trace_java_call_enter);
341 M_ALD_DSEG(REG_ITMP2, disp);
344 /* restore used argument registers */
345 /* for leaf methods restore all argument and temporary registers */
347 if (code_is_leafmethod(code)) {
348 off = 96 + (8 * md->paramcount);
350 for (i = 0; i < INT_ARG_CNT; ++i, off += 8) {
351 M_ILD(abi_registers_integer_argument[i], REG_SP, off);
354 for (i = 0; i < FLT_ARG_CNT; ++i, off += 8) {
355 M_DLD(abi_registers_float_argument[i], REG_SP, off);
358 for (i = 0; i < INT_TMP_CNT; ++i, off += 8) {
359 M_ILD(abi_registers_integer_temporary[i], REG_SP, off);
362 for (i = 0; i < FLT_TMP_CNT; ++i, off += 8) {
363 M_DLD(abi_registers_float_temporary[i], REG_SP, off);
368 for (i = 0; i < md->paramcount; i++) {
369 if (! md->params[i].inmemory) {
370 s = md->params[i].regoff;
371 switch (md->paramtypes[i].type) {
374 M_ILD(s, REG_SP, off);
377 M_LLD(s, REG_SP, off);
380 M_FLD(s, REG_SP, off);
383 M_DLD(s, REG_SP, off);
391 /* remove stack frame */
393 M_AADD_IMM(stackframesize, REG_SP);
395 /* mark trace code */
400 #endif /* !defined(NDEBUG) */
403 /* emit_verbosecall_exit *******************************************************
405 Generates the code for the call trace.
407 *******************************************************************************/
410 void emit_verbosecall_exit(jitdata *jd)
421 t = m->parseddesc->returntype.type;
423 /* mark trace code */
427 /* allocate stackframe */
429 stackframesize = 96 + (1 * 8);
430 M_ASUB_IMM(stackframesize, REG_SP);
434 /* store return values in array */
436 if (IS_INT_LNG_TYPE(t)) {
437 if (IS_2_WORD_TYPE(t)) {
438 M_LST(REG_RESULT_PACKED, REG_SP, off);
440 M_IST(REG_RESULT, REG_SP, off);
443 M_DST(REG_FRESULT, REG_SP, off);
446 /* call trace_java_call_exit */
448 disp = dseg_add_address(cd, m);
449 M_ALD_DSEG(REG_A0, disp);
450 M_LDA(REG_A1, REG_SP, off);
451 disp = dseg_add_functionptr(cd, trace_java_call_exit);
452 M_ALD_DSEG(REG_ITMP2, disp);
455 /* restore return value */
457 if (IS_INT_LNG_TYPE(t)) {
458 if (IS_2_WORD_TYPE(t)) {
459 M_LLD(REG_RESULT_PACKED, REG_SP, off);
461 M_ILD(REG_RESULT, REG_SP, off);
464 M_DLD(REG_FRESULT, REG_SP, off);
467 /* remove stackframe */
469 M_AADD_IMM(stackframesize, REG_SP);
471 /* mark trace code */
475 #endif /* !defined(NDEBUG) */
478 /* emit_load_high **************************************************************
480 Emits a possible load of the high 32-bits of an operand.
482 *******************************************************************************/
484 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
490 assert(src->type == TYPE_LNG);
492 /* get required compiler data */
496 if (IS_INMEMORY(src->flags)) {
499 disp = src->vv.regoff;
501 M_ILD(tempreg, REG_SP, disp);
506 reg = GET_HIGH_REG(src->vv.regoff);
511 /* emit_load_low ***************************************************************
513 Emits a possible load of the low 32-bits of an operand.
515 *******************************************************************************/
517 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
523 assert(src->type == TYPE_LNG);
525 /* get required compiler data */
529 if (IS_INMEMORY(src->flags)) {
532 disp = src->vv.regoff;
534 M_ILD(tempreg, REG_SP, disp + 4);
539 reg = GET_LOW_REG(src->vv.regoff);
544 s4 emit_load_s1_but(jitdata *jd, instruction *iptr, s4 tempreg, s4 notreg) {
545 codegendata *cd = jd->cd;
546 s4 reg = emit_load_s1(jd, iptr, tempreg);
548 if (IS_FLT_DBL_TYPE(VAROP(iptr->s1)->type)) {
549 M_FMOV(reg, tempreg);
559 s4 emit_load_s2_but(jitdata *jd, instruction *iptr, s4 tempreg, s4 notreg) {
560 codegendata *cd = jd->cd;
561 s4 reg = emit_load_s2(jd, iptr, tempreg);
563 if (IS_FLT_DBL_TYPE(VAROP(iptr->sx.s23.s2)->type)) {
564 M_FMOV(reg, tempreg);
574 void emit_copy_dst(jitdata *jd, instruction *iptr, s4 dtmpreg) {
578 dst = VAROP(iptr->dst);
579 if (! IS_INMEMORY(dst->flags)) {
580 if (dst->vv.regoff != dtmpreg) {
581 if (IS_FLT_DBL_TYPE(dst->type)) {
582 M_FLTMOVE(dtmpreg, dst->vv.regoff);
583 } else if (IS_2_WORD_TYPE(dst->type)) {
584 M_LNGMOVE(dtmpreg, dst->vv.regoff);
586 M_INTMOVE(dtmpreg, dst->vv.regoff);
592 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt) {
594 s4 branchdisp = disp;
598 if (N_VALID_BRANCH(branchdisp)) {
600 /* valid displacement */
621 case BRANCH_UNCONDITIONAL:
625 vm_abort("emit_branch: unknown condition %d", condition);
629 /* If LONGBRANCHES is not set, the flag and the error flag */
631 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
632 cd->flags |= (CODEGENDATA_FLAG_ERROR |
633 CODEGENDATA_FLAG_LONGBRANCHES);
636 /* If error flag is set, do nothing. The method has to be recompiled. */
638 if (CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd) && CODEGENDATA_HAS_FLAG_ERROR(cd)) {
642 /* Patch the displacement to branch over the actual branch manually
643 * to not get yet more nops.
646 branchmpc = cd->mcodeptr - cd->mcodebase;
668 case BRANCH_UNCONDITIONAL:
669 /* fall through, no displacement to patch */
673 vm_abort("emit_branch: unknown condition %d", condition);
676 /* The actual long branch */
678 disp = dseg_add_s4(cd, branchmpc + disp - N_PV_OFFSET);
679 M_ILD_DSEG(REG_ITMP2, disp);
680 M_AADD(REG_PV, REG_ITMP2);
681 M_JMP(RN, REG_ITMP2);
683 /* Patch back the displacement */
685 N_BRC_BACK_PATCH(ref);
689 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
691 if (INSTRUCTION_MUST_CHECK(iptr)) {
693 M_BNE(SZ_BRC + SZ_ILL);
694 M_ILL(TRAP_ArithmeticException);
698 /* emit_arrayindexoutofbounds_check ********************************************
700 Emit a ArrayIndexOutOfBoundsException check.
702 *******************************************************************************/
704 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
706 if (INSTRUCTION_MUST_CHECK(iptr)) {
708 * Do unsigned comparison to catch negative indexes.
710 N_CL(s2, OFFSET(java_array_t, size), RN, s1);
711 M_BLT(SZ_BRC + SZ_ILL);
712 M_ILL2(s2, TRAP_ArrayIndexOutOfBoundsException);
717 /* emit_arraystore_check *******************************************************
719 Emit an ArrayStoreException check.
721 *******************************************************************************/
723 void emit_arraystore_check(codegendata *cd, instruction *iptr)
725 if (INSTRUCTION_MUST_CHECK(iptr)) {
727 M_BNE(SZ_BRC + SZ_ILL);
728 M_ILL(TRAP_ArrayStoreException);
733 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1) {
734 if (INSTRUCTION_MUST_CHECK(iptr)) {
740 M_BGT(SZ_BRC + SZ_ILL);
743 M_BNE(SZ_BRC + SZ_ILL);
746 M_BLE(SZ_BRC + SZ_ILL);
749 vm_abort("emit_classcast_check: unknown condition %d", condition);
751 M_ILL2(s1, TRAP_ClassCastException);
755 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
757 if (INSTRUCTION_MUST_CHECK(iptr)) {
759 M_BNE(SZ_BRC + SZ_ILL);
760 M_ILL(TRAP_NullPointerException);
764 void emit_exception_check(codegendata *cd, instruction *iptr)
766 if (INSTRUCTION_MUST_CHECK(iptr)) {
768 M_BNE(SZ_BRC + SZ_ILL);
769 M_ILL(TRAP_CHECK_EXCEPTION);
773 void emit_restore_pv(codegendata *cd) {
774 s4 offset, offset_imm;
778 disp = (s4) (cd->mcodeptr - cd->mcodebase);
779 M_ASUB_IMM32(disp, REG_ITMP1, REG_PV);
782 /* If the offset from the method start does not fit into an immediate
783 * value, we can't put it into the data segment!
786 /* Displacement from start of method to here */
788 offset = (s4) (cd->mcodeptr - cd->mcodebase);
789 offset_imm = -offset - SZ_BASR + N_PV_OFFSET;
791 if (N_VALID_IMM(offset_imm)) {
792 /* Get program counter */
794 /* Substract displacement */
795 M_AADD_IMM(offset_imm, REG_PV);
797 /* Save program counter and jump over displacement in instruction flow */
798 N_BRAS(REG_PV, SZ_BRAS + SZ_LONG);
799 /* Place displacement here */
800 /* REG_PV points now exactly to this position */
801 N_LONG(-offset - SZ_BRAS + N_PV_OFFSET);
802 /* Substract *(REG_PV) from REG_PV */
803 N_A(REG_PV, 0, RN, REG_PV);
807 /* emit_trap_compiler **********************************************************
809 Emit a trap instruction which calls the JIT compiler.
811 *******************************************************************************/
813 void emit_trap_compiler(codegendata *cd)
815 M_ILL2(REG_METHODPTR, TRAP_COMPILER);
819 * These are local overrides for various environment variables in Emacs.
820 * Please do not remove this and leave it at the end of the file, where
821 * Emacs will automagically detect them.
822 * ---------------------------------------------------------------------
825 * indent-tabs-mode: t