[vmkit-commits] [vmkit] r197816 - register the safepoints in vmkit

Gael Thomas gael.thomas at lip6.fr
Fri Dec 20 08:49:17 PST 2013


Author: gthomas
Date: Fri Dec 20 10:49:17 2013
New Revision: 197816

URL: http://llvm.org/viewvc/llvm-project?rev=197816&view=rev
Log:
register the safepoints in vmkit

Modified:
    vmkit/branches/mcjit/include/j3/j3classloader.h
    vmkit/branches/mcjit/include/vmkit/compiler.h
    vmkit/branches/mcjit/include/vmkit/safepoint.h
    vmkit/branches/mcjit/include/vmkit/vmkit.h
    vmkit/branches/mcjit/lib/j3/vm/j3classloader.cc
    vmkit/branches/mcjit/lib/j3/vm/j3method.cc
    vmkit/branches/mcjit/lib/vmkit/compiler.cc
    vmkit/branches/mcjit/lib/vmkit/safpoint.cc
    vmkit/branches/mcjit/lib/vmkit/vmkit.cc

Modified: vmkit/branches/mcjit/include/j3/j3classloader.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/include/j3/j3classloader.h?rev=197816&r1=197815&r2=197816&view=diff
==============================================================================
--- vmkit/branches/mcjit/include/j3/j3classloader.h (original)
+++ vmkit/branches/mcjit/include/j3/j3classloader.h Fri Dec 20 10:49:17 2013
@@ -40,7 +40,6 @@ namespace j3 {
 		J3ObjectHandle*                      _javaClassLoader;
 		J3FixedPoint                         _fixedPoint;
 		pthread_mutex_t                      _mutex;       /* a lock */
-		J3*                                  _vm;          /* my vm */
 		vmkit::NameMap<J3Class*>::map        classes;      /* classes managed by this class loader */
 		vmkit::NameMap<J3Type*>::map         types;        /* shortcut to find types */
 		vmkit::NameMap<J3MethodType*>::map   methodTypes;  /* shortcut to find method types - REMOVE */
@@ -65,7 +64,7 @@ namespace j3 {
 		void                          lock() { pthread_mutex_lock(&_mutex); }
 		void                          unlock() { pthread_mutex_unlock(&_mutex); }
 
-		J3*                           vm() const { return _vm; };
+		J3*                           vm() const { return (J3*)vmkit::CompilationUnit::vm(); };
 
 		J3Method*                     method(uint16_t access, J3Class* cl, 
 																				 const vmkit::Name* name, const vmkit::Name* sign); /* find a method ref */

Modified: vmkit/branches/mcjit/include/vmkit/compiler.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/include/vmkit/compiler.h?rev=197816&r1=197815&r2=197816&view=diff
==============================================================================
--- vmkit/branches/mcjit/include/vmkit/compiler.h (original)
+++ vmkit/branches/mcjit/include/vmkit/compiler.h Fri Dec 20 10:49:17 2013
@@ -18,6 +18,8 @@ namespace llvm {
 };
 
 namespace vmkit {
+	class VMKit;
+
 	class Symbol : public PermanentObject {
 	public:
 		virtual uint8_t* getSymbolAddress();
@@ -34,6 +36,7 @@ namespace vmkit {
 	class CompilationUnit  : public llvm::SectionMemoryManager {
 		typedef std::map<const char*, Symbol*, Util::char_less_t, StdAllocator<std::pair<const char*, Symbol*> > > SymbolMap;
 
+		VMKit*                  _vmkit;
 		BumpAllocator*          _allocator;
 		SymbolMap               _symbolTable;
 		pthread_mutex_t         _mutexSymbolTable;
@@ -47,11 +50,13 @@ namespace vmkit {
 	public:
 		void* operator new(size_t n, BumpAllocator* allocator);
 
-		CompilationUnit(BumpAllocator* allocator, const char* id);
+		CompilationUnit(BumpAllocator* allocator, VMKit* vmkit, const char* id);
 		~CompilationUnit();
 
 		static void destroy(CompilationUnit* unit);
 
+		VMKit*                  vm() const { return _vmkit; }
+
 		void                    addSymbol(const char* id, vmkit::Symbol* symbol);
 		uint64_t                getSymbolAddress(const std::string &Name);
 
@@ -59,7 +64,7 @@ namespace vmkit {
 		llvm::ExecutionEngine*  ee() { return _ee; }
 		llvm::ExecutionEngine*  oldee() { return _oldee; }
 
-		void                    addModule(llvm::Module* module);
+		void                    compileModule(llvm::Module* module);
 	};
 }
 

Modified: vmkit/branches/mcjit/include/vmkit/safepoint.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/include/vmkit/safepoint.h?rev=197816&r1=197815&r2=197816&view=diff
==============================================================================
--- vmkit/branches/mcjit/include/vmkit/safepoint.h (original)
+++ vmkit/branches/mcjit/include/vmkit/safepoint.h Fri Dec 20 10:49:17 2013
@@ -3,23 +3,33 @@
 
 #include <stdint.h>
 
+namespace llvm {
+	class Module;
+}
+
 namespace vmkit {
+	class CompilationUnit;
+
 	class Safepoint { /* directly in the data sections */
-		void*    _addr;
-		void*    _metaData;
-		uint32_t _sourceIndex;
-		uint32_t _nbLives;
-		uint32_t _lives[2];
+		uintptr_t        _addr;
+		void*            _code;
+		CompilationUnit* _unit;
+		uint32_t         _sourceIndex;
+		uint32_t         _nbLives;
 
 	public:
-		void*    addr() { return _addr; }
-		void*    metaData() { return _metaData; }
-		void     updateMetaData(void* metaData) { _metaData = metaData; }
-		uint32_t sourceIndex() { return _sourceIndex; }
-		uint32_t nbLives() { return _nbLives; }
-		uint32_t liveAt(uint32_t i) { return _lives[i]; }
+		void       setUnit(CompilationUnit* unit) { _unit = unit; }
+
+		uintptr_t  addr() { return _addr; }
+		void*      code() { return _code; }
+		uint32_t   sourceIndex() { return _sourceIndex; }
+		uint32_t   nbLives() { return _nbLives; }
+		uint32_t   liveAt(uint32_t i) { return ((uint32_t*)(this + 1))[i]; }
 
 		Safepoint* getNext();
+		void       dump();
+
+		static Safepoint* get(CompilationUnit* unit, llvm::Module* module);
 	};
 }
 

Modified: vmkit/branches/mcjit/include/vmkit/vmkit.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/include/vmkit/vmkit.h?rev=197816&r1=197815&r2=197816&view=diff
==============================================================================
--- vmkit/branches/mcjit/include/vmkit/vmkit.h (original)
+++ vmkit/branches/mcjit/include/vmkit/vmkit.h Fri Dec 20 10:49:17 2013
@@ -31,8 +31,9 @@ namespace vmkit {
 		typedef std::map<const char*, llvm::GlobalValue*, Util::char_less_t, 
 										 StdAllocator<std::pair<const char*, llvm::GlobalValue*> > > MangleMap;
 
-		std::map<uintptr_t, Safepoint*>            safepointMap;   /* managed with malloc/free */
-		std::map<uintptr_t, ExceptionDescriptor*>  exceptionTable; /* managed with malloc/free */
+		std::map<uintptr_t, Safepoint*>            safepointMap;    
+		pthread_mutex_t                            safepointMapLock;
+		std::map<uintptr_t, ExceptionDescriptor*>  exceptionTable;   /* managed with malloc/free */
 		MangleMap                       mangleMap;
 		BumpAllocator*                  _allocator;
 		llvm::Module*                   _self;
@@ -50,6 +51,9 @@ namespace vmkit {
 
 		VMKit(BumpAllocator* allocator);
 
+		void                       addSafepoint(Safepoint* sf);
+		Safepoint*                 getSafepoint(uintptr_t addr);
+
 		BumpAllocator*             allocator() { return _allocator; }
 
 		void                       vmkitBootstrap(Thread* initialThread, const char* selfBitCodePath);

Modified: vmkit/branches/mcjit/lib/j3/vm/j3classloader.cc
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/lib/j3/vm/j3classloader.cc?rev=197816&r1=197815&r2=197816&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/j3/vm/j3classloader.cc (original)
+++ vmkit/branches/mcjit/lib/j3/vm/j3classloader.cc Fri Dec 20 10:49:17 2013
@@ -21,7 +21,7 @@ using namespace j3;
 J3ClassLoader::J3MethodLess J3ClassLoader::j3MethodLess;
 
 J3ClassLoader::J3ClassLoader(J3* v, J3ObjectHandle* javaClassLoader, vmkit::BumpAllocator* allocator) 
-	: CompilationUnit(allocator, "class-loader"),
+	: CompilationUnit(allocator, v, "class-loader"),
 		_fixedPoint(allocator),
 		classes(vmkit::Name::less, allocator),
 		types(vmkit::Name::less, allocator),
@@ -34,8 +34,6 @@ J3ClassLoader::J3ClassLoader(J3* v, J3Ob
 	//	pthread_mutexattr_init(&attr);
 	//	pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
 	pthread_mutex_init(&_mutex, 0);//&attr);
-
-	_vm = v;
 }
 
 void* J3ClassLoader::lookupNativeFunctionPointer(J3Method* method, const char* symbol) {

Modified: vmkit/branches/mcjit/lib/j3/vm/j3method.cc
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/lib/j3/vm/j3method.cc?rev=197816&r1=197815&r2=197816&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/j3/vm/j3method.cc (original)
+++ vmkit/branches/mcjit/lib/j3/vm/j3method.cc Fri Dec 20 10:49:17 2013
@@ -61,29 +61,10 @@ uint8_t* J3Method::fnPtr() {
 
 		J3CodeGen::translate(this, _llvmFunction);
 
-		llvm::ExecutionEngine* ee = cl()->loader()->ee();
-		cl()->loader()->addModule(module);
-		_fnPtr = (uint8_t*)ee->getFunctionAddress(_llvmFunction->getName().data());
+		cl()->loader()->compileModule(module);
 
-#if 1
-		fprintf(stderr, "%s is generated at %p\n", llvmFunctionName(), _fnPtr);
-		llvm::SmallString<256> symName;
-		symName += module->getModuleIdentifier();
-		symName += "__frametable";
-		vmkit::Safepoint* sf = (vmkit::Safepoint*)ee->getGlobalValueAddress(symName.c_str());
-
-		if(!sf)
-			cl()->loader()->vm()->internalError(L"unable to find safepoints");
-		
-		while(sf->addr()) {
-			fprintf(stderr, "  [%p] safepoint at %p for function %p::%d\n", sf, sf->addr(), sf->metaData(), sf->sourceIndex());
-			for(uint32_t i=0; i<sf->nbLives(); i++)
-				fprintf(stderr, "    live at %d\n", sf->liveAt(i));
-
-			sf = sf->getNext();
-		}
-#endif
-	}
+		_fnPtr = (uint8_t*)cl()->loader()->ee()->getFunctionAddress(_llvmFunction->getName().data());
+ 	}
 
 	return _fnPtr;
 }

Modified: vmkit/branches/mcjit/lib/vmkit/compiler.cc
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/lib/vmkit/compiler.cc?rev=197816&r1=197815&r2=197816&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/vmkit/compiler.cc (original)
+++ vmkit/branches/mcjit/lib/vmkit/compiler.cc Fri Dec 20 10:49:17 2013
@@ -3,6 +3,7 @@
 #include "vmkit/compiler.h"
 #include "vmkit/thread.h"
 #include "vmkit/vmkit.h"
+#include "vmkit/safepoint.h"
 
 #include "llvm/LinkAllPasses.h"
 #include "llvm/PassManager.h"
@@ -24,11 +25,13 @@ void* CompilationUnit::operator new(size
 void  CompilationUnit::operator delete(void* self) {
 }
 
-CompilationUnit::CompilationUnit(BumpAllocator* allocator, const char* id) :
+CompilationUnit::CompilationUnit(BumpAllocator* allocator, VMKit* vmkit, const char* id) :
 	_symbolTable(vmkit::Util::char_less, allocator) {
 	_allocator = allocator;
 	pthread_mutex_init(&_mutexSymbolTable, 0);
 
+	_vmkit = vmkit;
+
 	std::string err;
 	_ee = llvm::EngineBuilder(new llvm::Module(id, Thread::get()->vm()->llvmContext()))
 		.setUseMCJIT(1)
@@ -40,6 +43,7 @@ CompilationUnit::CompilationUnit(BumpAll
 		Thread::get()->vm()->internalError(L"Error while creating execution engine: %s\n", err.c_str());
 
   ee()->RegisterJITEventListener(Thread::get()->vm());
+	ee()->finalizeObject();
 
 	_oldee = llvm::EngineBuilder(new llvm::Module("old ee", Thread::get()->vm()->llvmContext()))
 		.setErrorStr(&err)
@@ -127,7 +131,20 @@ uint64_t CompilationUnit::getSymbolAddre
 	return (uint64_t)(uintptr_t)res->getSymbolAddress();
 }
 
-void CompilationUnit::addModule(llvm::Module* module) {
+void CompilationUnit::compileModule(llvm::Module* module) {
 	pm->run(*module);
 	ee()->addModule(module);
+	ee()->finalizeObject();
+
+	vmkit::Safepoint* sf = Safepoint::get(this, module);
+
+	if(!sf)
+		vm()->internalError(L"unable to find safepoints");
+		
+	while(sf->addr()) {
+		sf->setUnit(this);
+		vm()->addSafepoint(sf);
+		vm()->getSafepoint(sf->addr())->dump();
+		sf = sf->getNext();
+	}
 }

Modified: vmkit/branches/mcjit/lib/vmkit/safpoint.cc
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/lib/vmkit/safpoint.cc?rev=197816&r1=197815&r2=197816&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/vmkit/safpoint.cc (original)
+++ vmkit/branches/mcjit/lib/vmkit/safpoint.cc Fri Dec 20 10:49:17 2013
@@ -1,4 +1,5 @@
 #include "vmkit/safepoint.h"
+#include "vmkit/compiler.h"
 
 #include "llvm/IR/Function.h"
 #include "llvm/IR/Constants.h"
@@ -26,6 +27,8 @@
 #include "llvm/Target/Mangler.h"
 #include "llvm/Target/TargetLoweringObjectFile.h"
 
+#include "llvm/ExecutionEngine/ExecutionEngine.h"
+
 namespace vmkit {
 	class VmkitGCPass : public llvm::GCStrategy {
 	public:
@@ -94,6 +97,7 @@ void VmkitGCMetadataPrinter::finishAssem
 
 	unsigned IntPtrSize = AP.TM.getDataLayout()->getPointerSize(0);
 		
+	fprintf(stderr, "generating frame tables for: %s\n", getModule().getModuleIdentifier().c_str());
 	llvm::SmallString<256> symName("_");
 	symName += getModule().getModuleIdentifier();
 	symName += "__frametable";
@@ -125,6 +129,9 @@ void VmkitGCMetadataPrinter::finishAssem
 
 				AP.OutStreamer.EmitValue(address, IntPtrSize);
 				AP.EmitGlobalConstant(&gcInfo->getFunction());
+				AP.EmitInt32(0);
+				if(IntPtrSize == 8)
+					AP.EmitInt32(0);
 				AP.EmitInt32(debug->getLine());
 				AP.EmitInt32(gcInfo->live_size(safepoint));
 
@@ -144,6 +151,19 @@ void VmkitGCMetadataPrinter::finishAssem
  *    Safepoint
  */
 Safepoint* Safepoint::getNext() {
-	uintptr_t next = (uintptr_t)this + sizeof(Safepoint) - 2*sizeof(uint32_t) + nbLives()*sizeof(uint32_t);
+	uintptr_t next = (uintptr_t)this + sizeof(Safepoint) + nbLives()*sizeof(uint32_t);
 	return (Safepoint*)(((next - 1) & -sizeof(uintptr_t)) + sizeof(uintptr_t));
 }
+
+Safepoint* Safepoint::get(CompilationUnit* unit, llvm::Module* module) {
+	llvm::SmallString<256> symName;
+	symName += module->getModuleIdentifier();
+	symName += "__frametable";
+	return (vmkit::Safepoint*)unit->ee()->getGlobalValueAddress(symName.c_str());
+}
+
+void Safepoint::dump() {
+	fprintf(stderr, "  [%p] safepoint at 0x%lx for function %p::%d\n", this, addr(), code(), sourceIndex());
+	for(uint32_t i=0; i<nbLives(); i++)
+		fprintf(stderr, "    live at %d\n", liveAt(i));
+}

Modified: vmkit/branches/mcjit/lib/vmkit/vmkit.cc
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/lib/vmkit/vmkit.cc?rev=197816&r1=197815&r2=197816&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/vmkit/vmkit.cc (original)
+++ vmkit/branches/mcjit/lib/vmkit/vmkit.cc Fri Dec 20 10:49:17 2013
@@ -7,6 +7,7 @@
 
 #include "vmkit/vmkit.h"
 #include "vmkit/thread.h"
+#include "vmkit/safepoint.h"
 
 #include "llvm/IR/Module.h"
 #include "llvm/IR/LLVMContext.h"
@@ -35,6 +36,7 @@ VMKit::VMKit(vmkit::BumpAllocator* alloc
 	llvm::InitializeNativeTargetDisassembler();
 	llvm::llvm_start_multithreaded();
 	_allocator = allocator;
+	pthread_mutex_init(&safepointMapLock, 0);
 }
 
 void* VMKit::operator new(size_t n, vmkit::BumpAllocator* allocator) {
@@ -45,6 +47,19 @@ void VMKit::destroy(VMKit* vm) {
 	vmkit::BumpAllocator::destroy(vm->allocator());
 }
 
+void VMKit::addSafepoint(Safepoint* sf) {
+	pthread_mutex_lock(&safepointMapLock);
+	safepointMap[sf->addr()] = sf;
+	pthread_mutex_unlock(&safepointMapLock);
+}
+
+Safepoint* VMKit::getSafepoint(uintptr_t addr) {
+	pthread_mutex_lock(&safepointMapLock);
+	Safepoint* res = safepointMap[addr];
+	pthread_mutex_unlock(&safepointMapLock);
+	return res;
+}
+
 llvm::LLVMContext& VMKit::llvmContext() {
 	return self()->getContext();
 }
@@ -117,7 +132,7 @@ llvm::Function* VMKit::getGCRoot(llvm::M
 }
 
 void VMKit::NotifyObjectEmitted(const llvm::ObjectImage &obj) {
-	//fprintf(stderr, "**** object jit event listener!\n");
+	fprintf(stderr, "**** object jit event listener!\n");
 }
 
 void VMKit::NotifyFunctionEmitted(const llvm::Function &F,





More information about the vmkit-commits mailing list