xref: /csrg-svn/usr.bin/pascal/pc2/langpats.c (revision 11877)
12086Smckusick /* Copyright (c) 1979 Regents of the University of California */
22086Smckusick 
3*11877Smckusick static char sccsid[] = "@(#)langpats.c 1.12 04/08/83";
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 
13*11877Smckusick #ifdef vax
14*11877Smckusick #define CALLTEMPLATE	"calls\t$"
15*11877Smckusick #define TEMPLATESIZE	7
16*11877Smckusick #endif vax
17*11877Smckusick 
18*11877Smckusick #ifdef mc68000
19*11877Smckusick #define CALLTEMPLATE	"jbsr\t"
20*11877Smckusick #define TEMPLATESIZE	5
21*11877Smckusick #endif mc68000
22*11877Smckusick 
232086Smckusick struct pats {
242086Smckusick 	char	*name;
252086Smckusick 	char	*replace;
262086Smckusick } ptab[] = {
272086Smckusick 
28*11877Smckusick #ifdef vax
299141Smckusick /*
309141Smckusick  * C library routines
319141Smckusick  */
322189Smckusic 	{ "1,_fgetc\n",
332189Smckusic "	sobgeq	*(sp),1f\n\
342086Smckusick 	calls	$1,__filbuf\n\
352086Smckusick 	jbr     2f\n\
362086Smckusick 1:\n\
372086Smckusick 	addl3	$4,(sp)+,r1\n\
382086Smckusick 	movzbl	*(r1),r0\n\
392086Smckusick 	incl	(r1)\n\
402086Smckusick 2:\n" },
412086Smckusick 
422189Smckusic 	{ "2,_fputc\n",
432189Smckusic "	sobgeq	*4(sp),1f\n\
442086Smckusick 	calls	$2,__flsbuf\n\
452086Smckusick 	jbr	2f\n\
462086Smckusick 1:\n\
472195Smckusic 	popr	$0x3\n\
482195Smckusic 	movb	r0,*4(r1)\n\
492195Smckusic 	incl	4(r1)\n\
502086Smckusick 2:\n" },
512086Smckusick 
529141Smckusick /*
539141Smckusick  * VAX special instructions
549141Smckusick  */
552189Smckusic 	{ "3,_blkcpy\n",
569141Smckusick "	popr	$0xa\n\
572195Smckusic 	jbr	2f\n\
582086Smckusick 1:\n\
592195Smckusic 	subl2	r0,(sp)\n\
602195Smckusic 	movc3	r0,(r1),(r3)\n\
612195Smckusic 2:\n\
622086Smckusick 	movzwl	$65535,r0\n\
632086Smckusick 	cmpl	(sp),r0\n\
642195Smckusic 	jgtr	1b\n\
654015Smckusic 	movl	(sp)+,r0\n\
662086Smckusick 	movc3	r0,(r1),(r3)\n" },
672086Smckusick 
682189Smckusic 	{ "2,_blkclr\n",
699141Smckusick "	movl	(sp)+,r3\n\
702189Smckusic 	jbr	2f\n\
712086Smckusick 1:\n\
722189Smckusic 	subl2	r0,(sp)\n\
732189Smckusic 	movc5	$0,(r3),$0,r0,(r3)\n\
742189Smckusic 2:\n\
752086Smckusick 	movzwl	$65535,r0\n\
762086Smckusick 	cmpl	(sp),r0\n\
772189Smckusic 	jgtr	1b\n\
789141Smckusick 	movl	(sp)+,r0\n\
792189Smckusic 	movc5	$0,(r3),$0,r0,(r3)\n" },
802086Smckusick 
812189Smckusic 	{ "3,_LOCC\n",
822195Smckusic "	popr	$0x30\n\
834015Smckusic 	movl	(sp)+,r1\n\
842086Smckusick 1:\n\
852086Smckusick 	movzwl	$65535,r0\n\
862195Smckusic 	cmpl	r5,r0\n\
872102Smckusic 	jleq	1f\n\
882195Smckusic 	subl2	r0,r5\n\
892195Smckusic 	locc	r4,r0,(r1)\n\
902102Smckusic 	jeql	1b\n\
912195Smckusic 	addl2	r5,r0\n\
922102Smckusic 	jbr	2f\n\
932086Smckusick 1:\n\
942195Smckusic 	locc	r4,r5,(r1)\n\
952086Smckusick 2:\n" },
962086Smckusick 
972962Smckusic 	{ "2,_ROUND\n",
982086Smckusick "	cvtrdl	(sp)+,r0\n" },
992086Smckusick 
1002962Smckusic 	{ "2,_TRUNC\n",
1012086Smckusick "	cvtdl	(sp)+,r0\n" },
1022086Smckusick 
1039141Smckusick /*
1049141Smckusick  * General Pascal library routines
1059141Smckusick  */
1069141Smckusick 	{ "1,_ACTFILE\n",
1079141Smckusick "	movl	(sp)+,r1\n\
1089141Smckusick 	movl	12(r1),r0\n" },
1099141Smckusick 
1103437Smckusic 	{ "2,_FCALL\n",
1113437Smckusic "	movl	4(sp),r0\n\
1124015Smckusic 	movc3	4(r0),__disply+8,*(sp)+\n\
1133437Smckusic 	movl	(sp)+,r0\n\
1144015Smckusic 	movc3	4(r0),8(r0),__disply+8\n" },
1152086Smckusick 
1162189Smckusic 	{ "2,_FRTN\n",
1173437Smckusic "	movl	(sp)+,r0\n\
1184015Smckusic 	movc3	4(r0),*(sp)+,__disply+8\n" },
1192086Smckusick 
1202189Smckusic 	{ "3,_FSAV\n",
1212086Smckusick "	movl	8(sp),r0\n\
1222086Smckusick 	movl	(sp)+,(r0)\n\
1234015Smckusic 	ashl	$3,(sp)+,4(r0)\n\
1244015Smckusic 	movc3	4(r0),__disply+8,8(r0)\n\
1254015Smckusic 	movl	(sp)+,r0\n" },
1262086Smckusick 
1279141Smckusick /*
1289141Smckusick  * Pascal relational comparisons
1299141Smckusick  */
1302189Smckusic 	{ "3,_RELEQ\n",
1314015Smckusic "	popr	$0xb\n\
1324015Smckusic 	movl	r0,r4\n\
1332102Smckusic 1:\n\
1342102Smckusic 	movzwl	$65535,r0\n\
1352102Smckusic 	cmpl	r4,r0\n\
1362195Smckusic 	jleq	3f\n\
1372102Smckusic 	subl2	r0,r4\n\
1382102Smckusic 	cmpc3	r0,(r1),(r3)\n\
1392102Smckusic 	jeql	1b\n\
1402102Smckusic 2:\n\
1412195Smckusic 	clrl	r0\n\
1422195Smckusic 	jbr	4f\n\
1432195Smckusic 3:\n\
1442102Smckusic 	cmpc3	r4,(r1),(r3)\n\
1452195Smckusic 	jneq	2b\n\
1462102Smckusic 	incl	r0\n\
1472102Smckusic 4:\n" },
1482086Smckusick 
1492189Smckusic 	{ "3,_RELNE\n",
1504015Smckusic "	popr	$0xb\n\
1514015Smckusic 	movl	r0,r4\n\
1522086Smckusick 1:\n\
1532102Smckusic 	movzwl	$65535,r0\n\
1542102Smckusic 	cmpl	r4,r0\n\
1552195Smckusic 	jleq	3f\n\
1562102Smckusic 	subl2	r0,r4\n\
1572102Smckusic 	cmpc3	r0,(r1),(r3)\n\
1582102Smckusic 	jeql	1b\n\
1592102Smckusic 2:\n\
1602195Smckusic 	movl	$1,r0\n\
1612195Smckusic 	jbr	4f\n\
1622195Smckusic 3:\n\
1632102Smckusic 	cmpc3	r4,(r1),(r3)\n\
1642195Smckusic 	jneq	2b\n\
1652102Smckusic 4:\n" },
1662086Smckusick 
1672189Smckusic 	{ "3,_RELSLT\n",
1684015Smckusic "	popr	$0xb\n\
1694015Smckusic 	movl	r0,r4\n\
1702195Smckusic 	jbr	2f\n\
1712102Smckusic 1:\n\
1722102Smckusic 	subl2	r0,r4\n\
1732102Smckusic 	cmpc3	r0,(r1),(r3)\n\
1742195Smckusic 	jneq	3f\n\
1752102Smckusic 2:\n\
1762195Smckusic 	movzwl	$65535,r0\n\
1772195Smckusic 	cmpl	r4,r0\n\
1782195Smckusic 	jgtr	1b\n\
1792102Smckusic 	cmpc3	r4,(r1),(r3)\n\
1802102Smckusic 3:\n\
1812102Smckusic 	jlss	4f\n\
1822086Smckusick 	clrl	r0\n\
1832102Smckusic 	jbr	5f\n\
1842102Smckusic 4:\n\
1852086Smckusick 	movl	$1,r0\n\
1862102Smckusic 5:\n" },
1872086Smckusick 
1882189Smckusic 	{ "3,_RELSLE\n",
1894015Smckusic "	popr	$0xb\n\
1904015Smckusic 	movl	r0,r4\n\
1912195Smckusic 	jbr	2f\n\
1922102Smckusic 1:\n\
1932102Smckusic 	subl2	r0,r4\n\
1942102Smckusic 	cmpc3	r0,(r1),(r3)\n\
1952195Smckusic 	jneq	3f\n\
1962102Smckusic 2:\n\
1972195Smckusic 	movzwl	$65535,r0\n\
1982195Smckusic 	cmpl	r4,r0\n\
1992195Smckusic 	jgtr	1b\n\
2002102Smckusic 	cmpc3	r4,(r1),(r3)\n\
2012102Smckusic 3:\n\
2022102Smckusic 	jleq	4f\n\
2032086Smckusick 	clrl	r0\n\
2042102Smckusic 	jbr	5f\n\
2052102Smckusic 4:\n\
2062086Smckusick 	movl	$1,r0\n\
2072102Smckusic 5:\n" },
2082086Smckusick 
2092189Smckusic 	{ "3,_RELSGT\n",
2104015Smckusic "	popr	$0xb\n\
2114015Smckusic 	movl	r0,r4\n\
2122195Smckusic 	jbr	2f\n\
2132102Smckusic 1:\n\
2142102Smckusic 	subl2	r0,r4\n\
2152102Smckusic 	cmpc3	r0,(r1),(r3)\n\
2162195Smckusic 	jneq	3f\n\
2172102Smckusic 2:\n\
2182195Smckusic 	movzwl	$65535,r0\n\
2192195Smckusic 	cmpl	r4,r0\n\
2202195Smckusic 	jgtr	1b\n\
2212102Smckusic 	cmpc3	r4,(r1),(r3)\n\
2222102Smckusic 3:\n\
2232102Smckusic 	jgtr	4f\n\
2242086Smckusick 	clrl	r0\n\
2252102Smckusic 	jbr	5f\n\
2262102Smckusic 4:\n\
2272086Smckusick 	movl	$1,r0\n\
2282102Smckusic 5:\n" },
2292086Smckusick 
2302189Smckusic 	{ "3,_RELSGE\n",
2314015Smckusic "	popr	$0xb\n\
2324015Smckusic 	movl	r0,r4\n\
2332195Smckusic 	jbr	2f\n\
2342102Smckusic 1:\n\
2352102Smckusic 	subl2	r0,r4\n\
2362102Smckusic 	cmpc3	r0,(r1),(r3)\n\
2372195Smckusic 	jneq	3f\n\
2382102Smckusic 2:\n\
2392195Smckusic 	movzwl	$65535,r0\n\
2402195Smckusic 	cmpl	r4,r0\n\
2412195Smckusic 	jgtr	1b\n\
2422102Smckusic 	cmpc3	r4,(r1),(r3)\n\
2432102Smckusic 3:\n\
2442102Smckusic 	jgeq	4f\n\
2452086Smckusick 	clrl	r0\n\
2462102Smckusic 	jbr	5f\n\
2472102Smckusic 4:\n\
2482086Smckusick 	movl	$1,r0\n\
2492102Smckusic 5:\n" },
2502086Smckusick 
2519141Smckusick /*
2529141Smckusick  * Pascal set operations.
2539141Smckusick  */
2542189Smckusic 	{ "4,_ADDT\n",
2552195Smckusic "	popr	$0x17\n\
2562086Smckusick 	movl	r0,r3\n\
2572086Smckusick 1:\n\
2582086Smckusick 	bisl3	(r1)+,(r2)+,(r3)+\n\
2592086Smckusick 	sobgtr	r4,1b\n" },
2602086Smckusick 
2612189Smckusic 	{ "4,_SUBT\n",
2622195Smckusic "	popr	$0x17\n\
2632086Smckusick 	movl	r0,r3\n\
2642086Smckusick 1:\n\
2652086Smckusick 	bicl3	(r2)+,(r1)+,(r3)+\n\
2662086Smckusick 	sobgtr	r4,1b\n" },
2672086Smckusick 
2682189Smckusic 	{ "4,_MULT\n",
2692195Smckusic "	popr	$0x17\n\
2702086Smckusick 	movl	r0,r3\n\
2712086Smckusick 1:\n\
2722086Smckusick 	mcoml	(r1)+,r5\n\
2732086Smckusick 	bicl3	r5,(r2)+,(r3)+\n\
2742086Smckusick 	sobgtr	r4,1b\n" },
2752086Smckusick 
2762189Smckusic 	{ "4,_IN\n",
2772230Smckusic "	popr	$0x1e\n\
2782230Smckusic 	clrl	r0\n\
2792195Smckusic 	subl2	r2,r1\n\
2802195Smckusic 	cmpl	r1,r3\n\
2812102Smckusic 	jgtru	1f\n\
2822195Smckusic 	jbc	r1,(r4),1f\n\
2832195Smckusic 	incl	r0\n\
2849141Smckusick 1:\n" },
2859141Smckusick 
2869141Smckusick /*
2879141Smckusick  * Pascal runtime checks
2889141Smckusick  */
2899141Smckusick 	{ "1,_ASRT\n",
2909141Smckusick "	tstl	(sp)+\n\
2919141Smckusick 	jneq	1f\n\
2929141Smckusick 	pushl	$0\n\
2939141Smckusick 	pushl	$_EASRT\n\
2949141Smckusick 	calls	$2,_ERROR\n\
2959141Smckusick 1:\n" },
2969141Smckusick 
2979141Smckusick 	{ "2,_ASRTS\n",
2989141Smckusick "	popr	$0x03\n\
2999141Smckusick 	tstl	r0\n\
3009141Smckusick 	jneq	1f\n\
3019141Smckusick 	pushl	r1\n\
3029141Smckusick 	pushl	$_EASRTS\n\
3039141Smckusick 	calls	$2,_ERROR\n\
3049141Smckusick 1:\n" },
3059141Smckusick 
3069141Smckusick 	{ "1,_CHR\n",
3079141Smckusick "	movl	(sp)+,r0\n\
3089141Smckusick 	cmpl	r0,$127\n\
3099141Smckusick 	jlequ	1f\n\
3109141Smckusick 	pushl	r0\n\
3119141Smckusick 	pushl	$_ECHR\n\
3129141Smckusick 	calls	$2,_ERROR\n\
3139141Smckusick 1:\n" },
3149141Smckusick 
3159141Smckusick 	{ "0,_LINO\n",
3169141Smckusick "	incl	__stcnt\n\
3179141Smckusick 	cmpl	__stcnt,__stlim\n\
3189141Smckusick 	jlss	1f\n\
3199141Smckusick 	pushl	__stcnt\n\
3209141Smckusick 	pushl	$_ELINO\n\
3219141Smckusick 	calls	$2,_ERROR\n\
3229141Smckusick 1:\n" },
3239141Smckusick 
3249141Smckusick 	{ "1,_NIL\n",
3259141Smckusick "	movl	(sp)+,r0\n\
3269141Smckusick 	cmpl	r0,__maxptr\n\
3279141Smckusick 	jgtr	1f\n\
3289141Smckusick 	cmpl	r0,__minptr\n\
3299141Smckusick 	jgeq	2f\n\
3309141Smckusick 1:\n\
3319141Smckusick 	pushl	$0\n\
3329141Smckusick 	pushl	$_ENIL\n\
3339141Smckusick 	calls	$2,_ERROR\n\
3349141Smckusick 2:\n" },
3359141Smckusick 
3369141Smckusick 	{ "2,_RANDOM\n",
3379141Smckusick "	addl2	$8,sp\n\
3389141Smckusick 	emul	__seed,$1103515245,$0,r0\n\
3399141Smckusick 	ediv	$0x7fffffff,r0,r1,r0\n\
3409141Smckusick 	movl	r0,__seed\n\
3419141Smckusick 	cvtld	r0,r0\n\
3429141Smckusick 	divd2	$0d2.147483647e+09,r0\n" },
3439141Smckusick 
3449141Smckusick 	{ "3,_RANG4\n",
3459141Smckusick "	popr	$0x07\n\
3469141Smckusick 	cmpl	r0,r1\n\
3479141Smckusick 	jlss	1f\n\
3489141Smckusick 	cmpl	r0,r2\n\
3499141Smckusick 	jleq	2f\n\
3509141Smckusick 1:\n\
3519141Smckusick 	pushl	r0\n\
3529141Smckusick 	pushl	$_ERANG\n\
3539141Smckusick 	calls	$2,_ERROR\n\
3549141Smckusick 2:\n" },
3559141Smckusick 
3569141Smckusick 	{ "2,_RSNG4\n",
3579141Smckusick "	popr	$0x03\n\
3589141Smckusick 	cmpl	r0,r1\n\
3599141Smckusick 	jlequ	1f\n\
3609141Smckusick 	pushl	r0\n\
3619141Smckusick 	pushl	$_ERANG\n\
3629141Smckusick 	calls	$2,_ERROR\n\
3639141Smckusick 1:\n" },
3649141Smckusick 
3659141Smckusick 	{ "1,_SEED\n",
3669141Smckusick "	movl	__seed,r0\n\
3679141Smckusick 	movl	(sp)+,__seed\n" },
3689141Smckusick 
3699141Smckusick 	{ "3,_SUBSC\n",
3709141Smckusick "	popr	$0x07\n\
3719141Smckusick 	cmpl	r0,r1\n\
3729141Smckusick 	jlss	1f\n\
3739141Smckusick 	cmpl	r0,r2\n\
3749141Smckusick 	jleq	2f\n\
3759141Smckusick 1:\n\
3769141Smckusick 	pushl	r0\n\
3779141Smckusick 	pushl	$_ESUBSC\n\
3789141Smckusick 	calls	$2,_ERROR\n\
3799141Smckusick 2:\n" },
3809141Smckusick 
3819141Smckusick 	{ "2,_SUBSCZ\n",
3829141Smckusick "	popr	$0x03\n\
3839141Smckusick 	cmpl	r0,r1\n\
3849141Smckusick 	jlequ	1f\n\
3859141Smckusick 	pushl	r0\n\
3869141Smckusick 	pushl	$_ESUBSC\n\
3879141Smckusick 	calls	$2,_ERROR\n\
3889141Smckusick 1:\n" },
389*11877Smckusick #endif vax
3909141Smckusick 
391*11877Smckusick #ifdef mc68000
392*11877Smckusick 	{ "NONE", "IMPLEMENTED" },
393*11877Smckusick #endif mc68000
394*11877Smckusick 
3952086Smckusick };
3962086Smckusick 
3972086Smckusick struct pats		*htbl[HSHSIZ];
3982086Smckusick 
3992086Smckusick 
4002086Smckusick #define HASH(cp, hp) {\
4012086Smckusick 	hash = 0; rehash = 1; ccp = cp; \
4022086Smckusick 	do	{ \
4032086Smckusick 		hash *= (int)*ccp++; \
4042086Smckusick 	} while (*ccp && *ccp != '\n'); \
4052086Smckusick 	hash >>= 7; hash %= HSHSIZ; hp = &htbl[hash]; size = ccp - cp + 1; \
4062086Smckusick 	}
4072086Smckusick 
4082086Smckusick #define REHASH(hp) {\
4092086Smckusick 	hp += rehash; rehash += 2; \
4102086Smckusick 	if (hp >= &htbl[HSHSIZ]) \
4112086Smckusick 		hp -= HSHSIZ; \
4122086Smckusick 	}
4132086Smckusick 
4142086Smckusick 
4152086Smckusick main(argc, argv)
4162086Smckusick 
4172086Smckusick 	int	argc;
4182086Smckusick 	char	**argv;
4192086Smckusick {
4202086Smckusick 	register struct pats	*pp;
4212086Smckusick 	register struct pats	**hp;
4222341Smckusic 	register char		*cp, *ccp, *lp;
4232086Smckusick 	register int		hash, rehash, size;
4242086Smckusick 	char			line[BUFSIZ];
4252341Smckusic 	extern char		*index();
4262086Smckusick 
4272086Smckusick 	if (argc > 1)
4282086Smckusick 		freopen(argv[1], "r", stdin);
4292086Smckusick 	if (argc > 2)
4302086Smckusick 		freopen(argv[2], "w", stdout);
4312086Smckusick 	/*
4322086Smckusick 	 * set up the hash table
4332086Smckusick 	 */
4342086Smckusick 	for(pp = ptab; pp < &ptab[sizeof ptab/sizeof ptab[0]]; pp++) {
4352086Smckusick 		HASH(pp->name, hp);
4362086Smckusick 		while (*hp)
4372086Smckusick 			REHASH(hp);
4382086Smckusick 		*hp = pp;
4392086Smckusick 	}
4402086Smckusick 	/*
4412086Smckusick 	 * check each line and replace as appropriate
4422086Smckusick 	 */
4432086Smckusick 	while (fgets(line, BUFSIZ, stdin)) {
4442341Smckusic 		lp = index(line, ':');
4452341Smckusic 		for (cp = (lp != NULL) ? ++lp : line; *cp == '\t'; )
4462086Smckusick 			cp++;
447*11877Smckusick 		if (strcmpn(cp, CALLTEMPLATE, TEMPLATESIZE) != 0) {
4482341Smckusic 			fputs(line, stdout);
4492341Smckusic 			continue;
4502341Smckusic 		}
4512341Smckusic 		cp += 7;
4522086Smckusick 		HASH(cp, hp);
4532086Smckusick 		while (*hp) {
4542341Smckusic 			if (strcmpn((*hp)->name, cp, size)==NULL) {
4552341Smckusic 				if (lp != NULL) {
4562341Smckusic 					*lp++ = '\n';
4572341Smckusic 					*lp = '\0';
4582341Smckusic 					fputs(line, stdout);
4592341Smckusic 				}
4602086Smckusick 				fputs((*hp)->replace, stdout);
4612086Smckusick 				goto nextline;
4622086Smckusick 			}
4632086Smckusick 			REHASH(hp);
4642086Smckusick 		}
4652086Smckusick 		fputs(line, stdout);
4662086Smckusick nextline:;
4672086Smckusick 	}
4682086Smckusick 	exit(0);
4692086Smckusick }
470