Let TargetABI fix up LLVM mangles for more special symbols

For ModuleInfos, ModuleRefs, InterfaceInfos, interface vtables & thunks.
This commit is contained in:
Martin 2017-09-29 22:38:11 +02:00
parent 0b28925e9a
commit c9a3be1295
12 changed files with 134 additions and 79 deletions

View file

@ -522,16 +522,16 @@ void DtoDeclareFunction(FuncDeclaration *fdecl) {
const auto link = forceC ? LINKc : f->linkage;
// mangled name
std::string mangledName = getMangledName(fdecl, link);
const auto llMangle = DtoMangledName(fdecl, link);
// construct function
LLFunctionType *functype = DtoFunctionType(fdecl);
LLFunction *func = vafunc ? vafunc : gIR->module.getFunction(mangledName);
LLFunction *func = vafunc ? vafunc : gIR->module.getFunction(llMangle);
if (!func) {
// All function declarations are "external" - any other linkage type
// is set when actually defining the function.
func = LLFunction::Create(functype, llvm::GlobalValue::ExternalLinkage,
mangledName, &gIR->module);
llMangle, &gIR->module);
} else if (func->getFunctionType() != functype) {
error(fdecl->loc,
"Function type does not match previously declared "

View file

@ -834,7 +834,7 @@ void DtoResolveVariable(VarDeclaration *vd) {
if (gIR->dmodule) {
vd->ir->setInitialized();
}
std::string llName(getMangledName(vd));
const auto llMangle = DtoMangledName(vd);
// Since the type of a global must exactly match the type of its
// initializer, we cannot know the type until after we have emitted the
@ -855,7 +855,7 @@ void DtoResolveVariable(VarDeclaration *vd) {
llvm::GlobalVariable *gvar =
getOrCreateGlobal(vd->loc, gIR->module, DtoMemType(vd->type), isLLConst,
linkage, nullptr, llName, vd->isThreadlocal());
linkage, nullptr, llMangle, vd->isThreadlocal());
getIrGlobal(vd)->value = gvar;
// Set the alignment (it is important not to use type->alignsize because

View file

@ -97,8 +97,8 @@ std::string hashSymbolName(llvm::StringRef name, Dsymbol *symb) {
}
}
std::string getMangledName(FuncDeclaration *fdecl, LINK link) {
std::string mangledName(mangleExact(fdecl));
std::string DtoMangledName(FuncDeclaration *fdecl, LINK link) {
std::string mangledName = mangleExact(fdecl);
// Hash the name if necessary
if (((link == LINKd) || (link == LINKdefault)) &&
@ -111,22 +111,30 @@ std::string getMangledName(FuncDeclaration *fdecl, LINK link) {
// TODO: Cache the result?
return gABI->mangleFunctionForLLVM(std::move(mangledName), link);
return DtoMangledFuncName(std::move(mangledName), link);
}
std::string getMangledName(VarDeclaration *vd) {
std::string DtoMangledName(VarDeclaration *vd) {
OutBuffer mangleBuf;
mangleToBuffer(vd, &mangleBuf);
// TODO: is hashing of variable names necessary?
// TODO: Cache the result?
OutBuffer mangleBuf;
mangleToBuffer(vd, &mangleBuf);
return DtoMangledVarName(mangleBuf.peekString(), vd->linkage);
}
return gABI->mangleVariableForLLVM(mangleBuf.peekString(), vd->linkage);
std::string DtoMangledFuncName(std::string baseMangle, LINK link) {
return gABI->mangleFunctionForLLVM(std::move(baseMangle), link);
}
std::string DtoMangledVarName(std::string baseMangle, LINK link) {
return gABI->mangleVariableForLLVM(std::move(baseMangle), link);
}
namespace {
std::string getMangledAggregateName(AggregateDeclaration *ad,
std::string DtoMangledAggregateName(AggregateDeclaration *ad,
const char *suffix) {
std::string ret = "_D";
@ -143,20 +151,41 @@ std::string getMangledAggregateName(AggregateDeclaration *ad,
if (suffix)
ret += suffix;
return gABI->mangleVariableForLLVM(std::move(ret), LINKd);
return DtoMangledVarName(std::move(ret), LINKd);
}
}
std::string getMangledInitSymbolName(AggregateDeclaration *aggrdecl) {
return getMangledAggregateName(aggrdecl, "6__initZ");
std::string DtoMangledInitSymbolName(AggregateDeclaration *aggrdecl) {
return DtoMangledAggregateName(aggrdecl, "6__initZ");
}
std::string getMangledVTableSymbolName(AggregateDeclaration *aggrdecl) {
return getMangledAggregateName(aggrdecl, "6__vtblZ");
std::string DtoMangledVTableSymbolName(AggregateDeclaration *aggrdecl) {
return DtoMangledAggregateName(aggrdecl, "6__vtblZ");
}
std::string getMangledClassInfoSymbolName(AggregateDeclaration *aggrdecl) {
std::string DtoMangledClassInfoSymbolName(AggregateDeclaration *aggrdecl) {
const char *suffix =
aggrdecl->isInterfaceDeclaration() ? "11__InterfaceZ" : "7__ClassZ";
return getMangledAggregateName(aggrdecl, suffix);
return DtoMangledAggregateName(aggrdecl, suffix);
}
std::string DtoMangledInterfaceInfosSymbolName(ClassDeclaration *cd) {
OutBuffer mangledName;
mangledName.writestring("_D");
mangleToBuffer(cd, &mangledName);
mangledName.writestring("16__interfaceInfosZ");
return DtoMangledVarName(mangledName.peekString(), LINKd);
}
std::string DtoMangledModuleInfoSymbolName(Module *module) {
OutBuffer mangledName;
mangledName.writestring("_D");
mangleToBuffer(module, &mangledName);
mangledName.writestring("12__ModuleInfoZ");
return DtoMangledVarName(mangledName.peekString(), LINKd);
}
std::string DtoMangledModuleRefSymbolName(const char *moduleMangle) {
return DtoMangledVarName(
(llvm::Twine("_D") + moduleMangle + "11__moduleRefZ").str(), LINKd);
}

View file

@ -18,14 +18,29 @@
#include "ddmd/globals.h"
class AggregateDeclaration;
class ClassDeclaration;
class FuncDeclaration;
class Module;
class VarDeclaration;
std::string getMangledName(FuncDeclaration *fdecl, LINK link);
std::string getMangledName(VarDeclaration *vd);
/*
* These functions return a symbol's LLVM mangle.
* LLVM's codegen performs target-specific postprocessing of these LLVM mangles
* (for the final object file mangles) unless the LLVM mangle starts with a 0x1
* byte. The TargetABI gets a chance to tweak the LLVM mangle.
*/
std::string getMangledInitSymbolName(AggregateDeclaration *aggrdecl);
std::string getMangledVTableSymbolName(AggregateDeclaration *aggrdecl);
std::string getMangledClassInfoSymbolName(AggregateDeclaration *aggrdecl);
std::string DtoMangledName(FuncDeclaration *fdecl, LINK link);
std::string DtoMangledName(VarDeclaration *vd);
std::string DtoMangledFuncName(std::string baseMangle, LINK link);
std::string DtoMangledVarName(std::string baseMangle, LINK link);
std::string DtoMangledInitSymbolName(AggregateDeclaration *aggrdecl);
std::string DtoMangledVTableSymbolName(AggregateDeclaration *aggrdecl);
std::string DtoMangledClassInfoSymbolName(AggregateDeclaration *aggrdecl);
std::string DtoMangledInterfaceInfosSymbolName(ClassDeclaration *cd);
std::string DtoMangledModuleInfoSymbolName(Module *module);
std::string DtoMangledModuleRefSymbolName(const char *moduleMangle);
#endif // LDC_GEN_MANGLING_H

View file

@ -14,6 +14,7 @@
#include "gen/irstate.h"
#include "gen/llvmhelpers.h"
#include "gen/logger.h"
#include "gen/mangling.h"
#include "gen/objcgen.h"
#include "gen/rttibuilder.h"
#include "ir/irfunction.h"
@ -56,10 +57,10 @@ llvm::Function *buildForwarderFunction(
const auto fnTy =
LLFunctionType::get(LLType::getVoidTy(gIR->context()), {}, false);
std::string const symbolName = gABI->mangleFunctionForLLVM(name, LINKd);
assert(gIR->module.getFunction(symbolName) == NULL);
const auto llMangle = DtoMangledFuncName(name, LINKd);
assert(gIR->module.getFunction(llMangle) == NULL);
llvm::Function *fn = llvm::Function::Create(
fnTy, llvm::GlobalValue::InternalLinkage, symbolName, &gIR->module);
fnTy, llvm::GlobalValue::InternalLinkage, llMangle, &gIR->module);
fn->setCallingConv(gABI->callingConv(LINKd));
// Emit the body, consisting of...

View file

@ -29,6 +29,7 @@
#include "gen/llvm.h"
#include "gen/llvmhelpers.h"
#include "gen/logger.h"
#include "gen/mangling.h"
#include "gen/moduleinfo.h"
#include "gen/optimizer.h"
#include "gen/runtime.h"
@ -145,8 +146,9 @@ LLFunction *build_module_reference_and_ctor(const char *moduleMangle,
// build a function that registers the moduleinfo in the global moduleinfo
// linked list
LLFunction *ctor = LLFunction::Create(fty, LLGlobalValue::InternalLinkage,
fname, &gIR->module);
LLFunction *ctor =
LLFunction::Create(fty, LLGlobalValue::InternalLinkage,
DtoMangledFuncName(fname, LINKd), &gIR->module);
// provide the default initializer
LLStructType *modulerefTy = DtoModuleReferenceType();
@ -158,20 +160,19 @@ LLFunction *build_module_reference_and_ctor(const char *moduleMangle,
modulerefTy, llvm::ArrayRef<LLConstant *>(mrefvalues));
// create the ModuleReference node for this module
std::string thismrefname = "_D";
thismrefname += moduleMangle;
thismrefname += "11__moduleRefZ";
const auto thismrefLLMangle = DtoMangledModuleRefSymbolName(moduleMangle);
Loc loc;
LLGlobalVariable *thismref = getOrCreateGlobal(
loc, gIR->module, modulerefTy, false, LLGlobalValue::InternalLinkage,
thismrefinit, thismrefname);
thismrefinit, thismrefLLMangle);
// make sure _Dmodule_ref is declared
LLConstant *mref = gIR->module.getNamedGlobal("_Dmodule_ref");
const auto mrefLLMangle = DtoMangledVarName("_Dmodule_ref", LINKc);
LLConstant *mref = gIR->module.getNamedGlobal(mrefLLMangle);
LLType *modulerefPtrTy = getPtrToType(modulerefTy);
if (!mref) {
mref = new LLGlobalVariable(gIR->module, modulerefPtrTy, false,
LLGlobalValue::ExternalLinkage, nullptr,
"_Dmodule_ref");
mrefLLMangle);
}
mref = DtoBitCast(mref, getPtrToType(modulerefPtrTy));
@ -342,14 +343,13 @@ void emitModuleRefToSection(RegistryStyle style, std::string moduleMangle,
llvm::Type *const moduleInfoPtrTy = DtoPtrToType(Module::moduleinfo->type);
std::string thismrefname = "_D";
thismrefname += moduleMangle;
thismrefname += "11__moduleRefZ";
const auto thismrefLLMangle =
DtoMangledModuleRefSymbolName(moduleMangle.c_str());
auto thismref = new llvm::GlobalVariable(
gIR->module, moduleInfoPtrTy,
false, // FIXME: mRelocModel != llvm::Reloc::PIC_
llvm::GlobalValue::LinkOnceODRLinkage,
DtoBitCast(thisModuleInfo, moduleInfoPtrTy), thismrefname);
DtoBitCast(thisModuleInfo, moduleInfoPtrTy), thismrefLLMangle);
thismref->setSection((style == RegistryStyle::sectionDarwin) ? "__DATA,.minfo"
: "__minfo");
gIR->usedArray.push_back(thismref);
@ -544,8 +544,9 @@ void addCoverageAnalysis(Module *m) {
LLFunctionType *ctorTy =
LLFunctionType::get(LLType::getVoidTy(gIR->context()), {}, false);
ctor = LLFunction::Create(ctorTy, LLGlobalValue::InternalLinkage, ctorname,
&gIR->module);
ctor =
LLFunction::Create(ctorTy, LLGlobalValue::InternalLinkage,
DtoMangledFuncName(ctorname, LINKd), &gIR->module);
ctor->setCallingConv(gABI->callingConv(LINKd));
// Set function attributes. See functions.cpp:DtoDefineFunction()
if (global.params.targetTriple->getArch() == llvm::Triple::x86_64) {

View file

@ -17,6 +17,7 @@
#include "gen/llvm.h"
#include "gen/llvmhelpers.h"
#include "gen/logger.h"
#include "gen/mangling.h"
#include "gen/tollvm.h"
#include "ir/irfunction.h"
#include "ir/irtype.h"
@ -707,9 +708,9 @@ static void buildRuntimeModule() {
//////////////////////////////////////////////////////////////////////////////
// void invariant._d_invariant(Object o)
createFwdDecl(LINKd, voidTy,
{gABI->mangleFunctionForLLVM(
"_D9invariant12_d_invariantFC6ObjectZv", LINKd)},
createFwdDecl(
LINKd, voidTy,
{DtoMangledFuncName("_D9invariant12_d_invariantFC6ObjectZv", LINKd)},
{objectTy});
// void _d_dso_registry(void* data)

View file

@ -24,6 +24,7 @@
#include "gen/llvm.h"
#include "gen/llvmhelpers.h"
#include "gen/logger.h"
#include "gen/mangling.h"
#include "gen/nested.h"
#include "gen/optimizer.h"
#include "gen/pragma.h"
@ -1687,8 +1688,8 @@ public:
Logger::println("calling class invariant");
const auto fnMangle = gABI->mangleFunctionForLLVM(
"_D9invariant12_d_invariantFC6ObjectZv", LINKd);
const auto fnMangle =
DtoMangledFuncName("_D9invariant12_d_invariantFC6ObjectZv", LINKd);
const auto fn = getRuntimeFunction(e->loc, gIR->module, fnMangle.c_str());
const auto arg =

View file

@ -42,6 +42,7 @@
#include "gen/llvm.h"
#include "gen/llvmhelpers.h"
#include "gen/logger.h"
#include "gen/mangling.h"
#include "gen/metadata.h"
#include "gen/rttibuilder.h"
#include "gen/runtime.h"
@ -592,10 +593,11 @@ void TypeInfoDeclaration_codegen(TypeInfoDeclaration *decl, IRState *p) {
Logger::println("typeinfo mangle: %s", mangled);
}
const auto llMangle = DtoMangledVarName(mangled, LINKd);
IrGlobal *irg = getIrGlobal(decl, true);
const LinkageWithCOMDAT lwc(LLGlobalValue::ExternalLinkage, false);
irg->value = gIR->module.getGlobalVariable(mangled);
irg->value = gIR->module.getGlobalVariable(llMangle);
if (irg->value) {
assert(irg->getType()->isStructTy());
} else {
@ -610,7 +612,7 @@ void TypeInfoDeclaration_codegen(TypeInfoDeclaration *decl, IRState *p) {
// as immutable on the D side, and e.g. synchronized() can be used on the
// implicit monitor.
auto g = new LLGlobalVariable(gIR->module, type, false, lwc.first,
nullptr, mangled);
nullptr, llMangle);
setLinkage(lwc, g);
irg->value = g;
}

View file

@ -47,11 +47,11 @@ LLConstant *&IrAggr::getInitSymbol() {
}
// create the initZ symbol
auto initname = getMangledInitSymbolName(aggrdecl);
const auto llMangle = DtoMangledInitSymbolName(aggrdecl);
auto initGlobal =
getOrCreateGlobal(aggrdecl->loc, gIR->module, getLLStructType(), true,
llvm::GlobalValue::ExternalLinkage, nullptr, initname);
llvm::GlobalValue::ExternalLinkage, nullptr, llMangle);
initGlobal->setAlignment(DtoAlignment(type));
init = initGlobal;

View file

@ -47,13 +47,13 @@ LLGlobalVariable *IrAggr::getVtblSymbol() {
}
// create the vtblZ symbol
auto initname = getMangledVTableSymbolName(aggrdecl);
const auto llMangle = DtoMangledVTableSymbolName(aggrdecl);
LLType *vtblTy = stripModifiers(type)->ctype->isClass()->getVtblType();
vtbl =
getOrCreateGlobal(aggrdecl->loc, gIR->module, vtblTy, true,
llvm::GlobalValue::ExternalLinkage, nullptr, initname);
llvm::GlobalValue::ExternalLinkage, nullptr, llMangle);
return vtbl;
}
@ -66,7 +66,7 @@ LLGlobalVariable *IrAggr::getClassInfoSymbol() {
}
// create the ClassZ / InterfaceZ symbol
std::string initname = getMangledClassInfoSymbolName(aggrdecl);
const auto llMangle = DtoMangledClassInfoSymbolName(aggrdecl);
// The type is also ClassInfo for interfaces the actual TypeInfo for them
// is a TypeInfo_Interface instance that references __ClassZ in its "base"
@ -79,7 +79,7 @@ LLGlobalVariable *IrAggr::getClassInfoSymbol() {
// classinfos cannot be constants since they're used as locks for synchronized
classInfo = getOrCreateGlobal(
aggrdecl->loc, gIR->module, tc->getMemoryLLType(), false,
llvm::GlobalValue::ExternalLinkage, nullptr, initname);
llvm::GlobalValue::ExternalLinkage, nullptr, llMangle);
// Generate some metadata on this ClassInfo if it's for a class.
ClassDeclaration *classdecl = aggrdecl->isClassDeclaration();
@ -98,9 +98,15 @@ LLGlobalVariable *IrAggr::getClassInfoSymbol() {
mdVals[CD_CustomDelete] = llvm::ConstantAsMetadata::get(
LLConstantInt::get(LLType::getInt1Ty(gIR->context()), hasCustomDelete));
// Construct the metadata and insert it into the module.
llvm::SmallString<64> name;
llvm::NamedMDNode *node = gIR->module.getOrInsertNamedMetadata(
llvm::Twine(CD_PREFIX, initname).toStringRef(name));
OutBuffer debugName;
debugName.writestring(CD_PREFIX);
if (llMangle[0] == '\1') {
debugName.write(llMangle.data() + 1, llMangle.length() - 1);
} else {
debugName.write(llMangle.data(), llMangle.length());
}
llvm::NamedMDNode *node =
gIR->module.getOrInsertNamedMetadata(debugName.peekString());
node->addOperand(llvm::MDNode::get(
gIR->context(), llvm::makeArrayRef(mdVals, CD_NumFields)));
}
@ -124,19 +130,15 @@ LLGlobalVariable *IrAggr::getInterfaceArraySymbol() {
LLType *InterfaceTy = DtoType(Type::typeinfoclass->fields[3]->type->nextOf());
// create Interface[N]
const auto llMangle = DtoMangledInterfaceInfosSymbolName(cd);
LLArrayType *array_type = llvm::ArrayType::get(InterfaceTy, n);
// put it in a global
OutBuffer mangledName;
mangledName.writestring("_D");
mangleToBuffer(cd, &mangledName);
mangledName.writestring("16__interfaceInfosZ");
// We keep this as external for now and only consider template linkage if
// we emit the initializer later.
classInterfacesArray = getOrCreateGlobal(
cd->loc, gIR->module, array_type, true,
llvm::GlobalValue::ExternalLinkage, nullptr, mangledName.peekString());
// We keep the global as external for now and only consider template linkage
// if we emit the initializer later.
classInterfacesArray =
getOrCreateGlobal(cd->loc, gIR->module, array_type, true,
llvm::GlobalValue::ExternalLinkage, nullptr, llMangle);
return classInterfacesArray;
}
@ -329,15 +331,18 @@ llvm::GlobalVariable *IrAggr::getInterfaceVtbl(BaseClass *b, bool new_instance,
nameBuf.write(mangledTargetName, 2);
nameBuf.writestring(thunkPrefix);
nameBuf.writestring(mangledTargetName + 2);
const char *thunkName = nameBuf.extractString();
llvm::Function *thunk = gIR->module.getFunction(thunkName);
const auto thunkLLMangle =
DtoMangledFuncName(nameBuf.peekString(), fd->linkage);
llvm::Function *thunk = gIR->module.getFunction(thunkLLMangle);
if (!thunk) {
const LinkageWithCOMDAT lwc(LLGlobalValue::LinkOnceODRLinkage,
supportsCOMDAT());
const auto callee = irFunc->getLLVMCallee();
thunk = LLFunction::Create(
isaFunction(callee->getType()->getContainedType(0)), lwc.first,
thunkName, &gIR->module);
thunkLLMangle, &gIR->module);
setLinkage(lwc, thunk);
thunk->copyAttributesFrom(callee);
@ -440,10 +445,12 @@ llvm::GlobalVariable *IrAggr::getInterfaceVtbl(BaseClass *b, bool new_instance,
mangledName.writestring(thunkPrefix);
mangledName.writestring("6__vtblZ");
const auto llMangle = DtoMangledVarName(mangledName.peekString(), LINKd);
const auto lwc = DtoLinkage(cd);
LLGlobalVariable *GV =
getOrCreateGlobal(cd->loc, gIR->module, vtbl_constant->getType(), true,
lwc.first, vtbl_constant, mangledName.peekString());
lwc.first, vtbl_constant, llMangle);
setLinkage(lwc, GV);
// insert into the vtbl map

View file

@ -11,6 +11,7 @@
#include "module.h"
#include "gen/llvm.h"
#include "gen/irstate.h"
#include "gen/mangling.h"
#include "gen/tollvm.h"
#include "ir/irdsymbol.h"
#include "ir/irfunction.h"
@ -22,14 +23,11 @@ llvm::GlobalVariable *IrModule::moduleInfoSymbol() {
return moduleInfoVar;
}
OutBuffer mangledName;
mangledName.writestring("_D");
mangleToBuffer(M, &mangledName);
mangledName.writestring("12__ModuleInfoZ");
const auto llMangle = DtoMangledModuleInfoSymbolName(M);
moduleInfoVar = new llvm::GlobalVariable(
gIR->module, llvm::StructType::create(gIR->context()), false,
llvm::GlobalValue::ExternalLinkage, nullptr, mangledName.peekString());
llvm::GlobalValue::ExternalLinkage, nullptr, llMangle);
return moduleInfoVar;
}