Commit 0c7e05e7 authored by dcw's avatar dcw
Browse files

made more use of line() ie. automatic indentation system.

used fputs instead of fprintf for efficiency
added if( print ) checks.
and print and verbose as public variables (set by main)
parent 4740bfaa
......@@ -5,15 +5,15 @@
#ifdef HASPROTOS
static void init( FILE * , FILE * , char * );
static void data_decls( FILE * , declnlist );
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( FILE * , char * , shapelist );
static void cons_fn_body( char * , shapelist );
static void cons_fns( FILE * , declnlist );
static void print_fns( FILE * , declnlist );
static void print_fn_shape( FILE * , char * , shapelist );
static void print_param( FILE *, char * , char * , char * );
static void print_fn_shape( char * , shapelist );
static void print_param( char * , char * , char * );
static void count_empty_non( shapelist , int *, int * );
static void optimize( declnlist );
static void line( char *, long, long, long, long );
......@@ -34,6 +34,9 @@ static void line();
#endif
BOOL print, verbose; /* print == generate print functions */
/* verbose == be verbose - diagnostics */
static int numtabs = 0;
static FILE *outfile;
......@@ -41,7 +44,7 @@ static FILE *outfile;
#define indent() numtabs++
#define outdent() numtabs--
#define usefile( f ) outfile = f
#define usefile(f) outfile = f, numtabs = 0
/*VARARGS*/
......@@ -57,56 +60,47 @@ static void line( fmt, a, b, c, d ) char *fmt; long a, b, c, d;
static void init( h, c, basename ) FILE *c, *h; char *basename;
{
fprintf( h, "/*\n" );
fprintf( h, "\tAutomatically Generated by DataDec\n" );
fprintf( h, " */\n\n");
fprintf( h, "typedef char *string;\n\n" );
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" );
fprintf( c, " */\n\n");
fprintf( c, "#include <stdio.h>\n" );
fputs( "/*\n", h );
fputs( " * Automatically Generated by DataDec\n", h );
fputs( " */\n\n", h );
fputs( "typedef char *string;\n\n", h );
fputs( "typedef char BOOL;\n", h );
fputs( "#define TRUE 1\n", h );
fputs( "#define FALSE 0\n\n", h );
fputs( "#define NEW(t) ((t *)malloc(sizeof(t)))\n\n\n", h );
fputs( "/*\n", c );
fputs( " * Automatically Generated by DataDec\n", c );
fputs( " */\n\n", c);
fputs( "#include <stdio.h>\n", c );
fprintf( c, "#include \"%s.h\"\n\n\n", basename );
numtabs = 0;
}
static void data_decls( f, dl ) FILE *f; declnlist dl;
static void data_decls( decs ) declnlist decs;
{
declnlist d;
char *name;
shapelist s;
int n;
char *tag;
paramlist p;
declnlist d;
shapelist s;
paramlist p;
int n;
for( d=dl; d; d=d->next )
for( d=decs; d; d=d->next )
{
fprintf( f, "typedef struct %s_str *%s;\n", d->name, d->name );
line( "typedef struct %s_str *%s;", d->name, d->name );
}
fprintf( f, "\n\n" );
for( d=dl; d; d=d->next )
line( "\n" );
for( d=decs; d; d=d->next )
{
name = d->name;
fprintf( f, "/* ---- Type %s ---- */\n\n", name );
line( "/* ---- Type %s ---- */\n", d->name );
n = 0;
for( s = d->shapes; s; s=s->next )
{
fprintf( f, "#define %s_%s_tag %d\n",
name, s->tagname, n++ );
line( "#define %s_%s_tag %d",
d->name, s->name, n++ );
}
fputc( '\n', f );
line( "" );
usefile( f );
numtabs = 0;
line( "struct %s_str {", name );
line( "struct %s_str {", d->name );
indent();
line( "int\ttag;" );
line( "union {" );
......@@ -123,7 +117,7 @@ static void data_decls( f, dl ) FILE *f; declnlist dl;
p->type, p->name );
}
outdent();
line( "} %s;", s->tagname );
line( "} %s;", s->name );
}
}
outdent();
......@@ -134,71 +128,71 @@ static void data_decls( f, dl ) FILE *f; declnlist dl;
}
static void one_proto( f, name, s, protos ) FILE *f; char *name; shapelist s; BOOL protos;
static void one_proto( f, name, s, prot ) FILE *f; char *name; shapelist s; BOOL prot;
{
char *tag;
paramlist p;
BOOL first;
tag = s->tagname;
fprintf( f, "extern %s %s_%s( ", name, name, tag );
if( protos )
fprintf( f, "extern %s %s_%s( ", name, name, s->name );
if( prot )
{
if( s->params == NULL )
{
fprintf( f, "void " );
fputs( "void ", f );
} else
{
first = TRUE;
for( p = s->params; p ; p=p->next, first=FALSE )
{
if( !first ) fprintf( f, ", " );
fprintf( f, "%s ", p->type );
if( !first ) fputs( ", ", f );
fputs( p->type, f );
fputc( ' ', f );
}
}
}
fprintf( f, ");\n" );
fputs( ");\n", f );
}
static void protos( f, d, protos ) FILE *f; declnlist d; BOOL protos;
static void protos( f, d, prot ) FILE *f; declnlist d; BOOL prot;
{
char *name;
declnlist dl;
shapelist s;
shapelist s;
for( dl=d; dl; dl = dl->next )
for( ; d; d = d->next )
{
name = dl->name;
for( s = dl->shapes; s; s=s->next )
for( s = d->shapes; s; s = s->next )
{
one_proto( f, name, s, protos );
one_proto( f, d->name, s, prot );
}
if( print )
{
if( prot )
{
fprintf( f, "extern void print_%s( %s );\n",
d->name, d->name );
} else
{
fprintf( f, "extern void print_%s( );\n",
d->name );
}
}
}
for( dl=d; dl; dl = dl->next )
{
fprintf( f, "extern void print_%s( ", dl->name );
if( protos ) fprintf( f, "%s ", dl->name );
fprintf( f, ");\n" );
}
}
static void cons_fn_hdr( f, name, s ) FILE *f; char *name; shapelist s;
{
char *tag;
paramlist p;
BOOL first;
tag = s->tagname;
fprintf( f, "%s %s_%s( ", name, name, tag );
fprintf( f, "%s %s_%s( ", name, name, s->name );
first = TRUE;
for( p=s->params; p; p=p->next, first=FALSE )
{
if( !first ) fprintf( f, ", " );
fprintf( f, "%s", p->name );
if( !first ) fputs( ", ", f );
fputs( p->name, f );
}
fprintf( f, " ) " );
fputs( " ) ", f );
for( p=s->params; p; p=p->next )
{
fprintf( f, "%s %s; ", p->type, p->name );
......@@ -207,24 +201,18 @@ static void cons_fn_hdr( f, name, s ) FILE *f; char *name; shapelist s;
}
static void cons_fn_body( f, name, s ) FILE *f; char *name; shapelist s;
static void cons_fn_body( name, s ) char *name; shapelist s;
{
char *tag;
paramlist p;
tag = s->tagname;
numtabs = 0;
usefile( f );
line( "{" );
indent();
line( "%s new;\n", name );
line( "new = NEW( struct %s_str );", name );
line( "new->tag = %s_%s_tag;", name, tag );
line( "new->tag = %s_%s_tag;", name, s->name );
for( p=s->params; p; p=p->next )
{
line( "new->u.%s.%s = %s;", tag, p->name, p->name );
line( "new->u.%s.%s = %s;", s->name, p->name, p->name );
}
line( "return new;" );
outdent();
......@@ -234,16 +222,16 @@ static void cons_fn_body( f, name, s ) FILE *f; char *name; shapelist s;
static void cons_fns( f, d ) FILE *f; declnlist d;
{
char *name;
shapelist s;
usefile( f );
for( ; d; d=d->next )
{
name = d->name;
for( s = d->shapes; s; s=s->next )
{
cons_fn_hdr( f, name, s );
cons_fn_body( f, name, s );
cons_fn_hdr( f, d->name, s );
cons_fn_body( d->name, s );
fprintf( f, "\n\n" );
}
}
......@@ -260,14 +248,13 @@ static void print_param( shape, ftype, fname ) char *shape, *ftype, *fname;
line( "fprintf( f, \"%%d\", %s );", pname );
} else if( streq( ftype, "char" ) )
{
line( "fprintf( f, \"%%c\", %s );", pname );
line( "fputc( %s, f );", pname );
} else if( streq( ftype, "BOOL" ) )
{
line( "fprintf( f, \"%%s\", %s?\"true\":\"false\" );",
pname );
line( "fputs( %s?\"true\":\"false\", f );", pname );
} else if( streq( ftype, "string" ) )
{
line( "fprintf( f, \"%%s\", %s );", pname );
line( "fputs( %s, f );", pname );
} else
{
line( "print_%s( f, %s );", ftype, pname );
......@@ -277,18 +264,27 @@ static void print_param( shape, ftype, fname ) char *shape, *ftype, *fname;
static void print_fn_shape( typename, s ) char *typename; shapelist s;
{
printlist pl;
paramlist p;
int n;
printlist pl;
paramlist p;
int n;
line( "case %s_%s_tag:", typename, s->tagname );
line( "case %s_%s_tag:", typename, s->name );
indent();
if( s->pl == NULL )
{
line( "fputs( \"%s\", f );", s->name );
if( s->params )
{
line( "fputc( '(', f );" );
}
for( p = s->params; p; p = p->next )
{
print_param( s->tagname, p->type, p->name );
print_param( s->name, p->type, p->name );
}
if( s->params )
{
line( "fputc( ')', f );" );
}
} else
{
......@@ -296,7 +292,7 @@ static void print_fn_shape( typename, s ) char *typename; shapelist s;
{
if( pl->item->tag == printitem_is_str )
{
line( "fprintf( f, \"%s\" );",
line( "fputs( \"%s\", f );",
pl->item->str );
} else
{
......@@ -304,9 +300,9 @@ 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->tagname,typename));
("datadec: bad printitem in shape %s of type %s\n",s->name,typename));
}
print_param( s->tagname, p->type, p->name );
print_param( s->name, p->type, p->name );
}
}
}
......@@ -320,7 +316,6 @@ static void print_fns( f, d ) FILE *f ; declnlist d;
shapelist s;
usefile( f );
numtabs = 0;
for( ; d; d = d->next )
{
......@@ -413,40 +408,51 @@ static void optimize( d ) declnlist d;
d->Defines = d->TagField = d->Struct =
d->Union = TRUE;
}
if( verbose )
{
#define XXX(x) ( (x)?' ':'!')
printf( "type %s: %cDefines, %cTagField, ",
d->name, XXX(d->Defines),
XXX(d->TagField) );
printf( "%cStruct, %cUnion, %cUseNull\n",
XXX(d->Struct), XXX(d->Union),
XXX(d->UseNull) );
#undef XXX
}
ASSERT( implies(d->Union,d->TagField),
("optimizer error: type %s has Union&~TagField\n",d->name) );
("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) );
("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) );
("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)
);
("optimizer error: type %s has UseNull&!Struct\n",d->name) );
ASSERT( implies(d->TagField,d->Defines),
("optimizer error: type %s has TagField&!Defines\n",d->name) );
}
}
void make_declns( d, c, h, basename ) declnlist d; FILE *c, *h; char *basename;
{
printf( "Making data declarations\n" );
printf( "datadec: Making data declarations\n" );
init( h, c, basename );
optimize( d );
data_decls( h, d );
usefile( h );
data_decls( d );
fprintf( h, "#ifdef HASPROTOS\n" );
line( "#ifdef HASPROTOS" );
protos( h, d, TRUE );
fprintf( h, "#else\n" );
line( "#else" );
protos( h, d, FALSE );
fprintf( h, "#endif\n" );
line( "#endif" );
cons_fns( c, d );
print_fns( c, d );
if( print ) print_fns( c, d );
}
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