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[] = {
|
562
|
27 {DOTS,"...",""},
|
|
28 {LMACRO,"lmacro",""},
|
|
29 {FMACRO,"fmacro",""},
|
|
30 {KONST,"const",""},
|
|
31 {DEFINED,"defined",""},
|
|
32 {ENVIRONMENT,"environment",""},
|
|
33 {CODE,"code",""},
|
|
34 {REGISTER,"register",""},
|
|
35 {VOID,"void",""},
|
|
36 {EXTRN,"extern",""},
|
|
37 {SHORT,"short",""},
|
|
38 {LONG,"long",""},
|
|
39 {TYPE,"type",""},
|
|
40 {SIZEOF,"sizeof",""},
|
|
41 {TYPEDEF,"typedef",""},
|
|
42 {FLABEL,"flabel",""},
|
|
43 {BLABEL,"blabel",""},
|
|
44 {MACRO,"macro",""},
|
|
45 {STRING,"string",""},
|
|
46 {IDENT,"ident",""},
|
|
47 {FIELD,"field",""},
|
|
48 {TAG,"tag",""},
|
|
49 {RESERVE,"reserve",""},
|
|
50 {DEFAULT,"default",""},
|
|
51 {CASE,"case",""},
|
|
52 {SWITCH,"switch",""},
|
|
53 {WHILE,"while",""},
|
|
54 {DO,"do",""},
|
|
55 {FOR,"for",""},
|
|
56 {ELSE,"else",""},
|
|
57 {IF,"if",""},
|
|
58 {CONTINUE,"continue",""},
|
|
59 {BREAK,"break",""},
|
|
60 {RETURN,"return",""},
|
|
61 {GOTO,"goto",""},
|
|
62 {STATIC,"static",""},
|
|
63 {EMPTY,"empty",""},
|
|
64 {FUNCTION,"function","t"},
|
|
65 {UNION,"union",""},
|
|
66 {STRUCT,"struct","vt"},
|
|
67 {ARRAY,"array","tv"},
|
|
68 {POINTER,"pointer","t"},
|
|
69 {UNSIGNED,"unsigned",""},
|
|
70 {CHAR,"char",""},
|
|
71 {INT,"int",""},
|
37
|
72
|
562
|
73 {GVAR,"gvar","vs"},
|
|
74 {RGVAR,"rgvar","vs"},
|
|
75 {CRGVAR,"crgvar","vs"},
|
|
76 {LVAR,"lvar","v"},
|
|
77 {RLVAR,"rlvar","v"},
|
|
78 {CRLVAR,"crlvar","v"},
|
|
79 {CONST,"const","v"},
|
|
80 {FNAME,"fname","n"},
|
|
81 {MUL,"*","e"},
|
|
82 {RINDIRECT,"rindirect","e"},
|
|
83 {CRINDIRECT,"crindirect","e"},
|
|
84 {BAND,"&","e"},
|
|
85 {MINUS,"-","e"},
|
|
86 {LNOT,"!","e"},
|
|
87 {BNOT,"~","e"},
|
|
88 {INC,"++",""},
|
|
89 {POSTINC,"postinc","e"},
|
|
90 {PREINC,"preinc","e"},
|
|
91 {CPOSTINC,"cpostinc","e"},
|
|
92 {CPREINC,"cpreinc","e"},
|
|
93 {DEC,"--",""},
|
|
94 {DIV,"/","ee"},
|
|
95 {UDIV,"/","ee"},
|
|
96 {MUL,"*","ee"},
|
|
97 {UMUL,"*","ee"},
|
|
98 {MOD,"%","ee"},
|
|
99 {UMOD,"%","ee"},
|
|
100 {ADD,"+","ee"},
|
|
101 {SUB,"-","ee"},
|
|
102 {RSHIFT,">>","ee"},
|
|
103 {URSHIFT,">>","ee"},
|
|
104 {LSHIFT,"<<","ee"},
|
|
105 {ULSHIFT,"<<","ee"},
|
|
106 {GT,">","ee"},
|
|
107 {UGT,">","ee"},
|
|
108 {GE,">=","ee"},
|
|
109 {UGE,">=","ee"},
|
|
110 {LT,"<","ee"},
|
|
111 {ULT,"<","ee"},
|
|
112 {LE,"<=","ee"},
|
|
113 {ULE,"<=","ee"},
|
|
114 {EQ,"==","ee"},
|
|
115 {NEQ,"!=","ee"},
|
|
116 {BAND,"&","ee"},
|
|
117 {EOR,"^","ee"},
|
|
118 {BOR,"|","ee"},
|
|
119 {LAND,"&&","ee"},
|
|
120 {LOR,"||","ee"},
|
|
121 {COND,"cond","eee"},
|
|
122 {ASSOP,"assop","eev"},
|
|
123 {CASSOP,"cassop","eev"},
|
|
124 {COMMA,",","ee"},
|
|
125 {LPAR,"(",""},
|
|
126 {RPAR,")",""},
|
|
127 {LBRA,"[",""},
|
|
128 {RBRA,"]",""},
|
|
129 {LC,"{",""},
|
|
130 {RC,"}",""},
|
|
131 {COLON,":","ee"},
|
|
132 {SM,";",""},
|
|
133 {PERIOD,".",""},
|
|
134 {ARROW,"->",""},
|
|
135 {SASS,"sass",""},
|
|
136 {RSTRUCT,"rstruct",""},
|
|
137 {AS+MUL,"*=","ee"},
|
|
138 {AS+UMUL,"*=","ee"},
|
|
139 {AS+DIV,"/=","ee"},
|
|
140 {AS+UDIV,"/=","ee"},
|
|
141 {AS+MOD,"%=","ee"},
|
|
142 {AS+UMOD,"%=","ee"},
|
|
143 {AS+ADD,"+=","ee"},
|
|
144 {AS+MINUS,"-=","ee"},
|
|
145 {AS+RSHIFT,">>=","ee"},
|
|
146 {AS+URSHIFT,">>=","ee"},
|
|
147 {AS+LSHIFT,"<<=","ee"},
|
|
148 {AS+ULSHIFT,"<<=","ee"},
|
|
149 {AS+BAND,"&=","ee"},
|
|
150 {AS+EOR,"^=","ee"},
|
|
151 {AS+BOR,"|=","ee"},
|
0
|
152 };
|
|
153
|
18
|
154 void
|
50
|
155 tree_print_t(int e,int t)
|
|
156 {
|
|
157 printf("# type: ");
|
|
158 tree_print(t);
|
|
159 printf("expr: ");
|
|
160 tree_print(e);
|
|
161 printf("\n");
|
|
162 }
|
|
163
|
|
164 void
|
0
|
165 tree_print(int e)
|
|
166 {
|
|
167 printf("* generate code on type:\n* ");
|
|
168 tree_parse(type);
|
|
169 printf("\n* expr:\n* ");
|
|
170 tree_parse(e);
|
|
171 printf("\n");
|
|
172 }
|
|
173
|
18
|
174 static
|
0
|
175 int tree_level;
|
|
176
|
18
|
177 void
|
0
|
178 tree_parse(int e)
|
|
179 {
|
|
180 tree_node_type *t;
|
|
181 int i,j;
|
|
182 char *s;
|
|
183
|
|
184 if(e<0) {
|
67
|
185 t=find_node(e);
|
|
186 if(t->tree_type==e) {
|
|
187 for(j=0;j<tree_level;j++) putchar(' ');
|
|
188 printf("list(%s)",t->tree_name);
|
0
|
189 }
|
|
190 } else {
|
|
191 i = car(e);
|
67
|
192 t=find_node(e);
|
|
193 if(t->tree_type==i) {
|
|
194 tree_level++;
|
|
195 for(j=0;j<tree_level;j++) putchar(' ');
|
|
196 printf("list(%s",t->tree_name);
|
|
197 for(i=1,s=t->tree_args;*s;s++,i++) {
|
|
198 switch(*s) {
|
|
199 case 'e':
|
|
200 case 't':
|
|
201 printf(",\n*");
|
|
202 tree_parse(heap[e+i]); break;
|
|
203 case 'v':
|
|
204 printf(",%d",heap[e+i]); break;
|
|
205 case 'n':
|
|
206 printf(",%s",((NMTBL *)heap[e+i])->nm); break;
|
|
207 case 's':
|
|
208 printf(",%s",(char *)heap[e+i]); break;
|
|
209 case 'i':
|
|
210 printf(",%d",heap[e+i]); break;
|
0
|
211 }
|
|
212 }
|
67
|
213 tree_level--;
|
|
214 printf(")");
|
0
|
215 }
|
|
216 }
|
|
217 }
|
67
|
218
|
|
219 tree_node_type *
|
|
220 find_node(int e) {
|
|
221 int e1,e2;
|
|
222 int first=0;
|
|
223 int last=sizeof(tree_nodes)/sizeof(tree_node_type);
|
|
224 e2=-1;
|
|
225 while (first!=last) {
|
562
|
226 #if 0
|
67
|
227 e1 = (first+last)/2;
|
|
228 if(e2==e1)
|
|
229 return 0;
|
|
230 e2=e1;
|
|
231 if (tree_nodes[e1].tree_type>e)
|
|
232 last = e1;
|
|
233 else if (tree_nodes[e1].tree_type==e)
|
|
234 break;
|
|
235 else if (tree_nodes[e1].tree_type<e)
|
|
236 first = e1;
|
562
|
237 #else
|
|
238 if (tree_nodes[first].tree_type==e)
|
|
239 break;
|
|
240 first++;
|
|
241 #endif
|
67
|
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 *
|
468
|
295 typedef_search(int t,int 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 */
|