[vmkit-commits] [vmkit] r197338 - create a usefull compilation unit class

Gael Thomas gael.thomas at lip6.fr
Sun Dec 15 03:19:31 PST 2013


Author: gthomas
Date: Sun Dec 15 05:19:30 2013
New Revision: 197338

URL: http://llvm.org/viewvc/llvm-project?rev=197338&view=rev
Log:
create a usefull compilation unit class

Modified:
    vmkit/branches/mcjit/include/j3/j3codegen.h
    vmkit/branches/mcjit/include/vmkit/compiler.h
    vmkit/branches/mcjit/lib/vmkit/compiler.cc

Modified: vmkit/branches/mcjit/include/j3/j3codegen.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/include/j3/j3codegen.h?rev=197338&r1=197337&r2=197338&view=diff
==============================================================================
--- vmkit/branches/mcjit/include/j3/j3codegen.h (original)
+++ vmkit/branches/mcjit/include/j3/j3codegen.h Sun Dec 15 05:19:30 2013
@@ -96,7 +96,7 @@ namespace j3 {
 
 		bool                   isWide;
 
-		llvm::Module*      module();
+		llvm::Module*      module() { return _module; }
 
 		uint32_t           wideReadU1();
 		uint32_t           wideReadS1();

Modified: vmkit/branches/mcjit/include/vmkit/compiler.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/include/vmkit/compiler.h?rev=197338&r1=197337&r2=197338&view=diff
==============================================================================
--- vmkit/branches/mcjit/include/vmkit/compiler.h (original)
+++ vmkit/branches/mcjit/include/vmkit/compiler.h Sun Dec 15 05:19:30 2013
@@ -41,12 +41,25 @@ namespace vmkit {
 	class CompilationUnit  : public llvm::SectionMemoryManager {
 		typedef std::map<const char*, Symbol*, Util::char_less_t, StdAllocator<std::pair<const char*, Symbol*> > > SymbolMap;
 
+		BumpAllocator*          _allocator;
 		SymbolMap               _symbolTable;
 		pthread_mutex_t         _mutexSymbolTable;
 		llvm::ExecutionEngine*  _ee;
 		llvm::ExecutionEngine*  _oldee;
 
+		void  operator delete(void* self);
 	public:
+		void* operator new(size_t n, BumpAllocator* allocator);
+
+		CompilationUnit(BumpAllocator* allocator, const char* id);
+		~CompilationUnit();
+
+		static void destroy(CompilationUnit* unit);
+
+		void                    addSymbol(const char* id, vmkit::Symbol* symbol);
+		uint64_t                getSymbolAddress(const std::string &Name);
+
+		BumpAllocator*          allocator() { return _allocator; }
 		llvm::ExecutionEngine*  ee() { return _ee; }
 		llvm::ExecutionEngine*  oldee() { return _oldee; }
 	};

Modified: vmkit/branches/mcjit/lib/vmkit/compiler.cc
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/lib/vmkit/compiler.cc?rev=197338&r1=197337&r2=197338&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/vmkit/compiler.cc (original)
+++ vmkit/branches/mcjit/lib/vmkit/compiler.cc Sun Dec 15 05:19:30 2013
@@ -1,9 +1,85 @@
+#include <dlfcn.h>
+
 #include "vmkit/compiler.h"
 #include "vmkit/thread.h"
 #include "vmkit/vmkit.h"
 
+#include "llvm/ExecutionEngine/ExecutionEngine.h"
+#include "llvm/IR/Module.h"
+
 using namespace vmkit;
 
 uint8_t* Symbol::getSymbolAddress() {
 	Thread::get()->vm()->internalError(L"implement me: getSymbolAddress");
 }
+
+void* CompilationUnit::operator new(size_t n, BumpAllocator* allocator) {
+	return allocator->allocate(n);
+}
+
+void  CompilationUnit::operator delete(void* self) {
+}
+
+CompilationUnit::CompilationUnit(BumpAllocator* allocator, const char* id) :
+	_symbolTable(vmkit::Util::char_less, allocator) {
+	_allocator = allocator;
+	pthread_mutex_init(&_mutexSymbolTable, 0);
+
+	std::string err;
+	_ee = llvm::EngineBuilder(new llvm::Module(id, Thread::get()->vm()->llvmContext()))
+		.setUseMCJIT(1)
+		.setMCJITMemoryManager(this)
+		.setErrorStr(&err)
+		.create();
+
+	if (!ee())
+		Thread::get()->vm()->internalError(L"Error while creating execution engine: %s\n", err.c_str());
+
+  ee()->RegisterJITEventListener(Thread::get()->vm());
+
+	_oldee = llvm::EngineBuilder(new llvm::Module("old ee", Thread::get()->vm()->llvmContext()))
+		.setErrorStr(&err)
+		.create();
+
+	if (!oldee())
+		Thread::get()->vm()->internalError(L"Error while creating execution engine: %s\n", err.c_str());
+
+	oldee()->DisableLazyCompilation(0);
+}
+
+CompilationUnit::~CompilationUnit() {
+}
+
+void CompilationUnit::destroy(CompilationUnit* unit) {
+	delete unit;
+	BumpAllocator::destroy(unit->allocator());
+}
+
+void CompilationUnit::addSymbol(const char* id, vmkit::Symbol* symbol) {
+	pthread_mutex_lock(&_mutexSymbolTable);
+	_symbolTable[id] = symbol;
+	pthread_mutex_unlock(&_mutexSymbolTable);
+}
+
+uint64_t CompilationUnit::getSymbolAddress(const std::string &Name) {
+	pthread_mutex_lock(&_mutexSymbolTable);
+	const char* id = Name.c_str() + 1;
+
+	std::map<const char*, vmkit::Symbol*>::iterator it = _symbolTable.find(id);
+	vmkit::Symbol* res;
+
+	if(it == _symbolTable.end()) {
+		uint8_t* addr = (uint8_t*)dlsym(RTLD_SELF, id);
+		if(!addr)
+			Thread::get()->vm()->internalError(L"unable to resolve native symbol: %s", id);
+		res = new(allocator()) vmkit::NativeSymbol(addr);
+		size_t len = strlen(id);
+		char* buf = (char*)allocator()->allocate(len+1);
+		memcpy(buf, id, len+1);
+		_symbolTable[buf] = res;
+	} else
+		res = it->second;
+
+	pthread_mutex_unlock(&_mutexSymbolTable);
+	return (uint64_t)(uintptr_t)res->getSymbolAddress();
+}





More information about the vmkit-commits mailing list