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.h"
35 #include "vm/jit/powerpc64/codegen.h"
37 #include "threads/lock-common.h"
41 #include "vm/jit/abi.h"
42 #include "vm/jit/asmpart.h"
43 #include "vm/jit/emit-common.h"
44 #include "vm/jit/jit.h"
45 #include "vm/jit/trace.hpp"
46 #include "vm/jit/trap.h"
48 #include "vmcore/options.h"
51 /* emit_load *******************************************************************
53 Emits a possible load of an operand.
55 *******************************************************************************/
57 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
63 /* get required compiler data */
67 if (src->flags & INMEMORY) {
70 disp = src->vv.regoff;
72 if (IS_FLT_DBL_TYPE(src->type)) {
73 M_DLD(tempreg, REG_SP, disp);
76 M_LLD(tempreg, REG_SP, disp);
88 /* emit_store ******************************************************************
90 Emits a possible store to a variable.
92 *******************************************************************************/
94 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
98 /* get required compiler data */
102 if (dst->flags & INMEMORY) {
105 if (IS_FLT_DBL_TYPE(dst->type)) {
106 M_DST(d, REG_SP, dst->vv.regoff);
109 M_LST(d, REG_SP, dst->vv.regoff);
115 /* emit_copy *******************************************************************
117 Generates a register/memory to register/memory copy.
119 *******************************************************************************/
121 void emit_copy(jitdata *jd, instruction *iptr)
128 /* get required compiler data */
132 /* get source and destination variables */
134 src = VAROP(iptr->s1);
135 dst = VAROP(iptr->dst);
137 if ((src->vv.regoff != dst->vv.regoff) ||
138 ((src->flags ^ dst->flags) & INMEMORY)) {
140 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
141 /* emit nothing, as the value won't be used anyway */
145 /* If one of the variables resides in memory, we can eliminate
146 the register move from/to the temporary register with the
147 order of getting the destination register and the load. */
149 if (IS_INMEMORY(src->flags)) {
150 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
151 s1 = emit_load(jd, iptr, src, d);
154 s1 = emit_load(jd, iptr, src, REG_IFTMP);
155 d = codegen_reg_of_var(iptr->opc, dst, s1);
159 if (IS_FLT_DBL_TYPE(src->type))
165 emit_store(jd, iptr, dst, d);
170 /* emit_iconst *****************************************************************
174 *******************************************************************************/
176 void emit_iconst(codegendata *cd, s4 d, s4 value)
180 if ((value >= -32768) && (value <= 32767)) {
181 M_LDA_INTERN(d, REG_ZERO, value);
183 disp = dseg_add_s4(cd, value);
184 M_ILD(d, REG_PV, disp);
188 void emit_lconst(codegendata *cd, s4 d, s8 value)
191 if ((value >= -32768) && (value <= 32767)) {
192 M_LDA_INTERN(d, REG_ZERO, value);
194 disp = dseg_add_s8(cd, value);
195 M_LLD(d, REG_PV, disp);
200 /* emit_verbosecall_enter ******************************************************
202 Generates the code for the call trace.
204 *******************************************************************************/
207 void emit_verbosecall_enter(jitdata *jd)
213 int32_t stackframesize;
217 /* get required compiler data */
224 /* mark trace code */
228 /* align stack to 16-bytes */
230 paramcount = md->paramcount;
232 stackframesize = LA_SIZE + PA_SIZE + md->paramcount * 8;
235 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
236 M_STDU(REG_SP, REG_SP, -stackframesize);
238 #if defined(__DARWIN__)
239 #warning "emit_verbosecall_enter not implemented"
241 /* save argument registers */
243 for (i = 0; i < md->paramcount; i++) {
244 if (!md->params[i].inmemory) {
245 s = md->params[i].regoff;
247 switch (md->paramtypes[i].type) {
251 M_LST(s, REG_SP, LA_SIZE+PA_SIZE+i*8);
255 M_DST(s, REG_SP, LA_SIZE+PA_SIZE+i*8);
262 disp = dseg_add_address(cd, m);
263 M_ALD(REG_A0, REG_PV, disp);
264 M_AADD_IMM(REG_SP, LA_SIZE+PA_SIZE, REG_A1);
265 M_AADD_IMM(REG_SP, stackframesize + cd->stackframesize * 8, REG_A2);
266 /* call via function descriptor, XXX: what about TOC? */
267 disp = dseg_add_functionptr(cd, trace_java_call_enter);
268 M_ALD(REG_ITMP2, REG_PV, disp);
269 M_ALD(REG_ITMP1, REG_ITMP2, 0);
273 #if defined(__DARWIN__)
274 #warning "emit_verbosecall_enter not implemented"
276 /* restore argument registers */
278 for (i = 0; i < md->paramcount; i++) {
279 if (!md->params[i].inmemory) {
280 s = md->params[i].regoff;
282 switch (md->paramtypes[i].type) {
286 M_LLD(s, REG_SP, LA_SIZE+PA_SIZE+i*8);
290 M_DLD(s, REG_SP, LA_SIZE+PA_SIZE+i*8);
297 M_ALD(REG_ZERO, REG_SP, stackframesize + LA_LR_OFFSET);
299 M_LDA(REG_SP, REG_SP, stackframesize);
301 /* mark trace code */
308 /* emit_verbosecall_exit ******************************************************
310 Generates the code for the call trace.
312 *******************************************************************************/
315 void emit_verbosecall_exit(jitdata *jd)
322 /* get required compiler data */
329 /* mark trace code */
334 M_LDA(REG_SP, REG_SP, -(LA_SIZE+PA_SIZE+10*8));
335 M_AST(REG_ZERO, REG_SP, LA_SIZE+PA_SIZE+1*8);
337 /* save return value */
339 switch (md->returntype.type) {
343 M_LST(REG_RESULT, REG_SP, LA_SIZE+PA_SIZE+0*8);
347 M_DST(REG_FRESULT, REG_SP, LA_SIZE+PA_SIZE+0*8);
351 disp = dseg_add_address(cd, m);
352 M_ALD(REG_A0, REG_PV, disp);
353 M_AADD_IMM(REG_SP, LA_SIZE+PA_SIZE, REG_A1);
355 disp = dseg_add_functionptr(cd, trace_java_call_exit);
356 /* call via function descriptor, XXX: what about TOC ? */
357 M_ALD(REG_ITMP2, REG_PV, disp);
358 M_ALD(REG_ITMP2, REG_ITMP2, 0);
362 /* restore return value */
364 switch (md->returntype.type) {
368 M_LLD(REG_RESULT, REG_SP, LA_SIZE+PA_SIZE+0*8);
372 M_DLD(REG_FRESULT, REG_SP, LA_SIZE+PA_SIZE+0*8);
376 M_ALD(REG_ZERO, REG_SP, LA_SIZE+PA_SIZE+1*8);
377 M_LDA(REG_SP, REG_SP, LA_SIZE+PA_SIZE+10*8);
380 /* mark trace code */
387 /* emit_branch *****************************************************************
389 Emits the code for conditional and unconditional branchs.
391 *******************************************************************************/
393 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
398 /* calculate the different displacements */
400 checkdisp = disp + 4;
401 branchdisp = (disp - 4) >> 2;
403 /* check which branch to generate */
405 if (condition == BRANCH_UNCONDITIONAL) {
406 /* check displacement for overflow */
408 if ((checkdisp < (s4) 0xfe000000) || (checkdisp > (s4) 0x01fffffc)) {
409 /* if the long-branches flag isn't set yet, do it */
411 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
412 cd->flags |= (CODEGENDATA_FLAG_ERROR |
413 CODEGENDATA_FLAG_LONGBRANCHES);
416 vm_abort("emit_branch: emit unconditional long-branch code");
423 /* and displacement for overflow */
425 if ((checkdisp < (s4) 0xffff8000) || (checkdisp > (s4) 0x00007fff)) {
426 /* if the long-branches flag isn't set yet, do it */
428 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
429 cd->flags |= (CODEGENDATA_FLAG_ERROR |
430 CODEGENDATA_FLAG_LONGBRANCHES);
433 branchdisp --; /* we jump from the second instruction */
460 vm_abort("emit_branch: long BRANCH_NAN");
463 vm_abort("emit_branch: unknown condition %d", condition);
491 vm_abort("emit_branch: unknown condition %d", condition);
497 /* emit_arrayindexoutofbounds_check ********************************************
499 Emit a ArrayIndexOutOfBoundsException check.
501 *******************************************************************************/
503 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
506 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
507 M_CMPU(s2, REG_ITMP3);
509 /* ALD is 4 byte aligned, ILD 2, onyl LWZ is byte aligned */
510 M_LWZ(s2, REG_ZERO, TRAP_ArrayIndexOutOfBoundsException);
515 /* emit_arraystore_check *******************************************************
517 Emit an ArrayStoreException check.
519 *******************************************************************************/
521 void emit_arraystore_check(codegendata *cd, instruction *iptr)
523 if (INSTRUCTION_MUST_CHECK(iptr)) {
526 /* ALD is 4 byte aligned, ILD 2, onyl LWZ is byte aligned */
527 M_LWZ(REG_ZERO, REG_ZERO, TRAP_ArrayStoreException);
532 /* emit_arithmetic_check *******************************************************
534 Emit an ArithmeticException check.
536 *******************************************************************************/
538 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
540 if (INSTRUCTION_MUST_CHECK(iptr)) {
543 /* ALD is 4 byte aligned, ILD 2, onyl LWZ is byte aligned */
544 M_LWZ(REG_ZERO, REG_ZERO, TRAP_ArithmeticException);
549 /* emit_classcast_check ********************************************************
551 Emit a ClassCastException check.
553 *******************************************************************************/
555 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
557 if (INSTRUCTION_MUST_CHECK(iptr)) {
569 vm_abort("emit_classcast_check: unknown condition %d", condition);
572 /* ALD is 4 byte aligned, ILD 2, onyl LWZ is byte aligned */
573 M_LWZ(s1, REG_ZERO, TRAP_ClassCastException);
578 /* emit_nullpointer_check ******************************************************
580 Emit a NullPointerException check.
582 *******************************************************************************/
584 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
586 if (INSTRUCTION_MUST_CHECK(iptr)) {
589 /* ALD is 4 byte aligned, ILD 2, onyl LWZ is byte aligned */
590 M_LWZ(REG_ZERO, REG_ZERO, TRAP_NullPointerException);
594 /* emit_exception_check ********************************************************
596 Emit an Exception check.
598 *******************************************************************************/
600 void emit_exception_check(codegendata *cd, instruction *iptr)
602 if (INSTRUCTION_MUST_CHECK(iptr)) {
605 /* ALD is 4 byte aligned, ILD 2, onyl LWZ is byte aligned */
606 M_LWZ(REG_ZERO, REG_ZERO, TRAP_CHECK_EXCEPTION);
611 /* emit_trap_compiler **********************************************************
613 Emit a trap instruction which calls the JIT compiler.
615 *******************************************************************************/
617 void emit_trap_compiler(codegendata *cd)
619 M_LWZ(REG_METHODPTR, REG_ZERO, TRAP_COMPILER);
623 /* emit_trap *******************************************************************
625 Emit a trap instruction and return the original machine code.
627 *******************************************************************************/
629 uint32_t emit_trap(codegendata *cd)
633 /* Get machine code which is patched back in later. The
634 trap is 1 instruction word long. */
636 mcode = *((uint32_t *) cd->mcodeptr);
638 /* ALD is 4 byte aligned, ILD 2, only LWZ is byte aligned */
639 M_LWZ(REG_ZERO, REG_ZERO, TRAP_PATCHER);
646 * These are local overrides for various environment variables in Emacs.
647 * Please do not remove this and leave it at the end of the file, where
648 * Emacs will automagically detect them.
649 * ---------------------------------------------------------------------
652 * indent-tabs-mode: t
656 * vim:noexpandtab:sw=4:ts=4: