1 /* src/vm/jit/powerpc64/emit.c - PowerPC64 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
32 #include "mm/memory.hpp"
35 #include "vm/jit/powerpc64/codegen.h"
37 #include "threads/lock.hpp"
39 #include "vm/options.h"
42 #include "vm/jit/abi.h"
43 #include "vm/jit/asmpart.h"
44 #include "vm/jit/emit-common.hpp"
45 #include "vm/jit/jit.hpp"
46 #include "vm/jit/trace.hpp"
47 #include "vm/jit/trap.hpp"
50 /* emit_load *******************************************************************
52 Emits a possible load of an operand.
54 *******************************************************************************/
56 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
62 /* get required compiler data */
66 if (src->flags & INMEMORY) {
69 disp = src->vv.regoff;
71 if (IS_FLT_DBL_TYPE(src->type)) {
72 M_DLD(tempreg, REG_SP, disp);
75 M_LLD(tempreg, REG_SP, disp);
87 /* emit_store ******************************************************************
89 Emits a possible store to a variable.
91 *******************************************************************************/
93 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
97 /* get required compiler data */
101 if (dst->flags & INMEMORY) {
104 if (IS_FLT_DBL_TYPE(dst->type)) {
105 M_DST(d, REG_SP, dst->vv.regoff);
108 M_LST(d, REG_SP, dst->vv.regoff);
114 /* emit_copy *******************************************************************
116 Generates a register/memory to register/memory copy.
118 *******************************************************************************/
120 void emit_copy(jitdata *jd, instruction *iptr)
127 /* get required compiler data */
131 /* get source and destination variables */
133 src = VAROP(iptr->s1);
134 dst = VAROP(iptr->dst);
136 if ((src->vv.regoff != dst->vv.regoff) ||
137 ((src->flags ^ dst->flags) & INMEMORY)) {
139 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
140 /* emit nothing, as the value won't be used anyway */
144 /* If one of the variables resides in memory, we can eliminate
145 the register move from/to the temporary register with the
146 order of getting the destination register and the load. */
148 if (IS_INMEMORY(src->flags)) {
149 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
150 s1 = emit_load(jd, iptr, src, d);
153 s1 = emit_load(jd, iptr, src, REG_IFTMP);
154 d = codegen_reg_of_var(iptr->opc, dst, s1);
158 if (IS_FLT_DBL_TYPE(src->type))
164 emit_store(jd, iptr, dst, d);
169 /* emit_iconst *****************************************************************
173 *******************************************************************************/
175 void emit_iconst(codegendata *cd, s4 d, s4 value)
179 if ((value >= -32768) && (value <= 32767)) {
180 M_LDA_INTERN(d, REG_ZERO, value);
182 disp = dseg_add_s4(cd, value);
183 M_ILD(d, REG_PV, disp);
187 void emit_lconst(codegendata *cd, s4 d, s8 value)
190 if ((value >= -32768) && (value <= 32767)) {
191 M_LDA_INTERN(d, REG_ZERO, value);
193 disp = dseg_add_s8(cd, value);
194 M_LLD(d, REG_PV, disp);
200 * Emits code updating the condition register by comparing one integer
201 * register to an immediate integer value.
203 void emit_icmp_imm(codegendata* cd, int reg, int32_t value)
207 if ((value >= -32768) && (value <= 32767)) {
210 assert(reg != REG_ITMP2);
211 disp = dseg_add_s4(cd, value);
212 M_ILD(REG_ITMP2, REG_PV, disp);
213 M_CMP(reg, REG_ITMP2);
219 * Generates synchronization code to enter a monitor.
221 #if defined(ENABLE_THREADS)
222 void emit_monitor_enter(jitdata* jd, int32_t syncslot_offset)
226 // Get required compiler data.
227 methodinfo* m = jd->m;
228 codegendata* cd = jd->cd;
230 #if !defined (NDEBUG)
231 if (JITDATA_HAS_FLAG_VERBOSECALL(jd)) {
232 M_AADD_IMM(REG_SP, -((LA_SIZE_IN_POINTERS + PA_SIZE_IN_POINTERS + ARG_CNT) * 8), REG_SP);
234 for (p = 0; p < INT_ARG_CNT; p++)
235 M_LST(abi_registers_integer_argument[p], REG_SP, LA_SIZE + PA_SIZE + p * 8);
237 for (p = 0; p < FLT_ARG_CNT; p++)
238 M_DST(abi_registers_float_argument[p], REG_SP, LA_SIZE + PA_SIZE + (INT_ARG_CNT + p) * 8);
240 /* used for LOCK_monitor_exit, adopt size because we created another stackframe */
241 syncslot_offset += (LA_SIZE_IN_POINTERS + PA_SIZE_IN_POINTERS + ARG_CNT) * 8;
245 p = dseg_add_functionptr(cd, LOCK_monitor_enter);
246 M_ALD(REG_ITMP3, REG_PV, p);
247 M_ALD(REG_ITMP3, REG_ITMP3, 0); /* TOC */
250 /* get or test the lock object */
252 if (m->flags & ACC_STATIC) {
253 p = dseg_add_address(cd, &m->clazz->object.header);
254 M_ALD(REG_A0, REG_PV, p);
259 M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_NullPointerException);
262 M_AST(REG_A0, REG_SP, syncslot_offset);
266 if (JITDATA_HAS_FLAG_VERBOSECALL(jd)) {
267 for (p = 0; p < INT_ARG_CNT; p++)
268 M_LLD(abi_registers_integer_argument[p], REG_SP, LA_SIZE + PA_SIZE + p * 8);
270 for (p = 0; p < FLT_ARG_CNT; p++)
271 M_DLD(abi_registers_float_argument[p], REG_SP, LA_SIZE + PA_SIZE + (INT_ARG_CNT + p) * 8);
273 M_AADD_IMM(REG_SP, (LA_SIZE_IN_POINTERS + PA_SIZE_IN_POINTERS + ARG_CNT) * 8, REG_SP);
281 * Generates synchronization code to leave a monitor.
283 #if defined(ENABLE_THREADS)
284 void emit_monitor_exit(jitdata* jd, int32_t syncslot_offset)
288 // Get required compiler data.
289 methodinfo* m = jd->m;
290 codegendata* cd = jd->cd;
292 disp = dseg_add_functionptr(cd, LOCK_monitor_exit);
293 M_ALD(REG_ITMP3, REG_PV, disp);
294 M_ALD(REG_ITMP3, REG_ITMP3, 0); /* TOC */
297 /* we need to save the proper return value */
299 methoddesc* md = m->parseddesc;
301 switch (md->returntype.type) {
306 M_LST(REG_RESULT , REG_SP, syncslot_offset + 8);
309 M_FST(REG_FRESULT, REG_SP, syncslot_offset + 8);
312 M_DST(REG_FRESULT, REG_SP, syncslot_offset + 8);
316 M_ALD(REG_A0, REG_SP, syncslot_offset);
319 /* and now restore the proper return value */
321 switch (md->returntype.type) {
326 M_LLD(REG_RESULT , REG_SP, syncslot_offset + 8);
329 M_FLD(REG_FRESULT, REG_SP, syncslot_offset + 8);
332 M_DLD(REG_FRESULT, REG_SP, syncslot_offset + 8);
339 /* emit_verbosecall_enter ******************************************************
341 Generates the code for the call trace.
343 *******************************************************************************/
346 void emit_verbosecall_enter(jitdata *jd)
352 int32_t stackframesize;
356 /* get required compiler data */
363 /* mark trace code */
367 /* align stack to 16-bytes */
369 paramcount = md->paramcount;
371 stackframesize = LA_SIZE + PA_SIZE + md->paramcount * 8;
374 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
375 M_STDU(REG_SP, REG_SP, -stackframesize);
377 #if defined(__DARWIN__)
378 #warning "emit_verbosecall_enter not implemented"
380 /* save argument registers */
382 for (i = 0; i < md->paramcount; i++) {
383 if (!md->params[i].inmemory) {
384 s = md->params[i].regoff;
386 switch (md->paramtypes[i].type) {
390 M_LST(s, REG_SP, LA_SIZE+PA_SIZE+i*8);
394 M_DST(s, REG_SP, LA_SIZE+PA_SIZE+i*8);
401 disp = dseg_add_address(cd, m);
402 M_ALD(REG_A0, REG_PV, disp);
403 M_AADD_IMM(REG_SP, LA_SIZE+PA_SIZE, REG_A1);
404 M_AADD_IMM(REG_SP, stackframesize + cd->stackframesize * 8, REG_A2);
405 /* call via function descriptor, XXX: what about TOC? */
406 disp = dseg_add_functionptr(cd, trace_java_call_enter);
407 M_ALD(REG_ITMP2, REG_PV, disp);
408 M_ALD(REG_ITMP1, REG_ITMP2, 0);
412 #if defined(__DARWIN__)
413 #warning "emit_verbosecall_enter not implemented"
415 /* restore argument registers */
417 for (i = 0; i < md->paramcount; i++) {
418 if (!md->params[i].inmemory) {
419 s = md->params[i].regoff;
421 switch (md->paramtypes[i].type) {
425 M_LLD(s, REG_SP, LA_SIZE+PA_SIZE+i*8);
429 M_DLD(s, REG_SP, LA_SIZE+PA_SIZE+i*8);
436 M_ALD(REG_ZERO, REG_SP, stackframesize + LA_LR_OFFSET);
438 M_LDA(REG_SP, REG_SP, stackframesize);
440 /* mark trace code */
447 /* emit_verbosecall_exit ******************************************************
449 Generates the code for the call trace.
451 *******************************************************************************/
454 void emit_verbosecall_exit(jitdata *jd)
461 /* get required compiler data */
468 /* mark trace code */
473 M_LDA(REG_SP, REG_SP, -(LA_SIZE+PA_SIZE+10*8));
474 M_AST(REG_ZERO, REG_SP, LA_SIZE+PA_SIZE+1*8);
476 /* save return value */
478 switch (md->returntype.type) {
482 M_LST(REG_RESULT, REG_SP, LA_SIZE+PA_SIZE+0*8);
486 M_DST(REG_FRESULT, REG_SP, LA_SIZE+PA_SIZE+0*8);
490 disp = dseg_add_address(cd, m);
491 M_ALD(REG_A0, REG_PV, disp);
492 M_AADD_IMM(REG_SP, LA_SIZE+PA_SIZE, REG_A1);
494 disp = dseg_add_functionptr(cd, trace_java_call_exit);
495 /* call via function descriptor, XXX: what about TOC ? */
496 M_ALD(REG_ITMP2, REG_PV, disp);
497 M_ALD(REG_ITMP2, REG_ITMP2, 0);
501 /* restore return value */
503 switch (md->returntype.type) {
507 M_LLD(REG_RESULT, REG_SP, LA_SIZE+PA_SIZE+0*8);
511 M_DLD(REG_FRESULT, REG_SP, LA_SIZE+PA_SIZE+0*8);
515 M_ALD(REG_ZERO, REG_SP, LA_SIZE+PA_SIZE+1*8);
516 M_LDA(REG_SP, REG_SP, LA_SIZE+PA_SIZE+10*8);
519 /* mark trace code */
526 /* emit_branch *****************************************************************
528 Emits the code for conditional and unconditional branchs.
530 *******************************************************************************/
532 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
537 /* calculate the different displacements */
539 checkdisp = disp + 4;
540 branchdisp = (disp - 4) >> 2;
542 /* check which branch to generate */
544 if (condition == BRANCH_UNCONDITIONAL) {
545 /* check displacement for overflow */
547 if ((checkdisp < (s4) 0xfe000000) || (checkdisp > (s4) 0x01fffffc)) {
548 /* if the long-branches flag isn't set yet, do it */
550 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
551 cd->flags |= (CODEGENDATA_FLAG_ERROR |
552 CODEGENDATA_FLAG_LONGBRANCHES);
555 vm_abort("emit_branch: emit unconditional long-branch code");
562 /* and displacement for overflow */
564 if ((checkdisp < (s4) 0xffff8000) || (checkdisp > (s4) 0x00007fff)) {
565 /* if the long-branches flag isn't set yet, do it */
567 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
568 cd->flags |= (CODEGENDATA_FLAG_ERROR |
569 CODEGENDATA_FLAG_LONGBRANCHES);
572 // Subtract 1 instruction from the displacement as the
573 // actual branch is the second instruction.
574 checkdisp = checkdisp - 4;
575 branchdisp = branchdisp - 1;
577 if ((checkdisp < (int32_t) 0xfe000000) || (checkdisp > (int32_t) 0x01fffffc)) {
578 vm_abort("emit_branch: emit conditional long-branch code");
607 vm_abort("emit_branch: long BRANCH_NAN");
610 vm_abort("emit_branch: unknown condition %d", condition);
638 vm_abort("emit_branch: unknown condition %d", condition);
644 /* emit_arrayindexoutofbounds_check ********************************************
646 Emit a ArrayIndexOutOfBoundsException check.
648 *******************************************************************************/
650 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
653 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
654 M_CMPU(s2, REG_ITMP3);
656 /* ALD is 4 byte aligned, ILD 2, onyl LWZ is byte aligned */
657 M_LWZ(s2, REG_ZERO, TRAP_ArrayIndexOutOfBoundsException);
662 /* emit_arraystore_check *******************************************************
664 Emit an ArrayStoreException check.
666 *******************************************************************************/
668 void emit_arraystore_check(codegendata *cd, instruction *iptr)
670 if (INSTRUCTION_MUST_CHECK(iptr)) {
673 /* ALD is 4 byte aligned, ILD 2, onyl LWZ is byte aligned */
674 M_LWZ(REG_ZERO, REG_ZERO, TRAP_ArrayStoreException);
679 /* emit_arithmetic_check *******************************************************
681 Emit an ArithmeticException check.
683 *******************************************************************************/
685 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
687 if (INSTRUCTION_MUST_CHECK(iptr)) {
690 /* ALD is 4 byte aligned, ILD 2, onyl LWZ is byte aligned */
691 M_LWZ(REG_ZERO, REG_ZERO, TRAP_ArithmeticException);
696 /* emit_classcast_check ********************************************************
698 Emit a ClassCastException check.
700 *******************************************************************************/
702 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
704 if (INSTRUCTION_MUST_CHECK(iptr)) {
719 vm_abort("emit_classcast_check: unknown condition %d", condition);
722 /* ALD is 4 byte aligned, ILD 2, onyl LWZ is byte aligned */
723 M_LWZ(s1, REG_ZERO, TRAP_ClassCastException);
728 /* emit_nullpointer_check ******************************************************
730 Emit a NullPointerException check.
732 *******************************************************************************/
734 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
736 if (INSTRUCTION_MUST_CHECK(iptr)) {
739 /* ALD is 4 byte aligned, ILD 2, onyl LWZ is byte aligned */
740 M_LWZ(REG_ZERO, REG_ZERO, TRAP_NullPointerException);
744 /* emit_exception_check ********************************************************
746 Emit an Exception check.
748 *******************************************************************************/
750 void emit_exception_check(codegendata *cd, instruction *iptr)
752 if (INSTRUCTION_MUST_CHECK(iptr)) {
755 /* ALD is 4 byte aligned, ILD 2, onyl LWZ is byte aligned */
756 M_LWZ(REG_ZERO, REG_ZERO, TRAP_CHECK_EXCEPTION);
761 /* emit_trap_compiler **********************************************************
763 Emit a trap instruction which calls the JIT compiler.
765 *******************************************************************************/
767 void emit_trap_compiler(codegendata *cd)
769 M_LWZ(REG_METHODPTR, REG_ZERO, TRAP_COMPILER);
773 /* emit_trap *******************************************************************
775 Emit a trap instruction and return the original machine code.
777 *******************************************************************************/
779 uint32_t emit_trap(codegendata *cd)
781 // Get machine code which is patched back in later. The trap is 1
782 // instruction word long.
783 uint32_t mcode = *((uint32_t*) cd->mcodeptr);
792 * Emit code to recompute the procedure vector.
794 void emit_recompute_pv(codegendata *cd)
796 int32_t disp = (int32_t) (cd->mcodeptr - cd->mcodebase);
799 M_LDA(REG_PV, REG_ITMP1, -disp);
804 * These are local overrides for various environment variables in Emacs.
805 * Please do not remove this and leave it at the end of the file, where
806 * Emacs will automagically detect them.
807 * ---------------------------------------------------------------------
810 * indent-tabs-mode: t
814 * vim:noexpandtab:sw=4:ts=4: