Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20020520162701

Diff

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