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.hpp"
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.hpp"
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);
220 * Emits code updating the condition register by comparing one integer
221 * register to an immediate integer value.
223 void emit_icmp_imm(codegendata* cd, int reg, int32_t value)
227 if (N_VALID_IMM(value)) {
228 M_ICMP_IMM(reg, value);
230 disp = dseg_add_s4(cd, iptr->sx.val.i);
231 if (N_VALID_DSEG_DISP(disp)) {
232 N_C(s1, N_DSEG_DISP(disp), RN, REG_PV);
234 assert(reg != REG_ITMP2);
235 ICONST(REG_ITMP2, disp);
236 N_C(s1, -N_PV_OFFSET, REG_ITMP2, REG_PV);
242 /* emit_trap *******************************************************************
244 Emit a trap instruction and return the original machine code.
246 *******************************************************************************/
248 uint32_t emit_trap(codegendata *cd)
252 /* Get machine code which is patched back in later. The
253 trap is 2 bytes long. */
255 mcode = *((u2 *) cd->mcodeptr);
264 * Generates synchronization code to enter a monitor.
266 #if defined(ENABLE_THREADS)
267 void emit_monitor_enter(jitdata* jd, int32_t syncslot_offset)
272 // Get required compiler data.
273 methodinfo* m = jd->m;
274 codegendata* cd = jd->cd;
277 if (JITDATA_HAS_FLAG_VERBOSECALL(jd)) {
278 M_ASUB_IMM((INT_ARG_CNT + FLT_ARG_CNT) * 8, REG_SP);
280 for (p = 0; p < INT_ARG_CNT; p++)
281 M_IST(abi_registers_integer_argument[p], REG_SP, p * 8);
283 for (p = 0; p < FLT_ARG_CNT; p++)
284 M_DST(abi_registers_float_argument[p], REG_SP, (INT_ARG_CNT + p) * 8);
286 syncslot_offset += (INT_ARG_CNT + FLT_ARG_CNT) * 8;
290 /* decide which monitor enter function to call */
292 if (m->flags & ACC_STATIC) {
293 disp = dseg_add_address(cd, &m->clazz->object.header);
294 M_ALD_DSEG(REG_A0, disp);
298 M_BNE(SZ_BRC + SZ_ILL);
299 M_ILL(TRAP_NullPointerException);
302 disp = dseg_add_functionptr(cd, LOCK_monitor_enter);
303 M_ALD_DSEG(REG_ITMP2, disp);
305 M_AST(REG_A0, REG_SP, syncslot_offset);
307 M_ASUB_IMM(96, REG_SP);
309 M_AADD_IMM(96, REG_SP);
312 if (JITDATA_HAS_FLAG_VERBOSECALL(jd)) {
313 for (p = 0; p < INT_ARG_CNT; p++)
314 M_ILD(abi_registers_integer_argument[p], REG_SP, p * 8);
316 for (p = 0; p < FLT_ARG_CNT; p++)
317 M_DLD(abi_registers_float_argument[p], REG_SP, (INT_ARG_CNT + p) * 8);
319 M_AADD_IMM((INT_ARG_CNT + FLT_ARG_CNT) * 8, REG_SP);
327 * Generates synchronization code to leave a monitor.
329 #if defined(ENABLE_THREADS)
330 void emit_monitor_exit(jitdata* jd, int32_t syncslot_offset)
334 // Get required compiler data.
335 methodinfo* m = jd->m;
336 codegendata* cd = jd->cd;
338 /* we need to save the proper return value */
340 methoddesc* md = m->parseddesc;
342 switch (md->returntype.type) {
344 M_IST(REG_RESULT2, REG_SP, syncslot_offset + 8 + 4);
348 M_IST(REG_RESULT , REG_SP, syncslot_offset + 8);
351 M_FST(REG_FRESULT, REG_SP, syncslot_offset + 8);
354 M_DST(REG_FRESULT, REG_SP, syncslot_offset + 8);
358 M_ALD(REG_A0, REG_SP, syncslot_offset);
360 disp = dseg_add_functionptr(cd, LOCK_monitor_exit);
361 M_ALD_DSEG(REG_ITMP2, disp);
363 M_ASUB_IMM(96, REG_SP);
365 M_AADD_IMM(96, REG_SP);
367 /* and now restore the proper return value */
369 switch (md->returntype.type) {
371 M_ILD(REG_RESULT2, REG_SP, syncslot_offset + 8 + 4);
375 M_ILD(REG_RESULT , REG_SP, syncslot_offset + 8);
378 M_FLD(REG_FRESULT, REG_SP, syncslot_offset + 8);
381 M_DLD(REG_FRESULT, REG_SP, syncslot_offset + 8);
389 * Emit profiling code for method frequency counting.
391 #if defined(ENABLE_PROFILING)
392 void emit_profile_method(codegendata* cd, codeinfo* code)
394 M_ALD_DSEG(REG_ITMP1, CodeinfoPointer);
395 ICONST(REG_ITMP2, 1);
396 N_AL(REG_ITMP2, OFFSET(codeinfo, frequency), RN, REG_ITMP1);
397 M_IST(REG_ITMP2, REG_ITMP1, OFFSET(codeinfo, frequency));
403 * Emit profiling code for basicblock frequency counting.
405 #if defined(ENABLE_PROFILING)
406 void emit_profile_basicblock(codegendata* cd, codeinfo* code, basicblock* bptr)
408 M_ALD_DSEG(REG_ITMP1, CodeinfoPointer);
409 M_ALD(REG_ITMP1, REG_ITMP1, OFFSET(codeinfo, bbfrequency));
410 ICONST(REG_ITMP2, 1);
411 N_AL(REG_ITMP2, bptr->nr * 4, RN, REG_ITMP1);
412 M_IST(REG_ITMP2, REG_ITMP1, bptr->nr * 4);
417 /* emit_verbosecall_enter ******************************************************
419 Generates the code for the call trace.
421 *******************************************************************************/
424 void emit_verbosecall_enter(jitdata *jd)
439 /* mark trace code */
443 /* allocate stack frame */
445 stackframesize = 96 + (md->paramcount * 8);
447 /* for leaf methods we need to store unused argument and temporary registers */
449 if (code_is_leafmethod(code)) {
450 stackframesize += (ARG_CNT + TMP_CNT) * 8;
453 /* allocate stack frame */
455 M_ASUB_IMM(stackframesize, REG_SP);
457 /* store argument registers in array */
461 for (i = 0; i < md->paramcount; i++) {
462 if (! md->params[i].inmemory) {
463 s = md->params[i].regoff;
464 switch (md->paramtypes[i].type) {
467 M_IST(s, REG_SP, off);
470 M_LST(s, REG_SP, off);
473 M_FST(s, REG_SP, off);
476 M_DST(s, REG_SP, off);
483 /* save unused (currently all) argument registers for leaf methods */
484 /* save temporary registers for leaf methods */
486 if (code_is_leafmethod(code)) {
488 for (i = 0; i < INT_ARG_CNT; ++i, off += 8) {
489 M_IST(abi_registers_integer_argument[i], REG_SP, off);
492 for (i = 0; i < FLT_ARG_CNT; ++i, off += 8) {
493 M_DST(abi_registers_float_argument[i], REG_SP, off);
496 for (i = 0; i < INT_TMP_CNT; ++i, off += 8) {
497 M_IST(abi_registers_integer_temporary[i], REG_SP, off);
500 for (i = 0; i < FLT_TMP_CNT; ++i, off += 8) {
501 M_DST(abi_registers_float_temporary[i], REG_SP, off);
505 /* load arguments for trace_java_call_enter */
509 disp = dseg_add_address(cd, m);
510 M_ALD_DSEG(REG_A0, disp);
511 /* pointer to argument registers array */
512 M_LDA(REG_A1, REG_SP, 96);
513 /* pointer to on stack arguments */
514 M_LDA(REG_A2, REG_SP, stackframesize + (cd->stackframesize * 8));
516 /* call trace_java_call_enter */
518 disp = dseg_add_functionptr(cd, trace_java_call_enter);
519 M_ALD_DSEG(REG_ITMP2, disp);
522 /* restore used argument registers */
523 /* for leaf methods restore all argument and temporary registers */
525 if (code_is_leafmethod(code)) {
526 off = 96 + (8 * md->paramcount);
528 for (i = 0; i < INT_ARG_CNT; ++i, off += 8) {
529 M_ILD(abi_registers_integer_argument[i], REG_SP, off);
532 for (i = 0; i < FLT_ARG_CNT; ++i, off += 8) {
533 M_DLD(abi_registers_float_argument[i], REG_SP, off);
536 for (i = 0; i < INT_TMP_CNT; ++i, off += 8) {
537 M_ILD(abi_registers_integer_temporary[i], REG_SP, off);
540 for (i = 0; i < FLT_TMP_CNT; ++i, off += 8) {
541 M_DLD(abi_registers_float_temporary[i], REG_SP, off);
546 for (i = 0; i < md->paramcount; i++) {
547 if (! md->params[i].inmemory) {
548 s = md->params[i].regoff;
549 switch (md->paramtypes[i].type) {
552 M_ILD(s, REG_SP, off);
555 M_LLD(s, REG_SP, off);
558 M_FLD(s, REG_SP, off);
561 M_DLD(s, REG_SP, off);
569 /* remove stack frame */
571 M_AADD_IMM(stackframesize, REG_SP);
573 /* mark trace code */
578 #endif /* !defined(NDEBUG) */
581 /* emit_verbosecall_exit *******************************************************
583 Generates the code for the call trace.
585 *******************************************************************************/
588 void emit_verbosecall_exit(jitdata *jd)
599 t = m->parseddesc->returntype.type;
601 /* mark trace code */
605 /* allocate stackframe */
607 stackframesize = 96 + (1 * 8);
608 M_ASUB_IMM(stackframesize, REG_SP);
612 /* store return values in array */
614 if (IS_INT_LNG_TYPE(t)) {
615 if (IS_2_WORD_TYPE(t)) {
616 M_LST(REG_RESULT_PACKED, REG_SP, off);
618 M_IST(REG_RESULT, REG_SP, off);
621 M_DST(REG_FRESULT, REG_SP, off);
624 /* call trace_java_call_exit */
626 disp = dseg_add_address(cd, m);
627 M_ALD_DSEG(REG_A0, disp);
628 M_LDA(REG_A1, REG_SP, off);
629 disp = dseg_add_functionptr(cd, trace_java_call_exit);
630 M_ALD_DSEG(REG_ITMP2, disp);
633 /* restore return value */
635 if (IS_INT_LNG_TYPE(t)) {
636 if (IS_2_WORD_TYPE(t)) {
637 M_LLD(REG_RESULT_PACKED, REG_SP, off);
639 M_ILD(REG_RESULT, REG_SP, off);
642 M_DLD(REG_FRESULT, REG_SP, off);
645 /* remove stackframe */
647 M_AADD_IMM(stackframesize, REG_SP);
649 /* mark trace code */
653 #endif /* !defined(NDEBUG) */
656 /* emit_load_high **************************************************************
658 Emits a possible load of the high 32-bits of an operand.
660 *******************************************************************************/
662 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
668 assert(src->type == TYPE_LNG);
670 /* get required compiler data */
674 if (IS_INMEMORY(src->flags)) {
677 disp = src->vv.regoff;
679 M_ILD(tempreg, REG_SP, disp);
684 reg = GET_HIGH_REG(src->vv.regoff);
689 /* emit_load_low ***************************************************************
691 Emits a possible load of the low 32-bits of an operand.
693 *******************************************************************************/
695 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
701 assert(src->type == TYPE_LNG);
703 /* get required compiler data */
707 if (IS_INMEMORY(src->flags)) {
710 disp = src->vv.regoff;
712 M_ILD(tempreg, REG_SP, disp + 4);
717 reg = GET_LOW_REG(src->vv.regoff);
722 s4 emit_load_s1_but(jitdata *jd, instruction *iptr, s4 tempreg, s4 notreg) {
723 codegendata *cd = jd->cd;
724 s4 reg = emit_load_s1(jd, iptr, tempreg);
726 if (IS_FLT_DBL_TYPE(VAROP(iptr->s1)->type)) {
727 M_FMOV(reg, tempreg);
737 s4 emit_load_s2_but(jitdata *jd, instruction *iptr, s4 tempreg, s4 notreg) {
738 codegendata *cd = jd->cd;
739 s4 reg = emit_load_s2(jd, iptr, tempreg);
741 if (IS_FLT_DBL_TYPE(VAROP(iptr->sx.s23.s2)->type)) {
742 M_FMOV(reg, tempreg);
752 void emit_copy_dst(jitdata *jd, instruction *iptr, s4 dtmpreg) {
756 dst = VAROP(iptr->dst);
757 if (! IS_INMEMORY(dst->flags)) {
758 if (dst->vv.regoff != dtmpreg) {
759 if (IS_FLT_DBL_TYPE(dst->type)) {
760 M_FLTMOVE(dtmpreg, dst->vv.regoff);
761 } else if (IS_2_WORD_TYPE(dst->type)) {
762 M_LNGMOVE(dtmpreg, dst->vv.regoff);
764 M_INTMOVE(dtmpreg, dst->vv.regoff);
770 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt) {
772 s4 branchdisp = disp;
776 if (N_VALID_BRANCH(branchdisp)) {
778 /* valid displacement */
799 case BRANCH_UNCONDITIONAL:
803 vm_abort("emit_branch: unknown condition %d", condition);
807 /* If LONGBRANCHES is not set, the flag and the error flag */
809 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
810 cd->flags |= (CODEGENDATA_FLAG_ERROR |
811 CODEGENDATA_FLAG_LONGBRANCHES);
814 /* If error flag is set, do nothing. The method has to be recompiled. */
816 if (CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd) && CODEGENDATA_HAS_FLAG_ERROR(cd)) {
820 /* Patch the displacement to branch over the actual branch manually
821 * to not get yet more nops.
824 branchmpc = cd->mcodeptr - cd->mcodebase;
846 case BRANCH_UNCONDITIONAL:
847 /* fall through, no displacement to patch */
851 vm_abort("emit_branch: unknown condition %d", condition);
854 /* The actual long branch */
856 disp = dseg_add_s4(cd, branchmpc + disp - N_PV_OFFSET);
857 M_ILD_DSEG(REG_ITMP2, disp);
858 M_AADD(REG_PV, REG_ITMP2);
859 M_JMP(RN, REG_ITMP2);
861 /* Patch back the displacement */
863 N_BRC_BACK_PATCH(ref);
867 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
869 if (INSTRUCTION_MUST_CHECK(iptr)) {
871 M_BNE(SZ_BRC + SZ_ILL);
872 M_ILL(TRAP_ArithmeticException);
876 /* emit_arrayindexoutofbounds_check ********************************************
878 Emit a ArrayIndexOutOfBoundsException check.
880 *******************************************************************************/
882 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
884 if (INSTRUCTION_MUST_CHECK(iptr)) {
886 * Do unsigned comparison to catch negative indexes.
888 N_CL(s2, OFFSET(java_array_t, size), RN, s1);
889 M_BLT(SZ_BRC + SZ_ILL);
890 M_ILL2(s2, TRAP_ArrayIndexOutOfBoundsException);
895 /* emit_arraystore_check *******************************************************
897 Emit an ArrayStoreException check.
899 *******************************************************************************/
901 void emit_arraystore_check(codegendata *cd, instruction *iptr)
903 if (INSTRUCTION_MUST_CHECK(iptr)) {
905 M_BNE(SZ_BRC + SZ_ILL);
906 M_ILL(TRAP_ArrayStoreException);
911 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1) {
912 if (INSTRUCTION_MUST_CHECK(iptr)) {
918 M_BGT(SZ_BRC + SZ_ILL);
921 M_BNE(SZ_BRC + SZ_ILL);
924 M_BLE(SZ_BRC + SZ_ILL);
927 vm_abort("emit_classcast_check: unknown condition %d", condition);
929 M_ILL2(s1, TRAP_ClassCastException);
933 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
935 if (INSTRUCTION_MUST_CHECK(iptr)) {
937 M_BNE(SZ_BRC + SZ_ILL);
938 M_ILL(TRAP_NullPointerException);
942 void emit_exception_check(codegendata *cd, instruction *iptr)
944 if (INSTRUCTION_MUST_CHECK(iptr)) {
946 M_BNE(SZ_BRC + SZ_ILL);
947 M_ILL(TRAP_CHECK_EXCEPTION);
951 void emit_recompute_pv(codegendata *cd) {
952 s4 offset, offset_imm;
956 disp = (s4) (cd->mcodeptr - cd->mcodebase);
957 M_ASUB_IMM32(disp, REG_ITMP1, REG_PV);
960 /* If the offset from the method start does not fit into an immediate
961 * value, we can't put it into the data segment!
964 /* Displacement from start of method to here */
966 offset = (s4) (cd->mcodeptr - cd->mcodebase);
967 offset_imm = -offset - SZ_BASR + N_PV_OFFSET;
969 if (N_VALID_IMM(offset_imm)) {
970 /* Get program counter */
972 /* Substract displacement */
973 M_AADD_IMM(offset_imm, REG_PV);
975 /* Save program counter and jump over displacement in instruction flow */
976 N_BRAS(REG_PV, SZ_BRAS + SZ_LONG);
977 /* Place displacement here */
978 /* REG_PV points now exactly to this position */
979 N_LONG(-offset - SZ_BRAS + N_PV_OFFSET);
980 /* Substract *(REG_PV) from REG_PV */
981 N_A(REG_PV, 0, RN, REG_PV);
985 /* emit_trap_compiler **********************************************************
987 Emit a trap instruction which calls the JIT compiler.
989 *******************************************************************************/
991 void emit_trap_compiler(codegendata *cd)
993 M_ILL2(REG_METHODPTR, TRAP_COMPILER);
997 * These are local overrides for various environment variables in Emacs.
998 * Please do not remove this and leave it at the end of the file, where
999 * Emacs will automagically detect them.
1000 * ---------------------------------------------------------------------
1003 * indent-tabs-mode: t