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