go.c 3.1 KB

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