1
|
1 /* Micro-C tree print routine */
|
|
2 /* $Id$ */
|
|
3
|
0
|
4 #define EXTERN /**/
|
|
5 #include "mc.h"
|
|
6
|
|
7 typedef
|
|
8 struct tree_node {
|
|
9 int tree_type;
|
|
10 char *tree_name;
|
|
11 char *tree_args;
|
|
12 } tree_node_type;
|
|
13
|
67
|
14 extern void tree_print(int e);
|
|
15 extern void tree_parse(int e);
|
|
16 extern void tree_print_t(int e,int t);
|
|
17 static tree_node_type * find_node(int e);
|
|
18 extern void type_print(int type,NMTBL *n,FILE *out);
|
74
|
19 extern void type_print1(int type,NMTBL *n,FILE *out,int cont);
|
68
|
20 extern void sym_print(int type,FILE *out);
|
67
|
21
|
|
22 /* ascendant order for binary search */
|
|
23
|
18
|
24 static
|
0
|
25 tree_node_type tree_nodes[] = {
|
67
|
26 {(-45),"...",""},
|
|
27 {(-44),"lmacro",""},
|
|
28 {(-43),"fmacro",""},
|
|
29 {(-42),"konst",""},
|
|
30 {(-41),"defined",""},
|
|
31 {(-40),"environment",""},
|
|
32 {(-39),"code",""},
|
|
33 {(-38),"register",""},
|
|
34 {(-37),"void",""},
|
|
35 {(-36),"extern",""},
|
|
36 {(-35),"short",""},
|
|
37 {(-34),"long",""},
|
|
38 {(-33),"type",""},
|
|
39 {(-32),"sizeof",""},
|
|
40 {(-31),"typedef",""},
|
|
41 {(-30),"flabel",""},
|
|
42 {(-29),"blabel",""},
|
|
43 {(-28),"macro",""},
|
|
44 {(-27),"string",""},
|
|
45 {(-26),"ident",""},
|
|
46 {(-25),"field",""},
|
|
47 {(-24),"tag",""},
|
|
48 {(-23),"reserve",""},
|
|
49 {(-22),"default",""},
|
|
50 {(-21),"case",""},
|
|
51 {(-20),"switch",""},
|
|
52 {(-19),"while",""},
|
|
53 {(-18),"do",""},
|
|
54 {(-17),"for",""},
|
|
55 {(-16),"else",""},
|
|
56 {(-15),"if",""},
|
|
57 {(-14),"continue",""},
|
|
58 {(-13),"break",""},
|
|
59 {(-12),"return",""},
|
|
60 {(-11),"goto",""},
|
|
61 {(-10),"static",""},
|
|
62 {(-9),"empty",""},
|
|
63 {(-8),"function","t"},
|
|
64 {(-7),"union",""},
|
|
65 {(-6),"struct","vt"},
|
|
66 {(-5),"array","tv"},
|
|
67 {(-4),"pointer","t"},
|
|
68 {(-3),"unsigned",""},
|
|
69 {(-2),"char",""},
|
|
70 {(-1),"int",""},
|
37
|
71
|
67
|
72 {1,"gvar","vs"},
|
|
73 {2,"rgvar","vs"},
|
|
74 {3,"crgvar","vs"},
|
|
75 {4,"lvar","v"},
|
|
76 {5,"rlvar","v"},
|
|
77 {6,"crlvar","v"},
|
|
78 {7,"const","v"},
|
|
79 {8,"fname","n"},
|
|
80 {9,"*","e"},
|
|
81 {10,"rindirect","e"},
|
|
82 {11,"crindirect","e"},
|
|
83 {12,"&","e"},
|
|
84 {13,"-","e"},
|
|
85 {14,"!","e"},
|
|
86 {15,"~","e"},
|
|
87 {16,"++",""},
|
|
88 {17,"postinc","e"},
|
|
89 {18,"preinc","e"},
|
|
90 {19,"cpostinc","e"},
|
|
91 {20,"cpreinc","e"},
|
|
92 {21,"--",""},
|
|
93 {22,"cpostdec","e"},
|
|
94 {23,"cpredec","e"},
|
|
95 {24,"*","ee"},
|
|
96 {25,"*","ee"},
|
|
97 {26,"/","ee"},
|
|
98 {27,"/","ee"},
|
|
99 {28,"%","ee"},
|
|
100 {29,"%","ee"},
|
|
101 {30,"+","ee"},
|
|
102 {31,"-","ee"},
|
|
103 {32,">>","ee"},
|
|
104 {33,">>","ee"},
|
|
105 {34,"<<","ee"},
|
|
106 {35,"<<","ee"},
|
69
|
107 {36,">","ee"},
|
|
108 {37,">","ee"},
|
|
109 {38,">=","ee"},
|
|
110 {39,">=","ee"},
|
|
111 {40,"<","ee"},
|
|
112 {41,"<","ee"},
|
|
113 {42,"<=","ee"},
|
|
114 {43,"<=","ee"},
|
67
|
115 {44,"==","ee"},
|
|
116 {45,"!=","ee"},
|
|
117 {46,"&","ee"},
|
|
118 {47,"^","ee"},
|
|
119 {48,"|","ee"},
|
|
120 {49,"&&","ee"},
|
|
121 {50,"||","ee"},
|
|
122 {51,"cond","eee"},
|
69
|
123 {52," = ","ee"},
|
|
124 {53," = ","ee"},
|
67
|
125 {54,"assop","eev"},
|
|
126 {55,"cassop","eev"},
|
|
127 {56,",","ee"},
|
|
128 {57,"(",""},
|
|
129 {58,")",""},
|
|
130 {59,"[",""},
|
|
131 {60,"]",""},
|
|
132 {61,"{",""},
|
|
133 {62,"}",""},
|
|
134 {63,":","ee"},
|
|
135 {64,";",""},
|
|
136 {65,".",""},
|
|
137 {66,"->",""},
|
|
138 {67,"cname",""},
|
|
139 {68,"sass",""},
|
|
140 {69,"rstruct",""},
|
81
|
141 {00,"=",""},
|
|
142 {AS+24,"*=","ee"},
|
|
143 {AS+25,"*=","ee"},
|
|
144 {AS+26,"/=","ee"},
|
|
145 {AS+27,"/=","ee"},
|
|
146 {AS+28,"%=","ee"},
|
|
147 {AS+29,"%=","ee"},
|
|
148 {AS+30,"+=","ee"},
|
|
149 {AS+31,"-=","ee"},
|
|
150 {AS+32,">>=","ee"},
|
|
151 {AS+33,">>=","ee"},
|
|
152 {AS+34,"<<=","ee"},
|
|
153 {AS+35,"<<=","ee"},
|
|
154 {AS+46,"&=","ee"},
|
|
155 {AS+47,"^=","ee"},
|
|
156 {AS+48,"|=","ee"},
|
0
|
157 };
|
|
158
|
18
|
159 void
|
50
|
160 tree_print_t(int e,int t)
|
|
161 {
|
|
162 printf("# type: ");
|
|
163 tree_print(t);
|
|
164 printf("expr: ");
|
|
165 tree_print(e);
|
|
166 printf("\n");
|
|
167 }
|
|
168
|
|
169 void
|
0
|
170 tree_print(int e)
|
|
171 {
|
|
172 printf("* generate code on type:\n* ");
|
|
173 tree_parse(type);
|
|
174 printf("\n* expr:\n* ");
|
|
175 tree_parse(e);
|
|
176 printf("\n");
|
|
177 }
|
|
178
|
18
|
179 static
|
0
|
180 int tree_level;
|
|
181
|
18
|
182 void
|
0
|
183 tree_parse(int e)
|
|
184 {
|
|
185 tree_node_type *t;
|
|
186 int i,j;
|
|
187 char *s;
|
|
188
|
|
189 if(e<0) {
|
67
|
190 t=find_node(e);
|
|
191 if(t->tree_type==e) {
|
|
192 for(j=0;j<tree_level;j++) putchar(' ');
|
|
193 printf("list(%s)",t->tree_name);
|
0
|
194 }
|
|
195 } else {
|
|
196 i = car(e);
|
67
|
197 t=find_node(e);
|
|
198 if(t->tree_type==i) {
|
|
199 tree_level++;
|
|
200 for(j=0;j<tree_level;j++) putchar(' ');
|
|
201 printf("list(%s",t->tree_name);
|
|
202 for(i=1,s=t->tree_args;*s;s++,i++) {
|
|
203 switch(*s) {
|
|
204 case 'e':
|
|
205 case 't':
|
|
206 printf(",\n*");
|
|
207 tree_parse(heap[e+i]); break;
|
|
208 case 'v':
|
|
209 printf(",%d",heap[e+i]); break;
|
|
210 case 'n':
|
|
211 printf(",%s",((NMTBL *)heap[e+i])->nm); break;
|
|
212 case 's':
|
|
213 printf(",%s",(char *)heap[e+i]); break;
|
|
214 case 'i':
|
|
215 printf(",%d",heap[e+i]); break;
|
0
|
216 }
|
|
217 }
|
67
|
218 tree_level--;
|
|
219 printf(")");
|
0
|
220 }
|
|
221 }
|
|
222 }
|
67
|
223
|
|
224 tree_node_type *
|
|
225 find_node(int e) {
|
|
226 int e1,e2;
|
|
227 int first=0;
|
|
228 int last=sizeof(tree_nodes)/sizeof(tree_node_type);
|
|
229 e2=-1;
|
|
230 while (first!=last) {
|
|
231 e1 = (first+last)/2;
|
|
232 if(e2==e1)
|
|
233 return 0;
|
|
234 e2=e1;
|
|
235 if (tree_nodes[e1].tree_type>e)
|
|
236 last = e1;
|
|
237 else if (tree_nodes[e1].tree_type==e)
|
|
238 break;
|
|
239 else if (tree_nodes[e1].tree_type<e)
|
|
240 first = e1;
|
|
241 }
|
|
242 return &tree_nodes[e1];
|
|
243 }
|
|
244
|
|
245 void struct_type_print(int type,FILE *out)
|
|
246 {
|
|
247 NMTBL *n;
|
|
248 int tags;
|
|
249 if((n=(NMTBL*)cadddr(type))) {
|
|
250 fprintf(out,"%s ",n->nm);
|
|
251 return;
|
|
252 }
|
|
253 if((tags=caddr(type))) {
|
|
254 fprintf(out,"{");
|
|
255 while(tags) {
|
|
256 n=(NMTBL*)caddr(tags);
|
|
257 type_print(car(tags),n,out);
|
|
258 fprintf(out,";");
|
|
259 tags = cadr(tags);
|
|
260 }
|
|
261 fprintf(out,"}");
|
|
262 }
|
|
263 }
|
|
264
|
74
|
265 void function_type_print1(int type,NMTBL *n,FILE *out,int cont)
|
67
|
266 {
|
|
267 int args;
|
74
|
268 type_print1(cadr(type),0,out,cont);
|
67
|
269 if(n) fprintf(out," %s",n->nm);
|
|
270 fprintf(out,"(");
|
|
271 if((args=caddr(type))) {
|
|
272 while (args) {
|
|
273 type_print(car(args),0,out);
|
|
274 args=cadr(args);
|
|
275 if (args) fprintf(out,",");
|
|
276 }
|
|
277 }
|
|
278 fprintf(out,")");
|
|
279 }
|
|
280
|
74
|
281 void function_type_print(int type,NMTBL *n,FILE *out)
|
|
282 {
|
|
283 function_type_print1(type,n,out,0);
|
|
284 }
|
|
285
|
68
|
286 void sym_print(int sym,FILE *out)
|
|
287 {
|
|
288 tree_node_type *tn;
|
|
289 if (!(tn=find_node(sym))) { error(-1); return; }
|
|
290 fprintf(out,"%s",tn->tree_name);
|
|
291 }
|
|
292
|
70
|
293 NMTBL *
|
72
|
294 typedef_search(t,type)
|
70
|
295 {
|
|
296 while(t) {
|
|
297 if (((NMTBL*)car(t))->ty==type)
|
|
298 return (NMTBL*)car(t);
|
|
299 t=cadr(t);
|
|
300 }
|
|
301 return 0;
|
|
302 }
|
|
303
|
74
|
304 void type_print1(int type,NMTBL *n,FILE *out,int cont)
|
67
|
305 {
|
|
306 int t;
|
|
307 tree_node_type *tn;
|
70
|
308 NMTBL *td;
|
72
|
309 int args;
|
70
|
310
|
75
|
311 if(type>0&&(td=typedef_search(typedefed,type))) {
|
74
|
312 if (!cont)
|
72
|
313 fprintf(out,"%s ",td->nm);
|
75
|
314 } else if(type>0&&(td=typedef_search(gtypedefed,type))) {
|
74
|
315 if (!cont)
|
70
|
316 fprintf(out,"%s ",td->nm);
|
|
317 } else if (type<0) {
|
67
|
318 t=type;
|
|
319 if (!(tn=find_node(t))) { error(-1); return; }
|
74
|
320 if (!cont)
|
|
321 fprintf(out,"%s ",tn->tree_name);
|
67
|
322 } else if ((t=car(type))) {
|
|
323 if (!(tn=find_node(t))) { error(-1); return; }
|
|
324 if(t==STRUCT||t==UNION) {
|
74
|
325 if (!cont) {
|
|
326 fprintf(out,"%s ",tn->tree_name);
|
|
327 struct_type_print(type,out);
|
|
328 }
|
67
|
329 } else if(t==CODE) {
|
74
|
330 if (!cont) {
|
|
331 fprintf(out,"%s ",tn->tree_name);
|
|
332 }
|
|
333 function_type_print1(type,n,out,cont);
|
67
|
334 return;
|
|
335 } else if(t==FUNCTION) {
|
74
|
336 function_type_print1(type,n,out,cont);
|
67
|
337 return;
|
|
338 } else if(t==ARRAY) {
|
74
|
339 type_print1(cadr(type),n,out,cont);
|
70
|
340 if (caddr(type))
|
|
341 fprintf(out,"[%d]",caddr(type));
|
|
342 else
|
|
343 fprintf(out,"[]");
|
67
|
344 return;
|
|
345 } else if(t==POINTER) {
|
|
346 t=cadr(type);
|
|
347 if(car(t)==FUNCTION) {
|
74
|
348 type_print1(cadr(t),0,out,cont);
|
72
|
349 fprintf(out,"(*");
|
|
350 if(n) fprintf(out,"%s",n->nm);
|
|
351 fprintf(out,")");
|
67
|
352 fprintf(out,"(");
|
72
|
353 if((args=caddr(t))) {
|
|
354 while (args) {
|
|
355 type_print(car(args),0,out);
|
|
356 args=cadr(args);
|
|
357 if (args) fprintf(out,",");
|
|
358 }
|
|
359 }
|
67
|
360 fprintf(out,")");
|
72
|
361 return;
|
|
362 } else if(car(t)==ARRAY) {
|
|
363 fprintf(out,"(*");
|
|
364 type_print(cadr(t),n,out);
|
|
365 if (caddr(type))
|
|
366 fprintf(out,")[%d]",caddr(type));
|
|
367 else
|
|
368 fprintf(out,")[]");
|
|
369 return;
|
|
370 } else {
|
74
|
371 type_print1(t,0,out,cont);
|
72
|
372 fprintf(out,"*");
|
|
373 }
|
67
|
374 }
|
|
375 }
|
|
376 if(n) fprintf(out,"%s",n->nm);
|
|
377 }
|
|
378
|
74
|
379 void type_print(int type,NMTBL *n,FILE *out)
|
|
380 {
|
|
381 type_print1(type,n,out,0);
|
|
382 }
|
|
383
|
67
|
384 /* end */
|