show.c 7.0 KB

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