Commit f52c95b7 authored by dcw's avatar dcw
Browse files

added tail_optimize code

parent cdc38a23
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <dcw.h> #include <dcw.h>
#include <string.h>
#include "struct.h" #include "struct.h"
...@@ -9,10 +10,19 @@ BOOL opt; /* opt == perform optimizations */ ...@@ -9,10 +10,19 @@ BOOL opt; /* opt == perform optimizations */
BOOL verbose; /* verbose == be verbose - diagnostics */ BOOL verbose; /* verbose == be verbose - diagnostics */
/*
/^#ifdef HASPROTOS
!/endif$
stat %
*/
#ifdef HASPROTOS #ifdef HASPROTOS
static void optimize_decln( declnlist ); static void optimize_decln( decln );
static BOOL tail_optimize( decln );
#else #else
static void optimize_decln(); static void optimize_decln();
static BOOL tail_optimize();
#endif #endif
...@@ -35,7 +45,7 @@ void optimize( d ) declnlist d; ...@@ -35,7 +45,7 @@ void optimize( d ) declnlist d;
} }
static void optimize_decln( d ) declnlist d; /* JUST THE FIRST */ static void optimize_decln( d ) decln d;
{ {
int t, e, ne; int t, e, ne;
shapelist s; shapelist s;
...@@ -61,7 +71,8 @@ static void optimize_decln( d ) declnlist d; /* JUST THE FIRST */ ...@@ -61,7 +71,8 @@ static void optimize_decln( d ) declnlist d; /* JUST THE FIRST */
d->TagField = ne>1 || ( ne>0 && t>2); d->TagField = ne>1 || ( ne>0 && t>2);
d->Union = ne>1; d->Union = ne>1;
d->UseNull = ne>0 && firstempty; d->UseNull = ne>0 && firstempty;
d->PutLoop = TRUE;
d->PutLoop = tail_optimize( d );
if( verbose ) if( verbose )
{ {
...@@ -88,3 +99,41 @@ static void optimize_decln( d ) declnlist d; /* JUST THE FIRST */ ...@@ -88,3 +99,41 @@ static void optimize_decln( d ) declnlist d; /* JUST THE FIRST */
ASSERT( implies(d->Union,d->TagField), 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) );
} }
/*
Given a data decln, check if a tail recursion optimization is possible.
This is when the last print item of any shape is a number corresponding
to a field that is the same type as the decln itself.
Return TRUE if the optimization is possible.
*/
static BOOL tail_optimize( d ) decln d;
{
shapelist s;
printlist pl;
param p;
int n;
for( s = d->shapes; s != NULL; s = s->next )
{
if( s->pl != NULL )
{
/* find the last print item */
for( pl = s->pl; pl->next != NULL; pl = pl->next );
if( pl->item->tag == printitem_is_num )
{
p = findnthparam( pl->item->num, s->params,
s->name, d->name );
if( streq( p->type, d->name ) )
{
return TRUE;
}
}
}
}
return FALSE;
}
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