xref: /original-bsd/usr.bin/pascal/pxp/stat.c (revision 6c57d260)
1 static	char *sccsid = "@(#)stat.c	1.2 (Berkeley) 03/07/81";
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 		case T_ASRT:
118 			ppitem();
119 			asrtop(s);
120 			break;
121 	}
122 	setinfo(s[1]);
123 	putcm();
124 }
125 
126 withop(s)
127 	int *s;
128 {
129 	register *p;
130 
131 	ppkw("with");
132 	ppspac();
133 	p = s[2];
134 	if (p != NIL)
135 		for (;;) {
136 			lvalue(p[1]);
137 			p = p[2];
138 			if (p == NIL)
139 				break;
140 			ppsep(", ");
141 		}
142 	else
143 		ppid("{record variable list}");
144 	ppstdo(s[3], DECL);
145 }
146 
147 asgnop(r)
148 	int *r;
149 {
150 
151 	lvalue(r[2]);
152 	ppsep(" := ");
153 	rvalue(r[3], NIL);
154 }
155 
156 forop(r)
157 	int *r;
158 {
159 	struct pxcnt scnt;
160 
161 	savecnt(&scnt);
162 	ppkw("for");
163 	ppspac();
164 	asgnop(r[2]);
165 	ppspac();
166 	ppkw(r[0] == T_FORU ? "to" : "downto");
167 	ppspac();
168 	rvalue(r[3], NIL);
169 	getcnt();
170 	ppstdo(r[4], STAT);
171 	if (rescnt(&scnt))
172 		getcnt();
173 }
174 
175 ifop(r)
176 	int *r;
177 {
178 	register *s;
179 	struct pxcnt scnt;
180 
181 	ppkw("if");
182 	ppspac();
183 	rvalue(r[2], NIL);
184 	ppspac();
185 	ppkw("then");
186 	ppspac();
187 	s = r[3];
188 	savecnt(&scnt);
189 	getcnt();
190 	if (s != NIL && s[0] == T_BLOCK)
191 		ppstbl1(s, STAT);
192 	else {
193 		ppgoin(STAT);
194 		statement(s);
195 		ppgoout(STAT);
196 	}
197 	if (r[0] == T_IFEL) {
198 		setcnt(cntof(&scnt)-nowcnt());
199 		if (s == NIL || s[0] != T_BLOCK) {
200 			ppnl();
201 			indent();
202 		} else {
203 			ppstbl2();
204 			ppspac();
205 		}
206 		s = r[4];
207 		ppkw("else");
208 		unprint();
209 		ppspac();
210 		if (s == NIL)
211 			goto burp;
212 		if (s[0] == T_BLOCK)
213 			ppstbl1(s, STAT);
214 		else if (s[0] == T_IF || s[0] == T_IFEL)
215 			ifop(s);
216 		else {
217 burp:
218 			ppgoin(STAT);
219 			statement(s);
220 			ppgoout(STAT);
221 		}
222 	}
223 	if (rescnt(&scnt))
224 		getcnt();
225 	if (r[4] != NIL)
226 		unprint();
227 	if (s != NIL && s[0] == T_BLOCK)
228 		ppstbl2();
229 }
230 
231 whilop(r)
232 	int *r;
233 {
234 	struct pxcnt scnt;
235 
236 	ppkw("while");
237 	ppspac();
238 	rvalue(r[2], NIL);
239 	savecnt(&scnt);
240 	getcnt();
241 	ppstdo(r[3], STAT);
242 	if (rescnt(&scnt))
243 		getcnt();
244 }
245 
246 repop(r)
247 	int *r;
248 {
249 	struct pxcnt scnt;
250 
251 	ppkw("repeat");
252 	ppgoin(STAT);
253 	savecnt(&scnt);
254 	getcnt();
255 	statlist(r[2]);
256 	ppgoout(DECL);
257 	ppnl();
258 	indent();
259 	ppkw("until");
260 	ppspac();
261 	rvalue(r[3], NIL);
262 	ppgoin(DECL);
263 	ppgoout(STAT);
264 	if (rescnt(&scnt))
265 		getcnt();
266 }
267 
268 ppstbl(r, m)
269 int *r;
270 {
271 	ppstbl1(r, m);
272 	ppstbl2();
273 }
274 
275 ppstbl1(r, m)
276 int *r;
277 {
278 	ppkw("begin");
279 	ppgoin(m);
280 	statlist(r[2]);
281 	ppgoout(m);
282 }
283 
284 ppstbl2()
285 {
286 	ppnl();
287 	indent();
288 	ppkw("end");
289 }
290 
291 ppstdo(r, l)
292 int *r;
293 {
294 	register *s;
295 
296 	ppspac();
297 	ppkw("do");
298 	ppspac();
299 	s = r;
300 	if (s != NIL && s[0] == T_BLOCK)
301 		ppstbl(s, l);
302 	else {
303 		ppgoin(l);
304 		statement(s);
305 		ppgoout(l);
306 	}
307 }
308 
309 asrtop(s)
310 	int *s;
311 {
312 
313 	ppkw("assert");
314 	ppspac();
315 	rvalue(s[2], NIL);
316 }
317