12086Smckusick /* Copyright (c) 1979 Regents of the University of California */
22086Smckusick 
3*2195Smckusic static char sccsid[] = "@(#)langpats.c 1.4 01/17/81";
42086Smckusick 
52086Smckusick #include <stdio.h>
62086Smckusick #include <ctype.h>
72086Smckusick /*
82086Smckusick  * The hash table must be at least twice as big as the number
92086Smckusick  * of patterns, preferably bigger. It must also be a prime number
102086Smckusick  */
112086Smckusick #define HSHSIZ	101
122086Smckusick 
132086Smckusick struct pats {
142086Smckusick 	char	*name;
152086Smckusick 	char	*replace;
162086Smckusick } ptab[] = {
172086Smckusick 
182189Smckusic 	{ "1,_ACTFILE\n",
19*2195Smckusic "	popr	$0x2\n\
202086Smckusick 	movl	12(r1),r0\n" },
212086Smckusick 
222189Smckusic 	{ "1,_fgetc\n",
232189Smckusic "	sobgeq	*(sp),1f\n\
242086Smckusick 	calls	$1,__filbuf\n\
252086Smckusick 	jbr     2f\n\
262086Smckusick 1:\n\
272086Smckusick 	addl3	$4,(sp)+,r1\n\
282086Smckusick 	movzbl	*(r1),r0\n\
292086Smckusick 	incl	(r1)\n\
302086Smckusick 2:\n" },
312086Smckusick 
322189Smckusic 	{ "2,_fputc\n",
332189Smckusic "	sobgeq	*4(sp),1f\n\
342086Smckusick 	calls	$2,__flsbuf\n\
352086Smckusick 	jbr	2f\n\
362086Smckusick 1:\n\
37*2195Smckusic 	popr	$0x3\n\
38*2195Smckusic 	movb	r0,*4(r1)\n\
39*2195Smckusic 	incl	4(r1)\n\
402086Smckusick 2:\n" },
412086Smckusick 
422189Smckusic 	{ "3,_blkcpy\n",
43*2195Smckusic "	popr	$0xb\n\
44*2195Smckusic 	pushl	r0\n\
45*2195Smckusic 	jbr	2f\n\
462086Smckusick 1:\n\
47*2195Smckusic 	subl2	r0,(sp)\n\
48*2195Smckusic 	movc3	r0,(r1),(r3)\n\
49*2195Smckusic 2:\n\
502086Smckusick 	movzwl	$65535,r0\n\
512086Smckusick 	cmpl	(sp),r0\n\
52*2195Smckusic 	jgtr	1b\n\
53*2195Smckusic 	popr	$0x1\n\
542086Smckusick 	movc3	r0,(r1),(r3)\n" },
552086Smckusick 
562189Smckusic 	{ "2,_blkclr\n",
572086Smckusick "	movl	4(sp),r3\n\
582189Smckusic 	subl3	r3,r3,-4(sp)\n\
592189Smckusic 	jbr	2f\n\
602086Smckusick 1:\n\
612189Smckusic 	subl2	r0,(sp)\n\
622189Smckusic 	movc5	$0,(r3),$0,r0,(r3)\n\
632189Smckusic 2:\n\
642086Smckusick 	movzwl	$65535,r0\n\
652086Smckusick 	cmpl	(sp),r0\n\
662189Smckusic 	jgtr	1b\n\
67*2195Smckusic 	popr	$0x3\n\
682189Smckusic 	movc5	$0,(r3),$0,r0,(r3)\n" },
692086Smckusick 
702189Smckusic 	{ "3,_LOCC\n",
71*2195Smckusic "	popr	$0x30\n\
72*2195Smckusic 	popr	$0x2\n\
732086Smckusick 1:\n\
742086Smckusick 	movzwl	$65535,r0\n\
75*2195Smckusic 	cmpl	r5,r0\n\
762102Smckusic 	jleq	1f\n\
77*2195Smckusic 	subl2	r0,r5\n\
78*2195Smckusic 	locc	r4,r0,(r1)\n\
792102Smckusic 	jeql	1b\n\
80*2195Smckusic 	addl2	r5,r0\n\
812102Smckusic 	jbr	2f\n\
822086Smckusick 1:\n\
83*2195Smckusic 	locc	r4,r5,(r1)\n\
842086Smckusick 2:\n" },
852086Smckusick 
862189Smckusic 	{ "1,_ROUND\n",
872086Smckusick "	cvtrdl	(sp)+,r0\n" },
882086Smckusick 
892189Smckusic 	{ "1,_TRUNC\n",
902086Smckusick "	cvtdl	(sp)+,r0\n" },
912086Smckusick 
922189Smckusic 	{ "1,_FCALL\n",
932086Smckusick "	movl	(sp),r0\n\
942086Smckusick 	ashl	$3,4(r0),r1\n\
952086Smckusick 	movc3	r1,__disply+8,8(r0)[r1]\n\
962086Smckusick 	movl	(sp),r0\n\
972086Smckusick 	ashl	$3,4(r0),r1\n\
982086Smckusick 	movc3	r1,8(r0),__disply+8\n\
992086Smckusick 	movl	*(sp)+,r0\n" },
1002086Smckusick 
1012189Smckusic 	{ "2,_FRTN\n",
102*2195Smckusic "	popr	$0x1\n\
1032086Smckusick 	ashl	$3,4(r0),r1\n\
1042086Smckusick 	movc3	r1,8(r0)[r1],__disply+8\n\
105*2195Smckusic 	popr	$0x1\n" },
1062086Smckusick 
1072189Smckusic 	{ "3,_FSAV\n",
1082086Smckusick "	movl	8(sp),r0\n\
1092086Smckusick 	movl	(sp)+,(r0)\n\
1102086Smckusick 	movl	(sp)+,4(r0)\n\
1112086Smckusick 	ashl	$3,4(r0),r1\n\
1122086Smckusick 	movc3	r1,__disply+8,8(r0)\n\
113*2195Smckusic 	popr	$0x1\n" },
1142086Smckusick 
1152189Smckusic 	{ "3,_RELEQ\n",
116*2195Smckusic "	popr	$0x10\n\
117*2195Smckusic 	popr	$0xa\n\
1182102Smckusic 1:\n\
1192102Smckusic 	movzwl	$65535,r0\n\
1202102Smckusic 	cmpl	r4,r0\n\
121*2195Smckusic 	jleq	3f\n\
1222102Smckusic 	subl2	r0,r4\n\
1232102Smckusic 	cmpc3	r0,(r1),(r3)\n\
1242102Smckusic 	jeql	1b\n\
1252102Smckusic 2:\n\
126*2195Smckusic 	clrl	r0\n\
127*2195Smckusic 	jbr	4f\n\
128*2195Smckusic 3:\n\
1292102Smckusic 	cmpc3	r4,(r1),(r3)\n\
130*2195Smckusic 	jneq	2b\n\
1312102Smckusic 	incl	r0\n\
1322102Smckusic 4:\n" },
1332086Smckusick 
1342189Smckusic 	{ "3,_RELNE\n",
135*2195Smckusic "	popr	$0x10\n\
136*2195Smckusic 	popr	$0xa\n\
1372086Smckusick 1:\n\
1382102Smckusic 	movzwl	$65535,r0\n\
1392102Smckusic 	cmpl	r4,r0\n\
140*2195Smckusic 	jleq	3f\n\
1412102Smckusic 	subl2	r0,r4\n\
1422102Smckusic 	cmpc3	r0,(r1),(r3)\n\
1432102Smckusic 	jeql	1b\n\
1442102Smckusic 2:\n\
145*2195Smckusic 	movl	$1,r0\n\
146*2195Smckusic 	jbr	4f\n\
147*2195Smckusic 3:\n\
1482102Smckusic 	cmpc3	r4,(r1),(r3)\n\
149*2195Smckusic 	jneq	2b\n\
1502102Smckusic 4:\n" },
1512086Smckusick 
1522189Smckusic 	{ "3,_RELSLT\n",
153*2195Smckusic "	popr	$0x10\n\
154*2195Smckusic 	popr	$0xa\n\
155*2195Smckusic 	jbr	2f\n\
1562102Smckusic 1:\n\
1572102Smckusic 	subl2	r0,r4\n\
1582102Smckusic 	cmpc3	r0,(r1),(r3)\n\
159*2195Smckusic 	jneq	3f\n\
1602102Smckusic 2:\n\
161*2195Smckusic 	movzwl	$65535,r0\n\
162*2195Smckusic 	cmpl	r4,r0\n\
163*2195Smckusic 	jgtr	1b\n\
1642102Smckusic 	cmpc3	r4,(r1),(r3)\n\
1652102Smckusic 3:\n\
1662102Smckusic 	jlss	4f\n\
1672086Smckusick 	clrl	r0\n\
1682102Smckusic 	jbr	5f\n\
1692102Smckusic 4:\n\
1702086Smckusick 	movl	$1,r0\n\
1712102Smckusic 5:\n" },
1722086Smckusick 
1732189Smckusic 	{ "3,_RELSLE\n",
174*2195Smckusic "	popr	$0x10\n\
175*2195Smckusic 	popr	$0xa\n\
176*2195Smckusic 	jbr	2f\n\
1772102Smckusic 1:\n\
1782102Smckusic 	subl2	r0,r4\n\
1792102Smckusic 	cmpc3	r0,(r1),(r3)\n\
180*2195Smckusic 	jneq	3f\n\
1812102Smckusic 2:\n\
182*2195Smckusic 	movzwl	$65535,r0\n\
183*2195Smckusic 	cmpl	r4,r0\n\
184*2195Smckusic 	jgtr	1b\n\
1852102Smckusic 	cmpc3	r4,(r1),(r3)\n\
1862102Smckusic 3:\n\
1872102Smckusic 	jleq	4f\n\
1882086Smckusick 	clrl	r0\n\
1892102Smckusic 	jbr	5f\n\
1902102Smckusic 4:\n\
1912086Smckusick 	movl	$1,r0\n\
1922102Smckusic 5:\n" },
1932086Smckusick 
1942189Smckusic 	{ "3,_RELSGT\n",
195*2195Smckusic "	popr	$0x10\n\
196*2195Smckusic 	popr	$0xa\n\
197*2195Smckusic 	jbr	2f\n\
1982102Smckusic 1:\n\
1992102Smckusic 	subl2	r0,r4\n\
2002102Smckusic 	cmpc3	r0,(r1),(r3)\n\
201*2195Smckusic 	jneq	3f\n\
2022102Smckusic 2:\n\
203*2195Smckusic 	movzwl	$65535,r0\n\
204*2195Smckusic 	cmpl	r4,r0\n\
205*2195Smckusic 	jgtr	1b\n\
2062102Smckusic 	cmpc3	r4,(r1),(r3)\n\
2072102Smckusic 3:\n\
2082102Smckusic 	jgtr	4f\n\
2092086Smckusick 	clrl	r0\n\
2102102Smckusic 	jbr	5f\n\
2112102Smckusic 4:\n\
2122086Smckusick 	movl	$1,r0\n\
2132102Smckusic 5:\n" },
2142086Smckusick 
2152189Smckusic 	{ "3,_RELSGE\n",
216*2195Smckusic "	popr	$0x10\n\
217*2195Smckusic 	popr	$0xa\n\
218*2195Smckusic 	jbr	2f\n\
2192102Smckusic 1:\n\
2202102Smckusic 	subl2	r0,r4\n\
2212102Smckusic 	cmpc3	r0,(r1),(r3)\n\
222*2195Smckusic 	jneq	3f\n\
2232102Smckusic 2:\n\
224*2195Smckusic 	movzwl	$65535,r0\n\
225*2195Smckusic 	cmpl	r4,r0\n\
226*2195Smckusic 	jgtr	1b\n\
2272102Smckusic 	cmpc3	r4,(r1),(r3)\n\
2282102Smckusic 3:\n\
2292102Smckusic 	jgeq	4f\n\
2302086Smckusick 	clrl	r0\n\
2312102Smckusic 	jbr	5f\n\
2322102Smckusic 4:\n\
2332086Smckusick 	movl	$1,r0\n\
2342102Smckusic 5:\n" },
2352086Smckusick 
2362189Smckusic 	{ "4,_ADDT\n",
237*2195Smckusic "	popr	$0x17\n\
2382086Smckusick 	movl	r0,r3\n\
2392086Smckusick 1:\n\
2402086Smckusick 	bisl3	(r1)+,(r2)+,(r3)+\n\
2412086Smckusick 	sobgtr	r4,1b\n" },
2422086Smckusick 
2432189Smckusic 	{ "4,_SUBT\n",
244*2195Smckusic "	popr	$0x17\n\
2452086Smckusick 	movl	r0,r3\n\
2462086Smckusick 1:\n\
2472086Smckusick 	bicl3	(r2)+,(r1)+,(r3)+\n\
2482086Smckusick 	sobgtr	r4,1b\n" },
2492086Smckusick 
2502189Smckusic 	{ "4,_MULT\n",
251*2195Smckusic "	popr	$0x17\n\
2522086Smckusick 	movl	r0,r3\n\
2532086Smckusick 1:\n\
2542086Smckusick 	mcoml	(r1)+,r5\n\
2552086Smckusick 	bicl3	r5,(r2)+,(r3)+\n\
2562086Smckusick 	sobgtr	r4,1b\n" },
2572086Smckusick 
2582189Smckusic 	{ "4,_IN\n",
2592086Smckusick "	clrl	r0\n\
260*2195Smckusic 	popr	$0x1e\n\
261*2195Smckusic 	subl2	r2,r1\n\
262*2195Smckusic 	cmpl	r1,r3\n\
2632102Smckusic 	jgtru	1f\n\
264*2195Smckusic 	jbc	r1,(r4),1f\n\
265*2195Smckusic 	incl	r0\n\
266*2195Smckusic 1:\n" }
2672086Smckusick };
2682086Smckusick 
2692086Smckusick struct pats		*htbl[HSHSIZ];
2702086Smckusick 
2712086Smckusick 
2722086Smckusick #define	CHK(c)	if (*cp++ != c) goto copy;
2732086Smckusick 
2742086Smckusick #define HASH(cp, hp) {\
2752086Smckusick 	hash = 0; rehash = 1; ccp = cp; \
2762086Smckusick 	do	{ \
2772086Smckusick 		hash *= (int)*ccp++; \
2782086Smckusick 	} while (*ccp && *ccp != '\n'); \
2792086Smckusick 	hash >>= 7; hash %= HSHSIZ; hp = &htbl[hash]; size = ccp - cp + 1; \
2802086Smckusick 	}
2812086Smckusick 
2822086Smckusick #define REHASH(hp) {\
2832086Smckusick 	hp += rehash; rehash += 2; \
2842086Smckusick 	if (hp >= &htbl[HSHSIZ]) \
2852086Smckusick 		hp -= HSHSIZ; \
2862086Smckusick 	}
2872086Smckusick 
2882086Smckusick 
2892086Smckusick main(argc, argv)
2902086Smckusick 
2912086Smckusick 	int	argc;
2922086Smckusick 	char	**argv;
2932086Smckusick {
2942086Smckusick 	register struct pats	*pp;
2952086Smckusick 	register struct pats	**hp;
2962086Smckusick 	register char		*cp, *ccp;
2972086Smckusick 	register int		hash, rehash, size;
2982086Smckusick 	char			line[BUFSIZ];
2992086Smckusick 
3002086Smckusick 	if (argc > 1)
3012086Smckusick 		freopen(argv[1], "r", stdin);
3022086Smckusick 	if (argc > 2)
3032086Smckusick 		freopen(argv[2], "w", stdout);
3042086Smckusick 	/*
3052086Smckusick 	 * set up the hash table
3062086Smckusick 	 */
3072086Smckusick 	for(pp = ptab; pp < &ptab[sizeof ptab/sizeof ptab[0]]; pp++) {
3082086Smckusick 		HASH(pp->name, hp);
3092086Smckusick 		while (*hp)
3102086Smckusick 			REHASH(hp);
3112086Smckusick 		*hp = pp;
3122086Smckusick 	}
3132086Smckusick 	/*
3142086Smckusick 	 * check each line and replace as appropriate
3152086Smckusick 	 */
3162086Smckusick 	while (fgets(line, BUFSIZ, stdin)) {
3172086Smckusick 		for (cp = line; *cp && *cp == '\t'; )
3182086Smckusick 			cp++;
3192189Smckusic 		CHK('c'); CHK('a'); CHK('l'); CHK('l'); CHK('s');
3202189Smckusic 		CHK('\t'); CHK('$');
3212086Smckusick 		HASH(cp, hp);
3222086Smckusick 		while (*hp) {
3232086Smckusick 			if (RELEQ(size, (*hp)->name, cp)) {
3242086Smckusick 				fputs((*hp)->replace, stdout);
3252086Smckusick 				goto nextline;
3262086Smckusick 			}
3272086Smckusick 			REHASH(hp);
3282086Smckusick 		}
3292086Smckusick copy:
3302086Smckusick 		fputs(line, stdout);
3312086Smckusick nextline:;
3322086Smckusick 	}
3332086Smckusick 	exit(0);
3342086Smckusick }
335