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