Refactoring: Improve Ir{Aggr,Class,Struct} encapsulation

This commit is contained in:
Martin Kinkelin 2020-07-18 12:20:33 +02:00
parent a765bf8901
commit e9021fd6c8
6 changed files with 122 additions and 112 deletions

View file

@ -107,8 +107,7 @@ public:
// Emit TypeInfo.
IrClass *ir = getIrAggr(decl);
if (!ir->suppressTypeInfo()) {
llvm::GlobalVariable *interfaceZ = ir->getClassInfoSymbol();
defineGlobal(interfaceZ, ir->getClassInfoInit(), decl);
ir->getClassInfoSymbol(/*define=*/true);
}
}
@ -148,10 +147,7 @@ public:
// Define the __initZ symbol.
if (!decl->zeroInit) {
auto &initZ = ir->getInitSymbol();
auto initGlobal = llvm::cast<LLGlobalVariable>(initZ);
initZ = irs->setGlobalVarInitializer(initGlobal, ir->getDefaultInit());
setLinkageAndVisibility(decl, initGlobal);
ir->getInitSymbol(/*define=*/true);
}
// Emit special __xopEquals/__xopCmp/__xtoHash member functions required
@ -204,20 +200,15 @@ public:
IrClass *ir = getIrAggr(decl);
auto &initZ = ir->getInitSymbol();
auto initGlobal = llvm::cast<LLGlobalVariable>(initZ);
initZ = irs->setGlobalVarInitializer(initGlobal, ir->getDefaultInit());
setLinkageAndVisibility(decl, initGlobal);
ir->getInitSymbol(/*define=*/true);
llvm::GlobalVariable *vtbl = ir->getVtblSymbol();
defineGlobal(vtbl, ir->getVtblInit(), decl);
ir->getVtblSymbol(/*define*/true);
ir->defineInterfaceVtbls();
// Emit TypeInfo.
if (!ir->suppressTypeInfo()) {
llvm::GlobalVariable *classZ = ir->getClassInfoSymbol();
defineGlobal(classZ, ir->getClassInfoInit(), decl);
ir->getClassInfoSymbol(/*define=*/true);
}
}

View file

@ -487,13 +487,9 @@ class DeclareOrDefineVisitor : public Visitor {
return;
}
LLConstant *init = irstruct->getTypeInfoInit(); // might define ti!
if (!ti->hasInitializer()) {
defineGlobal(ti, init, sd);
irstruct->getTypeInfoSymbol(/*define=*/true);
ti->setLinkage(TYPEINFO_LINKAGE_TYPE); // override
}
}
// Only declare class TypeInfos. They are defined once in their owning module
// as part of ClassDeclaration codegen.

View file

@ -51,19 +51,26 @@ bool IrAggr::suppressTypeInfo() const {
//////////////////////////////////////////////////////////////////////////////
LLConstant *&IrAggr::getInitSymbol() {
if (init) {
return init;
}
// create the initZ symbol
LLConstant *IrAggr::getInitSymbol(bool define) {
if (!init) {
const auto irMangle = getIRMangledInitSymbolName(aggrdecl);
auto initGlobal = declareGlobal(aggrdecl->loc, gIR->module, getLLStructType(),
irMangle, /*isConstant=*/true);
auto initGlobal =
declareGlobal(aggrdecl->loc, gIR->module, getLLStructType(), irMangle,
/*isConstant=*/true);
initGlobal->setAlignment(LLMaybeAlign(DtoAlignment(type)));
init = initGlobal;
}
if (define) {
auto initConstant = getDefaultInit();
auto initGlobal = llvm::dyn_cast<LLGlobalVariable>(init);
if (initGlobal // NOT a bitcast pointer to helper global
&& !initGlobal->hasInitializer()) {
init = gIR->setGlobalVarInitializer(initGlobal, initConstant, aggrdecl);
}
}
return init;
}

View file

@ -58,7 +58,7 @@ public:
virtual ~IrAggr() = default;
/// Creates the __initZ symbol lazily.
llvm::Constant *&getInitSymbol();
llvm::Constant *getInitSymbol(bool define = false);
/// Builds the __initZ initializer constant lazily.
llvm::Constant *getDefaultInit();
@ -112,7 +112,9 @@ public:
explicit IrStruct(StructDeclaration *sd) : IrAggr(sd) {}
/// Creates the TypeInfo_Struct symbol lazily.
llvm::GlobalVariable *getTypeInfoSymbol();
llvm::GlobalVariable *getTypeInfoSymbol(bool define = false);
private:
/// Builds the TypeInfo_Struct initializer constant lazily.
llvm::Constant *getTypeInfoInit();
};
@ -123,21 +125,14 @@ public:
explicit IrClass(ClassDeclaration *cd) : IrAggr(cd) {}
/// Creates the __ClassZ/__InterfaceZ symbol lazily.
llvm::GlobalVariable *getClassInfoSymbol();
/// Builds the __ClassZ/__InterfaceZ initializer constant lazily.
llvm::Constant *getClassInfoInit();
llvm::GlobalVariable *getClassInfoSymbol(bool define = false);
/// Creates the __vtblZ symbol lazily.
llvm::GlobalVariable *getVtblSymbol();
/// Builds the __vtblZ initializer constant lazily.
llvm::Constant *getVtblInit();
llvm::GlobalVariable *getVtblSymbol(bool define = false);
/// Defines all interface vtbls.
void defineInterfaceVtbls();
/// Creates the __interfaceInfos symbol lazily.
llvm::GlobalVariable *getInterfaceArraySymbol();
/// Initialize interface.
void initializeInterface();
@ -162,6 +157,12 @@ private:
/// Corresponds to the Interface instances needed to be output.
std::vector<BaseClass *> interfacesWithVtbls;
/// Builds the __ClassZ/__InterfaceZ initializer constant lazily.
llvm::Constant *getClassInfoInit();
/// Builds the __vtblZ initializer constant lazily.
llvm::Constant *getVtblInit();
/// Returns the vtbl for an interface implementation.
llvm::GlobalVariable *getInterfaceVtblSymbol(BaseClass *b,
size_t interfaces_index);
@ -169,6 +170,9 @@ private:
void defineInterfaceVtbl(BaseClass *b, bool new_inst,
size_t interfaces_index);
/// Creates the __interfaceInfos symbol lazily.
llvm::GlobalVariable *getInterfaceArraySymbol();
/// Create the Interface[] interfaces ClassInfo field initializer.
llvm::Constant *getClassInfoInterfaces();

View file

@ -41,30 +41,29 @@
//////////////////////////////////////////////////////////////////////////////
LLGlobalVariable *IrClass::getVtblSymbol() {
if (vtbl) {
return vtbl;
}
// create the vtblZ symbol
LLGlobalVariable *IrClass::getVtblSymbol(bool define) {
if (!vtbl) {
const auto irMangle = getIRMangledVTableSymbolName(aggrdecl);
LLType *vtblTy = stripModifiers(type)->ctype->isClass()->getVtblType();
vtbl = declareGlobal(aggrdecl->loc, gIR->module, vtblTy, irMangle,
/*isConstant=*/true);
}
if (define) {
auto init = getVtblInit(); // might define vtbl
if (!vtbl->hasInitializer())
defineGlobal(vtbl, init, aggrdecl);
}
return vtbl;
}
//////////////////////////////////////////////////////////////////////////////
LLGlobalVariable *IrClass::getClassInfoSymbol() {
if (typeInfo) {
return typeInfo;
}
// create the ClassZ / InterfaceZ symbol
LLGlobalVariable *IrClass::getClassInfoSymbol(bool define) {
if (!typeInfo) {
const auto irMangle = getIRMangledClassInfoSymbolName(aggrdecl);
// The type is also ClassInfo for interfaces the actual TypeInfo for them
@ -75,7 +74,8 @@ LLGlobalVariable *IrClass::getClassInfoSymbol() {
IrTypeClass *tc = stripModifiers(cinfoType)->ctype->isClass();
assert(tc && "invalid ClassInfo type");
// classinfos cannot be constants since they're used as locks for synchronized
// classinfos cannot be constants since they're used as locks for
// synchronized
typeInfo = declareGlobal(aggrdecl->loc, gIR->module, tc->getMemoryLLType(),
irMangle, false);
@ -95,14 +95,22 @@ LLGlobalVariable *IrClass::getClassInfoSymbol() {
llvm::ConstantAsMetadata::get(llvm::UndefValue::get(bodyType));
mdVals[CD_Finalize] = llvm::ConstantAsMetadata::get(
LLConstantInt::get(LLType::getInt1Ty(gIR->context()), hasDestructor));
mdVals[CD_CustomDelete] = llvm::ConstantAsMetadata::get(
LLConstantInt::get(LLType::getInt1Ty(gIR->context()), hasCustomDelete));
mdVals[CD_CustomDelete] =
llvm::ConstantAsMetadata::get(LLConstantInt::get(
LLType::getInt1Ty(gIR->context()), hasCustomDelete));
// Construct the metadata and insert it into the module.
const auto metaname = getMetadataName(CD_PREFIX, typeInfo);
llvm::NamedMDNode *node = gIR->module.getOrInsertNamedMetadata(metaname);
node->addOperand(llvm::MDNode::get(
gIR->context(), llvm::makeArrayRef(mdVals, CD_NumFields)));
}
}
if (define) {
auto init = getClassInfoInit();
if (!typeInfo->hasInitializer())
defineGlobal(typeInfo, init, aggrdecl);
}
return typeInfo;
}

View file

@ -34,11 +34,8 @@ LLStructType* getTypeInfoStructMemType() {
}
}
LLGlobalVariable* IrStruct::getTypeInfoSymbol() {
if (typeInfo) {
return typeInfo;
}
LLGlobalVariable* IrStruct::getTypeInfoSymbol(bool define) {
if (!typeInfo) {
OutBuffer mangledName;
mangledName.writestring("TypeInfo_S");
mangleToBuffer(aggrdecl, &mangledName);
@ -55,6 +52,13 @@ LLGlobalVariable* IrStruct::getTypeInfoSymbol() {
getTypeInfoStructMemType(), irMangle, false);
emitTypeInfoMetadata(typeInfo, aggrdecl->type);
}
if (define) {
auto init = getTypeInfoInit();
if (!typeInfo->hasInitializer())
defineGlobal(typeInfo, init, aggrdecl);
}
return typeInfo;
}