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 7816 2007-04-25 19:38:46Z twisti $
38 #include "vm/jit/powerpc/codegen.h"
40 #include "mm/memory.h"
42 #include "threads/lock-common.h"
44 #include "vm/builtin.h"
45 #include "vm/exceptions.h"
47 #include "vm/jit/abi.h"
48 #include "vm/jit/asmpart.h"
49 #include "vm/jit/codegen-common.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)
228 /* get required compiler data */
232 /* get source and destination variables */
234 src = VAROP(iptr->s1);
235 dst = VAROP(iptr->dst);
237 if ((src->vv.regoff != dst->vv.regoff) ||
238 (IS_INMEMORY(src->flags ^ dst->flags))) {
240 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
241 /* emit nothing, as the value won't be used anyway */
245 /* If one of the variables resides in memory, we can eliminate
246 the register move from/to the temporary register with the
247 order of getting the destination register and the load. */
249 if (IS_INMEMORY(src->flags)) {
250 if (IS_LNG_TYPE(src->type))
251 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
253 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
255 s1 = emit_load(jd, iptr, src, d);
258 if (IS_LNG_TYPE(src->type))
259 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
261 s1 = emit_load(jd, iptr, src, REG_IFTMP);
263 d = codegen_reg_of_var(iptr->opc, dst, s1);
273 M_MOV(GET_LOW_REG(s1), GET_LOW_REG(d));
274 M_MOV(GET_HIGH_REG(s1), GET_HIGH_REG(d));
281 vm_abort("emit_copy: unknown type %d", src->type);
285 emit_store(jd, iptr, dst, d);
290 /* emit_iconst *****************************************************************
294 *******************************************************************************/
296 void emit_iconst(codegendata *cd, s4 d, s4 value)
300 if ((value >= -32768) && (value <= 32767))
301 M_LDA_INTERN(d, REG_ZERO, value);
303 disp = dseg_add_s4(cd, value);
304 M_ILD(d, REG_PV, disp);
309 /* emit_branch *****************************************************************
311 Emits the code for conditional and unconditional branchs.
313 *******************************************************************************/
315 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
320 /* calculate the different displacements */
322 checkdisp = disp + 4;
323 branchdisp = (disp - 4) >> 2;
325 /* check which branch to generate */
327 if (condition == BRANCH_UNCONDITIONAL) {
328 /* check displacement for overflow */
330 if ((checkdisp < (s4) 0xfe000000) || (checkdisp > (s4) 0x01fffffc)) {
331 /* if the long-branches flag isn't set yet, do it */
333 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
334 cd->flags |= (CODEGENDATA_FLAG_ERROR |
335 CODEGENDATA_FLAG_LONGBRANCHES);
338 vm_abort("emit_branch: emit unconditional long-branch code");
345 /* and displacement for overflow */
347 if ((checkdisp < (s4) 0xffff8000) || (checkdisp > (s4) 0x00007fff)) {
348 /* if the long-branches flag isn't set yet, do it */
350 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
351 cd->flags |= (CODEGENDATA_FLAG_ERROR |
352 CODEGENDATA_FLAG_LONGBRANCHES);
381 vm_abort("emit_branch: long BRANCH_NAN");
384 vm_abort("emit_branch: unknown condition %d", condition);
411 vm_abort("emit_branch: unknown condition %d", condition);
418 /* emit_arithmetic_check *******************************************************
420 Emit an ArithmeticException check.
422 *******************************************************************************/
424 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
426 if (INSTRUCTION_MUST_CHECK(iptr)) {
429 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_ARITHMETIC);
434 /* emit_arrayindexoutofbounds_check ********************************************
436 Emit a ArrayIndexOutOfBoundsException check.
438 *******************************************************************************/
440 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
442 if (INSTRUCTION_MUST_CHECK(iptr)) {
443 M_ILD(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
444 M_TRAPGEU(s2, REG_ITMP3);
449 /* emit_classcast_check ********************************************************
451 Emit a ClassCastException check.
453 *******************************************************************************/
455 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
457 if (INSTRUCTION_MUST_CHECK(iptr)) {
469 vm_abort("emit_classcast_check: unknown condition %d", condition);
471 M_ALD_INTERN(s1, REG_ZERO, EXCEPTION_HARDWARE_CLASSCAST);
476 /* emit_nullpointer_check ******************************************************
478 Emit a NullPointerException check.
480 *******************************************************************************/
482 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
484 if (INSTRUCTION_MUST_CHECK(iptr)) {
487 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_NULLPOINTER);
492 /* emit_exception_check ********************************************************
494 Emit an Exception check.
496 *******************************************************************************/
498 void emit_exception_check(codegendata *cd, instruction *iptr)
500 if (INSTRUCTION_MUST_CHECK(iptr)) {
503 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_EXCEPTION);
508 /* emit_patcher_stubs **********************************************************
510 Generates the code for the patcher stubs.
512 *******************************************************************************/
514 void emit_patcher_stubs(jitdata *jd)
524 /* get required compiler data */
528 /* generate code patching stub call code */
532 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
533 /* check code segment size */
537 /* Get machine code which is patched back in later. The
538 call is 1 instruction word long. */
540 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
542 mcode = *((u4 *) tmpmcodeptr);
544 /* Patch in the call to call the following code (done at
547 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
548 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
550 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
553 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
555 /* create stack frame - keep stack 16-byte aligned */
557 M_AADD_IMM(REG_SP, -8 * 4, REG_SP);
559 /* calculate return address and move it onto the stack */
561 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
562 M_AST_INTERN(REG_ITMP3, REG_SP, 5 * 4);
564 /* move pointer to java_objectheader onto stack */
566 #if defined(ENABLE_THREADS)
567 /* order reversed because of data segment layout */
569 (void) dseg_add_unique_address(cd, NULL); /* flcword */
570 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
571 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
573 M_LDA(REG_ITMP3, REG_PV, disp);
574 M_AST_INTERN(REG_ITMP3, REG_SP, 4 * 4);
579 /* move machine code onto stack */
581 disp = dseg_add_s4(cd, mcode);
582 M_ILD(REG_ITMP3, REG_PV, disp);
583 M_IST_INTERN(REG_ITMP3, REG_SP, 3 * 4);
585 /* move class/method/field reference onto stack */
587 disp = dseg_add_address(cd, pref->ref);
588 M_ALD(REG_ITMP3, REG_PV, disp);
589 M_AST_INTERN(REG_ITMP3, REG_SP, 2 * 4);
591 /* move data segment displacement onto stack */
593 disp = dseg_add_s4(cd, pref->disp);
594 M_ILD(REG_ITMP3, REG_PV, disp);
595 M_IST_INTERN(REG_ITMP3, REG_SP, 1 * 4);
597 /* move patcher function pointer onto stack */
599 disp = dseg_add_functionptr(cd, pref->patcher);
600 M_ALD(REG_ITMP3, REG_PV, disp);
601 M_AST_INTERN(REG_ITMP3, REG_SP, 0 * 4);
603 if (targetdisp == 0) {
604 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
606 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
607 M_ALD(REG_ITMP3, REG_PV, disp);
612 disp = (((u4 *) cd->mcodebase) + targetdisp) -
613 (((u4 *) cd->mcodeptr) + 1);
620 /* emit_replacement_stubs ******************************************************
622 Generates the code for the replacement stubs.
624 *******************************************************************************/
626 #if defined(ENABLE_REPLACEMENT)
627 void emit_replacement_stubs(jitdata *jd)
638 /* get required compiler data */
643 rplp = code->rplpoints;
645 /* store beginning of replacement stubs */
647 code->replacementstubs = (u1*) (cd->mcodeptr - cd->mcodebase);
649 for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
650 /* do not generate stubs for non-trappable points */
652 if (rplp->flags & RPLPOINT_FLAG_NOTRAP)
655 /* check code segment size */
660 savedmcodeptr = cd->mcodeptr;
663 /* create stack frame - keep 16-byte aligned */
665 M_AADD_IMM(REG_SP, -4 * 4, REG_SP);
667 /* push address of `rplpoint` struct */
669 disp = dseg_add_address(cd, rplp);
670 M_ALD(REG_ITMP3, REG_PV, disp);
671 M_AST_INTERN(REG_ITMP3, REG_SP, 0 * 4);
673 /* jump to replacement function */
675 disp = dseg_add_functionptr(cd, asm_replacement_out);
676 M_ALD(REG_ITMP3, REG_PV, disp);
680 assert((cd->mcodeptr - savedmcodeptr) == 4*REPLACEMENT_STUB_SIZE);
683 #endif /* defined(ENABLE_REPLACEMENT) */
686 /* emit_verbosecall_enter ******************************************************
688 Generates the code for the call trace.
690 *******************************************************************************/
692 void emit_verbosecall_enter(jitdata *jd)
703 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
706 /* get required compiler data */
714 /* Build up Stackframe for builtin_trace_args call (a multiple of 16) */
716 /* LA + TRACE_ARGS_NUM u8 args + methodinfo + LR */
717 /* LA_SIZE(=6*4) + 8*8 + 4 + 4 + 0(Padding) */
718 /* 6 * 4 + 8 * 8 + 2 * 4 = 12 * 8 = 6 * 16 */
720 /* LA + (TRACE_ARGS_NUM - INT_ARG_CNT/2) u8 args + methodinfo */
721 /* + INT_ARG_CNT * 4 ( save integer registers) + LR + 8 + 8 (Padding) */
722 /* LA_SIZE(=2*4) + 4 * 8 + 4 + 8 * 4 + 4 + 8 */
723 /* 2 * 4 + 4 * 8 + 10 * 4 + 1 * 8 + 8= 12 * 8 = 6 * 16 */
725 /* in nativestubs no Place to save the LR (Link Register) would be needed */
726 /* but since the stack frame has to be aligned the 4 Bytes would have to */
727 /* be padded again */
729 #if defined(__DARWIN__)
730 stack_size = LA_SIZE + (TRACE_ARGS_NUM + 1) * 8;
735 /* mark trace code */
740 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
741 M_STWU(REG_SP, REG_SP, -stack_size);
743 M_CLR(REG_ITMP1); /* clear help register */
745 /* save up to TRACE_ARGS_NUM arguments into the reserved stack space */
746 #if defined(__DARWIN__)
747 /* Copy Params starting from first to Stack */
748 /* since TRACE_ARGS == INT_ARG_CNT all used integer argument regs */
752 /* Copy Params starting from fifth to Stack (INT_ARG_CNT/2) are in */
753 /* integer argument regs */
754 /* all integer argument registers have to be saved */
755 for (p = 0; p < 8; p++) {
756 d = abi_registers_integer_argument[p];
757 /* save integer argument registers */
758 M_IST(d, REG_SP, LA_SIZE + 4 * 8 + 4 + p * 4);
764 for (; p < md->paramcount && p < TRACE_ARGS_NUM; p++, stack_off += 8) {
765 t = md->paramtypes[p].type;
767 if (IS_INT_LNG_TYPE(t)) {
768 if (!md->params[p].inmemory) {
769 s1 = md->params[p].regoff;
771 if (IS_2_WORD_TYPE(t)) {
772 M_IST(GET_HIGH_REG(s1), REG_SP, stack_off);
773 M_IST(GET_LOW_REG(s1), REG_SP, stack_off + 4);
776 M_IST(REG_ITMP1, REG_SP, stack_off);
777 M_IST(s1, REG_SP, stack_off + 4);
781 s1 = (md->params[p].regoff + cd->stackframesize) * 4
783 if (IS_2_WORD_TYPE(t)) {
784 M_ILD(REG_ITMP2, REG_SP, s1);
785 M_IST(REG_ITMP2, REG_SP, stack_off);
786 M_ILD(REG_ITMP2, REG_SP, s1 + 4);
787 M_IST(REG_ITMP2, REG_SP, stack_off + 4);
790 M_IST(REG_ITMP1, REG_SP, stack_off);
791 M_ILD(REG_ITMP2, REG_SP, s1);
792 M_IST(REG_ITMP2, REG_SP, stack_off + 4);
797 if (!md->params[p].inmemory) {
798 s1 = md->params[p].regoff;
800 if (!IS_2_WORD_TYPE(t)) {
801 M_IST(REG_ITMP1, REG_SP, stack_off);
802 M_FST(s1, REG_SP, stack_off + 4);
805 M_DST(s1, REG_SP, stack_off);
808 /* this should not happen */
813 /* load first 4 (==INT_ARG_CNT/2) arguments into integer registers */
814 #if defined(__DARWIN__)
815 for (p = 0; p < 8; p++) {
816 d = abi_registers_integer_argument[p];
817 M_ILD(d, REG_SP, LA_SIZE + p * 4);
821 /* Set integer and float argument registers vor trace_args call */
822 /* offset to saved integer argument registers */
824 stack_off = LA_SIZE + 4 * 8 + 4;
826 for (p = 0; (p < 4) && (p < md->paramcount); p++) {
827 t = md->paramtypes[p].type;
829 if (IS_INT_LNG_TYPE(t)) {
830 /* "stretch" int types */
831 if (!IS_2_WORD_TYPE(t)) {
832 M_CLR(abi_registers_integer_argument[2 * p]);
833 M_ILD(abi_registers_integer_argument[2 * p + 1], REG_SP,stack_off);
837 M_ILD(abi_registers_integer_argument[2 * p + 1], REG_SP,stack_off + 4);
838 M_ILD(abi_registers_integer_argument[2 * p], REG_SP,stack_off);
843 if (!md->params[p].inmemory) {
844 /* use reserved Place on Stack (sp + 5 * 16) to copy */
845 /* float/double arg reg to int reg */
847 s1 = md->params[p].regoff;
849 if (!IS_2_WORD_TYPE(t)) {
850 M_FST(s1, REG_SP, 5 * 16);
851 M_ILD(abi_registers_integer_argument[2 * p + 1], REG_SP, 5 * 16);
852 M_CLR(abi_registers_integer_argument[2 * p]);
855 M_DST(s1, REG_SP, 5 * 16);
856 M_ILD(abi_registers_integer_argument[2 * p + 1], REG_SP, 5 * 16 + 4);
857 M_ILD(abi_registers_integer_argument[2 * p], REG_SP, 5 * 16);
864 /* put methodinfo pointer on Stackframe */
865 p = dseg_add_address(cd, m);
866 M_ALD(REG_ITMP1, REG_PV, p);
867 #if defined(__DARWIN__)
868 M_AST(REG_ITMP1, REG_SP, LA_SIZE + TRACE_ARGS_NUM * 8);
870 M_AST(REG_ITMP1, REG_SP, LA_SIZE + 4 * 8);
872 p = dseg_add_functionptr(cd, builtin_verbosecall_enter);
873 M_ALD(REG_ITMP2, REG_PV, p);
877 #if defined(__DARWIN__)
878 /* restore integer argument registers from the reserved stack space */
882 for (p = 0; p < md->paramcount && p < TRACE_ARGS_NUM; p++, stack_off += 8) {
883 t = md->paramtypes[p].type;
885 if (IS_INT_LNG_TYPE(t)) {
886 if (!md->params[p].inmemory) {
887 s1 = md->params[p].regoff;
889 if (IS_2_WORD_TYPE(t)) {
890 M_ILD(GET_HIGH_REG(s1), REG_SP, stack_off);
891 M_ILD(GET_LOW_REG(s1), REG_SP, stack_off + 4);
894 M_ILD(s1, REG_SP, stack_off + 4);
900 for (p = 0; p < 8; p++) {
901 d = abi_registers_integer_argument[p];
902 /* save integer argument registers */
903 M_ILD(d, REG_SP, LA_SIZE + 4 * 8 + 4 + p * 4);
907 M_ALD(REG_ZERO, REG_SP, stack_size + LA_LR_OFFSET);
909 M_LDA(REG_SP, REG_SP, stack_size);
911 /* mark trace code */
914 #endif /* !defined(NDEBUG) */
918 /* emit_verbosecall_exit *******************************************************
920 Generates the code for the call trace.
922 void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
924 *******************************************************************************/
926 void emit_verbosecall_exit(jitdata *jd)
935 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
938 /* get required compiler data */
946 /* mark trace code */
951 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
952 M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4));
954 /* save return registers */
956 M_LST(REG_RESULT_PACKED, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 0) * 4);
957 M_DST(REG_FRESULT, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 2) * 4);
959 /* keep this order */
960 switch (md->returntype.type) {
963 M_INTMOVE(REG_RESULT, REG_A1);
968 M_LNGMOVE(REG_RESULT_PACKED, REG_A0_A1_PACKED);
972 M_FLTMOVE(REG_FRESULT, REG_FA0);
973 M_FLTMOVE(REG_FRESULT, REG_FA1);
975 disp = dseg_add_address(cd, m);
976 M_ALD(REG_A2, REG_PV, disp);
978 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
979 M_ALD(REG_ITMP2, REG_PV, disp);
983 /* restore return registers */
985 M_LLD(REG_RESULT_PACKED, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 0) * 4);
986 M_DLD(REG_FRESULT, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 2) * 4);
988 M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4 + LA_LR_OFFSET);
990 M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4);
992 /* mark trace code */
995 #endif /* !defined(NDEBUG) */
1000 * These are local overrides for various environment variables in Emacs.
1001 * Please do not remove this and leave it at the end of the file, where
1002 * Emacs will automagically detect them.
1003 * ---------------------------------------------------------------------
1006 * indent-tabs-mode: t
1010 * vim:noexpandtab:sw=4:ts=4: