em_ego.c 7.7 KB

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