Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20020325225236

Diff

Diff from 1.2 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/pcc/pcc/mip/common.c

Annotated File View

ragge
1.1
1 /*      common.c        4.5     88/05/11        */
2
ragge
1.2
3 #include <stdarg.h>
4 #include <stdlib.h>
5
ragge
1.1
6 #ifdef PASS1COMMON
7 #include "pass1.h"
8 #else
9 #ifdef PASS2COMMON
10 #include "pass2.h"
11 #endif
12 #endif
13
14 #ifdef FORT
15 #undef BUFSTDERR
16 #endif
17 #ifndef ONEPASS
18 #undef BUFSTDERR
19 #endif
20 # ifndef EXIT
21 # define EXIT exit
22 # endif
23
24 int nerrors = 0;  /* number of errors */
25
26 extern unsigned int offsz;
27
ragge
1.2
28 unsigned int
29 caloff()
30 {
31         int i;
ragge
1.1
32         unsigned int temp;
33         unsigned int off;
ragge
1.2
34
ragge
1.1
35         temp = 1;
36         i = 0;
37         do {
38                 temp <<= 1;
39                 ++i;
ragge
1.2
40         } whiletemp != 0 );
ragge
1.1
41         off = 1 << (i-1);
42         return (off);
ragge
1.2
43 }
ragge
1.1
44
45 NODE *lastfree;  /* pointer to last free node; (for allocator) */
46
ragge
1.2
47 /*
48  * nonfatal error message
49  * the routine where is different for pass 1 and pass 2;
50  * it tells where the error took place
51  */
52 void
53 uerror(char *s, ...)
54 {
55         va_list ap;
ragge
1.1
56
ragge
1.2
57         va_start(aps);
ragge
1.1
58         ++nerrors;
59         where('u');
ragge
1.2
60         vfprintf(stderrsap);
61         fprintf(stderr"\n");
ragge
1.1
62 #ifdef BUFSTDERR
63         fflush(stderr);
64 #endif
ragge
1.2
65         if (nerrors > 30)
66                 cerror("too many errors");
67         va_end(ap);
68 }
69
70 /*
71  * compiler error: die
72  */
73 void
74 cerror(char *s, ...)
75 {
76         va_list ap;
ragge
1.1
77
ragge
1.2
78         va_start(aps);
ragge
1.1
79         where('c');
ragge
1.2
80
81         /* give the compiler the benefit of the doubt */
82         if (nerrors && nerrors <= 30) {
83                 fprintf(stderr,
84                     "cannot recover from earlier errors: goodbye!\n");
85         } else {
86                 fprintf(stderr"compiler error: ");
87                 vfprintf(stderrsap);
88                 fprintf(stderr"\n");
89         }
ragge
1.1
90 #ifdef BUFSTDERR
91         fflush(stderr);
92 #endif
ragge
1.2
93         va_end(ap);
ragge
1.1
94         EXIT(1);
ragge
1.2
95 }
ragge
1.1
96
97 int Wflag = 0/* Non-zero means do not print warnings */
98
ragge
1.2
99 /*
100  * warning
101  */
102 void
103 werror(char *s, ...)
104 {
105         va_list ap;
106
107         if(Wflag)
108                 return;
109         va_start(aps);
ragge
1.1
110         where('w');
ragge
1.2
111         fprintf(stderr"warning: ");
112         vfprintf(stderrsap);
113         fprintf(stderr"\n");
ragge
1.1
114 #ifdef BUFSTDERR
115         fflush(stderr);
116 #endif
ragge
1.2
117 }
ragge
1.1
118
ragge
1.2
119 /*
120  * initialize expression tree search
121  */
122 void
123 tinit()
124 {
125         NODE *p;
ragge
1.1
126
ragge
1.2
127         for (p=nodep<= &node[TREESZ-1]; ++p)
128                 p->in.op = FREE;
ragge
1.1
129         lastfree = node;
ragge
1.2
130 }
ragge
1.1
131
132 # define TNEXT(p) (p== &node[TREESZ-1]?node:p+1)
133
134 NODE *
ragge
1.2
135 talloc()
136 {
ragge
1.1
137         register NODE *p, *q;
138
139         q = lastfree;
140         forp = TNEXT(q); p!=qpTNEXT(p))
141                 ifp->in.op ==FREE )
142                         return(lastfree=p);
143
144         cerror"out of tree space; simplify expression");
145         /* NOTREACHED */
ragge
1.2
146         return NULL;
147 }
ragge
1.1
148
ragge
1.2
149 /*
150  * ensure that all nodes have been freed
151  */
152 void
153 tcheck()
154 {
155         NODE *p;
ragge
1.1
156
157         if( !nerrors )
158                 forp=nodep<= &node[TREESZ-1]; ++p )
159                         ifp->in.op != FREE )
160                                 cerror"wasted space: %o"p );
161         tinit();
162 #ifdef FLEXNAMES
163         freetstr();
164 #endif
ragge
1.2
165 }
ragge
1.1
166
ragge
1.2
167 /*
168  * free the tree p
169  */
170 void
171 tfree(NODE *p)
172 {
173         if (p->in.op != FREE)
174                 walkf(ptfree1);
175 }
ragge
1.1
176
ragge
1.2
177 void
178 tfree1(NODE *p)
179 {
180         if (p == 0)
181                 cerror("freeing blank tree!");
182         else
183                 p->in.op = FREE;
184 }
ragge
1.1
185
ragge
1.2
186 void
187 fwalk(NODE *tint (*f)(NODE *, intint *, int *), int down)
188 {
ragge
1.1
189
190         int down1down2;
191
192         more:
193         down1 = down2 = 0;
194
ragge
1.2
195         (*f)(tdown, &down1, &down2);
ragge
1.1
196
ragge
1.2
197         switch (optypet->in.op )) {
ragge
1.1
198
199         case BITYPE:
200                 fwalkt->in.leftfdown1 );
201                 t = t->in.right;
202                 down = down2;
203                 goto more;
204
205         case UTYPE:
206                 t = t->in.left;
207                 down = down1;
208                 goto more;
209
210         }
ragge
1.2
211 }
ragge
1.1
212
213 #ifndef vax
ragge
1.2
214 void
215 walkf(NODE *tvoid (*f)(NODE *))
216 {
217         int opty;
ragge
1.1
218
219         opty = optype(t->in.op);
220
ragge
1.2
221         if (opty != LTYPE)
222                 walkft->in.leftf );
223         if (opty == BITYPE)
224                 walkft->in.rightf );
225         (*f)(t);
226 }
ragge
1.1
227 #else
228 #define NR      32
229
230 /*
231  * Deliberately avoids recursion -- use this version on machines with
232  * expensive procedure calls.
233  */
234 walkf(tf)
235         register NODE *t;
236         register int (*f)();
237 {
238         NODE *Aat[NR];
239         int Aao[NR];
240         register int i = 1;
241         register int opty = optype(t->in.op);
242         register NODE **at = Aat;
243         register int *ao = Aao;
244
245 #define PUSH(dir, state) \
246         (ao[i] = stateat[i++] = tt = t->in.diropty = optype(t->in.op))
247 #define POP() \
248         (opty = ao[--i], t = at[i])
249
250         do {
251                 switch (opty) {
252                 case LTYPE:     (*f)(t); POP(); break;
253                 case UTYPE:     PUSH(leftLTYPE); break;
254                 case BITYPE:    PUSH(leftBITYPE+1); break;
255                 case BITYPE+1:  PUSH(rightLTYPE); break;
256                 default:
257                         cerror("bad op type in walkf");
258                 }
259                 if (i >= NR) {
260                         walkf(tf);
261                         POP();
262                 }
263         } while (i > 0);
264 }
265 #undef NR
266 #undef PUSH
267 #undef POP
268 #endif
269
270
271
272 int dopeDSIZE ];
273 char *opst[DSIZE];
274
ragge
1.2
275 struct dopest {
276         int dopeop;
277         char opst[8];
278         int dopeval;
279 indope[] = {
280         { NAME"NAME"LTYPE, },
281         { STRING"STRING"LTYPE, },
282         { REG"REG"LTYPE, },
283         { OREG"OREG"LTYPE, },
284         { ICON"ICON"LTYPE, },
285         { FCON"FCON"LTYPE, },
286         { DCON"DCON"LTYPE, },
287         { CCODES"CCODES"LTYPE, },
288         { UNARY MINUS"U-"UTYPE, },
289         { UNARY MUL"U*"UTYPE, },
290         { UNARY AND"U&"UTYPE, },
291         { UNARY CALL"UCALL"UTYPE|CALLFLG, },
292         { UNARY FORTCALL"UFCALL"UTYPE|CALLFLG, },
293         { NOT"!"UTYPE|LOGFLG, },
294         { COMPL"~"UTYPE, },
295         { FORCE"FORCE"UTYPE, },
296         { INIT"INIT"UTYPE, },
297         { SCONV"SCONV"UTYPE, },
298         { PCONV"PCONV"UTYPE, },
299         { PLUS"+"BITYPE|FLOFLG|SIMPFLG|COMMFLG, },
300         { ASG PLUS"+="BITYPE|ASGFLG|ASGOPFLG|FLOFLG|SIMPFLG|COMMFLG, },
301         { MINUS"-"BITYPE|FLOFLG|SIMPFLG, },
302         { ASG MINUS"-="BITYPE|FLOFLG|SIMPFLG|ASGFLG|ASGOPFLG, },
303         { MUL"*"BITYPE|FLOFLG|MULFLG, },
304         { ASG MUL"*="BITYPE|FLOFLG|MULFLG|ASGFLG|ASGOPFLG, },
305         { AND"&"BITYPE|SIMPFLG|COMMFLG, },
306         { ASG AND"&="BITYPE|SIMPFLG|COMMFLG|ASGFLG|ASGOPFLG, },
307         { QUEST"?"BITYPE, },
308         { COLON":"BITYPE, },
309         { ANDAND"&&"BITYPE|LOGFLG, },
310         { OROR"||"BITYPE|LOGFLG, },
311         { CM","BITYPE, },
312         { COMOP",OP"BITYPE, },
313         { ASSIGN"="BITYPE|ASGFLG, },
314         { DIV"/"BITYPE|FLOFLG|MULFLG|DIVFLG, },
315         { ASG DIV"/="BITYPE|FLOFLG|MULFLG|DIVFLG|ASGFLG|ASGOPFLG, },
316         { MOD"%"BITYPE|DIVFLG, },
317         { ASG MOD"%="BITYPE|DIVFLG|ASGFLG|ASGOPFLG, },
318         { LS"<<"BITYPE|SHFFLG, },
319         { ASG LS"<<="BITYPE|SHFFLG|ASGFLG|ASGOPFLG, },
320         { RS">>"BITYPE|SHFFLG, },
321         { ASG RS">>="BITYPE|SHFFLG|ASGFLG|ASGOPFLG, },
322         { OR"|"BITYPE|COMMFLG|SIMPFLG, },
323         { ASG OR"|="BITYPE|COMMFLG|SIMPFLG|ASGFLG|ASGOPFLG, },
324         { ER"^"BITYPE|COMMFLG|SIMPFLG, },
325         { ASG ER"^="BITYPE|COMMFLG|SIMPFLG|ASGFLG|ASGOPFLG, },
326         { INCR"++"BITYPE|ASGFLG, },
327         { DECR"--"BITYPE|ASGFLG, },
328         { STREF"->"BITYPE, },
329         { CALL"CALL"BITYPE|CALLFLG, },
330         { FORTCALL"FCALL"BITYPE|CALLFLG, },
331         { EQ"=="BITYPE|LOGFLG, },
332         { NE"!="BITYPE|LOGFLG, },
333         { LE"<="BITYPE|LOGFLG, },
334         { LT"<"BITYPE|LOGFLG, },
335         { GE">"BITYPE|LOGFLG, },
336         { GT">"BITYPE|LOGFLG, },
337         { UGT"UGT"BITYPE|LOGFLG, },
338         { UGE"UGE"BITYPE|LOGFLG, },
339         { ULT"ULT"BITYPE|LOGFLG, },
340         { ULE"ULE"BITYPE|LOGFLG, },
ragge
1.1
341 #ifdef ARS
ragge
1.2
342         { ARS"A>>"BITYPE, },
ragge
1.1
343 #endif
ragge
1.2
344         { TYPE"TYPE"LTYPE, },
345         { LB"["BITYPE, },
346         { CBRANCH"CBRANCH"BITYPE, },
347         { FLD"FLD"UTYPE, },
348         { PMCONV"PMCONV"BITYPE, },
349         { PVCONV"PVCONV"BITYPE, },
350         { RETURN"RETURN"BITYPE|ASGFLG|ASGOPFLG, },
351         { CAST"CAST"BITYPE|ASGFLG|ASGOPFLG, },
352         { GOTO"GOTO"UTYPE, },
353         { STASG"STASG"BITYPE|ASGFLG, },
354         { STARG"STARG"UTYPE, },
355         { STCALL"STCALL"BITYPE|CALLFLG, },
356         { UNARY STCALL"USTCALL"UTYPE|CALLFLG, },
ragge
1.1
357
ragge
1.2
358         { -1,   "",     0 },
ragge
1.1
359 };
360
ragge
1.2
361 void
362 mkdope()
363 {
364         struct dopest *q;
ragge
1.1
365
366         forq = indopeq->dopeop >= 0; ++q ){
367                 dope[q->dopeop] = q->dopeval;
368                 opst[q->dopeop] = q->opst;
369         }
ragge
1.2
370 }
371
ragge
1.1
372 # ifndef BUG4
ragge
1.2
373 /*
374  * output a nice description of the type of t
375  */
376 void
377 tprint(TWORD t)
378 {
ragge
1.1
379         static char * tnames[] = {
380                 "undef",
381                 "farg",
382                 "char",
383                 "short",
384                 "int",
385                 "long",
386                 "float",
387                 "double",
388                 "strty",
389                 "unionty",
390                 "enumty",
391                 "moety",
392                 "uchar",
393                 "ushort",
394                 "unsigned",
395                 "ulong",
396                 "?""?"
397                 };
398
399         for(;; t = DECREF(t) ){
400
ragge
1.2
401                 if (ISPTR(t))
402                         printf("PTR ");
403                 else if (ISFTN(t))
404                         printf("FTN ");
405                 else if (ISARY(t))
406                         printf("ARY ");
ragge
1.1
407                 else {
ragge
1.2
408                         printf("%s"tnames[t]);
ragge
1.1
409                         return;
410                 }
411         }
ragge
1.2
412 }
ragge
1.1
413 # endif
414
415 #ifdef FLEXNAMES
416 #define NTSTRBUF        40
417 #define TSTRSZ          2048
418 char    itstrbuf[TSTRSZ];
419 char    *tstrbuf[NTSTRBUF] = { itstrbuf };
420 char    **curtstr = tstrbuf;
421 int     tstrused;
422
423 char *
ragge
1.2
424 tstr(char *cp)
ragge
1.1
425 {
426         register int i = strlen(cp);
427         register char *dp;
428         
429         if (tstrused + i >= TSTRSZ) {
430                 if (++curtstr >= &tstrbuf[NTSTRBUF])
431                         cerror("out of temporary string space");
432                 tstrused = 0;
433                 if (*curtstr == 0) {
434                         dp = malloc(TSTRSZ);
435                         if (dp == 0)
436                                 cerror("out of memory (tstr)");
437                         *curtstr = dp;
438                 }
439         }
440         (voidstrcpy(dp = *curtstr+tstrusedcp);
441         tstrused += i + 1;
442         return (dp);
443 }
444 #endif
FishEye: Open Source License registered to PCC.
Atlassian FishEye, CVS analysis. (Version:1.6.3 Build:build-336 2008-11-04) - Administration - Page generated 2014-12-29 13:22 +0100