Commit 43bfaa55 authored by dcw's avatar dcw
Browse files

added optimization calculation.

not used to alter code gen yet.
parent 92df1cd4
......@@ -37,6 +37,7 @@ static void init( h, c, basename ) FILE *c, *h; char *basename;
fprintf( h, "typedef char BOOL;\n" );
fprintf( h, "#define TRUE 1\n" );
fprintf( h, "#define FALSE 0\n\n" );
fprintf( h, "#define NEW(t) ((t *)malloc(sizeof(t)))\n\n\n" );
fprintf( c, "/*\n" );
fprintf( c, "\tAutomatically Generated by DataDec\n" );
......@@ -44,8 +45,6 @@ static void init( h, c, basename ) FILE *c, *h; char *basename;
fprintf( c, "#include <stdio.h>\n" );
fprintf( c, "#include \"%s.h\"\n\n\n", basename );
fprintf( c, "#define NEW(t) ((t *)malloc(sizeof(t)))\n\n\n" );
}
......@@ -60,8 +59,7 @@ static void data_decls( f, dl ) FILE *f; declnlist dl;
for( d=dl; d; d=d->next )
{
name = d->name;
fprintf( f, "typedef struct %s_str *%s;\n", name, name );
fprintf( f, "typedef struct %s_str *%s;\n", d->name, d->name );
}
fprintf( f, "\n\n" );
for( d=dl; d; d=d->next )
......@@ -288,12 +286,99 @@ static void print_fns( f, d ) FILE *f ; declnlist d;
}
}
static void count_empty_non( s, e, ne ) shapelist s; int *e, *ne;
{
*e = *ne = 0;
for( ; s!=NULL; s = s->next )
{
if( s->params == NULL ) (*e)++; else (*ne)++;
}
}
#define implies( a, b ) (!(a) || (b))
static void optimize( d ) declnlist d;
{
int t, e, ne;
for( ; d != NULL; d = d->next )
{
count_empty_non( d->shapes, &e, &ne );
t = e+ne;
d->Defines = d->TagField = d->Struct =
d->Union = d->UseNull = FALSE;
/* ONE, EMPTY, eg. 'data x = y' => ERROR */
ASSERT( t!=1 || e!=1,
( "datadec: type %s is empty!\n",d->name) );
if( t==1 && ne==1 )
/* ONE, NON EMPTY, eg. 'data x = fred(string z)' */
{
d->Struct = TRUE;
/* !Defines, !TagField, !Union, !UseNull */
} else if( t>1 && ne==0 )
/* >1, ALL EMPTY, eg. 'data x = a | b | c | d | e' */
{
d->Defines = TRUE;
/* !TagField, !Struct, !Union, !UseNull */
} else if( d->shapes->params == NULL ) /* 1st is empty */
{
d->UseNull = d->Struct = TRUE;
if( t==2 && ne==1 )
{
/* TWO, 1ST EMPTY, 2ND NE, eg. 'data x = a | b(char z)' */
d->UseNull = d->Struct = TRUE;
/* !Union, !TagField, !Defines */
} else if( t>2 && ne==1 )
{
/* >TWO, 1ST EMPTY, ONLY ONE NE, eg. 'data x = a | b | c(char z) | d | e' */
d->UseNull = d->Struct =
d->Defines = d->TagField = TRUE;
/* !Union */
} else
{
/* >TWO, 1ST EMPTY, >ONE NE, eg. 'data x = a | b(int y) | c(char z) | d | e' */
d->UseNull = d->Struct =
d->Defines = d->TagField =
d->Union = TRUE;
}
} else
/* ELSE */
{
d->Defines = d->TagField = d->Struct =
d->Union = TRUE;
}
ASSERT( implies(d->Union,d->TagField),
("optimizer error: type %s has Union&~TagField\n",d->name) );
ASSERT( implies(d->Union,d->Struct),
("optimizer error: type %s has Union&~Struct\n",d->name) );
ASSERT( implies(d->TagField,d->Struct),
("optimizer error: type %s has TagField&~Struct\n",d->name) );
ASSERT( implies(d->UseNull,d->Struct),
("optimizer error: type %s has UseNull&~Struct\n",d->name) );
#define eek(x) ( (x)?' ':'~')
printf( "type %s: %cDefines, %cTagField, %cStruct, %cUnion, %cUseNull\n",
d->name, eek(d->Defines), eek(d->TagField),
eek(d->Struct), eek(d->Union), eek(d->UseNull)
);
}
}
void make_declns( d, c, h, basename ) declnlist d; FILE *c, *h; char *basename;
{
printf( "Making data declarations\n" );
init( h, c, basename );
optimize( d );
data_decls( h, d );
fprintf( h, "#ifdef HASPROTOS\n" );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment