Skip to content

Commit

Permalink
towards struct ABI
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed May 6, 2012
1 parent eec6aac commit bde6fff
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 20 deletions.
5 changes: 5 additions & 0 deletions src/aggregate.h
Expand Up @@ -139,6 +139,10 @@ struct StructDeclaration : AggregateDeclaration
FuncDeclaration *postblit; // aggregate postblit
#endif

// For 64 bit Efl function call/return ABI
Type *arg1type;
Type *arg2type;

StructDeclaration(Loc loc, Identifier *id);
Dsymbol *syntaxCopy(Dsymbol *s);
void semantic(Scope *sc);
Expand All @@ -147,6 +151,7 @@ struct StructDeclaration : AggregateDeclaration
char *mangle();
const char *kind();
void finalizeSize(Scope *sc);
bool isPOD();
#if DMDV1
Expression *cloneMembers();
#endif
Expand Down
9 changes: 8 additions & 1 deletion src/argtypes.c
@@ -1,6 +1,6 @@

// Compiler implementation of the D programming language
// Copyright (c) 2010-2011 by Digital Mars
// Copyright (c) 2010-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
Expand Down Expand Up @@ -124,6 +124,13 @@ TypeTuple *TypeBasic::toArgTypes()
return t;
}

#if DMDV2
TypeTuple *TypeVector::toArgTypes()
{
return new TypeTuple(Type::tfloat64);
}
#endif

