xref: /original-bsd/usr.bin/pascal/pxp/stat.c (revision 264c46cb)
1 static	char *sccsid = "@(#)stat.c	1.3 (Berkeley) 08/30/82";
2 /* Copyright (c) 1979 Regents of the University of California */
3 #
4 /*
5  * pxp - Pascal execution profiler
6  *
7  * Bill Joy UCB
8  * Version 1.2 January 1979
9  */
10 
11 #include "0.h"
12 #include "tree.h"
13 
14 int cntstat;
15 int cnts = 3;
16 
17 statlist(r)
18 	int *r;
19 {
20 	register int *sl;
21 
22 	sl = r;
23 	if (sl != NIL)
24 		for (;;) {
25 			statement(sl[1]);
26 			sl = sl[2];
27 			if (sl == NIL)
28 				break;
29 			ppsep(";");
30 		}
31 	else
32 		statement(NIL);
33 }
34 
35 
36 statement(r)
37 	int *r;
38 {
39 	register int *s;
40 
41 	s = r;
42 top:
43 	if (cntstat) {
44 		cntstat = 0;
45 		getcnt();
46 	}
47 	if (s == NIL) {
48 		putcm();
49 		ppitem();
50 		ppid("null");
51 		return;
52 	}
53 	if (s[0] == T_REPEAT)
54 		setinfo(s[1]);
55 	else
56 		setline(s[1]);
57 	if (s[0] == T_LABEL) {
58 		cntstat = 1;
59 		ppnl();
60 		labeled(s[2]);
61 		statement(s[3]);
62 		return;
63 	}
64 	switch (s[0]) {
65 		default:
66 			panic("stat");
67 		case T_PCALL:
68 			ppitem();
69 			proc(s);
70 			break;
71 		case T_IF:
72 		case T_IFEL:
73 			ppnl();
74 			indent();
75 			ifop(s);
76 			break;
77 		case T_WHILE:
78 			ppnl();
79 			indent();
80 			whilop(s);
81 			break;
82 		case T_REPEAT:
83 			ppnl();
84 			indent();
85 			repop(s);
86 			break;
87 		case T_FORU:
88 		case T_FORD:
89 			ppnl();
90 			indent();
91 			forop(s);
92 			break;
93 		case T_BLOCK:
94 			ppnl();
95 			indent();
96 			ppstbl(s, DECL);
97 			break;
98 		case T_ASGN:
99 			ppitem();
100 			asgnop(s);
101 			break;
102 		case T_GOTO:
103 			ppitem();
104 			gotoop(s[2]);
105 			cntstat = 1;
106 			break;
107 		case T_CASE:
108 			ppnl();
109 			indent();
110 			caseop(s);
111 			break;
112 		case T_WITH:
113 			ppnl();
114 			indent();
115 			withop(s);
116 			break;
117 	}
118 	setinfo(s[1]);
119 	putcm();
120 }
121 
122 withop(s)
123 	int *s;
124 {
125 	register *p;
126 
127 	ppkw("with");
128 	ppspac();
129 	p = s[2];
130 	if (p != NIL)
131 		for (;;) {
132 			lvalue(p[1]);
133 			p = p[2];
134 			if (p == NIL)
135 				break;
136 			ppsep(", ");
137 		}
138 	else
139 		ppid("{record variable list}");
140 	ppstdo(s[3], DECL);
141 }
142 
143 asgnop(r)
144 	int *r;
145 {
146 
147 	lvalue(r[2]);
148 	ppsep(" := ");
149 	rvalue(r[3], NIL);
150 }
151 
152 forop(r)
153 	int *r;
154 {
155 	struct pxcnt scnt;
156 
157 	savecnt(&scnt);
158 	ppkw("for");
159 	ppspac();
160 	asgnop(r[2]);
161 	ppspac();
162 	ppkw(r[0] == T_FORU ? "to" : "downto");
163 	ppspac();
164 	rvalue(r[3], NIL);
165 	getcnt();
166 	ppstdo(r[4], STAT);
167 	if (rescnt(&scnt))
168 		getcnt();
169 }
170 
171 ifop(r)
172 	int *r;
173 {
174 	register *s;
175 	struct pxcnt scnt;
176 
177 	ppkw("if");
178 	ppspac();
179 	rvalue(r[2], NIL);
180 	ppspac();
181 	ppkw("then");
182 	ppspac();
183 	s = r[3];
184 	savecnt(&scnt);
185 	getcnt();
186 	if (s != NIL && s[0] == T_BLOCK)
187 		ppstbl1(s, STAT);
188 	else {
189 		ppgoin(STAT);
190 		statement(s);
191 		ppgoout(STAT);
192 	}
193 	if (r[0] == T_IFEL) {
194 		setcnt(cntof(&scnt)-nowcnt());
195 		if (s == NIL || s[0] != T_BLOCK) {
196 			ppnl();
197 			indent();
198 		} else {
199 			ppstbl2();
200 			ppspac();
201 		}
202 		s = r[4];
203 		ppkw("else");
204 		unprint();
205 		ppspac();
206 		if (s == NIL)
207 			goto burp;
208 		if (s[0] == T_BLOCK)
209 			ppstbl1(s, STAT);
210 		else if (s[0] == T_IF || s[0] == T_IFEL)
211 			ifop(s);
212 		else {
213 burp:
214 			ppgoin(STAT);
215 			statement(s);
216 			ppgoout(STAT);
217 		}
218 	}
219 	if (rescnt(&scnt))
220 		getcnt();
221 	if (r[4] != NIL)
222 		unprint();
223 	if (s != NIL && s[0] == T_BLOCK)
224 		ppstbl2();
225 }
226 
227 whilop(r)
228 	int *r;
229 {
230 	struct pxcnt scnt;
231 
232 	ppkw("while");
233 	ppspac();
234 	rvalue(r[2], NIL);
235 	savecnt(&scnt);
236 	getcnt();
237 	ppstdo(r[3], STAT);
238 	if (rescnt(&scnt))
239 		getcnt();
240 }
241 
242 repop(r)
243 	int *r;
244 {
245 	struct pxcnt scnt;
246 
247 	ppkw("repeat");
248 	ppgoin(STAT);
249 	savecnt(&scnt);
250 	getcnt();
251 	statlist(r[2]);
252 	ppgoout(DECL);
253 	ppnl();
254 	indent();
255 	ppkw("until");
256 	ppspac();
257 	rvalue(r[3], NIL);
258 	ppgoin(DECL);
259 	ppgoout(STAT);
260 	if (rescnt(&scnt))
261 		getcnt();
262 }
263 
264 ppstbl(r, m)
265 int *r;
266 {
267 	ppstbl1(r, m);
268 	ppstbl2();
269 }
270 
271 ppstbl1(r, m)
272 int *r;
273 {
274 	ppkw("begin");
275 	ppgoin(m);
276 	statlist(r[2]);
277 	ppgoout(m);
278 }
279 
280 ppstbl2()
281 {
282 	ppnl();
283 	indent();
284 	ppkw("end");
285 }
286 
287 ppstdo(r, l)
288 int *r;
289 {
290 	register *s;
291 
292 	ppspac();
293 	ppkw("do");
294 	ppspac();
295 	s = r;
296 	if (s != NIL && s[0] == T_BLOCK)
297 		ppstbl(s, l);
298 	else {
299 		ppgoin(l);
300 		statement(s);
301 		ppgoout(l);
302 	}
303 }
304