Pristine Ack-5.5
[Ack-5.5.git] / util / ego / il / il.c
1 /* $Id: il.c,v 1.11 1994/06/24 10:25:12 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 /*  I N L I N E   S U B S T I T U T I O N */
7 #include <stdio.h>
8 #include <em_path.h>
9 #include <em_mnem.h>
10 #include <em_pseu.h>
11 #include "../share/types.h"
12 #include "il.h"
13 #include "../share/debug.h"
14 #include "../share/alloc.h"
15 #include "../share/global.h"
16 #include "../share/lset.h"
17 #include "../share/files.h"
18 #include "../share/map.h"
19 #include "il_aux.h"
20 #include "il1_anal.h"
21 #include "il2_aux.h"
22 #include "il3_subst.h"
23 #include "../share/get.h"
24 #include "../share/put.h"
25 #include "../share/go.h"
26
27 int calnr;
28 int complete_program;
29 calcnt_p cchead;        /* call-count info of current proc */
30 STATIC long space = 0;
31 STATIC long total_size = 0;
32
33 STATIC char cname[128] = TMP_DIR;
34 STATIC char ccname[128] = TMP_DIR;
35
36 /* For debugging only */
37 STATIC char sname[128] = TMP_DIR;
38 STATIC int kp_temps = 0;
39
40 int Ssubst;
41 #ifdef VERBOSE
42 int Senv,Srecursive,Slocals,Sinstrlab,Sparsefails,Spremoved,Scals;
43 int Sbig_caller,Sdispensable,Schangedcallee,Sbigcallee,Sspace,Szeroratio;
44 #endif
45
46 /* P A S S  1
47  *
48  * Pass 1 reads and analyses the EM text and the CFG.
49  * It determines for every procedure if it may be expanded
50  * in line and how it uses its formal parameters.
51  * It also collects all calls appearing in the program and
52  * recognizes the actual parameters of every call.
53  * The call descriptors are put in a file (calfile).
54  */
55
56 pass1(lnam,bnam,cnam)
57         char *lnam, *bnam, *cnam;
58 {
59         FILE *f, *gf, *cf, *ccf; /* The EM input, the basic block graph,
60                                   * the call-list file and the calcnt file.
61                                   */
62         long laddr;
63         bblock_p g;
64         short kind;
65         line_p l;
66
67         f = openfile(lnam,"r");
68         gf = openfile(bnam,"r");
69         cf = openfile(cnam,"w");
70         ccf = openfile(ccname,"w");
71         mesregs = Lempty_set();
72         apriori(fproc);
73         /* use information from the procedure table to
74          * see which calls certainly cannot be expanded.
75          */
76         while(TRUE) {
77                 laddr = ftell(f);
78                 if (!getunit(gf,f,&kind,&g,&l,&curproc,TRUE)) break;
79                 /* Read the control flow graph and EM text of
80                  * one procedure and analyze it.
81                  */
82                 if (kind == LDATA) {
83                         remunit(LDATA,(proc_p) 0,l);
84                         continue;
85                 }
86                 OUTTRACE("flow graph of proc %d read",curproc->p_id);
87                 assert(INSTR(g->b_start) == ps_pro);
88                 curproc->p_start = g;
89                 curproc->P_LADDR = laddr;
90                 /* address of em text in em-file */
91                 /* address of graph in basic block file */
92                 curproc->P_SIZE = proclength(curproc); /* #instructions */
93                 total_size += curproc->P_SIZE;
94                 if (BIG_PROC(curproc)) {
95                         /* curproc is too large to be expanded in line */
96                         UNSUITABLE(curproc);
97                 }
98                 calnr = 0;
99                 anal_proc(curproc,cf,ccf);
100                 OUTTRACE("proc %d processed",curproc->p_id);
101                 remunit(LTEXT,curproc,(line_p) 0);
102                 /* remove control flow graph + text */
103                 OUTTRACE("graph of proc %d removed",curproc->p_id);
104                 Ldeleteset(mesregs);
105                 mesregs = Lempty_set();
106         }
107         fclose(f);
108         fclose(gf);
109         fclose(cf);
110         fclose(ccf); 
111 }
112
113
114
115 /* P A S S  2
116  *
117  * Pass 2 reads the calfile and determines which calls should
118  * be expanded in line. It does not use the EM text.
119  */
120
121
122
123 STATIC char cname2[128] = TMP_DIR;
124
125 pass2(cnam,space)
126         char *cnam;
127         long space;
128 {
129         FILE  *cf, *cf2, *ccf;
130         call_p c,a;
131
132         cf = openfile(cnam,"r");
133         cf2 = openfile(cname2,"w");
134         ccf = openfile(ccname,"r");
135         while ((c = getcall(cf)) != (call_p) 0) {
136                 /* process all calls */
137                 if (SUITABLE(c->cl_proc) && anal_params(c)) {
138                         /* called proc. may be put in line */
139                         /* see which parameters may be put in line */
140                         assign_ratio(c); /* assign a rank */
141                         a = abstract(c); /* abstract essential info */
142                         append_abstract(a,a->cl_caller);
143                         /* put it in call-list of calling proc. */
144                         putcall(c,cf2,(short) 0);
145                 } else {
146                         rem_call(c);
147                 }
148         }
149         select_calls(fproc,ccf,space);
150         fclose(cf); if (! kp_temps) unlink(cnam);
151         fclose(cf2);
152         fclose(ccf); if (! kp_temps) unlink(ccname);
153         cf2 = openfile(cname2,"r");
154         add_actuals(fproc,cf2);
155         cleancals(fproc); /* remove calls that were not selected */
156         /* add actual parameters to each selected call */
157         fclose(cf2); if (! kp_temps) unlink(cname2);
158 }
159
160
161
162 /* P A S S  3
163  *
164  * pass 3 reads the substitution file and performs all
165  * substitutions described in that file. It reads the
166  * original EM text and produced a new (optimized)
167  * EM textfile.
168  */
169
170
171 pass3(lnam,lnam2)
172         char *lnam,*lnam2;
173 {
174         bool verbose = TRUE;
175         FILE *lfile, *lfilerand, *lfile2, *sfile;
176         call_p c,next;
177         line_p l,startscan,cal;
178         short lastcid; /* last call-id seen */
179
180         lfile = openfile(lnam, "r");
181         lfilerand = openfile(lnam, "r");
182         lfile2 = openfile(lnam2,"w");
183         if (verbose) {
184                 sfile = openfile(sname,"w");
185         }
186         mesregs = Lempty_set();
187         while ((l = get_text(lfile,&curproc)) != (line_p) 0) {
188                 if (curproc == (proc_p) 0) {
189                         /* Just a data-unit; no real instructions */
190                         putlines(l->l_next,lfile2);
191                         oldline(l);
192                         continue;
193                 }
194                 if (IS_DISPENSABLE(curproc)) {
195                         liquidate(curproc,l->l_next);
196                 } else {
197                         startscan = l->l_next;
198                         lastcid = 0;
199                         for (c = curproc->P_CALS; c != (call_p) 0; c = next) {
200                                 next = c->cl_cdr;
201                                 cal = scan_to_cal(startscan,c->cl_id - lastcid);
202                                 assert (cal != (line_p) 0);
203                                 startscan = scan_to_cal(cal->l_next,1);
204                                 /* next CAL */
205                                 lastcid = c->cl_id;
206                                 /* next CAL after current one */
207                                 substitute(lfilerand,c,cal,l->l_next);
208                                 if (verbose) {
209                                         putcall(c,sfile,0);
210                                 } else {
211                                         rem_call(c);
212                                 }
213                         }
214                 }
215                 putlines(l->l_next,lfile2);
216                 Ldeleteset(mesregs);
217                 mesregs = Lempty_set();
218                 oldline(l);
219         }
220         fclose(lfile);
221         fclose(lfile2);
222         if (verbose) {
223                 fclose(sfile);
224                 if (! kp_temps) unlink(sname);
225         }
226 }
227
228
229 STATIC il_extptab(ptab)
230         proc_p ptab;
231 {
232         /* Allocate space for extension of proctable entries.
233          * Also, initialise some of the fields just allocated.
234          */
235
236         register proc_p p;
237
238         for (p = ptab; p != (proc_p) 0; p = p->p_next) {
239                 p->p_extend = newilpx();
240                 p->P_ORGLABELS = p->p_nrlabels;
241                 p->P_ORGLOCALS = p->p_localbytes;
242         }
243 }
244
245 STATIC il_cleanptab(ptab)
246         proc_p ptab;
247 {
248         /* De-allocate space for extensions */
249
250         register proc_p p;
251
252         for (p = ptab; p != (proc_p) 0; p = p->p_next) {
253                 oldilpx(p->p_extend);
254         }
255 }
256
257 #ifdef VERBOSE
258 Sdiagnostics()
259 {
260         /* print statictical information */
261
262         fprintf(stderr,"STATISTICS:\n");
263         fprintf(stderr,"Info about procedures:\n");
264         fprintf(stderr,"environment accessed: %d\n",Senv);
265         fprintf(stderr,"recursive: %d\n",Srecursive);
266         fprintf(stderr,"too many locals: %d\n",Slocals);
267         fprintf(stderr,"instr. lab in data block: %d\n",Sinstrlab);
268         fprintf(stderr,"procedures removed: %d\n",Spremoved);
269         fprintf(stderr,"\nInfo about calls:\n");
270         fprintf(stderr,"total number of calls: %d\n",Scals);
271         fprintf(stderr,"total number of calls substituted: %d\n",Ssubst);
272         fprintf(stderr,"parser failed: %d\n",Sparsefails);
273         fprintf(stderr,"caller too big: %d\n",Sbig_caller);
274         fprintf(stderr,"caller dispensable: %d\n",Sdispensable);
275         fprintf(stderr,"callee is changed: %d\n",Schangedcallee);
276         fprintf(stderr,"callee too big: %d\n",Sbigcallee);
277         fprintf(stderr,"no space available: %d\n",Sspace);
278         fprintf(stderr,"zero ratio: %d\n",Szeroratio);
279 }
280 #endif
281
282 il_flags(p)
283         char *p;
284 {
285         switch(*p++) {
286         case 's':
287                 while (*p != '\0') {
288                         space = 10*space +*p++ -'0';
289                 }
290                 break;
291         case 'a':
292                 complete_program = 1;
293                 break;
294         case 't':
295                 strcpy(cname, ".");
296                 strcpy(ccname, ".");
297                 strcpy(sname, ".");
298                 strcpy(cname2, ".");
299                 kp_temps = 1;
300                 break;
301         }
302 }
303
304 main(argc,argv)
305         int argc;
306         char *argv[];
307 {
308         FILE *f;
309         
310         go(argc,argv,no_action,no_action,no_action,il_flags);
311         il_extptab(fproc); /* add extended data structures */
312         strcat(cname, "/ego.i1.XXXXXX");
313         strcat(ccname, "/ego.i2.XXXXXX");
314         strcat(sname, "/ego.i3.XXXXXX");
315         strcat(cname2, "/ego.i4.XXXXXX");
316         mktemp(cname);
317         mktemp(ccname);
318         mktemp(sname);
319         mktemp(cname2);
320         pass1(lname,bname,cname); /* grep calls, analyse procedures */
321         space = total_size * space / 100 ;
322         pass2(cname,space);  /* select calls to be expanded */
323         pass3(lname,lname2); /* do substitutions */
324         f = openfile(dname2,"w");
325         il_cleanptab(fproc); /* remove extended data structures */
326         putdtable(fdblock,f);
327         f = openfile(pname2,"w");
328         putptable(fproc,f,FALSE);
329         report("inline substitutions",Ssubst);
330 #ifdef VERBOSE
331         if (verbose_flag) {
332                 Sdiagnostics();
333         }
334 #endif
335 #ifdef DEBUG
336         core_usage();
337 #endif
338         exit(0);
339 }