* src/vm/jit/codegen-common.h (codegen_start_native_call): Changed signature.
[cacao.git] / src / vm / jit / m68k / codegen.c
index 89cb88588527d664b284c7f2007842ca22f167ba..37c41d5c668e673bc9eeb8a6d164128d6f22678d 100644 (file)
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: codegen.c 7564 2007-03-23 23:36:17Z twisti $
-
 */
 
 
 #include "config.h"
 
 #include <assert.h>
+#include <stdint.h>
 
 #include "md-abi.h"
 #include "md-os.h"
@@ -40,6 +39,7 @@
 
 #include "mm/memory.h"
 #include "native/jni.h"
+#include "native/localref.h"
 #include "native/native.h"
 
 #include "threads/lock-common.h"
@@ -55,6 +55,7 @@
 #include "vm/jit/dseg.h"
 #include "vm/jit/emit-common.h"
 #include "vm/jit/jit.h"
+#include "vm/jit/abi.h"
 #include "vm/jit/parse.h"
 #include "vm/jit/patcher.h"
 #include "vm/jit/reg.h"
@@ -109,41 +110,45 @@ bool codegen_emit(jitdata *jd)
 
                savedregs_num += (INT_SAV_CNT - rd->savintreguse);
                savedregs_num += (ADR_SAV_CNT - rd->savadrreguse);
-               savedregs_num += (FLT_SAV_CNT - rd->savfltreguse) * 2;
+               savedregs_num += (FLT_SAV_CNT - rd->savfltreguse);
 
                cd->stackframesize = rd->memuse + savedregs_num;
+       
+               /* we always add 2 stack slots.
+                * 1 word the lock word, which may be unused and resides @ rd->memuse * 8
+                * + 2 words to either save the return value for LOCK_monitor_exit @ rd->memuse * 8 + 8
+                * on the other hand we could use 2 words when a builtin returns a doulbe which are
+                * returned in %d0, %d1 and need to be stored onto the stack and read in used a fmovemd
+                * so we always _need_ at least 2 slots, and this keeps the code simple */
+               cd->stackframesize += 2;        
 
-               /* FIXME: we could need 2 words to move a double result, which gets
-                * passed in %d0, %d1 into a floating point register, this is of 
-                * course onyl true when not using ENABLE_SOFTFLOAT, so this could be
-                * optimized away, for now always use 2 more words. When optimizing watch
-                * the threading code, which stores the lock word, the offset would change */
-               cd->stackframesize += 2;
+               cd->stackframesize *= 8;        /* we use 8 byte stack slots */
 
+#if 0
 #if defined(ENABLE_THREADS)
                /* we need additional space to save argument of monitor_enter */
                if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
                        if (IS_2_WORD_TYPE(m->parseddesc->returntype.type))     {
                                cd->stackframesize += 2;
                        } else  {
-                               cd->stackframesize ++;
+                               cd->stackframesize += 1;
                        }
                }
 #endif
-               
+#endif 
        
                /* create method header */
                (void) dseg_add_unique_address(cd, code);              /* CodeinfoPointer */
-               (void) dseg_add_unique_s4(cd, cd->stackframesize * 4); /* FrameSize       */
+               (void) dseg_add_unique_s4(cd, cd->stackframesize);         /* FrameSize       */
 #if defined(ENABLE_THREADS)
                if (checksync && (m->flags & ACC_SYNCHRONIZED))
-                       (void) dseg_add_unique_s4(cd, (rd->memuse + 1) * 4);/* IsSync         */
+                       (void) dseg_add_unique_s4(cd, (rd->memuse + 1) * 8);/* IsSync         */
                else
 #endif
                (void) dseg_add_unique_s4(cd, 0);                      /* IsSync          */
                (void) dseg_add_unique_s4(cd, jd->isleafmethod);       /* IsLeaf          */
 
-               /* XXX we use the IntSAce a split field for the adr now */
+               /* XXX we use the IntSave a split field for the adr now */
                (void) dseg_add_unique_s4(cd, (ADR_SAV_CNT - rd->savadrreguse) << 16 | (INT_SAV_CNT - rd->savintreguse)); /* IntSave */
                (void) dseg_add_unique_s4(cd, FLT_SAV_CNT - rd->savfltreguse); /* FltSave */
 
@@ -167,19 +172,19 @@ bool codegen_emit(jitdata *jd)
                emit_verbosecall_enter(jd);
 #endif
                /* create stack frame */
-               M_AADD_IMM(-(cd->stackframesize*4), REG_SP);
+               M_AADD_IMM(-(cd->stackframesize), REG_SP);
 
                /* save used callee saved registers */
                p = cd->stackframesize;
                for (i=INT_SAV_CNT-1; i>=rd->savintreguse; --i) {
-                       p--; M_IST(rd->savintregs[i], REG_SP, p*4);
+                       p-=8; M_IST(rd->savintregs[i], REG_SP, p);
                }
                for (i=ADR_SAV_CNT-1; i>=rd->savadrreguse; --i) {
-                       p--; M_AST(rd->savadrregs[i], REG_SP, p*4);
+                       p-=8; M_AST(rd->savadrregs[i], REG_SP, p);
                }
 #if !defined(ENABLE_SOFTFLOAT)
                for (i=FLT_SAV_CNT-1; i>=rd->savfltreguse; --i) {
-                       p-=2; M_DST(rd->savfltregs[i], REG_SP, p*4);    /* FIXME */
+                       p-=8; M_FSTORE(rd->savfltregs[i], REG_SP, p);
                }       
 #else
                assert(FLT_SAV_CNT == 0);
@@ -212,22 +217,17 @@ bool codegen_emit(jitdata *jd)
                        case TYPE_INT:
                                if (!IS_INMEMORY(var->flags)) {      /* stack arg -> register */
                                        if (IS_2_WORD_TYPE(t))  {
-                                               M_LLD(var->vv.regoff, REG_SP, (cd->stackframesize + s1 + 1) * 4);
+                                               M_LLD(var->vv.regoff, REG_SP, cd->stackframesize + s1 + 4);
                                        } else {
-                                               M_ILD(var->vv.regoff, REG_SP, (cd->stackframesize + s1 + 1) * 4);
+                                               M_ILD(var->vv.regoff, REG_SP, cd->stackframesize + s1 + 4);
                                        }
                                } else {                             /* stack arg -> spilled  */
-#if 1
-                                       M_ILD(REG_ITMP1, REG_SP, (cd->stackframesize + s1 + 1) * 4);
-                                       M_IST(REG_ITMP1, REG_SP, var->vv.regoff * 4);
+                                       M_ILD(REG_ITMP1, REG_SP, cd->stackframesize + s1 + 4);
+                                       M_IST(REG_ITMP1, REG_SP, var->vv.regoff);
                                        if (IS_2_WORD_TYPE(t)) {
-                                               M_ILD(REG_ITMP1, REG_SP, (cd->stackframesize + s1 + 1) * 4 + 4);
-                                               M_IST(REG_ITMP1, REG_SP, var->vv.regoff * 4 + 4);
+                                               M_ILD(REG_ITMP1, REG_SP, cd->stackframesize  + s1 + 4 + 4);
+                                               M_IST(REG_ITMP1, REG_SP, var->vv.regoff + 4);
                                        }
-#else
-                                       /* Reuse Memory Position on Caller Stack */
-                                       var->vv.regoff = cd->stackframesize + s1;
-#endif
                                } 
                                break;
 #if !defined(ENABLE_SOFTFLOAT)
