diff --git a/cmake/Modules/GetLuaJIT.cmake b/cmake/Modules/GetLuaJIT.cmake index f9ba0a01..11000ed2 100644 --- a/cmake/Modules/GetLuaJIT.cmake +++ b/cmake/Modules/GetLuaJIT.cmake @@ -86,8 +86,8 @@ endif() if(WIN32) add_custom_command( - OUTPUT ${LUAJIT_STATIC_LIBRARY} ${LUAJIT_SHARED_LIBRARY_PATHS} ${LUAJIT_EXECUTABLE} ${LUAJIT_INSTALL_HEADERS} - DEPENDS ${LUAJIT_SOURCE_DIR} + OUTPUT ${LUAJIT_STATIC_LIBRARY} ${LUAJIT_SHARED_LIBRARY_PATHS} ${LUAJIT_EXECUTABLE} + DEPENDS ${LUAJIT_INSTALL_HEADERS} COMMAND msvcbuild WORKING_DIRECTORY ${LUAJIT_SOURCE_DIR}/src VERBATIM diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4cbde5d2..5de3e534 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -134,7 +134,7 @@ if(WIN32) /nologo /EHsc # extern "C" functions don't throw exceptions /w # disable warnings - /MD # use thread-safe version of MSVCRT.lib + #/MD # Don't do this, cmake chooses the correct version automatically /Zi # debug info ) target_compile_definitions(TerraObjectFiles diff --git a/src/llvmheaders.h b/src/llvmheaders.h index e2ed2826..f66a6b36 100644 --- a/src/llvmheaders.h +++ b/src/llvmheaders.h @@ -103,7 +103,6 @@ using llvm::legacy::PassManager; typedef llvm::raw_pwrite_stream emitobjfile_t; typedef llvm::DIFile* DIFileP; #else -#define DEBUG_INFO_WORKING using llvm::FunctionPassManager; using llvm::PassManager; typedef llvm::raw_ostream emitobjfile_t; diff --git a/src/main.cpp b/src/main.cpp index fbe870ad..498ccdd8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,8 +10,7 @@ #include #include "ext/getopt.h" #define isatty(x) _isatty(x) -#define NOMINMAX -#include +#include "twindows.h" #else #include #include diff --git a/src/tcompiler.cpp b/src/tcompiler.cpp index 31d41701..dd24a25e 100644 --- a/src/tcompiler.cpp +++ b/src/tcompiler.cpp @@ -11,16 +11,6 @@ extern "C" { #include #include -#ifdef _WIN32 -#include -#include -#include -#undef interface -#else -#include -#include -#endif - #include #include #include "llvmheaders.h" @@ -38,8 +28,19 @@ extern "C" { #include "llvm/Support/Atomic.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/Path.h" #include "tllvmutil.h" +#ifdef _WIN32 +#include +#include +#include "twindows.h" +#undef interface +#else +#include +#include +#endif + using namespace llvm; #define TERRALIB_FUNCTIONS(_) \ @@ -414,8 +415,10 @@ static void InitializeJIT(TerraCompilationUnit *CU) { (CU->T->options.usemcjit) ? new Module("terra", *CU->TT->ctx) : CU->M; #ifdef _WIN32 std::string MCJITTriple = CU->TT->Triple; +#if LLVM_VERSION < 38 MCJITTriple.append("-elf"); // on windows we need to use an elf container because // coff is not supported yet +#endif topeemodule->setTargetTriple(MCJITTriple); #else topeemodule->setTargetTriple(CU->TT->Triple); @@ -1347,8 +1350,10 @@ struct FunctionEmitter { std::vector > breakpoints; // stack of basic blocks where a break statement should go -#ifdef DEBUG_INFO_WORKING DIBuilder *DB; +#if LLVM_VERSION >= 37 + DISubprogram *SP; +#else DISubprogram SP; #endif @@ -2422,7 +2427,6 @@ struct FunctionEmitter { B->CreateBr(footer); } -#ifdef DEBUG_INFO_WORKING DIFileP createDebugInfoForFile(const char *filename) { // checking the existence of a file once per function can be expensive, // so only do it if debug mode is set to slow compile anyway. @@ -2432,6 +2436,7 @@ struct FunctionEmitter { llvm::sys::fs::make_absolute(filepath); return DB->createFile(llvm::sys::path::filename(filepath), llvm::sys::path::parent_path(filepath)); + } else { return DB->createFile(filename, "."); } @@ -2450,20 +2455,55 @@ struct FunctionEmitter { DB = new DIBuilder(*M); DIFileP file = createDebugInfoForFile(filename); +#if LLVM_VERSION >= 40 + DICompileUnit *CU = DB->createCompileUnit( + dwarf::DW_LANG_C89, DB->createFile("compilationunit", "."), "terra", + true, "", 0); +#elif LLVM_VERSION >= 37 + DICompileUnit *CU = DB->createCompileUnit(1, "compilationunit", ".", "terra", + true, "", 0); +#else DICompileUnit CU = DB->createCompileUnit(1, "compilationunit", ".", "terra", true, "", 0); +#endif + #if LLVM_VERSION >= 36 auto TA = DB->getOrCreateTypeArray(ArrayRef()); #else auto TA = DB->getOrCreateArray(ArrayRef()); #endif + +#if LLVM_VERSION >= 80 + SP = DB->createFunction( + CU, fstate->func->getName(), fstate->func->getName(), file, lineno, + DB->createSubroutineType(TA), 0, llvm::DINode::FlagZero, + llvm::DISubprogram::DISPFlags::SPFlagOptimized | + llvm::DISubprogram::DISPFlags::SPFlagDefinition); + fstate->func->setSubprogram(SP); +#elif LLVM_VERSION >= 40 + SP = DB->createFunction(CU, fstate->func->getName(), fstate->func->getName(), + file, lineno, DB->createSubroutineType(TA), false, + true, 0, llvm::DINode::FlagZero, true); + fstate->func->setSubprogram(SP); +#elif LLVM_VERSION >= 37 + SP = DB->createFunction(CU, fstate->func->getName(), fstate->func->getName(), + file, lineno, DB->createSubroutineType(TA), false, + true, 0, 0, true); + fstate->func->setSubprogram(SP); +#else SP = DB->createFunction(CU, fstate->func->getName(), fstate->func->getName(), file, lineno, DB->createSubroutineType(file, TA), false, true, 0, 0, true, fstate->func); +#endif if (!M->getModuleFlagsMetadata()) { M->addModuleFlag(llvm::Module::Warning, "Dwarf Version", 2); M->addModuleFlag(llvm::Module::Warning, "Debug Info Version", 1); + +#ifdef _WIN32 + M->addModuleFlag(llvm::Module::Warning, "CodeView", 1); + M->addModuleFlag(llvm::Module::Warning, "CodeViewGHash", 1); +#endif } filenamecache[filename] = SP; } @@ -2472,6 +2512,7 @@ struct FunctionEmitter { DEBUG_ONLY(T) { DB->finalize(); delete DB; + DB = nullptr; } } void setDebugPoint(Obj *obj) { @@ -2483,11 +2524,6 @@ struct FunctionEmitter { scope)); } } -#else - void initDebug(const char *filename, int lineno) {} - void endDebug() {} - void setDebugPoint(Obj *obj) {} -#endif void setInsertBlock(BasicBlock *bb) { B->SetInsertPoint(bb); } void pushBreakpoint(BasicBlock *exit) { @@ -2596,8 +2632,30 @@ struct FunctionEmitter { } BasicBlock *copyBlock(BasicBlock *BB) { ValueToValueMapTy VMap; - BasicBlock *NewBB = CloneBasicBlock(BB, VMap, "", fstate->func); - for (BasicBlock::iterator II = NewBB->begin(), IE = NewBB->end(); II != IE; ++II) +#if LLVM_VERSION < 50 + BasicBlock *NewBB = CloneBasicBlock(BB, VMap, "defer", fstate->func); +#else + DebugInfoFinder DIFinder; + BasicBlock *NewBB = + CloneBasicBlock(BB, VMap, "defer", fstate->func, nullptr, &DIFinder); +#endif + VMap[BB] = NewBB; + +#if LLVM_VERSION >= 40 + BasicBlock::iterator oldII = BB->begin(); +#endif + + for (BasicBlock::iterator II = NewBB->begin(), IE = NewBB->end(); II != IE; + ++II) { +#if LLVM_VERSION >= 40 + // HACK: LLVM is not happy about terra copying single isolated BasicBlocks, + // and will blow up when copying metadata. This removes the metadata before + // remapping, then copies it afterwards, which isn't fully correct, but works. + llvm::SmallVector, 4> MDs; + II->getAllMetadata(MDs); + for (auto md : MDs) II->setMetadata(md.first, nullptr); +#endif + RemapInstruction(&*II, VMap, #if LLVM_VERSION < 39 RF_IgnoreMissingEntries @@ -2605,6 +2663,12 @@ struct FunctionEmitter { RF_IgnoreMissingLocals #endif ); + +#if LLVM_VERSION >= 40 + II->copyMetadata(*oldII); + ++oldII; +#endif + } return NewBB; } // emit an exit path that includes the most recent num deferred statements diff --git a/src/tcwrapper.cpp b/src/tcwrapper.cpp index 52eb19bf..c231cc9e 100644 --- a/src/tcwrapper.cpp +++ b/src/tcwrapper.cpp @@ -1187,7 +1187,7 @@ int include_c(lua_State *L) { lua_pushvalue(L, -2); result.initFromStack(L, ref_table); - dofile(T, TT, code, &args[0], &args[args.size()], &result); + dofile(T, TT, code, &args.data()[0], &args.data()[args.size()], &result); } lobj_removereftable(L, ref_table); diff --git a/src/tdebug.cpp b/src/tdebug.cpp index e7e1ab2d..8846a513 100644 --- a/src/tdebug.cpp +++ b/src/tdebug.cpp @@ -13,23 +13,20 @@ #include #include #else -#define NOMINMAX -#include +#include "twindows.h" #include #include #endif using namespace llvm; -#ifdef DEBUG_INFO_WORKING static bool pointisbeforeinstruction(uintptr_t point, uintptr_t inst, bool isNextInst) { return point < inst || (!isNextInst && point == inst); } -#endif static bool stacktrace_findline(terra_CompilerState *C, const TerraFunctionInfo *fi, uintptr_t ip, bool isNextInstr, StringRef *file, size_t *lineno) { -#ifdef DEBUG_INFO_WORKING +#if LLVM_VERSION < 80 const std::vector &LineStarts = fi->efd.LineStarts; size_t i; @@ -42,14 +39,23 @@ static bool stacktrace_findline(terra_CompilerState *C, const TerraFunctionInfo if (i < LineStarts.size()) { if (lineno) *lineno = LineStarts[i].Loc.getLine(); +#if LLVM_VERSION >= 37 + // getFilename was just converting the 0th operand to a string + if (file && LineStarts[i].Loc.getScope()) { + if (auto *s = llvm::cast_or_null( + LineStarts[i].Loc.getScope()->getOperand(0))) + *file = s->getString(); + else + *file = StringRef(); + } +#else if (file) *file = DIFile(LineStarts[i].Loc.getScope(*fi->ctx)).getFilename(); +#endif return true; - } else { - return false; } -#else - return false; #endif + + return false; } static bool stacktrace_findsymbol(terra_CompilerState *C, uintptr_t ip, diff --git a/src/terra.cpp b/src/terra.cpp index 48ff3c96..1e528a61 100644 --- a/src/terra.cpp +++ b/src/terra.cpp @@ -20,9 +20,8 @@ #include #include #else -#define NOMINMAX +#include "twindows.h" #include -#include #include #include #include "MSVCSetupAPI.h" diff --git a/src/tllvmutil.cpp b/src/tllvmutil.cpp index 9158f262..f3487173 100644 --- a/src/tllvmutil.cpp +++ b/src/tllvmutil.cpp @@ -212,7 +212,12 @@ struct CopyConnectedComponent : public ValueMaterializer { CopyConnectedComponent(Module *dest_, Module *src_, llvmutil_Property copyGlobal_, void *data_, ValueToValueMapTy &VMap_) - : dest(dest_), src(src_), copyGlobal(copyGlobal_), data(data_), VMap(VMap_) {} + : dest(dest_), + src(src_), + copyGlobal(copyGlobal_), + data(data_), + VMap(VMap_), + DI(NULL) {} bool needsFreshlyNamedConstant(GlobalVariable *GV, GlobalVariable *newGV) { if (GV->isConstant() && GV->hasPrivateLinkage() && #if LLVM_VERSION < 39 @@ -228,11 +233,11 @@ struct CopyConnectedComponent : public ValueMaterializer { return false; } #if LLVM_VERSION == 38 - virtual Value *materializeDeclFor(Value *V) { + virtual Value *materializeDeclFor(Value *V) override { #elif LLVM_VERSION < 38 - virtual Value *materializeValueFor(Value *V) { + virtual Value *materializeValueFor(Value *V) override { #else - virtual Value *materialize(Value *V) { + virtual Value *materialize(Value *V) override { #endif if (Function *fn = dyn_cast(V)) { assert(fn->getParent() == src); @@ -241,8 +246,8 @@ struct CopyConnectedComponent : public ValueMaterializer { newfn = Function::Create(fn->getFunctionType(), fn->getLinkage(), fn->getName(), dest); newfn->copyAttributesFrom(fn); - newfn->setComdat( - fn->getComdat()); // copyAttributesFrom does not copy comdats + // copyAttributesFrom does not copy comdats + newfn->setComdat(fn->getComdat()); } if (!fn->isDeclaration() && newfn->isDeclaration() && copyGlobal(fn, data)) { for (Function::arg_iterator II = newfn->arg_begin(), I = fn->arg_begin(), @@ -254,6 +259,34 @@ struct CopyConnectedComponent : public ValueMaterializer { VMap[fn] = newfn; SmallVector Returns; CloneFunctionInto(newfn, fn, VMap, true, Returns, "", NULL, NULL, this); +#if LLVM_VERSION >= 37 + DISubprogram *SP = fn->getSubprogram(); + + Function *F = cast(MapValue(fn, VMap, RF_None, NULL, this)); + + if (SP) { + F->setSubprogram(DI->createFunction( +#if LLVM_VERSION >= 40 && LLVM_VERSION < 90 + SP->getScope().resolve(), SP->getName(), SP->getLinkageName(), +#else + SP->getScope(), SP->getName(), SP->getLinkageName(), +#endif + DI->createFile(SP->getFilename(), SP->getDirectory()), +#if LLVM_VERSION >= 80 + SP->getLine(), SP->getType(), SP->getScopeLine(), + SP->getFlags(), SP->getSPFlags(), SP->getTemplateParams(), +#else + SP->getLine(), SP->getType(), SP->isLocalToUnit(), + SP->isDefinition(), SP->getScopeLine(), SP->getFlags(), + SP->isOptimized(), SP->getTemplateParams(), +#endif +#if LLVM_VERSION >= 40 + SP->getDeclaration(), SP->getThrownTypes())); +#else + SP->getDeclaration())); +#endif + } +#endif } else { newfn->setLinkage(GlobalValue::ExternalLinkage); } @@ -266,6 +299,8 @@ struct CopyConnectedComponent : public ValueMaterializer { GV->getLinkage(), NULL, GV->getName(), NULL, GlobalVariable::NotThreadLocal, GV->getType()->getAddressSpace()); newGV->copyAttributesFrom(GV); + // copyAttributesFrom does not copy comdats + newGV->setComdat(GV->getComdat()); if (!GV->isDeclaration()) { if (!copyGlobal(GV, data)) { newGV->setLinkage(GlobalValue::ExternalLinkage); @@ -280,11 +315,13 @@ struct CopyConnectedComponent : public ValueMaterializer { } else return materializeValueForMetadata(V); } -#ifdef DEBUG_INFO_WORKING DIBuilder *DI; +#if LLVM_VERSION >= 37 + DICompileUnit *NCU; +#else DICompileUnit NCU; +#endif void CopyDebugMetadata() { - DI = NULL; if (NamedMDNode *NMD = src->getNamedMetadata("llvm.module.flags")) { NamedMDNode *New = dest->getOrInsertNamedMetadata(NMD->getName()); for (unsigned i = 0; i < NMD->getNumOperands(); i++) { @@ -295,16 +332,52 @@ struct CopyConnectedComponent : public ValueMaterializer { #endif } } + +#if LLVM_VERSION >= 40 + NCU = NULL; + // for (DICompileUnit *CU : src->debug_compile_units()) { + if (src->debug_compile_units_begin() != src->debug_compile_units_end()) { + DICompileUnit *CU = *src->debug_compile_units_begin(); + + if (DI == NULL) DI = new DIBuilder(*dest); + + if (CU) { + NCU = DI->createCompileUnit( + CU->getSourceLanguage(), CU->getFile(), CU->getProducer(), + CU->isOptimized(), CU->getFlags(), CU->getRuntimeVersion(), + CU->getSplitDebugFilename(), CU->getEmissionKind(), + CU->getDWOId(), CU->getSplitDebugInlining(), +#if LLVM_VERSION >= 80 + CU->getDebugInfoForProfiling(), CU->getNameTableKind(), + CU->getRangesBaseAddress()); +#elif LLVM_VERSION >= 60 + CU->getDebugInfoForProfiling(), CU->getGnuPubnames()); +#else + CU->getDebugInfoForProfiling()); +#endif + } + } +#elif LLVM_VERSION >= 37 + if (NamedMDNode *CUN = src->getNamedMetadata("llvm.dbg.cu")) { + DI = new DIBuilder(*dest); + DICompileUnit *CU = cast(CUN->getOperand(0)); + NCU = DI->createCompileUnit(CU->getSourceLanguage(), CU->getFilename(), + CU->getDirectory(), CU->getProducer(), + CU->isOptimized(), CU->getFlags(), + CU->getRuntimeVersion()); + } +#else if (NamedMDNode *CUN = src->getNamedMetadata("llvm.dbg.cu")) { DI = new DIBuilder(*dest); - DICompileUnit CU(CUN->getOperand(0)); NCU = DI->createCompileUnit(CU.getLanguage(), CU.getFilename(), CU.getDirectory(), CU.getProducer(), CU.isOptimized(), CU.getFlags(), CU.getRunTimeVersion()); } +#endif } + Value *materializeValueForMetadata(Value *V) { #if LLVM_VERSION <= 35 if (MDNode *MD = dyn_cast(V)) { @@ -314,18 +387,66 @@ struct CopyConnectedComponent : public ValueMaterializer { if (auto *MDV = dyn_cast(V)) { Metadata *MDraw = MDV->getMetadata(); MDNode *MD = dyn_cast(MDraw); +#if LLVM_VERSION >= 37 + DISubprogram *SP = getDISubprogram(MD); + if (MD != NULL && DI != NULL && SP != NULL) { +#else DISubprogram SP(MD); if (MD != NULL && DI != NULL && SP.isSubprogram()) { #endif +#endif +#if LLVM_VERSION >= 37 + { +#else if (Function *OF = SP.getFunction()) { Function *F = cast(MapValue(OF, VMap, RF_None, NULL, this)); +#endif + +#if LLVM_VERSION >= 37 +#if LLVM_VERSION >= 40 + // DISubprogram *NSP = SP; + DISubprogram *NSP = DI->createFunction( +#if LLVM_VERSION >= 40 && LLVM_VERSION < 90 + SP->getScope().resolve(), SP->getName(), SP->getLinkageName(), +#else + SP->getScope(), SP->getName(), SP->getLinkageName(), +#endif + DI->createFile(SP->getFilename(), SP->getDirectory()), +#if LLVM_VERSION >= 80 + SP->getLine(), SP->getType(), SP->getScopeLine(), + SP->getFlags(), SP->getSPFlags(), SP->getTemplateParams(), +#else + SP->getLine(), SP->getType(), SP->isLocalToUnit(), + SP->isDefinition(), SP->getScopeLine(), SP->getFlags(), + SP->isOptimized(), SP->getTemplateParams(), +#endif + SP->getDeclaration(), SP->getThrownTypes()); +#else + DISubprogram *NSP = DI->createFunction( + SP->getScope(), SP->getName(), SP->getLinkageName(), + DI->createFile(SP->getFilename(), SP->getDirectory()), + SP->getLine(), SP->getType(), SP->isLocalToUnit(), + SP->isDefinition(), SP->getScopeLine(), SP->getFlags(), + SP->isOptimized(), SP->getTemplateParams(), + SP->getDeclaration()); +#endif + + Function *newfn = dest->getFunction(SP->getName()); + if (!newfn) newfn = dest->getFunction(SP->getLinkageName()); + if (newfn) { + newfn->setSubprogram(NSP); + } + +#else DISubprogram NSP = DI->createFunction( SP.getContext(), SP.getName(), SP.getLinkageName(), DI->createFile(SP.getFilename(), SP.getDirectory()), SP.getLineNumber(), SP.getType(), SP.isLocalToUnit(), SP.isDefinition(), SP.getScopeLineNumber(), SP.getFlags(), SP.isOptimized(), F); +#endif + #if LLVM_VERSION <= 35 return NSP; #else @@ -342,13 +463,9 @@ struct CopyConnectedComponent : public ValueMaterializer { if (DI) { DI->finalize(); delete DI; + DI = NULL; } } -#else - void CopyDebugMetadata() {} - Value *materializeValueForMetadata(Value *V) { return NULL; } - void finalize() {} -#endif }; llvm::Module *llvmutil_extractmodulewithproperties( diff --git a/src/twindows.h b/src/twindows.h new file mode 100644 index 00000000..7ad548cc --- /dev/null +++ b/src/twindows.h @@ -0,0 +1,21 @@ +#ifndef twindows_h +#define twindows_h + +#pragma pack(push) +#pragma pack(8) +#define WINVER 0x0600 //_WIN32_WINNT_VISTA +#define _WIN32_WINNT 0x0600 +#define NTDDI_VERSION 0x06000100 // NTDDI_VISTASP1 +#define WIN32_LEAN_AND_MEAN +#ifndef NOMINMAX // Some compilers enable this by default +#define NOMINMAX +#endif +#define NODRAWTEXT +#define NOBITMAP +#define NOMCX +#define NOSERVICE +#define NOHELP +#include +#pragma pack(pop) + +#endif \ No newline at end of file