em_ego.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. {
  73. register int i;
  74. for (i = NTEMPS * 2; i > 0; i--)
  75. {
  76. register char* f = phargs[i];
  77. if (f != 0 && *f != '\0' && *f != '-')
  78. (void)unlink(f);
  79. }
  80. if (ddump[0] != '\0')
  81. (void)unlink(ddump);
  82. if (pdump[0] != '\0')
  83. (void)unlink(pdump);
  84. }
  85. }
  86. /*VARARGS1*/
  87. static void
  88. fatal(s, s2) char* s;
  89. char* s2;
  90. {
  91. /* A fatal error occurred; exit gracefully */
  92. fprint(STDERR, "%s: ", prog_name);
  93. fprint(STDERR, s, s2);
  94. fprint(STDERR, "\n");
  95. cleanup();
  96. sys_stop(S_EXIT);
  97. /*NOTREACHED*/
  98. }
  99. static void
  100. add_file(s) char* s;
  101. {
  102. /* Add an input file to the list */
  103. if (nfiles >= MAXARGS)
  104. fatal("too many files");
  105. phargs[nfiles++] = s;
  106. }
  107. static void
  108. add_uphase(p) int p;
  109. {
  110. /* Add an optimizer phase to the list of phases to run */
  111. if (nuphases >= MAXUPHASES)
  112. fatal("too many phases");
  113. uphases[nuphases++] = p;
  114. }
  115. static void catch ()
  116. {
  117. /* Catch interrupts and exit gracefully */
  118. cleanup();
  119. sys_stop(S_EXIT);
  120. }
  121. static void
  122. old_infiles()
  123. {
  124. /* Remove old input files unless we have to keep them around. */
  125. register int i;
  126. if (phargs[1] == pdump || keeptemps)
  127. return;
  128. for (i = 1; i <= NTEMPS; i++)
  129. (void)unlink(phargs[i]);
  130. }
  131. static void
  132. get_infiles()
  133. {
  134. /* Make output temps from previous phase input temps of next phase. */
  135. register int i;
  136. register char** dst = &phargs[1];
  137. register char** src = &phargs[NTEMPS + 1];
  138. for (i = 1; i <= NTEMPS; i++)
  139. {
  140. *dst++ = *src++;
  141. }
  142. }
  143. static void
  144. new_outfiles()
  145. {
  146. static int tmpindex = 0;
  147. static int Bindex = 0;
  148. static char dig1 = '1';
  149. static char dig2 = '0';
  150. register int i;
  151. register char** dst = &phargs[NTEMPS + 1];
  152. if (!Bindex)
  153. {
  154. Bindex = strrchr(tmpbufs[0], 'B') - tmpbufs[0];
  155. }
  156. for (i = 1; i <= NTEMPS; i++)
  157. {
  158. *dst = tmpbufs[tmpindex];
  159. (*dst)[Bindex - 1] = dig2;
  160. (*dst)[Bindex] = dig1;
  161. tmpindex++;
  162. dst++;
  163. }
  164. if (tmpindex >= 2 * NTEMPS)
  165. tmpindex = 0;
  166. if (++dig1 > '9')
  167. {
  168. ++dig2;
  169. dig1 = '0';
  170. }
  171. }
  172. static void
  173. run_phase(phase) int phase;
  174. {
  175. /* Run one phase of the global optimizer; special cases are
  176. IC and CA.
  177. */
  178. static int flags_added;
  179. register int argc;
  180. register int i;
  181. char buf[256];
  182. int pid, status;
  183. phargs[0] = buf;
  184. (void)strcpy(buf, opt_dir);
  185. (void)strcat(buf, "/");
  186. (void)strcat(buf, phnames[phase]);
  187. switch (phase)
  188. {
  189. case IC:
  190. phargs[1] = pdump;
  191. phargs[2] = ddump;
  192. for (i = 3; i <= NTEMPS; i++)
  193. phargs[i] = "-";
  194. new_outfiles();
  195. argc = nfiles;
  196. phargs[argc] = 0;
  197. break;
  198. case CA:
  199. old_infiles();
  200. get_infiles();
  201. phargs[NTEMPS + 1] = pdump;
  202. phargs[NTEMPS + 2] = ddump;
  203. for (i = NTEMPS + 3; i <= 2 * NTEMPS; i++)
  204. phargs[i] = "-";
  205. argc = 2 * NTEMPS + 1;
  206. phargs[argc] = 0;
  207. break;
  208. default:
  209. old_infiles();
  210. get_infiles();
  211. new_outfiles();
  212. if (!flags_added)
  213. {
  214. flags_added = 1;
  215. argc = 2 * NTEMPS + 1;
  216. while (--nphase_args >= 0)
  217. {
  218. phargs[argc++] = *phase_args++;
  219. }
  220. phargs[argc] = 0;
  221. }
  222. break;
  223. }
  224. if ((pid = fork()) < 0)
  225. {
  226. fatal("Could not fork");
  227. }
  228. else if (pid == 0)
  229. {
  230. if (v_flag)
  231. {
  232. register int i = 0;
  233. while (phargs[i])
  234. {
  235. fprint(STDERR, "%s ", phargs[i]);
  236. i++;
  237. }
  238. fprint(STDERR, "\n");
  239. }
  240. (void)execv(phargs[0], phargs);
  241. fatal("Could not exec %s", phargs[0]);
  242. sys_stop(S_EXIT);
  243. }
  244. else
  245. {
  246. while (wait(&status) != pid) /* nothing */
  247. ;
  248. if ((status & 0177) != 0)
  249. {
  250. fatal("%s got a unix signal", phargs[0]);
  251. }
  252. if (((status >> 8) & 0377) != 0)
  253. {
  254. cleanup();
  255. sys_stop(S_EXIT);
  256. }
  257. }
  258. }
  259. main(argc, argv) int argc;
  260. char* argv[];
  261. {
  262. register int i = 0;
  263. if (signal(SIGHUP, catch) == SIG_IGN)
  264. (void)signal(SIGHUP, SIG_IGN);
  265. if (signal(SIGQUIT, catch) == SIG_IGN)
  266. (void)signal(SIGQUIT, SIG_IGN);
  267. if (signal(SIGINT, catch) == SIG_IGN)
  268. (void)signal(SIGINT, SIG_IGN);
  269. prog_name = argv[0];
  270. phase_args = &argv[1];
  271. while (--argc > 0)
  272. {
  273. argv++;
  274. if (argv[0][0] == '-')
  275. {
  276. switch (argv[0][1])
  277. {
  278. case 'P':
  279. if (argv[0][2] == '\0')
  280. {
  281. opt_dir = argv[1];
  282. argc--;
  283. argv++;
  284. continue;
  285. }
  286. break;
  287. case 't':
  288. if (argv[0][2] == '\0')
  289. {
  290. keeptemps = 1;
  291. /* no continue; IL also needs this */
  292. }
  293. break;
  294. case 'v':
  295. v_flag = 1;
  296. break;
  297. case 'O':
  298. if (argv[0][2] == '2' || argv[0][2] == '\0')
  299. continue;
  300. if (argv[0][2] == '3')
  301. {
  302. Ophase = &O3phases[0];
  303. continue;
  304. }
  305. Ophase = &O4phases[0];
  306. continue;
  307. case 'I':
  308. if (!strcmp(&argv[0][1], "IL"))
  309. {
  310. add_uphase(IL);
  311. add_uphase(CF);
  312. continue;
  313. }
  314. break;
  315. case 'B':
  316. if (!strcmp(&argv[0][1], "BO"))
  317. {
  318. add_uphase(BO);
  319. continue;
  320. }
  321. break;
  322. case 'R':
  323. if (!strcmp(&argv[0][1], "RA"))
  324. {
  325. add_uphase(RA);
  326. continue;
  327. }
  328. break;
  329. case 'U':
  330. if (!strcmp(&argv[0][1], "UD"))
  331. {
  332. add_uphase(UD);
  333. continue;
  334. }
  335. break;
  336. case 'L':
  337. if (!strcmp(&argv[0][1], "LV"))
  338. {
  339. add_uphase(LV);
  340. continue;
  341. }
  342. break;
  343. case 'C':
  344. if (!strcmp(&argv[0][1], "CS"))
  345. {
  346. add_uphase(CS);
  347. continue;
  348. }
  349. if (!strcmp(&argv[0][1], "CJ"))
  350. {
  351. add_uphase(CJ);
  352. continue;
  353. }
  354. break;
  355. case 'S':
  356. if (!strcmp(&argv[0][1], "SR"))
  357. {
  358. add_uphase(SR);
  359. continue;
  360. }
  361. if (!strcmp(&argv[0][1], "SP"))
  362. {
  363. add_uphase(SP);
  364. continue;
  365. }
  366. break;
  367. }
  368. phase_args[i++] = argv[0];
  369. }
  370. else
  371. {
  372. add_file(argv[0]);
  373. }
  374. }
  375. phase_args[i] = 0;
  376. nphase_args = i;
  377. if (nuphases)
  378. Ophase = uphases;
  379. if (nfiles == 2 * NTEMPS + 1)
  380. {
  381. /* 2*NTEMPS+1 was the starting value; nothing to do */
  382. sys_stop(S_END);
  383. }
  384. if (!opt_dir)
  385. {
  386. fatal("no correct -P flag given");
  387. }
  388. if (keeptemps)
  389. {
  390. (void)strcpy(ddump, ".");
  391. (void)strcpy(pdump, ".");
  392. (void)strcpy(tmpbufs[0], ".");
  393. }
  394. (void)strcat(ddump, "/ego.dd.XXXXXX");
  395. (void)mktemp(ddump);
  396. (void)strcat(pdump, "/ego.pd.XXXXXX");
  397. (void)mktemp(pdump);
  398. (void)strcat(tmpbufs[0], "/ego.A.BB.XXXXXX");
  399. (void)mktemp(tmpbufs[0]);
  400. for (i = 2 * NTEMPS - 1; i >= 1; i--)
  401. {
  402. (void)strcpy(tmpbufs[i], tmpbufs[0]);
  403. }
  404. i = strrchr(tmpbufs[0], 'A') - tmpbufs[0];
  405. tmpbufs[0][i] = 'p';
  406. tmpbufs[NTEMPS + 0][i] = 'p';
  407. tmpbufs[1][i] = 'd';
  408. tmpbufs[NTEMPS + 1][i] = 'd';
  409. tmpbufs[2][i] = 'l';
  410. tmpbufs[NTEMPS + 2][i] = 'l';
  411. tmpbufs[3][i] = 'b';
  412. tmpbufs[NTEMPS + 3][i] = 'b';
  413. run_phase(IC);
  414. run_phase(CF);
  415. while (*Ophase)
  416. {
  417. run_phase(*Ophase++);
  418. }
  419. run_phase(CA);
  420. cleanup();
  421. sys_stop(S_END);
  422. /*NOTREACHED*/
  423. }