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