@@ -235,37 +235,27 @@ bool codegen_emit(jitdata *jd)
                        case TYPE_DBL:
                                if (!IS_INMEMORY(var->flags)) {      /* stack-arg -> register */
                                        if (IS_2_WORD_TYPE(t))  {
-                                               M_DLD(var->vv.regoff, REG_SP, (cd->stackframesize + s1 + 1) * 4);
+                                               M_DLD(var->vv.regoff, REG_SP, cd->stackframesize + s1 + 4);
                                        } else {
-                                               M_FLD(var->vv.regoff, REG_SP, (cd->stackframesize + s1 + 1) * 4);
+                                               M_FLD(var->vv.regoff, REG_SP, cd->stackframesize + s1 + 4);
                                        }
                                } else {                             /* stack-arg -> spilled  */
-#if 1
                                        if (IS_2_WORD_TYPE(t)) {
-                                               M_DLD(REG_FTMP1, REG_SP, (cd->stackframesize + s1 + 1) * 4);
-                                               M_DST(REG_FTMP1, REG_SP, var->vv.regoff * 4);
+                                               M_DLD(REG_FTMP1, REG_SP, cd->stackframesize + s1 + 4);
+                                               M_DST(REG_FTMP1, REG_SP, var->vv.regoff);
                                        } else {
-                                               M_FLD(REG_FTMP1, REG_SP, (cd->stackframesize + s1 + 1) * 4);
-                                               M_FST(REG_FTMP1, REG_SP, var->vv.regoff * 4);
+                                               M_FLD(REG_FTMP1, REG_SP, cd->stackframesize + s1 + 4);
+                                               M_FST(REG_FTMP1, REG_SP, var->vv.regoff);
                                        }
-#else
-                                       /* Reuse Memory Position on Caller Stack */
-                                       var->vv.regoff = cd->stackframesize + s1;
-#endif
                                }
                                break;
 #endif /* SOFTFLOAT */
                        case TYPE_ADR:
                                if (!IS_INMEMORY(var->flags)) {      /* stack-arg -> register */
-                                       M_ALD(var->vv.regoff, REG_SP, (cd->stackframesize + s1 + 1) * 4);
+                                       M_ALD(var->vv.regoff, REG_SP, cd->stackframesize + s1 + 4);
                                } else {                             /* stack-arg -> spilled  */
-#if 1
-                                       M_ALD(REG_ATMP1, REG_SP, (cd->stackframesize + s1 + 1) * 4);
-                                       M_AST(REG_ATMP1, REG_SP, var->vv.regoff * 4);
-#else
-                               /* Reuse Memory Position on Caller Stack */
-                               var->vv.regoff = cd->stackframesize + s1;
-#endif
+                                       M_ALD(REG_ATMP1, REG_SP, cd->stackframesize + s1 + 4);
+                                       M_AST(REG_ATMP1, REG_SP, var->vv.regoff);
                                }
                                break;
                        default: assert(0);
@@ -273,19 +263,19 @@ bool codegen_emit(jitdata *jd)
                } /* end for argument out of stack*/
 
 #if defined(ENABLE_THREADS)
-       /* call monitor_enter function */
+       /* call lock_monitor_enter function */
        if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
                if (m->flags & ACC_STATIC)      {
                        M_AMOV_IMM((&m->class->object.header), REG_ATMP1);
                } else  {
                        /* for non-static case the first arg is the object */
-                       M_ALD(REG_ATMP1, REG_SP, cd->stackframesize*4 + 4);
+                       M_ALD(REG_ATMP1, REG_SP, cd->stackframesize + 4);
                        M_ATST(REG_ATMP1);
                        M_BNE(2);
                        M_TRAP(M68K_EXCEPTION_HARDWARE_NULLPOINTER);
                }
 
-               M_AST(REG_ATMP1, REG_SP, rd->memuse * 4 + 2*4);
+               M_AST(REG_ATMP1, REG_SP, rd->memuse * 8);
                M_AST(REG_ATMP1, REG_SP, 0 * 4);
                M_JSR_IMM(LOCK_monitor_enter);
        }
@@ -306,8 +296,35 @@ bool codegen_emit(jitdata *jd)
        /* branch resolving */
        codegen_resolve_branchrefs(cd, bptr);
 
+       /* handle replacement points */
+       REPLACEMENT_POINT_BLOCK_START(cd, bptr);
+
+#if defined(ENABLE_PROFILING)
+       assert(0);
+#endif
        /* FIXME there are still some constrcuts to copy in here */
 
+#if defined(ENABLE_LSRA)
+       assert(0);
+#endif
+
+       /* copy interface registers to their destination */
+       len = bptr->indepth;
+       MCODECHECK(64+len);
+
+       while (len > 0) {
+               len--;
+               var = VAR(bptr->invars[len]);
+               if ((len == bptr->indepth-1) && (bptr->type == BBTYPE_EXH)) {
+                       d = codegen_reg_of_var(0, var, REG_ATMP1_XPTR);
+                       M_ADRMOVE(REG_ATMP1_XPTR, d);
+                       emit_store(jd, NULL, var, d);
+               }
+               else {
+                       assert((var->flags & INOUT));
+               }
+       }
+
        /* walk through all instructions */
        len = bptr->icount;
        currentline = 0;
@@ -396,6 +413,76 @@ bool codegen_emit(jitdata *jd)
                        break;
 
 
