em_ego.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /* $Id$ */
  2. /* Driver program for the global optimizer. It might even become the global
  3. optimizer itself one day ...
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <em_path.h>
  9. #include <signal.h>
  10. #include <system.h>
  11. #define IC 1
  12. #define CF 2
  13. #define IL 3
  14. #define CS 4
  15. #define SR 5
  16. #define UD 6
  17. #define LV 7
  18. #define RA 8
  19. #define SP 9
  20. #define BO 10
  21. #define CJ 11
  22. #define CA 12
  23. static char *phnames[] = {
  24. 0,
  25. "ic",
  26. "cf",
  27. "il",
  28. "cs",
  29. "sr",
  30. "ud",
  31. "lv",
  32. "ra",
  33. "sp",
  34. "bo",
  35. "cj",
  36. "ca",
  37. 0
  38. };
  39. #define MAXUPHASES 64 /* max # of phases to be run */
  40. #define MAXARGS 1024 /* mar # of args */
  41. #define NTEMPS 4 /* # of temporary files; not tunable */
  42. static char ddump[128] = TMP_DIR; /* data label dump file */
  43. static char pdump[128] = TMP_DIR; /* procedure name dump file */
  44. static char tmpbufs[NTEMPS*2][128] = {
  45. TMP_DIR
  46. };
  47. static int O2phases[] = { /* Passes for -O2 */
  48. CJ, BO, SP, 0
  49. };
  50. static int O3phases[] = { /* Passes for -O3 */
  51. CS, SR, CJ, BO, SP, UD, LV, RA, 0
  52. };
  53. static int O4phases[] = { /* Passes for -O4 */
  54. IL, CF, CS, SR, CJ, BO, SP, UD, LV, RA, 0
  55. };
  56. static int *Ophase = &O2phases[0]; /* default : -O2 */
  57. static int nuphases; /* # of phases specified by user */
  58. static int uphases[MAXUPHASES+1]; /* phases to be run */
  59. static int nfiles = NTEMPS*2+1; /* leave space for tempfilenames */
  60. static char *phargs[MAXARGS+1];
  61. static int keeptemps = 0;
  62. static char **phase_args;
  63. static int nphase_args;
  64. static char *opt_dir;
  65. static char *prog_name;
  66. static int v_flag;
  67. static void cleanup()
  68. {
  69. /* Cleanup temporaries */
  70. if (! keeptemps) {
  71. register int i;
  72. for (i = NTEMPS*2; i > 0; i--) {
  73. register char *f = phargs[i];
  74. if (f != 0 && *f != '\0' && *f != '-') (void) unlink(f);
  75. }
  76. if (ddump[0] != '\0') (void) unlink(ddump);
  77. if (pdump[0] != '\0') (void) unlink(pdump);
  78. }
  79. }
  80. /*VARARGS1*/
  81. static void fatal(char *s, char *s2)
  82. {
  83. /* A fatal error occurred; exit gracefully */
  84. fprint(STDERR, "%s: ", prog_name);
  85. fprint(STDERR, s, s2);
  86. fprint(STDERR, "\n");
  87. cleanup();
  88. sys_stop(S_EXIT);
  89. /*NOTREACHED*/
  90. }
  91. static void add_file(char *s)
  92. {
  93. /* Add an input file to the list */
  94. if (nfiles >= MAXARGS) fatal("too many files", NULL);
  95. phargs[nfiles++] = s;
  96. }
  97. static void add_uphase(int p)
  98. {
  99. /* Add an optimizer phase to the list of phases to run */
  100. if (nuphases >= MAXUPHASES) fatal("too many phases", NULL);
  101. uphases[nuphases++] = p;
  102. }
  103. static void catch()
  104. {
  105. /* Catch interrupts and exit gracefully */
  106. cleanup();
  107. sys_stop(S_EXIT);
  108. }
  109. static void old_infiles()
  110. {
  111. /* Remove old input files unless we have to keep them around. */
  112. register int i;
  113. if (phargs[1] == pdump || keeptemps) return;
  114. for (i = 1; i <= NTEMPS; i++) (void) unlink(phargs[i]);
  115. }
  116. static void get_infiles()
  117. {
  118. /* Make output temps from previous phase input temps of next phase. */
  119. register int i;
  120. register char **dst = &phargs[1];
  121. register char **src = &phargs[NTEMPS+1];
  122. for (i = 1; i <= NTEMPS; i++) {
  123. *dst++ = *src++;
  124. }
  125. }
  126. static void new_outfiles()
  127. {
  128. static int tmpindex = 0;
  129. static int Bindex = 0;
  130. static char dig1 = '1';
  131. static char dig2 = '0';
  132. register int i;
  133. register char **dst = &phargs[NTEMPS+1];
  134. if (! Bindex) {
  135. Bindex = strrchr(tmpbufs[0], 'B') - tmpbufs[0];
  136. }
  137. for (i = 1; i <= NTEMPS; i++) {
  138. *dst = tmpbufs[tmpindex];
  139. (*dst)[Bindex-1] = dig2;
  140. (*dst)[Bindex] = dig1;
  141. tmpindex++;
  142. dst++;
  143. }
  144. if (tmpindex >= 2*NTEMPS) tmpindex = 0;
  145. if (++dig1 > '9') {
  146. ++dig2;
  147. dig1 = '0';
  148. }
  149. }
  150. static void run_phase(int phase)
  151. {
  152. /* Run one phase of the global optimizer; special cases are
  153. IC and CA.
  154. */
  155. static int flags_added;
  156. register int argc;
  157. register int i;
  158. char buf[256];
  159. int pid, status;
  160. phargs[0] = buf;
  161. (void) strcpy(buf, opt_dir);
  162. (void) strcat(buf, "/");
  163. (void) strcat(buf, phnames[phase]);
  164. switch(phase) {
  165. case IC:
  166. phargs[1] = pdump;
  167. phargs[2] = ddump;
  168. for (i = 3; i <= NTEMPS; i++) phargs[i] = "-";
  169. new_outfiles();
  170. argc = nfiles;
  171. phargs[argc] = 0;
  172. break;
  173. case CA:
  174. old_infiles();
  175. get_infiles();
  176. phargs[NTEMPS+1] = pdump;
  177. phargs[NTEMPS+2] = ddump;
  178. for (i = NTEMPS+3; i <= 2*NTEMPS; i++) phargs[i] = "-";
  179. argc = 2*NTEMPS+1;
  180. phargs[argc] = 0;
  181. break;
  182. default:
  183. old_infiles();
  184. get_infiles();
  185. new_outfiles();
  186. if (! flags_added) {
  187. flags_added = 1;
  188. argc = 2*NTEMPS+1;
  189. while (--nphase_args >= 0) {
  190. phargs[argc++] = *phase_args++;
  191. }
  192. phargs[argc] = 0;
  193. }
  194. break;
  195. }
  196. if ((pid = fork()) < 0) {
  197. fatal("Could not fork", NULL);
  198. }
  199. else if (pid == 0) {
  200. if (v_flag) {
  201. register int i = 0;
  202. while (phargs[i]) {
  203. fprint(STDERR, "%s ", phargs[i]);
  204. i++;
  205. }
  206. fprint(STDERR, "\n");
  207. }
  208. (void) execv(phargs[0], phargs);
  209. fatal("Could not exec %s", phargs[0]);
  210. sys_stop(S_EXIT);
  211. }
  212. else {
  213. while (wait(&status) != pid) /* nothing */ ;
  214. if ((status & 0177) != 0) {
  215. fatal("%s got a unix signal", phargs[0]);
  216. }
  217. if (((status >> 8) & 0377) != 0) {
  218. cleanup();
  219. sys_stop(S_EXIT);
  220. }
  221. }
  222. }
  223. int main(int argc, char *argv[])
  224. {
  225. int i = 0;
  226. if (signal(SIGHUP, catch) == SIG_IGN) (void) signal(SIGHUP, SIG_IGN);
  227. if (signal(SIGQUIT, catch) == SIG_IGN) (void) signal(SIGQUIT, SIG_IGN);
  228. if (signal(SIGINT, catch) == SIG_IGN) (void) signal(SIGINT, SIG_IGN);
  229. prog_name = argv[0];
  230. phase_args = &argv[1];
  231. while (--argc > 0) {
  232. argv++;
  233. if (argv[0][0] == '-') {
  234. switch(argv[0][1]) {
  235. case 'P':
  236. if (argv[0][2] == '\0') {
  237. opt_dir = argv[1];
  238. argc--;
  239. argv++;
  240. continue;
  241. }
  242. break;
  243. case 't':
  244. if (argv[0][2] == '\0') {
  245. keeptemps = 1;
  246. /* no continue; IL also needs this */
  247. }
  248. break;
  249. case 'v':
  250. v_flag = 1;
  251. break;
  252. case 'O':
  253. if (argv[0][2] == '2' || argv[0][2] == '\0') continue;
  254. if (argv[0][2] == '3') {
  255. Ophase = &O3phases[0];
  256. continue;
  257. }
  258. Ophase = &O4phases[0];
  259. continue;
  260. case 'I':
  261. if (! strcmp(&argv[0][1], "IL")) {
  262. add_uphase(IL);
  263. add_uphase(CF);
  264. continue;
  265. }
  266. break;
  267. case 'B':
  268. if (! strcmp(&argv[0][1], "BO")) {
  269. add_uphase(BO);
  270. continue;
  271. }
  272. break;
  273. case 'R':
  274. if (! strcmp(&argv[0][1], "RA")) {
  275. add_uphase(RA);
  276. continue;
  277. }
  278. break;
  279. case 'U':
  280. if (! strcmp(&argv[0][1], "UD")) {
  281. add_uphase(UD);
  282. continue;
  283. }
  284. break;
  285. case 'L':
  286. if (! strcmp(&argv[0][1], "LV")) {
  287. add_uphase(LV);
  288. continue;
  289. }
  290. break;
  291. case 'C':
  292. if (! strcmp(&argv[0][1], "CS")) {
  293. add_uphase(CS);
  294. continue;
  295. }
  296. if (! strcmp(&argv[0][1], "CJ")) {
  297. add_uphase(CJ);
  298. continue;
  299. }
  300. break;
  301. case 'S':
  302. if (! strcmp(&argv[0][1], "SR")) {
  303. add_uphase(SR);
  304. continue;
  305. }
  306. if (! strcmp(&argv[0][1], "SP")) {
  307. add_uphase(SP);
  308. continue;
  309. }
  310. break;
  311. }
  312. phase_args[i++] = argv[0];
  313. }
  314. else {
  315. add_file(argv[0]);
  316. }
  317. }
  318. phase_args[i] = 0;
  319. nphase_args = i;
  320. if (nuphases) Ophase = uphases;
  321. if (nfiles == 2*NTEMPS+1) {
  322. /* 2*NTEMPS+1 was the starting value; nothing to do */
  323. sys_stop(S_END);
  324. }
  325. if (! opt_dir) {
  326. fatal("no correct -P flag given", NULL);
  327. }
  328. if (keeptemps) {
  329. (void) strcpy(ddump, ".");
  330. (void) strcpy(pdump, ".");
  331. (void) strcpy(tmpbufs[0], ".");
  332. }
  333. (void) strcat(ddump, "/ego.dd.XXXXXX");
  334. (void) mktemp(ddump);
  335. (void) strcat(pdump, "/ego.pd.XXXXXX");
  336. (void) mktemp(pdump);
  337. (void) strcat(tmpbufs[0], "/ego.A.BB.XXXXXX");
  338. (void) mktemp(tmpbufs[0]);
  339. for (i = 2*NTEMPS-1; i >= 1; i--) {
  340. (void) strcpy(tmpbufs[i], tmpbufs[0]);
  341. }
  342. i = strrchr(tmpbufs[0], 'A') - tmpbufs[0];
  343. tmpbufs[0][i] = 'p'; tmpbufs[NTEMPS+0][i] = 'p';
  344. tmpbufs[1][i] = 'd'; tmpbufs[NTEMPS+1][i] = 'd';
  345. tmpbufs[2][i] = 'l'; tmpbufs[NTEMPS+2][i] = 'l';
  346. tmpbufs[3][i] = 'b'; tmpbufs[NTEMPS+3][i] = 'b';
  347. run_phase(IC);
  348. run_phase(CF);
  349. while (*Ophase) {
  350. run_phase(*Ophase++);
  351. }
  352. run_phase(CA);
  353. cleanup();
  354. sys_stop(S_END);
  355. /*NOTREACHED*/
  356. return 0;
  357. }