mirror of
https://github.com/xomboverlord/ldc.git
synced 2026-01-14 20:03:14 +01:00
Divided ir/irtypestruct.* in general aggregate and struct specific part.
This is just to improve clarity, as it was rather non-obvious what of the code also applied to classes before. IrTypeAggr::createInitializerConstant would currently belong in IrTypeStruct, but this will be refactored anyway.
This commit is contained in:
@@ -22,7 +22,6 @@
|
||||
#include "gen/runtime.h"
|
||||
#include "gen/tollvm.h"
|
||||
#include "ir/irmodule.h"
|
||||
#include "ir/irtypestruct.h"
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
@@ -22,7 +22,6 @@
|
||||
#include "gen/tollvm.h"
|
||||
#include "ir/ir.h"
|
||||
#include "ir/irtype.h"
|
||||
#include "ir/irtypestruct.h"
|
||||
#include "ir/irvar.h"
|
||||
|
||||
/* ================================================================== */
|
||||
|
||||
@@ -22,7 +22,6 @@
|
||||
#include "gen/tollvm.h"
|
||||
#include "gen/utils.h"
|
||||
#include "ir/iraggr.h"
|
||||
#include "ir/irtypestruct.h"
|
||||
#include "llvm/ADT/DenseMap.h"
|
||||
#include "llvm/Support/ManagedStatic.h"
|
||||
#include <algorithm>
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
#include "ir/irtype.h"
|
||||
#include "ir/irtypeclass.h"
|
||||
#include "ir/irtypefunction.h"
|
||||
#include "ir/irtypestruct.h"
|
||||
|
||||
bool DtoIsPassedByRef(Type* type)
|
||||
{
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "gen/utils.h"
|
||||
#include "ir/iraggr.h"
|
||||
#include "ir/irtypeclass.h"
|
||||
#include "ir/irtypestruct.h"
|
||||
#include <algorithm>
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
139
ir/irtypeaggr.cpp
Normal file
139
ir/irtypeaggr.cpp
Normal file
@@ -0,0 +1,139 @@
|
||||
//===-- irtypeaggr.cpp ----------------------------------------------------===//
|
||||
//
|
||||
// LDC – the LLVM D compiler
|
||||
//
|
||||
// This file is distributed under the BSD-style LDC license. See the LICENSE
|
||||
// file for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "ir/irtypeaggr.h"
|
||||
|
||||
#if LDC_LLVM_VER >= 303
|
||||
#include "llvm/IR/DerivedTypes.h"
|
||||
#else
|
||||
#include "llvm/DerivedTypes.h"
|
||||
#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"
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
IrTypeAggr::IrTypeAggr(AggregateDeclaration * ad)
|
||||
: IrType(ad->type, LLStructType::create(gIR->context(), ad->toPrettyChars())),
|
||||
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;
|
||||
}
|
||||
76
ir/irtypeaggr.h
Normal file
76
ir/irtypeaggr.h
Normal file
@@ -0,0 +1,76 @@
|
||||
//===-- ir/irtypeaggr.h - IrType subclasses for aggregates ------*- C++ -*-===//
|
||||
//
|
||||
// LDC – the LLVM D compiler
|
||||
//
|
||||
// This file is distributed under the BSD-style LDC license. See the LICENSE
|
||||
// file for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef LDC_IR_IRTYPEAGGR_H
|
||||
#define LDC_IR_IRTYPEAGGR_H
|
||||
|
||||
#include "ir/irtype.h"
|
||||
#include "llvm/ADT/ArrayRef.h"
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
|
||||
namespace llvm {
|
||||
class Constant;
|
||||
class StructType;
|
||||
}
|
||||
|
||||
struct AggregateDeclaration;
|
||||
struct VarDeclaration;
|
||||
|
||||
/// Base class of IrTypes for aggregate types.
|
||||
class IrTypeAggr : public IrType
|
||||
{
|
||||
public:
|
||||
///
|
||||
IrTypeAggr* isAggr() { return this; }
|
||||
|
||||
///
|
||||
typedef std::vector<VarDeclaration*>::iterator iterator;
|
||||
|
||||
///
|
||||
iterator def_begin() { return default_fields.begin(); }
|
||||
|
||||
///
|
||||
iterator def_end() { return default_fields.end(); }
|
||||
|
||||
/// A pair of a member variable declaration and an associated initializer
|
||||
/// constant.
|
||||
typedef std::pair<VarDeclaration*, llvm::Constant*> VarInitConst;
|
||||
|
||||
/// Creates an initializer constant for the struct type with the given
|
||||
/// fields set to the provided constants. The remaining space (not
|
||||
/// explicitly specified fields, padding) is default-initialized.
|
||||
///
|
||||
/// The optional initializerType parmeter can be used to specify the exact
|
||||
/// LLVM type to use for the initializer. If non-null and non-opaque, the
|
||||
/// type must exactly match the generated constant. This parameter is used
|
||||
/// mainly for supporting legacy code.
|
||||
///
|
||||
/// Note that in the general case (if e.g. unions are involved), the
|
||||
/// returned type is not necessarily the same as getLLType().
|
||||
llvm::Constant* createInitializerConstant(
|
||||
llvm::ArrayRef<VarInitConst> initializers,
|
||||
llvm::StructType* initializerType = 0);
|
||||
|
||||
protected:
|
||||
///
|
||||
IrTypeAggr(AggregateDeclaration* ad);
|
||||
|
||||
/// AggregateDeclaration this type represents.
|
||||
AggregateDeclaration* aggr;
|
||||
|
||||
/// Sorted list of all default fields.
|
||||
/// A default field is a field that contributes to the default initializer
|
||||
/// and the default type, and thus it has it's own unique GEP index into
|
||||
/// the aggregate.
|
||||
/// For classes, field of any super classes are not included.
|
||||
std::vector<VarDeclaration*> default_fields;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -14,7 +14,7 @@
|
||||
#ifndef __LDC_IR_IRTYPECLASS_H__
|
||||
#define __LDC_IR_IRTYPECLASS_H__
|
||||
|
||||
#include "ir/irtypestruct.h"
|
||||
#include "ir/irtypeaggr.h"
|
||||
#if LDC_LLVM_VER >= 303
|
||||
#include "llvm/IR/DerivedTypes.h"
|
||||
#else
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "ir/irtypestruct.h"
|
||||
|
||||
#if LDC_LLVM_VER >= 303
|
||||
#include "llvm/IR/DerivedTypes.h"
|
||||
#else
|
||||
@@ -23,120 +25,6 @@
|
||||
#include "gen/logger.h"
|
||||
#include "gen/utils.h"
|
||||
#include "gen/llvmhelpers.h"
|
||||
#include "ir/irtypestruct.h"
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
IrTypeAggr::IrTypeAggr(AggregateDeclaration * ad)
|
||||
: IrType(ad->type, LLStructType::create(gIR->context(), ad->toPrettyChars())),
|
||||
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;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
@@ -344,5 +232,3 @@ IrTypeStruct* IrTypeStruct::get(StructDeclaration* sd)
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//===-- ir/irtypestruct.h - IrType subclasses for aggregates ----*- C++ -*-===//
|
||||
//===-- ir/irtypestruct.h - IrType for structs and unions -------*- C++ -*-===//
|
||||
//
|
||||
// LDC – the LLVM D compiler
|
||||
//
|
||||
@@ -6,82 +6,16 @@
|
||||
// file for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Provides the IrType subclasses used to represent D struct types
|
||||
// (see irtypeclass.h for the class version).
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef __LDC_IR_IRTYPESTRUCT_H__
|
||||
#define __LDC_IR_IRTYPESTRUCT_H__
|
||||
#ifndef LDC_IR_IRTYPESTRUCT_H
|
||||
#define LDC_IR_IRTYPESTRUCT_H
|
||||
|
||||
#include "ir/irtype.h"
|
||||
#include "llvm/ADT/ArrayRef.h"
|
||||
#include <utility>
|
||||
#include "ir/irtypeaggr.h"
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
namespace llvm {
|
||||
class StructType;
|
||||
}
|
||||
|
||||
struct AggregateDeclaration;
|
||||
struct StructDeclaration;
|
||||
struct TypeStruct;
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class IrTypeAggr : public IrType
|
||||
{
|
||||
public:
|
||||
///
|
||||
IrTypeAggr* isAggr() { return this; }
|
||||
|
||||
///
|
||||
typedef std::vector<VarDeclaration*>::iterator iterator;
|
||||
|
||||
///
|
||||
iterator def_begin() { return default_fields.begin(); }
|
||||
|
||||
///
|
||||
iterator def_end() { return default_fields.end(); }
|
||||
|
||||
/// A pair of a member variable declaration and an associated initializer
|
||||
/// constant.
|
||||
typedef std::pair<VarDeclaration*, llvm::Constant*> VarInitConst;
|
||||
|
||||
/// Creates an initializer constant for the struct type with the given
|
||||
/// fields set to the provided constants. The remaining space (not
|
||||
/// explicitly specified fields, padding) is default-initialized.
|
||||
///
|
||||
/// The optional initializerType parmeter can be used to specify the exact
|
||||
/// LLVM type to use for the initializer. If non-null and non-opaque, the
|
||||
/// type must exactly match the generated constant. This parameter is used
|
||||
/// mainly for supporting legacy code.
|
||||
///
|
||||
/// Note that in the general case (if e.g. unions are involved), the
|
||||
/// returned type is not necessarily the same as getLLType().
|
||||
llvm::Constant* createInitializerConstant(
|
||||
llvm::ArrayRef<VarInitConst> initializers,
|
||||
llvm::StructType* initializerType = 0);
|
||||
|
||||
protected:
|
||||
///
|
||||
IrTypeAggr(AggregateDeclaration* ad);
|
||||
|
||||
/// AggregateDeclaration this type represents.
|
||||
AggregateDeclaration* aggr;
|
||||
|
||||
/// Sorted list of all default fields.
|
||||
/// A default field is a field that contributes to the default initializer
|
||||
/// and the default type, and thus it has it's own unique GEP index into
|
||||
/// the aggregate.
|
||||
/// For classes, field of any super classes are not included.
|
||||
std::vector<VarDeclaration*> default_fields;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// IrType for struct/union types.
|
||||
class IrTypeStruct : public IrTypeAggr
|
||||
{
|
||||
public:
|
||||
@@ -102,6 +36,4 @@ protected:
|
||||
TypeStruct* ts;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user