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