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",""},
|
563
|
38 {USHORT,"unsigned short",""},
|
562
|
39 {LONG,"long",""},
|
|
40 {TYPE,"type",""},
|
|
41 {SIZEOF,"sizeof",""},
|
|
42 {TYPEDEF,"typedef",""},
|
|
43 {FLABEL,"flabel",""},
|
|
44 {BLABEL,"blabel",""},
|
|
45 {MACRO,"macro",""},
|
|
46 {STRING,"string",""},
|
|
47 {IDENT,"ident",""},
|
|
48 {FIELD,"field",""},
|
|
49 {TAG,"tag",""},
|
|
50 {RESERVE,"reserve",""},
|
|
51 {DEFAULT,"default",""},
|
563
|
52 {ATTRIBUTE,"attribute",""},
|
562
|
53 {CASE,"case",""},
|
|
54 {SWITCH,"switch",""},
|
|
55 {WHILE,"while",""},
|
|
56 {DO,"do",""},
|
|
57 {FOR,"for",""},
|
|
58 {ELSE,"else",""},
|
|
59 {IF,"if",""},
|
|
60 {CONTINUE,"continue",""},
|
|
61 {BREAK,"break",""},
|
|
62 {RETURN,"return",""},
|
|
63 {GOTO,"goto",""},
|
|
64 {STATIC,"static",""},
|
|
65 {EMPTY,"empty",""},
|
|
66 {FUNCTION,"function","t"},
|
|
67 {UNION,"union",""},
|
|
68 {STRUCT,"struct","vt"},
|
|
69 {ARRAY,"array","tv"},
|
563
|
70 {POINTER,"*","t"},
|
562
|
71 {UNSIGNED,"unsigned",""},
|
563
|
72 {INLINE,"inline",""},
|
|
73 {SIGNED,"signed",""},
|
562
|
74 {CHAR,"char",""},
|
563
|
75 {UCHAR,"unsigned char",""},
|
562
|
76 {INT,"int",""},
|
563
|
77 {FLOAT,"float",""},
|
|
78 {DOUBLE,"double",""},
|
|
79 {LONGLONG,"long long",""},
|
|
80 {ULONGLONG,"unsigned long long",""},
|
37
|
81
|
562
|
82 {GVAR,"gvar","vs"},
|
|
83 {RGVAR,"rgvar","vs"},
|
|
84 {CRGVAR,"crgvar","vs"},
|
|
85 {LVAR,"lvar","v"},
|
|
86 {RLVAR,"rlvar","v"},
|
|
87 {CRLVAR,"crlvar","v"},
|
|
88 {CONST,"const","v"},
|
|
89 {FNAME,"fname","n"},
|
|
90 {MUL,"*","e"},
|
|
91 {RINDIRECT,"rindirect","e"},
|
|
92 {CRINDIRECT,"crindirect","e"},
|
|
93 {BAND,"&","e"},
|
|
94 {MINUS,"-","e"},
|
|
95 {LNOT,"!","e"},
|
|
96 {BNOT,"~","e"},
|
|
97 {INC,"++",""},
|
|
98 {POSTINC,"postinc","e"},
|
|
99 {PREINC,"preinc","e"},
|
|
100 {CPOSTINC,"cpostinc","e"},
|
|
101 {CPREINC,"cpreinc","e"},
|
|
102 {DEC,"--",""},
|
|
103 {DIV,"/","ee"},
|
|
104 {UDIV,"/","ee"},
|
|
105 {MUL,"*","ee"},
|
|
106 {UMUL,"*","ee"},
|
|
107 {MOD,"%","ee"},
|
|
108 {UMOD,"%","ee"},
|
|
109 {ADD,"+","ee"},
|
|
110 {SUB,"-","ee"},
|
|
111 {RSHIFT,">>","ee"},
|
|
112 {URSHIFT,">>","ee"},
|
|
113 {LSHIFT,"<<","ee"},
|
|
114 {ULSHIFT,"<<","ee"},
|
|
115 {GT,">","ee"},
|
|
116 {UGT,">","ee"},
|
|
117 {GE,">=","ee"},
|
|
118 {UGE,">=","ee"},
|
|
119 {LT,"<","ee"},
|
|
120 {ULT,"<","ee"},
|
|
121 {LE,"<=","ee"},
|
|
122 {ULE,"<=","ee"},
|
|
123 {EQ,"==","ee"},
|
|
124 {NEQ,"!=","ee"},
|
|
125 {BAND,"&","ee"},
|
|
126 {EOR,"^","ee"},
|
|
127 {BOR,"|","ee"},
|
|
128 {LAND,"&&","ee"},
|
|
129 {LOR,"||","ee"},
|
563
|
130 {COND,"?","eee"},
|
|
131 {ASS,"=","ee"},
|
562
|
132 {ASSOP,"assop","eev"},
|
|
133 {CASSOP,"cassop","eev"},
|
|
134 {COMMA,",","ee"},
|
|
135 {LPAR,"(",""},
|
|
136 {RPAR,")",""},
|
|
137 {LBRA,"[",""},
|
|
138 {RBRA,"]",""},
|
|
139 {LC,"{",""},
|
|
140 {RC,"}",""},
|
|
141 {COLON,":","ee"},
|
|
142 {SM,";",""},
|
|
143 {PERIOD,".",""},
|
|
144 {ARROW,"->",""},
|
|
145 {SASS,"sass",""},
|
|
146 {RSTRUCT,"rstruct",""},
|
|
147 {AS+MUL,"*=","ee"},
|
|
148 {AS+UMUL,"*=","ee"},
|
|
149 {AS+DIV,"/=","ee"},
|
|
150 {AS+UDIV,"/=","ee"},
|
|
151 {AS+MOD,"%=","ee"},
|
|
152 {AS+UMOD,"%=","ee"},
|
|
153 {AS+ADD,"+=","ee"},
|
|
154 {AS+MINUS,"-=","ee"},
|
|
155 {AS+RSHIFT,">>=","ee"},
|
|
156 {AS+URSHIFT,">>=","ee"},
|
|
157 {AS+LSHIFT,"<<=","ee"},
|
|
158 {AS+ULSHIFT,"<<=","ee"},
|
|
159 {AS+BAND,"&=","ee"},
|
|
160 {AS+EOR,"^=","ee"},
|
|
161 {AS+BOR,"|=","ee"},
|
0
|
162 };
|
|
163
|
564
|
164 static int
|
|
165 attr_print(int t)
|
|
166 {
|
|
167 while (t>0 && car(t)==ATTRIBUTE) {
|
|
168 switch (caddr(t)) {
|
|
169 case KONST: printf( "const"); break;
|
|
170 case VOLATILE: printf( "volatile"); break;
|
|
171 case RESTRICT: printf( "restrict"); break;
|
|
172 case INLINE: printf( "inline"); break;
|
|
173 }
|
|
174 t = cadr(t);
|
|
175 }
|
|
176 return t;
|
|
177 }
|
|
178
|
18
|
179 void
|
50
|
180 tree_print_t(int e,int t)
|
|
181 {
|
|
182 printf("# type: ");
|
|
183 tree_print(t);
|
|
184 printf("expr: ");
|
|
185 tree_print(e);
|
|
186 printf("\n");
|
|
187 }
|
|
188
|
|
189 void
|
0
|
190 tree_print(int e)
|
|
191 {
|
|
192 printf("* generate code on type:\n* ");
|
|
193 tree_parse(type);
|
|
194 printf("\n* expr:\n* ");
|
|
195 tree_parse(e);
|
|
196 printf("\n");
|
|
197 }
|
|
198
|
18
|
199 static
|
0
|
200 int tree_level;
|
|
201
|
18
|
202 void
|
0
|
203 tree_parse(int e)
|
|
204 {
|
|
205 tree_node_type *t;
|
|
206 int i,j;
|
|
207 char *s;
|
|
208
|
|
209 if(e<0) {
|
67
|
210 t=find_node(e);
|
|
211 if(t->tree_type==e) {
|
|
212 for(j=0;j<tree_level;j++) putchar(' ');
|
|
213 printf("list(%s)",t->tree_name);
|
0
|
214 }
|
|
215 } else {
|
|
216 i = car(e);
|
67
|
217 t=find_node(e);
|
|
218 if(t->tree_type==i) {
|
|
219 tree_level++;
|
|
220 for(j=0;j<tree_level;j++) putchar(' ');
|
|
221 printf("list(%s",t->tree_name);
|
|
222 for(i=1,s=t->tree_args;*s;s++,i++) {
|
|
223 switch(*s) {
|
|
224 case 'e':
|
|
225 case 't':
|
|
226 printf(",\n*");
|
|
227 tree_parse(heap[e+i]); break;
|
|
228 case 'v':
|
|
229 printf(",%d",heap[e+i]); break;
|
|
230 case 'n':
|
|
231 printf(",%s",((NMTBL *)heap[e+i])->nm); break;
|
|
232 case 's':
|
|
233 printf(",%s",(char *)heap[e+i]); break;
|
|
234 case 'i':
|
|
235 printf(",%d",heap[e+i]); break;
|
0
|
236 }
|
|
237 }
|
67
|
238 tree_level--;
|
|
239 printf(")");
|
0
|
240 }
|
|
241 }
|
|
242 }
|
67
|
243
|
|
244 tree_node_type *
|
|
245 find_node(int e) {
|
|
246 int e1,e2;
|
|
247 int first=0;
|
|
248 int last=sizeof(tree_nodes)/sizeof(tree_node_type);
|
563
|
249 // e2=-1;
|
|
250 e2=e%200;
|
|
251 e1=0;
|
67
|
252 while (first!=last) {
|
562
|
253 #if 0
|
67
|
254 e1 = (first+last)/2;
|
|
255 if(e2==e1)
|
|
256 return 0;
|
|
257 e2=e1;
|
|
258 if (tree_nodes[e1].tree_type>e)
|
|
259 last = e1;
|
|
260 else if (tree_nodes[e1].tree_type==e)
|
|
261 break;
|
|
262 else if (tree_nodes[e1].tree_type<e)
|
|
263 first = e1;
|
562
|
264 #else
|
563
|
265 if (tree_nodes[first].tree_type==e2)
|
|
266 return &tree_nodes[first];
|
562
|
267 first++;
|
|
268 #endif
|
67
|
269 }
|
563
|
270 fprintf(stderr,"Unknown ID %d [%d]in find note\n",e2,e);
|
67
|
271 return &tree_nodes[e1];
|
|
272 }
|
|
273
|
|
274 void struct_type_print(int type,FILE *out)
|
|
275 {
|
|
276 NMTBL *n;
|
|
277 int tags;
|
|
278 if((n=(NMTBL*)cadddr(type))) {
|
|
279 fprintf(out,"%s ",n->nm);
|
|
280 return;
|
|
281 }
|
|
282 if((tags=caddr(type))) {
|
|
283 fprintf(out,"{");
|
|
284 while(tags) {
|
|
285 n=(NMTBL*)caddr(tags);
|
|
286 type_print(car(tags),n,out);
|
|
287 fprintf(out,";");
|
|
288 tags = cadr(tags);
|
|
289 }
|
|
290 fprintf(out,"}");
|
|
291 }
|
|
292 }
|
|
293
|
74
|
294 void function_type_print1(int type,NMTBL *n,FILE *out,int cont)
|
67
|
295 {
|
|
296 int args;
|
74
|
297 type_print1(cadr(type),0,out,cont);
|
67
|
298 if(n) fprintf(out," %s",n->nm);
|
|
299 fprintf(out,"(");
|
|
300 if((args=caddr(type))) {
|
|
301 while (args) {
|
|
302 type_print(car(args),0,out);
|
|
303 args=cadr(args);
|
|
304 if (args) fprintf(out,",");
|
|
305 }
|
|
306 }
|
|
307 fprintf(out,")");
|
|
308 }
|
|
309
|
74
|
310 void function_type_print(int type,NMTBL *n,FILE *out)
|
|
311 {
|
|
312 function_type_print1(type,n,out,0);
|
|
313 }
|
|
314
|
68
|
315 void sym_print(int sym,FILE *out)
|
|
316 {
|
|
317 tree_node_type *tn;
|
|
318 if (!(tn=find_node(sym))) { error(-1); return; }
|
|
319 fprintf(out,"%s",tn->tree_name);
|
|
320 }
|
|
321
|
70
|
322 NMTBL *
|
468
|
323 typedef_search(int t,int type)
|
70
|
324 {
|
|
325 while(t) {
|
|
326 if (((NMTBL*)car(t))->ty==type)
|
|
327 return (NMTBL*)car(t);
|
|
328 t=cadr(t);
|
|
329 }
|
|
330 return 0;
|
|
331 }
|
|
332
|
564
|
333 static void n_attr_print(int attr, FILE *out)
|
|
334 {
|
|
335 for(;attr;attr=cadr(attr)) {
|
|
336 switch(car(attr)) {
|
|
337 case INLINE: fprintf(out,"inline "); break;
|
|
338 case CONST: fprintf(out,"const "); break;
|
|
339 case VOLATILE: fprintf(out,"const "); break;
|
|
340 case RESTRICT: fprintf(out,"const "); break;
|
|
341 }
|
|
342 }
|
|
343 }
|
|
344
|
74
|
345 void type_print1(int type,NMTBL *n,FILE *out,int cont)
|
67
|
346 {
|
|
347 int t;
|
|
348 tree_node_type *tn;
|
70
|
349 NMTBL *td;
|
72
|
350 int args;
|
70
|
351
|
564
|
352 if (n) {
|
|
353 if (n->attr) n_attr_print(n->attr,out);
|
|
354 while(type>0 && car(type)==ATTRIBUTE) type=cadr(type);
|
|
355 } else
|
|
356 type = attr_print(type);
|
75
|
357 if(type>0&&(td=typedef_search(typedefed,type))) {
|
74
|
358 if (!cont)
|
72
|
359 fprintf(out,"%s ",td->nm);
|
75
|
360 } else if(type>0&&(td=typedef_search(gtypedefed,type))) {
|
74
|
361 if (!cont)
|
70
|
362 fprintf(out,"%s ",td->nm);
|
|
363 } else if (type<0) {
|
67
|
364 t=type;
|
|
365 if (!(tn=find_node(t))) { error(-1); return; }
|
74
|
366 if (!cont)
|
|
367 fprintf(out,"%s ",tn->tree_name);
|
67
|
368 } else if ((t=car(type))) {
|
|
369 if (!(tn=find_node(t))) { error(-1); return; }
|
|
370 if(t==STRUCT||t==UNION) {
|
74
|
371 if (!cont) {
|
|
372 fprintf(out,"%s ",tn->tree_name);
|
|
373 struct_type_print(type,out);
|
|
374 }
|
67
|
375 } else if(t==CODE) {
|
74
|
376 if (!cont) {
|
|
377 fprintf(out,"%s ",tn->tree_name);
|
|
378 }
|
|
379 function_type_print1(type,n,out,cont);
|
67
|
380 return;
|
|
381 } else if(t==FUNCTION) {
|
74
|
382 function_type_print1(type,n,out,cont);
|
67
|
383 return;
|
|
384 } else if(t==ARRAY) {
|
74
|
385 type_print1(cadr(type),n,out,cont);
|
70
|
386 if (caddr(type))
|
|
387 fprintf(out,"[%d]",caddr(type));
|
|
388 else
|
|
389 fprintf(out,"[]");
|
67
|
390 return;
|
|
391 } else if(t==POINTER) {
|
|
392 t=cadr(type);
|
563
|
393 if(t<0) {
|
|
394 type_print1(t,0,out,cont);
|
|
395 fprintf(out,"*");
|
|
396 } else if(car(t)==FUNCTION) {
|
74
|
397 type_print1(cadr(t),0,out,cont);
|
72
|
398 fprintf(out,"(*");
|
|
399 if(n) fprintf(out,"%s",n->nm);
|
|
400 fprintf(out,")");
|
67
|
401 fprintf(out,"(");
|
72
|
402 if((args=caddr(t))) {
|
|
403 while (args) {
|
|
404 type_print(car(args),0,out);
|
|
405 args=cadr(args);
|
|
406 if (args) fprintf(out,",");
|
|
407 }
|
|
408 }
|
67
|
409 fprintf(out,")");
|
72
|
410 return;
|
|
411 } else if(car(t)==ARRAY) {
|
|
412 fprintf(out,"(*");
|
|
413 type_print(cadr(t),n,out);
|
|
414 if (caddr(type))
|
|
415 fprintf(out,")[%d]",caddr(type));
|
|
416 else
|
|
417 fprintf(out,")[]");
|
|
418 return;
|
|
419 } else {
|
74
|
420 type_print1(t,0,out,cont);
|
72
|
421 fprintf(out,"*");
|
|
422 }
|
67
|
423 }
|
|
424 }
|
|
425 if(n) fprintf(out,"%s",n->nm);
|
|
426 }
|
|
427
|
74
|
428 void type_print(int type,NMTBL *n,FILE *out)
|
|
429 {
|
|
430 type_print1(type,n,out,0);
|
|
431 }
|
|
432
|
67
|
433 /* end */
|