TypeTuple *TypeSArray::toArgTypes()
{
#if DMDV2
Expand Down
4 changes: 4 additions & 0 deletions src/backend/cc.h
Expand Up @@ -1029,6 +1029,10 @@ typedef struct STRUCT
// of a template class, this is the
// template class Symbol

// For 64 bit Elf function ABI
type *Sarg1type;
type *Sarg2type;

/* For:
* template<class T> struct A { };
* template<class T> struct A<T *> { };
Expand Down
31 changes: 28 additions & 3 deletions src/struct.c
Expand Up @@ -99,7 +99,7 @@ void AggregateDeclaration::inlineScan()
{
for (size_t i = 0; i < members->dim; i++)
{
Dsymbol *s = members->tdata()[i];
Dsymbol *s = (*members)[i];
//printf("inline scan aggregate symbol '%s'\n", s->toChars());
s->inlineScan();
}
Expand Down Expand Up @@ -256,6 +256,8 @@ StructDeclaration::StructDeclaration(Loc loc, Identifier *id)
postblit = NULL;
eq = NULL;
#endif
arg1type = NULL;
arg2type = NULL;

// For forward references
type = new TypeStruct(this);
Expand Down Expand Up @@ -329,7 +331,7 @@ void StructDeclaration::semantic(Scope *sc)
{
for (size_t i = 0; i < members->dim; i++)
{
Dsymbol *s = members->tdata()[i];
Dsymbol *s = (*members)[i];
//printf("adding member '%s' to '%s'\n", s->toChars(), this->toChars());
s->addMember(sc, this, 1);
}
Expand Down Expand Up @@ -528,6 +530,15 @@ void StructDeclaration::semantic(Scope *sc)
aggNew = (NewDeclaration *)search(0, Id::classNew, 0);
aggDelete = (DeleteDeclaration *)search(0, Id::classDelete, 0);

TypeTuple *tup = type->toArgTypes();
size_t dim = tup->arguments->dim;
if (dim >= 1)
{ assert(dim <= 2);
arg1type = (*tup->arguments)[0]->type;
if (dim == 2)
arg2type = (*tup->arguments)[1]->type;
}

if (sc->func)
{
semantic2(sc);
Expand All @@ -553,6 +564,7 @@ Dsymbol *StructDeclaration::search(Loc loc, Identifier *ident, int flags)

void StructDeclaration::finalizeSize(Scope *sc)
{
//printf("StructDeclaration::finalizeSize() %s\n", toChars());
if (sizeok != SIZEOKnone)
return;

Expand Down Expand Up @@ -581,6 +593,19 @@ void StructDeclaration::finalizeSize(Scope *sc)
sizeok = SIZEOKdone;
}

/***************************************
* Return true if struct is POD (Plain Old Data).
* This is defined as:
* not nested
* no constructors, postblits, destructors, or assignment operators
* no fields with with any of those
* The idea being these are compatible with C structs.
*/
bool StructDeclaration::isPOD()
{
return true;
}

void StructDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
buf->printf("%s ", kind());
Expand All @@ -597,7 +622,7 @@ void StructDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
buf->writenl();
for (size_t i = 0; i < members->dim; i++)
{
Dsymbol *s = members->tdata()[i];
Dsymbol *s = (*members)[i];

buf->writestring(" ");
s->toCBuffer(buf, hgs);
Expand Down
8 changes: 5 additions & 3 deletions src/toctype.c
Expand Up @@ -326,6 +326,8 @@ type *TypeStruct::toCtype()
s->Sstruct->Salignsize = sym->alignsize;
s->Sstruct->Sstructalign = sym->alignsize;
s->Sstruct->Sstructsize = sym->structsize;
s->Sstruct->Sarg1type = sym->arg1type ? sym->arg1type->toCtype() : NULL;
s->Sstruct->Sarg2type = sym->arg2type ? sym->arg2type->toCtype() : NULL;

if (sym->isUnionDeclaration())
s->Sstruct->Sflags |= STRunion;
Expand All @@ -341,8 +343,8 @@ type *TypeStruct::toCtype()
* (after setting ctype to avoid infinite recursion)
*/
if (global.params.symdebug)
for (int i = 0; i < sym->fields.dim; i++)
{ VarDeclaration *v = (VarDeclaration *)sym->fields.data[i];
for (size_t i = 0; i < sym->fields.dim; i++)
{ VarDeclaration *v = sym->fields[i];

Symbol *s2 = symbol_name(v->ident->toChars(), SCmember, v->type->toCtype());
s2->Smemoff = v->offset;
Expand Down Expand Up @@ -400,7 +402,7 @@ type *TypeClass::toCtype()
*/
if (global.params.symdebug)
for (size_t i = 0; i < sym->fields.dim; i++)
{ VarDeclaration *v = (VarDeclaration *)sym->fields.data[i];
{ VarDeclaration *v = sym->fields[i];

Symbol *s2 = symbol_name(v->ident->toChars(), SCmember, v->type->toCtype());
s2->Smemoff = v->offset;
Expand Down
39 changes: 26 additions & 13 deletions src/typinf.c
@@ -1,6 +1,6 @@

// Compiler implementation of the D programming language
// Copyright (c) 1999-2011 by Digital Mars
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
Expand Down Expand Up @@ -337,8 +337,12 @@ void TypeInfoEnumDeclaration::toDt(dt_t **pdt)
* void[] m_init;
*/

sd->memtype->getTypeInfo(NULL);
if (sd->memtype)
{ sd->memtype->getTypeInfo(NULL);
dtxoff(pdt, sd->memtype->vtinfo->toSymbol(), 0, TYnptr); // TypeInfo for enum members
}
else
dtsize_t(pdt, 0);

const char *name = sd->toPrettyChars();
size_t namelen = strlen(name);
Expand Down Expand Up @@ -615,12 +619,14 @@ void TypeInfoStructDeclaration::toDt(dt_t **pdt)
dtdword(pdt, tc->alignsize());

#if DMDV2
#if 0
// xgetMembers
FuncDeclaration *sgetmembers = sd->findGetMembers();
if (sgetmembers)
dtxoff(pdt, sgetmembers->toSymbol(), 0, TYnptr);
else
dtsize_t(pdt, 0); // xgetMembers
#endif

// xdtor
FuncDeclaration *sdtor = sd->dtor;
Expand All @@ -638,19 +644,18 @@ void TypeInfoStructDeclaration::toDt(dt_t **pdt)
#endif
if (global.params.is64bit)
{
TypeTuple *tup = tc->toArgTypes();
assert(tup->arguments->dim <= 2);
Type *t = sd->arg1type;
for (int i = 0; i < 2; i++)
{
if (i < tup->arguments->dim)
{
Type *targ = (tup->arguments->tdata()[i])->type;
targ = targ->merge();
targ->getTypeInfo(NULL);
dtxoff(pdt, targ->vtinfo->toSymbol(), 0, TYnptr); // m_argi
// m_argi
if (t)
{ t->getTypeInfo(NULL);
dtxoff(pdt, t->vtinfo->toSymbol(), 0, TYnptr);
}
else
dtsize_t(pdt, 0); // m_argi
dtsize_t(pdt, 0);

t = sd->arg2type;
}
}

Expand All @@ -661,6 +666,7 @@ void TypeInfoStructDeclaration::toDt(dt_t **pdt)
void TypeInfoClassDeclaration::toDt(dt_t **pdt)
{
//printf("TypeInfoClassDeclaration::toDt() %s\n", tinfo->toChars());
#if DMDV1
dtxoff(pdt, Type::typeinfoclass->toVtblSymbol(), 0, TYnptr); // vtbl for TypeInfoClass
dtsize_t(pdt, 0); // monitor

Expand All @@ -673,6 +679,9 @@ void TypeInfoClassDeclaration::toDt(dt_t **pdt)
tc->sym->vclassinfo = new ClassInfoDeclaration(tc->sym);
s = tc->sym->vclassinfo->toSymbol();
dtxoff(pdt, s, 0, TYnptr); // ClassInfo for tinfo
#else
assert(0);
#endif
}

void TypeInfoInterfaceDeclaration::toDt(dt_t **pdt)
Expand All @@ -687,7 +696,11 @@ void TypeInfoInterfaceDeclaration::toDt(dt_t **pdt)
Symbol *s;

if (!tc->sym->vclassinfo)
#if DMDV1
tc->sym->vclassinfo = new ClassInfoDeclaration(tc->sym);
#else
tc->sym->vclassinfo = new TypeInfoClassDeclaration(tc);
#endif
s = tc->sym->vclassinfo->toSymbol();
dtxoff(pdt, s, 0, TYnptr); // ClassInfo for tinfo
}
Expand All @@ -707,7 +720,7 @@ void TypeInfoTupleDeclaration::toDt(dt_t **pdt)

dt_t *d = NULL;
for (size_t i = 0; i < dim; i++)
{ Parameter *arg = (Parameter *)tu->arguments->data[i];
{ Parameter *arg = (*tu->arguments)[i];
Expression *e = arg->type->getTypeInfo(NULL);
e = e->optimize(WANTvalue);
e->toDt(&d);
Expand Down Expand Up @@ -834,7 +847,7 @@ Expression *createTypeInfoArray(Scope *sc, Expression *exps[], unsigned dim)
args->setDim(dim);
for (size_t i = 0; i < dim; i++)
{ Parameter *arg = new Parameter(STCin, exps[i]->type, NULL, NULL);
args->data[i] = (void *)arg;
(*args)[i] = arg;
}
TypeTuple *tup = new TypeTuple(args);
Expression *e = tup->getTypeInfo(sc);
Expand Down

0 comments on commit bde6fff

Please sign in to comment.