12086Smckusick /* Copyright (c) 1979 Regents of the University of California */
22086Smckusick 
3*2962Smckusic static char sccsid[] = "@(#)langpats.c 1.8 03/07/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",
192195Smckusic "	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\
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\
532195Smckusic 	popr	$0x1\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\
712195Smckusic 	popr	$0x2\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 
85*2962Smckusic 	{ "2,_ROUND\n",
862086Smckusick "	cvtrdl	(sp)+,r0\n" },
872086Smckusick 
88*2962Smckusic 	{ "2,_TRUNC\n",
892086Smckusick "	cvtdl	(sp)+,r0\n" },
902086Smckusick 
912189Smckusic 	{ "1,_FCALL\n",
922086Smckusick "	movl	(sp),r0\n\
932086Smckusick 	ashl	$3,4(r0),r1\n\
942086Smckusick 	movc3	r1,__disply+8,8(r0)[r1]\n\
952086Smckusick 	movl	(sp),r0\n\
962086Smckusick 	ashl	$3,4(r0),r1\n\
972086Smckusick 	movc3	r1,8(r0),__disply+8\n\
982086Smckusick 	movl	*(sp)+,r0\n" },
992086Smckusick 
1002189Smckusic 	{ "2,_FRTN\n",
1012195Smckusic "	popr	$0x1\n\
1022086Smckusick 	ashl	$3,4(r0),r1\n\
1032086Smckusick 	movc3	r1,8(r0)[r1],__disply+8\n\
1042195Smckusic 	popr	$0x1\n" },
1052086Smckusick 
1062189Smckusic 	{ "3,_FSAV\n",
1072086Smckusick "	movl	8(sp),r0\n\
1082086Smckusick 	movl	(sp)+,(r0)\n\
1092086Smckusick 	movl	(sp)+,4(r0)\n\
1102086Smckusick 	ashl	$3,4(r0),r1\n\
1112086Smckusick 	movc3	r1,__disply+8,8(r0)\n\
1122195Smckusic 	popr	$0x1\n" },
1132086Smckusick 
1142189Smckusic 	{ "3,_RELEQ\n",
1152195Smckusic "	popr	$0x10\n\
1162195Smckusic 	popr	$0xa\n\
1172102Smckusic 1:\n\
1182102Smckusic 	movzwl	$65535,r0\n\
1192102Smckusic 	cmpl	r4,r0\n\
1202195Smckusic 	jleq	3f\n\
1212102Smckusic 	subl2	r0,r4\n\
1222102Smckusic 	cmpc3	r0,(r1),(r3)\n\
1232102Smckusic 	jeql	1b\n\
1242102Smckusic 2:\n\
1252195Smckusic 	clrl	r0\n\
1262195Smckusic 	jbr	4f\n\
1272195Smckusic 3:\n\
1282102Smckusic 	cmpc3	r4,(r1),(r3)\n\
1292195Smckusic 	jneq	2b\n\
1302102Smckusic 	incl	r0\n\
1312102Smckusic 4:\n" },
1322086Smckusick 
1332189Smckusic 	{ "3,_RELNE\n",
1342195Smckusic "	popr	$0x10\n\
1352195Smckusic 	popr	$0xa\n\
1362086Smckusick 1:\n\
1372102Smckusic 	movzwl	$65535,r0\n\
1382102Smckusic 	cmpl	r4,r0\n\
1392195Smckusic 	jleq	3f\n\
1402102Smckusic 	subl2	r0,r4\n\
1412102Smckusic 	cmpc3	r0,(r1),(r3)\n\
1422102Smckusic 	jeql	1b\n\
1432102Smckusic 2:\n\
1442195Smckusic 	movl	$1,r0\n\
1452195Smckusic 	jbr	4f\n\
1462195Smckusic 3:\n\
1472102Smckusic 	cmpc3	r4,(r1),(r3)\n\
1482195Smckusic 	jneq	2b\n\
1492102Smckusic 4:\n" },
1502086Smckusick 
1512189Smckusic 	{ "3,_RELSLT\n",
1522195Smckusic "	popr	$0x10\n\
1532195Smckusic 	popr	$0xa\n\
1542195Smckusic 	jbr	2f\n\
1552102Smckusic 1:\n\
1562102Smckusic 	subl2	r0,r4\n\
1572102Smckusic 	cmpc3	r0,(r1),(r3)\n\
1582195Smckusic 	jneq	3f\n\
1592102Smckusic 2:\n\
1602195Smckusic 	movzwl	$65535,r0\n\
1612195Smckusic 	cmpl	r4,r0\n\
1622195Smckusic 	jgtr	1b\n\
1632102Smckusic 	cmpc3	r4,(r1),(r3)\n\
1642102Smckusic 3:\n\
1652102Smckusic 	jlss	4f\n\
1662086Smckusick 	clrl	r0\n\
1672102Smckusic 	jbr	5f\n\
1682102Smckusic 4:\n\
1692086Smckusick 	movl	$1,r0\n\
1702102Smckusic 5:\n" },
1712086Smckusick 
1722189Smckusic 	{ "3,_RELSLE\n",
1732195Smckusic "	popr	$0x10\n\
1742195Smckusic 	popr	$0xa\n\
1752195Smckusic 	jbr	2f\n\
1762102Smckusic 1:\n\
1772102Smckusic 	subl2	r0,r4\n\
1782102Smckusic 	cmpc3	r0,(r1),(r3)\n\
1792195Smckusic 	jneq	3f\n\
1802102Smckusic 2:\n\
1812195Smckusic 	movzwl	$65535,r0\n\
1822195Smckusic 	cmpl	r4,r0\n\
1832195Smckusic 	jgtr	1b\n\
1842102Smckusic 	cmpc3	r4,(r1),(r3)\n\
1852102Smckusic 3:\n\
1862102Smckusic 	jleq	4f\n\
1872086Smckusick 	clrl	r0\n\
1882102Smckusic 	jbr	5f\n\
1892102Smckusic 4:\n\
1902086Smckusick 	movl	$1,r0\n\
1912102Smckusic 5:\n" },
1922086Smckusick 
1932189Smckusic 	{ "3,_RELSGT\n",
1942195Smckusic "	popr	$0x10\n\
1952195Smckusic 	popr	$0xa\n\
1962195Smckusic 	jbr	2f\n\
1972102Smckusic 1:\n\
1982102Smckusic 	subl2	r0,r4\n\
1992102Smckusic 	cmpc3	r0,(r1),(r3)\n\
2002195Smckusic 	jneq	3f\n\
2012102Smckusic 2:\n\
2022195Smckusic 	movzwl	$65535,r0\n\
2032195Smckusic 	cmpl	r4,r0\n\
2042195Smckusic 	jgtr	1b\n\
2052102Smckusic 	cmpc3	r4,(r1),(r3)\n\
2062102Smckusic 3:\n\
2072102Smckusic 	jgtr	4f\n\
2082086Smckusick 	clrl	r0\n\
2092102Smckusic 	jbr	5f\n\
2102102Smckusic 4:\n\
2112086Smckusick 	movl	$1,r0\n\
2122102Smckusic 5:\n" },
2132086Smckusick 
2142189Smckusic 	{ "3,_RELSGE\n",
2152195Smckusic "	popr	$0x10\n\
2162195Smckusic 	popr	$0xa\n\
2172195Smckusic 	jbr	2f\n\
2182102Smckusic 1:\n\
2192102Smckusic 	subl2	r0,r4\n\
2202102Smckusic 	cmpc3	r0,(r1),(r3)\n\
2212195Smckusic 	jneq	3f\n\
2222102Smckusic 2:\n\
2232195Smckusic 	movzwl	$65535,r0\n\
2242195Smckusic 	cmpl	r4,r0\n\
2252195Smckusic 	jgtr	1b\n\
2262102Smckusic 	cmpc3	r4,(r1),(r3)\n\
2272102Smckusic 3:\n\
2282102Smckusic 	jgeq	4f\n\
2292086Smckusick 	clrl	r0\n\
2302102Smckusic 	jbr	5f\n\
2312102Smckusic 4:\n\
2322086Smckusick 	movl	$1,r0\n\
2332102Smckusic 5:\n" },
2342086Smckusick 
2352189Smckusic 	{ "4,_ADDT\n",
2362195Smckusic "	popr	$0x17\n\
2372086Smckusick 	movl	r0,r3\n\
2382086Smckusick 1:\n\
2392086Smckusick 	bisl3	(r1)+,(r2)+,(r3)+\n\
2402086Smckusick 	sobgtr	r4,1b\n" },
2412086Smckusick 
2422189Smckusic 	{ "4,_SUBT\n",
2432195Smckusic "	popr	$0x17\n\
2442086Smckusick 	movl	r0,r3\n\
2452086Smckusick 1:\n\
2462086Smckusick 	bicl3	(r2)+,(r1)+,(r3)+\n\
2472086Smckusick 	sobgtr	r4,1b\n" },
2482086Smckusick 
2492189Smckusic 	{ "4,_MULT\n",
2502195Smckusic "	popr	$0x17\n\
2512086Smckusick 	movl	r0,r3\n\
2522086Smckusick 1:\n\
2532086Smckusick 	mcoml	(r1)+,r5\n\
2542086Smckusick 	bicl3	r5,(r2)+,(r3)+\n\
2552086Smckusick 	sobgtr	r4,1b\n" },
2562086Smckusick 
2572189Smckusic 	{ "4,_IN\n",
2582230Smckusic "	popr	$0x1e\n\
2592230Smckusic 	clrl	r0\n\
2602195Smckusic 	subl2	r2,r1\n\
2612195Smckusic 	cmpl	r1,r3\n\
2622102Smckusic 	jgtru	1f\n\
2632195Smckusic 	jbc	r1,(r4),1f\n\
2642195Smckusic 	incl	r0\n\
2652195Smckusic 1:\n" }
2662086Smckusick };
2672086Smckusick 
2682086Smckusick struct pats		*htbl[HSHSIZ];
2692086Smckusick 
2702086Smckusick 
2712086Smckusick #define HASH(cp, hp) {\
2722086Smckusick 	hash = 0; rehash = 1; ccp = cp; \
2732086Smckusick 	do	{ \
2742086Smckusick 		hash *= (int)*ccp++; \
2752086Smckusick 	} while (*ccp && *ccp != '\n'); \
2762086Smckusick 	hash >>= 7; hash %= HSHSIZ; hp = &htbl[hash]; size = ccp - cp + 1; \
2772086Smckusick 	}
2782086Smckusick 
2792086Smckusick #define REHASH(hp) {\
2802086Smckusick 	hp += rehash; rehash += 2; \
2812086Smckusick 	if (hp >= &htbl[HSHSIZ]) \
2822086Smckusick 		hp -= HSHSIZ; \
2832086Smckusick 	}
2842086Smckusick 
2852086Smckusick 
2862086Smckusick main(argc, argv)
2872086Smckusick 
2882086Smckusick 	int	argc;
2892086Smckusick 	char	**argv;
2902086Smckusick {
2912086Smckusick 	register struct pats	*pp;
2922086Smckusick 	register struct pats	**hp;
2932341Smckusic 	register char		*cp, *ccp, *lp;
2942086Smckusick 	register int		hash, rehash, size;
2952086Smckusick 	char			line[BUFSIZ];
2962341Smckusic 	extern char		*index();
2972086Smckusick 
2982086Smckusick 	if (argc > 1)
2992086Smckusick 		freopen(argv[1], "r", stdin);
3002086Smckusick 	if (argc > 2)
3012086Smckusick 		freopen(argv[2], "w", stdout);
3022086Smckusick 	/*
3032086Smckusick 	 * set up the hash table
3042086Smckusick 	 */
3052086Smckusick 	for(pp = ptab; pp < &ptab[sizeof ptab/sizeof ptab[0]]; pp++) {
3062086Smckusick 		HASH(pp->name, hp);
3072086Smckusick 		while (*hp)
3082086Smckusick 			REHASH(hp);
3092086Smckusick 		*hp = pp;
3102086Smckusick 	}
3112086Smckusick 	/*
3122086Smckusick 	 * check each line and replace as appropriate
3132086Smckusick 	 */
3142086Smckusick 	while (fgets(line, BUFSIZ, stdin)) {
3152341Smckusic 		lp = index(line, ':');
3162341Smckusic 		for (cp = (lp != NULL) ? ++lp : line; *cp == '\t'; )
3172086Smckusick 			cp++;
3182341Smckusic 		if (strcmpn(cp, "calls\t$", 7) != 0) {
3192341Smckusic 			fputs(line, stdout);
3202341Smckusic 			continue;
3212341Smckusic 		}
3222341Smckusic 		cp += 7;
3232086Smckusick 		HASH(cp, hp);
3242086Smckusick 		while (*hp) {
3252341Smckusic 			if (strcmpn((*hp)->name, cp, size)==NULL) {
3262341Smckusic 				if (lp != NULL) {
3272341Smckusic 					*lp++ = '\n';
3282341Smckusic 					*lp = '\0';
3292341Smckusic 					fputs(line, stdout);
3302341Smckusic 				}
3312086Smckusick 				fputs((*hp)->replace, stdout);
3322086Smckusick 				goto nextline;
3332086Smckusick 			}
3342086Smckusick 			REHASH(hp);
3352086Smckusick 		}
3362086Smckusick 		fputs(line, stdout);
3372086Smckusick nextline:;
3382086Smckusick 	}
3392086Smckusick 	exit(0);
3402086Smckusick }
341