[vmkit-commits] [vmkit] r199641 - Use a single flag to drive the code generator.

Gael Thomas gael.thomas at lip6.fr
Mon Jan 20 02:45:42 PST 2014


Author: gthomas
Date: Mon Jan 20 04:45:41 2014
New Revision: 199641

URL: http://llvm.org/viewvc/llvm-project?rev=199641&view=rev
Log:
Use a single flag to drive the code generator.

Modified:
    vmkit/branches/mcjit/include/j3/j3codegen.h
    vmkit/branches/mcjit/include/j3/j3method.h
    vmkit/branches/mcjit/lib/j3/vm/j3codegen.cc
    vmkit/branches/mcjit/lib/j3/vm/j3method.cc
    vmkit/branches/mcjit/lib/j3/vm/j3trampoline.cc

Modified: vmkit/branches/mcjit/include/j3/j3codegen.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/include/j3/j3codegen.h?rev=199641&r1=199640&r2=199641&view=diff
==============================================================================
--- vmkit/branches/mcjit/include/j3/j3codegen.h (original)
+++ vmkit/branches/mcjit/include/j3/j3codegen.h Mon Jan 20 04:45:41 2014
@@ -36,11 +36,27 @@ namespace j3 {
 	};
 
 	class J3CodeGen {
+	public:
+		enum {
+			WithMethod = 1,
+			WithCaller = 2,
+			OnlyTranslate = 4,
+			NotUseStub = 8
+		};
+
+		bool withMethod() { return mode & WithMethod; }
+		bool withCaller() { return mode & WithCaller; }
+		bool onlyTranslate() { return mode & OnlyTranslate; }
+		bool useStub() { return !(mode & NotUseStub); }
+
+	private:
 		friend class J3CodeGenVar;
 		friend class J3ExceptionTable;
 		friend class J3ExceptionNode;
 		friend class J3Signature;
 
+		uint32_t               mode;
+
 		vmkit::BumpAllocator*  allocator;
 		llvm::Module*          module;
 		llvm::IRBuilder<>      builder;
@@ -52,6 +68,7 @@ namespace j3 {
 		J3Method*              method;
 		J3Signature*           signature;
 		J3Reader*              codeReader;
+		uint32_t               access;
 
 		llvm::BasicBlock*      bbCheckCastFailed;
 		llvm::BasicBlock*      bbNullCheckFailed;
@@ -184,13 +201,13 @@ namespace j3 {
 		llvm::Function*    patchPoint64;
 		llvm::Function*    patchPointVoid;
 
-		J3CodeGen(vmkit::BumpAllocator* _allocator, J3Method* method, bool withMethod, bool withCaller, bool onlyTranslate);
+		J3CodeGen(vmkit::BumpAllocator* _allocator, J3Method* method, uint32_t mode);
 		~J3CodeGen();
 
 		void* operator new(size_t n, vmkit::BumpAllocator* _allocator);
 		void  operator delete(void* ptr);
 	public:
-		static void translate(J3Method* method, bool withMethod=1, bool withCaller=1, bool onlyTranslate=0);
+		static void translate(J3Method* method, uint32_t mode);
 	};
 }
 

Modified: vmkit/branches/mcjit/include/j3/j3method.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/include/j3/j3method.h?rev=199641&r1=199640&r2=199641&view=diff
==============================================================================
--- vmkit/branches/mcjit/include/j3/j3method.h (original)
+++ vmkit/branches/mcjit/include/j3/j3method.h Mon Jan 20 04:45:41 2014
@@ -94,9 +94,10 @@ namespace j3 {
 
 		void                aotCompile();
 		void                aotSnapshot(llvm::Linker* linker);
-		void                ensureCompiled(bool withCaller, bool onlyTranslate=0);
+		void                ensureCompiled(uint32_t mode);
 		J3Signature::function_t cxxCaller();
 		void*               fnPtr();
+		llvm::Function*     llvmFunction() { return _llvmFunction; }
 		void*               functionPointerOrStaticTrampoline();
 		void*               functionPointerOrVirtualTrampoline();
 

Modified: vmkit/branches/mcjit/lib/j3/vm/j3codegen.cc
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/lib/j3/vm/j3codegen.cc?rev=199641&r1=199640&r2=199641&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/j3/vm/j3codegen.cc (original)
+++ vmkit/branches/mcjit/lib/j3/vm/j3codegen.cc Mon Jan 20 04:45:41 2014
@@ -28,17 +28,19 @@ using namespace j3;
 
 #define _onEndPoint() ({ if(onEndPoint()) return; })
 
-J3CodeGen::J3CodeGen(vmkit::BumpAllocator* _allocator, J3Method* m, bool withMethod, bool withCaller, bool onlyTranslate) :
+J3CodeGen::J3CodeGen(vmkit::BumpAllocator* _allocator, J3Method* m, uint32_t _mode) :
 	builder(J3Thread::get()->vm()->llvmContext()),
 	exceptions(this) {
 
 	allocator = _allocator;
+	mode = _mode;
 
 	method = m;
 	cl = method->cl()->asClass();
 	signature = method->signature();
 	loader = cl->loader();
 	vm = J3Thread::get()->vm();
+	access = method->access();
 
 #if 0
 	/* usefull to debug a single function */
@@ -56,9 +58,8 @@ J3CodeGen::J3CodeGen(vmkit::BumpAllocato
 						method->name()->cStr(), 
 						method->signature()->name()->cStr());
 
-	module = new llvm::Module(method->llvmFunctionName(), builder.getContext());
-	llvmFunction = buildFunction(method, 0);
-	llvmFunction->setGC("vmkit");
+	llvmFunction = method->llvmFunction();
+	module = llvmFunction ? llvmFunction->getParent() : new llvm::Module(method->llvmFunctionName(), builder.getContext());
 
 	bbCheckCastFailed = 0;
 	bbNullCheckFailed = 0;
@@ -95,8 +96,12 @@ J3CodeGen::J3CodeGen(vmkit::BumpAllocato
 	}
 #endif
 
-	if(withMethod) {
-		if(J3Cst::isNative(method->access()))
+	/* only translate of requested and not already translated */
+	if(withMethod() && !llvmFunction) {
+		llvmFunction = buildFunction(method, 0);
+		llvmFunction->setGC("vmkit");
+
+		if(J3Cst::isNative(access))
 			generateNative();
 		else
 			generateJava();
@@ -105,21 +110,23 @@ J3CodeGen::J3CodeGen(vmkit::BumpAllocato
 			llvmFunction->dump();
 	}
 
-	uint32_t access = method->access();
-	uint32_t needsCaller = withCaller && !signature->caller(access);
+	uint32_t needsCaller = withCaller() && !signature->caller(access);
+
 	if(needsCaller)
 		signature->generateCallerIR(access, this, module, "generic-caller");
 
-	if(!onlyTranslate)
+	/* compile only if requested and not already compiled */
+	void* fnPtr;
+	if(withMethod() && !onlyTranslate() && !method->fnPtr()) {
 		loader->compileModule(module);
+		fnPtr = (void*)loader->ee()->getFunctionAddress(llvmFunction->getName().data());
+	} else
+		fnPtr = method->fnPtr();
 	
 	if(needsCaller)
 		signature->setCaller(access, (J3Signature::function_t)loader->ee()->getFunctionAddress("generic-caller"));
 
-	if(withMethod) {
-		void* fnPtr = onlyTranslate ? 0 : (void*)loader->ee()->getFunctionAddress(llvmFunction->getName().data());
-		method->markCompiled(llvmFunction, fnPtr);
-	}
+	method->markCompiled(llvmFunction, fnPtr);
 }
 
 J3CodeGen::~J3CodeGen() {
@@ -132,11 +139,11 @@ void* J3CodeGen::operator new(size_t n,
 void J3CodeGen::operator delete(void* ptr) {
 }
 
-void J3CodeGen::translate(J3Method* method, bool withMethod, bool withCaller, bool onlyTranslate) {
+void J3CodeGen::translate(J3Method* method, uint32_t mode) {
 	J3Thread::get()->vm()->lockCompiler();
 	
 	vmkit::BumpAllocator* allocator = vmkit::BumpAllocator::create();
-	delete new(allocator) J3CodeGen(allocator, method, withMethod, withCaller, onlyTranslate);
+	delete new(allocator) J3CodeGen(allocator, method, mode);
 	vmkit::BumpAllocator::destroy(allocator);
 
 	J3Thread::get()->vm()->unlockCompiler();
@@ -1721,7 +1728,7 @@ void J3CodeGen::generateJava() {
 	else
 		builder.CreateRet(unflatten(ret.at(0, llvmFunction->getReturnType()), llvmFunction->getReturnType()));
 
-	if(J3Cst::isSynchronized(method->access())) {
+	if(J3Cst::isSynchronized(access)) {
 		static bool echoDone = 0;
 		if(!echoDone) {
 			fprintf(stderr, "IMPLEMENT ME: synchronized java\n");
@@ -1773,10 +1780,10 @@ llvm::Function* J3CodeGen::lookupNative(
 
 	nativeIns.push_back(vm->typeJNIEnvPtr);
 
-	if(J3Cst::isStatic(method->access()))
+	if(J3Cst::isStatic(access))
 		nativeIns.push_back(doNativeType(vm->classClass->llvmType()));
 
-	llvm::FunctionType*      origFType = method->signature()->functionType(method->access());
+	llvm::FunctionType*      origFType = method->signature()->functionType(access);
 	for(llvm::FunctionType::param_iterator it=origFType->param_begin(); it!=origFType->param_end(); it++)
 		nativeIns.push_back(doNativeType(*it));
 
@@ -1807,7 +1814,7 @@ void J3CodeGen::generateNative() {
 	llvm::Value* thread = currentThread();
 	llvm::Value* frame = builder.CreateCall(funcJ3ThreadTell, thread);
 
-	if(J3Cst::isSynchronized(method->access())) {
+	if(J3Cst::isSynchronized(access)) {
 		static bool echoDone = 0;
 		if(!echoDone) {
 			fprintf(stderr, "IMPLEMENT ME: synchronized java\n");
@@ -1816,7 +1823,7 @@ void J3CodeGen::generateNative() {
 	}
 
 	args.push_back(builder.CreateCall(funcJniEnv));
-	if(J3Cst::isStatic(method->access()))
+	if(J3Cst::isStatic(access))
 		args.push_back(javaClass(cl, 1));
 
 	uint32_t selfDone = 0;

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=199641&r1=199640&r2=199641&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/j3/vm/j3method.cc (original)
+++ vmkit/branches/mcjit/lib/j3/vm/j3method.cc Mon Jan 20 04:45:41 2014
@@ -55,7 +55,7 @@ J3Signature::function_t J3Method::cxxCal
 void J3Method::aotCompile() {
 	if(!J3Cst::isAbstract(access())) {
 		//fprintf(stderr, "compiling: %s::%s%s\n", cl()->name()->cStr(), name()->cStr(), signature()->name()->cStr());
-		ensureCompiled(0, 1);
+		ensureCompiled(J3CodeGen::WithMethod | J3CodeGen::OnlyTranslate);
 	}
 }
 
@@ -66,10 +66,10 @@ void J3Method::aotSnapshot(llvm::Linker*
 	}
 }
 
-void J3Method::ensureCompiled(bool withCaller, bool onlyTranslate) {
-	if(!fnPtr() || (withCaller && !cxxCaller())) {
+void J3Method::ensureCompiled(uint32_t mode) {
+	if(((mode & J3CodeGen::WithMethod) && !fnPtr()) || ((mode & J3CodeGen::WithCaller) && !cxxCaller())) {
 		//fprintf(stderr, "materializing: %s::%s%s\n", cl()->name()->cStr(), name()->cStr(), signature()->name()->cStr());
-		J3CodeGen::translate(this, !fnPtr(), withCaller, onlyTranslate);
+		J3CodeGen::translate(this, mode);
  	}
 }
 
@@ -114,7 +114,7 @@ J3Method* J3Method::resolve(J3ObjectHand
 
 J3Value J3Method::internalInvoke(J3ObjectHandle* handle, J3Value* inArgs) {
 	cl()->initialise();
-	ensureCompiled(1);  /* force the generation of the code and thus of the functionType */
+	ensureCompiled(J3CodeGen::WithMethod | J3CodeGen::WithCaller);  /* force the generation of the code and thus of the functionType */
 
 	J3Value* reIn;
 	if(handle) {
@@ -138,7 +138,7 @@ J3Value J3Method::internalInvoke(J3Objec
 
 J3Value J3Method::internalInvoke(J3ObjectHandle* handle, va_list va) {
 	cl()->initialise();
-	ensureCompiled(1);  /* force the generation of the code and thus of the functionType */
+	ensureCompiled(J3CodeGen::WithMethod | J3CodeGen::WithCaller);  /* force the generation of the code and thus of the functionType */
 
 	llvm::FunctionType* fType = signature()->functionType(J3Cst::ACC_STATIC);      /* static signature for va */
 	J3Value* args = (J3Value*)alloca(sizeof(J3Value)*(fType->getNumParams() + 1));

Modified: vmkit/branches/mcjit/lib/j3/vm/j3trampoline.cc
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/lib/j3/vm/j3trampoline.cc?rev=199641&r1=199640&r2=199641&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/j3/vm/j3trampoline.cc (original)
+++ vmkit/branches/mcjit/lib/j3/vm/j3trampoline.cc Mon Jan 20 04:45:41 2014
@@ -3,6 +3,7 @@
 #include "j3/j3method.h"
 #include "j3/j3thread.h"
 #include "j3/j3class.h"
+#include "j3/j3codegen.h"
 #include "j3/j3.h"
 
 using namespace j3;
@@ -20,7 +21,7 @@ void J3Trampoline::interfaceTrampoline(J
 	void* res;
 
 	if(desc->nbMethods == 1) {
-		desc->methods[0]->ensureCompiled(0);
+		desc->methods[0]->ensureCompiled(J3CodeGen::WithMethod);
 		res = desc->methods[0]->fnPtr();
 		handle->vt()->_interfaceMethodTable[index] = res;
 	} else {
@@ -40,7 +41,7 @@ void J3Trampoline::staticTrampoline(J3Ob
 	J3TrampolineArg arg = J3Thread::get()->_trampolineArg;
 
 	target->cl()->initialise();
-	target->ensureCompiled(0);
+	target->ensureCompiled(J3CodeGen::WithMethod);
 
 	trampoline_restart(target->fnPtr(), &arg);
 }
@@ -52,7 +53,7 @@ void J3Trampoline::virtualTrampoline(J3O
 	J3ObjectType* cl = handle->vt()->type()->asObjectType();
 	J3Method* impl = cl == target->cl() ? target : cl->findMethod(0, target->name(), target->signature());
 
-	impl->ensureCompiled(0);
+	impl->ensureCompiled(J3CodeGen::WithMethod);
 	void* res = impl->fnPtr();
 	handle->vt()->virtualMethods()[impl->index()] = res;
 





More information about the vmkit-commits mailing list