+               /* some long operations *********************************************/
+               case ICMD_LADD:       /* ..., val1, val2  ==> ..., val1 + val2        */
+                       s1 = emit_load_s1_low(jd, iptr, REG_ITMP3);
+                       s2 = emit_load_s2_low(jd, iptr, REG_ITMP1);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP12_PACKED);
+                       M_INTMOVE(s2, REG_ITMP1);
+                       M_IADD(s1, REG_ITMP1);                  /* low word */
+                       s1 = emit_load_s1_high(jd, iptr, REG_ITMP3);
+                       s2 = emit_load_s2_high(jd, iptr, REG_ITMP2);
+                       M_INTMOVE(s2, REG_ITMP2);
+                       M_IADDX(s1, REG_ITMP2);                 /* high word */
+                       emit_store_dst(jd, iptr, d);
+                       break;
+                       
+               case ICMD_LADDCONST:  /* ..., value  ==> ..., value + constant        */
+                                     /* sx.val.l = constant                          */
+                       s1 = emit_load_s1_low(jd, iptr, REG_ITMP1);
+                       s2 = emit_load_s1_high(jd, iptr, REG_ITMP2);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP12_PACKED);
+                       
+                       M_IMOV_IMM(iptr->sx.val.l >> 32, REG_ITMP3);
+
+                       s3 = iptr->sx.val.l & 0xffffffff;
+                       M_INTMOVE(s1, REG_ITMP1);
+                       M_IADD_IMM(s3, REG_ITMP1);              /* lower word in REG_ITMP1 now */
+
+                       M_IADDX(REG_ITMP3, REG_ITMP2);  /* high word in REG_ITMP2 now */
+                       M_LNGMOVE(REG_ITMP12_PACKED, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
+               case ICMD_LSUB:       /* ..., val1, val2  ==> ..., val1 - val2        */
+                       s1 = emit_load_s1_low(jd, iptr, REG_ITMP1);
+                       s2 = emit_load_s2_low(jd, iptr, REG_ITMP3);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP12_PACKED);
+                       M_INTMOVE(s1, REG_ITMP1);
+                       M_ISUB(s2, REG_ITMP1);                  /* low word */
+                       s1 = emit_load_s1_high(jd, iptr, REG_ITMP2);
+                       s2 = emit_load_s2_high(jd, iptr, REG_ITMP3);
+                       M_INTMOVE(s1, REG_ITMP2);
+                       M_ISUBX(s2, REG_ITMP2);                 /* high word */
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
+               case ICMD_LSUBCONST:  /* ..., value  ==> ..., value - constant        */
+                                     /* sx.val.l = constant                          */
+                       s1 = emit_load_s1_low(jd, iptr, REG_ITMP1);
+                       s2 = emit_load_s1_high(jd, iptr, REG_ITMP2);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP12_PACKED);
+                       
+                       M_IMOV_IMM( (-iptr->sx.val.l) >> 32, REG_ITMP3);
+
+                       s3 = (-iptr->sx.val.l) & 0xffffffff;
+                       M_INTMOVE(s1, REG_ITMP1);
+                       M_IADD_IMM(s3, REG_ITMP1);              /* lower word in REG_ITMP1 now */
+
+                       M_IADDX(REG_ITMP3, REG_ITMP2);  /* high word in REG_ITMP2 now */
+                       M_LNGMOVE(REG_ITMP12_PACKED, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
+               case ICMD_LNEG:       /* ..., value  ==> ..., - value                 */
+                       s1 = emit_load_s1(jd, iptr, REG_ITMP12_PACKED);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP12_PACKED);
+                       M_LNGMOVE(s1, REG_ITMP12_PACKED);
+                       M_INEG(GET_LOW_REG(REG_ITMP12_PACKED));
+                       M_INEGX(GET_HIGH_REG(REG_ITMP12_PACKED));
+                       M_LNGMOVE(REG_ITMP12_PACKED, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
 
                /* integer operations ************************************************/
                case ICMD_INEG:       /* ..., value  ==> ..., - value                 */
@@ -408,16 +495,6 @@ bool codegen_emit(jitdata *jd)
                        emit_store_dst(jd, iptr, d);
                        break;
 
-#if 0
-               case ICMD_LNEG:       /* ..., value  ==> ..., - value                 */
-
-                       s1 = emit_load_s1(jd, iptr, REG_ITMP12_PACKED);
-                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP12_PACKED);
-                       M_SUBFIC(GET_LOW_REG(s1), 0, GET_LOW_REG(d));
-                       M_SUBFZE(GET_HIGH_REG(s1), GET_HIGH_REG(d));
-                       emit_store_dst(jd, iptr, d);
-                       break;
-#endif
                case ICMD_I2L:        /* ..., value  ==> ..., value                   */
 
                        s1 = emit_load_s1(jd, iptr, REG_ITMP3);
@@ -779,7 +856,147 @@ bool codegen_emit(jitdata *jd)
                        emit_store_dst(jd, iptr, d);
                        break;
 
