1*10935Srrh #ifndef lint
2*10935Srrh static char sccsid[] = "@(#)y4.c 4.1 (Berkeley) 02/11/83";
3*10935Srrh #endif not lint
4*10935Srrh
5*10935Srrh # include "dextern"
6*10935Srrh
7*10935Srrh # define a amem
8*10935Srrh # define mem mem0
9*10935Srrh # define pa indgo
10*10935Srrh # define yypact temp1
11*10935Srrh # define greed tystate
12*10935Srrh
13*10935Srrh int * ggreed = lkst[0].lset;
14*10935Srrh int * pgo = wsets[0].ws.lset;
15*10935Srrh int *yypgo = &nontrst[0].tvalue;
16*10935Srrh
17*10935Srrh int maxspr = 0; /* maximum spread of any entry */
18*10935Srrh int maxoff = 0; /* maximum offset into a array */
19*10935Srrh int *pmem = mem;
20*10935Srrh int *maxa;
21*10935Srrh # define NOMORE -1000
22*10935Srrh
23*10935Srrh int nxdb = 0;
24*10935Srrh int adb = 0;
25*10935Srrh
callopt()26*10935Srrh callopt(){
27*10935Srrh
28*10935Srrh register i, *p, j, k, *q;
29*10935Srrh
30*10935Srrh /* read the arrays from tempfile and set parameters */
31*10935Srrh
32*10935Srrh if( (finput=fopen(TEMPNAME,"r")) == NULL ) error( "optimizer cannot open tempfile" );
33*10935Srrh
34*10935Srrh pgo[0] = 0;
35*10935Srrh yypact[0] = 0;
36*10935Srrh nstate = 0;
37*10935Srrh nnonter = 0;
38*10935Srrh for(;;){
39*10935Srrh switch( gtnm() ){
40*10935Srrh
41*10935Srrh case '\n':
42*10935Srrh yypact[++nstate] = (--pmem) - mem;
43*10935Srrh case ',':
44*10935Srrh continue;
45*10935Srrh
46*10935Srrh case '$':
47*10935Srrh break;
48*10935Srrh
49*10935Srrh default:
50*10935Srrh error( "bad tempfile" );
51*10935Srrh }
52*10935Srrh break;
53*10935Srrh }
54*10935Srrh
55*10935Srrh yypact[nstate] = yypgo[0] = (--pmem) - mem;
56*10935Srrh
57*10935Srrh for(;;){
58*10935Srrh switch( gtnm() ){
59*10935Srrh
60*10935Srrh case '\n':
61*10935Srrh yypgo[++nnonter]= pmem-mem;
62*10935Srrh case ',':
63*10935Srrh continue;
64*10935Srrh
65*10935Srrh case EOF:
66*10935Srrh break;
67*10935Srrh
68*10935Srrh default:
69*10935Srrh error( "bad tempfile" );
70*10935Srrh }
71*10935Srrh break;
72*10935Srrh }
73*10935Srrh
74*10935Srrh yypgo[nnonter--] = (--pmem) - mem;
75*10935Srrh
76*10935Srrh
77*10935Srrh
78*10935Srrh for( i=0; i<nstate; ++i ){
79*10935Srrh
80*10935Srrh k = 32000;
81*10935Srrh j = 0;
82*10935Srrh q = mem + yypact[i+1];
83*10935Srrh for( p = mem + yypact[i]; p<q ; p += 2 ){
84*10935Srrh if( *p > j ) j = *p;
85*10935Srrh if( *p < k ) k = *p;
86*10935Srrh }
87*10935Srrh if( k <= j ){ /* nontrivial situation */
88*10935Srrh /* temporarily, kill this for compatibility
89*10935Srrh j -= k; /* j is now the range */
90*10935Srrh if( k > maxoff ) maxoff = k;
91*10935Srrh }
92*10935Srrh greed[i] = (yypact[i+1]-yypact[i]) + 2*j;
93*10935Srrh if( j > maxspr ) maxspr = j;
94*10935Srrh }
95*10935Srrh
96*10935Srrh /* initialize ggreed table */
97*10935Srrh
98*10935Srrh for( i=1; i<=nnonter; ++i ){
99*10935Srrh ggreed[i] = 1;
100*10935Srrh j = 0;
101*10935Srrh /* minimum entry index is always 0 */
102*10935Srrh q = mem + yypgo[i+1] -1;
103*10935Srrh for( p = mem+yypgo[i]; p<q ; p += 2 ) {
104*10935Srrh ggreed[i] += 2;
105*10935Srrh if( *p > j ) j = *p;
106*10935Srrh }
107*10935Srrh ggreed[i] = ggreed[i] + 2*j;
108*10935Srrh if( j > maxoff ) maxoff = j;
109*10935Srrh }
110*10935Srrh
111*10935Srrh
112*10935Srrh /* now, prepare to put the shift actions into the a array */
113*10935Srrh
114*10935Srrh for( i=0; i<ACTSIZE; ++i ) a[i] = 0;
115*10935Srrh maxa = a;
116*10935Srrh
117*10935Srrh for( i=0; i<nstate; ++i ) {
118*10935Srrh if( greed[i]==0 && adb>1 ) fprintf( ftable, "State %d: null\n", i );
119*10935Srrh pa[i] = YYFLAG1;
120*10935Srrh }
121*10935Srrh
122*10935Srrh while( (i = nxti()) != NOMORE ) {
123*10935Srrh if( i >= 0 ) stin(i);
124*10935Srrh else gin(-i);
125*10935Srrh
126*10935Srrh }
127*10935Srrh
128*10935Srrh if( adb>2 ){ /* print a array */
129*10935Srrh for( p=a; p <= maxa; p += 10){
130*10935Srrh fprintf( ftable, "%4d ", p-a );
131*10935Srrh for( i=0; i<10; ++i ) fprintf( ftable, "%4d ", p[i] );
132*10935Srrh fprintf( ftable, "\n" );
133*10935Srrh }
134*10935Srrh }
135*10935Srrh /* write out the output appropriate to the language */
136*10935Srrh
137*10935Srrh aoutput();
138*10935Srrh
139*10935Srrh osummary();
140*10935Srrh ZAPFILE(TEMPNAME);
141*10935Srrh }
142*10935Srrh
gin(i)143*10935Srrh gin(i){
144*10935Srrh
145*10935Srrh register *p, *r, *s, *q1, *q2;
146*10935Srrh
147*10935Srrh /* enter gotos on nonterminal i into array a */
148*10935Srrh
149*10935Srrh ggreed[i] = 0;
150*10935Srrh
151*10935Srrh q2 = mem+ yypgo[i+1] - 1;
152*10935Srrh q1 = mem + yypgo[i];
153*10935Srrh
154*10935Srrh /* now, find a place for it */
155*10935Srrh
156*10935Srrh for( p=a; p < &a[ACTSIZE]; ++p ){
157*10935Srrh if( *p ) continue;
158*10935Srrh for( r=q1; r<q2; r+=2 ){
159*10935Srrh s = p + *r +1;
160*10935Srrh if( *s ) goto nextgp;
161*10935Srrh if( s > maxa ){
162*10935Srrh if( (maxa=s) > &a[ACTSIZE] ) error( "a array overflow" );
163*10935Srrh }
164*10935Srrh }
165*10935Srrh /* we have found a spot */
166*10935Srrh
167*10935Srrh *p = *q2;
168*10935Srrh if( p > maxa ){
169*10935Srrh if( (maxa=p) > &a[ACTSIZE] ) error( "a array overflow" );
170*10935Srrh }
171*10935Srrh for( r=q1; r<q2; r+=2 ){
172*10935Srrh s = p + *r + 1;
173*10935Srrh *s = r[1];
174*10935Srrh }
175*10935Srrh
176*10935Srrh pgo[i] = p-a;
177*10935Srrh if( adb>1 ) fprintf( ftable, "Nonterminal %d, entry at %d\n" , i, pgo[i] );
178*10935Srrh goto nextgi;
179*10935Srrh
180*10935Srrh nextgp: ;
181*10935Srrh }
182*10935Srrh
183*10935Srrh error( "cannot place goto %d\n", i );
184*10935Srrh
185*10935Srrh nextgi: ;
186*10935Srrh }
187*10935Srrh
stin(i)188*10935Srrh stin(i){
189*10935Srrh register *r, *s, n, flag, j, *q1, *q2;
190*10935Srrh
191*10935Srrh greed[i] = 0;
192*10935Srrh
193*10935Srrh /* enter state i into the a array */
194*10935Srrh
195*10935Srrh q2 = mem+yypact[i+1];
196*10935Srrh q1 = mem+yypact[i];
197*10935Srrh /* find an acceptable place */
198*10935Srrh
199*10935Srrh for( n= -maxoff; n<ACTSIZE; ++n ){
200*10935Srrh
201*10935Srrh flag = 0;
202*10935Srrh for( r = q1; r < q2; r += 2 ){
203*10935Srrh if( (s = *r + n + a ) < a ) goto nextn;
204*10935Srrh if( *s == 0 ) ++flag;
205*10935Srrh else if( *s != r[1] ) goto nextn;
206*10935Srrh }
207*10935Srrh
208*10935Srrh /* check that the position equals another only if the states are identical */
209*10935Srrh
210*10935Srrh for( j=0; j<nstate; ++j ){
211*10935Srrh if( pa[j] == n ) {
212*10935Srrh if( flag ) goto nextn; /* we have some disagreement */
213*10935Srrh if( yypact[j+1] + yypact[i] == yypact[j] + yypact[i+1] ){
214*10935Srrh /* states are equal */
215*10935Srrh pa[i] = n;
216*10935Srrh if( adb>1 ) fprintf( ftable, "State %d: entry at %d equals state %d\n",
217*10935Srrh i, n, j );
218*10935Srrh return;
219*10935Srrh }
220*10935Srrh goto nextn; /* we have some disagreement */
221*10935Srrh }
222*10935Srrh }
223*10935Srrh
224*10935Srrh for( r = q1; r < q2; r += 2 ){
225*10935Srrh if( (s = *r + n + a ) >= &a[ACTSIZE] ) error( "out of space in optimizer a array" );
226*10935Srrh if( s > maxa ) maxa = s;
227*10935Srrh if( *s != 0 && *s != r[1] ) error( "clobber of a array, pos'n %d, by %d", s-a, r[1] );
228*10935Srrh *s = r[1];
229*10935Srrh }
230*10935Srrh pa[i] = n;
231*10935Srrh if( adb>1 ) fprintf( ftable, "State %d: entry at %d\n", i, pa[i] );
232*10935Srrh return;
233*10935Srrh
234*10935Srrh nextn: ;
235*10935Srrh }
236*10935Srrh
237*10935Srrh error( "Error; failure to place state %d\n", i );
238*10935Srrh
239*10935Srrh }
240*10935Srrh
nxti()241*10935Srrh nxti(){ /* finds the next i */
242*10935Srrh register i, max, maxi;
243*10935Srrh
244*10935Srrh max = 0;
245*10935Srrh
246*10935Srrh for( i=1; i<= nnonter; ++i ) if( ggreed[i] >= max ){
247*10935Srrh max = ggreed[i];
248*10935Srrh maxi = -i;
249*10935Srrh }
250*10935Srrh
251*10935Srrh for( i=0; i<nstate; ++i ) if( greed[i] >= max ){
252*10935Srrh max = greed[i];
253*10935Srrh maxi = i;
254*10935Srrh }
255*10935Srrh
256*10935Srrh if( nxdb ) fprintf( ftable, "nxti = %d, max = %d\n", maxi, max );
257*10935Srrh if( max==0 ) return( NOMORE );
258*10935Srrh else return( maxi );
259*10935Srrh }
260*10935Srrh
osummary()261*10935Srrh osummary(){
262*10935Srrh /* write summary */
263*10935Srrh
264*10935Srrh register i, *p;
265*10935Srrh
266*10935Srrh if( foutput == NULL ) return;
267*10935Srrh i=0;
268*10935Srrh for( p=maxa; p>=a; --p ) {
269*10935Srrh if( *p == 0 ) ++i;
270*10935Srrh }
271*10935Srrh
272*10935Srrh fprintf( foutput, "Optimizer space used: input %d/%d, output %d/%d\n",
273*10935Srrh pmem-mem+1, MEMSIZE, maxa-a+1, ACTSIZE );
274*10935Srrh fprintf( foutput, "%d table entries, %d zero\n", (maxa-a)+1, i );
275*10935Srrh fprintf( foutput, "maximum spread: %d, maximum offset: %d\n", maxspr, maxoff );
276*10935Srrh
277*10935Srrh }
278*10935Srrh
aoutput()279*10935Srrh aoutput(){ /* this version is for C */
280*10935Srrh
281*10935Srrh
282*10935Srrh /* write out the optimized parser */
283*10935Srrh
284*10935Srrh fprintf( ftable, "# define YYLAST %d\n", maxa-a+1 );
285*10935Srrh
286*10935Srrh arout( "yyact", a, (maxa-a)+1 );
287*10935Srrh arout( "yypact", pa, nstate );
288*10935Srrh arout( "yypgo", pgo, nnonter+1 );
289*10935Srrh
290*10935Srrh }
291*10935Srrh
arout(s,v,n)292*10935Srrh arout( s, v, n ) char *s; int *v, n; {
293*10935Srrh
294*10935Srrh register i;
295*10935Srrh
296*10935Srrh fprintf( ftable, "short %s[]={\n", s );
297*10935Srrh for( i=0; i<n; ){
298*10935Srrh if( i%10 == 0 ) fprintf( ftable, "\n" );
299*10935Srrh fprintf( ftable, "%4d", v[i] );
300*10935Srrh if( ++i == n ) fprintf( ftable, " };\n" );
301*10935Srrh else fprintf( ftable, "," );
302*10935Srrh }
303*10935Srrh }
304*10935Srrh
305*10935Srrh
gtnm()306*10935Srrh gtnm(){
307*10935Srrh
308*10935Srrh register s, val, c;
309*10935Srrh
310*10935Srrh /* read and convert an integer from the standard input */
311*10935Srrh /* return the terminating character */
312*10935Srrh /* blanks, tabs, and newlines are ignored */
313*10935Srrh
314*10935Srrh s = 1;
315*10935Srrh val = 0;
316*10935Srrh
317*10935Srrh while( (c=getc(finput)) != EOF ){
318*10935Srrh if( isdigit(c) ){
319*10935Srrh val = val * 10 + c - '0';
320*10935Srrh }
321*10935Srrh else if ( c == '-' ) s = -1;
322*10935Srrh else break;
323*10935Srrh }
324*10935Srrh
325*10935Srrh *pmem++ = s*val;
326*10935Srrh if( pmem > &mem[MEMSIZE] ) error( "out of space" );
327*10935Srrh return( c );
328*10935Srrh
329*10935Srrh }
330