|
|
1.1 root 1: # define FORT
2: /* this forces larger trees, etc. */
3: # include "mfile2"
4: # include "fort.h"
5:
6: /* masks for unpacking longs */
7:
8: # ifndef FOP
9: # define FOP(x) (int)((x)&0377)
10: # endif
11:
12: # ifndef VAL
13: # define VAL(x) (int)(((x)>>8)&0377)
14: # endif
15:
16: # ifndef REST
17: # define REST(x) (((x)>>16)&0177777)
18: # endif
19:
20: FILE * lrd; /* for default reading routines */
21:
22: # ifndef NOLNREAD && FLEXNAMES
23: char *
24: lnread()
25: {
26: char buf[BUFSIZ];
27: register char *cp = buf;
28: register char *limit = &buf[BUFSIZ];
29:
30: for (;;) {
31: if (fread(cp, sizeof (long), 1, lrd) != 1)
32: cerror("intermediate file read error");
33: cp += sizeof (long);
34: if (cp[-1] == 0)
35: break;
36: if (cp >= limit)
37: cerror("lnread overran string buffer");
38: }
39: return (tstr(buf));
40: }
41: # endif NOLNREAD
42:
43: # ifndef NOLREAD
44: long lread(){
45: static long x;
46: if( fread( (char *) &x, 4, 1, lrd ) <= 0 ) cerror( "intermediate file read error" );
47: return( x );
48: }
49: # endif
50:
51: # ifndef NOLOPEN
52: lopen( s ) char *s; {
53: /* if null, opens the standard input */
54: if( *s ){
55: lrd = fopen( s, "r" );
56: if( lrd == NULL ) cerror( "cannot open intermediate file %s", s );
57: }
58: else lrd = stdin;
59: }
60: # endif
61:
62: # ifndef NOLCREAD
63: lcread( cp, n ) char *cp; {
64: if( n > 0 ){
65: if( fread( cp, 4, n, lrd ) != n ) cerror( "intermediate file read error" );
66: }
67: }
68: # endif
69:
70: # ifndef NOLCCOPY
71: lccopy( n ) register n; {
72: register i;
73: static char fbuf[128];
74: if( n > 0 ){
75: if( n > 32 ) cerror( "lccopy asked to copy too much" );
76: if( fread( fbuf, 4, n, lrd ) != n ) cerror( "intermediate file read error" );
77: for( i=4*n; fbuf[i-1] == '\0' && i>0; --i ) { /* VOID */ }
78: if( i ) {
79: if( fwrite( fbuf, 1, i, stdout ) != i ) cerror( "output file error" );
80: }
81: }
82: }
83: # endif
84:
85: /* new opcode definitions */
86:
87: # define FORTOPS 200
88: # define FTEXT 200
89: # define FEXPR 201
90: # define FSWITCH 202
91: # define FLBRAC 203
92: # define FRBRAC 204
93: # define FEOF 205
94: # define FARIF 206
95: # define LABEL 207
96:
97: /* stack for reading nodes in postfix form */
98:
99: # define NSTACKSZ 250
100:
101: NODE * fstack[NSTACKSZ];
102: NODE ** fsp; /* points to next free position on the stack */
103:
104: unsigned int offsz;
105: unsigned int caloff();
106: mainp2( argc, argv ) char *argv[]; {
107: int files;
108: register long x;
109: register NODE *p;
110:
111: offsz = caloff();
112: files = p2init( argc, argv );
113: tinit();
114:
115:
116: if( files ){
117: while( files < argc && argv[files][0] == '-' ) {
118: ++files;
119: }
120: if( files > argc ) return( nerrors );
121: lopen( argv[files] );
122: }
123: else lopen( "" );
124:
125: fsp = fstack;
126:
127: for(;;){
128: /* read nodes, and go to work... */
129: x = lread();
130:
131: if( xdebug ) fprintf( stderr, "op=%d, val = %d, rest = 0%o\n", FOP(x), VAL(x), (int)REST(x) );
132: switch( (int)FOP(x) ){ /* switch on opcode */
133:
134: case 0:
135: fprintf( stderr, "null opcode ignored\n" );
136: continue;
137: case FTEXT:
138: lccopy( VAL(x) );
139: printf( "\n" );
140: continue;
141:
142: case FLBRAC:
143: tmpoff = baseoff = lread();
144: maxtreg = VAL(x);
145: if( ftnno != REST(x) ){
146: /* beginning of function */
147: maxoff = baseoff;
148: ftnno = REST(x);
149: maxtemp = 0;
150: }
151: else {
152: if( baseoff > maxoff ) maxoff = baseoff;
153: /* maxoff at end of ftn is max of autos and temps
154: over all blocks in the function */
155: }
156: setregs();
157: continue;
158:
159: case FRBRAC:
160: SETOFF( maxoff, ALSTACK );
161: eobl2();
162: continue;
163:
164: case FEOF:
165: return( nerrors );
166:
167: case FSWITCH:
168: uerror( "switch not yet done" );
169: for( x=VAL(x); x>0; --x ) lread();
170: continue;
171:
172: case ICON:
173: p = talloc();
174: p->in.op = ICON;
175: p->in.type = REST(x);
176: p->tn.rval = 0;
177: p->tn.lval = lread();
178: if( VAL(x) ){
179: #ifndef FLEXNAMES
180: lcread( p->in.name, 2 );
181: #else
182: p->in.name = lnread();
183: #endif
184: }
185: #ifndef FLEXNAMES
186: else p->in.name[0] = '\0';
187: #else
188: else p->in.name = "";
189: #endif
190:
191: bump:
192: p->in.su = 0;
193: p->in.rall = NOPREF;
194: *fsp++ = p;
195: if( fsp >= &fstack[NSTACKSZ] ) uerror( "expression depth exceeded" );
196: continue;
197:
198: case NAME:
199: p = talloc();
200: p->in.op = NAME;
201: p->in.type = REST(x);
202: p->tn.rval = 0;
203: if( VAL(x) ) p->tn.lval = lread();
204: else p->tn.lval = 0;
205: #ifndef FLEXNAMES
206: lcread( p->in.name, 2 );
207: #else
208: p->in.name = lnread();
209: #endif
210: goto bump;
211:
212: case OREG:
213: p = talloc();
214: p->in.op = OREG;
215: p->in.type = REST(x);
216: p->tn.rval = VAL(x);
217: p->tn.lval = lread();
218: #ifndef FLEXNAMES
219: lcread( p->in.name, 2 );
220: #else
221: p->in.name = lnread();
222: #endif
223: goto bump;
224:
225: case REG:
226: p = talloc();
227: p->in.op = REG;
228: p->in.type = REST(x);
229: p->tn.rval = VAL(x);
230: rbusy( p->tn.rval, p->in.type );
231: p->tn.lval = 0;
232: #ifndef FLEXNAMES
233: p->in.name[0] = '\0';
234: #else
235: p->in.name = "";
236: #endif
237: goto bump;
238:
239: case FEXPR:
240: lineno = REST(x);
241: if( VAL(x) ) lcread( filename, VAL(x) );
242: if( fsp == fstack ) continue; /* filename only */
243: if( --fsp != fstack ) uerror( "expression poorly formed" );
244: if( lflag ) lineid( lineno, filename );
245: tmpoff = baseoff;
246: p = fstack[0];
247: if( edebug ) fwalk( p, eprint, 0 );
248: # ifdef MYREADER
249: MYREADER(p);
250: # endif
251:
252: nrecur = 0;
253: delay( p );
254: reclaim( p, RNULL, 0 );
255:
256: allchk();
257: tcheck();
258: continue;
259:
260: case LABEL:
261: if( VAL(x) ){
262: tlabel();
263: }
264: else {
265: label( (int) REST(x) );
266: }
267: continue;
268:
269: case GOTO:
270: if( VAL(x) ) {
271: cbgen( 0, (int) REST(x), 'I' ); /* unconditional branch */
272: continue;
273: }
274: /* otherwise, treat as unary */
275: goto def;
276:
277: case STASG:
278: case STARG:
279: case STCALL:
280: case UNARY STCALL:
281: /*
282: * size and alignment come from next long words
283: */
284: p = talloc();
285: p -> stn.stsize = lread();
286: p -> stn.stalign = lread();
287: goto defa;
288: default:
289: def:
290: p = talloc();
291: defa:
292: p->in.op = FOP(x);
293: p->in.type = REST(x);
294:
295: switch( optype( p->in.op ) ){
296:
297: case BITYPE:
298: p->in.right = *--fsp;
299: p->in.left = *--fsp;
300: goto bump;
301:
302: case UTYPE:
303: p->in.left = *--fsp;
304: p->tn.rval = 0;
305: goto bump;
306:
307: case LTYPE:
308: uerror( "illegal leaf node: %d", p->in.op );
309: exit( 1 );
310: }
311: }
312: }
313: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.