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