|
|
1.1 root 1: /*
2: * COCOA optimizer
3: * gets memory, reads in optimizer temp file
4: * spits out parser
5: */
6:
7: #include <limits.h>
8: #include "action.h"
9:
10: struct actn *atab;
11: struct go2n *gotab;
12: int *patab, *pgotab;
13: int *pdl;
14: #include "yacc.h"
15:
16: callopt()
17: {
18: extern yyredns, yypact;
19: frlset();
20: atab = (struct actn *)yalloc(yypact, sizeof *atab);
21: gotab = (struct go2n *)yalloc(yyredns, sizeof *gotab);
22: patab = (int *)yalloc(nstates, sizeof *patab);
23: pgotab = (int *)yalloc(nnonterm, sizeof *pgotab);
24: pdl = (int *)yalloc(nprod, sizeof *pdl);
25: cpyparse();
26: }
27:
28: pronts()
29: {
30: register i;
31: for (i=0; i<nprod; i++)
32: pdl[i] = -prdptr[i]->p_left - NTBASE;
33:
34: warray("yypdnt", pdl, nprod, 0);
35: }
36:
37: rdgos()
38: {
39: register i, size;
40: struct go2n g2, *pgo;
41: int oldi;
42:
43: pgo = gotab;
44: for(i=1; i<nnonterm; i++) {
45: pgotab[i] = 2*(pgo-gotab);
46: fread(&g2, sizeof g2, 1, optout);
47: if( g2.from != (YYGOTO<<YYACTSH | i) )
48: tmperr();
49: size = g2.to;
50: fread(pgo, sizeof *pgo, size, optout);
51: if( (oldi = findgo2(pgo, size, i)) >= 0 )
52: pgotab[i] = oldi;
53: else
54: pgo += size;
55: }
56: warray("yypgo", pgotab, nnonterm, 0);
57: warray("yygo", gotab, 2*(pgo-gotab), 1);
58: }
59:
60: rdacts()
61: {
62: register i, size, j;
63: struct actn act, *pa;
64: int oldi;
65:
66: pa = atab;
67: for(i=0; i<nstates; i++) {
68: patab[i] = 2*(pa-atab);
69: fread(&act, sizeof act, 1, optout);
70: if( act.a_no != (YYPACTION<<YYACTSH | i) )
71: tmperr();
72: size = act.a_chr;
73: fread(pa, sizeof *pa, size, optout);
74: for(j=0; j<size; j++)
75: if( pa[j].a_chr!=YYOTHERS )
76: pa[j].a_chr = trmptr[pa[j].a_chr]->s_val;
77: if( (oldi = findact(pa, size, i)) >= 0 )
78: patab[i] = oldi;
79: else
80: pa += size;
81: }
82: warray("yypa", patab, nstates, 0);
83: warray("yyact", atab, 2*(pa-atab), 1);
84: }
85:
86: prodls()
87: {
88: register i;
89: for(i=0; i<nprod; i++)
90: pdl[i] = prodl(prdptr[i]);
91: warray("yypn", pdl, nprod, 0);
92: }
93:
94: warray(s, a, n, sw)
95: char *s;
96: int *a, n, sw;
97: {
98: register i, v;
99: char *type = "int";
100:
101: if (!sw) { /* !sw means array of array refs */
102: type = "char";
103: for (i = 0; i < n; i++) {
104: if (a[i] > UCHAR_MAX) {
105: type = "short";
106: break;
107: }
108: }
109: }
110: fprintf(tabout, "unsigned %s %s[%d] = {\n", type, s, n--);
111:
112: i = 0;
113: do {
114: switch(v = a[i]) {
115: case YYEOFVAL:
116: fputs("YYEOFVAL", tabout);
117: break;
118: case YYERRVAL:
119: fputs("YYERRVAL", tabout);
120: break;
121: case YYOTHERS:
122: fputs("YYOTHERS", tabout);
123: break;
124: default:
125: if (!sw || (0 > v))
126: fprintf(tabout, "%d", v);
127: else
128: fprintf(tabout, "0x%x", v);
129: }
130:
131: if (i != n)
132: fputc(',', tabout);
133:
134: fputc(((++i%8 == 0) ? '\n' : ' '), tabout);
135: } while (i <= n);
136:
137: if (i%8 != 0)
138: fputc('\n', tabout);
139: fprintf(tabout, "};\n");
140: }
141:
142: tmperr()
143: {
144: yyerror(NLNO|FATAL, "temp file error in optimizer");
145: }
146:
147: cpyparse()
148: {
149: register FILE *fparse, *actin;
150: register c;
151: if( (fparse = fopen(parser, "r")) == NULL )
152: yyerror(NLNO|FATAL, "can't find parser");
153: if( (actin = fopen(acttmp, "r")) == NULL )
154: yyerror(NLNO|FATAL, "someone lost action temp file");
155: while ((c = getc (fparse)) != EOF)
156: if (c == '$') {
157: switch (c = getc(fparse)) {
158: case 'A':
159: while ((c = getc(actin)) != EOF)
160: putc (c, tabout);
161: break;
162:
163: case 'D':
164: rewopt();
165: pronts();
166: prodls();
167: rdgos();
168: rdacts();
169: break;
170:
171: default:
172: putc('$', tabout);
173: break;
174: }
175: } else
176: putc(c, tabout);
177: fclose(actin);
178: fclose(fparse);
179: fclose(tabout);
180: }
181:
182: findgo2(gtp,s,n)
183: struct go2n *gtp;
184: int s, n;
185: {
186: register os;
187: register struct go2n *np, *op;
188: int i;
189: for(i=1; i<n; i++) {
190: os = (pgotab[i+1] - pgotab[i]) / 2;
191: if( os != s )
192: continue;
193: op = gotab + pgotab[i]/2;
194: np = gtp;
195: while( op->from==np->from && op->to==np->to && os-- ) {
196: op++;
197: np++;
198: }
199: if( os==0 ) {
200: ndupgos += (pgotab[i+1] - pgotab[i])/2;
201: return( pgotab[i] );
202: }
203: }
204: return(-1);
205: }
206:
207: findact(atp, s, n)
208: struct actn *atp;
209: {
210: register struct actn *op, *np;
211: register os;
212: int i;
213:
214: for(i=0; i<n; i++) {
215: os = (patab[i+1] - patab[i])/2;
216: if( os!=s )
217: continue;
218: np = atab + patab[i]/2;
219: op = atp;
220: while(op->a_no==np->a_no && op->a_chr==np->a_chr && os--){
221: op++;
222: np++;
223: }
224: if( os==0 ) {
225: ndupacts += (patab[i+1] - patab[i])/2;
226: return( patab[i] );
227: }
228: }
229: return(-1);
230: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.