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