Pristine Ack-5.5
[Ack-5.5.git] / util / ego / share / show.c
1 /* $Id: show.c,v 1.13 1995/11/08 11:09:14 ceriel Exp $ */
2 /*
3  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4  * See the copyright notice in the ACK home directory, in the file "Copyright".
5  */
6 /*  S H O W . C  */
7
8 /* This program can be used to make the output of the 'cf' pass
9  * human readable. It will display either the procedure table,
10  * the datablock table, the basic block table or the EM text,
11  * depending on the flag that is passed as first argument.
12  */
13
14
15
16 #include <stdio.h>
17 #include <em_spec.h>
18 #include <em_pseu.h>
19 #include "types.h"
20 #include "def.h"
21 #include "global.h"
22
23
24 #define BMASK 0377
25
26
27
28
29
30
31 #define space1()        printf("        ")
32 char format[] = "       %-11s%d\n";
33 char lformat[] = "      %-11s%ld\n";
34 char sformat[] = "      %-10s%s\n";
35 char dformat[] = "              %-11s%d\n";
36 char oformat[] = "              %-11s%ld\n";
37
38
39
40 FILE *f;        /* input file */
41
42
43 #define getbyte()       getc(f)
44
45 short getshort()
46 {
47         register n;
48
49         n = getbyte();
50         n |= getbyte() << 8;
51         return n;
52 }
53
54 offset getoff()
55 {
56         register offset n;
57
58         n = getshort() & 0xFFFF;
59         n |= ((offset) getshort() ) << 16;
60         return n;
61 }
62
63
64 int getint()
65 {
66         /* Read an integer from the input file. This routine is
67          * only used when reading a bitvector-set. We expect  an
68          * integer to be either a short or a long.
69          */
70
71         if (sizeof(int) == sizeof(short)) {
72                 return getshort();
73         } else {
74                 return getoff();
75         }
76 }
77
78
79 /* VARARGS 1 */
80 error(s,a) char *s,*a; {
81
82         fprintf(stderr,"error");
83         fprintf(stderr,": ");
84         fprintf(stderr,s,a);
85         fprintf(stderr,"\n");
86         exit(-1);
87 }
88
89 main(argc, argv)
90         int argc;
91         char *argv[];
92 {
93         if (argc != 3 || argv[1][0] != '-') {
94                 error("usage: %s -[ldpbc] filename",argv[0]);
95         }
96         if ((f = fopen(argv[2], "r")) == NULL) {
97                 error("cannot open %s", argv[2]);
98         }
99         switch(argv[1][1]) {
100                 case 'l':
101                         showl();
102                         break;
103                 case 'd':
104                         showd();
105                         break;
106                 case 'p':
107                         showp();
108                         break;
109                 case 'b':
110                         showb();
111                         break;
112                 case 'c':
113                         showc();
114                         break;
115                 default:
116                         error("bad flag");
117         }
118
119         fclose(f);
120         exit(0);
121 }
122
123
124 showcset()
125 {
126         /* print a compact (bitvector) set */
127
128         short size;
129         register short i,j;
130         int w, mask;
131
132         size = getshort();
133         /* # significant bits in bitvector */
134         i = 1;
135         printf(" { ");
136         if (size == 0) {
137                 printf("}\n");
138                 return;
139         }
140         for (;;) {
141                 w = getint();
142                 mask = 1 ;
143                 for (j = 1; j <= sizeof(int)*8; j++) {
144                         if (w & mask) {
145                                 printf("%d ",i);
146                         }
147                         if (i++ == size) {
148                                 printf ("}\n");
149                                 return;
150                         }
151                         mask <<=  1;
152                 }
153         }
154 }
155
156
157
158 showp()
159 {
160         byte b;
161         short n;
162         short all;
163         printf("total number of procs: %d\n\n",getshort());
164         all = getshort();
165         while (TRUE) {
166                 n = getshort();
167                 if (feof(f)) break;
168                 printf("PROC\n");
169                 printf(format,"id =",n);
170                 printf(format,"flags1 =",b = getbyte());
171                 if (b & PF_BODYSEEN) {
172                         printf(format,"# labels =",getshort());
173                         printf(lformat,"# locals =",getoff());
174                         printf(lformat,"# formals =",getoff());
175                         if (all == 1) {
176                                 printf("        changed ="); showcset();
177                                 printf(format,"c_flags =",getshort());
178                                 printf(format,"u_flags =",getshort());
179                                 printf("        calling ="); showcset();
180                         }
181                 } else {
182                         printf("        body not available\n");
183                 }
184         }
185 }
186
187
188 char *pseudo[5] = {"hol", "bss", "rom", "con", "unknown" };
189
190 showd()
191 {
192         short n;
193         printf("total number of objects: %d\n\n",getshort());
194         while (TRUE) {
195                 n = getbyte();
196                 if (feof(f)) break;
197                 switch(n) {
198                         case MARK_DBLOCK:
199                                 printf("DBLOCK\n");
200                                 printf(format,"id =",getshort());
201                                 printf(sformat,"pseudo =",
202                                         pseudo[(short) getbyte()]);
203                                 printf(lformat,"size =",getoff());
204                                 printf(format,"fragment =",getshort());
205                                 printf(format,"flags1 =",
206                                         (short) getbyte());
207                                 break;
208                         case MARK_OBJ:
209                                 printf("        OBJ\n");
210                                 space1();
211                                 printf(format,"id =",getshort());
212                                 space1();
213                                 printf(lformat,"size =",getoff());
214                                 space1();
215                                 printf(lformat,"offset =",getoff());
216                                 break;
217                         case MARK_ARG:
218                                 printf("        VALUE\n");
219                                 space1();
220                                 printf(lformat,"offset =",getoff());
221                                 break;
222                 }
223         }
224 }
225
226
227 /* The mnemonics of the EM instructions and pseudos */
228
229
230 extern char em_mnem[];
231 extern char em_pseu[];
232 char lab_mnem[] = "instrlab";
233 char sym_mnem[] = "datalab";
234
235 showinstr()
236 {
237         short instr;
238         char *s;
239
240         instr = (short) getbyte();
241         if (feof(f)) return FALSE;
242         if (instr >= sp_fmnem && instr <= sp_lmnem) {
243                 s = &(em_mnem[(instr-sp_fmnem) *4]);
244         } else {
245                 if (instr == op_lab) {
246                         s = lab_mnem;
247                 } else {
248                         if (instr == ps_sym) {
249                                 s = sym_mnem;
250                         } else {
251                                 s = &(em_pseu[(instr-sp_fpseu)*4]);
252                         }
253                 }
254         }
255         printf("%s",s);
256         switch((short) getbyte()) {
257                 case OPSHORT:
258                 case OPOBJECT:
259                         printf(" %d", getshort());
260                         break;
261                 case OPPROC:
262                         printf(" $%d",getshort());
263                         break;
264                 case OPINSTRLAB:
265                         printf(" *%d",getshort());
266                         break;
267                 case OPOFFSET:
268                         printf(" %ld", getoff());
269                         break;
270                 case OPLIST:
271                         arglist();
272                         break;
273         }
274         printf("\n");
275         return TRUE;
276 }
277
278
279 showl()
280 {
281         while (showinstr());
282 }
283
284
285
286 arglist()
287 {
288         short length;
289         for (;;) {
290                 switch((short) getbyte()) {
291                         case ARGOBJECT:
292                                 printf(" %d", getshort());
293                                 break;
294                         case ARGPROC:
295                                 printf(" $%d",getshort());
296                                 break;
297                         case ARGINSTRLAB:
298                                 printf(" *%d",getshort());
299                                 break;
300                         case ARGOFF:
301                                 printf(" %ld", getoff());
302                                 break;
303                         case ARGICN:
304                         case ARGUCN:
305                         case ARGFCN:
306                                 printf(" %d",getshort());
307                                 /* Fall through !! */
308                         case ARGSTRING:
309                                 length = getshort();
310                                 putchar(' ');
311                                 putchar('"');
312                                 while (length--) {
313                                         putchar(getbyte());
314                                 }
315                                 putchar('"');
316                                 break;
317                         case ARGCEND:
318                                 return;
319                 }
320         }
321 }
322
323
324
325 showlset()
326 {
327         register short x;
328
329         printf("{ ");
330         while (x = getshort()) {
331                 printf("%d ",x);
332         }
333         printf("}\n");
334 }
335
336
337
338
339 showb()
340 {
341         /* basic block file */
342
343         short n,m;
344
345         while (TRUE) {
346                 n = getshort();
347                 if (feof(f)) break;
348                 if (n == 0) {
349                         printf("Declaration Unit:\n");
350                         printf(dformat,"#instrs =",getshort());
351                         printf("\n");
352                         continue;
353                 }
354                 printf("Control Flow Graph:\n");
355                 printf("number of basic blocks: %d\n",n);
356                 m = getshort(); /* #loops */
357                 while (n--) {
358                         printf("        BASIC BLOCK\n");
359                         printf(dformat,"id =",getshort());
360                         printf(dformat,"# instrs =",getshort());
361                         printf("                succ =");
362                         showlset();
363                         printf("                pred =");
364                         showlset();
365                         printf(dformat,"idom =",getshort());
366                         printf("                loops =");
367                         showlset();
368                         printf(dformat,"flags =",getshort());
369                 }
370                 printf("number of loops: %d\n",m);
371                 while (m--) {
372                         printf("        LOOP\n");
373                         printf(dformat,"id =",getshort());
374                         printf(dformat,"level =",getshort());
375                         printf(dformat,"entry =",getshort());
376                         printf(dformat,"end =",getshort());
377                 }
378                 printf("\n");
379         }
380 }
381
382
383 showc()
384 {
385         int n,m,cnt,t;
386
387         cnt = 1;
388         while(TRUE) {
389                 t = getshort();
390                 if (feof(f)) break;
391                 printf("CALL %d\n",cnt++);
392                 printf(format,"nestlevel =",t);
393                 printf(format,"calling p. =",getshort());
394                 printf(format,"call_id =",getshort());
395                 printf(format,"called p. =",getshort());
396                 printf(format,"looplevel =",getbyte());
397                 printf(format,"flags =",getbyte());
398                 printf(format,"ratio =",getshort());
399                 printf("        actuals:");
400                 n = getshort();
401                 if (n == 0) {
402                         printf("  ---\n");
403                 } else {
404                         while (n--) {
405                                 printf("\n");
406                                 m = getshort();
407                                 printf(oformat,"size =",getoff());
408                                 printf(dformat,"inl =",getbyte());
409                                 while (m--) {
410                                         printf("                ");
411                                         showinstr();
412                                 }
413                         }
414                 }
415         }
416 }