Commit | Line | Data |
---|---|---|
262eaf56 | 1 | #ifndef lint |
69023ebb | 2 | static char *sccsid ="@(#)fort.c 4.4 (Berkeley) %G%"; |
262eaf56 RC |
3 | #endif lint |
4 | ||
2b5d4291 SL |
5 | # define FORT |
6 | /* this forces larger trees, etc. */ | |
69023ebb | 7 | # include "pass2.h" |
2b5d4291 SL |
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 | } | |
7360bc74 | 46 | #endif |
2b5d4291 SL |
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[128]; | |
80 | if( n > 0 ){ | |
81 | if( n > 32 ) cerror( "lccopy asked to copy too much" ); | |
82 | if( fread( fbuf, 4, n, lrd ) != n ) cerror( "intermediate file read error" ); | |
83 | for( i=4*n; fbuf[i-1] == '\0' && i>0; --i ) { /* VOID */ } | |
84 | if( i ) { | |
85 | if( fwrite( fbuf, 1, i, stdout ) != i ) cerror( "output file error" ); | |
86 | } | |
87 | } | |
88 | } | |
89 | # endif | |
90 | ||
2b5d4291 SL |
91 | /* stack for reading nodes in postfix form */ |
92 | ||
93 | # define NSTACKSZ 250 | |
94 | ||
95 | NODE * fstack[NSTACKSZ]; | |
96 | NODE ** fsp; /* points to next free position on the stack */ | |
97 | ||
98 | unsigned int offsz; | |
99 | unsigned int caloff(); | |
100 | mainp2( argc, argv ) char *argv[]; { | |
101 | int files; | |
102 | register long x; | |
103 | register NODE *p; | |
104 | ||
105 | offsz = caloff(); | |
106 | files = p2init( argc, argv ); | |
107 | tinit(); | |
108 | ||
109 | ||
110 | if( files ){ | |
111 | while( files < argc && argv[files][0] == '-' ) { | |
112 | ++files; | |
113 | } | |
114 | if( files > argc ) return( nerrors ); | |
115 | lopen( argv[files] ); | |
116 | } | |
117 | else lopen( "" ); | |
118 | ||
119 | fsp = fstack; | |
120 | ||
121 | for(;;){ | |
122 | /* read nodes, and go to work... */ | |
123 | x = lread(); | |
124 | ||
125 | if( xdebug ) fprintf( stderr, "op=%d, val = %d, rest = 0%o\n", FOP(x), VAL(x), (int)REST(x) ); | |
126 | switch( (int)FOP(x) ){ /* switch on opcode */ | |
127 | ||
128 | case 0: | |
129 | fprintf( stderr, "null opcode ignored\n" ); | |
130 | continue; | |
131 | case FTEXT: | |
132 | lccopy( VAL(x) ); | |
133 | printf( "\n" ); | |
134 | continue; | |
135 | ||
136 | case FLBRAC: | |
137 | tmpoff = baseoff = lread(); | |
138 | maxtreg = VAL(x); | |
139 | if( ftnno != REST(x) ){ | |
140 | /* beginning of function */ | |
141 | maxoff = baseoff; | |
142 | ftnno = REST(x); | |
143 | maxtemp = 0; | |
144 | } | |
145 | else { | |
146 | if( baseoff > maxoff ) maxoff = baseoff; | |
147 | /* maxoff at end of ftn is max of autos and temps | |
148 | over all blocks in the function */ | |
149 | } | |
150 | setregs(); | |
151 | continue; | |
152 | ||
153 | case FRBRAC: | |
154 | SETOFF( maxoff, ALSTACK ); | |
155 | eobl2(); | |
156 | continue; | |
157 | ||
158 | case FEOF: | |
159 | return( nerrors ); | |
160 | ||
161 | case FSWITCH: | |
162 | uerror( "switch not yet done" ); | |
163 | for( x=VAL(x); x>0; --x ) lread(); | |
164 | continue; | |
165 | ||
166 | case ICON: | |
167 | p = talloc(); | |
168 | p->in.op = ICON; | |
169 | p->in.type = REST(x); | |
170 | p->tn.rval = 0; | |
171 | p->tn.lval = lread(); | |
172 | if( VAL(x) ){ | |
173 | #ifndef FLEXNAMES | |
174 | lcread( p->in.name, 2 ); | |
175 | #else | |
176 | p->in.name = lnread(); | |
177 | #endif | |
178 | } | |
179 | #ifndef FLEXNAMES | |
180 | else p->in.name[0] = '\0'; | |
181 | #else | |
182 | else p->in.name = ""; | |
183 | #endif | |
184 | ||
185 | bump: | |
186 | p->in.su = 0; | |
187 | p->in.rall = NOPREF; | |
188 | *fsp++ = p; | |
189 | if( fsp >= &fstack[NSTACKSZ] ) uerror( "expression depth exceeded" ); | |
190 | continue; | |
191 | ||
192 | case NAME: | |
193 | p = talloc(); | |
194 | p->in.op = NAME; | |
195 | p->in.type = REST(x); | |
196 | p->tn.rval = 0; | |
197 | if( VAL(x) ) p->tn.lval = lread(); | |
198 | else p->tn.lval = 0; | |
199 | #ifndef FLEXNAMES | |
200 | lcread( p->in.name, 2 ); | |
201 | #else | |
202 | p->in.name = lnread(); | |
203 | #endif | |
204 | goto bump; | |
205 | ||
206 | case OREG: | |
207 | p = talloc(); | |
208 | p->in.op = OREG; | |
209 | p->in.type = REST(x); | |
210 | p->tn.rval = VAL(x); | |
211 | p->tn.lval = lread(); | |
212 | #ifndef FLEXNAMES | |
213 | lcread( p->in.name, 2 ); | |
214 | #else | |
215 | p->in.name = lnread(); | |
216 | #endif | |
217 | goto bump; | |
218 | ||
219 | case REG: | |
220 | p = talloc(); | |
221 | p->in.op = REG; | |
222 | p->in.type = REST(x); | |
223 | p->tn.rval = VAL(x); | |
224 | rbusy( p->tn.rval, p->in.type ); | |
225 | p->tn.lval = 0; | |
226 | #ifndef FLEXNAMES | |
227 | p->in.name[0] = '\0'; | |
228 | #else | |
229 | p->in.name = ""; | |
230 | #endif | |
231 | goto bump; | |
232 | ||
233 | case FEXPR: | |
234 | lineno = REST(x); | |
235 | if( VAL(x) ) lcread( filename, VAL(x) ); | |
236 | if( fsp == fstack ) continue; /* filename only */ | |
237 | if( --fsp != fstack ) uerror( "expression poorly formed" ); | |
238 | if( lflag ) lineid( lineno, filename ); | |
239 | tmpoff = baseoff; | |
240 | p = fstack[0]; | |
241 | if( edebug ) fwalk( p, eprint, 0 ); | |
242 | # ifdef MYREADER | |
243 | MYREADER(p); | |
244 | # endif | |
245 | ||
246 | nrecur = 0; | |
247 | delay( p ); | |
248 | reclaim( p, RNULL, 0 ); | |
249 | ||
250 | allchk(); | |
251 | tcheck(); | |
252 | continue; | |
253 | ||
69023ebb | 254 | case FLABEL: |
2b5d4291 SL |
255 | if( VAL(x) ){ |
256 | tlabel(); | |
257 | } | |
258 | else { | |
259 | label( (int) REST(x) ); | |
260 | } | |
261 | continue; | |
262 | ||
263 | case GOTO: | |
264 | if( VAL(x) ) { | |
265 | cbgen( 0, (int) REST(x), 'I' ); /* unconditional branch */ | |
266 | continue; | |
267 | } | |
268 | /* otherwise, treat as unary */ | |
269 | goto def; | |
270 | ||
271 | case STASG: | |
272 | case STARG: | |
273 | case STCALL: | |
274 | case UNARY STCALL: | |
275 | /* | |
276 | * size and alignment come from next long words | |
277 | */ | |
278 | p = talloc(); | |
279 | p -> stn.stsize = lread(); | |
280 | p -> stn.stalign = lread(); | |
281 | goto defa; | |
282 | default: | |
283 | def: | |
284 | p = talloc(); | |
285 | defa: | |
286 | p->in.op = FOP(x); | |
287 | p->in.type = REST(x); | |
288 | ||
289 | switch( optype( p->in.op ) ){ | |
290 | ||
291 | case BITYPE: | |
292 | p->in.right = *--fsp; | |
293 | p->in.left = *--fsp; | |
294 | goto bump; | |
295 | ||
296 | case UTYPE: | |
297 | p->in.left = *--fsp; | |
298 | p->tn.rval = 0; | |
299 | goto bump; | |
300 | ||
301 | case LTYPE: | |
302 | uerror( "illegal leaf node: %d", p->in.op ); | |
303 | exit( 1 ); | |
304 | } | |
305 | } | |
306 | } | |
307 | } |