[vmkit-commits] [vmkit] r58302 - in /vmkit/trunk: include/mvm/CompilationUnit.h include/mvm/VirtualMachine.h lib/JnJVM/VMCore/JavaInitialise.cpp lib/JnJVM/VMCore/Jnjvm.cpp lib/JnJVM/VMCore/Jnjvm.h lib/JnJVM/VMCore/JnjvmClassLoader.cpp lib/JnJVM/VMCore/JnjvmClassLoader.h lib/JnJVM/VMCore/JnjvmModule.cpp lib/JnJVM/VMCore/JnjvmModule.h lib/N3/VMCore/N3.h lib/N3/VMCore/N3Initialise.cpp lib/N3/VMCore/VirtualMachine.h tools/Makefile tools/vmjc/ tools/vmjc/Makefile tools/vmjc/vmjc.cpp
Nicolas Geoffray
nicolas.geoffray at lip6.fr
Mon Oct 27 16:56:29 PDT 2008
Author: geoffray
Date: Mon Oct 27 18:56:29 2008
New Revision: 58302
URL: http://llvm.org/viewvc/llvm-project?rev=58302&view=rev
Log:
New tool in VMKit, a static Java compiler!
Added:
vmkit/trunk/tools/vmjc/
vmkit/trunk/tools/vmjc/Makefile (with props)
vmkit/trunk/tools/vmjc/vmjc.cpp
Modified:
vmkit/trunk/include/mvm/CompilationUnit.h
vmkit/trunk/include/mvm/VirtualMachine.h
vmkit/trunk/lib/JnJVM/VMCore/JavaInitialise.cpp
vmkit/trunk/lib/JnJVM/VMCore/Jnjvm.cpp
vmkit/trunk/lib/JnJVM/VMCore/Jnjvm.h
vmkit/trunk/lib/JnJVM/VMCore/JnjvmClassLoader.cpp
vmkit/trunk/lib/JnJVM/VMCore/JnjvmClassLoader.h
vmkit/trunk/lib/JnJVM/VMCore/JnjvmModule.cpp
vmkit/trunk/lib/JnJVM/VMCore/JnjvmModule.h
vmkit/trunk/lib/N3/VMCore/N3.h
vmkit/trunk/lib/N3/VMCore/N3Initialise.cpp
vmkit/trunk/lib/N3/VMCore/VirtualMachine.h
vmkit/trunk/tools/Makefile
Modified: vmkit/trunk/include/mvm/CompilationUnit.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/include/mvm/CompilationUnit.h?rev=58302&r1=58301&r2=58302&view=diff
==============================================================================
--- vmkit/trunk/include/mvm/CompilationUnit.h (original)
+++ vmkit/trunk/include/mvm/CompilationUnit.h Mon Oct 27 18:56:29 2008
@@ -27,7 +27,6 @@
class CompilationUnit : public mvm::Object {
public:
llvm::Module* TheModule;
-
};
}
Modified: vmkit/trunk/include/mvm/VirtualMachine.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/include/mvm/VirtualMachine.h?rev=58302&r1=58301&r2=58302&view=diff
==============================================================================
--- vmkit/trunk/include/mvm/VirtualMachine.h (original)
+++ vmkit/trunk/include/mvm/VirtualMachine.h Mon Oct 27 18:56:29 2008
@@ -30,8 +30,12 @@
/// runApplication - Run an application. The application name is in
/// the arguments, hence it is the virtual machine's job to parse them.
virtual void runApplication(int argc, char** argv) = 0;
+
+ /// compile - Compile a given file to LLVM.
+ virtual void compile(const char* name) = 0;
+
- static CompilationUnit* initialiseJVM();
+ static CompilationUnit* initialiseJVM(bool staticCompilation = false);
static VirtualMachine* createJVM(CompilationUnit* C = 0);
static CompilationUnit* initialiseCLIVM();
Modified: vmkit/trunk/lib/JnJVM/VMCore/JavaInitialise.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/JnJVM/VMCore/JavaInitialise.cpp?rev=58302&r1=58301&r2=58302&view=diff
==============================================================================
--- vmkit/trunk/lib/JnJVM/VMCore/JavaInitialise.cpp (original)
+++ vmkit/trunk/lib/JnJVM/VMCore/JavaInitialise.cpp Mon Oct 27 18:56:29 2008
@@ -62,22 +62,23 @@
}
#ifdef ISOLATE_SHARING
-mvm::CompilationUnit* mvm::VirtualMachine::initialiseJVM() {
+mvm::CompilationUnit* mvm::VirtualMachine::initialiseJVM(bool sc) {
initialiseVT();
JnjvmSharedLoader::sharedLoader = JnjvmSharedLoader::createSharedLoader();
return JnjvmSharedLoader::sharedLoader;
}
mvm::VirtualMachine* mvm::VirtualMachine::createJVM(mvm::CompilationUnit* C) {
- JnjvmBootstraLoader* bootstrapLoader = gc_new(JnjvmBootstrapLoader)(0);
+ JnjvmBootstraLoader* bootstrapLoader = gc_new(JnjvmBootstrapLoader)(false);
Jnjvm* vm = gc_new(Jnjvm)(bootstrapLoader);
return vm;
}
#else
-mvm::CompilationUnit* mvm::VirtualMachine::initialiseJVM() {
+mvm::CompilationUnit*
+mvm::VirtualMachine::initialiseJVM(bool staticCompilation) {
initialiseVT();
- return gc_new(JnjvmBootstrapLoader)(0);
+ return gc_new(JnjvmBootstrapLoader)(staticCompilation);
}
mvm::VirtualMachine* mvm::VirtualMachine::createJVM(mvm::CompilationUnit* C) {
Modified: vmkit/trunk/lib/JnJVM/VMCore/Jnjvm.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/JnJVM/VMCore/Jnjvm.cpp?rev=58302&r1=58301&r2=58302&view=diff
==============================================================================
--- vmkit/trunk/lib/JnJVM/VMCore/Jnjvm.cpp (original)
+++ vmkit/trunk/lib/JnJVM/VMCore/Jnjvm.cpp Mon Oct 27 18:56:29 2008
@@ -135,9 +135,13 @@
vsnprintf(tmp, 4096, fmt, ap);
va_end(ap);
- JavaObject* obj = cl->doNew(this);
- init->invokeIntSpecial(this, cl, obj, asciizToStr(tmp));
- JavaThread::throwException(obj);
+ if (cl && !bootstrapLoader->getModule()->isStaticCompiling()) {
+ JavaObject* obj = cl->doNew(this);
+ init->invokeIntSpecial(this, cl, obj, asciizToStr(tmp));
+ JavaThread::throwException(obj);
+ } else {
+ throw std::string(tmp);
+ }
}
void Jnjvm::arrayStoreException() {
@@ -253,10 +257,10 @@
obj);
}
-void Jnjvm::noClassDefFoundError(const char* fmt, ...) {
+void Jnjvm::noClassDefFoundError(const UTF8* name) {
error(upcalls->NoClassDefFoundError,
upcalls->InitNoClassDefFoundError,
- fmt);
+ "Unable to load %s", name->UTF8ToAsciiz());
}
void Jnjvm::classNotFoundException(JavaString* str) {
@@ -824,3 +828,34 @@
}
return (const UTF8*)tmp;
}
+
+
+void Jnjvm::compile(const char* name) {
+ bootstrapLoader->analyseClasspathEnv(classpath);
+
+ mvm::Thread* oldThread = mvm::Thread::get();
+ JavaThread thread(0, this, oldThread->baseSP);
+ bootstrapThread = &thread;
+
+
+
+ const UTF8* utf8 = bootstrapLoader->asciizConstructUTF8(name);
+ UserClass* cl = bootstrapLoader->loadName(utf8, true, true);
+
+ for (uint32 i = 0; i < cl->nbVirtualMethods; ++i) {
+ JavaMethod& meth = cl->virtualMethods[i];
+ bootstrapLoader->TheModuleProvider->parseFunction(&meth);
+ }
+
+ for (uint32 i = 0; i < cl->nbStaticMethods; ++i) {
+ JavaMethod& meth = cl->staticMethods[i];
+ bootstrapLoader->TheModuleProvider->parseFunction(&meth);
+ }
+
+ llvm::Module* M = bootstrapLoader->getModule();
+ for (Module::iterator i = M->begin(), e = M->end(); i != e; ++i) {
+ i->setLinkage(llvm::GlobalValue::ExternalLinkage);
+ }
+
+ mvm::Thread::set(oldThread);
+}
Modified: vmkit/trunk/lib/JnJVM/VMCore/Jnjvm.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/JnJVM/VMCore/Jnjvm.h?rev=58302&r1=58301&r2=58302&view=diff
==============================================================================
--- vmkit/trunk/lib/JnJVM/VMCore/Jnjvm.h (original)
+++ vmkit/trunk/lib/JnJVM/VMCore/Jnjvm.h Mon Oct 27 18:56:29 2008
@@ -242,7 +242,7 @@
void noSuchMethodError(CommonClass* cl, const UTF8* name);
void classFormatError(const char* fmt, ...);
void noClassDefFoundError(JavaObject* obj);
- void noClassDefFoundError(const char* fmt, ...);
+ void noClassDefFoundError(const UTF8* name);
void classNotFoundException(JavaString* str);
/// asciizToStr - Constructs a java/lang/String object from the given asciiz.
@@ -296,6 +296,10 @@
///
virtual void runApplication(int argc, char** argv);
+ /// compile - Compile the .class, .zip or .jar file to LLVM IR.
+ ///
+ virtual void compile(const char* name);
+
};
} // end namespace jnjvm
Modified: vmkit/trunk/lib/JnJVM/VMCore/JnjvmClassLoader.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/JnJVM/VMCore/JnjvmClassLoader.cpp?rev=58302&r1=58301&r2=58302&view=diff
==============================================================================
--- vmkit/trunk/lib/JnJVM/VMCore/JnjvmClassLoader.cpp (original)
+++ vmkit/trunk/lib/JnJVM/VMCore/JnjvmClassLoader.cpp Mon Oct 27 18:56:29 2008
@@ -40,11 +40,12 @@
extern const char* GNUClasspathGlibj;
extern const char* GNUClasspathLibs;
-JnjvmBootstrapLoader::JnjvmBootstrapLoader(uint32 memLimit) {
+JnjvmBootstrapLoader::JnjvmBootstrapLoader(bool staticCompilation) {
JnjvmModule::initialise();
TheModule = new JnjvmModule("Bootstrap JnJVM");
TheModuleProvider = new JnjvmModuleProvider(getModule());
+ getModule()->setIsStaticCompiling(staticCompilation);
hashUTF8 = new(allocator) UTF8Map(allocator, 0);
classes = new(allocator) ClassMap();
@@ -317,7 +318,7 @@
if (name->equals(bootstrapLoader->NoClassDefFoundError)) {
vm->unknownError("Unable to load NoClassDefFoundError");
}
- vm->noClassDefFoundError("unable to load %s", name->printString());
+ vm->noClassDefFoundError(name);
}
if (cl && doResolve) cl->resolveClass();
Modified: vmkit/trunk/lib/JnJVM/VMCore/JnjvmClassLoader.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/JnJVM/VMCore/JnjvmClassLoader.h?rev=58302&r1=58301&r2=58302&view=diff
==============================================================================
--- vmkit/trunk/lib/JnJVM/VMCore/JnjvmClassLoader.h (original)
+++ vmkit/trunk/lib/JnJVM/VMCore/JnjvmClassLoader.h Mon Oct 27 18:56:29 2008
@@ -285,7 +285,7 @@
/// createBootstrapLoader - Creates the bootstrap loader, first thing
/// to do before any execution of a JVM.
///
- JnjvmBootstrapLoader(uint32 memLimit);
+ JnjvmBootstrapLoader(bool staticCompilation);
JnjvmBootstrapLoader() {}
virtual JavaString* UTF8ToStr(const UTF8* utf8);
Modified: vmkit/trunk/lib/JnJVM/VMCore/JnjvmModule.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/JnJVM/VMCore/JnjvmModule.cpp?rev=58302&r1=58301&r2=58302&view=diff
==============================================================================
--- vmkit/trunk/lib/JnJVM/VMCore/JnjvmModule.cpp (original)
+++ vmkit/trunk/lib/JnJVM/VMCore/JnjvmModule.cpp Mon Oct 27 18:56:29 2008
@@ -444,13 +444,15 @@
field.ptrOffset = sl->getElementOffset(i + 1);
}
- JnjvmModule* Mod = classDef->classLoader->getModule();
- VirtualTable* VT = Mod->makeVT((Class*)classDef, false);
-
uint64 size = mvm::MvmModule::getTypeSize(structType);
classDef->virtualSize = (uint32)size;
- classDef->virtualVT = VT;
virtualSizeConstant = ConstantInt::get(Type::Int32Ty, size);
+
+ JnjvmModule* Mod = classDef->classLoader->getModule();
+ if (!Mod->isStaticCompiling()) {
+ classDef->virtualVT = Mod->makeVT((Class*)classDef, false);
+ }
+
}
@@ -483,13 +485,13 @@
field.ptrOffset = sl->getElementOffset(i + 1);
}
-
- JnjvmModule* Mod = cl->classLoader->getModule();
- VirtualTable* VT = Mod->makeVT((Class*)classDef, true);
-
uint64 size = mvm::MvmModule::getTypeSize(structType);
cl->staticSize = size;
- cl->staticVT = VT;
+
+ JnjvmModule* Mod = cl->classLoader->getModule();
+ if (!Mod->isStaticCompiling()) {
+ cl->staticVT = Mod->makeVT((Class*)classDef, true);
+ }
}
return staticType;
}
Modified: vmkit/trunk/lib/JnJVM/VMCore/JnjvmModule.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/JnJVM/VMCore/JnjvmModule.h?rev=58302&r1=58301&r2=58302&view=diff
==============================================================================
--- vmkit/trunk/lib/JnJVM/VMCore/JnjvmModule.h (original)
+++ vmkit/trunk/lib/JnJVM/VMCore/JnjvmModule.h Mon Oct 27 18:56:29 2008
@@ -222,6 +222,10 @@
return staticCompilation;
}
+ void setIsStaticCompiling(bool sc) {
+ staticCompilation = sc;
+ }
+
static llvm::ConstantInt* JavaArraySizeOffsetConstant;
static llvm::ConstantInt* JavaArrayElementsOffsetConstant;
static llvm::ConstantInt* JavaObjectLockOffsetConstant;
Modified: vmkit/trunk/lib/N3/VMCore/N3.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/N3/VMCore/N3.h?rev=58302&r1=58301&r2=58302&view=diff
==============================================================================
--- vmkit/trunk/lib/N3/VMCore/N3.h (original)
+++ vmkit/trunk/lib/N3/VMCore/N3.h Mon Oct 27 18:56:29 2008
@@ -79,7 +79,7 @@
static const UTF8* floatName;
static const UTF8* doubleName;
static const UTF8* testInfinity;
-
+
};
} // end namespace n3
Modified: vmkit/trunk/lib/N3/VMCore/N3Initialise.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/N3/VMCore/N3Initialise.cpp?rev=58302&r1=58301&r2=58302&view=diff
==============================================================================
--- vmkit/trunk/lib/N3/VMCore/N3Initialise.cpp (original)
+++ vmkit/trunk/lib/N3/VMCore/N3Initialise.cpp Mon Oct 27 18:56:29 2008
@@ -341,6 +341,10 @@
((N3*)this)->runMain(argc, argv);
}
+void VirtualMachine::compile(const char* argv) {
+ assert(0 && "This virtual machine does not perform static compilation yet!\n");
+}
+
mvm::VirtualMachine* mvm::VirtualMachine::createCLIVM(mvm::CompilationUnit* C) {
N3* vm = N3::allocate("", N3::bootstrapVM);
return vm;
Modified: vmkit/trunk/lib/N3/VMCore/VirtualMachine.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/N3/VMCore/VirtualMachine.h?rev=58302&r1=58301&r2=58302&view=diff
==============================================================================
--- vmkit/trunk/lib/N3/VMCore/VirtualMachine.h (original)
+++ vmkit/trunk/lib/N3/VMCore/VirtualMachine.h Mon Oct 27 18:56:29 2008
@@ -127,6 +127,7 @@
VMThread* bootstrapThread;
virtual void runApplication(int argc, char** argv);
+ virtual void compile(const char* name);
};
Modified: vmkit/trunk/tools/Makefile
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/tools/Makefile?rev=58302&r1=58301&r2=58302&view=diff
==============================================================================
--- vmkit/trunk/tools/Makefile (original)
+++ vmkit/trunk/tools/Makefile Mon Oct 27 18:56:29 2008
@@ -8,7 +8,7 @@
##===----------------------------------------------------------------------===##
LEVEL = ..
-PARALLEL_DIRS = vmkit
+PARALLEL_DIRS = vmkit vmjc
include $(LEVEL)/Makefile.config
Added: vmkit/trunk/tools/vmjc/Makefile
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/tools/vmjc/Makefile?rev=58302&view=auto
==============================================================================
--- vmkit/trunk/tools/vmjc/Makefile (added)
+++ vmkit/trunk/tools/vmjc/Makefile Mon Oct 27 18:56:29 2008
@@ -0,0 +1,21 @@
+##===- tools/vmjc/Makefile --------------------------------*- Makefile -*-===##
+#
+# The VMKit project
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+LEVEL = ../..
+
+include $(LEVEL)/Makefile.config
+
+TOOLNAME = vmjc
+USEDLIBS = Allocator CommonThread Mvm JnJVM Classpath $(GCLIB)
+LINK_COMPONENTS = jit nativecodegen scalaropts instrumentation ipa ipo
+
+ifeq ($(ISOLATE_BUILD), 1)
+ USEDLIBS += Isolate
+endif
+
+include $(LEVEL)/Makefile.common
Propchange: vmkit/trunk/tools/vmjc/Makefile
------------------------------------------------------------------------------
svn:executable = *
Added: vmkit/trunk/tools/vmjc/vmjc.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/tools/vmjc/vmjc.cpp?rev=58302&view=auto
==============================================================================
--- vmkit/trunk/tools/vmjc/vmjc.cpp (added)
+++ vmkit/trunk/tools/vmjc/vmjc.cpp Mon Oct 27 18:56:29 2008
@@ -0,0 +1,145 @@
+//===----------------- vmjc.cpp - Java static compiler --------------------===//
+//
+// The VMKit project
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This utility may be invoked in the following manner:
+// vmjc [options] x.bc - Read Java bytecode from the x.class file, write llvm
+// asm to the x.ll file.
+// Options:
+// --help - Output information about command line switches
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Module.h"
+#include "llvm/PassManager.h"
+#include "llvm/Bitcode/ReaderWriter.h"
+#include "llvm/Assembly/PrintModulePass.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/ManagedStatic.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Streams.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/System/Signals.h"
+
+#include "MvmGC.h"
+#include "mvm/JIT.h"
+#include "mvm/Object.h"
+#include "mvm/VirtualMachine.h"
+#include "mvm/Threads/Thread.h"
+
+#include <iostream>
+#include <fstream>
+#include <memory>
+
+using namespace llvm;
+
+static cl::opt<std::string>
+InputFilename(cl::Positional, cl::desc("<input Java bytecode>"), cl::init("-"));
+
+static cl::opt<std::string>
+OutputFilename("o", cl::desc("Override output filename"),
+ cl::value_desc("filename"));
+
+static cl::opt<bool>
+Force("f", cl::desc("Overwrite output files"));
+
+static cl::opt<bool>
+DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
+
+int main(int argc, char **argv) {
+ llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
+ int base;
+ try {
+ cl::ParseCommandLineOptions(argc, argv, "vmkit .class -> .ll compiler\n");
+ sys::PrintStackTraceOnErrorSignal();
+
+ std::ostream *Out = &std::cout; // Default to printing to stdout.
+ std::string ErrorMessage;
+
+
+ if (InputFilename == "-") {
+ cl::PrintHelpMessage();
+ return 0;
+ }
+
+ mvm::MvmModule::initialise();
+ mvm::Object::initialise();
+ mvm::Thread::initialise();
+ Collector::initialise(0, &base);
+
+ mvm::CompilationUnit* CU = mvm::VirtualMachine::initialiseJVM(true);
+ mvm::VirtualMachine* vm = mvm::VirtualMachine::createJVM(CU);
+ vm->compile(InputFilename.c_str());
+
+
+ if (DontPrint) {
+ // Just use stdout. We won't actually print anything on it.
+ } else if (OutputFilename != "") { // Specified an output filename?
+ if (OutputFilename != "-") { // Not stdout?
+ if (!Force && std::ifstream(OutputFilename.c_str())) {
+ // If force is not specified, make sure not to overwrite a file!
+ cerr << argv[0] << ": error opening '" << OutputFilename
+ << "': file exists! Sending to standard output.\n";
+ } else {
+ Out = new std::ofstream(OutputFilename.c_str());
+ }
+ }
+ } else {
+ if (InputFilename == "-") {
+ OutputFilename = "-";
+ } else {
+ std::string IFN = InputFilename;
+ int Len = IFN.length();
+ if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
+ // Source ends in .bc
+ OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
+ } else {
+ OutputFilename = IFN+".ll";
+ }
+
+ if (!Force && std::ifstream(OutputFilename.c_str())) {
+ // If force is not specified, make sure not to overwrite a file!
+ cerr << argv[0] << ": error opening '" << OutputFilename
+ << "': file exists! Sending to standard output.\n";
+ } else {
+ Out = new std::ofstream(OutputFilename.c_str());
+
+ // Make sure that the Out file gets unlinked from the disk if we get a
+ // SIGINT
+ sys::RemoveFileOnSignal(sys::Path(OutputFilename));
+ }
+ }
+ }
+
+ if (!Out->good()) {
+ cerr << argv[0] << ": error opening " << OutputFilename
+ << ": sending to stdout instead!\n";
+ Out = &std::cout;
+ }
+
+ // All that llvm-dis does is write the assembly to a file.
+ if (!DontPrint) {
+ PassManager Passes;
+ raw_os_ostream L(*Out);
+ Passes.add(createPrintModulePass(&L));
+ Passes.run(*CU->TheModule);
+ }
+
+ if (Out != &std::cout) {
+ ((std::ofstream*)Out)->close();
+ delete Out;
+ }
+ return 0;
+ } catch (const std::string& msg) {
+ cerr << argv[0] << ": " << msg << "\n";
+ } catch (...) {
+ cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
+ }
+ return 1;
+}
+
More information about the vmkit-commits
mailing list