[vmkit-commits] [vmkit] r61697 - in /vmkit/trunk/lib/JnJVM/VMCore: JavaJIT.cpp JnjvmClassLoader.cpp JnjvmModule.cpp JnjvmModule.h

Nicolas Geoffray nicolas.geoffray at lip6.fr
Mon Jan 5 08:49:44 PST 2009


Author: geoffray
Date: Mon Jan  5 10:49:43 2009
New Revision: 61697

URL: http://llvm.org/viewvc/llvm-project?rev=61697&view=rev
Log:
Better handling of native functions when static compiling.
Also add initial support for loading the base classes as a .so.


Modified:
    vmkit/trunk/lib/JnJVM/VMCore/JavaJIT.cpp
    vmkit/trunk/lib/JnJVM/VMCore/JnjvmClassLoader.cpp
    vmkit/trunk/lib/JnJVM/VMCore/JnjvmModule.cpp
    vmkit/trunk/lib/JnJVM/VMCore/JnjvmModule.h

Modified: vmkit/trunk/lib/JnJVM/VMCore/JavaJIT.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/JnJVM/VMCore/JavaJIT.cpp?rev=61697&r1=61696&r2=61697&view=diff

==============================================================================
--- vmkit/trunk/lib/JnJVM/VMCore/JavaJIT.cpp (original)
+++ vmkit/trunk/lib/JnJVM/VMCore/JavaJIT.cpp Mon Jan  5 10:49:43 2009
@@ -295,6 +295,44 @@
   
   Value* nativeFunc = module->getNativeFunction(compilingMethod, (void*)natPtr);
 
+  if (module->isStaticCompiling()) {
+    
+    if (compilingClass->isNativeOverloaded(compilingMethod)) {
+      compilingMethod->jniConsFromMethOverloaded(functionName);
+    } else {
+      compilingMethod->jniConsFromMeth(functionName);
+    }
+
+    Constant* Arg = ConstantArray::get(std::string(functionName), true);
+    GlobalVariable* GV = new GlobalVariable(Arg->getType(), true,
+                                            GlobalValue::InternalLinkage, Arg,
+                                            "", module);
+    Arg = ConstantExpr::getBitCast(GV, module->ptrType);
+    Value* Args[2] = { module->getNativeClass(compilingClass), Arg };
+
+    // If the global variable is null, then load it.
+    BasicBlock* unloadedBlock = createBasicBlock("");
+    BasicBlock* endBlock = createBasicBlock("");
+    Value* test = new LoadInst(nativeFunc, "", currentBlock);
+    const llvm::Type* Ty = test->getType();
+    PHINode* node = PHINode::Create(Ty, "", endBlock);
+    node->addIncoming(test, currentBlock);
+    Value* cmp = new ICmpInst(ICmpInst::ICMP_EQ, test,
+                              Constant::getNullValue(Ty), "", currentBlock);
+    BranchInst::Create(unloadedBlock, endBlock, cmp, currentBlock);
+    currentBlock = unloadedBlock;
+
+    Value* res = CallInst::Create(module->NativeLoader, Args, Args + 2, "",
+                                  currentBlock);
+
+    res = new BitCastInst(res, Ty, "", currentBlock);
+    new StoreInst(res, nativeFunc, currentBlock);
+    node->addIncoming(res, currentBlock);
+    BranchInst::Create(endBlock, currentBlock);
+    currentBlock = endBlock;
+    nativeFunc = node;
+  }
+
   Value* result = llvm::CallInst::Create(nativeFunc, nativeArgs.begin(),
                                          nativeArgs.end(), "", currentBlock);
 

Modified: vmkit/trunk/lib/JnJVM/VMCore/JnjvmClassLoader.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/JnJVM/VMCore/JnjvmClassLoader.cpp?rev=61697&r1=61696&r2=61697&view=diff

==============================================================================
--- vmkit/trunk/lib/JnJVM/VMCore/JnjvmClassLoader.cpp (original)
+++ vmkit/trunk/lib/JnJVM/VMCore/JnjvmClassLoader.cpp Mon Jan  5 10:49:43 2009
@@ -23,8 +23,10 @@
 
 #if defined(__MACH__)
 #define SELF_HANDLE RTLD_DEFAULT
+#define BOOTLIBNAME "libvmjc.dylib"
 #else
 #define SELF_HANDLE 0
+#define BOOTLIBNAME "libvmjc.so"
 #endif
 
 #include "debug.h"
@@ -57,6 +59,8 @@
 ClassArray ArrayOfDouble;
 ClassArray ArrayOfLong;
 
