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