BSD 3 development
[unix-history] / usr / src / cmd / mip / common
CommitLineData
37d5a6d0
BJ
1# ifndef EXIT
2# define EXIT exit
3# endif
4
5int nerrors = 0; /* number of errors */
6
7NODE *NIL; /* pointer which always has 0 in it */
8
9NODE *lastfree; /* pointer to last free node; (for allocator) */
10
11 /* VARARGS1 */
12uerror( s, a ) char *s; { /* nonfatal error message */
13 /* the routine where is different for pass 1 and pass 2;
14 /* it tells where the error took place */
15
16 ++nerrors;
17 where('u');
18 fprintf( stderr, s, a );
19 fprintf( stderr, "\n" );
20 if( nerrors > 30 ) cerror( "too many errors");
21 }
22
23 /* VARARGS1 */
24cerror( s, a, b, c ) char *s; { /* compiler error: die */
25 where('c');
26 if( nerrors && nerrors <= 30 ){ /* give the compiler the benefit of the doubt */
27 fprintf( stderr, "cannot recover from earlier errors: goodbye!\n" );
28 }
29 else {
30 fprintf( stderr, "compiler error: " );
31 fprintf( stderr, s, a, b, c );
32 fprintf( stderr, "\n" );
33 }
34 EXIT(1);
35 }
36
37 /* VARARGS1 */
38int Wflag = 0;
39werror( s, a, b ) char *s; { /* warning */
40 if(Wflag) return;
41 where('w');
42 fprintf( stderr, "warning: " );
43 fprintf( stderr, s, a, b );
44 fprintf( stderr, "\n" );
45 }
46
47tinit(){ /* initialize expression tree search */
48
49 NODE *p;
50
51 for( p=node; p<= &node[TREESZ-1]; ++p ) p->op = FREE;
52 lastfree = node;
53
54 }
55
56# define TNEXT(p) (p== &node[TREESZ-1]?node:p+1)
57
58NODE *
59talloc(){
60 NODE *p, *q;
61
62 q = lastfree;
63 for( p = TNEXT(q); p!=q; p= TNEXT(p))
64 if( p->op ==FREE ) return(lastfree=p);
65
66 cerror( "out of tree space; simplify expression");
67 /* NOTREACHED */
68 }
69
70tcheck(){ /* ensure that all nodes have been freed */
71
72 NODE *p;
73
74 if( !nerrors )
75 for( p=node; p<= &node[TREESZ-1]; ++p )
76 if( p->op != FREE ) cerror( "wasted space: %o", p );
77 tinit();
78 }
79tfree( p ) NODE *p; {
80 /* free the tree p */
81 extern tfree1();
82
83 if( p->op != FREE ) walkf( p, tfree1 );
84
85 }
86
87tfree1(p) NODE *p; {
88 if( p == 0 ) cerror( "freeing blank tree!");
89 else p->op = FREE;
90 }
91
92fwalk( t, f, down ) register NODE *t; int (*f)(); {
93
94 int down1, down2;
95
96 more:
97 down1 = down2 = 0;
98
99 (*f)( t, down, &down1, &down2 );
100
101 switch( optype( t->op ) ){
102
103 case BITYPE:
104 fwalk( t->left, f, down1 );
105 t = t->right;
106 down = down2;
107 goto more;
108
109 case UTYPE:
110 t = t->left;
111 down = down1;
112 goto more;
113
114 }
115 }
116
117walkf( t, f ) register NODE *t; int (*f)(); {
118 register opty;
119
120 opty = optype(t->op);
121
122 if( opty != LTYPE ) walkf( t->left, f );
123 if( opty == BITYPE ) walkf( t->right, f );
124 (*f)( t );
125 }
126
127
128
129int dope[ DSIZE ];
130char *opst[DSIZE];
131
132struct dopest { int dopeop; char opst[8]; int dopeval; } indope[] = {
133
134 NAME, "NAME", LTYPE,
135 STRING, "STRING", LTYPE,
136 REG, "REG", LTYPE,
137 OREG, "OREG", LTYPE,
138 ICON, "ICON", LTYPE,
139 FCON, "FCON", LTYPE,
140 CCODES, "CCODES", LTYPE,
141 UNARY MINUS, "U-", UTYPE,
142 UNARY MUL, "U*", UTYPE,
143 UNARY AND, "U&", UTYPE,
144 UNARY CALL, "UCALL", UTYPE|CALLFLG,
145 UNARY FORTCALL, "UFCALL", UTYPE|CALLFLG,
146 NOT, "!", UTYPE|LOGFLG,
147 COMPL, "~", UTYPE,
148 FORCE, "FORCE", UTYPE,
149 INIT, "INIT", UTYPE,
150 SCONV, "SCONV", UTYPE,
151 PCONV, "PCONV", UTYPE,
152 PLUS, "+", BITYPE|FLOFLG|SIMPFLG|COMMFLG,
153 ASG PLUS, "+=", BITYPE|ASGFLG|ASGOPFLG|FLOFLG|SIMPFLG|COMMFLG,
154 MINUS, "-", BITYPE|FLOFLG|SIMPFLG,
155 ASG MINUS, "-=", BITYPE|FLOFLG|SIMPFLG|ASGFLG|ASGOPFLG,
156 MUL, "*", BITYPE|FLOFLG|MULFLG,
157 ASG MUL, "*=", BITYPE|FLOFLG|MULFLG|ASGFLG|ASGOPFLG,
158 AND, "&", BITYPE|SIMPFLG|COMMFLG,
159 ASG AND, "&=", BITYPE|SIMPFLG|COMMFLG|ASGFLG|ASGOPFLG,
160 QUEST, "?", BITYPE,
161 COLON, ":", BITYPE,
162 ANDAND, "&&", BITYPE|LOGFLG,
163 OROR, "||", BITYPE|LOGFLG,
164 CM, ",", BITYPE,
165 COMOP, ",OP", BITYPE,
166 ASSIGN, "=", BITYPE|ASGFLG,
167 DIV, "/", BITYPE|FLOFLG|MULFLG|DIVFLG,
168 ASG DIV, "/=", BITYPE|FLOFLG|MULFLG|DIVFLG|ASGFLG|ASGOPFLG,
169 MOD, "%", BITYPE|DIVFLG,
170 ASG MOD, "%=", BITYPE|DIVFLG|ASGFLG|ASGOPFLG,
171 LS, "<<", BITYPE|SHFFLG,
172 ASG LS, "<<=", BITYPE|SHFFLG|ASGFLG|ASGOPFLG,
173 RS, ">>", BITYPE|SHFFLG,
174 ASG RS, ">>=", BITYPE|SHFFLG|ASGFLG|ASGOPFLG,
175 OR, "|", BITYPE|COMMFLG|SIMPFLG,
176 ASG OR, "|=", BITYPE|COMMFLG|SIMPFLG|ASGFLG|ASGOPFLG,
177 ER, "^", BITYPE|COMMFLG|SIMPFLG,
178 ASG ER, "^=", BITYPE|COMMFLG|SIMPFLG|ASGFLG|ASGOPFLG,
179 INCR, "++", BITYPE|ASGFLG,
180 DECR, "--", BITYPE|ASGFLG,
181 STREF, "->", BITYPE,
182 CALL, "CALL", BITYPE|CALLFLG,
183 FORTCALL, "FCALL", BITYPE|CALLFLG,
184 EQ, "==", BITYPE|LOGFLG,
185 NE, "!=", BITYPE|LOGFLG,
186 LE, "<=", BITYPE|LOGFLG,
187 LT, "<", BITYPE|LOGFLG,
188 GE, ">", BITYPE|LOGFLG,
189 GT, ">", BITYPE|LOGFLG,
190 UGT, "UGT", BITYPE|LOGFLG,
191 UGE, "UGE", BITYPE|LOGFLG,
192 ULT, "ULT", BITYPE|LOGFLG,
193 ULE, "ULE", BITYPE|LOGFLG,
194 ARS, "A>>", BITYPE,
195 TYPE, "TYPE", LTYPE,
196 LB, "[", BITYPE,
197 CBRANCH, "CBRANCH", BITYPE,
198 FLD, "FLD", UTYPE,
199 PMCONV, "PMCONV", BITYPE,
200 PVCONV, "PVCONV", BITYPE,
201 RETURN, "RETURN", BITYPE|ASGFLG|ASGOPFLG,
202 CAST, "CAST", BITYPE|ASGFLG|ASGOPFLG,
203 GOTO, "GOTO", UTYPE,
204 STASG, "STASG", BITYPE|ASGFLG,
205 STARG, "STARG", UTYPE,
206 STCALL, "STCALL", BITYPE|CALLFLG,
207 UNARY STCALL, "USTCALL", UTYPE|CALLFLG,
208
209-1, 0
210};
211
212mkdope(){
213 register struct dopest *q;
214
215 for( q = indope; q->dopeop >= 0; ++q ){
216 dope[q->dopeop] = q->dopeval;
217 opst[q->dopeop] = q->opst;
218 }
219 }
220tprint( t ) TWORD t; { /* output a nice description of the type of t */
221
222 static char * tnames[] = {
223 "undef",
224 "farg",
225 "char",
226 "short",
227 "int",
228 "long",
229 "float",
230 "double",
231 "strty",
232 "unionty",
233 "enumty",
234 "moety",
235 "uchar",
236 "ushort",
237 "unsigned",
238 "ulong",
239 "?", "?"
240 };
241
242 for(;; t = DECREF(t) ){
243
244 if( ISPTR(t) ) printf( "PTR " );
245 else if( ISFTN(t) ) printf( "FTN " );
246 else if( ISARY(t) ) printf( "ARY " );
247 else {
248 printf( "%s", tnames[t] );
249 return;
250 }
251 }
252 }