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