* src/vm/jit/code.h (codeinfo) [ENABLE_PROFILING]: Made frequency,
[cacao.git] / src / vm / jit / codegen-common.c
index 72e763fd4c60c392a1ff11565996ed6043e02afc..2eb113e7156135198ca6c25ec7fc0d91eee3dacf 100644 (file)
@@ -1,6 +1,6 @@
 /* src/vm/jit/codegen-common.c - architecture independent code generator stuff
 
-   Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
+   Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
    J. Wenninger, Institut f. Computersprachen - TU Wien
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   Contact: cacao@cacaojvm.org
-
-   Authors: Reinhard Grafl
-            Andreas  Krall
-
-   Changes: Christian Thalinger
-            Joseph Wenninger
-                       Edwin Steiner
-
    All functions assume the following code area / data area layout:
 
    +-----------+
@@ -48,7 +39,7 @@
    memory. All functions writing values into the data area return the offset
    relative the begin of the code area (start of procedure).   
 
-   $Id: codegen-common.c 5805 2006-10-19 09:32:29Z twisti $
+   $Id: codegen-common.c 7246 2007-01-29 18:49:05Z twisti $
 
 */
 
@@ -61,7 +52,7 @@
 #include "vm/types.h"
 
 #if defined(ENABLE_JIT)
-/* this is required for gen_resolvebranch and PATCHER_CALL_SIZE */
+/* this is required PATCHER_CALL_SIZE */
 # include "codegen.h"
 #endif
 
 #endif
 
 #include "mm/memory.h"
+
 #include "toolbox/avl.h"
+#include "toolbox/list.h"
 #include "toolbox/logging.h"
+
 #include "native/jni.h"
 #include "native/native.h"
 
 #endif
 
 #include "vm/exceptions.h"
-#include "vm/method.h"
-#include "vm/options.h"
-#include "vm/statistics.h"
 #include "vm/stringlocal.h"
+
 #include "vm/jit/asmpart.h"
 #include "vm/jit/codegen-common.h"
 
@@ -94,6 +86,7 @@
 
 #include "vm/jit/dseg.h"
 #include "vm/jit/jit.h"
+#include "vm/jit/md.h"
 #include "vm/jit/stacktrace.h"
 #include "vm/jit/replace.h"
 
 #include "vm/jit/intrp/intrp.h"
 #endif
 
+#include "vmcore/method.h"
+#include "vmcore/options.h"
+
+#if defined(ENABLE_STATISTICS)
+# include "vmcore/statistics.h"
+#endif
+
 
 /* in this tree we store all method addresses *********************************/
 
@@ -190,6 +190,7 @@ void codegen_setup(jitdata *jd)
 #endif
 
        cd->exceptionrefs  = NULL;
+/*     cd->patchrefs      = list_create_dump(OFFSET(patchref, linkage)); */
        cd->patchrefs      = NULL;
 
        cd->linenumberreferences = NULL;
@@ -200,7 +201,6 @@ void codegen_setup(jitdata *jd)
        cd->method = m;
 
        cd->maxstack = m->maxstack;
-       cd->maxlocals = m->maxlocals;
 
 #if defined(ENABLE_THREADS)
        cd->threadcritcurrent.next = NULL;
@@ -287,35 +287,41 @@ u1 *codegen_ncode_increase(codegendata *cd, u1 *ncodeptr)
 #endif
 
 
-void codegen_addreference(codegendata *cd, basicblock *target)
+/* codegen_add_branch_ref ******************************************************
+
+   Prepends an branch to the list.
+
+*******************************************************************************/
+
+void codegen_add_branch_ref(codegendata *cd, basicblock *target)
 {
-       s4 branchpos;
+       s4 branchmpc;
 
-       branchpos = (u1 *) cd->mcodeptr - cd->mcodebase;
+       /* calculate the mpc of the branch instruction */
+
+       branchmpc = cd->mcodeptr - cd->mcodebase;
 
 #if defined(ENABLE_JIT)
        /* Check if the target basicblock has already a start pc, so the
           jump is backward and we can resolve it immediately. */
 
-       /* The interpreter uses absolute branches, so we do branch
-          resolving after the code and data segment move. */
-
-       if (target->mpc >= 0
+       if ((target->mpc >= 0)
 # if defined(ENABLE_INTRP)
+               /* The interpreter uses absolute branches, so we do branch
+                  resolving after the code and data segment move. */
+
                && !opt_intrp
 # endif
                )
        {
-               gen_resolvebranch((u1 *) cd->mcodebase + branchpos,
-                                                 branchpos,
-                                                 target->mpc);
-
-       } else
+               md_codegen_patch_branch(cd, branchmpc, target->mpc);
+       }
+       else
 #endif
        {
                branchref *br = DNEW(branchref);
 
-               br->branchpos = branchpos;
+               br->branchpos = branchmpc;
                br->next      = target->branchrefs;
 
                target->branchrefs = br;
@@ -323,28 +329,53 @@ void codegen_addreference(codegendata *cd, basicblock *target)
 }
 
 
+/* codegen_resolve_branchrefs **************************************************
+
+   Resolves and patches the branch references of a given basic block.
+
+*******************************************************************************/
+
+void codegen_resolve_branchrefs(codegendata *cd, basicblock *bptr)
+{
+       branchref *br;
+       s4         branchmpc;
+       s4         targetmpc;
+
+       /* set target */
+
+       targetmpc = bptr->mpc;
+
+       for (br = bptr->branchrefs; br != NULL; br = br->next) {
+               branchmpc = br->branchpos;
+
+               md_codegen_patch_branch(cd, branchmpc, targetmpc);
+       }
+}
+
+
 /* codegen_add_exception_ref ***************************************************
 
-   Adds an exception branch to the list.
+   Prepends an exception branch to the list.
 
 *******************************************************************************/
 
 static void codegen_add_exception_ref(codegendata *cd, s4 reg,
                                                                          functionptr function)
 {
-       s4            branchpos;
-       exceptionref *eref;
+       s4            branchmpc;
+       exceptionref *er;
+
+       branchmpc = cd->mcodeptr - cd->mcodebase;
 
-       branchpos = (u1 *) cd->mcodeptr - cd->mcodebase;
+       er = DNEW(exceptionref);
 
-       eref = DNEW(exceptionref);
+       er->branchpos = branchmpc;
+       er->reg       = reg;
+       er->function  = function;
 
-       eref->branchpos = branchpos;
-       eref->reg       = reg;
-       eref->function  = function;
-       eref->next      = cd->exceptionrefs;
+       er->next      = cd->exceptionrefs;
 
-       cd->exceptionrefs = eref;
+       cd->exceptionrefs = er;
 }
 
 
@@ -421,30 +452,38 @@ void codegen_add_fillinstacktrace_ref(codegendata *cd)
 }
 
 
-/* codegen_addpatchref *********************************************************
+/* codegen_add_patch_ref *******************************************************
 
-   Adds a new patcher reference to the list of patching positions.
+   Appends a new patcher reference to the list of patching positions.
 
 *******************************************************************************/
 
