use two functions for creating/removing items of the native stackframe list (i386...
[cacao.git] / src / vm / jit / codegen.inc
index 4b0f571d00f00b6782e9fca52596ea183facb34f..a2300e621348f624d2e5c61d010db077d0561045 100644 (file)
@@ -1,4 +1,4 @@
-/* jit/codegen.inc - architecture independent code generator
+/* vm/jit/codegen.inc - architecture independent code generator
 
    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
    Institut f. Computersprachen, TU Wien
@@ -28,8 +28,7 @@
    Authors: Reinhard Grafl
             Andreas  Krall
 
-   Changes: Michael Gschwind
-            Christian Thalinger
+   Changes: Christian Thalinger
 
    All functions assume the following code area / data area layout:
 
    memory. All functions writing values into the data area return the offset
    relative the begin of the code area (start of procedure).   
 
-   $Id: codegen.inc 1502 2004-11-12 18:32:57Z motse $
+   $Id: codegen.inc 1680 2004-12-04 12:02:08Z jowenn $
 
 */
 
+#ifndef _CODEGEN_INC_H_
+#define _CODEGEN_INC_H_
 
 #include <string.h>
-#include "exceptions.h"
-#include "native.h"
-#include "options.h"
-#include "statistics.h"
-#include "jit/codegen.inc.h"
-#include "toolbox/memory.h"
-#include "toolbox/logging.h"
+
+#if !defined(STATIC_CLASSPATH)
+# include <dlfcn.h>
+#endif
+
+#include "mm/memory.h"
 #include "toolbox/avl.h"
-#include "threads/thread.h"
-#ifndef STATIC_CLASSPATH
-#include <dlfcn.h>
+#include "toolbox/logging.h"
+#include "native/native.h"
+
+#if defined(USE_THREADS)
+# if defined(NATIVE_THREADS)
+#  include "threads/native/threads.h"
+# else
+#  include "threads/green/threads.h"
+# endif
 #endif
 
-/* in this tree we store all method addresses */
+#include "vm/exceptions.h"
+#include "vm/options.h"
+#include "vm/statistics.h"
+#include "vm/jit/codegen.inc.h"
+
+
+/* in this tree we store all method addresses *********************************/
 
 #if defined(__I386__) || defined(__X86_64__)
 static struct avl_table *methodtree = NULL;
@@ -133,6 +145,7 @@ void codegen_setup(methodinfo *m, codegendata *cd, t_inlining_globals *id)
        cd->xcastrefs = NULL;
        cd->xdivrefs = NULL;
        cd->xexceptionrefs = NULL;
+       cd->clinitrefs = NULL;
 
        cd->linenumberreferences = NULL;
        cd->linenumbertablesizepos = 0;
@@ -518,6 +531,23 @@ static void codegen_addxdivrefs(codegendata *cd, void *branchptr)
 #endif
 
 
+static void codegen_addclinitref(codegendata *cd,
+                                                                void *branchptr,
+                                                                classinfo *class)
+{
+       s4 branchpos;
+       clinitref *cr;
+
+       branchpos = (u1 *) branchptr - cd->mcodebase;
+
+       cr = DNEW(clinitref);
+       cr->branchpos = branchpos;
+       cr->class = class;
+       cr->next = cd->clinitrefs;
+       cd->clinitrefs = cr;
+}
+
+
 static void codegen_createlinenumbertable(codegendata *cd)
 {
 #ifdef __I386__
@@ -702,15 +732,16 @@ static void codegen_finish(methodinfo *m, codegendata *cd, s4 mcodelen)
 
                for (lr = cd->linenumberreferences; lr != NULL; lr = lr->next) {
                        lrtlen++;
-                       *((void**) (epoint + lr->tablepos)) = epoint + lr->targetmpc;
+                       *((functionptr *) ((long) epoint + (long) lr->tablepos)) =
+                               (functionptr) ((long) epoint + (long) lr->targetmpc);
                }
                
-               *((void**) (epoint + cd->linenumbertablestartpos)) =
-                       epoint + cd->linenumbertab;
+               *((functionptr *) ((long) epoint + cd->linenumbertablestartpos)) =
+                       (functionptr) ((long) epoint + cd->linenumbertab);
 #if POINTERSIZE == 8
-               *((s8*) (epoint + cd->linenumbertablesizepos)) = lrtlen;
+               *((s8 *) ((s8) epoint + cd->linenumbertablesizepos)) = lrtlen;
 #else
-               *((s4*) (epoint + cd->linenumbertablesizepos)) = lrtlen;
+               *((s4 *) ((s4) epoint + cd->linenumbertablesizepos)) = lrtlen;
 #endif
        }
 #endif
@@ -932,18 +963,24 @@ static void codegen_resolve_native(methodinfo *m,void **insertionPoint,void *jmp
   char *nativeName, *nativeNameEscape;
   size_t nativeLen;
   size_t i;
+       void *lib;
+       void *sym;
 
   builtin_monitorenter((java_objectheader*) m);
+#if defined(__X86_64__)
+  if ((*((s4*)jmpPatchTarget))==((s4)jmpTarget)) {
+#else
   if ((*jmpPatchTarget)==jmpTarget) {
+#endif
     builtin_monitorexit((java_objectheader*) m);
     return;
   }
-  log_text("trying to resolve a native method");
+  /*log_text("trying to resolve a native method");
   utf_display(m->class->name);
-  utf_display(m->name);
+  utf_display(m->name);*/
   
-  void *lib=dlopen(0,RTLD_NOW | RTLD_GLOBAL);
-  if (lib) {
+  lib=dlopen(0,RTLD_NOW | RTLD_GLOBAL); /* open the application execution image */
+  if (lib) { /* it did work -> good, otherwise fail with error*/
     int ok=0;
     /*generate the name of the native function in the form Java_package1_package2...._classname_methodname*/
     nativeLen=/*Java_*/5+strlen(m->class->name->text)+/*_*/1+strlen(m->name->text)+/*\0*/1;
@@ -968,38 +1005,46 @@ static void codegen_resolve_native(methodinfo *m,void **insertionPoint,void *jmp
 
 /*     printf("nativename: %s\n",nativeName); */
 
-    void *sym=dlsym(lib,nativeName);
+    /*try to find the symbal fo the function */
+    sym=dlsym(lib,nativeName);
     if (sym) {
-      ok=1;
-      log_text("resolved");
+      ok=1; /* it worked, everything fine */
+      /*log_text("resolved");*/
       MFREE(nativeName,char,nativeLen);
-    } else {
+    } else { /* we didn't find the symbol yet, try to resolve an overlaoded function (having the types in it's name*/
       size_t overloadedNativeLen=nativeLen+codegen_overloadPartLen(m->descriptor);
       char *overloadedNative=MNEW(char,overloadedNativeLen);
       sprintf(overloadedNative,"%s",nativeName);
       MFREE(nativeName,char,nativeLen);
       codegen_fillInOverloadPart(overloadedNative,m->descriptor);
-      log_text("symbol not found,trying harder (overloaded member ?)");
+      /*log_text("symbol not found,trying harder (overloaded member ?)");*/
       sym=dlsym(lib,overloadedNative);
       if (sym) {
         MFREE(overloadedNative,char,overloadedNativeLen);
-        ok=1;
-        log_text("resolved");
+        ok=1; /* we eventually found the native function -> everything ok*/
+        /*log_text("resolved");*/
       } else { 
+         /* we failed to find the native function within the execution image (app + loaded libraries) -> will cause an exit*/
          MFREE(overloadedNative,char,overloadedNativeLen);
          log_text("It was not possible to find the native function implementation. Not even in overloading case");
       }
    }
+    /* patch the address of the native function into the stub and make the stub jump over this function call in the future */
     if (ok) {
       (*insertionPoint)=sym;
+#if defined(__X86_64__)
+      (*((s4*)jmpPatchTarget))=(s4)jmpTarget;
+#else
       (*jmpPatchTarget)=jmpTarget;
+#endif
       builtin_monitorexit((java_objectheader *) m );
       return;
     }
 
   } else log_text("library not found");
   
-
+  /* There was an error, either the app image could not be opened or the native does not exist in the application and the
+     loaded libraries. Show an additional error and exit the vm*/
   {
     char *info;
     size_t slen;
@@ -1016,6 +1061,8 @@ static void codegen_resolve_native(methodinfo *m,void **insertionPoint,void *jmp
 #endif
 
 
+#endif
+
 /*
  * These are local overrides for various environment variables in Emacs.
  * Please do not remove this and leave it at the end of the file, where