xref: /original-bsd/usr.bin/pascal/pc2/langpats.c (revision 6c57d260)
1 /* Copyright (c) 1979 Regents of the University of California */
2 
3 static char sccsid[] = "@(#)langpats.c 1.9 04/01/81";
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 	{ "1,_ACTFILE\n",
19 "	popr	$0x2\n\
20 	movl	12(r1),r0\n" },
21 
22 	{ "1,_fgetc\n",
23 "	sobgeq	*(sp),1f\n\
24 	calls	$1,__filbuf\n\
25 	jbr     2f\n\
26 1:\n\
27 	addl3	$4,(sp)+,r1\n\
28 	movzbl	*(r1),r0\n\
29 	incl	(r1)\n\
30 2:\n" },
31 
32 	{ "2,_fputc\n",
33 "	sobgeq	*4(sp),1f\n\
34 	calls	$2,__flsbuf\n\
35 	jbr	2f\n\
36 1:\n\
37 	popr	$0x3\n\
38 	movb	r0,*4(r1)\n\
39 	incl	4(r1)\n\
40 2:\n" },
41 
42 	{ "3,_blkcpy\n",
43 "	popr	$0xb\n\
44 	pushl	r0\n\
45 	jbr	2f\n\
46 1:\n\
47 	subl2	r0,(sp)\n\
48 	movc3	r0,(r1),(r3)\n\
49 2:\n\
50 	movzwl	$65535,r0\n\
51 	cmpl	(sp),r0\n\
52 	jgtr	1b\n\
53 	popr	$0x1\n\
54 	movc3	r0,(r1),(r3)\n" },
55 
56 	{ "2,_blkclr\n",
57 "	movl	4(sp),r3\n\
58 	jbr	2f\n\
59 1:\n\
60 	subl2	r0,(sp)\n\
61 	movc5	$0,(r3),$0,r0,(r3)\n\
62 2:\n\
63 	movzwl	$65535,r0\n\
64 	cmpl	(sp),r0\n\
65 	jgtr	1b\n\
66 	popr	$0x3\n\
67 	movc5	$0,(r3),$0,r0,(r3)\n" },
68 
69 	{ "3,_LOCC\n",
70 "	popr	$0x30\n\
71 	popr	$0x2\n\
72 1:\n\
73 	movzwl	$65535,r0\n\
74 	cmpl	r5,r0\n\
75 	jleq	1f\n\
76 	subl2	r0,r5\n\
77 	locc	r4,r0,(r1)\n\
78 	jeql	1b\n\
79 	addl2	r5,r0\n\
80 	jbr	2f\n\
81 1:\n\
82 	locc	r4,r5,(r1)\n\
83 2:\n" },
84 
85 	{ "2,_ROUND\n",
86 "	cvtrdl	(sp)+,r0\n" },
87 
88 	{ "2,_TRUNC\n",
89 "	cvtdl	(sp)+,r0\n" },
90 
91 	{ "2,_FCALL\n",
92 "	movl	4(sp),r0\n\
93 	ashl	$3,4(r0),r1\n\
94 	movc3	r1,__disply+8,*(sp)+\n\
95 	movl	(sp)+,r0\n\
96 	ashl	$3,4(r0),r1\n\
97 	movc3	r1,8(r0),__disply+8\n" },
98 
99 	{ "2,_FRTN\n",
100 "	movl	(sp)+,r0\n\
101 	ashl	$3,4(r0),r1\n\
102 	movc3	r1,*(sp)+,__disply+8\n" },
103 
104 	{ "3,_FSAV\n",
105 "	movl	8(sp),r0\n\
106 	movl	(sp)+,(r0)\n\
107 	movl	(sp)+,4(r0)\n\
108 	ashl	$3,4(r0),r1\n\
109 	movc3	r1,__disply+8,8(r0)\n\
110 	popr	$0x1\n" },
111 
112 	{ "3,_RELEQ\n",
113 "	popr	$0x10\n\
114 	popr	$0xa\n\
115 1:\n\
116 	movzwl	$65535,r0\n\
117 	cmpl	r4,r0\n\
118 	jleq	3f\n\
119 	subl2	r0,r4\n\
120 	cmpc3	r0,(r1),(r3)\n\
121 	jeql	1b\n\
122 2:\n\
123 	clrl	r0\n\
124 	jbr	4f\n\
125 3:\n\
126 	cmpc3	r4,(r1),(r3)\n\
127 	jneq	2b\n\
128 	incl	r0\n\
129 4:\n" },
130 
131 	{ "3,_RELNE\n",
132 "	popr	$0x10\n\
133 	popr	$0xa\n\
134 1:\n\
135 	movzwl	$65535,r0\n\
136 	cmpl	r4,r0\n\
137 	jleq	3f\n\
138 	subl2	r0,r4\n\
139 	cmpc3	r0,(r1),(r3)\n\
140 	jeql	1b\n\
141 2:\n\
142 	movl	$1,r0\n\
143 	jbr	4f\n\
144 3:\n\
145 	cmpc3	r4,(r1),(r3)\n\
146 	jneq	2b\n\
147 4:\n" },
148 
149 	{ "3,_RELSLT\n",
150 "	popr	$0x10\n\
151 	popr	$0xa\n\
152 	jbr	2f\n\
153 1:\n\
154 	subl2	r0,r4\n\
155 	cmpc3	r0,(r1),(r3)\n\
156 	jneq	3f\n\
157 2:\n\
158 	movzwl	$65535,r0\n\
159 	cmpl	r4,r0\n\
160 	jgtr	1b\n\
161 	cmpc3	r4,(r1),(r3)\n\
162 3:\n\
163 	jlss	4f\n\
164 	clrl	r0\n\
165 	jbr	5f\n\
166 4:\n\
167 	movl	$1,r0\n\
168 5:\n" },
169 
170 	{ "3,_RELSLE\n",
171 "	popr	$0x10\n\
172 	popr	$0xa\n\
173 	jbr	2f\n\
174 1:\n\
175 	subl2	r0,r4\n\
176 	cmpc3	r0,(r1),(r3)\n\
177 	jneq	3f\n\
178 2:\n\
179 	movzwl	$65535,r0\n\
180 	cmpl	r4,r0\n\
181 	jgtr	1b\n\
182 	cmpc3	r4,(r1),(r3)\n\
183 3:\n\
184 	jleq	4f\n\
185 	clrl	r0\n\
186 	jbr	5f\n\
187 4:\n\
188 	movl	$1,r0\n\
189 5:\n" },
190 
191 	{ "3,_RELSGT\n",
192 "	popr	$0x10\n\
193 	popr	$0xa\n\
194 	jbr	2f\n\
195 1:\n\
196 	subl2	r0,r4\n\
197 	cmpc3	r0,(r1),(r3)\n\
198 	jneq	3f\n\
199 2:\n\
200 	movzwl	$65535,r0\n\
201 	cmpl	r4,r0\n\
202 	jgtr	1b\n\
203 	cmpc3	r4,(r1),(r3)\n\
204 3:\n\
205 	jgtr	4f\n\
206 	clrl	r0\n\
207 	jbr	5f\n\
208 4:\n\
209 	movl	$1,r0\n\
210 5:\n" },
211 
212 	{ "3,_RELSGE\n",
213 "	popr	$0x10\n\
214 	popr	$0xa\n\
215 	jbr	2f\n\
216 1:\n\
217 	subl2	r0,r4\n\
218 	cmpc3	r0,(r1),(r3)\n\
219 	jneq	3f\n\
220 2:\n\
221 	movzwl	$65535,r0\n\
222 	cmpl	r4,r0\n\
223 	jgtr	1b\n\
224 	cmpc3	r4,(r1),(r3)\n\
225 3:\n\
226 	jgeq	4f\n\
227 	clrl	r0\n\
228 	jbr	5f\n\
229 4:\n\
230 	movl	$1,r0\n\
231 5:\n" },
232 
233 	{ "4,_ADDT\n",
234 "	popr	$0x17\n\
235 	movl	r0,r3\n\
236 1:\n\
237 	bisl3	(r1)+,(r2)+,(r3)+\n\
238 	sobgtr	r4,1b\n" },
239 
240 	{ "4,_SUBT\n",
241 "	popr	$0x17\n\
242 	movl	r0,r3\n\
243 1:\n\
244 	bicl3	(r2)+,(r1)+,(r3)+\n\
245 	sobgtr	r4,1b\n" },
246 
247 	{ "4,_MULT\n",
248 "	popr	$0x17\n\
249 	movl	r0,r3\n\
250 1:\n\
251 	mcoml	(r1)+,r5\n\
252 	bicl3	r5,(r2)+,(r3)+\n\
253 	sobgtr	r4,1b\n" },
254 
255 	{ "4,_IN\n",
256 "	popr	$0x1e\n\
257 	clrl	r0\n\
258 	subl2	r2,r1\n\
259 	cmpl	r1,r3\n\
260 	jgtru	1f\n\
261 	jbc	r1,(r4),1f\n\
262 	incl	r0\n\
263 1:\n" }
264 };
265 
266 struct pats		*htbl[HSHSIZ];
267 
268 
269 #define HASH(cp, hp) {\
270 	hash = 0; rehash = 1; ccp = cp; \
271 	do	{ \
272 		hash *= (int)*ccp++; \
273 	} while (*ccp && *ccp != '\n'); \
274 	hash >>= 7; hash %= HSHSIZ; hp = &htbl[hash]; size = ccp - cp + 1; \
275 	}
276 
277 #define REHASH(hp) {\
278 	hp += rehash; rehash += 2; \
279 	if (hp >= &htbl[HSHSIZ]) \
280 		hp -= HSHSIZ; \
281 	}
282 
283 
284 main(argc, argv)
285 
286 	int	argc;
287 	char	**argv;
288 {
289 	register struct pats	*pp;
290 	register struct pats	**hp;
291 	register char		*cp, *ccp, *lp;
292 	register int		hash, rehash, size;
293 	char			line[BUFSIZ];
294 	extern char		*index();
295 
296 	if (argc > 1)
297 		freopen(argv[1], "r", stdin);
298 	if (argc > 2)
299 		freopen(argv[2], "w", stdout);
300 	/*
301 	 * set up the hash table
302 	 */
303 	for(pp = ptab; pp < &ptab[sizeof ptab/sizeof ptab[0]]; pp++) {
304 		HASH(pp->name, hp);
305 		while (*hp)
306 			REHASH(hp);
307 		*hp = pp;
308 	}
309 	/*
310 	 * check each line and replace as appropriate
311 	 */
312 	while (fgets(line, BUFSIZ, stdin)) {
313 		lp = index(line, ':');
314 		for (cp = (lp != NULL) ? ++lp : line; *cp == '\t'; )
315 			cp++;
316 		if (strcmpn(cp, "calls\t$", 7) != 0) {
317 			fputs(line, stdout);
318 			continue;
319 		}
320 		cp += 7;
321 		HASH(cp, hp);
322 		while (*hp) {
323 			if (strcmpn((*hp)->name, cp, size)==NULL) {
324 				if (lp != NULL) {
325 					*lp++ = '\n';
326 					*lp = '\0';
327 					fputs(line, stdout);
328 				}
329 				fputs((*hp)->replace, stdout);
330 				goto nextline;
331 			}
332 			REHASH(hp);
333 		}
334 		fputs(line, stdout);
335 nextline:;
336 	}
337 	exit(0);
338 }
339