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.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.h"
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);
199 /* emit_verbosecall_enter ******************************************************
201 Generates the code for the call trace.
203 *******************************************************************************/
206 void emit_verbosecall_enter(jitdata *jd)
212 int32_t stackframesize;
216 /* get required compiler data */
223 /* mark trace code */
227 /* align stack to 16-bytes */
229 paramcount = md->paramcount;
231 stackframesize = LA_SIZE + PA_SIZE + md->paramcount * 8;
234 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
235 M_STDU(REG_SP, REG_SP, -stackframesize);
237 #if defined(__DARWIN__)
238 #warning "emit_verbosecall_enter not implemented"
240 /* save argument registers */
242 for (i = 0; i < md->paramcount; i++) {
243 if (!md->params[i].inmemory) {
244 s = md->params[i].regoff;
246 switch (md->paramtypes[i].type) {
250 M_LST(s, REG_SP, LA_SIZE+PA_SIZE+i*8);
254 M_DST(s, REG_SP, LA_SIZE+PA_SIZE+i*8);
261 disp = dseg_add_address(cd, m);
262 M_ALD(REG_A0, REG_PV, disp);
263 M_AADD_IMM(REG_SP, LA_SIZE+PA_SIZE, REG_A1);
264 M_AADD_IMM(REG_SP, stackframesize + cd->stackframesize * 8, REG_A2);
265 /* call via function descriptor, XXX: what about TOC? */
266 disp = dseg_add_functionptr(cd, trace_java_call_enter);
267 M_ALD(REG_ITMP2, REG_PV, disp);
268 M_ALD(REG_ITMP1, REG_ITMP2, 0);
272 #if defined(__DARWIN__)
273 #warning "emit_verbosecall_enter not implemented"
275 /* restore argument registers */
277 for (i = 0; i < md->paramcount; i++) {
278 if (!md->params[i].inmemory) {
279 s = md->params[i].regoff;
281 switch (md->paramtypes[i].type) {
285 M_LLD(s, REG_SP, LA_SIZE+PA_SIZE+i*8);
289 M_DLD(s, REG_SP, LA_SIZE+PA_SIZE+i*8);
296 M_ALD(REG_ZERO, REG_SP, stackframesize + LA_LR_OFFSET);
298 M_LDA(REG_SP, REG_SP, stackframesize);
300 /* mark trace code */
307 /* emit_verbosecall_exit ******************************************************
309 Generates the code for the call trace.
311 *******************************************************************************/
314 void emit_verbosecall_exit(jitdata *jd)
321 /* get required compiler data */
328 /* mark trace code */
333 M_LDA(REG_SP, REG_SP, -(LA_SIZE+PA_SIZE+10*8));
334 M_AST(REG_ZERO, REG_SP, LA_SIZE+PA_SIZE+1*8);
336 /* save return value */
338 switch (md->returntype.type) {
342 M_LST(REG_RESULT, REG_SP, LA_SIZE+PA_SIZE+0*8);
346 M_DST(REG_FRESULT, REG_SP, LA_SIZE+PA_SIZE+0*8);
350 disp = dseg_add_address(cd, m);
351 M_ALD(REG_A0, REG_PV, disp);
352 M_AADD_IMM(REG_SP, LA_SIZE+PA_SIZE, REG_A1);
354 disp = dseg_add_functionptr(cd, trace_java_call_exit);
355 /* call via function descriptor, XXX: what about TOC ? */
356 M_ALD(REG_ITMP2, REG_PV, disp);
357 M_ALD(REG_ITMP2, REG_ITMP2, 0);
361 /* restore return value */
363 switch (md->returntype.type) {
367 M_LLD(REG_RESULT, REG_SP, LA_SIZE+PA_SIZE+0*8);
371 M_DLD(REG_FRESULT, REG_SP, LA_SIZE+PA_SIZE+0*8);
375 M_ALD(REG_ZERO, REG_SP, LA_SIZE+PA_SIZE+1*8);
376 M_LDA(REG_SP, REG_SP, LA_SIZE+PA_SIZE+10*8);
379 /* mark trace code */
386 /* emit_branch *****************************************************************
388 Emits the code for conditional and unconditional branchs.
390 *******************************************************************************/
392 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
397 /* calculate the different displacements */
399 checkdisp = disp + 4;
400 branchdisp = (disp - 4) >> 2;
402 /* check which branch to generate */
404 if (condition == BRANCH_UNCONDITIONAL) {
405 /* check displacement for overflow */
407 if ((checkdisp < (s4) 0xfe000000) || (checkdisp > (s4) 0x01fffffc)) {
408 /* if the long-branches flag isn't set yet, do it */
410 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
411 cd->flags |= (CODEGENDATA_FLAG_ERROR |
412 CODEGENDATA_FLAG_LONGBRANCHES);
415 vm_abort("emit_branch: emit unconditional long-branch code");
422 /* and displacement for overflow */
424 if ((checkdisp < (s4) 0xffff8000) || (checkdisp > (s4) 0x00007fff)) {
425 /* if the long-branches flag isn't set yet, do it */
427 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
428 cd->flags |= (CODEGENDATA_FLAG_ERROR |
429 CODEGENDATA_FLAG_LONGBRANCHES);
432 // Subtract 1 instruction from the displacement as the
433 // actual branch is the second instruction.
434 checkdisp = checkdisp - 4;
435 branchdisp = branchdisp - 1;
437 if ((checkdisp < (int32_t) 0xfe000000) || (checkdisp > (int32_t) 0x01fffffc)) {
438 vm_abort("emit_branch: emit conditional long-branch code");
467 vm_abort("emit_branch: long BRANCH_NAN");
470 vm_abort("emit_branch: unknown condition %d", condition);
498 vm_abort("emit_branch: unknown condition %d", condition);
504 /* emit_arrayindexoutofbounds_check ********************************************
506 Emit a ArrayIndexOutOfBoundsException check.
508 *******************************************************************************/
510 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
513 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
514 M_CMPU(s2, REG_ITMP3);
516 /* ALD is 4 byte aligned, ILD 2, onyl LWZ is byte aligned */
517 M_LWZ(s2, REG_ZERO, TRAP_ArrayIndexOutOfBoundsException);
522 /* emit_arraystore_check *******************************************************
524 Emit an ArrayStoreException check.
526 *******************************************************************************/
528 void emit_arraystore_check(codegendata *cd, instruction *iptr)
530 if (INSTRUCTION_MUST_CHECK(iptr)) {
533 /* ALD is 4 byte aligned, ILD 2, onyl LWZ is byte aligned */
534 M_LWZ(REG_ZERO, REG_ZERO, TRAP_ArrayStoreException);
539 /* emit_arithmetic_check *******************************************************
541 Emit an ArithmeticException check.
543 *******************************************************************************/
545 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
547 if (INSTRUCTION_MUST_CHECK(iptr)) {
550 /* ALD is 4 byte aligned, ILD 2, onyl LWZ is byte aligned */
551 M_LWZ(REG_ZERO, REG_ZERO, TRAP_ArithmeticException);
556 /* emit_classcast_check ********************************************************
558 Emit a ClassCastException check.
560 *******************************************************************************/
562 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
564 if (INSTRUCTION_MUST_CHECK(iptr)) {
576 vm_abort("emit_classcast_check: unknown condition %d", condition);
579 /* ALD is 4 byte aligned, ILD 2, onyl LWZ is byte aligned */
580 M_LWZ(s1, REG_ZERO, TRAP_ClassCastException);
585 /* emit_nullpointer_check ******************************************************
587 Emit a NullPointerException check.
589 *******************************************************************************/
591 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
593 if (INSTRUCTION_MUST_CHECK(iptr)) {
596 /* ALD is 4 byte aligned, ILD 2, onyl LWZ is byte aligned */
597 M_LWZ(REG_ZERO, REG_ZERO, TRAP_NullPointerException);
601 /* emit_exception_check ********************************************************
603 Emit an Exception check.
605 *******************************************************************************/
607 void emit_exception_check(codegendata *cd, instruction *iptr)
609 if (INSTRUCTION_MUST_CHECK(iptr)) {
612 /* ALD is 4 byte aligned, ILD 2, onyl LWZ is byte aligned */
613 M_LWZ(REG_ZERO, REG_ZERO, TRAP_CHECK_EXCEPTION);
618 /* emit_trap_compiler **********************************************************
620 Emit a trap instruction which calls the JIT compiler.
622 *******************************************************************************/
624 void emit_trap_compiler(codegendata *cd)
626 M_LWZ(REG_METHODPTR, REG_ZERO, TRAP_COMPILER);
630 /* emit_trap *******************************************************************
632 Emit a trap instruction and return the original machine code.
634 *******************************************************************************/
636 uint32_t emit_trap(codegendata *cd)
638 // Get machine code which is patched back in later. The trap is 1
639 // instruction word long.
640 uint32_t mcode = *((uint32_t*) cd->mcodeptr);
649 * These are local overrides for various environment variables in Emacs.
650 * Please do not remove this and leave it at the end of the file, where
651 * Emacs will automagically detect them.
652 * ---------------------------------------------------------------------
655 * indent-tabs-mode: t
659 * vim:noexpandtab:sw=4:ts=4: