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