+typedef void (*static_init_t)(JnjvmClassLoader*);
+
 JnjvmBootstrapLoader::JnjvmBootstrapLoader(bool staticCompilation) {
   
   TheModule = new JnjvmModule("Bootstrap JnJVM", staticCompilation);
@@ -83,7 +87,20 @@
   
   upcalls = new(allocator) Classpath();
   bootstrapLoader = this;
+  
 
+  // First, try to find if we have a pre-compiled rt.jar
+  void* handle = dlopen(BOOTLIBNAME, RTLD_LAZY | RTLD_GLOBAL);
+  if (handle) {
+    // Found it!
+    Class* ptr = (Class*)dlsym(handle, "java/lang/Object");
+    if (ptr) {
+      // We have the java/lang/Object class, execute the static initializer.
+      static_init_t init = (static_init_t)(uintptr_t)ptr->classLoader;
+      assert(init && "Loaded the wrong boot library");
+      init(this);
+    }
+  }
 
   // Create the name of char arrays.
   const UTF8* utf8OfChar = asciizConstructUTF8("[C");
@@ -947,3 +964,10 @@
 extern "C" void vmjcAddString(JnjvmClassLoader* JCL, JavaString* val) {
   JCL->strings.push_back(val);
 }
+
+extern "C" intptr_t vmjcLoadNative(UserClass* cl, const char* name) {
+  bool jnjvm = false;
+  intptr_t res = cl->classLoader->loadInLib(name, jnjvm);
+  assert(res && "Could not find required native method");
+  return res;
+}

Modified: vmkit/trunk/lib/JnJVM/VMCore/JnjvmModule.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/JnJVM/VMCore/JnjvmModule.cpp?rev=61697&r1=61696&r2=61697&view=diff

==============================================================================
--- vmkit/trunk/lib/JnJVM/VMCore/JnjvmModule.cpp (original)
+++ vmkit/trunk/lib/JnJVM/VMCore/JnjvmModule.cpp Mon Jan  5 10:49:43 2009
@@ -34,6 +34,7 @@
 llvm::Constant* JnjvmModule::PrimitiveArrayVT;
 llvm::Constant* JnjvmModule::ReferenceArrayVT;
 llvm::Function* JnjvmModule::StaticInitializer;
+llvm::Function* JnjvmModule::NativeLoader;
 
 extern void* JavaArrayVT[];
 extern void* ArrayObjectVT[];
@@ -375,26 +376,11 @@
     if (I == End) {
       
       LLVMSignatureInfo* LSI = getSignatureInfo(meth->getSignature());
-      const llvm::Type* valPtrType = LSI->getNativeType();
+      const llvm::Type* valPtrType = LSI->getNativePtrType();
       
-      const UTF8* jniConsClName = meth->classDef->name;
-      const UTF8* jniConsName = meth->name;
-      const UTF8* jniConsType = meth->type;
-      sint32 clen = jniConsClName->size;
-      sint32 mnlen = jniConsName->size;
-      sint32 mtlen = jniConsType->size;
-
-      char* buf = (char*)alloca(3 + JNI_NAME_PRE_LEN + 1 +
-                                ((mnlen + clen + mtlen) << 1));
-    
-      if (meth->classDef->isNativeOverloaded(meth))
-        meth->jniConsFromMethOverloaded(buf);
-      else
-        meth->jniConsFromMeth(buf);
-
       varGV = new GlobalVariable(valPtrType, true,
-                                 GlobalValue::ExternalLinkage,
-                                 0, buf, this);
+                                 GlobalValue::InternalLinkage,
+                                 Constant::getNullValue(valPtrType), "", this);
     
       nativeFunctions.insert(std::make_pair(meth, varGV));
       return varGV;
@@ -1908,6 +1894,15 @@
     StaticInitializer = Function::Create(FTy, GlobalValue::InternalLinkage,
                                          "Init", this);
 
+    llvmArgs.clear();
+    llvmArgs.push_back(JavaClassType);
+    llvmArgs.push_back(ptrType);
+    
+    FTy = FunctionType::get(ptrType, llvmArgs, false);
+  
+    NativeLoader = Function::Create(FTy, GlobalValue::ExternalLinkage,
+                                    "vmjcNativeLoader", this);
+
   } else {
     PrimitiveArrayVT = ConstantExpr::getIntToPtr(ConstantInt::get(Type::Int64Ty,
                                                          uint64(JavaArrayVT)),

Modified: vmkit/trunk/lib/JnJVM/VMCore/JnjvmModule.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/JnJVM/VMCore/JnjvmModule.h?rev=61697&r1=61696&r2=61697&view=diff

==============================================================================
--- vmkit/trunk/lib/JnJVM/VMCore/JnjvmModule.h (original)
+++ vmkit/trunk/lib/JnJVM/VMCore/JnjvmModule.h Mon Jan  5 10:49:43 2009
@@ -236,6 +236,8 @@
   static llvm::Function* StaticInitializer;
   
 public:
+  
+  static llvm::Function* NativeLoader;
 
   bool isStaticCompiling() {
     return staticCompilation;





More information about the vmkit-commits mailing list