-void codegen_addpatchref(codegendata *cd, functionptr patcher, voidptr ref,
-                                                s4 disp)
+void codegen_add_patch_ref(codegendata *cd, functionptr patcher, voidptr ref,
+                                                  s4 disp)
 {
        patchref *pr;
-       s4        branchpos;
+       s4        branchmpc;
 
-       branchpos = cd->mcodeptr - cd->mcodebase;
+       branchmpc = cd->mcodeptr - cd->mcodebase;
 
        pr = DNEW(patchref);
 
-       pr->branchpos = branchpos;
+       pr->branchpos = branchmpc;
+       pr->disp      = disp;
        pr->patcher   = patcher;
        pr->ref       = ref;
-       pr->disp      = disp;
 
+/*     list_add_first(cd->patchrefs, pr); */
        pr->next      = cd->patchrefs;
        cd->patchrefs = pr;
 
+#if defined(ENABLE_JIT) && (defined(__ALPHA__) || defined(__MIPS__) || defined(__POWERPC__) || defined(__X86_64__))
+       /* Generate NOPs for opt_shownops. */
+
+       if (opt_shownops)
+               PATCHER_NOPS;
+#endif
+
 #if defined(ENABLE_JIT) && (defined(__I386__) || defined(__MIPS__) || defined(__X86_64__))
        /* On some architectures the patcher stub call instruction might
           be longer than the actual instruction generated.  On this
@@ -583,6 +622,58 @@ u1 *codegen_get_pv_from_pc_nocheck(u1 *pc)
 }
 
 
+/* codegen_set_replacement_point_notrap ****************************************
+
+   Record the position of a non-trappable replacement point.
+
+*******************************************************************************/
+
+#if defined(ENABLE_REPLACEMENT)
+#if !defined(NDEBUG)
+void codegen_set_replacement_point_notrap(codegendata *cd, s4 type)
+#else
+void codegen_set_replacement_point_notrap(codegendata *cd)
+#endif
+{
+       assert(cd->replacementpoint);
+       assert(cd->replacementpoint->type == type);
+       assert(cd->replacementpoint->flags & RPLPOINT_FLAG_NOTRAP);
+
+       cd->replacementpoint->pc = (u1*) (ptrint) (cd->mcodeptr - cd->mcodebase);
+
+       cd->replacementpoint++;
+}
+#endif /* defined(ENABLE_REPLACEMENT) */
+
+
+/* codegen_set_replacement_point ***********************************************
+
+   Record the position of a trappable replacement point.
+
+*******************************************************************************/
+
+#if defined(ENABLE_REPLACEMENT)
+#if !defined(NDEBUG)
+void codegen_set_replacement_point(codegendata *cd, s4 type)
+#else
+void codegen_set_replacement_point(codegendata *cd)
+#endif
+{
+       assert(cd->replacementpoint);
+       assert(cd->replacementpoint->type == type);
+       assert(!(cd->replacementpoint->flags & RPLPOINT_FLAG_NOTRAP));
+
+       cd->replacementpoint->pc = (u1*) (ptrint) (cd->mcodeptr - cd->mcodebase);
+
+       cd->replacementpoint++;
+
+       /* XXX assert(cd->lastmcodeptr <= cd->mcodeptr); */
+
+       cd->lastmcodeptr = cd->mcodeptr + PATCHER_CALL_SIZE;
+}
+#endif /* defined(ENABLE_REPLACEMENT) */
+
+
 /* codegen_finish **************************************************************
 
    Finishes the code generation. A new memory, large enough for both
@@ -633,7 +724,7 @@ void codegen_finish(jitdata *jd)
        }
 #endif
 
-       alignedmcodelen = ALIGN(mcodelen, MAX_ALIGN);
+       alignedmcodelen = MEMORY_ALIGN(mcodelen, MAX_ALIGN);
 
 #if defined(ENABLE_INTRP)
        if (opt_intrp)
@@ -643,7 +734,7 @@ void codegen_finish(jitdata *jd)
        }
 #endif
 
-       cd->dseglen = ALIGN(cd->dseglen, MAX_ALIGN);
+       cd->dseglen = MEMORY_ALIGN(cd->dseglen, MAX_ALIGN);
        alignedlen = alignedmcodelen + cd->dseglen;
 
 #if defined(ENABLE_INTRP)
@@ -727,17 +818,20 @@ void codegen_finish(jitdata *jd)
                *((ptrint *) ((ptrint) epoint + cd->linenumbertablesizepos)) = lrtlen;
        }
 
+#if defined(ENABLE_REPLACEMENT)
        /* replacement point resolving */
        {
                int i;
                rplpoint *rp;
 
+               code->replacementstubs += (ptrint) epoint;
+
                rp = code->rplpoints;
                for (i=0; i<code->rplpointcount; ++i, ++rp) {
                        rp->pc = (u1*) ((ptrint) epoint + (ptrint) rp->pc);
-                       rp->outcode = (u1*) ((ptrint) epoint + (ptrint) rp->outcode);
                }
        }
+#endif /* defined(ENABLE_REPLACEMENT) */
 
        /* add method into methodtree to find the entrypoint */
 
@@ -811,8 +905,10 @@ codeinfo *codegen_createnativestub(functionptr f, methodinfo *m)
 
        /* set the flags for the current JIT run */
 
+#if defined(ENABLE_PROFILING)
        if (opt_prof)
                jd->flags |= JITDATA_FLAG_INSTRUMENT;
+#endif
 
        if (opt_verbosecall)
                jd->flags |= JITDATA_FLAG_VERBOSECALL;
@@ -964,6 +1060,7 @@ void codegen_start_native_call(u1 *datasp, u1 *pv, u1 *sp, u1 *ra)
 
        stacktrace_create_native_stackframeinfo(sfi, pv, sp, ra);
 
+#if defined(ENABLE_JAVASE)
        /* add current JNI local references table to this thread */
 
        lrt->capacity    = LOCALREFTABLE_CAPACITY;
@@ -976,6 +1073,7 @@ void codegen_start_native_call(u1 *datasp, u1 *pv, u1 *sp, u1 *ra)
        MSET(lrt->refs, 0, java_objectheader*, LOCALREFTABLE_CAPACITY);
 
        LOCALREFTABLE = lrt;
+#endif
 }
 
 
@@ -1008,6 +1106,7 @@ java_objectheader *codegen_finish_native_call(u1 *datasp)
 
        *psfi = sfi->prev;
 
+#if defined(ENABLE_JAVASE)
        /* release JNI local references tables for this thread */
 
        lrt = LOCALREFTABLE;
@@ -1035,6 +1134,7 @@ java_objectheader *codegen_finish_native_call(u1 *datasp)
        /* now store the previous local frames in the thread structure */
 
        LOCALREFTABLE = lrt;
+#endif
 
        /* get the exception and return it */