Skip to content

Commit

Permalink
Move Initializer::toDt into a visitor class
Browse files Browse the repository at this point in the history
  • Loading branch information
yebblies committed Feb 4, 2014
1 parent 8706495 commit 152e480
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 128 deletions.
11 changes: 0 additions & 11 deletions src/init.h
Expand Up @@ -21,7 +21,6 @@ class Identifier;
class Expression;
struct Scope;
class Type;
struct dt_t;
class AggregateDeclaration;
class ErrorInitializer;
class VoidInitializer;
Expand All @@ -48,8 +47,6 @@ class Initializer : public RootObject

static Initializers *arraySyntaxCopy(Initializers *ai);

virtual dt_t *toDt();

virtual ErrorInitializer *isErrorInitializer() { return NULL; }
virtual VoidInitializer *isVoidInitializer() { return NULL; }
virtual StructInitializer *isStructInitializer() { return NULL; }
Expand All @@ -69,8 +66,6 @@ class VoidInitializer : public Initializer
Expression *toExpression(Type *t = NULL);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);

dt_t *toDt();

virtual VoidInitializer *isVoidInitializer() { return this; }
void accept(Visitor *v) { v->visit(this); }
};
Expand Down Expand Up @@ -101,8 +96,6 @@ class StructInitializer : public Initializer
Expression *toExpression(Type *t = NULL);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);

dt_t *toDt();

StructInitializer *isStructInitializer() { return this; }
void accept(Visitor *v) { v->visit(this); }
};
Expand All @@ -126,8 +119,6 @@ class ArrayInitializer : public Initializer
Expression *toAssocArrayLiteral();
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);

dt_t *toDt();

ArrayInitializer *isArrayInitializer() { return this; }
void accept(Visitor *v) { v->visit(this); }
};
Expand All @@ -145,8 +136,6 @@ class ExpInitializer : public Initializer
Expression *toExpression(Type *t = NULL);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);

dt_t *toDt();

virtual ExpInitializer *isExpInitializer() { return this; }
void accept(Visitor *v) { v->visit(this); }
};
Expand Down
238 changes: 123 additions & 115 deletions src/todt.c
Expand Up @@ -31,145 +31,153 @@
#include "target.h"
#include "ctfe.h"
#include "arraytypes.h"
#include "visitor.h"
// Back end
#include "dt.h"

/* ================================================================ */

dt_t *Initializer::toDt()
dt_t *Initializer_toDt(Initializer *init)
{
assert(0);
return NULL;
}


dt_t *VoidInitializer::toDt()
{ /* Void initializers are set to 0, just because we need something
* to set them to in the static data segment.
*/
dt_t *dt = NULL;

dtnzeros(&dt, type->size());
return dt;
}

