Research V7 development
[unix-history] / usr / src / cmd / mip / fort.c
CommitLineData
3c5d933b
SJ
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 */
81
82mainp2( argc, argv ) char *argv[]; {
83 int files;
84 register long x;
85 register NODE *p;
86
87 files = p2init( argc, argv );
88 tinit();
89
90
91 if( files ){
92 while( files < argc && argv[files][0] == '-' ) {
93 ++files;
94 }
95 if( files > argc ) return( nerrors );
96 lopen( argv[files] );
97 }
98 else lopen( "" );
99
100 fsp = fstack;
101
102 for(;;){
103 /* read nodes, and go to work... */
104 x = lread();
105
106 if( xdebug ) fprintf( stderr, "op=%d, val = %d, rest = 0%o\n", FOP(x), VAL(x), (int)REST(x) );
107 switch( (int)FOP(x) ){ /* switch on opcode */
108
109 case 0:
110 fprintf( stderr, "null opcode ignored\n" );
111 continue;
112 case FTEXT:
113 lccopy( VAL(x) );
114 printf( "\n" );
115 continue;
116
117 case FLBRAC:
118 tmpoff = baseoff = lread();
119 maxtreg = VAL(x);
120 if( ftnno != REST(x) ){
121 /* beginning of function */
122 maxoff = baseoff;
123 ftnno = REST(x);
124 maxtemp = 0;
125 }
126 else {
127 if( baseoff > maxoff ) maxoff = baseoff;
128 /* maxoff at end of ftn is max of autos and temps
129 over all blocks in the function */
130 }
131 setregs();
132 continue;
133
134 case FRBRAC:
135 SETOFF( maxoff, ALSTACK );
136 eobl2();
137 continue;
138
139 case FEOF:
140 return( nerrors );
141
142 case FSWITCH:
143 uerror( "switch not yet done" );
144 for( x=VAL(x); x>0; --x ) lread();
145 continue;
146
147 case ICON:
148 p = talloc();
149 p->op = ICON;
150 p->type = REST(x);
151 p->rval = 0;
152 p->lval = lread();
153 if( VAL(x) ){
154 lcread( p->name, 2 );
155 }
156 else p->name[0] = '\0';
157
158 bump:
159 p->su = 0;
160 p->rall = NOPREF;
161 *fsp++ = p;
162 if( fsp >= &fstack[NSTACKSZ] ) uerror( "expression depth exceeded" );
163 continue;
164
165 case NAME:
166 p = talloc();
167 p->op = NAME;
168 p->type = REST(x);
169 p->rval = 0;
170 if( VAL(x) ) p->lval = lread();
171 else p->lval = 0;
172 lcread( p->name, 2 );
173 goto bump;
174
175 case OREG:
176 p = talloc();
177 p->op = OREG;
178 p->type = REST(x);
179 p->rval = VAL(x);
180 p->lval = lread();
181 lcread( p->name, 2 );
182 goto bump;
183
184 case REG:
185 p = talloc();
186 p->op = REG;
187 p->type = REST(x);
188 p->rval = VAL(x);
189 rbusy( p->rval, p->type );
190 p->lval = 0;
191 p->name[0] = '\0';
192 goto bump;
193
194 case FEXPR:
195 lineno = REST(x);
196 if( VAL(x) ) lcread( filename, VAL(x) );
197 if( fsp == fstack ) continue; /* filename only */
198 if( --fsp != fstack ) uerror( "expression poorly formed" );
199 if( lflag ) lineid( lineno, filename );
200 tmpoff = baseoff;
201 p = fstack[0];
202 if( edebug ) fwalk( p, eprint, 0 );
203# ifdef MYREADER
204 MYREADER(p);
205# endif
206
207 nrecur = 0;
208 delay( p );
209 reclaim( p, RNULL, 0 );
210
211 allchk();
212 tcheck();
213 continue;
214
215 case LABEL:
216 if( VAL(x) ){
217 tlabel();
218 }
219 else {
220 label( (int) REST(x) );
221 }
222 continue;
223
224 case GOTO:
225 if( VAL(x) ) {
226 cbgen( 0, (int) REST(x), 'I' ); /* unconditional branch */
227 continue;
228 }
229 /* otherwise, treat as unary */
230 goto def;
231
232 default:
233 def:
234 p = talloc();
235 p->op = FOP(x);
236 p->type = REST(x);
237
238 switch( optype( p->op ) ){
239
240 case BITYPE:
241 p->right = *--fsp;
242 p->left = *--fsp;
243 goto bump;
244
245 case UTYPE:
246 p->left = *--fsp;
247 p->rval = 0;
248 goto bump;
249
250 case LTYPE:
251 uerror( "illegal leaf node: %d", p->op );
252 exit( 1 );
253 }
254 }
255 }
256 }