mirror of
https://github.com/ldc-developers/ldc.git
synced 2025-05-01 23:50:43 +03:00
Use new functions appendToGlobalCtors() and appendToGlobalDtors.
This simplifies the code in module.cpp a bit. But it is also the base to implement a pragma to place an arbitrary function in llvm.global_ctors and llvm.global_dtors.
This commit is contained in:
parent
e78ff5a131
commit
d57eaa49ae
5 changed files with 144 additions and 91 deletions
|
@ -1,83 +1,129 @@
|
||||||
#include "gen/llvmcompat.h"
|
#include "gen/llvmcompat.h"
|
||||||
#include "llvm/Config/llvm-config.h"
|
#include "llvm/Config/llvm-config.h"
|
||||||
#include "llvm/ADT/Triple.h"
|
#include "llvm/ADT/Triple.h"
|
||||||
|
#include "llvm/DerivedTypes.h"
|
||||||
|
#include "llvm/Function.h"
|
||||||
|
#include "llvm/Module.h"
|
||||||
|
#include "llvm/Support/IRBuilder.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#if LDC_LLVM_VER == 300
|
#if LDC_LLVM_VER == 300
|
||||||
namespace llvm {
|
using namespace llvm;
|
||||||
namespace sys {
|
|
||||||
std::string getDefaultTargetTriple() {
|
|
||||||
return LLVM_HOSTTRIPLE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Triple Triple__get32BitArchVariant(const std::string& triple) {
|
|
||||||
Triple T(triple);
|
|
||||||
switch (T.getArch()) {
|
|
||||||
case Triple::UnknownArch:
|
|
||||||
case Triple::msp430:
|
|
||||||
T.setArch(Triple::UnknownArch);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case Triple::amdil:
|
|
||||||
case Triple::arm:
|
|
||||||
case Triple::cellspu:
|
|
||||||
case Triple::le32:
|
|
||||||
case Triple::mblaze:
|
|
||||||
case Triple::mips:
|
|
||||||
case Triple::mipsel:
|
|
||||||
case Triple::ppc:
|
|
||||||
case Triple::sparc:
|
|
||||||
case Triple::tce:
|
|
||||||
case Triple::thumb:
|
|
||||||
case Triple::x86:
|
|
||||||
case Triple::xcore:
|
|
||||||
// Already 32-bit.
|
|
||||||
break;
|
|
||||||
|
|
||||||
case Triple::mips64: T.setArch(Triple::mips); break;
|
|
||||||
case Triple::mips64el: T.setArch(Triple::mipsel); break;
|
|
||||||
case Triple::ppc64: T.setArch(Triple::ppc); break;
|
|
||||||
case Triple::sparcv9: T.setArch(Triple::sparc); break;
|
|
||||||
case Triple::x86_64: T.setArch(Triple::x86); break;
|
|
||||||
}
|
|
||||||
return T;
|
|
||||||
}
|
|
||||||
|
|
||||||
Triple Triple__get64BitArchVariant(const std::string& triple) {
|
|
||||||
Triple T(triple);
|
|
||||||
switch (T.getArch()) {
|
|
||||||
case Triple::UnknownArch:
|
|
||||||
case Triple::amdil:
|
|
||||||
case Triple::arm:
|
|
||||||
case Triple::cellspu:
|
|
||||||
case Triple::le32:
|
|
||||||
case Triple::mblaze:
|
|
||||||
case Triple::msp430:
|
|
||||||
case Triple::tce:
|
|
||||||
case Triple::thumb:
|
|
||||||
case Triple::xcore:
|
|
||||||
T.setArch(Triple::UnknownArch);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case Triple::mips64:
|
|
||||||
case Triple::mips64el:
|
|
||||||
case Triple::ppc64:
|
|
||||||
case Triple::sparcv9:
|
|
||||||
case Triple::x86_64:
|
|
||||||
// Already 64-bit.
|
|
||||||
break;
|
|
||||||
|
|
||||||
case Triple::mips: T.setArch(Triple::mips64); break;
|
|
||||||
case Triple::mipsel: T.setArch(Triple::mips64el); break;
|
|
||||||
case Triple::ppc: T.setArch(Triple::ppc64); break;
|
|
||||||
case Triple::sparc: T.setArch(Triple::sparcv9); break;
|
|
||||||
case Triple::x86: T.setArch(Triple::x86_64); break;
|
|
||||||
}
|
|
||||||
return T;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
std::string llvm::sys::getDefaultTargetTriple() {
|
||||||
|
return LLVM_HOSTTRIPLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Triple llvm::Triple__get32BitArchVariant(const std::string& triple) {
|
||||||
|
Triple T(triple);
|
||||||
|
switch (T.getArch()) {
|
||||||
|
case Triple::UnknownArch:
|
||||||
|
case Triple::msp430:
|
||||||
|
T.setArch(Triple::UnknownArch);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Triple::amdil:
|
||||||
|
case Triple::arm:
|
||||||
|
case Triple::cellspu:
|
||||||
|
case Triple::le32:
|
||||||
|
case Triple::mblaze:
|
||||||
|
case Triple::mips:
|
||||||
|
case Triple::mipsel:
|
||||||
|
case Triple::ppc:
|
||||||
|
case Triple::sparc:
|
||||||
|
case Triple::tce:
|
||||||
|
case Triple::thumb:
|
||||||
|
case Triple::x86:
|
||||||
|
case Triple::xcore:
|
||||||
|
// Already 32-bit.
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Triple::mips64: T.setArch(Triple::mips); break;
|
||||||
|
case Triple::mips64el: T.setArch(Triple::mipsel); break;
|
||||||
|
case Triple::ppc64: T.setArch(Triple::ppc); break;
|
||||||
|
case Triple::sparcv9: T.setArch(Triple::sparc); break;
|
||||||
|
case Triple::x86_64: T.setArch(Triple::x86); break;
|
||||||
|
}
|
||||||
|
return T;
|
||||||
|
}
|
||||||
|
|
||||||
|
Triple llvm::Triple__get64BitArchVariant(const std::string& triple) {
|
||||||
|
Triple T(triple);
|
||||||
|
switch (T.getArch()) {
|
||||||
|
case Triple::UnknownArch:
|
||||||
|
case Triple::amdil:
|
||||||
|
case Triple::arm:
|
||||||
|
case Triple::cellspu:
|
||||||
|
case Triple::le32:
|
||||||
|
case Triple::mblaze:
|
||||||
|
case Triple::msp430:
|
||||||
|
case Triple::tce:
|
||||||
|
case Triple::thumb:
|
||||||
|
case Triple::xcore:
|
||||||
|
T.setArch(Triple::UnknownArch);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Triple::mips64:
|
||||||
|
case Triple::mips64el:
|
||||||
|
case Triple::ppc64:
|
||||||
|
case Triple::sparcv9:
|
||||||
|
case Triple::x86_64:
|
||||||
|
// Already 64-bit.
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Triple::mips: T.setArch(Triple::mips64); break;
|
||||||
|
case Triple::mipsel: T.setArch(Triple::mips64el); break;
|
||||||
|
case Triple::ppc: T.setArch(Triple::ppc64); break;
|
||||||
|
case Triple::sparc: T.setArch(Triple::sparcv9); break;
|
||||||
|
case Triple::x86: T.setArch(Triple::x86_64); break;
|
||||||
|
}
|
||||||
|
return T;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void appendToGlobalArray(const char *Array,
|
||||||
|
Module &M, Function *F, int Priority) {
|
||||||
|
IRBuilder<> IRB(M.getContext());
|
||||||
|
FunctionType *FnTy = FunctionType::get(IRB.getVoidTy(), false);
|
||||||
|
StructType *Ty = StructType::get(
|
||||||
|
IRB.getInt32Ty(), PointerType::getUnqual(FnTy), NULL);
|
||||||
|
|
||||||
|
Constant *RuntimeCtorInit = ConstantStruct::get(
|
||||||
|
Ty, IRB.getInt32(Priority), F, NULL);
|
||||||
|
|
||||||
|
// Get the current set of static global constructors and add the new ctor
|
||||||
|
// to the list.
|
||||||
|
SmallVector<Constant *, 16> CurrentCtors;
|
||||||
|
if (GlobalVariable * GVCtor = M.getNamedGlobal(Array)) {
|
||||||
|
if (Constant *Init = GVCtor->getInitializer()) {
|
||||||
|
unsigned n = Init->getNumOperands();
|
||||||
|
CurrentCtors.reserve(n + 1);
|
||||||
|
for (unsigned i = 0; i != n; ++i)
|
||||||
|
CurrentCtors.push_back(cast<Constant>(Init->getOperand(i)));
|
||||||
|
}
|
||||||
|
GVCtor->eraseFromParent();
|
||||||
|
}
|
||||||
|
|
||||||
|
CurrentCtors.push_back(RuntimeCtorInit);
|
||||||
|
|
||||||
|
// Create a new initializer.
|
||||||
|
ArrayType *AT = ArrayType::get(RuntimeCtorInit->getType(),
|
||||||
|
CurrentCtors.size());
|
||||||
|
Constant *NewInit = ConstantArray::get(AT, CurrentCtors);
|
||||||
|
|
||||||
|
// Create the new global variable and replace all uses of
|
||||||
|
// the old global variable with the new one.
|
||||||
|
(void)new GlobalVariable(M, NewInit->getType(), false,
|
||||||
|
GlobalValue::AppendingLinkage, NewInit, Array);
|
||||||
|
}
|
||||||
|
|
||||||
|
void llvm::appendToGlobalCtors(Module &M, Function *F, int Priority) {
|
||||||
|
appendToGlobalArray("llvm.global_ctors", M, F, Priority);
|
||||||
|
}
|
||||||
|
|
||||||
|
void llvm::appendToGlobalDtors(Module &M, Function *F, int Priority) {
|
||||||
|
appendToGlobalArray("llvm.global_dtors", M, F, Priority);
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -14,12 +14,19 @@
|
||||||
|
|
||||||
#if LDC_LLVM_VER == 300
|
#if LDC_LLVM_VER == 300
|
||||||
namespace llvm {
|
namespace llvm {
|
||||||
|
class Module;
|
||||||
|
class Function;
|
||||||
|
|
||||||
namespace sys {
|
namespace sys {
|
||||||
std::string getDefaultTargetTriple();
|
std::string getDefaultTargetTriple();
|
||||||
}
|
}
|
||||||
|
|
||||||
Triple Triple__get32BitArchVariant(const std::string&_this);
|
Triple Triple__get32BitArchVariant(const std::string&_this);
|
||||||
Triple Triple__get64BitArchVariant(const std::string& _this);
|
Triple Triple__get64BitArchVariant(const std::string& _this);
|
||||||
|
|
||||||
|
// From Transforms/Utils/ModuleUtils
|
||||||
|
void appendToGlobalCtors(Module &M, Function *F, int Priority);
|
||||||
|
void appendToGlobalDtors(Module &M, Function *F, int Priority);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,9 @@
|
||||||
|
|
||||||
#include "llvm/MC/MCAsmInfo.h"
|
#include "llvm/MC/MCAsmInfo.h"
|
||||||
#include "llvm/Target/TargetMachine.h"
|
#include "llvm/Target/TargetMachine.h"
|
||||||
|
#if LDC_LLVM_VER >= 301
|
||||||
|
#include "llvm/Transforms/Utils/ModuleUtils.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "gen/tollvm.h"
|
#include "gen/tollvm.h"
|
||||||
#include "gen/irstate.h"
|
#include "gen/irstate.h"
|
||||||
|
@ -24,6 +27,7 @@
|
||||||
#include "gen/todebug.h"
|
#include "gen/todebug.h"
|
||||||
#include "gen/nested.h"
|
#include "gen/nested.h"
|
||||||
#include "ir/irmodule.h"
|
#include "ir/irmodule.h"
|
||||||
|
#include "gen/llvmcompat.h"
|
||||||
|
|
||||||
#include <stack>
|
#include <stack>
|
||||||
|
|
||||||
|
@ -1837,3 +1841,15 @@ void printLabelName(std::ostream& target, const char* func_mangle, const char* l
|
||||||
target << gTargetMachine->getMCAsmInfo()->getPrivateGlobalPrefix() <<
|
target << gTargetMachine->getMCAsmInfo()->getPrivateGlobalPrefix() <<
|
||||||
func_mangle << "_" << label_name;
|
func_mangle << "_" << label_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// CTOR and DTOR
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
void AppendFunctionToLLVMGlobalCtorsDtors(llvm::Function* func, const uint32_t priority, const bool isCtor)
|
||||||
|
{
|
||||||
|
if (isCtor)
|
||||||
|
llvm::appendToGlobalCtors(*gIR->module, func, priority);
|
||||||
|
else
|
||||||
|
llvm::appendToGlobalDtors(*gIR->module, func, priority);
|
||||||
|
}
|
||||||
|
|
|
@ -196,4 +196,6 @@ Type* stripModifiers(Type* type);
|
||||||
|
|
||||||
void printLabelName(std::ostream& target, const char* func_mangle, const char* label_name);
|
void printLabelName(std::ostream& target, const char* func_mangle, const char* label_name);
|
||||||
|
|
||||||
|
void AppendFunctionToLLVMGlobalCtorsDtors(llvm::Function* func, const uint32_t priority, const bool isCtor);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -645,23 +645,5 @@ void Module::genmoduleinfo()
|
||||||
// build the modulereference and ctor for registering it
|
// build the modulereference and ctor for registering it
|
||||||
LLFunction* mictor = build_module_reference_and_ctor(moduleInfoSymbol());
|
LLFunction* mictor = build_module_reference_and_ctor(moduleInfoSymbol());
|
||||||
|
|
||||||
// register this ctor in the magic llvm.global_ctors appending array
|
AppendFunctionToLLVMGlobalCtorsDtors(mictor, 65535, true);
|
||||||
LLFunctionType* magicfty = LLFunctionType::get(LLType::getVoidTy(gIR->context()), std::vector<LLType*>(), false);
|
|
||||||
std::vector<LLType*> magictypes;
|
|
||||||
magictypes.push_back(LLType::getInt32Ty(gIR->context()));
|
|
||||||
magictypes.push_back(getPtrToType(magicfty));
|
|
||||||
LLStructType* magicsty = LLStructType::get(gIR->context(), magictypes);
|
|
||||||
|
|
||||||
// make the constant element
|
|
||||||
std::vector<LLConstant*> magicconstants;
|
|
||||||
magicconstants.push_back(DtoConstUint(65535));
|
|
||||||
magicconstants.push_back(mictor);
|
|
||||||
LLConstant* magicinit = LLConstantStruct::get(magicsty, magicconstants);
|
|
||||||
|
|
||||||
// declare the appending array
|
|
||||||
llvm::ArrayType* appendArrTy = llvm::ArrayType::get(magicsty, 1);
|
|
||||||
std::vector<LLConstant*> appendInits(1, magicinit);
|
|
||||||
LLConstant* appendInit = LLConstantArray::get(appendArrTy, appendInits);
|
|
||||||
std::string appendName("llvm.global_ctors");
|
|
||||||
new llvm::GlobalVariable(*gIR->module, appendArrTy, true, llvm::GlobalValue::AppendingLinkage, appendInit, appendName);
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue