1 /* $Id: em_ego.c,v 1.6 1994/06/24 10:23:30 ceriel Exp $ */
3 /* Driver program for the global optimizer. It might even become the global
4 optimizer itself one day ...
24 static char *phnames[] = {
41 #define MAXUPHASES 64 /* max # of phases to be run */
42 #define MAXARGS 1024 /* mar # of args */
43 #define NTEMPS 4 /* # of temporary files; not tunable */
45 extern char *mktemp();
46 extern char *strcpy(), *strcat();
47 extern char *strrindex();
49 static char ddump[128] = TMP_DIR; /* data label dump file */
50 static char pdump[128] = TMP_DIR; /* procedure name dump file */
51 static char tmpbufs[NTEMPS*2][128] = {
55 static int O2phases[] = { /* Passes for -O2 */
59 static int O3phases[] = { /* Passes for -O3 */
60 CS, SR, CJ, BO, SP, UD, LV, RA, 0
63 static int O4phases[] = { /* Passes for -O4 */
64 IL, CF, CS, SR, CJ, BO, SP, UD, LV, RA, 0
67 static int *Ophase = &O2phases[0]; /* default : -O2 */
69 static int nuphases; /* # of phases specified by user */
70 static int uphases[MAXUPHASES+1]; /* phases to be run */
72 static int nfiles = NTEMPS*2+1; /* leave space for tempfilenames */
73 static char *phargs[MAXARGS+1];
75 static int keeptemps = 0;
77 static char **phase_args;
78 static int nphase_args;
81 static char *prog_name;
88 /* Cleanup temporaries */
93 for (i = NTEMPS*2; i > 0; i--) {
94 register char *f = phargs[i];
95 if (f != 0 && *f != '\0' && *f != '-') (void) unlink(f);
97 if (ddump[0] != '\0') (void) unlink(ddump);
98 if (pdump[0] != '\0') (void) unlink(pdump);
108 /* A fatal error occurred; exit gracefully */
110 fprint(STDERR, "%s: ", prog_name);
111 fprint(STDERR, s, s2);
112 fprint(STDERR, "\n");
122 /* Add an input file to the list */
124 if (nfiles >= MAXARGS) fatal("too many files");
125 phargs[nfiles++] = s;
132 /* Add an optimizer phase to the list of phases to run */
134 if (nuphases >= MAXUPHASES) fatal("too many phases");
135 uphases[nuphases++] = p;
141 /* Catch interrupts and exit gracefully */
150 /* Remove old input files unless we have to keep them around. */
154 if (phargs[1] == pdump || keeptemps) return;
156 for (i = 1; i <= NTEMPS; i++) (void) unlink(phargs[i]);
162 /* Make output temps from previous phase input temps of next phase. */
165 register char **dst = &phargs[1];
166 register char **src = &phargs[NTEMPS+1];
168 for (i = 1; i <= NTEMPS; i++) {
176 static int tmpindex = 0;
177 static int Bindex = 0;
178 static char dig1 = '1';
179 static char dig2 = '0';
181 register char **dst = &phargs[NTEMPS+1];
184 Bindex = strrindex(tmpbufs[0], 'B') - tmpbufs[0];
186 for (i = 1; i <= NTEMPS; i++) {
187 *dst = tmpbufs[tmpindex];
188 (*dst)[Bindex-1] = dig2;
189 (*dst)[Bindex] = dig1;
193 if (tmpindex >= 2*NTEMPS) tmpindex = 0;
204 /* Run one phase of the global optimizer; special cases are
207 static int flags_added;
214 (void) strcpy(buf, opt_dir);
215 (void) strcat(buf, "/");
216 (void) strcat(buf, phnames[phase]);
222 for (i = 3; i <= NTEMPS; i++) phargs[i] = "-";
231 phargs[NTEMPS+1] = pdump;
232 phargs[NTEMPS+2] = ddump;
233 for (i = NTEMPS+3; i <= 2*NTEMPS; i++) phargs[i] = "-";
245 while (--nphase_args >= 0) {
246 phargs[argc++] = *phase_args++;
252 if ((pid = fork()) < 0) {
253 fatal("Could not fork");
260 fprint(STDERR, "%s ", phargs[i]);
263 fprint(STDERR, "\n");
265 (void) execv(phargs[0], phargs);
266 fatal("Could not exec %s", phargs[0]);
270 while (wait(&status) != pid) /* nothing */ ;
271 if ((status & 0177) != 0) {
272 fatal("%s got a unix signal", phargs[0]);
274 if (((status >> 8) & 0377) != 0) {
287 if (signal(SIGHUP, catch) == SIG_IGN) (void) signal(SIGHUP, SIG_IGN);
288 if (signal(SIGQUIT, catch) == SIG_IGN) (void) signal(SIGQUIT, SIG_IGN);
289 if (signal(SIGINT, catch) == SIG_IGN) (void) signal(SIGINT, SIG_IGN);
291 phase_args = &argv[1];
294 if (argv[0][0] == '-') {
297 if (argv[0][2] == '\0') {
305 if (argv[0][2] == '\0') {
307 /* no continue; IL also needs this */
314 if (argv[0][2] == '2' || argv[0][2] == '\0') continue;
315 if (argv[0][2] == '3') {
316 Ophase = &O3phases[0];
319 Ophase = &O4phases[0];
322 if (! strcmp(&argv[0][1], "IL")) {
329 if (! strcmp(&argv[0][1], "BO")) {
335 if (! strcmp(&argv[0][1], "RA")) {
341 if (! strcmp(&argv[0][1], "UD")) {
347 if (! strcmp(&argv[0][1], "LV")) {
353 if (! strcmp(&argv[0][1], "CS")) {
357 if (! strcmp(&argv[0][1], "CJ")) {
363 if (! strcmp(&argv[0][1], "SR")) {
367 if (! strcmp(&argv[0][1], "SP")) {
373 phase_args[i++] = argv[0];
381 if (nuphases) Ophase = uphases;
383 if (nfiles == 2*NTEMPS+1) {
384 /* 2*NTEMPS+1 was the starting value; nothing to do */
389 fatal("no correct -P flag given");
393 (void) strcpy(ddump, ".");
394 (void) strcpy(pdump, ".");
395 (void) strcpy(tmpbufs[0], ".");
397 (void) strcat(ddump, "/ego.dd.XXXXXX");
398 (void) mktemp(ddump);
399 (void) strcat(pdump, "/ego.pd.XXXXXX");
400 (void) mktemp(pdump);
402 (void) strcat(tmpbufs[0], "/ego.A.BB.XXXXXX");
403 (void) mktemp(tmpbufs[0]);
404 for (i = 2*NTEMPS-1; i >= 1; i--) {
405 (void) strcpy(tmpbufs[i], tmpbufs[0]);
407 i = strrindex(tmpbufs[0], 'A') - tmpbufs[0];
408 tmpbufs[0][i] = 'p'; tmpbufs[NTEMPS+0][i] = 'p';
409 tmpbufs[1][i] = 'd'; tmpbufs[NTEMPS+1][i] = 'd';
410 tmpbufs[2][i] = 'l'; tmpbufs[NTEMPS+2][i] = 'l';
411 tmpbufs[3][i] = 'b'; tmpbufs[NTEMPS+3][i] = 'b';
415 run_phase(*Ophase++);