Fixed class default initializers and type generation. Bug #260 is fixed.

This commit is contained in:
Tomas Lindquist Olsen 2009-04-27 03:40:40 +02:00
parent 3bd5cf70c2
commit ba38e15f0d
9 changed files with 169 additions and 44 deletions

View file

@ -205,7 +205,11 @@ void DtoInitClass(TypeClass* tc, LLValue* dst)
// copy the rest from the static initializer // copy the rest from the static initializer
LLValue* dstarr = DtoGEPi(dst,0,2,"tmp"); LLValue* dstarr = DtoGEPi(dst,0,2,"tmp");
LLValue* srcarr = DtoGEPi(tc->sym->ir.irStruct->getInitSymbol(),0,2,"tmp");
// init symbols might not have valid types
LLValue* initsym = tc->sym->ir.irStruct->getInitSymbol();
initsym = DtoBitCast(initsym, DtoType(tc));
LLValue* srcarr = DtoGEPi(initsym,0,2,"tmp");
DtoMemCpy(dstarr, srcarr, DtoConstSize_t(n)); DtoMemCpy(dstarr, srcarr, DtoConstSize_t(n));
} }

View file

@ -44,13 +44,13 @@ void DtoResolveStruct(StructDeclaration* sd)
IrStruct* irstruct = new IrStruct(sd); IrStruct* irstruct = new IrStruct(sd);
sd->ir.irStruct = irstruct; sd->ir.irStruct = irstruct;
// emit the initZ symbol
LLGlobalVariable* initZ = irstruct->getInitSymbol();
// perform definition // perform definition
bool needs_def = mustDefineSymbol(sd); bool needs_def = mustDefineSymbol(sd);
if (needs_def) if (needs_def)
{ {
// emit the initZ symbol
LLGlobalVariable* initZ = irstruct->getInitSymbol();
// set initZ initializer // set initZ initializer
initZ->setInitializer(irstruct->getDefaultInit()); initZ->setInitializer(irstruct->getDefaultInit());
} }

View file

@ -217,7 +217,9 @@ DValue* VarExp::toElem(IRState* p)
assert(ts->sym); assert(ts->sym);
ts->sym->codegen(Type::sir); ts->sym->codegen(Type::sir);
return new DVarValue(type, ts->sym->ir.irStruct->getInitSymbol()); LLValue* initsym = ts->sym->ir.irStruct->getInitSymbol();
initsym = DtoBitCast(initsym, DtoType(ts->pointerTo()));
return new DVarValue(type, initsym);
} }
else else
{ {
@ -1645,7 +1647,7 @@ DValue* NewExp::toElem(IRState* p)
else { else {
assert(ts->sym); assert(ts->sym);
ts->sym->codegen(Type::sir); ts->sym->codegen(Type::sir);
DtoAggrCopy(mem,ts->sym->ir.irStruct->getInitSymbol()); DtoAggrCopy(mem, ts->sym->ir.irStruct->getInitSymbol());
} }
return new DImValue(type, mem); return new DImValue(type, mem);
} }

View file

