em_ego.c 7.7 KB

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