Pristine Ack-5.5
[Ack-5.5.git] / util / ego / share / go.c
1 /* $Id: go.c,v 1.9 1994/06/24 10:30:05 ceriel Exp $ */
2 /*
3  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4  * See the copyright notice in the ACK home directory, in the file "Copyright".
5  */
6 /*  S H A R E D    F I L E
7  *
8  *  G O . C
9  *
10  */
11
12
13 #include <stdio.h>
14 #include "types.h"
15 #include "debug.h"
16 #include "global.h"
17 #include "files.h"
18 #include "get.h"
19 #include "put.h"
20 #include "lset.h"
21 #include "map.h"
22 #include "alloc.h"
23 #include "go.h"
24
25
26 STATIC bool report_flag = FALSE;  /* report #optimizations found? */
27 #ifdef DEBUG
28 STATIC bool core_flag = FALSE;    /* report core usage? */
29 #endif
30
31
32 STATIC mach_init(machfile,phase_machinit)
33         char *machfile;
34         int (*phase_machinit)();
35 {
36         /* Read target machine dependent information */
37
38         FILE *f;
39
40         f = openfile(machfile,"r");
41         fscanf(f,"%d",&ws);
42         fscanf(f,"%d",&ps);
43         if (ws != ps && ps != 2*ws) error("illegal pointer size");
44         (*phase_machinit)(f);
45         fclose(f);
46 }
47
48
49
50 go(argc,argv,initialize,optimize,phase_machinit,proc_flag)
51         int argc;
52         char *argv[];
53         int (*initialize)();
54         int (*optimize)();
55         int (*phase_machinit)();
56         int (*proc_flag)();
57 {
58         FILE *f, *gf, *f2, *gf2;  /* The EM input and output and
59                                  * the basic block graphs input and output
60                                  */
61         bblock_p g;
62         line_p l;
63         short kind;
64         int i;
65         char *p;
66         bool time_opt = TRUE;
67
68         linecount = 0;
69         for (i = ARGSTART; i < argc; i++) {
70                 p = argv[i];
71                 if (*p++ != '-') error("illegal argument");
72                 switch(*p) {
73                         case 'S':
74                                 time_opt = FALSE;
75                                 break;
76                         case 'T':
77                                 time_opt = TRUE;
78                                 break;
79                         case 'M':
80                                 p++;
81                                 mach_init(p,phase_machinit);
82                                 break;
83                         case 'C':
84 #ifdef DEBUG
85                                 core_flag = TRUE;
86 #endif
87                                 break;
88                         case 'Q':
89                                 report_flag = TRUE;
90                                 break;
91                         case 'V':
92                                 verbose_flag = TRUE;
93                                 break;
94                         default:
95                                 (*proc_flag)(p);
96                                 break;
97                 }
98         }
99         time_space_ratio = (time_opt ? 100 : 0);
100         fproc = getptable(pname); /* proc table */
101         fdblock = getdtable(dname);  /* data block table */
102         (*initialize)();
103         if (optimize == no_action) return;
104         f   = openfile(lname,"r");
105         gf  = openfile(bname,"r");
106         f2  = openfile(lname2,"w");
107         gf2 = openfile(bname2,"w");
108         mesregs = Lempty_set();
109         while (getunit(gf,f,&kind,&g,&l,&curproc,TRUE)) {
110                 /* Read the control flow graph and EM text of
111                  * one procedure and optimize it.
112                  */
113                 if (kind == LDATA) {
114                         putunit(LDATA, (proc_p) 0, l, gf2, f2);
115                         continue;
116                 }
117                 OUTTRACE("flow graph of proc %d read",curproc->p_id);
118                 curproc->p_start = g;
119                 /* The global variable curproc points to the
120                  * current procedure. It is set by getgraph
121                  */
122                 (*optimize)(curproc);
123                 putunit(LTEXT,curproc,(line_p) 0,gf2,f2);
124                 /* output control flow graph + text */
125                 OUTTRACE("graph of proc %d outputted",curproc->p_id);
126                 Ldeleteset(mesregs);
127                 mesregs = Lempty_set();
128         }
129         fclose(f);
130         fclose(f2);
131         fclose(gf);
132         fclose(gf2);
133         f = openfile(dname2,"w");
134         putdtable(fdblock,f);
135         /* fclose(f); done by putdtable */
136         f = openfile(pname2,"w");
137         putptable(fproc,f,TRUE);
138         /* fclose(f); done by putptable */
139         core_usage();
140 }
141
142
143 no_action() { }
144
145 core_usage()
146 {
147 #ifdef DEBUG
148         if (core_flag) {
149                 coreusage();
150         }
151 #endif
152 }
153
154 report(s,n)
155         char *s;
156         int n;
157 {
158         /* Report number of optimizations found, if report_flag is set */
159
160         if (report_flag) {
161                 fprintf(stderr,"%s:  %d\n",s,n);
162         }
163 }