xref: /original-bsd/usr.bin/pascal/pc2/langpats.c (revision 62734ea8)
1 /* Copyright (c) 1979 Regents of the University of California */
2 
3 static char sccsid[] = "@(#)langpats.c 1.11 11/12/82";
4 
5 #include <stdio.h>
6 #include <ctype.h>
7 /*
8  * The hash table must be at least twice as big as the number
9  * of patterns, preferably bigger. It must also be a prime number
10  */
11 #define HSHSIZ	101
12 
13 struct pats {
14 	char	*name;
15 	char	*replace;
16 } ptab[] = {
17 
18 /*
19  * C library routines
20  */
21 	{ "1,_fgetc\n",
22 "	sobgeq	*(sp),1f\n\
23 	calls	$1,__filbuf\n\
24 	jbr     2f\n\
25 1:\n\
26 	addl3	$4,(sp)+,r1\n\
27 	movzbl	*(r1),r0\n\
28 	incl	(r1)\n\
29 2:\n" },
30 
31 	{ "2,_fputc\n",
32 "	sobgeq	*4(sp),1f\n\
33 	calls	$2,__flsbuf\n\
34 	jbr	2f\n\
35 1:\n\
36 	popr	$0x3\n\
37 	movb	r0,*4(r1)\n\
38 	incl	4(r1)\n\
39 2:\n" },
40 
41 /*
42  * VAX special instructions
43  */
44 	{ "3,_blkcpy\n",
45 "	popr	$0xa\n\
46 	jbr	2f\n\
47 1:\n\
48 	subl2	r0,(sp)\n\
49 	movc3	r0,(r1),(r3)\n\
50 2:\n\
51 	movzwl	$65535,r0\n\
52 	cmpl	(sp),r0\n\
53 	jgtr	1b\n\
54 	movl	(sp)+,r0\n\
55 	movc3	r0,(r1),(r3)\n" },
56 
57 	{ "2,_blkclr\n",
58 "	movl	(sp)+,r3\n\
59 	jbr	2f\n\
60 1:\n\
61 	subl2	r0,(sp)\n\
62 	movc5	$0,(r3),$0,r0,(r3)\n\
63 2:\n\
64 	movzwl	$65535,r0\n\
65 	cmpl	(sp),r0\n\
66 	jgtr	1b\n\
67 	movl	(sp)+,r0\n\
68 	movc5	$0,(r3),$0,r0,(r3)\n" },
69 
70 	{ "3,_LOCC\n",
71 "	popr	$0x30\n\
72 	movl	(sp)+,r1\n\
73 1:\n\
74 	movzwl	$65535,r0\n\
75 	cmpl	r5,r0\n\
76 	jleq	1f\n\
77 	subl2	r0,r5\n\
78 	locc	r4,r0,(r1)\n\
79 	jeql	1b\n\
80 	addl2	r5,r0\n\
81 	jbr	2f\n\
82 1:\n\
83 	locc	r4,r5,(r1)\n\
84 2:\n" },
85 
86 	{ "2,_ROUND\n",
87 "	cvtrdl	(sp)+,r0\n" },
88 
89 	{ "2,_TRUNC\n",
90 "	cvtdl	(sp)+,r0\n" },
91 
92 /*
93  * General Pascal library routines
94  */
95 	{ "1,_ACTFILE\n",
96 "	movl	(sp)+,r1\n\
97 	movl	12(r1),r0\n" },
98 
99 	{ "2,_FCALL\n",
100 "	movl	4(sp),r0\n\
101 	movc3	4(r0),__disply+8,*(sp)+\n\
102 	movl	(sp)+,r0\n\
103 	movc3	4(r0),8(r0),__disply+8\n" },
104 
105 	{ "2,_FRTN\n",
106 "	movl	(sp)+,r0\n\
107 	movc3	4(r0),*(sp)+,__disply+8\n" },
108 
109 	{ "3,_FSAV\n",
110 "	movl	8(sp),r0\n\
111 	movl	(sp)+,(r0)\n\
112 	ashl	$3,(sp)+,4(r0)\n\
113 	movc3	4(r0),__disply+8,8(r0)\n\
114 	movl	(sp)+,r0\n" },
115 
116 /*
117  * Pascal relational comparisons
118  */
119 	{ "3,_RELEQ\n",
120 "	popr	$0xb\n\
121 	movl	r0,r4\n\
122 1:\n\
123 	movzwl	$65535,r0\n\
124 	cmpl	r4,r0\n\
125 	jleq	3f\n\
126 	subl2	r0,r4\n\
127 	cmpc3	r0,(r1),(r3)\n\
128 	jeql	1b\n\
129 2:\n\
130 	clrl	r0\n\
131 	jbr	4f\n\
132 3:\n\
133 	cmpc3	r4,(r1),(r3)\n\
134 	jneq	2b\n\
135 	incl	r0\n\
136 4:\n" },
137 
138 	{ "3,_RELNE\n",
139 "	popr	$0xb\n\
140 	movl	r0,r4\n\
141 1:\n\
142 	movzwl	$65535,r0\n\
143 	cmpl	r4,r0\n\
144 	jleq	3f\n\
145 	subl2	r0,r4\n\
146 	cmpc3	r0,(r1),(r3)\n\
147 	jeql	1b\n\
148 2:\n\
149 	movl	$1,r0\n\
150 	jbr	4f\n\
151 3:\n\
152 	cmpc3	r4,(r1),(r3)\n\
153 	jneq	2b\n\
154 4:\n" },
155 
156 	{ "3,_RELSLT\n",
157 "	popr	$0xb\n\
158 	movl	r0,r4\n\
159 	jbr	2f\n\
160 1:\n\
161 	subl2	r0,r4\n\
162 	cmpc3	r0,(r1),(r3)\n\
163 	jneq	3f\n\
164 2:\n\
165 	movzwl	$65535,r0\n\
166 	cmpl	r4,r0\n\
167 	jgtr	1b\n\
168 	cmpc3	r4,(r1),(r3)\n\
169 3:\n\
170 	jlss	4f\n\
171 	clrl	r0\n\
172 	jbr	5f\n\
173 4:\n\
174 	movl	$1,r0\n\
175 5:\n" },
176 
177 	{ "3,_RELSLE\n",
178 "	popr	$0xb\n\
179 	movl	r0,r4\n\
180 	jbr	2f\n\
181 1:\n\
182 	subl2	r0,r4\n\
183 	cmpc3	r0,(r1),(r3)\n\
184 	jneq	3f\n\
185 2:\n\
186 	movzwl	$65535,r0\n\
187 	cmpl	r4,r0\n\
188 	jgtr	1b\n\
189 	cmpc3	r4,(r1),(r3)\n\
190 3:\n\
191 	jleq	4f\n\
192 	clrl	r0\n\
193 	jbr	5f\n\
194 4:\n\
195 	movl	$1,r0\n\
196 5:\n" },
197 
198 	{ "3,_RELSGT\n",
199 "	popr	$0xb\n\
200 	movl	r0,r4\n\
201 	jbr	2f\n\
202 1:\n\
203 	subl2	r0,r4\n\
204 	cmpc3	r0,(r1),(r3)\n\
205 	jneq	3f\n\
206 2:\n\
207 	movzwl	$65535,r0\n\
208 	cmpl	r4,r0\n\
209 	jgtr	1b\n\
210 	cmpc3	r4,(r1),(r3)\n\
211 3:\n\
212 	jgtr	4f\n\
213 	clrl	r0\n\
214 	jbr	5f\n\
215 4:\n\
216 	movl	$1,r0\n\
217 5:\n" },
218 
219 	{ "3,_RELSGE\n",
220 "	popr	$0xb\n\
221 	movl	r0,r4\n\
222 	jbr	2f\n\
223 1:\n\
224 	subl2	r0,r4\n\
225 	cmpc3	r0,(r1),(r3)\n\
226 	jneq	3f\n\
227 2:\n\
228 	movzwl	$65535,r0\n\
229 	cmpl	r4,r0\n\
230 	jgtr	1b\n\
231 	cmpc3	r4,(r1),(r3)\n\
232 3:\n\
233 	jgeq	4f\n\
234 	clrl	r0\n\
235 	jbr	5f\n\
236 4:\n\
237 	movl	$1,r0\n\
238 5:\n" },
239 
240 /*
241  * Pascal set operations.
242  */
243 	{ "4,_ADDT\n",
244 "	popr	$0x17\n\
245 	movl	r0,r3\n\
246 1:\n\
247 	bisl3	(r1)+,(r2)+,(r3)+\n\
248 	sobgtr	r4,1b\n" },
249 
250 	{ "4,_SUBT\n",
251 "	popr	$0x17\n\
252 	movl	r0,r3\n\
253 1:\n\
254 	bicl3	(r2)+,(r1)+,(r3)+\n\
255 	sobgtr	r4,1b\n" },
256 
257 	{ "4,_MULT\n",
258 "	popr	$0x17\n\
259 	movl	r0,r3\n\
260 1:\n\
261 	mcoml	(r1)+,r5\n\
262 	bicl3	r5,(r2)+,(r3)+\n\
263 	sobgtr	r4,1b\n" },
264 
265 	{ "4,_IN\n",
266 "	popr	$0x1e\n\
267 	clrl	r0\n\
268 	subl2	r2,r1\n\
269 	cmpl	r1,r3\n\
270 	jgtru	1f\n\
271 	jbc	r1,(r4),1f\n\
272 	incl	r0\n\
273 1:\n" },
274 
275 /*
276  * Pascal runtime checks
277  */
278 	{ "1,_ASRT\n",
279 "	tstl	(sp)+\n\
280 	jneq	1f\n\
281 	pushl	$0\n\
282 	pushl	$_EASRT\n\
283 	calls	$2,_ERROR\n\
284 1:\n" },
285 
286 	{ "2,_ASRTS\n",
287 "	popr	$0x03\n\
288 	tstl	r0\n\
289 	jneq	1f\n\
290 	pushl	r1\n\
291 	pushl	$_EASRTS\n\
292 	calls	$2,_ERROR\n\
293 1:\n" },
294 
295 	{ "1,_CHR\n",
296 "	movl	(sp)+,r0\n\
297 	cmpl	r0,$127\n\
298 	jlequ	1f\n\
299 	pushl	r0\n\
300 	pushl	$_ECHR\n\
301 	calls	$2,_ERROR\n\
302 1:\n" },
303 
304 	{ "0,_LINO\n",
305 "	incl	__stcnt\n\
306 	cmpl	__stcnt,__stlim\n\
307 	jlss	1f\n\
308 	pushl	__stcnt\n\
309 	pushl	$_ELINO\n\
310 	calls	$2,_ERROR\n\
311 1:\n" },
312 
313 	{ "1,_NIL\n",
314 "	movl	(sp)+,r0\n\
315 	cmpl	r0,__maxptr\n\
316 	jgtr	1f\n\
317 	cmpl	r0,__minptr\n\
318 	jgeq	2f\n\
319 1:\n\
320 	pushl	$0\n\
321 	pushl	$_ENIL\n\
322 	calls	$2,_ERROR\n\
323 2:\n" },
324 
325 	{ "2,_RANDOM\n",
326 "	addl2	$8,sp\n\
327 	emul	__seed,$1103515245,$0,r0\n\
328 	ediv	$0x7fffffff,r0,r1,r0\n\
329 	movl	r0,__seed\n\
330 	cvtld	r0,r0\n\
331 	divd2	$0d2.147483647e+09,r0\n" },
332 
333 	{ "3,_RANG4\n",
334 "	popr	$0x07\n\
335 	cmpl	r0,r1\n\
336 	jlss	1f\n\
337 	cmpl	r0,r2\n\
338 	jleq	2f\n\
339 1:\n\
340 	pushl	r0\n\
341 	pushl	$_ERANG\n\
342 	calls	$2,_ERROR\n\
343 2:\n" },
344 
345 	{ "2,_RSNG4\n",
346 "	popr	$0x03\n\
347 	cmpl	r0,r1\n\
348 	jlequ	1f\n\
349 	pushl	r0\n\
350 	pushl	$_ERANG\n\
351 	calls	$2,_ERROR\n\
352 1:\n" },
353 
354 	{ "1,_SEED\n",
355 "	movl	__seed,r0\n\
356 	movl	(sp)+,__seed\n" },
357 
358 	{ "3,_SUBSC\n",
359 "	popr	$0x07\n\
360 	cmpl	r0,r1\n\
361 	jlss	1f\n\
362 	cmpl	r0,r2\n\
363 	jleq	2f\n\
364 1:\n\
365 	pushl	r0\n\
366 	pushl	$_ESUBSC\n\
367 	calls	$2,_ERROR\n\
368 2:\n" },
369 
370 	{ "2,_SUBSCZ\n",
371 "	popr	$0x03\n\
372 	cmpl	r0,r1\n\
373 	jlequ	1f\n\
374 	pushl	r0\n\
375 	pushl	$_ESUBSC\n\
376 	calls	$2,_ERROR\n\
377 1:\n" },
378 
379 };
380 
381 struct pats		*htbl[HSHSIZ];
382 
383 
384 #define HASH(cp, hp) {\
385 	hash = 0; rehash = 1; ccp = cp; \
386 	do	{ \
387 		hash *= (int)*ccp++; \
388 	} while (*ccp && *ccp != '\n'); \
389 	hash >>= 7; hash %= HSHSIZ; hp = &htbl[hash]; size = ccp - cp + 1; \
390 	}
391 
392 #define REHASH(hp) {\
393 	hp += rehash; rehash += 2; \
394 	if (hp >= &htbl[HSHSIZ]) \
395 		hp -= HSHSIZ; \
396 	}
397 
398 
399 main(argc, argv)
400 
401 	int	argc;
402 	char	**argv;
403 {
404 	register struct pats	*pp;
405 	register struct pats	**hp;
406 	register char		*cp, *ccp, *lp;
407 	register int		hash, rehash, size;
408 	char			line[BUFSIZ];
409 	extern char		*index();
410 
411 	if (argc > 1)
412 		freopen(argv[1], "r", stdin);
413 	if (argc > 2)
414 		freopen(argv[2], "w", stdout);
415 	/*
416 	 * set up the hash table
417 	 */
418 	for(pp = ptab; pp < &ptab[sizeof ptab/sizeof ptab[0]]; pp++) {
419 		HASH(pp->name, hp);
420 		while (*hp)
421 			REHASH(hp);
422 		*hp = pp;
423 	}
424 	/*
425 	 * check each line and replace as appropriate
426 	 */
427 	while (fgets(line, BUFSIZ, stdin)) {
428 		lp = index(line, ':');
429 		for (cp = (lp != NULL) ? ++lp : line; *cp == '\t'; )
430 			cp++;
431 		if (strcmpn(cp, "calls\t$", 7) != 0) {
432 			fputs(line, stdout);
433 			continue;
434 		}
435 		cp += 7;
436 		HASH(cp, hp);
437 		while (*hp) {
438 			if (strcmpn((*hp)->name, cp, size)==NULL) {
439 				if (lp != NULL) {
440 					*lp++ = '\n';
441 					*lp = '\0';
442 					fputs(line, stdout);
443 				}
444 				fputs((*hp)->replace, stdout);
445 				goto nextline;
446 			}
447 			REHASH(hp);
448 		}
449 		fputs(line, stdout);
450 nextline:;
451 	}
452 	exit(0);
453 }
454