+               /* floating point operations ******************************************/
+               #if !defined(ENABLE_SOFTFLOAT)
+               case ICMD_FCMPL:                /* ..., val1, val2  ==> ..., val1 fcmpl val2  */
+               case ICMD_DCMPL:
+                       s1 = emit_load_s1(jd, iptr, REG_FTMP1);
+                       s2 = emit_load_s2(jd, iptr, REG_FTMP2);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP1);
+                       M_IMOV_IMM(-1, d);
+                       M_FCMP(s1, s2);
+                       M_BFUN(14);     /* result is -1, branch to end */
+                       M_BFLT(10);     /* result is -1, branch to end */
+                       M_IMOV_IMM(0, d);
+                       M_BFEQ(4)       /* result is 0, branch to end */
+                       M_IMOV_IMM(1, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
+               case ICMD_FCMPG:                /* ..., val1, val2  ==> ..., val1 fcmpg val2  */
+               case ICMD_DCMPG:
+                       s1 = emit_load_s1(jd, iptr, REG_FTMP1);
+                       s2 = emit_load_s2(jd, iptr, REG_FTMP2);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP1);
+                       M_IMOV_IMM(1, d);
+                       M_FCMP(s1, s2);
+                       M_BFUN(16);     /* result is +1, branch to end */
+                       M_BFGT(14);     /* result is +1, branch to end */
+                       M_IMOV_IMM(0, d);
+                       M_BFEQ(8)       /* result is 0, branch to end */
+                       M_IMOV_IMM(-1, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
+               case ICMD_FMUL:       /* ..., val1, val2  ==> ..., val1 * val2        */
+                       s1 = emit_load_s1(jd, iptr, REG_FTMP1);
+                       s2 = emit_load_s2(jd, iptr, REG_FTMP2);
+                       d = codegen_reg_of_dst(jd, iptr, REG_FTMP2);
+                       M_FLTMOVE(s2, REG_FTMP2);
+                       M_FMUL(s1, REG_FTMP2);
+                       M_FLTMOVE(REG_FTMP2, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
+               case ICMD_DMUL:       /* ..., val1, val2  ==> ..., val1 * val2        */
+                       s1 = emit_load_s1(jd, iptr, REG_FTMP1);
+                       s2 = emit_load_s2(jd, iptr, REG_FTMP2);
+                       d = codegen_reg_of_dst(jd, iptr, REG_FTMP2);
+                       M_DBLMOVE(s2, REG_FTMP2);
+                       M_DMUL(s1, REG_FTMP2);
+                       M_DBLMOVE(REG_FTMP2, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
+               case ICMD_FDIV:       /* ..., val1, val2  ==> ..., val1 / val2        */
+                       s1 = emit_load_s1(jd, iptr, REG_FTMP1);
+                       s2 = emit_load_s2(jd, iptr, REG_FTMP2);
+                       d = codegen_reg_of_dst(jd, iptr, REG_FTMP1);
+                       M_FLTMOVE(s1, REG_FTMP1);
+                       M_FDIV(s2, REG_FTMP1);
+                       M_FLTMOVE(REG_FTMP1, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
+               case ICMD_DDIV:       /* ..., val1, val2  ==> ..., val1 / val2        */
+                       s1 = emit_load_s1(jd, iptr, REG_FTMP1);
+                       s2 = emit_load_s2(jd, iptr, REG_FTMP2);
+                       d = codegen_reg_of_dst(jd, iptr, REG_FTMP1);
+                       M_DBLMOVE(s1, REG_FTMP1);
+                       M_DDIV(s2, REG_FTMP1);
+                       M_DBLMOVE(REG_FTMP1, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
+               case ICMD_FADD:       /* ..., val1, val2  ==> ..., val1 + val2        */
+                       s1 = emit_load_s1(jd, iptr, REG_FTMP1);
+                       s2 = emit_load_s2(jd, iptr, REG_FTMP2);
+                       d = codegen_reg_of_dst(jd, iptr, REG_FTMP2);
+                       M_FLTMOVE(s2, REG_FTMP2);
+                       M_FADD(s1, REG_FTMP2);
+                       M_FLTMOVE(REG_FTMP2, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
+               case ICMD_DADD:       /* ..., val1, val2  ==> ..., val1 + val2        */
+                       s1 = emit_load_s1(jd, iptr, REG_FTMP1);
+                       s2 = emit_load_s2(jd, iptr, REG_FTMP2);
+                       d = codegen_reg_of_dst(jd, iptr, REG_FTMP2);
+                       M_DBLMOVE(s2, REG_FTMP2);
+                       M_DADD(s1, REG_FTMP2);
+                       M_DBLMOVE(REG_FTMP2, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
+               case ICMD_FSUB:       /* ..., val1, val2  ==> ..., val1 - val2        */
+                       s1 = emit_load_s1(jd, iptr, REG_FTMP1);
+                       s2 = emit_load_s2(jd, iptr, REG_FTMP2);
+                       d = codegen_reg_of_dst(jd, iptr, REG_FTMP2);
+                       M_FLTMOVE(s1, REG_FTMP1);
+                       M_FSUB(s2, REG_FTMP1);
+                       M_FLTMOVE(REG_FTMP1, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
+               case ICMD_DSUB:       /* ..., val1, val2  ==> ..., val1 - val2        */
+                       s1 = emit_load_s1(jd, iptr, REG_FTMP1);
+                       s2 = emit_load_s2(jd, iptr, REG_FTMP2);
+                       d = codegen_reg_of_dst(jd, iptr, REG_FTMP2);
+                       M_DBLMOVE(s1, REG_FTMP1);
+                       M_DSUB(s2, REG_FTMP1);
+                       M_DBLMOVE(REG_FTMP1, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
+               case ICMD_F2D:       /* ..., value  ==> ..., (double) value           */
+                       s1 = emit_load_s1(jd, iptr, REG_FTMP1);
+                       d = codegen_reg_of_dst(jd, iptr, REG_FTMP2);
+                       M_F2D(s1, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
+               case ICMD_D2F:       /* ..., value  ==> ..., (float) value           */
+                       s1 = emit_load_s1(jd, iptr, REG_FTMP1);
+                       d = codegen_reg_of_dst(jd, iptr, REG_FTMP2);
+                       M_D2F(s1, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
+               case ICMD_FNEG:       /* ..., value  ==> ..., - value                 */
+                       s1 = emit_load_s1(jd, iptr, REG_FTMP1);
+                       d = codegen_reg_of_dst(jd, iptr, REG_FTMP2);
+                       M_FNEG(s1, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
+               case ICMD_DNEG:       /* ..., value  ==> ..., - value                 */
+                       s1 = emit_load_s1(jd, iptr, REG_FTMP1);
+                       d = codegen_reg_of_dst(jd, iptr, REG_FTMP2);
+                       M_DNEG(s1, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
 
+               #endif
 
                /* load/store/copy/move operations ************************************/
 
@@ -899,21 +1116,27 @@ bool codegen_emit(jitdata *jd)
 
 
                /* MEMORY *************************************************************/
-               case ICMD_GETSTATIC:
-                       if (INSTRUCTION_IS_UNRESOLVED(iptr))    {
+
+               case ICMD_GETSTATIC:  /* ...  ==> ..., value                          */
+
+                       if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
                                uf        = iptr->sx.s23.s3.uf;
                                fieldtype = uf->fieldref->parseddesc.fd->type;
-                               codegen_addpatchref(cd, PATCHER_get_putstatic, uf, 0);
-                       } else  {
-                               fieldinfo *fi = iptr->sx.s23.s3.fmiref->p.field;
+                               disp      = 0;
 
+                               codegen_addpatchref(cd, PATCHER_get_putstatic, uf, 0);
+                       }
+                       else {
+                               fi        = iptr->sx.s23.s3.fmiref->p.field;
                                fieldtype = fi->type;
+                               disp      = (intptr_t) fi->value;
+
                                if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class)) {
-                                       codegen_addpatchref(cd, PATCHER_initialize_class, fi->class, 0);
+                                       codegen_addpatchref(cd, PATCHER_initialize_class, fi->class,
+                                                                               0);
                                }
-
-                               disp = (ptrint) &(fi->value);
                        }
+
                        M_AMOV_IMM(disp, REG_ATMP1);
                        switch (fieldtype) {
 #if defined(ENABLE_SOFTFLOAT)
@@ -953,15 +1176,18 @@ bool codegen_emit(jitdata *jd)
                        if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
                                uf        = iptr->sx.s23.s3.uf;
                                fieldtype = uf->fieldref->parseddesc.fd->type;
+                               disp      = 0;
 
                                codegen_addpatchref(cd, PATCHER_get_putstatic, uf, 0);
-                       } else {
+                       }
+                       else {
                                fi        = iptr->sx.s23.s3.fmiref->p.field;
                                fieldtype = fi->type;
-                               disp      = &(fi->value);
+                               disp      = (intptr_t) fi->value;
 
                                if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class))
-                                       codegen_addpatchref(cd, PATCHER_initialize_class, fi->class, 0);
+                                       codegen_addpatchref(cd, PATCHER_initialize_class, fi->class,
+                                                                               0);
                        }
                
                        M_AMOV_IMM(disp, REG_ATMP1);
@@ -1111,7 +1337,7 @@ bool codegen_emit(jitdata *jd)
                        s1 = emit_load_s1(jd, iptr, REG_ATMP1);
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
                        /* implicit null-pointer check */
-                       M_ILD(d, s1, OFFSET(java_arrayheader, size));
+                       M_ILD(d, s1, OFFSET(java_array_t, size));
                        emit_store_dst(jd, iptr, d);
                        break;
 
@@ -1122,7 +1348,7 @@ bool codegen_emit(jitdata *jd)
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_INTMOVE(s2, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_bytearray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_bytearray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1139,7 +1365,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_INTMOVE(s2, REG_ITMP2);
                        M_ISSL_IMM(1, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_chararray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_chararray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1156,7 +1382,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_INTMOVE(s2, REG_ITMP2);
                        M_ISSL_IMM(1, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_shortarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_shortarray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                
@@ -1174,7 +1400,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_INTMOVE(s2, REG_ITMP2);
                        M_ISSL_IMM(2, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_intarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_intarray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1190,7 +1416,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_INTMOVE(s2, REG_ITMP1);
                        M_ISSL_IMM(3, REG_ITMP1);
-                       M_IADD_IMM(OFFSET(java_longarray, data[0]), REG_ITMP1);
+                       M_IADD_IMM(OFFSET(java_longarray_t, data[0]), REG_ITMP1);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP1, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1204,7 +1430,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_INTMOVE(s2, REG_ITMP2);
                        M_ISSL_IMM(2, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_floatarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_floatarray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1224,7 +1450,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_INTMOVE(s2, REG_ITMP2);
                        M_ISSL_IMM(3, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_doublearray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_doublearray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1245,7 +1471,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_INTMOVE(s2, REG_ITMP2);
                        M_ISSL_IMM(2, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_objectarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_objectarray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
        
@@ -1261,7 +1487,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        s3 = emit_load_s3(jd, iptr, REG_ITMP3);
                        M_INTMOVE(s2, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_bytearray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_bytearray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1275,7 +1501,7 @@ bool codegen_emit(jitdata *jd)
                        s3 = emit_load_s3(jd, iptr, REG_ITMP3);
                        M_INTMOVE(s2, REG_ITMP2);
                        M_ISSL_IMM(1, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_chararray, data[0]), REG_ITMP2); 
+                       M_IADD_IMM(OFFSET(java_chararray_t, data[0]), REG_ITMP2); 
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1289,7 +1515,7 @@ bool codegen_emit(jitdata *jd)
                        s3 = emit_load_s3(jd, iptr, REG_ITMP3);
                        M_INTMOVE(s2, REG_ITMP2);
                        M_ISSL_IMM(1, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_shortarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_shortarray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1303,7 +1529,7 @@ bool codegen_emit(jitdata *jd)
                        s3 = emit_load_s3(jd, iptr, REG_ITMP3);
                        M_INTMOVE(s2, REG_ITMP2);
                        M_ISSL_IMM(2, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_intarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_intarray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1317,7 +1543,7 @@ bool codegen_emit(jitdata *jd)
 
                        M_INTMOVE(s2, REG_ITMP1);
                        M_ISSL_IMM(3, REG_ITMP1);
-                       M_IADD_IMM(OFFSET(java_longarray, data[0]), REG_ITMP1);
+                       M_IADD_IMM(OFFSET(java_longarray_t, data[0]), REG_ITMP1);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP1, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1331,7 +1557,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_INTMOVE(s2, REG_ITMP2);
                        M_ISSL_IMM(2, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_floatarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_floatarray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1350,7 +1576,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_INTMOVE(s2, REG_ITMP2);
                        M_ISSL_IMM(3, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_doublearray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_doublearray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1372,19 +1598,19 @@ bool codegen_emit(jitdata *jd)
                        s3 = emit_load_s3(jd, iptr, REG_ATMP2);
 
                        /* XXX what if array is NULL */
-                       disp = dseg_add_functionptr(cd, BUILTIN_canstore);
+                       disp = dseg_add_functionptr(cd, BUILTIN_FAST_canstore);
 
                        M_AST(s1, REG_SP, 0*4);
                        M_AST(s3, REG_SP, 1*4);
-                       M_JSR_IMM(BUILTIN_canstore);    
-                       emit_exception_check(cd, iptr);
+                       M_JSR_IMM(BUILTIN_FAST_canstore);
+                       emit_arraystore_check(cd, iptr);
 
                        s1 = emit_load_s1(jd, iptr, REG_ATMP1);
                        s2 = emit_load_s2(jd, iptr, REG_ITMP1);
                        s3 = emit_load_s3(jd, iptr, REG_ATMP2);
                        M_INTMOVE(s2, REG_ITMP1);
                        M_ISSL_IMM(2, REG_ITMP1);
-                       M_IADD_IMM(OFFSET(java_objectarray, data[0]), REG_ITMP1);
+                       M_IADD_IMM(OFFSET(java_objectarray_t, data[0]), REG_ITMP1);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP1, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1395,6 +1621,8 @@ bool codegen_emit(jitdata *jd)
 
                /* METHOD INVOCATION *********************************************************/
                case ICMD_BUILTIN:      /* ..., [arg1, [arg2 ...]] ==> ...            */
+                       REPLACEMENT_POINT_FORGC_BUILTIN(cd, iptr);
+
                        bte = iptr->sx.s23.s3.bte;
                        md  = bte->md;
                        goto gen_method;
@@ -1434,27 +1662,27 @@ bool codegen_emit(jitdata *jd)
 #endif
                                        case TYPE_LNG:
                                                d = emit_load(jd, iptr, var, REG_ITMP12_PACKED);
-                                               M_LST(d, REG_SP, md->params[s3].regoff*4);
+                                               M_LST(d, REG_SP, md->params[s3].regoff);
                                                break;
 #if defined(ENABLE_SOFTFLOAT)
                                        case TYPE_FLT:
 #endif
                                        case TYPE_INT:
                                                d = emit_load(jd, iptr, var, REG_ITMP1);
-                                               M_IST(d, REG_SP, md->params[s3].regoff*4);
+                                               M_IST(d, REG_SP, md->params[s3].regoff);
                                                break;
                                        case TYPE_ADR:
                                                d = emit_load(jd, iptr, var, REG_ATMP1);
-                                               M_AST(d, REG_SP, md->params[s3].regoff*4);
+                                               M_AST(d, REG_SP, md->params[s3].regoff);
                                                break;
 #if !defined(ENABLE_SOFTFLOAT)
                                        case TYPE_FLT:
                                                d = emit_load(jd, iptr, var, REG_FTMP1);
-                                               M_FST(d, REG_SP, md->params[s3].regoff*4);
+                                               M_FST(d, REG_SP, md->params[s3].regoff);
                                                break;
                                        case TYPE_DBL:
                                                d = emit_load(jd, iptr, var, REG_FTMP1);
-                                               M_DST(d, REG_SP, md->params[s3].regoff*4);
+                                               M_DST(d, REG_SP, md->params[s3].regoff);
                                                break;
 #endif
                                        default:
@@ -1504,7 +1732,7 @@ bool codegen_emit(jitdata *jd)
                                        /* load object pointer (==argument 0) */
                                        M_ALD(REG_ATMP1, REG_SP, 0);
                                        /* implicit null-pointer check */
-                                       M_ALD(REG_METHODPTR, REG_ATMP1, OFFSET(java_objectheader, vftbl));
+                                       M_ALD(REG_METHODPTR, REG_ATMP1, OFFSET(java_object_t, vftbl));
                                        M_ALD(REG_ATMP3, REG_METHODPTR, s1);
                                        /* generate the actual call */
                                        M_JSR(REG_ATMP3);
@@ -1523,7 +1751,7 @@ bool codegen_emit(jitdata *jd)
                                        M_ALD(REG_ATMP1, REG_SP, 0);
 
                                        /* implicit null-pointer check */
-                                       M_ALD(REG_METHODPTR, REG_ATMP1, OFFSET(java_objectheader, vftbl));
+                                       M_ALD(REG_METHODPTR, REG_ATMP1, OFFSET(java_object_t, vftbl));
                                        M_ALD(REG_METHODPTR, REG_METHODPTR, s1);
                                        M_ALD(REG_ATMP3, REG_METHODPTR, s2);
 
@@ -1536,6 +1764,7 @@ bool codegen_emit(jitdata *jd)
                                }       /* switch (iptr->opc) */
 
                                REPLACEMENT_POINT_INVOKE_RETURN(cd, iptr);
+                               REPLACEMENT_POINT_FORGC_BUILTIN_RETURN(cd, iptr);
                                
                                /* store return value */
                                d = md->returntype.type;
@@ -1562,14 +1791,26 @@ bool codegen_emit(jitdata *jd)
                                                M_INT2ADRMOVE(REG_RESULT, s1);
                                                break;
 #if !defined(ENABLE_SOFTFLOAT)
+                                       /*
+                                        *      for BUILTINS float values are returned in %d0,%d1
+                                        *      within cacao we use %fp0 for that.
+                                        */
                                        case TYPE_FLT:
                                                s1 = codegen_reg_of_dst(jd, iptr, REG_FTMP1);
-                                               M_INT2FLTMOVE(REG_FRESULT, s1);
+                                               if (iptr->opc == ICMD_BUILTIN)  {
+                                                       M_INT2FLTMOVE(REG_FRESULT, s1);
+                                               } else  {
+                                                       M_FLTMOVE(REG_FRESULT, s1);
+                                               }
                                                break;
                                        case TYPE_DBL:
                                                s1 = codegen_reg_of_dst(jd, iptr, REG_FTMP1);
-                                               M_LST(REG_RESULT_PACKED, REG_SP, rd->memuse * 4);
-                                               M_DLD(s1, REG_SP, rd->memuse * 4);
+                                               if (iptr->opc == ICMD_BUILTIN)  {
+                                                       M_LST(REG_RESULT_PACKED, REG_SP, rd->memuse * 4 + 4);
+                                                       M_DLD(s1, REG_SP, rd->memuse * 4 + 4);
+                                               } else  {
+                                                       M_DBLMOVE(REG_FRESULT, s1);
+                                               }
                                                break;
 #endif
                                        default:
@@ -1608,7 +1849,6 @@ bool codegen_emit(jitdata *jd)
                case ICMD_DRETURN:
 #endif
                case ICMD_LRETURN:      /* ..., retvalue ==> ...                      */
-
                        REPLACEMENT_POINT_RETURN(cd, iptr);
                        s1 = emit_load_s1(jd, iptr, REG_RESULT_PACKED);
                        M_LNGMOVE(s1, REG_RESULT_PACKED);
@@ -1616,12 +1856,17 @@ bool codegen_emit(jitdata *jd)
 
 #if !defined(ENABLE_SOFTFLOAT)
                case ICMD_FRETURN:      /* ..., retvalue ==> ...                      */
-               case ICMD_DRETURN:
-
                        REPLACEMENT_POINT_RETURN(cd, iptr);
                        s1 = emit_load_s1(jd, iptr, REG_FRESULT);
                        M_FLTMOVE(s1, REG_FRESULT);
                        goto nowperformreturn;
+
+               case ICMD_DRETURN:
+                       REPLACEMENT_POINT_RETURN(cd, iptr);
+                       s1 = emit_load_s1(jd, iptr, REG_FRESULT);
+                       M_DBLMOVE(s1, REG_FRESULT);
+                       goto nowperformreturn;
+
 #endif
 
                case ICMD_RETURN:      /* ...  ==> ...                                */
@@ -1642,7 +1887,7 @@ nowperformreturn:
 #if defined(ENABLE_THREADS)
                        /* call lock_monitor_exit */
                        if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
-                               M_ILD(REG_ITMP3, REG_SP, rd->memuse * 4 + 2*4);
+                               M_ILD(REG_ITMP3, REG_SP, rd->memuse * 8);
 
                                /* we need to save the proper return value */
                                /* we do not care for the long -> doubel convert space here */
@@ -1651,21 +1896,21 @@ nowperformreturn:
                                case ICMD_DRETURN:
 #endif
                                case ICMD_LRETURN:
-                                       M_LST(REG_RESULT_PACKED, REG_SP, rd->memuse * 4);
+                                       M_LST(REG_RESULT_PACKED, REG_SP, rd->memuse * 8 + 8);
                                        break;
 #if defined(ENABLE_SOFTFLOAT)
                                case ICMD_FRETURN:
 #endif
                                case ICMD_IRETURN:
                                case ICMD_ARETURN:
-                                       M_IST(REG_RESULT , REG_SP, rd->memuse * 4);
+                                       M_IST(REG_RESULT , REG_SP, rd->memuse * 8 + 8);
                                        break;
 #if !defined(ENABLE_SOFTFLOAT)
                                case ICMD_FRETURN:
-                                       M_FST(REG_FRESULT, REG_SP, rd->memuse * 4);
+                                       M_FST(REG_FRESULT, REG_SP, rd->memuse * 8 + 8);
                                        break;
                                case ICMD_DRETURN:
-                                       M_DST(REG_FRESULT, REG_SP, rd->memuse * 4);
+                                       M_DST(REG_FRESULT, REG_SP, rd->memuse * 8 + 8);
                                        break;
 #endif
                                }
@@ -1680,21 +1925,21 @@ nowperformreturn:
                                case ICMD_DRETURN:
 #endif
                                case ICMD_LRETURN:
-                                       M_LLD(REG_RESULT_PACKED, REG_SP, rd->memuse * 4);
+                                       M_LLD(REG_RESULT_PACKED, REG_SP, rd->memuse * 8 + 8);
                                        break;
 #if defined(ENABLE_SOFTFLOAT)
                                case ICMD_FRETURN:
 #endif
                                case ICMD_IRETURN:
                                case ICMD_ARETURN:
-                                       M_ILD(REG_RESULT , REG_SP, rd->memuse * 4);
+                                       M_ILD(REG_RESULT , REG_SP, rd->memuse * 8 + 8);
                                        break;
 #if !defined(ENABLE_SOFTFLOAT)
                                case ICMD_FRETURN:
-                                       M_FLD(REG_FRESULT, REG_SP, rd->memuse * 4);
+                                       M_FLD(REG_FRESULT, REG_SP, rd->memuse * 8 + 8);
                                        break;
                                case ICMD_DRETURN:
-                                       M_DLD(REG_FRESULT, REG_SP, rd->memuse * 4);
+                                       M_DLD(REG_FRESULT, REG_SP, rd->memuse * 8 + 8);
                                        break;
 #endif
                                }
@@ -1715,18 +1960,18 @@ nowperformreturn:
                        /* restore saved registers                                        */
 
                        for (i = INT_SAV_CNT - 1; i >= rd->savintreguse; i--) {
-                               p--; M_ILD(rd->savintregs[i], REG_SP, p * 4);
+                               p-=8; M_ILD(rd->savintregs[i], REG_SP, p);
                        }
                        for (i=ADR_SAV_CNT-1; i>=rd->savadrreguse; --i) {
-                               p--; M_ALD(rd->savadrregs[i], REG_SP, p*4);
+                               p-=8; M_ALD(rd->savadrregs[i], REG_SP, p);
                        }
 #if !defined(ENABLE_SOFTFLOAT)
                        for (i = FLT_SAV_CNT - 1; i >= rd->savfltreguse; i--) {
-                               p -= 2; M_DLD(rd->savfltregs[i], REG_SP, p * 4);
+                               p-=8; M_FLOAD(rd->savfltregs[i], REG_SP, p);
                        }
 #endif
                        /* deallocate stack                                               */
-                       M_AADD_IMM(cd->stackframesize*4, REG_SP);
+                       M_AADD_IMM(cd->stackframesize, REG_SP);
                        M_RET;
                        }
                        break;
@@ -1795,7 +2040,7 @@ nowperformreturn:
                                        emit_label_beq(cd, BRANCH_LABEL_3);
                                }
 
-                               M_ALD(REG_ATMP1, s1, OFFSET(java_objectheader, vftbl));
+                               M_ALD(REG_ATMP1, s1, OFFSET(java_object_t, vftbl));
                                M_ILD(REG_ITMP3, REG_ATMP1, OFFSET(vftbl_t, interfacetablelength));
                                M_IADD_IMM(-superindex, REG_ITMP3);     /* -superindex may be patched patched */
                                M_ITST(REG_ITMP3);
@@ -1825,7 +2070,7 @@ nowperformreturn:
                                        emit_label_beq(cd, BRANCH_LABEL_5);
                                }
 
-                               M_ALD(REG_ATMP1, s1, OFFSET(java_objectheader, vftbl));
+                               M_ALD(REG_ATMP1, s1, OFFSET(java_object_t, vftbl));
 
                                CODEGEN_CRITICAL_SECTION_START;
 
@@ -1915,7 +2160,7 @@ nowperformreturn:
                                                emit_label_beq(cd, BRANCH_LABEL_3);
                                        }
 
-                                       M_ALD(REG_ATMP2, s1, OFFSET(java_objectheader, vftbl));
+                                       M_ALD(REG_ATMP2, s1, OFFSET(java_object_t, vftbl));
                                        M_ILD(REG_ITMP3, REG_ATMP2, OFFSET(vftbl_t, interfacetablelength));
        
                                        M_IADD_IMM(-superindex, REG_ITMP3);     /* superindex patched */
@@ -1946,7 +2191,7 @@ nowperformreturn:
                                                emit_label_beq(cd, BRANCH_LABEL_5);
                                        }
 
-                                       M_ALD(REG_ATMP2, s1, OFFSET(java_objectheader, vftbl));
+                                       M_ALD(REG_ATMP2, s1, OFFSET(java_object_t, vftbl));
 
                                        CODEGEN_CRITICAL_SECTION_START;
 
@@ -2114,8 +2359,21 @@ nowperformreturn:
                        exceptions_throw_internalerror("Unknown ICMD %d during code generation", iptr->opc);
                        return false;
        } /* switch */
-       M_TPF;
+       /* M_TPF; */ /* nop after each ICMD */
        } /* for each instruction */
+
+       /* At the end of a basic block we may have to append some nops,
+          because the patcher stub calling code might be longer than the
+          actual instruction. So codepatching does not change the
+          following block unintentionally. */
+
+       if (cd->mcodeptr < cd->lastmcodeptr) {
+               while (cd->mcodeptr < cd->lastmcodeptr) {
+                       M_NOP;
+               }
+       }
+
+
        } /* if (btpre->flags >= BBREACHED) */
        } /* for each basic block */
 
@@ -2123,7 +2381,6 @@ nowperformreturn:
 
        /* generate stubs */
        emit_patcher_stubs(jd);
-       REPLACEMENT_EMIT_STUBS(jd);
 
        return true;
 }
@@ -2152,6 +2409,165 @@ void codegen_emit_stub_compiler(jitdata *jd)
        M_JMP(REG_ATMP3);
 }
 
+/* codegen_emit_stub_builtin ***************************************************
+
+   Creates a stub routine which calls a builtin function.
+
+*******************************************************************************/
+
+void codegen_emit_stub_builtin(jitdata *jd, builtintable_entry *bte)
+{
+       codeinfo    *code;
+       codegendata *cd;
+       methoddesc  *md;
+       s4           i;
+       s4           disp;
+       s4           s1, s2;
+
+       /* get required compiler data */
+       code = jd->code;
+       cd   = jd->cd;
+
+       /* set some variables */
+       md = bte->md;
+
+       /* calculate stack frame size */
+       cd->stackframesize =
+               sizeof(stackframeinfo) / SIZEOF_VOID_P +
+               4;                              /* 4 arguments or return value        */
+
+       /* create method header */
+       (void) dseg_add_unique_address(cd, code);              /* CodeinfoPointer */
+       (void) dseg_add_unique_s4(cd, cd->stackframesize * 4); /* FrameSize       */
+       (void) dseg_add_unique_s4(cd, 0);                      /* IsSync          */
+       (void) dseg_add_unique_s4(cd, 0);                      /* IsLeaf          */
+       (void) dseg_add_unique_s4(cd, 0);                      /* IntSave         */
+       (void) dseg_add_unique_s4(cd, 0);                      /* FltSave         */
+       (void) dseg_addlinenumbertablesize(cd);
+       (void) dseg_add_unique_s4(cd, 0);                      /* ExTableSize     */
+
+       /* generate stub code */
+       M_AADD_IMM(-(cd->stackframesize*4), REG_SP);
+
+#if defined(ENABLE_GC_CACAO)
+       /* Save callee saved integer registers in stackframeinfo (GC may
+          need to recover them during a collection). */
+
+       disp = cd->stackframesize * 4 - sizeof(stackframeinfo) +
+               OFFSET(stackframeinfo, adrregs);
+
+       for (i = 0; i < ADR_SAV_CNT; i++)
+               M_AST(abi_registers_address_saved[i], REG_SP, disp + i * 4);
+#endif
+
+       /* create dynamic stack info */
+
+       M_AMOV(REG_SP, REG_ATMP1);
+       M_AADD_IMM(cd->stackframesize * 4, REG_ATMP1);
+       M_AST(REG_ATMP1, REG_SP, 0 * 4);        /* datasp */
+
+       M_AMOV_IMM(0, REG_ATMP1);                       /* we need pv patched in */
+       dseg_adddata(cd);                                       /* this does the trick */
+       M_AST(REG_ATMP1, REG_SP, 1 * 4);        /* pv */
+
+       M_AMOV(REG_SP, REG_ATMP1);
+       M_AADD_IMM(cd->stackframesize * 4 + SIZEOF_VOID_P, REG_ATMP1);
+       M_AST(REG_ATMP1, REG_SP, 2 * 4);                        /* sp */
+
+       M_ALD(REG_ATMP3, REG_SP, cd->stackframesize * 4);
+       M_AST(REG_ATMP3, REG_SP, 3 * 4);                        /* ra */
+
+       M_JSR_IMM(codegen_stub_builtin_enter);
+
+       /* copy arguments into new stackframe */
+
+       for (i = 0; i < md->paramcount; i++) {
+               if (!md->params[i].inmemory) {
+                       log_text("No integer argument registers available!");
+                       assert(0);
+
+               } else {       /* float/double in memory can be copied like int/longs */
+                       s1 = md->params[i].regoff + cd->stackframesize * 4 + 4;
+                       s2 = md->params[i].regoff;
+
+                       M_ILD(REG_ITMP1, REG_SP, s1);
+                       M_IST(REG_ITMP1, REG_SP, s2);
+                       if (IS_2_WORD_TYPE(md->paramtypes[i].type)) {
+                               M_ILD(REG_ITMP1, REG_SP, s1 + 4);
+                               M_IST(REG_ITMP1, REG_SP, s2 + 4);
+                       }
+
+               }
+       }
+
+       /* call the builtin function */
+
+       M_AMOV_IMM(bte->fp, REG_ATMP3);
+       M_JSR(REG_ATMP3);
+
+       /* save return value */
+       switch (md->returntype.type)    {
+               case TYPE_VOID: break;
+
+               /* natives return float arguments in %d0, %d1, cacao expects them in %fp0 */
+               case TYPE_DBL:
+               case TYPE_LNG:
+                       M_IST(REG_D1, REG_SP, 2 * 4);
+                       /* fall through */
+
+               case TYPE_FLT:
+               case TYPE_INT:
+               case TYPE_ADR:
+                       M_IST(REG_D0, REG_SP, 1 * 4);
+                       break;
+
+               default: assert(0);
+       }
+
+       /* remove native stackframe info */
+
+       M_AMOV(REG_SP, REG_ATMP1);
+       M_AADD_IMM(cd->stackframesize * 4, REG_ATMP1);
+       M_AST(REG_ATMP1, REG_SP, 0 * 4);
+
+       M_JSR_IMM(codegen_stub_builtin_exit);
+
+       /* restore return value */
+       switch (md->returntype.type)    {
+               case TYPE_VOID: break;
+
+               case TYPE_DBL:
+               case TYPE_LNG:
+                       M_ILD(REG_D1, REG_SP, 2 * 4);
+                       /* fall through */
+
+               case TYPE_FLT:
+               case TYPE_INT:
+               case TYPE_ADR:
+                       M_ILD(REG_D0, REG_SP, 1 * 4);
+                       break;
+
+               default: assert(0);
+       }
+
+#if defined(ENABLE_GC_CACAO)
+       /* Restore callee saved integer registers from stackframeinfo (GC
+          might have modified them during a collection). */
+        
+       disp = cd->stackframesize * 4 - sizeof(stackframeinfo) +
+               OFFSET(stackframeinfo, adrregs);
+
+       for (i = 0; i < ADR_SAV_CNT; i++)
+               M_ALD(abi_registers_address_saved[i], REG_SP, disp + i * 4);
+#endif
+
+       /* remove stackframe */
+       M_AADD_IMM(cd->stackframesize * 4, REG_SP);
+       M_RET;
+}
+
+
+
 
 /* codegen_emit_stub_native ****************************************************
 
@@ -2182,10 +2598,6 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
        cd->stackframesize =    sizeof(stackframeinfo) / SIZEOF_VOID_P +
                                sizeof(localref_table) / SIZEOF_VOID_P +
                                nmd->memuse +
-                       #if 0
-                               4 +                                             /* %d0,%d1,%a0,%a1*/
-                               2 * 2 +                                         /* %f0,%f1 */
-                       #endif
                                1 +                                             /* functionptr */
                                4;                                              /* args for codegen_start_native_call */
 
@@ -2219,30 +2631,11 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
 
        M_AST(REG_ATMP2, REG_SP, 4 * 4);
 
-       /* save integer and float temp registers */
-       /*
-       M_IST(REG_D0, REG_SP, 4*4 + 2*8 + 3*4);
-       M_IST(REG_D1, REG_SP, 4*4 + 2*8 + 2*4);
-       M_AST(REG_A0, REG_SP, 4*4 + 2*8 + 1*4);
-       M_AST(REG_A1, REG_SP, 4*4 + 2*8 + 0*4);
-       */
-
-       /* TODO */
-       /* store %f0, %f1 */
-
        /* put arguments for codegen_start_native_call onto stack */
        /* void codegen_start_native_call(u1 *datasp, u1 *pv, u1 *sp, u1 *ra) */
        
        M_AMOV(REG_SP, REG_ATMP1);
-       M_AADD_IMM(cd->stackframesize * 4, REG_ATMP1);
-
-       M_ALD(REG_ATMP3, REG_ATMP1, 0 * 4);
-       M_AST(REG_ATMP3, REG_SP, 3 * 4);                /* ra */
-
-       M_AST(REG_ATMP1, REG_SP, 0 * 4);                /* datasp */
-
-       M_AADD_IMM(1 * 4 , REG_ATMP1);                  
-       M_AST(REG_ATMP1, REG_SP, 2 * 4);                /* sp */
+       M_AST(REG_ATMP1, REG_SP, 0 * 4);                /* currentsp */
 
        M_AMOV_IMM(0, REG_ATMP2);                       /* 0 needs to patched */
        dseg_adddata(cd);                                   /* this patches it */
@@ -2260,8 +2653,8 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
                /* all arguments via stack */
                assert(md->params[i].inmemory);                                         
 
-               s1 = (md->params[i].regoff + cd->stackframesize + 1) * 4;
-               s2 = nmd->params[j].regoff * 4;
+               s1 = md->params[i].regoff + cd->stackframesize * 4 + 4;
+               s2 = nmd->params[j].regoff;
 
                /* simply copy argument stack */
                M_ILD(REG_ITMP1, REG_SP, s1);
@@ -2288,25 +2681,18 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
        switch (md->returntype.type)    {
                case TYPE_VOID: break;
 
-#if defined(ENABLE_SOFTFLOAT)
+               /* natives return float arguments in %d0, %d1, cacao expects them in %fp0 */
                case TYPE_DBL:
-#endif
                case TYPE_LNG:
                        M_IST(REG_D1, REG_SP, 2 * 4);
                        /* fall through */
 
-#if defined(ENABLE_SOFTFLOAT)
                case TYPE_FLT:
-#endif
                case TYPE_INT:
                case TYPE_ADR:
                        M_IST(REG_D0, REG_SP, 1 * 4);
                        break;
 
-#if !defined(ENABLE_SOFTFLOAT)
-               case TYPE_FLT:
-               case TYPE_DBL:  /* FIXME */
-#endif
                default: assert(0);
        }
        
@@ -2329,26 +2715,30 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
        switch (md->returntype.type)    {
                case TYPE_VOID: break;
 
-#if defined(ENABLE_SOFTFLOAT)
                case TYPE_DBL:
-#endif
                case TYPE_LNG:
                        M_ILD(REG_D1, REG_SP, 2 * 4);
                        /* fall through */
-#if defined(ENABLE_SOFTFLOAT)
                case TYPE_FLT:
-#endif
                case TYPE_INT:
                case TYPE_ADR:
                        M_ILD(REG_D0, REG_SP, 1 * 4);
                        break;
 
+               default: assert(0);
+       }
 #if !defined(ENABLE_SOFTFLOAT)
+               /* additionally load values into floating points registers
+                * as cacao jit code expects them there */
+       switch (md->returntype.type)    {
                case TYPE_FLT:
-               case TYPE_DBL:  /* FIXME */
-#endif
-               default: assert(0);
+                       M_FLD(REG_D0, REG_SP, 1 * 4);
+                       break;
+               case TYPE_DBL:  
+                       M_DLD(REG_D0, REG_SP, 1 * 4);
+                       break;
        }
+#endif
        /* restore saved registers */
 
        M_AADD_IMM(cd->stackframesize*4, REG_SP);