@ -183,21 +183,20 @@ void IrStruct::addBaseClassInits(
addBaseClassInits(constants, base->baseClass, offset, field_index); addBaseClassInits(constants, base->baseClass, offset, field_index);
} }
ArrayIter<VarDeclaration> it(base->fields); IrTypeClass* tc = base->type->irtype->isClass();
for (; !it.done(); it.next()) assert(tc);
{
VarDeclaration* vd = it.get();
// skip if offset moved backwards // go through fields
if (vd->offset < offset) IrTypeAggr::iterator it;
{ for (it = tc->def_begin(); it != tc->def_end(); ++it)
IF_LOG Logger::println("Skipping field %s %s (+%u) for default", vd->type->toChars(), vd->toChars(), vd->offset); {
continue; VarDeclaration* vd = *it;
}
IF_LOG Logger::println("Adding default field %s %s (+%u)", vd->type->toChars(), vd->toChars(), vd->offset); IF_LOG Logger::println("Adding default field %s %s (+%u)", vd->type->toChars(), vd->toChars(), vd->offset);
LOG_SCOPE; LOG_SCOPE;
assert(vd->offset >= offset && "default fields not sorted by offset");
// get next aligned offset for this type // get next aligned offset for this type
size_t alignsize = vd->type->alignsize(); size_t alignsize = vd->type->alignsize();
size_t alignedoffset = (offset + alignsize - 1) & ~(alignsize - 1); size_t alignedoffset = (offset + alignsize - 1) & ~(alignsize - 1);
@ -216,13 +215,15 @@ void IrStruct::addBaseClassInits(
} }
// has interface vtbls? // has interface vtbls?
if (base->vtblInterfaces) if (base->vtblInterfaces && base->vtblInterfaces->dim > 0)
{ {
// false when it's not okay to use functions from super classes // false when it's not okay to use functions from super classes
bool newinsts = (base == aggrdecl->isClassDeclaration()); bool newinsts = (base == aggrdecl->isClassDeclaration());
size_t inter_idx = interfacesWithVtbls.size(); size_t inter_idx = interfacesWithVtbls.size();
offset = (offset + PTRSIZE - 1) & ~(PTRSIZE - 1);
ArrayIter<BaseClass> it2(*base->vtblInterfaces); ArrayIter<BaseClass> it2(*base->vtblInterfaces);
for (; !it2.done(); it2.next()) for (; !it2.done(); it2.next())
{ {

View file

@ -19,7 +19,8 @@
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
IrStruct::IrStruct(AggregateDeclaration* aggr) IrStruct::IrStruct(AggregateDeclaration* aggr)
: diCompositeType(NULL) : diCompositeType(NULL),
init_pa(llvm::OpaqueType::get())
{ {
aggrdecl = aggr; aggrdecl = aggr;
@ -57,7 +58,7 @@ LLGlobalVariable * IrStruct::getInitSymbol()
llvm::GlobalValue::LinkageTypes _linkage = DtoExternalLinkage(aggrdecl); llvm::GlobalValue::LinkageTypes _linkage = DtoExternalLinkage(aggrdecl);
init = new llvm::GlobalVariable( init = new llvm::GlobalVariable(
type->irtype->getPA().get(), true, _linkage, NULL, initname, gIR->module); init_pa.get(), true, _linkage, NULL, initname, gIR->module);
return init; return init;
} }
@ -78,6 +79,9 @@ llvm::Constant * IrStruct::getDefaultInit()
constInit = createClassDefaultInitializer(); constInit = createClassDefaultInitializer();
} }
llvm::OpaqueType* o = llvm::cast<llvm::OpaqueType>(init_pa.get());
o->refineAbstractTypeTo(constInit->getType());
return constInit; return constInit;
} }
@ -157,11 +161,7 @@ LLConstant * IrStruct::createStructDefaultInitializer()
{ {
VarDeclaration* vd = *it; VarDeclaration* vd = *it;
if (vd->offset < offset) assert(vd->offset >= offset && "default fields not sorted by offset");
{
IF_LOG Logger::println("skipping field: %s %s (+%u)", vd->type->toChars(), vd->toChars(), vd->offset);
continue;
}
IF_LOG Logger::println("using field: %s %s (+%u)", vd->type->toChars(), vd->toChars(), vd->offset); IF_LOG Logger::println("using field: %s %s (+%u)", vd->type->toChars(), vd->toChars(), vd->offset);

View file

@ -68,6 +68,8 @@ protected:
llvm::GlobalVariable* init; llvm::GlobalVariable* init;
/// Static default initializer constant. /// Static default initializer constant.
LLConstant* constInit; LLConstant* constInit;
/// Static default initialier type holder.
llvm::PATypeHolder init_pa;
/// Vtbl global. /// Vtbl global.
llvm::GlobalVariable* vtbl; llvm::GlobalVariable* vtbl;

View file

@ -14,6 +14,7 @@
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
extern size_t add_zeros(std::vector<const llvm::Type*>& defaultTypes, size_t diff); extern size_t add_zeros(std::vector<const llvm::Type*>& defaultTypes, size_t diff);
extern bool var_offset_sort_cb(const VarDeclaration* v1, const VarDeclaration* v2);
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
@ -40,29 +41,112 @@ void IrTypeClass::addBaseClassData(
addBaseClassData(defaultTypes, base->baseClass, offset, field_index); addBaseClassData(defaultTypes, base->baseClass, offset, field_index);
} }
ArrayIter<VarDeclaration> it(base->fields); // FIXME: merge code with structs in IrTypeAggr
for (; !it.done(); it.next())
{
VarDeclaration* vd = it.get();
// skip if offset moved backwards // mirror the sd->fields array but only fill in contributors
if (vd->offset < offset) size_t n = base->fields.dim;
LLSmallVector<VarDeclaration*, 16> data(n, NULL);
default_fields.reserve(n);
// first fill in the fields with explicit initializers
VarDeclarationIter field_it(base->fields);
for (; field_it.more(); field_it.next())
{
// init is !null for explicit inits
if (field_it->init != NULL)
{ {
IF_LOG Logger::println("Skipping field %s %s (+%u) for default", vd->type->toChars(), vd->toChars(), vd->offset); IF_LOG Logger::println("adding explicit initializer for struct field %s",
if (vd->ir.irField == NULL) field_it->toChars());
data[field_it.index] = *field_it;
size_t f_begin = field_it->offset;
size_t f_end = f_begin + field_it->type->size();
// make sure there is no overlap
for (size_t i = 0; i < field_it.index; i++)
{ {
new IrField(vd, 2, vd->offset - PTRSIZE * 2); if (data[i] != NULL)
{
VarDeclaration* vd = data[i];
size_t v_begin = vd->offset;
size_t v_end = v_begin + vd->type->size();
if (v_begin >= f_end || v_end <= f_begin)
continue;
base->error(vd->loc, "has overlapping initialization for %s and %s",
field_it->toChars(), vd->toChars());
}
} }
}
}
if (global.errors)
{
fatal();
}
// fill in default initializers
field_it = VarDeclarationIter(base->fields);
for (;field_it.more(); field_it.next())
{
if (data[field_it.index])
continue; continue;
size_t f_begin = field_it->offset;
size_t f_end = f_begin + field_it->type->size();
// make sure it doesn't overlap anything explicit
bool overlaps = false;
for (size_t i = 0; i < n; i++)
{
if (data[i])
{
size_t v_begin = data[i]->offset;
size_t v_end = v_begin + data[i]->type->size();
if (v_begin >= f_end || v_end <= f_begin)
continue;
overlaps = true;
break;
}
} }
IF_LOG Logger::println("Adding default field %s %s (+%u)", vd->type->toChars(), vd->toChars(), vd->offset); // if no overlap was found, add the default initializer
if (!overlaps)
{
IF_LOG Logger::println("adding default initializer for struct field %s",
field_it->toChars());
data[field_it.index] = *field_it;
}
}
// ok. now we can build a list of llvm types. and make sure zeros are inserted if necessary.
// first we sort the list by offset
std::sort(data.begin(), data.end(), var_offset_sort_cb);
// add types to list
for (size_t i = 0; i < n; i++)
{
VarDeclaration* vd = data[i];
if (vd == NULL)
continue;
assert(vd->offset >= offset && "it's a bug...");
// add to default field list
if (cd == base)
default_fields.push_back(vd);
// get next aligned offset for this type // get next aligned offset for this type
size_t alignsize = vd->type->alignsize(); size_t alignsize = vd->type->alignsize();
size_t alignedoffset = (offset + alignsize - 1) & ~(alignsize - 1); size_t alignedoffset = (offset + alignsize - 1) & ~(alignsize - 1);
// do we need to insert explicit padding before the field? // insert explicit padding?
if (alignedoffset < vd->offset) if (alignedoffset < vd->offset)
{ {
field_index += add_zeros(defaultTypes, vd->offset - alignedoffset); field_index += add_zeros(defaultTypes, vd->offset - alignedoffset);
@ -74,19 +158,27 @@ void IrTypeClass::addBaseClassData(
// advance offset to right past this field // advance offset to right past this field
offset = vd->offset + vd->type->size(); offset = vd->offset + vd->type->size();
// give field index // create ir field
// the IrField creation doesn't really belong here, but it's a trivial operation
// and it save yet another of these loops.
IF_LOG Logger::println("Field index: %zu", field_index);
if (vd->ir.irField == NULL) if (vd->ir.irField == NULL)
{
new IrField(vd, field_index); new IrField(vd, field_index);
} else
assert(vd->ir.irField->index == field_index &&
vd->ir.irField->unionOffset == 0 &&
"inconsistent field data");
field_index++; field_index++;
} }
// make sure all fields really get their ir field
ArrayIter<VarDeclaration> it(base->fields);
for (; !it.done(); it.next())
{
VarDeclaration* vd = it.get();
if (vd->ir.irField == NULL)
new IrField(vd, 0, vd->offset);
}
// any interface implementations? // any interface implementations?
if (base->vtblInterfaces) if (base->vtblInterfaces && base->vtblInterfaces->dim > 0)
{ {
bool new_instances = (base == cd); bool new_instances = (base == cd);
@ -95,6 +187,9 @@ void IrTypeClass::addBaseClassData(
VarDeclarationIter interfaces_idx(ClassDeclaration::classinfo->fields, 3); VarDeclarationIter interfaces_idx(ClassDeclaration::classinfo->fields, 3);
Type* first = interfaces_idx->type->next->pointerTo(); Type* first = interfaces_idx->type->next->pointerTo();
// align offset
offset = (offset + PTRSIZE - 1) & ~(PTRSIZE - 1);
for (; !it2.done(); it2.next()) for (; !it2.done(); it2.next())
{ {
BaseClass* b = it2.get(); BaseClass* b = it2.get();
@ -117,12 +212,14 @@ void IrTypeClass::addBaseClassData(
} }
} }
#if 0
// tail padding? // tail padding?
if (offset < base->structsize) if (offset < base->structsize)
{ {
field_index += add_zeros(defaultTypes, base->structsize - offset); field_index += add_zeros(defaultTypes, base->structsize - offset);
offset = base->structsize; offset = base->structsize;
} }
#endif
} }
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
@ -159,6 +256,15 @@ const llvm::Type* IrTypeClass::buildType()
// add data members recursively // add data members recursively
addBaseClassData(defaultTypes, cd, offset, field_index); addBaseClassData(defaultTypes, cd, offset, field_index);
#if 1
// tail padding?
if (offset < cd->structsize)
{
field_index += add_zeros(defaultTypes, cd->structsize - offset);
offset = cd->structsize;
}
#endif
} }
// errors are fatal during codegen // errors are fatal during codegen

View file

@ -64,10 +64,15 @@ size_t add_zeros(std::vector<const llvm::Type*>& defaultTypes, size_t diff)
bool var_offset_sort_cb(const VarDeclaration* v1, const VarDeclaration* v2) bool var_offset_sort_cb(const VarDeclaration* v1, const VarDeclaration* v2)
{ {
if (v1 && v2) return v1->offset < v2->offset; if (v1 && v2)
else return false; return v1->offset < v2->offset;
else
return false;
} }
// this is pretty much the exact same thing we need to do for fields in each
// base class of a class
const llvm::Type* IrTypeStruct::buildType() const llvm::Type* IrTypeStruct::buildType()
{ {
IF_LOG Logger::println("Building struct type %s @ %s", IF_LOG Logger::println("Building struct type %s @ %s",

View file

@ -33,6 +33,11 @@ protected:
/// AggregateDeclaration this type represents. /// AggregateDeclaration this type represents.
AggregateDeclaration* aggr; 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; std::vector<VarDeclaration*> default_fields;
}; };