[vmkit-commits] [vmkit] r198251 - remove last unsafe llvm functions from J3Class and J3Method. The code is now probably thread safe for llvm.

Gael Thomas gael.thomas at lip6.fr
Mon Dec 30 14:10:22 PST 2013


Author: gthomas
Date: Mon Dec 30 16:10:21 2013
New Revision: 198251

URL: http://llvm.org/viewvc/llvm-project?rev=198251&view=rev
Log:
remove last unsafe llvm functions from J3Class and J3Method. The code is now probably thread safe for llvm.

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

Modified: vmkit/branches/mcjit/include/j3/j3class.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/include/j3/j3class.h?rev=198251&r1=198250&r2=198251&view=diff
==============================================================================
--- vmkit/branches/mcjit/include/j3/j3class.h (original)
+++ vmkit/branches/mcjit/include/j3/j3class.h Mon Dec 30 16:10:21 2013
@@ -11,8 +11,6 @@
 
 namespace llvm {
 	class Type;
-	class GlobalValue;
-	class Module;
 }
 
 namespace vmkit {
@@ -168,7 +166,6 @@ namespace j3 {
 
 		uint32_t                   logSize() { return sizeof(uintptr_t) == 8 ? 3 : 2; }
 		llvm::Type*                llvmType();
-		llvm::GlobalValue*         unsafe_llvmDescriptor(llvm::Module* module);
 
 		J3InterfaceSlotDescriptor* slotDescriptorAt(uint32_t index) { return &_interfaceSlotDescriptors[index]; }
 		void                       prepareInterfaceTable();

Modified: vmkit/branches/mcjit/include/j3/j3codegen.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/include/j3/j3codegen.h?rev=198251&r1=198250&r2=198251&view=diff
==============================================================================
--- vmkit/branches/mcjit/include/j3/j3codegen.h (original)
+++ vmkit/branches/mcjit/include/j3/j3codegen.h Mon Dec 30 16:10:21 2013
@@ -40,7 +40,7 @@ namespace j3 {
 		friend class J3ExceptionNode;
 
 		vmkit::BumpAllocator*  allocator;
-		llvm::Module*          _module;
+		llvm::Module*          module;
 		llvm::BasicBlock*      bb;
 		llvm::IRBuilder<>*     builder;
 		llvm::Function*        llvmFunction;
@@ -77,87 +77,92 @@ namespace j3 {
 
 		bool                   isWide;
 
-		llvm::Module*      module() { return _module; }
+		uint32_t            wideReadU1();
+		uint32_t            wideReadS1();
 
-		uint32_t           wideReadU1();
-		uint32_t           wideReadS1();
+		llvm::FunctionType* llvmFunctionType(J3MethodType* type);
+		llvm::Function*     buildFunction(J3Method* method, bool isStub=1);
+		llvm::Value*        methodDescriptor(J3Method* method);
+		llvm::Value*        typeDescriptor(J3ObjectType* objectType, llvm::Type* type);
 
-		llvm::Value*       spToCurrentThread(llvm::Value* sp);
-		llvm::Value*       currentThread();
+		llvm::Value*        spToCurrentThread(llvm::Value* sp);
+		llvm::Value*        currentThread();
 
-		llvm::Value*       nullCheck(llvm::Value* obj);
+		llvm::Value*        nullCheck(llvm::Value* obj);
 
-		llvm::BasicBlock*  newBB(const char* name);
-		bool               onEndPoint();
-		llvm::BasicBlock*  forwardBranch(const char* id, uint32_t pc, bool doAlloc, bool doPush);
-		void               condBr(llvm::Value* op);
+		llvm::BasicBlock*   newBB(const char* name);
+		bool                onEndPoint();
+		llvm::BasicBlock*   forwardBranch(const char* id, uint32_t pc, bool doAlloc, bool doPush);
+		void                condBr(llvm::Value* op);
 
-		llvm::Value*       flatten(llvm::Value* v, J3Type* type);
-		llvm::Value*       unflatten(llvm::Value* v, J3Type* type);
+		llvm::Value*        flatten(llvm::Value* v, J3Type* type);
+		llvm::Value*        unflatten(llvm::Value* v, J3Type* type);
 
-		llvm::Value*       handleToObject(llvm::Value* obj);
-		llvm::Value*       javaClass(J3ObjectType* type);
-		llvm::Value*       staticInstance(J3Class* cl);
-		llvm::Value*       vt(J3ObjectType* cl, bool resolve=0);
-		llvm::Value*       vt(llvm::Value* obj);
-		void               initialiseJ3ObjectType(J3ObjectType* cl);
+		llvm::Value*        handleToObject(llvm::Value* obj);
+		llvm::Value*        javaClass(J3ObjectType* type);
+		llvm::Value*        staticInstance(J3Class* cl);
+		llvm::Value*        vt(J3ObjectType* cl, bool resolve=0);
+		llvm::Value*        vt(llvm::Value* obj);
+		void                initialiseJ3ObjectType(J3ObjectType* cl);
 
-		void               monitorEnter(llvm::Value* obj);
-		void               monitorExit(llvm::Value* obj);
+		void                monitorEnter(llvm::Value* obj);
+		void                monitorExit(llvm::Value* obj);
 
-		llvm::Value*       isAssignableTo(llvm::Value* obj, J3ObjectType* type);
-		void               instanceof(llvm::Value* obj, J3ObjectType* type);
-		void               checkCast(llvm::Value* obj, J3ObjectType* type);
+		llvm::Value*        isAssignableTo(llvm::Value* obj, J3ObjectType* type);
+		void                instanceof(llvm::Value* obj, J3ObjectType* type);
+		void                checkCast(llvm::Value* obj, J3ObjectType* type);
 
-		void               floatToInteger(J3Type* from, J3Type* to);
-		void               compareFP(bool isL);
-		void               compareLong();
+		void                floatToInteger(J3Type* from, J3Type* to);
+		void                compareFP(bool isL);
+		void                compareLong();
 
-		llvm::Value*       fieldOffset(llvm::Value* obj, J3Field* f);
+		llvm::Value*        fieldOffset(llvm::Value* obj, J3Field* f);
 
-		void               get(llvm::Value* obj, J3Field* field);
-		void               getField(uint32_t idx);
-		void               getStatic(uint32_t idx);
+		void                get(llvm::Value* obj, J3Field* field);
+		void                getField(uint32_t idx);
+		void                getStatic(uint32_t idx);
 
-		void               put(llvm::Value* obj, llvm::Value* val, J3Field* field);
-		void               putField(uint32_t idx);
-		void               putStatic(uint32_t idx);
+		void                put(llvm::Value* obj, llvm::Value* val, J3Field* field);
+		void                putField(uint32_t idx);
+		void                putStatic(uint32_t idx);
 
-		void               invoke(J3Method* method, llvm::Value* func);
-		void               invokeVirtual(uint32_t idx);
-		void               invokeStatic(uint32_t idx);
-		void               invokeSpecial(uint32_t idx);
-		void               invokeInterface(uint32_t idx);
+		void                invoke(J3Method* method, llvm::Value* func);
+		void                invokeVirtual(uint32_t idx);
+		void                invokeStatic(uint32_t idx);
+		void                invokeSpecial(uint32_t idx);
+		void                invokeInterface(uint32_t idx);
 
-		llvm::Value*       arrayContent(J3Type* cType, llvm::Value* array, llvm::Value* idx);
+		llvm::Value*        arrayContent(J3Type* cType, llvm::Value* array, llvm::Value* idx);
 
-		void               arrayBoundCheck(llvm::Value* obj, llvm::Value* idx);
-		llvm::Value*       arrayLength(llvm::Value* obj);
-		llvm::Value*       arrayLengthPtr(llvm::Value* obj);
-		void               arrayStore(J3Type* cType);
-		void               arrayLoad(J3Type* cType);
+		void                arrayBoundCheck(llvm::Value* obj, llvm::Value* idx);
+		llvm::Value*        arrayLength(llvm::Value* obj);
+		llvm::Value*        arrayLengthPtr(llvm::Value* obj);
+		void                arrayStore(J3Type* cType);
+		void                arrayLoad(J3Type* cType);
 
-		void               newArray(uint8_t atype);
-		void               newArray(J3ArrayClass* type);
-		void               newObject(J3Class* cl);
+		void                newArray(uint8_t atype);
+		void                newArray(J3ArrayClass* type);
+		void                newObject(J3Class* cl);
 
-		void               ldc(uint32_t idx);
+		void                ldc(uint32_t idx);
 
-		void               selectExceptionNode(uint32_t idx);
+		void                selectExceptionNode(uint32_t idx);
 
-		void               translate();
+		void                translate();
 
-		void               initExceptionNode(J3ExceptionNode** pnode, uint32_t pc, J3ExceptionNode* node);
-		void               addToExceptionNode(J3ExceptionNode* node, J3ExceptionEntry* entry);
-		void               closeExceptionNode(J3ExceptionNode* node);
+		void                initExceptionNode(J3ExceptionNode** pnode, uint32_t pc, J3ExceptionNode* node);
+		void                addToExceptionNode(J3ExceptionNode* node, J3ExceptionEntry* entry);
+		void                closeExceptionNode(J3ExceptionNode* node);
 
-		void               generateJava();
-		void               generateNative();
+		void                generateJava();
+		void                generateNative();
+		llvm::Function*     lookupNative();
+		llvm::Type*         doNativeType(J3Type* type);
 
-		llvm::Value*       buildString(const char* msg);
+		llvm::Value*        buildString(const char* msg);
 
-		static void        echoDebugEnter(uint32_t isLeave, const char* msg, ...);
-		static void        echoDebugExecute(uint32_t level, const char* msg, ...);
+		static void         echoDebugEnter(uint32_t isLeave, const char* msg, ...);
+		static void         echoDebugExecute(uint32_t level, const char* msg, ...);
 
 #define _x(name, id)														\
 		llvm::Function* name;
@@ -170,13 +175,13 @@ namespace j3 {
 		llvm::Function*    patchPoint64;
 		llvm::Function*    patchPointVoid;
 
-		J3CodeGen(vmkit::BumpAllocator* _allocator, J3Method* method, llvm::Function* _llvmFunction);
+		J3CodeGen(vmkit::BumpAllocator* _allocator, J3Method* method);
 		~J3CodeGen();
 
 		void* operator new(size_t n, vmkit::BumpAllocator* _allocator);
 		void  operator delete(void* ptr);
 	public:
-		static void translate(J3Method* method, llvm::Function* llvmFunction);
+		static void translate(J3Method* method);
 	};
 }
 

Modified: vmkit/branches/mcjit/include/j3/j3method.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/include/j3/j3method.h?rev=198251&r1=198250&r2=198251&view=diff
==============================================================================
--- vmkit/branches/mcjit/include/j3/j3method.h (original)
+++ vmkit/branches/mcjit/include/j3/j3method.h Mon Dec 30 16:10:21 2013
@@ -9,10 +9,6 @@
 namespace llvm {
 	class FunctionType;
 	class Function;
-	class Type;
-	class GlobalValue;
-	class Module;
-	struct GenericValue;
 }
 
 namespace vmkit {
@@ -36,7 +32,8 @@ namespace j3 {
 	public:
 		J3MethodType(J3Type** args, size_t nbArgs);
 
-		llvm::FunctionType* unsafe_llvmFunctionType();                   /* call only while compiler locked */
+		void                setFunctionType(llvm::FunctionType* functionType) { _llvmFunctionType = functionType; }
+		llvm::FunctionType* functionType() { return _llvmFunctionType; }
 		uint32_t            nbIns() { return _nbIns; }
 		J3Type*             out() { return _out; }
 		J3Type*             ins(uint32_t idx) { return _ins[idx]; }
@@ -72,14 +69,16 @@ namespace j3 {
 		void* volatile               _staticTrampoline;
 		void* volatile               _virtualTrampoline;
 
-		llvm::Type* doNativeType(J3Type* type);
-
 		J3Value            internalInvoke(bool statically, J3ObjectHandle* handle, va_list va);
 		J3Value            internalInvoke(bool statically, J3ObjectHandle* handle, J3Value* args);
 		void               buildLLVMNames(J3Class* from);
 	public:
 		J3Method(uint16_t access, J3Class* cl, const vmkit::Name* name, const vmkit::Name* sign);
 
+		void*               nativeFnPtr() { return _nativeFnPtr; }
+
+		void                markCompiled(llvm::Function* llvmFunction, void* fnPtr);
+
 		uint32_t            interfaceIndex();
 
 		void*               getSymbolAddress();
@@ -87,20 +86,16 @@ namespace j3 {
 		char*               llvmFunctionName(J3Class* from=0);
 		char*               llvmDescriptorName(J3Class* from=0);
 		char*               llvmStubName(J3Class* from=0);
-		llvm::FunctionType* llvmType(J3Class* from=0);
 
 		void                postInitialise(uint32_t access, J3Attributes* attributes);
 		void                setResolved(uint32_t index); 
 
 		J3Method*           resolve(J3ObjectHandle* obj);
 
-		llvm::Function*     unsafe_nativeLLVMFunction(llvm::Module* module); /* call only while compiler locked */
-		llvm::GlobalValue*  unsafe_llvmDescriptor(llvm::Module* module);
-		llvm::Function*     unsafe_llvmFunction(bool isStub, llvm::Module* module, J3Class* from=0); /* call only while compiler locked */
-
 		uint32_t            index();
 		uint32_t*           indexPtr() { return &_index; }
 		bool                isResolved() { return _index != -1; }
+		bool                isCompiled() { return _fnPtr; }
 
 		J3Attributes*       attributes() const { return _attributes; }
 		uint16_t            access() const { return _access; }

Modified: vmkit/branches/mcjit/lib/j3/vm/j3class.cc
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/lib/j3/vm/j3class.cc?rev=198251&r1=198250&r2=198251&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/j3/vm/j3class.cc (original)
+++ vmkit/branches/mcjit/lib/j3/vm/j3class.cc Mon Dec 30 16:10:21 2013
@@ -151,10 +151,6 @@ llvm::Type* J3ObjectType::llvmType() {
 	return loader()->vm()->typeJ3ObjectPtr;
 }
 
-llvm::GlobalValue* J3ObjectType::unsafe_llvmDescriptor(llvm::Module* module) {
-	return llvm::cast<llvm::GlobalValue>(module->getOrInsertGlobal(nativeName(), loader()->vm()->typeJ3ObjectType));
-}
-
 J3Method* J3ObjectType::findVirtualMethod(const vmkit::Name* name, const vmkit::Name* sign, bool error) {
 	J3::internalError(L"should not happe: %ls::%ls\n", J3ObjectType::name()->cStr(), name->cStr());
 }

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=198251&r1=198250&r2=198251&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/j3/vm/j3codegen.cc (original)
+++ vmkit/branches/mcjit/lib/j3/vm/j3codegen.cc Mon Dec 30 16:10:21 2013
@@ -26,7 +26,7 @@ using namespace j3;
 
 #define _onEndPoint() ({ if(onEndPoint()) return; })
 
-J3CodeGen::J3CodeGen(vmkit::BumpAllocator* _allocator, J3Method* m, llvm::Function* _llvmFunction) :
+J3CodeGen::J3CodeGen(vmkit::BumpAllocator* _allocator, J3Method* m) :
 	exceptions(this) {
 	
 	allocator = _allocator;
@@ -45,9 +45,9 @@ J3CodeGen::J3CodeGen(vmkit::BumpAllocato
 	if(vm->options()->debugTranslate)
 		fprintf(stderr, "  translating bytecode of: %ls::%ls%ls\n", method->cl()->name()->cStr(), method->name()->cStr(), method->sign()->cStr());
 
-	llvmFunction = _llvmFunction;
+	module = new llvm::Module(method->llvmFunctionName(), vm->llvmContext());
+	llvmFunction = buildFunction(method, 0);
 	llvmFunction->setGC("vmkit");
-	_module = llvmFunction->getParent();
 
 	bbCheckCastFailed = 0;
 	bbNullCheckFailed = 0;
@@ -62,19 +62,19 @@ J3CodeGen::J3CodeGen(vmkit::BumpAllocato
 	uintPtrTy = builder->getIntPtrTy(vm->dataLayout());
 
 #define _x(name, id)														\
-	name = vm->introspectFunction(module(), id);
+	name = vm->introspectFunction(module, id);
 #include "j3/j3meta.def"
 #undef _x
 
-	gvTypeInfo               = vm->introspectGlobalValue(module(),  "typeinfo for void*");
+	gvTypeInfo               = vm->introspectGlobalValue(module,  "typeinfo for void*");
 
-	gcRoot                   = vm->getGCRoot(module());
+	gcRoot                   = vm->getGCRoot(module);
 
-	frameAddress             = llvm::Intrinsic::getDeclaration(module(), llvm::Intrinsic::frameaddress);
+	frameAddress             = llvm::Intrinsic::getDeclaration(module, llvm::Intrinsic::frameaddress);
 
 #if 0
-	//stackMap       = llvm::Intrinsic::getDeclaration(module(), llvm::Intrinsic::experimental_stackmap);
-	//patchPointVoid = llvm::Intrinsic::getDeclaration(module(), llvm::Intrinsic::experimental_patchpoint_i64);
+	//stackMap       = llvm::Intrinsic::getDeclaration(module, llvm::Intrinsic::experimental_stackmap);
+	//patchPointVoid = llvm::Intrinsic::getDeclaration(module, llvm::Intrinsic::experimental_patchpoint_i64);
 	{
 		llvm::Type* ins[] = {
 			builder->getInt64Ty(),
@@ -83,7 +83,7 @@ J3CodeGen::J3CodeGen(vmkit::BumpAllocato
 			builder->getInt32Ty()
 		};
 		patchPointVoid = (llvm::Function*)
-			module()->getOrInsertFunction(llvm::Intrinsic::getName(llvm::Intrinsic::experimental_patchpoint_void),
+			module->getOrInsertFunction(llvm::Intrinsic::getName(llvm::Intrinsic::experimental_patchpoint_void),
 																		llvm::FunctionType::get(builder->getVoidTy(), ins, 1));
 	}
 #endif
@@ -99,6 +99,11 @@ J3CodeGen::J3CodeGen(vmkit::BumpAllocato
 
 	if(vm->options()->debugTranslate > 3)
 		llvmFunction->dump();
+
+	loader->compileModule(module);
+	void* fnPtr = (void*)loader->ee()->getFunctionAddress(llvmFunction->getName().data());
+
+	method->markCompiled(llvmFunction, fnPtr);
 }
 
 J3CodeGen::~J3CodeGen() {
@@ -111,10 +116,14 @@ void* J3CodeGen::operator new(size_t n,
 void J3CodeGen::operator delete(void* ptr) {
 }
 
-void J3CodeGen::translate(J3Method* method, llvm::Function* llvmFunction) {
+void J3CodeGen::translate(J3Method* method) {
+	method->cl()->loader()->vm()->lockCompiler();
+
 	vmkit::BumpAllocator* allocator = vmkit::BumpAllocator::create();
-	delete new(allocator) J3CodeGen(allocator, method, llvmFunction);
+	delete new(allocator) J3CodeGen(allocator, method);
 	vmkit::BumpAllocator::destroy(allocator);
+
+	method->cl()->loader()->vm()->unlockCompiler();
 }
 
 uint32_t J3CodeGen::wideReadU1() {
@@ -164,6 +173,34 @@ llvm::Value* J3CodeGen::unflatten(llvm::
 	}
 }
 
+llvm::FunctionType* J3CodeGen::llvmFunctionType(J3MethodType* type) {
+	llvm::FunctionType* res = type->functionType();
+
+	if(!res) {
+		std::vector<llvm::Type*> in;
+		for(uint32_t i=0; i<type->nbIns(); i++)
+			in.push_back(type->ins(i)->llvmType());
+		res = llvm::FunctionType::get(type->out()->llvmType(), in, 0);
+		type->setFunctionType(res);
+	}
+	return res;
+}
+
+llvm::Function* J3CodeGen::buildFunction(J3Method* method, bool isStub) {
+	const char* id = (isStub && !method->isCompiled()) ? method->llvmStubName(cl) : method->llvmFunctionName(cl);
+	return (llvm::Function*)module->getOrInsertFunction(id, llvmFunctionType(method->methodType(cl)));
+}
+
+llvm::Value* J3CodeGen::typeDescriptor(J3ObjectType* objectType, llvm::Type* type) {
+	llvm::Value* v = module->getOrInsertGlobal(objectType->nativeName(), 
+																							 vm->typeJ3ObjectType);
+	return type == vm->typeJ3ObjectTypePtr ? v : builder->CreateBitCast(v, type);
+}
+
+llvm::Value* J3CodeGen::methodDescriptor(J3Method* method) {
+	return module->getOrInsertGlobal(method->llvmDescriptorName(), vm->typeJ3Method);
+}
+
 llvm::Value* J3CodeGen::spToCurrentThread(llvm::Value* sp) {
 	return builder->CreateIntToPtr(builder->CreateAnd(builder->CreatePtrToInt(sp, uintPtrTy),
 																										llvm::ConstantInt::get(uintPtrTy, vmkit::Thread::getThreadMask())),
@@ -273,11 +310,11 @@ void J3CodeGen::monitorExit(llvm::Value*
 
 void J3CodeGen::initialiseJ3ObjectType(J3ObjectType* cl) {
 	if(!cl->isInitialised())
-		builder->CreateCall(funcJ3TypeInitialise, builder->CreateBitCast(cl->unsafe_llvmDescriptor(module()), vm->typeJ3TypePtr));
+		builder->CreateCall(funcJ3TypeInitialise, typeDescriptor(cl, vm->typeJ3TypePtr));
 }
 
 llvm::Value* J3CodeGen::javaClass(J3ObjectType* type) {
-	return builder->CreateCall(funcJ3ObjectTypeJavaClass, type->unsafe_llvmDescriptor(module()));
+	return builder->CreateCall(funcJ3ObjectTypeJavaClass, typeDescriptor(type, vm->typeJ3ObjectTypePtr));
 }
 
 llvm::Value* J3CodeGen::handleToObject(llvm::Value* obj) {
@@ -288,7 +325,7 @@ llvm::Value* J3CodeGen::handleToObject(l
 llvm::Value* J3CodeGen::staticInstance(J3Class* cl) {
 	initialiseJ3ObjectType(cl);
 	return handleToObject(builder->CreateCall(funcJ3ClassStaticInstance, 
-																						builder->CreateBitCast(cl->unsafe_llvmDescriptor(module()), vm->typeJ3ClassPtr)));
+																						typeDescriptor(cl, vm->typeJ3ClassPtr)));
 }
 
 llvm::Value* J3CodeGen::vt(llvm::Value* obj) {
@@ -301,7 +338,7 @@ llvm::Value* J3CodeGen::vt(llvm::Value*
 
 llvm::Value* J3CodeGen::vt(J3ObjectType* type, bool doResolve) {
 	llvm::Value* func = doResolve && !type->isResolved() ? funcJ3TypeVTAndResolve : funcJ3TypeVT;
-	return builder->CreateCall(func, builder->CreateBitCast(type->unsafe_llvmDescriptor(module()), vm->typeJ3TypePtr));
+	return builder->CreateCall(func, typeDescriptor(type, vm->typeJ3TypePtr));
 }
 
 llvm::Value* J3CodeGen::nullCheck(llvm::Value* obj) {
@@ -365,7 +402,7 @@ void J3CodeGen::invokeInterface(uint32_t
 															builder->getInt32(J3VirtualTable::gepInterfaceMethods),
 															builder->getInt32(index % J3VirtualTable::nbInterfaceMethodTable) };
 	llvm::Value* func = builder->CreateBitCast(builder->CreateLoad(builder->CreateGEP(vt(obj), gepFunc)), 
-																						 type->unsafe_llvmFunctionType()->getPointerTo());
+																						 llvmFunctionType(type)->getPointerTo());
 
 	invoke(target, func);
 }
@@ -378,14 +415,14 @@ void J3CodeGen::invokeVirtual(uint32_t i
 	if(target->isResolved())
 		funcEntry = builder->getInt32(target->index());
 	else
-		funcEntry = builder->CreateCall(funcJ3MethodIndex, target->unsafe_llvmDescriptor(module()));
+		funcEntry = builder->CreateCall(funcJ3MethodIndex, methodDescriptor(target));
 
 	llvm::Value*  obj = nullCheck(stack.top(type->nbIns() - 1));
 	llvm::Value*  gepFunc[] = { builder->getInt32(0),
 															builder->getInt32(J3VirtualTable::gepVirtualMethods),
 															funcEntry };
 	llvm::Value* func = builder->CreateBitCast(builder->CreateLoad(builder->CreateGEP(vt(obj), gepFunc)), 
-																						 type->unsafe_llvmFunctionType()->getPointerTo());
+																						 llvmFunctionType(type)->getPointerTo());
 
 	invoke(target, func);
 }
@@ -393,12 +430,12 @@ void J3CodeGen::invokeVirtual(uint32_t i
 void J3CodeGen::invokeStatic(uint32_t idx) {
 	J3Method* target = cl->methodAt(idx, J3Cst::ACC_STATIC);
 	initialiseJ3ObjectType(target->cl());
-	invoke(target, target->unsafe_llvmFunction(1, module(), cl));
+	invoke(target, buildFunction(target));
 }
 
 void J3CodeGen::invokeSpecial(uint32_t idx) {
 	J3Method* target = cl->methodAt(idx, 0);
-	invoke(target, target->unsafe_llvmFunction(1, module(), cl));
+	invoke(target, buildFunction(target));
 }
 
 llvm::Value* J3CodeGen::fieldOffset(llvm::Value* obj, J3Field* f) {
@@ -514,7 +551,7 @@ void J3CodeGen::newObject(J3Class* cl) {
 	llvm::Value* size;
 
 	if(!cl->isResolved()) {
-		size = builder->CreateCall(funcJ3LayoutStructSize, builder->CreateBitCast(cl->unsafe_llvmDescriptor(module()), vm->typeJ3LayoutPtr));
+		size = builder->CreateCall(funcJ3LayoutStructSize, typeDescriptor(cl, vm->typeJ3LayoutPtr));
 	} else {
 		size = builder->getInt64(cl->structSize());
 	}
@@ -641,7 +678,7 @@ void J3CodeGen::ldc(uint32_t idx) {
 		case J3Cst::CONSTANT_Class:   res = handleToObject(javaClass(cl->classAt(idx))); break;
 		case J3Cst::CONSTANT_String:  
 			res = handleToObject(builder->CreateCall2(funcJ3ClassStringAt, 
-																								builder->CreateBitCast(cl->unsafe_llvmDescriptor(module()), vm->typeJ3ClassPtr),
+																								typeDescriptor(cl, vm->typeJ3ClassPtr),
 																								builder->getInt16(idx)));
 			break;
 		default:
@@ -737,7 +774,7 @@ llvm::Value* J3CodeGen::buildString(cons
 	elmts.push_back(builder->getInt8(0));
 
 	llvm::Constant* str = llvm::ConstantArray::get(llvm::ArrayType::get(builder->getInt8Ty(), n+1), elmts);
-	llvm::Value* var = new llvm::GlobalVariable(*module(),
+	llvm::Value* var = new llvm::GlobalVariable(*module,
 																							str->getType(),
 																							1,
 																							llvm::GlobalVariable::InternalLinkage,
@@ -1492,7 +1529,7 @@ void J3CodeGen::generateJava() {
 	if(!reader.adjustSize(length))
 		J3::classFormatError(cl, L"Code attribute of %ls %ls is too large (%d)", method->name()->cStr(), method->sign()->cStr(), length);
 
-	llvm::DIBuilder* dbgBuilder = new llvm::DIBuilder(*module());
+	llvm::DIBuilder* dbgBuilder = new llvm::DIBuilder(*module);
 
   dbgInfo =
 		dbgBuilder->createFunction(llvm::DIDescriptor(),    // Function scope
@@ -1568,10 +1605,66 @@ void J3CodeGen::generateJava() {
 	ret.killUnused();
 }
 
+llvm::Type* J3CodeGen::doNativeType(J3Type* type) {
+	llvm::Type* t = type->llvmType();
+
+	if(t->isPointerTy())
+		return vm->typeJ3ObjectHandlePtr;
+	else
+		return t;
+}
+
+llvm::Function* J3CodeGen::lookupNative() {
+	J3Mangler      mangler(cl);
+
+	mangler.mangle(mangler.javaId)->mangle(method);
+	uint32_t length = mangler.length();
+	mangler.mangleType(method);
+
+	void* fnPtr = method->nativeFnPtr();
+
+	if(!fnPtr)
+		fnPtr = loader->lookupNativeFunctionPointer(method, mangler.cStr());
+
+	if(!fnPtr) {
+		mangler.cStr()[length] = 0;
+		fnPtr = loader->lookupNativeFunctionPointer(method, mangler.mangleType(method)->cStr());
+	}
+
+	if(!fnPtr)
+		J3::linkageError(method);
+
+	std::vector<llvm::Type*> nativeIns;
+	llvm::Type*              nativeOut;
+
+	nativeIns.push_back(vm->typeJNIEnvPtr);
+
+	if(J3Cst::isStatic(method->access()))
+		nativeIns.push_back(doNativeType(vm->classClass));
+			
+	for(int i=0; i<methodType->nbIns(); i++)
+		nativeIns.push_back(doNativeType(methodType->ins(i)));
+
+	nativeOut = doNativeType(methodType->out());
+
+	char* buf = (char*)loader->allocator()->allocate(mangler.length()+1);
+	memcpy(buf, mangler.cStr(), mangler.length()+1);
+
+	llvm::FunctionType* fType = llvm::FunctionType::get(nativeOut, nativeIns, 0);
+	llvm::Function* res = llvm::Function::Create(fType,
+																							 llvm::GlobalValue::ExternalLinkage,
+																							 buf,
+																							 module);
+
+	loader->addSymbol(buf, new(loader->allocator()) vmkit::NativeSymbol(fnPtr));
+
+	return res;
+}
+
 void J3CodeGen::generateNative() {
 	std::vector<llvm::Value*> args;
 
-	llvm::Function* nat = method->unsafe_nativeLLVMFunction(module());
+	llvm::Function* nat = lookupNative();
 
 	llvm::Value* res;
 	llvm::Value* thread = currentThread();

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=198251&r1=198250&r2=198251&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/j3/vm/j3method.cc (original)
+++ vmkit/branches/mcjit/lib/j3/vm/j3method.cc Mon Dec 30 16:10:21 2013
@@ -28,16 +28,6 @@ J3MethodType::J3MethodType(J3Type** args
 			
 }
 
-llvm::FunctionType* J3MethodType::unsafe_llvmFunctionType() {
-	if(!_llvmFunctionType) {
-		std::vector<llvm::Type*> in;
-		for(uint32_t i=0; i<nbIns(); i++)
-			in.push_back(ins(i)->llvmType());
-		_llvmFunctionType = llvm::FunctionType::get(out()->llvmType(), in, 0);
-	}
-	return _llvmFunctionType;
-}
-
 J3Method::J3Method(uint16_t access, J3Class* cl, const vmkit::Name* name, const vmkit::Name* sign) :
 	_selfCode(this) {
 	_access = access;
@@ -55,8 +45,13 @@ uint32_t J3Method::index()  {
 	return _index; 
 }
 
+void J3Method::markCompiled(llvm::Function* llvmFunction, void* fnPtr) {
+	_llvmFunction = llvmFunction;
+	_fnPtr = fnPtr;
+}
+
 void* J3Method::fnPtr() {
-	if(!_fnPtr) {
+	if(!isCompiled()) {
 		//fprintf(stderr, "materializing: %ls::%ls%ls\n", this, cl()->name()->cStr(), name()->cStr(), sign()->cStr());
 		if(!isResolved()) {
 			if(cl()->loader()->vm()->options()->debugLinking)
@@ -67,23 +62,14 @@ void* J3Method::fnPtr() {
 				J3::noSuchMethodError(L"unable to find method", cl(), name(), sign());
 		}
 
-		cl()->loader()->vm()->lockCompiler();
-		llvm::Module* module = new llvm::Module(llvmFunctionName(), cl()->loader()->vm()->llvmContext());
-		_llvmFunction = unsafe_llvmFunction(0, module);
-
-		J3CodeGen::translate(this, _llvmFunction);
-
-		cl()->loader()->compileModule(module);
-
-		_fnPtr = (void*)cl()->loader()->ee()->getFunctionAddress(_llvmFunction->getName().data());
-		cl()->loader()->vm()->unlockCompiler();
+		J3CodeGen::translate(this);
  	}
 
 	return _fnPtr;
 }
 
 void* J3Method::functionPointerOrStaticTrampoline() {
-	if(_fnPtr)
+	if(isCompiled())
 		return _fnPtr;
 	if(!_staticTrampoline)
 		_staticTrampoline = J3Trampoline::buildStaticTrampoline(cl()->loader()->allocator(), this);
@@ -91,7 +77,7 @@ void* J3Method::functionPointerOrStaticT
 }
 
 void* J3Method::functionPointerOrVirtualTrampoline() {
-	if(_fnPtr)
+	if(isCompiled())
 		return _fnPtr;
 	if(!_virtualTrampoline)
 		_virtualTrampoline = J3Trampoline::buildVirtualTrampoline(cl()->loader()->allocator(), this);
@@ -335,15 +321,6 @@ char* J3Method::llvmStubName(J3Class* fr
 	return _llvmAllNames + 0;
 }
 
-llvm::GlobalValue* J3Method::unsafe_llvmDescriptor(llvm::Module* module) {
-	return llvm::cast<llvm::GlobalValue>(module->getOrInsertGlobal(llvmDescriptorName(), cl()->loader()->vm()->typeJ3Method));
-}
-
-llvm::Function* J3Method::unsafe_llvmFunction(bool isStub, llvm::Module* module, J3Class* from) {
-	const char* id = (isStub && !_fnPtr) ? llvmStubName(from) : llvmFunctionName(from);
-	return (llvm::Function*)module->getOrInsertFunction(id, methodType(from ? from : cl())->unsafe_llvmFunctionType());
-}
-
 void J3Method::dump() {
 	printf("Method: %ls %ls::%ls\n", sign()->cStr(), cl()->name()->cStr(), name()->cStr());
 }
@@ -353,61 +330,3 @@ void J3Method::registerNative(void* fnPt
 		J3::noSuchMethodError(L"unable to dynamically modify a native function", cl(), name(), sign());
 	_nativeFnPtr = fnPtr;
 }
-
-llvm::Type* J3Method::doNativeType(J3Type* type) {
-	llvm::Type* t = type->llvmType();
-
-	if(t->isPointerTy())
-		return cl()->loader()->vm()->typeJ3ObjectHandlePtr;
-	else
-		return t;
-}
-
-llvm::Function* J3Method::unsafe_nativeLLVMFunction(llvm::Module* module) {
-	J3ClassLoader* loader = cl()->loader();
-	J3Mangler      mangler(cl());
-
-	mangler.mangle(mangler.javaId)->mangle(this);
-	uint32_t length = mangler.length();
-	mangler.mangleType(this);
-
-	void* fnPtr = _nativeFnPtr;
-
-	if(!fnPtr)
-		fnPtr = loader->lookupNativeFunctionPointer(this, mangler.cStr());
-
-	if(!fnPtr) {
-		mangler.cStr()[length] = 0;
-		fnPtr = loader->lookupNativeFunctionPointer(this, mangler.mangleType(this)->cStr());
-	}
-
-	if(!fnPtr)
-		J3::linkageError(this);
-
-	J3MethodType*            type = methodType();
-	std::vector<llvm::Type*> nativeIns;
-	llvm::Type*              nativeOut;
-
-	nativeIns.push_back(loader->vm()->typeJNIEnvPtr);
-
-	if(J3Cst::isStatic(access()))
-		nativeIns.push_back(doNativeType(loader->vm()->classClass));
-			
-	for(int i=0; i<type->nbIns(); i++)
-		nativeIns.push_back(doNativeType(type->ins(i)));
-
-	nativeOut = doNativeType(type->out());
-
-	char* buf = (char*)cl()->loader()->allocator()->allocate(mangler.length()+1);
-	memcpy(buf, mangler.cStr(), mangler.length()+1);
-
-	llvm::FunctionType* fType = llvm::FunctionType::get(nativeOut, nativeIns, 0);
-	llvm::Function* res = llvm::Function::Create(fType,
-																							 llvm::GlobalValue::ExternalLinkage,
-																							 buf,
-																							 module);
-
-	cl()->loader()->addSymbol(buf, new(cl()->loader()->allocator()) vmkit::NativeSymbol(fnPtr));
-
-	return res;
-}

Modified: vmkit/branches/mcjit/lib/j3/vm/j3thread.cc
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/lib/j3/vm/j3thread.cc?rev=198251&r1=198250&r2=198251&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/j3/vm/j3thread.cc (original)
+++ vmkit/branches/mcjit/lib/j3/vm/j3thread.cc Mon Dec 30 16:10:21 2013
@@ -33,7 +33,7 @@ void J3Thread::start(J3ObjectHandle* han
 	vmkit::BumpAllocator* allocator = vmkit::BumpAllocator::create();
 	J3Thread* thread = new J3Thread(get()->vm());
 	thread->assocJavaThread(handle);
-	//thread->Thread::start();
+	thread->Thread::start();
 }
 
 J3Method* J3Thread::getJavaCaller(uint32_t level) {





More information about the vmkit-commits mailing list