optimize.c 2.84 KB
Newer Older
dcw's avatar
dcw committed
1
2
3
4
/* optimize.c */


#include <dcw.h>
dcw's avatar
dcw committed
5
#include <string.h>
dcw's avatar
dcw committed
6
7
8
9
10
11
12
#include "struct.h"


BOOL opt;		/* opt     == perform optimizations    */
BOOL verbose;		/* verbose == be verbose - diagnostics */


dcw's avatar
dcw committed
13
14
15
16
17
18
19
/*
/^#ifdef HASPROTOS
!/endif$
stat %
*/


dcw's avatar
dcw committed
20
#ifdef HASPROTOS
dcw's avatar
dcw committed
21
22
static void optimize_decln( decln );
static BOOL tail_optimize( decln );
dcw's avatar
dcw committed
23
24
#else
static void optimize_decln();
dcw's avatar
dcw committed
25
static BOOL tail_optimize();
dcw's avatar
dcw committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#endif


#define implies( a, b ) (!(a) || (b))


void optimize( d ) declnlist d;
{
	for( ; d != NULL; d = d->next )
	{
		if( opt )
		{
			optimize_decln( d );
		} else
		{
			d->Defines = d->TagField = d->Struct = d->Union = TRUE;
42
			d->UseNull = d->PutLoop = FALSE;
dcw's avatar
dcw committed
43
44
45
46
47
		}
	}
}


dcw's avatar
dcw committed
48
static void optimize_decln( d ) decln d;
dcw's avatar
dcw committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
{
	int		t, e, ne;
	shapelist	s;
	BOOL		firstempty = d->shapes->params == NULL;

	e = ne = 0;
	for( s = d->shapes; s != NULL; s = s->next )
	{
		if( s->params == NULL ) e++; else ne++;
	}

	t = e+ne;

/*
	Need Defines when:	">1 shape"
	Need Struct when:	"some data to be stored"
	Need TagField when:	"struct && (union or >2 shapes)"
	Need Union when:	">1 chunks of data to be stored"
	Can  UseNull when:	"firstempty and some data elsewhere"
 */
	d->Defines	= t>1;
	d->Struct	= ne>0;
	d->TagField	= ne>1 || ( ne>0 && t>2);
	d->Union	= ne>1;
	d->UseNull	= ne>0 && firstempty;
dcw's avatar
dcw committed
74
75

	d->PutLoop	= tail_optimize( d );
dcw's avatar
dcw committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

	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->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) );
	ASSERT( implies(d->TagField,d->Defines),
		("optimizer error: type %s has TagField&!Defines\n",d->name) );

	/******* Not sure about this one.. Want it, but can't prove it! ******/
	ASSERT( implies(d->Union,d->TagField),
		("optimizer error: type %s has Union&!TagField\n",d->name) );
}
dcw's avatar
dcw committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139


/*
	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;
}