Commit 45130002 authored by dcw's avatar dcw
Browse files

added all the optimized code gen.

parent 50e945a7
......@@ -9,14 +9,16 @@ static void data_decls( declnlist );
static void one_proto( FILE * , char * , shapelist , BOOL );
static void protos( FILE * , declnlist , BOOL );
static void cons_fn_hdr( FILE * , char * , shapelist );
static void cons_fn_body( char * , shapelist );
static void cons_fn_body( declnlist, char * , shapelist );
static void cons_fns( FILE * , declnlist );
static void print_fns( FILE * , declnlist );
static void print_fn_shape( char * , shapelist );
static void print_param( char * , char * , char * );
static void print_fn_shape( declnlist , shapelist );
static void print_param( char * , paramlist , BOOL );
static void print_all_params( declnlist , shapelist );
static void count_empty_non( shapelist , int *, int * );
static void optimize( declnlist );
static void line( char *, long, long, long, long );
static void declfields( paramlist );
#else
static void init();
static void data_decls();
......@@ -28,14 +30,17 @@ static void cons_fns();
static void print_fns();
static void print_fn_shape();
static void print_param();
static void print_all_params();
static void count_empty_non();
static void optimize();
static void line();
static void declfields();
#endif
BOOL print, verbose; /* print == generate print functions */
BOOL print, verbose, opt; /* print == generate print functions */
/* verbose == be verbose - diagnostics */
/* opt == perform optimizations */
static int numtabs = 0;
static FILE *outfile;
......@@ -77,6 +82,15 @@ static void init( h, c, basename ) FILE *c, *h; char *basename;
}
static void declfields( p ) paramlist p;
{
for( ; p; p=p->next )
{
line( "%s\t%s;", p->type, p->name );
}
}
static void data_decls( decs ) declnlist decs;
{
declnlist d;
......@@ -86,44 +100,72 @@ static void data_decls( decs ) declnlist decs;
for( d=decs; d; d=d->next )
{
line( "typedef struct %s_str *%s;", d->name, d->name );
if( d->Struct )
{
line( "typedef struct %s_str *%s;", d->name, d->name );
} else
{
line( "typedef int %s;", d->name );
}
}
line( "\n" );
for( d=decs; d; d=d->next )
{
line( "/* ---- Type %s ---- */\n", d->name );
n = 0;
for( s = d->shapes; s; s=s->next )
if( d->Defines )
{
line( "#define %s_%s_tag %d",
d->name, s->name, n++ );
for( s = d->shapes; s; s=s->next )
{
if( d->Struct )
{
line( "#define %s_%s_tag %d",
d->name, s->name, n++ );
} else
{
line( "#define %s_%s() %d",
d->name, s->name, n++ );
}
}
line( "" );
}
line( "" );
line( "struct %s_str {", d->name );
indent();
line( "int\ttag;" );
line( "union {" );
indent();
for( s = d->shapes; s; s=s->next )
if( d->Struct )
{
if( s->params )
line( "struct %s_str {", d->name );
indent();
if( d->TagField )
{
line( "struct {" );
line( "int\ttag;" );
}
if( d->Union )
{
line( "union {" );
indent();
for( p = s->params; p; p=p->next )
}
for( s = d->shapes; s; s=s->next )
{
if( d->Union && s->params )
/* need internal structures */
{
line( "struct {" );
indent();
declfields( s->params );
outdent();
line( "} %s;", s->name );
} else
{
line( "%s\t%s;",
p->type, p->name );
declfields( s->params );
}
}
if( d->Union )
{
outdent();
line( "} %s;", s->name );
line( "} u;" );
}
outdent();
line( "};\n" );
}
outdent();
line( "} u;" );
outdent();
line( "};\n" );
}
}
......@@ -157,24 +199,27 @@ static void one_proto( f, name, s, prot ) FILE *f; char *name; shapelist s; BOOL
static void protos( f, d, prot ) FILE *f; declnlist d; BOOL prot;
{
shapelist s;
shapelist shapes;
for( ; d; d = d->next )
{
for( s = d->shapes; s; s = s->next )
shapes = d->shapes;
if( d->UseNull )
{
shapes = shapes->next;
}
for( s = shapes; s; s = s->next )
{
one_proto( f, d->name, s, prot );
}
if( print )
{
fprintf( f, "extern void print_%s(", d->name );
if( prot )
{
fprintf( f, "extern void print_%s( %s );\n",
d->name, d->name );
} else
{
fprintf( f, "extern void print_%s( );\n",
d->name );
fprintf( f, " %s ", d->name );
}
fputs( ");\n", f );
}
}
}
......@@ -201,18 +246,30 @@ static void cons_fn_hdr( f, name, s ) FILE *f; char *name; shapelist s;
}
static void cons_fn_body( name, s ) char *name; shapelist s;
static void cons_fn_body( d, name, s ) declnlist d; char *name; shapelist s;
{
paramlist p;
ASSERT( d->Struct,
("error: cons_func_body called but d->Struct is FALSE!\n") );
line( "{" );
indent();
line( "%s new;\n", name );
line( "%s\tnew;\n", name );
line( "new = NEW( struct %s_str );", name );
line( "new->tag = %s_%s_tag;", name, s->name );
if( d->TagField )
{
line( "new->tag = %s_%s_tag;", name, s->name );
}
for( p=s->params; p; p=p->next )
{
line( "new->u.%s.%s = %s;", s->name, p->name, p->name );
if( d->Union )
{
line( "new->u.%s.%s = %s;",
s->name, p->name, p->name );
} else
{
line( "new->%s = %s;", p->name, p->name );
}
}
line( "return new;" );
outdent();
......@@ -222,55 +279,70 @@ static void cons_fn_body( name, s ) char *name; shapelist s;
static void cons_fns( f, d ) FILE *f; declnlist d;
{
shapelist s;
shapelist s;
shapelist shapes;
usefile( f );
for( ; d; d=d->next )
{
for( s = d->shapes; s; s=s->next )
if( d->Struct )
{
cons_fn_hdr( f, d->name, s );
cons_fn_body( d->name, s );
fprintf( f, "\n\n" );
/* We need constructor functions */
shapes = d->shapes;
if( d->UseNull )
{
/* Null cons func already made */
shapes = shapes->next;
}
for( s = shapes; s; s=s->next )
{
cons_fn_hdr( f, d->name, s );
cons_fn_body( d, d->name, s );
fprintf( f, "\n\n" );
}
}
}
}
static void print_param( shape, ftype, fname ) char *shape, *ftype, *fname;
static void print_param( shape, p, Union ) char *shape; paramlist p; BOOL Union;
{
char pname[200];
sprintf( pname, "p->u.%s.%s", shape, fname );
if( streq( ftype, "int" ) )
if( Union )
{
sprintf( pname, "p->u.%s.%s", shape, p->name );
} else
{
sprintf( pname, "p->%s", p->name );
}
if( streq( p->type, "int" ) )
{
line( "fprintf( f, \"%%d\", %s );", pname );
} else if( streq( ftype, "char" ) )
} else if( streq( p->type, "char" ) )
{
line( "fputc( %s, f );", pname );
} else if( streq( ftype, "BOOL" ) )
} else if( streq( p->type, "BOOL" ) )
{
line( "fputs( %s?\"true\":\"false\", f );", pname );
} else if( streq( ftype, "string" ) )
} else if( streq( p->type, "string" ) )
{
line( "fputs( %s, f );", pname );
} else
{
line( "print_%s( f, %s );", ftype, pname );
line( "print_%s( f, %s );", p->type, pname );
}
}
static void print_fn_shape( typename, s ) char *typename; shapelist s;
static void print_all_params( d, s ) declnlist d; shapelist s;
{
printlist pl;
paramlist p;
int n;
line( "case %s_%s_tag:", typename, s->name );
indent();
if( s->pl == NULL )
{
line( "fputs( \"%s\", f );", s->name );
......@@ -280,7 +352,7 @@ static void print_fn_shape( typename, s ) char *typename; shapelist s;
}
for( p = s->params; p; p = p->next )
{
print_param( s->name, p->type, p->name );
print_param( s->name, p, d->Union );
}
if( s->params )
{
......@@ -300,12 +372,20 @@ static void print_fn_shape( typename, s ) char *typename; shapelist s;
for( p = s->params; --n; p = p->next )
{
ASSERT( p != NULL,
("datadec: bad printitem in shape %s of type %s\n",s->name,typename));
("datadec: bad printitem in shape %s of type %s\n",s->name,d->name));
}
print_param( s->name, p->type, p->name );
print_param( s->name, p, d->Union );
}
}
}
}
static void print_fn_shape( d, s ) declnlist d; shapelist s;
{
line( "case %s_%s_tag:", d->name, s->name );
indent();
print_all_params( d, s );
line( "break;" );
outdent();
}
......@@ -314,28 +394,60 @@ static void print_fn_shape( typename, s ) char *typename; shapelist s;
static void print_fns( f, d ) FILE *f ; declnlist d;
{
shapelist s;
shapelist shapes;
usefile( f );
for( ; d; d = d->next )
{
shapes = d->shapes;
line( "void print_%s( f, p ) FILE *f; %s p;",d->name, d->name );
line( "{" );
indent();
line( "switch( p->tag )" );
line( "{" );
for( s = d->shapes; s; s = s->next )
if( d->UseNull )
{
print_fn_shape( d->name, s );
line( "if( p == NULL )" );
line( "{" );
indent();
print_all_params( d, shapes );
line( "return;" );
outdent();
line( "}" );
shapes = shapes->next;
}
if( d->TagField )
{
line( "switch( p->tag )" );
line( "{" );
for( s = shapes; s; s = s->next )
{
print_fn_shape( d, s );
}
line( "default:" );
indent();
line( "fprintf( stderr," );
line( " \"print_%s: impossible tag %%d\\n\", p->tag );",
d->name );
line( "exit(1);" );
outdent();
line( "}" );
} else if( d->Struct ) /* only one shape left */
{
print_all_params( d, shapes );
} else /* enumerated type */
{
line( "switch( p )" );
line( "{" );
for( s = shapes; s; s = s->next )
{
line( "case %s_%s:", d->name, s->name );
indent();
print_all_params( d, s );
line( "break;" );
outdent();
}
line( "}" );
}
line( "default:" );
indent();
line( "fprintf( stderr," );
line( " \"print_%s: impossible tag %%d\\n\", p->tag );",
d->name );
line( "exit(1);" );
outdent();
line( "}" );
outdent();
line( "}\n" );
}
......@@ -444,6 +556,8 @@ static void optimize( d ) declnlist d;
void make_declns( d, c, h, basename ) declnlist d; FILE *c, *h; char *basename;
{
declnlist dl;
printf( "datadec: Making data declarations\n" );
init( h, c, basename );
......@@ -454,6 +568,14 @@ void make_declns( d, c, h, basename ) declnlist d; FILE *c, *h; char *basename;
data_decls( d );
for( dl = d; dl; dl=dl->next )
{
if( dl->UseNull )
{
line( "#define %s_%s() ((%s)NULL)",
dl->name, dl->shapes->name, dl->name );
}
}
line( "#ifdef HASPROTOS" );
protos( h, d, TRUE );
line( "#else" );
......
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