mirror of
https://github.com/xomboverlord/ldc.git
synced 2026-03-01 18:13:18 +01:00
Refactor struct initializers codegen.
This not only reduces code duplication, but the unification also enables code a la StructLiteralExp to handle classes (for CTFE class constant support in 2.063).
This commit is contained in:
@@ -16,15 +16,8 @@
|
||||
#endif
|
||||
|
||||
#include "aggregate.h"
|
||||
#include "declaration.h"
|
||||
#include "init.h"
|
||||
#include "mtype.h"
|
||||
|
||||
#include "gen/irstate.h"
|
||||
#include "gen/tollvm.h"
|
||||
#include "gen/logger.h"
|
||||
#include "gen/utils.h"
|
||||
#include "gen/llvmhelpers.h"
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
@@ -34,106 +27,3 @@ IrTypeAggr::IrTypeAggr(AggregateDeclaration * ad)
|
||||
aggr(ad)
|
||||
{
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static bool struct_init_data_sort(const IrTypeAggr::VarInitConst& a,
|
||||
const IrTypeAggr::VarInitConst& b)
|
||||
{
|
||||
return (a.first && b.first)
|
||||
? a.first->offset < b.first->offset
|
||||
: false;
|
||||
}
|
||||
|
||||
extern size_t add_zeros(std::vector<llvm::Constant*>& constants, size_t diff);
|
||||
|
||||
// return a constant array of type arrTypeD initialized with a constant value, or that constant value
|
||||
static llvm::Constant* FillSArrayDims(Type* arrTypeD, llvm::Constant* init)
|
||||
{
|
||||
if (arrTypeD->ty == Tsarray)
|
||||
{
|
||||
init = FillSArrayDims(arrTypeD->nextOf(), init);
|
||||
size_t dim = static_cast<TypeSArray*>(arrTypeD)->dim->toUInteger();
|
||||
llvm::ArrayType* arrty = llvm::ArrayType::get(init->getType(), dim);
|
||||
return llvm::ConstantArray::get(arrty,
|
||||
std::vector<llvm::Constant*>(dim, init));
|
||||
}
|
||||
return init;
|
||||
}
|
||||
|
||||
llvm::Constant* IrTypeAggr::createInitializerConstant(
|
||||
llvm::ArrayRef<IrTypeAggr::VarInitConst> initializers,
|
||||
llvm::StructType* initializerType)
|
||||
{
|
||||
const bool packed = (dtype->ty == Tstruct)
|
||||
? dtype->alignsize() == 1
|
||||
: false;
|
||||
|
||||
const size_t n = initializers.size();
|
||||
|
||||
// sort data array by offset
|
||||
llvm::SmallVector<IrTypeAggr::VarInitConst, 16> data(
|
||||
initializers.begin(), initializers.end());
|
||||
std::sort(data.begin(), data.end(), struct_init_data_sort);
|
||||
|
||||
// build array of constants and make sure explicit zero padding is inserted when necessary.
|
||||
size_t offset = 0;
|
||||
std::vector<llvm::Constant*> constants;
|
||||
constants.reserve(n);
|
||||
|
||||
for (size_t i = 0; i < n; i++)
|
||||
{
|
||||
VarDeclaration* vd = data[i].first;
|
||||
if (vd == NULL)
|
||||
continue;
|
||||
|
||||
// get next aligned offset for this field
|
||||
size_t alignedoffset = offset;
|
||||
if (!packed)
|
||||
{
|
||||
alignedoffset = realignOffset(alignedoffset, vd->type);
|
||||
}
|
||||
|
||||
// insert explicit padding?
|
||||
if (alignedoffset < vd->offset)
|
||||
{
|
||||
size_t diff = vd->offset - alignedoffset;
|
||||
IF_LOG Logger::println("adding %zu bytes zero padding", diff);
|
||||
add_zeros(constants, diff);
|
||||
}
|
||||
|
||||
IF_LOG Logger::println("adding field %s", vd->toChars());
|
||||
|
||||
constants.push_back(FillSArrayDims(vd->type, data[i].second));
|
||||
offset = vd->offset + vd->type->size();
|
||||
}
|
||||
|
||||
// tail padding?
|
||||
const size_t structsize = getTypePaddedSize(type);
|
||||
if (offset < structsize)
|
||||
{
|
||||
size_t diff = structsize - offset;
|
||||
IF_LOG Logger::println("adding %zu bytes zero padding", diff);
|
||||
add_zeros(constants, diff);
|
||||
}
|
||||
|
||||
// get initializer type
|
||||
if (!initializerType || initializerType->isOpaque())
|
||||
{
|
||||
std::vector<llvm::Constant*>::iterator itr, end = constants.end();
|
||||
std::vector<llvm::Type*> types;
|
||||
types.reserve(constants.size());
|
||||
for (itr = constants.begin(); itr != end; ++itr)
|
||||
types.push_back((*itr)->getType());
|
||||
if (!initializerType)
|
||||
initializerType = LLStructType::get(gIR->context(), types, packed);
|
||||
else
|
||||
initializerType->setBody(types, packed);
|
||||
}
|
||||
|
||||
// build constant
|
||||
assert(!constants.empty());
|
||||
llvm::Constant* c = LLConstantStruct::get(initializerType, constants);
|
||||
IF_LOG Logger::cout() << "final struct initializer: " << *c << std::endl;
|
||||
return c;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user