Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20020930173457

Diff

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