go.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 A R E D F I L E
  7. *
  8. * G O . C
  9. *
  10. */
  11. #include <stdio.h>
  12. #include <unistd.h>
  13. #include "types.h"
  14. #include "debug.h"
  15. #include "global.h"
  16. #include "get.h"
  17. #include "put.h"
  18. #include "lset.h"
  19. #include "map.h"
  20. #include "alloc.h"
  21. #include "go.h"
  22. #include "files.h"
  23. STATIC bool report_flag = FALSE; /* report #optimizations found? */
  24. #ifdef DEBUG
  25. STATIC bool core_flag = FALSE; /* report core usage? */
  26. #endif
  27. static mach_init(char* machfile, int (*phase_machinit)())
  28. {
  29. /* Read target machine dependent information */
  30. FILE* f;
  31. f = openfile(machfile, "r");
  32. fscanf(f, "%d", &ws);
  33. fscanf(f, "%d", &ps);
  34. if (ws != ps && ps != 2 * ws)
  35. error("illegal pointer size");
  36. (*phase_machinit)(f);
  37. fclose(f);
  38. }
  39. void go(int argc, const char** argv,
  40. int (*initialize)(), int (*optimize)(), int (*phase_machinit)(), int (*proc_flag)())
  41. {
  42. struct files* files = findfiles(argc, argv);
  43. FILE* f, *gf, *f2, *gf2; /* The EM input and output and
  44. * the basic block graphs input and output
  45. */
  46. bblock_p g;
  47. line_p l;
  48. short kind;
  49. int i;
  50. char* p;
  51. bool time_opt = TRUE;
  52. linecount = 0;
  53. opterr = 0;
  54. for (;;)
  55. {
  56. int opt = getopt(files->argc, files->argv, "STM:CQV");
  57. if (opt == -1)
  58. break;
  59. switch (opt)
  60. {
  61. case 'S':
  62. time_opt = FALSE;
  63. break;
  64. case 'T':
  65. time_opt = TRUE;
  66. break;
  67. case 'M':
  68. mach_init(optarg, phase_machinit);
  69. break;
  70. case 'C':
  71. #ifdef DEBUG
  72. core_flag = TRUE;
  73. #endif
  74. break;
  75. case 'Q':
  76. report_flag = TRUE;
  77. break;
  78. case 'V':
  79. verbose_flag = TRUE;
  80. break;
  81. case '?':
  82. proc_flag(argv[optind - 1]);
  83. break;
  84. }
  85. }
  86. time_space_ratio = (time_opt ? 100 : 0);
  87. fproc = getptable(files->pname_in); /* proc table */
  88. fdblock = getdtable(files->dname_in); /* data block table */
  89. (*initialize)();
  90. if (optimize == no_action)
  91. return;
  92. f = openfile(files->lname_in, "r");
  93. gf = openfile(files->bname_in, "r");
  94. f2 = openfile(files->lname_out, "w");
  95. gf2 = openfile(files->bname_out, "w");
  96. mesregs = Lempty_set();
  97. while (getunit(gf, f, &kind, &g, &l, &curproc, TRUE))
  98. {
  99. /* Read the control flow graph and EM text of
  100. * one procedure and optimize it.
  101. */
  102. if (kind == LDATA)
  103. {
  104. putunit(LDATA, (proc_p)0, l, gf2, f2);
  105. continue;
  106. }
  107. OUTTRACE("flow graph of proc %d read", curproc->p_id);
  108. curproc->p_start = g;
  109. /* The global variable curproc points to the
  110. * current procedure. It is set by getgraph
  111. */
  112. (*optimize)(curproc);
  113. putunit(LTEXT, curproc, (line_p)0, gf2, f2);
  114. /* output control flow graph + text */
  115. OUTTRACE("graph of proc %d outputted", curproc->p_id);
  116. Ldeleteset(mesregs);
  117. mesregs = Lempty_set();
  118. }
  119. fclose(f);
  120. fclose(f2);
  121. fclose(gf);
  122. fclose(gf2);
  123. f = openfile(files->dname_out, "w");
  124. putdtable(fdblock, f);
  125. /* fclose(f); done by putdtable */
  126. f = openfile(files->pname_out, "w");
  127. putptable(fproc, f, TRUE);
  128. /* fclose(f); done by putptable */
  129. core_usage();
  130. }
  131. int no_action() {}
  132. void core_usage(void)
  133. {
  134. #ifdef DEBUG
  135. if (core_flag)
  136. {
  137. coreusage();
  138. }
  139. #endif
  140. }
  141. void report(char* s, int n)
  142. {
  143. /* Report number of optimizations found, if report_flag is set */
  144. if (report_flag)
  145. {
  146. fprintf(stderr, "%s: %d\n", s, n);
  147. }
  148. }