class InitToDt : public Visitor
{
public:
dt_t *result;

dt_t *StructInitializer::toDt()
{
//printf("StructInitializer::toDt('%s')\n", toChars());
assert(0);
return NULL;
}
InitToDt()
: result(NULL)
{
}

void visit(Initializer *)
{
assert(0);
}

dt_t *ArrayInitializer::toDt()
{
//printf("ArrayInitializer::toDt('%s')\n", toChars());
Type *tb = type->toBasetype();
if (tb->ty == Tvector)
tb = ((TypeVector *)tb)->basetype;
void visit(VoidInitializer *vi)
{
/* Void initializers are set to 0, just because we need something
* to set them to in the static data segment.
*/
dtnzeros(&result, vi->type->size());
}

Type *tn = tb->nextOf()->toBasetype();
void visit(StructInitializer *si)
{
//printf("StructInitializer::toDt('%s')\n", si->toChars());
assert(0);
}

//printf("\tdim = %d\n", dim);
Dts dts;
dts.setDim(dim);
dts.zero();
void visit(ArrayInitializer *ai)
{
//printf("ArrayInitializer::toDt('%s')\n", ai->toChars());
Type *tb = ai->type->toBasetype();
if (tb->ty == Tvector)
tb = ((TypeVector *)tb)->basetype;

unsigned size = tn->size();
Type *tn = tb->nextOf()->toBasetype();

unsigned length = 0;
for (size_t i = 0; i < index.dim; i++)
{
Expression *idx = index[i];
if (idx)
length = idx->toInteger();
//printf("\tindex[%d] = %p, length = %u, dim = %u\n", i, idx, length, dim);

assert(length < dim);
Initializer *val = value[i];
dt_t *dt = val->toDt();
if (dts[length])
error(loc, "duplicate initializations for index %d", length);
dts[length] = dt;
length++;
}
//printf("\tdim = %d\n", ai->dim);
Dts dts;
dts.setDim(ai->dim);
dts.zero();

Expression *edefault = tb->nextOf()->defaultInit();
unsigned size = tn->size();

size_t n = 1;
for (Type *tbn = tn; tbn->ty == Tsarray; tbn = tbn->nextOf()->toBasetype())
{ TypeSArray *tsa = (TypeSArray *)tbn;
unsigned length = 0;
for (size_t i = 0; i < ai->index.dim; i++)
{
Expression *idx = ai->index[i];
if (idx)
length = idx->toInteger();
//printf("\tindex[%d] = %p, length = %u, dim = %u\n", i, idx, length, ai->dim);

assert(length < ai->dim);
dt_t *dt = Initializer_toDt(ai->value[i]);
if (dts[length])
error(ai->loc, "duplicate initializations for index %d", length);
dts[length] = dt;
length++;
}

n *= tsa->dim->toInteger();
}
Expression *edefault = tb->nextOf()->defaultInit();

dt_t *d = NULL;
dt_t **pdtend = &d;
for (size_t i = 0; i < dim; i++)
{
dt_t *dt = dts[i];
if (dt)
pdtend = dtcat(pdtend, dt);
else
{
for (size_t j = 0; j < n; j++)
pdtend = edefault->toDt(pdtend);
}
}
switch (tb->ty)
{
case Tsarray:
{ size_t tadim;
TypeSArray *ta = (TypeSArray *)tb;
size_t n = 1;
for (Type *tbn = tn; tbn->ty == Tsarray; tbn = tbn->nextOf()->toBasetype())
{
TypeSArray *tsa = (TypeSArray *)tbn;
n *= tsa->dim->toInteger();
}

tadim = ta->dim->toInteger();
if (dim < tadim)
dt_t **pdtend = &result;
for (size_t i = 0; i < ai->dim; i++)
{
if (edefault->isBool(false))
// pad out end of array
pdtend = dtnzeros(pdtend, size * (tadim - dim));
dt_t *dt = dts[i];
if (dt)
pdtend = dtcat(pdtend, dt);
else
{
for (size_t i = dim; i < tadim; i++)
{ for (size_t j = 0; j < n; j++)
pdtend = edefault->toDt(pdtend);
}
for (size_t j = 0; j < n; j++)
pdtend = edefault->toDt(pdtend);
}
}
else if (dim > tadim)
switch (tb->ty)
{
error(loc, "too many initializers, %d, for array[%d]", dim, tadim);
case Tsarray:
{
TypeSArray *ta = (TypeSArray *)tb;
size_t tadim = ta->dim->toInteger();
if (ai->dim < tadim)
{
if (edefault->isBool(false))
{
// pad out end of array
pdtend = dtnzeros(pdtend, size * (tadim - ai->dim));
}
else
{
for (size_t i = ai->dim; i < tadim; i++)
{
for (size_t j = 0; j < n; j++)
pdtend = edefault->toDt(pdtend);
}
}
}
else if (ai->dim > tadim)
{
error(ai->loc, "too many initializers, %d, for array[%d]", ai->dim, tadim);
}
break;
}

case Tpointer:
case Tarray:
{
dt_t *dtarray = result;
result = NULL;
if (tb->ty == Tarray)
dtsize_t(&result, ai->dim);
dtdtoff(&result, dtarray, 0);
break;
}

default:
assert(0);
}
break;
}

case Tpointer:
case Tarray:
void visit(ExpInitializer *ei)
{
dt_t *dtarray = d;
d = NULL;
if (tb->ty == Tarray)
dtsize_t(&d, dim);
dtdtoff(&d, dtarray, 0);
break;
//printf("ExpInitializer::toDt() %s\n", ei->exp->toChars());
ei->exp = ei->exp->optimize(WANTvalue);
ei->exp->toDt(&result);
}
};

default:
assert(0);
}
return d;
}



dt_t *ExpInitializer::toDt()
{
//printf("ExpInitializer::toDt() %s\n", exp->toChars());
dt_t *dt = NULL;

exp = exp->optimize(WANTvalue);
exp->toDt(&dt);
return dt;
InitToDt v;
init->accept(&v);
assert(v.result);
return v.result;
}

/* ================================================================ */
Expand Down Expand Up @@ -481,7 +489,7 @@ dt_t **VarExp::toDt(dt_t **pdt)
return pdt;
}
v->inuse++;
*pdt = v->init->toDt();
*pdt = Initializer_toDt(v->init);
v->inuse--;
return pdt;
}
Expand Down Expand Up @@ -592,7 +600,7 @@ void ClassDeclaration::toDt2(dt_t **pdt, ClassDeclaration *cd)
else if (ei && tb->ty == Tsarray)
((TypeSArray *)tb)->toDtElem(&dt, ei->exp);
else
dt = init->toDt();
dt = Initializer_toDt(init);
}
else if (v->offset >= offset)
{ //printf("\t\tdefault initializer\n");
Expand Down Expand Up @@ -723,7 +731,7 @@ dt_t **TypeTypedef::toDt(dt_t **pdt)
{
if (sym->init)
{
dt_t *dt = sym->init->toDt();
dt_t *dt = Initializer_toDt(sym->init);

pdt = dtend(pdt);
*pdt = dt;
Expand Down Expand Up @@ -833,7 +841,7 @@ dt_t **ClassReferenceExp::toDt2(dt_t **pdt, ClassDeclaration *cd, Dts *dts)
else if (ei && tb->ty == Tsarray)
((TypeSArray *)tb)->toDtElem(&dt, ei->exp);
else
dt = init->toDt();
dt = Initializer_toDt(init);
}
else if (v->offset >= offset)
{ //printf("\t\tdefault initializer\n");
Expand Down Expand Up @@ -885,7 +893,7 @@ dt_t **ClassReferenceExp::toDt2(dt_t **pdt, ClassDeclaration *cd, Dts *dts)
if (!d)
{
if (v->init)
d = v->init->toDt();
d = Initializer_toDt(v->init);
else
vt->toDt(&d);
}
Expand Down
6 changes: 4 additions & 2 deletions src/toobj.c
Expand Up @@ -42,6 +42,7 @@ void obj_startaddress(Symbol *s);
void obj_lzext(Symbol *s1,Symbol *s2);

void TypeInfo_toDt(dt_t **pdt, TypeInfoDeclaration *d);
dt_t *Initializer_toDt(Initializer *init);

/* ================================================================== */

Expand Down Expand Up @@ -914,7 +915,8 @@ void VarDeclaration::toObjFile(int multiobj)
s->Sfl = FLdata;

if (init)
{ s->Sdt = init->toDt();
{
s->Sdt = Initializer_toDt(init);

// Look for static array that is block initialized
Type *tb;
Expand Down Expand Up @@ -1002,7 +1004,7 @@ void TypedefDeclaration::toObjFile(int multiobj)
toInitializer();
sinit->Sclass = scclass;
sinit->Sfl = FLdata;
sinit->Sdt = tc->sym->init->toDt();
sinit->Sdt = Initializer_toDt(tc->sym->init);
out_readonly(sinit);
outdata(sinit);
}
Expand Down

0 comments on commit 152e480

Please sign in to comment.