1 /* src/vm/jit/powerpc/emit.c - PowerPC code emitter functions
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 $Id: emit.c 4398 2006-01-31 23:43:08Z twisti $
38 #include "vm/jit/powerpc/codegen.h"
40 #include "mm/memory.h"
42 #if defined(ENABLE_THREADS)
43 # include "threads/native/lock.h"
46 #include "vm/builtin.h"
47 #include "vm/exceptions.h"
49 #include "vm/jit/asmpart.h"
50 #include "vm/jit/dseg.h"
51 #include "vm/jit/emit-common.h"
52 #include "vm/jit/jit.h"
53 #include "vm/jit/replace.h"
55 #include "vmcore/options.h"
58 /* emit_load *******************************************************************
60 Emits a possible load of an operand.
62 *******************************************************************************/
64 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
70 /* get required compiler data */
74 if (IS_INMEMORY(src->flags)) {
77 disp = src->vv.regoff * 4;
82 M_ILD(tempreg, REG_SP, disp);
85 M_LLD(tempreg, REG_SP, disp);
88 M_FLD(tempreg, REG_SP, disp);
91 M_DLD(tempreg, REG_SP, disp);
94 vm_abort("emit_load: unknown type %d", src->type);
100 reg = src->vv.regoff;
106 /* emit_load_low ***************************************************************
108 Emits a possible load of the low 32-bits of an operand.
110 *******************************************************************************/
112 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
118 assert(src->type == TYPE_LNG);
120 /* get required compiler data */
124 if (IS_INMEMORY(src->flags)) {
127 disp = src->vv.regoff * 4;
129 M_ILD(tempreg, REG_SP, disp + 4);
134 reg = GET_LOW_REG(src->vv.regoff);
140 /* emit_load_high **************************************************************
142 Emits a possible load of the high 32-bits of an operand.
144 *******************************************************************************/
146 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
152 assert(src->type == TYPE_LNG);
154 /* get required compiler data */
158 if (IS_INMEMORY(src->flags)) {
161 disp = src->vv.regoff * 4;
163 M_ILD(tempreg, REG_SP, disp);
168 reg = GET_HIGH_REG(src->vv.regoff);
174 /* emit_store ******************************************************************
176 Emit a possible store for the given variable.
178 *******************************************************************************/
180 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
185 /* get required compiler data */
189 if (IS_INMEMORY(dst->flags)) {
192 disp = dst->vv.regoff * 4;
197 M_IST(d, REG_SP, disp);
200 M_LST(d, REG_SP, disp);
203 M_FST(d, REG_SP, disp);
206 M_DST(d, REG_SP, disp);
209 vm_abort("emit_store: unknown type %d", dst->type);
215 /* emit_copy *******************************************************************
217 Generates a register/memory to register/memory copy.
219 *******************************************************************************/
221 void emit_copy(jitdata *jd, instruction *iptr, varinfo *src, varinfo *dst)
226 /* get required compiler data */
230 if ((src->vv.regoff != dst->vv.regoff) ||
231 (IS_INMEMORY(src->flags ^ dst->flags))) {
233 /* If one of the variables resides in memory, we can eliminate
234 the register move from/to the temporary register with the
235 order of getting the destination register and the load. */
237 if (IS_INMEMORY(src->flags)) {
238 if (IS_LNG_TYPE(src->type))
239 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
241 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
243 s1 = emit_load(jd, iptr, src, d);
246 if (IS_LNG_TYPE(src->type))
247 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
249 s1 = emit_load(jd, iptr, src, REG_IFTMP);
251 d = codegen_reg_of_var(iptr->opc, dst, s1);
261 M_MOV(GET_LOW_REG(s1), GET_LOW_REG(d));
262 M_MOV(GET_HIGH_REG(s1), GET_HIGH_REG(d));
269 vm_abort("emit_copy: unknown type %d", dst->type);
273 emit_store(jd, iptr, dst, d);
278 /* emit_iconst *****************************************************************
282 *******************************************************************************/
284 void emit_iconst(codegendata *cd, s4 d, s4 value)
288 if ((value >= -32768) && (value <= 32767))
289 M_LDA_INTERN(d, REG_ZERO, value);
291 disp = dseg_add_s4(cd, value);
292 M_ILD(d, REG_PV, disp);
297 /* emit_nullpointer_check ******************************************************
299 Emit a NullPointerException check.
301 *******************************************************************************/
303 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
305 if (INSTRUCTION_MUST_CHECK(iptr)) {
308 codegen_add_nullpointerexception_ref(cd);
313 /* emit_arrayindexoutofbounds_check ********************************************
315 Emit a ArrayIndexOutOfBoundsException check.
317 *******************************************************************************/
319 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
321 if (INSTRUCTION_MUST_CHECK(iptr)) {
322 M_ILD(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
323 M_CMPU(s2, REG_ITMP3);
325 codegen_add_arrayindexoutofboundsexception_ref(cd, s2);
330 /* emit_exception_stubs ********************************************************
332 Generates the code for the exception stubs.
334 *******************************************************************************/
336 void emit_exception_stubs(jitdata *jd)
346 /* get required compiler data */
351 /* generate exception stubs */
355 for (er = cd->exceptionrefs; er != NULL; er = er->next) {
356 /* back-patch the branch to this exception code */
358 branchmpc = er->branchpos;
359 targetmpc = cd->mcodeptr - cd->mcodebase;
361 md_codegen_patch_branch(cd, branchmpc, targetmpc);
365 /* Move the value register to a temporary register, if
366 there is the need for it. */
369 M_MOV(er->reg, REG_ITMP1);
371 /* calcuate exception address */
373 M_LDA(REG_ITMP2_XPC, REG_PV, er->branchpos - 4);
375 /* move function to call into REG_ITMP3 */
377 disp = dseg_add_functionptr(cd, er->function);
378 M_ALD(REG_ITMP3, REG_PV, disp);
380 if (targetdisp == 0) {
381 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
383 if (jd->isleafmethod) {
385 M_AST(REG_ZERO, REG_SP, cd->stackframesize * 4 + LA_LR_OFFSET);
388 M_MOV(REG_PV, rd->argintregs[0]);
389 M_MOV(REG_SP, rd->argintregs[1]);
391 if (jd->isleafmethod)
392 M_MOV(REG_ZERO, rd->argintregs[2]);
394 M_ALD(rd->argintregs[2],
395 REG_SP, cd->stackframesize * 4 + LA_LR_OFFSET);
397 M_MOV(REG_ITMP2_XPC, rd->argintregs[3]);
398 M_MOV(REG_ITMP1, rd->argintregs[4]);
400 M_STWU(REG_SP, REG_SP, -(LA_SIZE + 6 * 4));
401 M_AST(REG_ITMP2_XPC, REG_SP, LA_SIZE + 5 * 4);
405 M_MOV(REG_RESULT, REG_ITMP1_XPTR);
407 M_ALD(REG_ITMP2_XPC, REG_SP, LA_SIZE + 5 * 4);
408 M_IADD_IMM(REG_SP, LA_SIZE + 6 * 4, REG_SP);
410 if (jd->isleafmethod) {
411 /* XXX FIXME: REG_ZERO can cause problems here! */
412 assert(cd->stackframesize * 4 <= 32767);
414 M_ALD(REG_ZERO, REG_SP, cd->stackframesize * 4 + LA_LR_OFFSET);
418 disp = dseg_add_functionptr(cd, asm_handle_exception);
419 M_ALD(REG_ITMP3, REG_PV, disp);
424 disp = (((u4 *) cd->mcodebase) + targetdisp) -
425 (((u4 *) cd->mcodeptr) + 1);
432 /* emit_patcher_stubs **********************************************************
434 Generates the code for the patcher stubs.
436 *******************************************************************************/
438 void emit_patcher_stubs(jitdata *jd)
448 /* get required compiler data */
452 /* generate code patching stub call code */
456 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
457 /* check code segment size */
461 /* Get machine code which is patched back in later. The
462 call is 1 instruction word long. */
464 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
466 mcode = *((u4 *) tmpmcodeptr);
468 /* Patch in the call to call the following code (done at
471 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
472 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
474 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
477 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
479 /* create stack frame - keep stack 16-byte aligned */
481 M_AADD_IMM(REG_SP, -8 * 4, REG_SP);
483 /* calculate return address and move it onto the stack */
485 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
486 M_AST_INTERN(REG_ITMP3, REG_SP, 5 * 4);
488 /* move pointer to java_objectheader onto stack */
490 #if defined(ENABLE_THREADS)
491 /* order reversed because of data segment layout */
493 (void) dseg_add_unique_address(cd, NULL); /* flcword */
494 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
495 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
497 M_LDA(REG_ITMP3, REG_PV, disp);
498 M_AST_INTERN(REG_ITMP3, REG_SP, 4 * 4);
503 /* move machine code onto stack */
505 disp = dseg_add_s4(cd, mcode);
506 M_ILD(REG_ITMP3, REG_PV, disp);
507 M_IST_INTERN(REG_ITMP3, REG_SP, 3 * 4);
509 /* move class/method/field reference onto stack */
511 disp = dseg_add_address(cd, pref->ref);
512 M_ALD(REG_ITMP3, REG_PV, disp);
513 M_AST_INTERN(REG_ITMP3, REG_SP, 2 * 4);
515 /* move data segment displacement onto stack */
517 disp = dseg_add_s4(cd, pref->disp);
518 M_ILD(REG_ITMP3, REG_PV, disp);
519 M_IST_INTERN(REG_ITMP3, REG_SP, 1 * 4);
521 /* move patcher function pointer onto stack */
523 disp = dseg_add_functionptr(cd, pref->patcher);
524 M_ALD(REG_ITMP3, REG_PV, disp);
525 M_AST_INTERN(REG_ITMP3, REG_SP, 0 * 4);
527 if (targetdisp == 0) {
528 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
530 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
531 M_ALD(REG_ITMP3, REG_PV, disp);
536 disp = (((u4 *) cd->mcodebase) + targetdisp) -
537 (((u4 *) cd->mcodeptr) + 1);
544 /* emit_replacement_stubs ******************************************************
546 Generates the code for the replacement stubs.
548 *******************************************************************************/
550 #if defined(ENABLE_REPLACEMENT)
551 void emit_replacement_stubs(jitdata *jd)
562 /* get required compiler data */
567 rplp = code->rplpoints;
569 /* store beginning of replacement stubs */
571 code->replacementstubs = (u1*) (cd->mcodeptr - cd->mcodebase);
573 for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
574 /* do not generate stubs for non-trappable points */
576 if (rplp->flags & RPLPOINT_FLAG_NOTRAP)
579 /* check code segment size */
584 savedmcodeptr = cd->mcodeptr;
587 /* create stack frame - keep 16-byte aligned */
589 M_AADD_IMM(REG_SP, -4 * 4, REG_SP);
591 /* push address of `rplpoint` struct */
593 disp = dseg_add_address(cd, rplp);
594 M_ALD(REG_ITMP3, REG_PV, disp);
595 M_AST_INTERN(REG_ITMP3, REG_SP, 0 * 4);
597 /* jump to replacement function */
599 disp = dseg_add_functionptr(cd, asm_replacement_out);
600 M_ALD(REG_ITMP3, REG_PV, disp);
604 assert((cd->mcodeptr - savedmcodeptr) == 4*REPLACEMENT_STUB_SIZE);
607 #endif /* defined(ENABLE_REPLACEMENT) */
610 /* emit_verbosecall_enter ******************************************************
612 Generates the code for the call trace.
614 *******************************************************************************/
616 void emit_verbosecall_enter(jitdata *jd)
627 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
630 /* get required compiler data */
638 /* Build up Stackframe for builtin_trace_args call (a multiple of 16) */
640 /* LA + TRACE_ARGS_NUM u8 args + methodinfo + LR */
641 /* LA_SIZE(=6*4) + 8*8 + 4 + 4 + 0(Padding) */
642 /* 6 * 4 + 8 * 8 + 2 * 4 = 12 * 8 = 6 * 16 */
644 /* LA + (TRACE_ARGS_NUM - INT_ARG_CNT/2) u8 args + methodinfo */
645 /* + INT_ARG_CNT * 4 ( save integer registers) + LR + 8 + 8 (Padding) */
646 /* LA_SIZE(=2*4) + 4 * 8 + 4 + 8 * 4 + 4 + 8 */
647 /* 2 * 4 + 4 * 8 + 10 * 4 + 1 * 8 + 8= 12 * 8 = 6 * 16 */
649 /* in nativestubs no Place to save the LR (Link Register) would be needed */
650 /* but since the stack frame has to be aligned the 4 Bytes would have to */
651 /* be padded again */
653 #if defined(__DARWIN__)
654 stack_size = LA_SIZE + (TRACE_ARGS_NUM + 1) * 8;
659 /* mark trace code */
664 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
665 M_STWU(REG_SP, REG_SP, -stack_size);
667 M_CLR(REG_ITMP1); /* clear help register */
669 /* save up to TRACE_ARGS_NUM arguments into the reserved stack space */
670 #if defined(__DARWIN__)
671 /* Copy Params starting from first to Stack */
672 /* since TRACE_ARGS == INT_ARG_CNT all used integer argument regs */
676 /* Copy Params starting from fifth to Stack (INT_ARG_CNT/2) are in */
677 /* integer argument regs */
678 /* all integer argument registers have to be saved */
679 for (p = 0; p < 8; p++) {
680 d = rd->argintregs[p];
681 /* save integer argument registers */
682 M_IST(d, REG_SP, LA_SIZE + 4 * 8 + 4 + p * 4);
687 for (; p < md->paramcount && p < TRACE_ARGS_NUM; p++, stack_off += 8) {
688 t = md->paramtypes[p].type;
689 if (IS_INT_LNG_TYPE(t)) {
690 if (!md->params[p].inmemory) { /* Param in Arg Reg */
691 if (IS_2_WORD_TYPE(t)) {
692 M_IST(rd->argintregs[GET_HIGH_REG(md->params[p].regoff)]
693 , REG_SP, stack_off);
694 M_IST(rd->argintregs[GET_LOW_REG(md->params[p].regoff)]
695 , REG_SP, stack_off + 4);
697 M_IST(REG_ITMP1, REG_SP, stack_off);
698 M_IST(rd->argintregs[md->params[p].regoff]
699 , REG_SP, stack_off + 4);
701 } else { /* Param on Stack */
702 s1 = (md->params[p].regoff + cd->stackframesize) * 4
704 if (IS_2_WORD_TYPE(t)) {
705 M_ILD(REG_ITMP2, REG_SP, s1);
706 M_IST(REG_ITMP2, REG_SP, stack_off);
707 M_ILD(REG_ITMP2, REG_SP, s1 + 4);
708 M_IST(REG_ITMP2, REG_SP, stack_off + 4);
710 M_IST(REG_ITMP1, REG_SP, stack_off);
711 M_ILD(REG_ITMP2, REG_SP, s1);
712 M_IST(REG_ITMP2, REG_SP, stack_off + 4);
715 } else { /* IS_FLT_DBL_TYPE(t) */
716 if (!md->params[p].inmemory) { /* in Arg Reg */
717 s1 = rd->argfltregs[md->params[p].regoff];
718 if (!IS_2_WORD_TYPE(t)) {
719 M_IST(REG_ITMP1, REG_SP, stack_off);
720 M_FST(s1, REG_SP, stack_off + 4);
722 M_DST(s1, REG_SP, stack_off);
724 } else { /* on Stack */
725 /* this should not happen */
730 /* load first 4 (==INT_ARG_CNT/2) arguments into integer registers */
731 #if defined(__DARWIN__)
732 for (p = 0; p < 8; p++) {
733 d = rd->argintregs[p];
734 M_ILD(d, REG_SP, LA_SIZE + p * 4);
738 /* Set integer and float argument registers vor trace_args call */
739 /* offset to saved integer argument registers */
740 stack_off = LA_SIZE + 4 * 8 + 4;
741 for (p = 0; (p < 4) && (p < md->paramcount); p++) {
742 t = md->paramtypes[p].type;
743 if (IS_INT_LNG_TYPE(t)) {
744 /* "stretch" int types */
745 if (!IS_2_WORD_TYPE(t)) {
746 M_CLR(rd->argintregs[2 * p]);
747 M_ILD(rd->argintregs[2 * p + 1], REG_SP,stack_off);
750 M_ILD(rd->argintregs[2 * p + 1], REG_SP,stack_off + 4);
751 M_ILD(rd->argintregs[2 * p], REG_SP,stack_off);
754 } else { /* Float/Dbl */
755 if (!md->params[p].inmemory) { /* Param in Arg Reg */
756 /* use reserved Place on Stack (sp + 5 * 16) to copy */
757 /* float/double arg reg to int reg */
758 s1 = rd->argfltregs[md->params[p].regoff];
759 if (!IS_2_WORD_TYPE(t)) {
760 M_FST(s1, REG_SP, 5 * 16);
761 M_ILD(rd->argintregs[2 * p + 1], REG_SP, 5 * 16);
762 M_CLR(rd->argintregs[2 * p]);
764 M_DST(s1, REG_SP, 5 * 16);
765 M_ILD(rd->argintregs[2 * p + 1], REG_SP, 5 * 16 + 4);
766 M_ILD(rd->argintregs[2 * p], REG_SP, 5 * 16);
773 /* put methodinfo pointer on Stackframe */
774 p = dseg_add_address(cd, m);
775 M_ALD(REG_ITMP1, REG_PV, p);
776 #if defined(__DARWIN__)
777 M_AST(REG_ITMP1, REG_SP, LA_SIZE + TRACE_ARGS_NUM * 8);
779 M_AST(REG_ITMP1, REG_SP, LA_SIZE + 4 * 8);
781 p = dseg_add_functionptr(cd, builtin_verbosecall_enter);
782 M_ALD(REG_ITMP2, REG_PV, p);
786 #if defined(__DARWIN__)
787 /* restore integer argument registers from the reserved stack space */
790 for (p = 0; p < md->paramcount && p < TRACE_ARGS_NUM;
791 p++, stack_off += 8) {
792 t = md->paramtypes[p].type;
794 if (IS_INT_LNG_TYPE(t)) {
795 if (!md->params[p].inmemory) {
796 if (IS_2_WORD_TYPE(t)) {
797 M_ILD(rd->argintregs[GET_HIGH_REG(md->params[p].regoff)]
798 , REG_SP, stack_off);
799 M_ILD(rd->argintregs[GET_LOW_REG(md->params[p].regoff)]
800 , REG_SP, stack_off + 4);
802 M_ILD(rd->argintregs[md->params[p].regoff]
803 , REG_SP, stack_off + 4);
810 for (p = 0; p < 8; p++) {
811 d = rd->argintregs[p];
812 /* save integer argument registers */
813 M_ILD(d, REG_SP, LA_SIZE + 4 * 8 + 4 + p * 4);
817 M_ALD(REG_ZERO, REG_SP, stack_size + LA_LR_OFFSET);
819 M_LDA(REG_SP, REG_SP, stack_size);
821 /* mark trace code */
824 #endif /* !defined(NDEBUG) */
828 /* emit_verbosecall_exit *******************************************************
830 Generates the code for the call trace.
832 void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
834 *******************************************************************************/
836 void emit_verbosecall_exit(jitdata *jd)
845 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
848 /* get required compiler data */
856 /* mark trace code */
861 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
862 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4));
864 /* save return registers */
866 M_LST(REG_RESULT_PACKED, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 0) * 4);
867 M_DST(REG_FRESULT, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 2) * 4);
869 /* keep this order */
870 switch (md->returntype.type) {
873 M_INTMOVE(REG_RESULT, REG_A1);
878 M_LNGMOVE(REG_RESULT_PACKED, REG_A0_A1_PACKED);
882 M_FLTMOVE(REG_FRESULT, REG_FA0);
883 M_FLTMOVE(REG_FRESULT, REG_FA1);
885 disp = dseg_add_address(cd, m);
886 M_ALD(REG_A2, REG_PV, disp);
888 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
889 M_ALD(REG_ITMP2, REG_PV, disp);
893 /* restore return registers */
895 M_LLD(REG_RESULT_PACKED, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 0) * 4);
896 M_DLD(REG_FRESULT, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 2) * 4);
898 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4 + LA_LR_OFFSET);
900 M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4);
902 /* mark trace code */
905 #endif /* !defined(NDEBUG) */
910 * These are local overrides for various environment variables in Emacs.
911 * Please do not remove this and leave it at the end of the file, where
912 * Emacs will automagically detect them.
913 * ---------------------------------------------------------------------
916 * indent-tabs-mode: t
920 * vim:noexpandtab:sw=4:ts=4: