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