12086Smckusick /* Copyright (c) 1979 Regents of the University of California */
22086Smckusick 
3*4015Smckusic static char sccsid[] = "@(#)langpats.c 1.10 07/15/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*4015Smckusic "	movl	(sp)+,r1\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\
372195Smckusic 	popr	$0x3\n\
382195Smckusic 	movb	r0,*4(r1)\n\
392195Smckusic 	incl	4(r1)\n\
402086Smckusick 2:\n" },
412086Smckusick 
422189Smckusic 	{ "3,_blkcpy\n",
432195Smckusic "	popr	$0xb\n\
442195Smckusic 	pushl	r0\n\
452195Smckusic 	jbr	2f\n\
462086Smckusick 1:\n\
472195Smckusic 	subl2	r0,(sp)\n\
482195Smckusic 	movc3	r0,(r1),(r3)\n\
492195Smckusic 2:\n\
502086Smckusick 	movzwl	$65535,r0\n\
512086Smckusick 	cmpl	(sp),r0\n\
522195Smckusic 	jgtr	1b\n\
53*4015Smckusic 	movl	(sp)+,r0\n\
542086Smckusick 	movc3	r0,(r1),(r3)\n" },
552086Smckusick 
562189Smckusic 	{ "2,_blkclr\n",
572086Smckusick "	movl	4(sp),r3\n\
582189Smckusic 	jbr	2f\n\
592086Smckusick 1:\n\
602189Smckusic 	subl2	r0,(sp)\n\
612189Smckusic 	movc5	$0,(r3),$0,r0,(r3)\n\
622189Smckusic 2:\n\
632086Smckusick 	movzwl	$65535,r0\n\
642086Smckusick 	cmpl	(sp),r0\n\
652189Smckusic 	jgtr	1b\n\
662195Smckusic 	popr	$0x3\n\
672189Smckusic 	movc5	$0,(r3),$0,r0,(r3)\n" },
682086Smckusick 
692189Smckusic 	{ "3,_LOCC\n",
702195Smckusic "	popr	$0x30\n\
71*4015Smckusic 	movl	(sp)+,r1\n\
722086Smckusick 1:\n\
732086Smckusick 	movzwl	$65535,r0\n\
742195Smckusic 	cmpl	r5,r0\n\
752102Smckusic 	jleq	1f\n\
762195Smckusic 	subl2	r0,r5\n\
772195Smckusic 	locc	r4,r0,(r1)\n\
782102Smckusic 	jeql	1b\n\
792195Smckusic 	addl2	r5,r0\n\
802102Smckusic 	jbr	2f\n\
812086Smckusick 1:\n\
822195Smckusic 	locc	r4,r5,(r1)\n\
832086Smckusick 2:\n" },
842086Smckusick 
852962Smckusic 	{ "2,_ROUND\n",
862086Smckusick "	cvtrdl	(sp)+,r0\n" },
872086Smckusick 
882962Smckusic 	{ "2,_TRUNC\n",
892086Smckusick "	cvtdl	(sp)+,r0\n" },
902086Smckusick 
913437Smckusic 	{ "2,_FCALL\n",
923437Smckusic "	movl	4(sp),r0\n\
93*4015Smckusic 	movc3	4(r0),__disply+8,*(sp)+\n\
943437Smckusic 	movl	(sp)+,r0\n\
95*4015Smckusic 	movc3	4(r0),8(r0),__disply+8\n" },
962086Smckusick 
972189Smckusic 	{ "2,_FRTN\n",
983437Smckusic "	movl	(sp)+,r0\n\
99*4015Smckusic 	movc3	4(r0),*(sp)+,__disply+8\n" },
1002086Smckusick 
1012189Smckusic 	{ "3,_FSAV\n",
1022086Smckusick "	movl	8(sp),r0\n\
1032086Smckusick 	movl	(sp)+,(r0)\n\
104*4015Smckusic 	ashl	$3,(sp)+,4(r0)\n\
105*4015Smckusic 	movc3	4(r0),__disply+8,8(r0)\n\
106*4015Smckusic 	movl	(sp)+,r0\n" },
1072086Smckusick 
1082189Smckusic 	{ "3,_RELEQ\n",
109*4015Smckusic "	popr	$0xb\n\
110*4015Smckusic 	movl	r0,r4\n\
1112102Smckusic 1:\n\
1122102Smckusic 	movzwl	$65535,r0\n\
1132102Smckusic 	cmpl	r4,r0\n\
1142195Smckusic 	jleq	3f\n\
1152102Smckusic 	subl2	r0,r4\n\
1162102Smckusic 	cmpc3	r0,(r1),(r3)\n\
1172102Smckusic 	jeql	1b\n\
1182102Smckusic 2:\n\
1192195Smckusic 	clrl	r0\n\
1202195Smckusic 	jbr	4f\n\
1212195Smckusic 3:\n\
1222102Smckusic 	cmpc3	r4,(r1),(r3)\n\
1232195Smckusic 	jneq	2b\n\
1242102Smckusic 	incl	r0\n\
1252102Smckusic 4:\n" },
1262086Smckusick 
1272189Smckusic 	{ "3,_RELNE\n",
128*4015Smckusic "	popr	$0xb\n\
129*4015Smckusic 	movl	r0,r4\n\
1302086Smckusick 1:\n\
1312102Smckusic 	movzwl	$65535,r0\n\
1322102Smckusic 	cmpl	r4,r0\n\
1332195Smckusic 	jleq	3f\n\
1342102Smckusic 	subl2	r0,r4\n\
1352102Smckusic 	cmpc3	r0,(r1),(r3)\n\
1362102Smckusic 	jeql	1b\n\
1372102Smckusic 2:\n\
1382195Smckusic 	movl	$1,r0\n\
1392195Smckusic 	jbr	4f\n\
1402195Smckusic 3:\n\
1412102Smckusic 	cmpc3	r4,(r1),(r3)\n\
1422195Smckusic 	jneq	2b\n\
1432102Smckusic 4:\n" },
1442086Smckusick 
1452189Smckusic 	{ "3,_RELSLT\n",
146*4015Smckusic "	popr	$0xb\n\
147*4015Smckusic 	movl	r0,r4\n\
1482195Smckusic 	jbr	2f\n\
1492102Smckusic 1:\n\
1502102Smckusic 	subl2	r0,r4\n\
1512102Smckusic 	cmpc3	r0,(r1),(r3)\n\
1522195Smckusic 	jneq	3f\n\
1532102Smckusic 2:\n\
1542195Smckusic 	movzwl	$65535,r0\n\
1552195Smckusic 	cmpl	r4,r0\n\
1562195Smckusic 	jgtr	1b\n\
1572102Smckusic 	cmpc3	r4,(r1),(r3)\n\
1582102Smckusic 3:\n\
1592102Smckusic 	jlss	4f\n\
1602086Smckusick 	clrl	r0\n\
1612102Smckusic 	jbr	5f\n\
1622102Smckusic 4:\n\
1632086Smckusick 	movl	$1,r0\n\
1642102Smckusic 5:\n" },
1652086Smckusick 
1662189Smckusic 	{ "3,_RELSLE\n",
167*4015Smckusic "	popr	$0xb\n\
168*4015Smckusic 	movl	r0,r4\n\
1692195Smckusic 	jbr	2f\n\
1702102Smckusic 1:\n\
1712102Smckusic 	subl2	r0,r4\n\
1722102Smckusic 	cmpc3	r0,(r1),(r3)\n\
1732195Smckusic 	jneq	3f\n\
1742102Smckusic 2:\n\
1752195Smckusic 	movzwl	$65535,r0\n\
1762195Smckusic 	cmpl	r4,r0\n\
1772195Smckusic 	jgtr	1b\n\
1782102Smckusic 	cmpc3	r4,(r1),(r3)\n\
1792102Smckusic 3:\n\
1802102Smckusic 	jleq	4f\n\
1812086Smckusick 	clrl	r0\n\
1822102Smckusic 	jbr	5f\n\
1832102Smckusic 4:\n\
1842086Smckusick 	movl	$1,r0\n\
1852102Smckusic 5:\n" },
1862086Smckusick 
1872189Smckusic 	{ "3,_RELSGT\n",
188*4015Smckusic "	popr	$0xb\n\
189*4015Smckusic 	movl	r0,r4\n\
1902195Smckusic 	jbr	2f\n\
1912102Smckusic 1:\n\
1922102Smckusic 	subl2	r0,r4\n\
1932102Smckusic 	cmpc3	r0,(r1),(r3)\n\
1942195Smckusic 	jneq	3f\n\
1952102Smckusic 2:\n\
1962195Smckusic 	movzwl	$65535,r0\n\
1972195Smckusic 	cmpl	r4,r0\n\
1982195Smckusic 	jgtr	1b\n\
1992102Smckusic 	cmpc3	r4,(r1),(r3)\n\
2002102Smckusic 3:\n\
2012102Smckusic 	jgtr	4f\n\
2022086Smckusick 	clrl	r0\n\
2032102Smckusic 	jbr	5f\n\
2042102Smckusic 4:\n\
2052086Smckusick 	movl	$1,r0\n\
2062102Smckusic 5:\n" },
2072086Smckusick 
2082189Smckusic 	{ "3,_RELSGE\n",
209*4015Smckusic "	popr	$0xb\n\
210*4015Smckusic 	movl	r0,r4\n\
2112195Smckusic 	jbr	2f\n\
2122102Smckusic 1:\n\
2132102Smckusic 	subl2	r0,r4\n\
2142102Smckusic 	cmpc3	r0,(r1),(r3)\n\
2152195Smckusic 	jneq	3f\n\
2162102Smckusic 2:\n\
2172195Smckusic 	movzwl	$65535,r0\n\
2182195Smckusic 	cmpl	r4,r0\n\
2192195Smckusic 	jgtr	1b\n\
2202102Smckusic 	cmpc3	r4,(r1),(r3)\n\
2212102Smckusic 3:\n\
2222102Smckusic 	jgeq	4f\n\
2232086Smckusick 	clrl	r0\n\
2242102Smckusic 	jbr	5f\n\
2252102Smckusic 4:\n\
2262086Smckusick 	movl	$1,r0\n\
2272102Smckusic 5:\n" },
2282086Smckusick 
2292189Smckusic 	{ "4,_ADDT\n",
2302195Smckusic "	popr	$0x17\n\
2312086Smckusick 	movl	r0,r3\n\
2322086Smckusick 1:\n\
2332086Smckusick 	bisl3	(r1)+,(r2)+,(r3)+\n\
2342086Smckusick 	sobgtr	r4,1b\n" },
2352086Smckusick 
2362189Smckusic 	{ "4,_SUBT\n",
2372195Smckusic "	popr	$0x17\n\
2382086Smckusick 	movl	r0,r3\n\
2392086Smckusick 1:\n\
2402086Smckusick 	bicl3	(r2)+,(r1)+,(r3)+\n\
2412086Smckusick 	sobgtr	r4,1b\n" },
2422086Smckusick 
2432189Smckusic 	{ "4,_MULT\n",
2442195Smckusic "	popr	$0x17\n\
2452086Smckusick 	movl	r0,r3\n\
2462086Smckusick 1:\n\
2472086Smckusick 	mcoml	(r1)+,r5\n\
2482086Smckusick 	bicl3	r5,(r2)+,(r3)+\n\
2492086Smckusick 	sobgtr	r4,1b\n" },
2502086Smckusick 
2512189Smckusic 	{ "4,_IN\n",
2522230Smckusic "	popr	$0x1e\n\
2532230Smckusic 	clrl	r0\n\
2542195Smckusic 	subl2	r2,r1\n\
2552195Smckusic 	cmpl	r1,r3\n\
2562102Smckusic 	jgtru	1f\n\
2572195Smckusic 	jbc	r1,(r4),1f\n\
2582195Smckusic 	incl	r0\n\
2592195Smckusic 1:\n" }
2602086Smckusick };
2612086Smckusick 
2622086Smckusick struct pats		*htbl[HSHSIZ];
2632086Smckusick 
2642086Smckusick 
2652086Smckusick #define HASH(cp, hp) {\
2662086Smckusick 	hash = 0; rehash = 1; ccp = cp; \
2672086Smckusick 	do	{ \
2682086Smckusick 		hash *= (int)*ccp++; \
2692086Smckusick 	} while (*ccp && *ccp != '\n'); \
2702086Smckusick 	hash >>= 7; hash %= HSHSIZ; hp = &htbl[hash]; size = ccp - cp + 1; \
2712086Smckusick 	}
2722086Smckusick 
2732086Smckusick #define REHASH(hp) {\
2742086Smckusick 	hp += rehash; rehash += 2; \
2752086Smckusick 	if (hp >= &htbl[HSHSIZ]) \
2762086Smckusick 		hp -= HSHSIZ; \
2772086Smckusick 	}
2782086Smckusick 
2792086Smckusick 
2802086Smckusick main(argc, argv)
2812086Smckusick 
2822086Smckusick 	int	argc;
2832086Smckusick 	char	**argv;
2842086Smckusick {
2852086Smckusick 	register struct pats	*pp;
2862086Smckusick 	register struct pats	**hp;
2872341Smckusic 	register char		*cp, *ccp, *lp;
2882086Smckusick 	register int		hash, rehash, size;
2892086Smckusick 	char			line[BUFSIZ];
2902341Smckusic 	extern char		*index();
2912086Smckusick 
2922086Smckusick 	if (argc > 1)
2932086Smckusick 		freopen(argv[1], "r", stdin);
2942086Smckusick 	if (argc > 2)
2952086Smckusick 		freopen(argv[2], "w", stdout);
2962086Smckusick 	/*
2972086Smckusick 	 * set up the hash table
2982086Smckusick 	 */
2992086Smckusick 	for(pp = ptab; pp < &ptab[sizeof ptab/sizeof ptab[0]]; pp++) {
3002086Smckusick 		HASH(pp->name, hp);
3012086Smckusick 		while (*hp)
3022086Smckusick 			REHASH(hp);
3032086Smckusick 		*hp = pp;
3042086Smckusick 	}
3052086Smckusick 	/*
3062086Smckusick 	 * check each line and replace as appropriate
3072086Smckusick 	 */
3082086Smckusick 	while (fgets(line, BUFSIZ, stdin)) {
3092341Smckusic 		lp = index(line, ':');
3102341Smckusic 		for (cp = (lp != NULL) ? ++lp : line; *cp == '\t'; )
3112086Smckusick 			cp++;
3122341Smckusic 		if (strcmpn(cp, "calls\t$", 7) != 0) {
3132341Smckusic 			fputs(line, stdout);
3142341Smckusic 			continue;
3152341Smckusic 		}
3162341Smckusic 		cp += 7;
3172086Smckusick 		HASH(cp, hp);
3182086Smckusick 		while (*hp) {
3192341Smckusic 			if (strcmpn((*hp)->name, cp, size)==NULL) {
3202341Smckusic 				if (lp != NULL) {
3212341Smckusic 					*lp++ = '\n';
3222341Smckusic 					*lp = '\0';
3232341Smckusic 					fputs(line, stdout);
3242341Smckusic 				}
3252086Smckusick 				fputs((*hp)->replace, stdout);
3262086Smckusick 				goto nextline;
3272086Smckusick 			}
3282086Smckusick 			REHASH(hp);
3292086Smckusick 		}
3302086Smckusick 		fputs(line, stdout);
3312086Smckusick nextline:;
3322086Smckusick 	}
3332086Smckusick 	exit(0);
3342086Smckusick }
335