xref: /csrg-svn/sys/vax/inline/machdep.c (revision 49438)
1*49438Sbostic /*-
2*49438Sbostic  * Copyright (c) 1984, 1986 The Regents of the University of California.
3*49438Sbostic  * All rights reserved.
4*49438Sbostic  *
5*49438Sbostic  * %sccs.include.redist.c%
623346Smckusick  */
716960Smckusick 
816960Smckusick #ifndef lint
9*49438Sbostic static char sccsid[] = "@(#)machdep.c	7.2 (Berkeley) 05/08/91";
10*49438Sbostic #endif /* not lint */
1116960Smckusick 
1216960Smckusick #include <stdio.h>
1316960Smckusick #include <ctype.h>
1416979Smckusick #include "inline.h"
1516960Smckusick 
1624396Smckusick extern char *strcpy();
1724396Smckusick extern char *strcat();
1824396Smckusick extern char *index();
1924396Smckusick 
2016960Smckusick /*
2116979Smckusick  * The routines and tables in this file must be rewritten
2216979Smckusick  * for each new machine that this program is ported to.
2316960Smckusick  */
2416960Smckusick 
2517203Smckusick #ifdef vax
2616960Smckusick /*
2716979Smckusick  * Instruction stop table.
2816979Smckusick  * All instructions that implicitly modify any of the temporary
2916979Smckusick  * registers, change control flow, or implicitly loop must be
3016979Smckusick  * listed in this table. It is used to find the end of a basic
3116979Smckusick  * block when scanning backwards through the instruction stream
3216979Smckusick  * trying to merge the inline expansion.
3316979Smckusick  */
3416979Smckusick struct inststoptbl inststoptable[] = {
3516979Smckusick 	{ "jbc" }, { "jlbc" }, { "jbs" }, { "jlbs" }, { "jbcc" },
3616979Smckusick 	{ "jbsc" }, { "jbcs" }, { "jbss" }, { "jbr" }, { "jcc" },
3716979Smckusick 	{ "jcs" }, { "jvc" }, { "jvs" }, { "jlss" }, { "jlssu" },
3816979Smckusick 	{ "jleq" }, { "jlequ" }, { "jeql" }, { "jeqlu" }, { "jneq" },
3916979Smckusick 	{ "jnequ" }, { "jgeq" }, { "jgequ" }, { "jgtr" }, { "jgtru" },
4016979Smckusick 	{ "chmk" }, { "chme" }, { "chms" }, { "chmu" }, { "rei" },
4116979Smckusick 	{ "ldpctx" }, { "svpctx" }, { "xfc" }, { "bpt" },
4216979Smckusick 	{ "bugw" }, { "bugl" }, { "halt" }, { "pushr" }, { "popr" },
4316979Smckusick 	{ "polyf" }, { "polyd" }, { "polyg" }, { "polyh" },
4416979Smckusick 	{ "bneq" }, { "bnequ" }, { "beql" }, { "beqlu" }, { "bgtr" },
4516979Smckusick 	{ "bleq" }, { "bgeq" }, { "blss" }, { "bgtru" }, { "blequ" },
4616979Smckusick 	{ "bvc" }, { "bvs" }, { "bgequ" }, { "bcc" }, { "blssu" },
4716979Smckusick 	{ "bcs" }, { "brb" }, { "brw" }, { "jmp" },
4816979Smckusick 	{ "bbs" }, { "bbc" }, { "bbss" }, { "bbcs" }, { "bbsc" },
4916979Smckusick 	{ "bbcc" }, { "bbssi" }, { "bbcci" }, { "blbs" }, { "blbc" },
5016979Smckusick 	{ "acbb" }, { "acbw" }, { "acbl" }, { "acbf" }, { "acbd" },
5116979Smckusick 	{ "acbg" }, { "acbh" }, { "aoblss" }, { "aobleq" },
5216979Smckusick 	{ "sobgeq" }, { "sobgtr" }, { "caseb" }, { "casew" }, { "casel" },
5316979Smckusick 	{ "bsbb" }, { "bsbw" }, { "jsb" }, { "rsb" },
5416979Smckusick 	{ "callg" }, { "calls" }, { "ret" },
5516979Smckusick 	{ "movc3" }, { "movc5" }, { "movtc" }, { "movtuc" },
5616979Smckusick 	{ "cmpc3" }, { "cmpc5" }, { "scanc" }, { "spanc" },
5716979Smckusick 	{ "locc" }, { "skpc" }, { "matchc" }, { "crc" },
5816979Smckusick 	{ "movp" }, { "cmpp3" }, { "cmpp4" }, { "addp4" }, { "addp6" },
5916979Smckusick 	{ "subp4" }, { "subp6" }, { "mulp" }, { "divp" }, { "cvtlp" },
6016979Smckusick 	{ "cvtpl" }, { "cvtpt" }, { "cvttp" }, { "cvtps" }, { "cvtsp" },
6116979Smckusick 	{ "ashp" }, { "editpc" },
6216979Smckusick 	{ "escd" }, { "esce" }, { "escf" },
6316979Smckusick 	{ "" }
6416979Smckusick };
6516979Smckusick 
6616979Smckusick /*
6716960Smckusick  * Check to see if a line is a candidate for replacement.
6816960Smckusick  * Return pointer to name to be looked up in pattern table.
6916960Smckusick  */
7016960Smckusick char *
doreplaceon(cp)7116960Smckusick doreplaceon(cp)
7216960Smckusick 	char *cp;
7316960Smckusick {
7416960Smckusick 
7524396Smckusick 	if (bcmp(cp, "calls\t", 6) != 0)
7624396Smckusick 		return (0);
7724396Smckusick 	if ((cp = index(cp + 6, ',')) == 0)
7824396Smckusick 		return (0);
7924396Smckusick 	return (++cp);
8016960Smckusick }
8116960Smckusick 
8216960Smckusick /*
8324396Smckusick  * Find out how many arguments the function is being called with.
8424396Smckusick  * A return value of -1 indicates that the count can't be determined.
8524396Smckusick  */
8624396Smckusick int
countargs(cp)8724396Smckusick countargs(cp)
8824396Smckusick 	char *cp;
8924396Smckusick {
9024396Smckusick 
9124396Smckusick 	if ((cp = index(cp, '$')) == 0)
9224396Smckusick 		return (-1);
9324396Smckusick 	if (!isdigit(*++cp))
9424396Smckusick 		return (-1);
9524396Smckusick 	return (atoi(cp));
9624396Smckusick }
9724396Smckusick 
9824396Smckusick /*
9916960Smckusick  * Find the next argument to the function being expanded.
10016960Smckusick  */
nextarg(argc,argv)10116960Smckusick nextarg(argc, argv)
10216960Smckusick 	int argc;
10316960Smckusick 	char *argv[];
10416960Smckusick {
10516960Smckusick 	register char *lastarg = argv[2];
10616960Smckusick 
10716960Smckusick 	if (argc == 3 &&
10816960Smckusick 	    bcmp(argv[0], "mov", 3) == 0 &&
10916960Smckusick 	    bcmp(argv[1], "(sp)+", 6) == 0 &&
11016960Smckusick 	    lastarg[0] == 'r' && isdigit(lastarg[1]) && lastarg[2] == '\0')
11116960Smckusick 		return (lastarg[1] - '0');
11216960Smckusick 	return (-1);
11316960Smckusick }
11416960Smckusick 
11516960Smckusick /*
11616960Smckusick  * Determine whether the current line pushes an argument.
11716960Smckusick  */
ispusharg(argc,argv)11824396Smckusick ispusharg(argc, argv)
11916960Smckusick 	int argc;
12016960Smckusick 	char *argv[];
12116960Smckusick {
12216960Smckusick 
12316960Smckusick 	if (argc < 2)
12416960Smckusick 		return (0);
12516960Smckusick 	if (argc == 2 && bcmp(argv[0], "push", 4) == 0)
12616960Smckusick 		return (1);
12716960Smckusick 	if (bcmp(argv[argc - 1], "-(sp)", 6) == 0)
12816960Smckusick 		return (1);
12916960Smckusick 	return (0);
13016960Smckusick }
13116960Smckusick 
13216960Smckusick /*
13316960Smckusick  * Determine which (if any) registers are modified
13416960Smckusick  * Return register number that is modified, -1 if none are modified.
13516960Smckusick  */
modifies(argc,argv)13616960Smckusick modifies(argc, argv)
13716960Smckusick 	int argc;
13816960Smckusick 	char *argv[];
13916960Smckusick {
14016960Smckusick 	/*
14116960Smckusick 	 * For the VAX all we care about are r0 to r5
14216960Smckusick 	 */
14316960Smckusick 	register char *lastarg = argv[argc - 1];
14416960Smckusick 
14516960Smckusick 	if (lastarg[0] == 'r' && isdigit(lastarg[1]) && lastarg[2] == '\0')
14616960Smckusick 		return (lastarg[1] - '0');
14716960Smckusick 	return (-1);
14816960Smckusick }
14916960Smckusick 
15016960Smckusick /*
15116960Smckusick  * Rewrite the instruction in (argc, argv) to store its
15216960Smckusick  * contents into arg instead of onto the stack. The new
15316960Smckusick  * instruction is placed in the buffer that is provided.
15416960Smckusick  */
rewrite(instbuf,argc,argv,target)15516960Smckusick rewrite(instbuf, argc, argv, target)
15616960Smckusick 	char *instbuf;
15716960Smckusick 	int argc;
15816960Smckusick 	char *argv[];
15916960Smckusick 	int target;
16016960Smckusick {
16116960Smckusick 
16216960Smckusick 	switch (argc) {
16316960Smckusick 	case 0:
16416960Smckusick 		instbuf[0] = '\0';
16521192Smckusick 		fprintf(stderr, "blank line to rewrite?\n");
16616960Smckusick 		return;
16716960Smckusick 	case 1:
16816960Smckusick 		sprintf(instbuf, "\t%s\n", argv[0]);
16916960Smckusick 		fprintf(stderr, "rewrite?-> %s", instbuf);
17016960Smckusick 		return;
17116960Smckusick 	case 2:
17216960Smckusick 		if (bcmp(argv[0], "push", 4) == 0) {
17316960Smckusick 			sprintf(instbuf, "\tmov%s\t%s,r%d\n",
17416960Smckusick 				&argv[0][4], argv[1], target);
17516960Smckusick 			return;
17616960Smckusick 		}
17716960Smckusick 		sprintf(instbuf, "\t%s\tr%d\n", argv[0], target);
17816960Smckusick 		return;
17916960Smckusick 	case 3:
18016960Smckusick 		sprintf(instbuf, "\t%s\t%s,r%d\n", argv[0], argv[1], target);
18116960Smckusick 		return;
18216960Smckusick 	case 4:
18316960Smckusick 		sprintf(instbuf, "\t%s\t%s,%s,r%d\n",
18416960Smckusick 			argv[0], argv[1], argv[2], target);
18516960Smckusick 		return;
18616971Smckusick 	case 5:
18716971Smckusick 		sprintf(instbuf, "\t%s\t%s,%s,%s,r%d\n",
18816971Smckusick 			argv[0], argv[1], argv[2], argv[3], target);
18916971Smckusick 		return;
19016960Smckusick 	default:
19116960Smckusick 		sprintf(instbuf, "\t%s\t%s", argv[0], argv[1]);
19216960Smckusick 		argc -= 2, argv += 2;
19316960Smckusick 		while (argc-- > 0) {
19424396Smckusick 			(void) strcat(instbuf, ",");
19524396Smckusick 			(void) strcat(instbuf, *argv++);
19616960Smckusick 		}
19724396Smckusick 		(void) strcat(instbuf, "\n");
19816960Smckusick 		fprintf(stderr, "rewrite?-> %s", instbuf);
19916960Smckusick 		return;
20016960Smckusick 	}
20116960Smckusick }
20217203Smckusick 
20317203Smckusick /*
20417203Smckusick  * Do any necessary post expansion cleanup.
20517203Smckusick  */
20624396Smckusick /*ARGSUSED*/
cleanup(numargs)20717203Smckusick cleanup(numargs)
20817203Smckusick 	int numargs;
20917203Smckusick {
21017203Smckusick 
21117203Smckusick 	return;
21217203Smckusick }
21317203Smckusick #endif vax
21417203Smckusick 
21517203Smckusick #ifdef mc68000
21617203Smckusick /*
21717203Smckusick  * Instruction stop table.
21817203Smckusick  * All instructions that implicitly modify any of the temporary
21917203Smckusick  * registers, change control flow, or implicitly loop must be
22017203Smckusick  * listed in this table. It is used to find the end of a basic
22117203Smckusick  * block when scanning backwards through the instruction stream
22217203Smckusick  * trying to merge the inline expansion.
22317203Smckusick  */
22417203Smckusick struct inststoptbl inststoptable[] = {
22517203Smckusick 	{ "" }
22617203Smckusick };
22717203Smckusick 
22817203Smckusick /*
22917203Smckusick  * Check to see if a line is a candidate for replacement.
23017203Smckusick  * Return pointer to name to be looked up in pattern table.
23117203Smckusick  */
23217203Smckusick char *
doreplaceon(cp)23317203Smckusick doreplaceon(cp)
23417203Smckusick 	char *cp;
23517203Smckusick {
23617203Smckusick 
23717203Smckusick 	if (bcmp(cp, "jbsr\t", 5) == 0)
23817203Smckusick 		return (cp + 5);
23917203Smckusick 	return (0);
24017203Smckusick }
24117203Smckusick 
24217203Smckusick /*
24324396Smckusick  * Find out how many arguments the function is being called with.
24424396Smckusick  * A return value of -1 indicates that the count can't be determined.
24524396Smckusick  */
24624396Smckusick /* ARGSUSED */
24724396Smckusick int
countargs(cp)24824396Smckusick countargs(cp)
24924396Smckusick 	char *cp;
25024396Smckusick {
25124396Smckusick 
25224396Smckusick 	/*
25324396Smckusick 	 * TODO
25424396Smckusick 	 * Figure out what the count should be.
25524396Smckusick 	 * Probably have to read the next instruction here
25624396Smckusick 	 * instead of in cleanup() below.
25724396Smckusick 	 */
25824396Smckusick 	return (-1);
25924396Smckusick }
26024396Smckusick 
26124396Smckusick /*
26217203Smckusick  * Find the next argument to the function being expanded.
26317203Smckusick  */
nextarg(argc,argv)26417203Smckusick nextarg(argc, argv)
26517203Smckusick 	int argc;
26617203Smckusick 	char *argv[];
26717203Smckusick {
26817203Smckusick 	register char *lastarg = argv[2];
26917203Smckusick 
27017203Smckusick 	if (argc == 3 &&
27117203Smckusick 	    bcmp(argv[0], "movl", 5) == 0 &&
27217203Smckusick 	    bcmp(argv[1], "sp@+", 5) == 0 &&
27317203Smckusick 	    (lastarg[1] == '0' || lastarg[1] == '1') &&
27417203Smckusick 	    lastarg[2] == '\0') {
27517203Smckusick 		if (lastarg[0] == 'd')
27617203Smckusick 			return (lastarg[1] - '0');
27717203Smckusick 		return (lastarg[1] - '0' + 8);
27817203Smckusick 	}
27917203Smckusick 	return (-1);
28017203Smckusick }
28117203Smckusick 
28217203Smckusick /*
28317203Smckusick  * Determine whether the current line pushes an argument.
28417203Smckusick  */
ispusharg(argc,argv)28524396Smckusick ispusharg(argc, argv)
28617203Smckusick 	int argc;
28717203Smckusick 	char *argv[];
28817203Smckusick {
28917203Smckusick 
29017203Smckusick 	if (argc < 2)
29117203Smckusick 		return (0);
29217203Smckusick 	if (argc == 2 && bcmp(argv[0], "pea", 4) == 0)
29317203Smckusick 		return (1);
29417203Smckusick 	if (bcmp(argv[argc - 1], "sp@-", 5) == 0)
29517203Smckusick 		return (1);
29617203Smckusick 	return (0);
29717203Smckusick }
29817203Smckusick 
29917203Smckusick /*
30017203Smckusick  * Determine which (if any) registers are modified
30117203Smckusick  * Return register number that is modified, -1 if none are modified.
30217203Smckusick  */
modifies(argc,argv)30317203Smckusick modifies(argc, argv)
30417203Smckusick 	int argc;
30517203Smckusick 	char *argv[];
30617203Smckusick {
30717203Smckusick 	/*
30817203Smckusick 	 * For the MC68000 all we care about are d0, d1, a0, and a1.
30917203Smckusick 	 */
31017203Smckusick 	register char *lastarg = argv[argc - 1];
31117203Smckusick 
31217203Smckusick 	if (lastarg[0] == 'd' && isdigit(lastarg[1]) && lastarg[2] == '\0')
31317203Smckusick 		return (lastarg[1] - '0');
31417203Smckusick 	if (lastarg[0] == 'a' && isdigit(lastarg[1]) && lastarg[2] == '\0')
31517203Smckusick 		return (lastarg[1] - '0' + 8);
31617203Smckusick 	return (-1);
31717203Smckusick }
31817203Smckusick 
31917203Smckusick /*
32017203Smckusick  * Rewrite the instruction in (argc, argv) to store its
32117203Smckusick  * contents into arg instead of onto the stack. The new
32217203Smckusick  * instruction is placed in the buffer that is provided.
32317203Smckusick  */
rewrite(instbuf,argc,argv,target)32417203Smckusick rewrite(instbuf, argc, argv, target)
32517203Smckusick 	char *instbuf;
32617203Smckusick 	int argc;
32717203Smckusick 	char *argv[];
32817203Smckusick 	int target;
32917203Smckusick {
33017203Smckusick 	int regno;
33117203Smckusick 	char regtype;
33217203Smckusick 
33317203Smckusick 	if (target < 8) {
33417203Smckusick 		regtype = 'd';
33517203Smckusick 		regno = target;
33617203Smckusick 	} else {
33717203Smckusick 		regtype = 'a';
33817203Smckusick 		regno = target - 8;
33917203Smckusick 	}
34017203Smckusick 	switch (argc) {
34117203Smckusick 	case 0:
34217203Smckusick 		instbuf[0] = '\0';
34321192Smckusick 		fprintf(stderr, "blank line to rewrite?\n");
34417203Smckusick 		return;
34517203Smckusick 	case 1:
34617203Smckusick 		sprintf(instbuf, "\t%s\n", argv[0]);
34717203Smckusick 		fprintf(stderr, "rewrite?-> %s", instbuf);
34817203Smckusick 		return;
34917203Smckusick 	case 2:
35017203Smckusick 		if (bcmp(argv[0], "pea", 4) == 0) {
35117203Smckusick 			if (regtype == 'a') {
35217203Smckusick 				sprintf(instbuf, "\tlea\t%s,%c%d\n",
35317203Smckusick 					argv[1], regtype, regno);
35417203Smckusick 				return;
35517203Smckusick 			}
35617203Smckusick 			if (argv[1][0] == '_' || isdigit(argv[1][0])) {
35717203Smckusick 				sprintf(instbuf, "\tmovl\t#%s,%c%d\n",
35817203Smckusick 					argv[1], regtype, regno);
35917203Smckusick 				return;
36017203Smckusick 			}
36117203Smckusick 			sprintf(instbuf,
36217203Smckusick 				"\texg\ta0,d%d\n\tlea\t%s,a0\n\texg\ta0,d%d\n",
36317203Smckusick 				regno, argv[1], regno);
36417203Smckusick 			return;
36517203Smckusick 		}
36617203Smckusick 		sprintf(instbuf, "\t%s\t%c%d\n", argv[0], regtype, regno);
36717203Smckusick 		return;
36817203Smckusick 	case 3:
36917203Smckusick 		sprintf(instbuf, "\t%s\t%s,%c%d\n",
37017203Smckusick 			argv[0], argv[1], regtype, regno);
37117203Smckusick 		return;
37217203Smckusick 	default:
37317203Smckusick 		sprintf(instbuf, "\t%s\t%s", argv[0], argv[1]);
37417203Smckusick 		argc -= 2, argv += 2;
37517203Smckusick 		while (argc-- > 0) {
37624396Smckusick 			(void) strcat(instbuf, ",");
37724396Smckusick 			(void) strcat(instbuf, *argv++);
37817203Smckusick 		}
37924396Smckusick 		(void) strcat(instbuf, "\n");
38017203Smckusick 		fprintf(stderr, "rewrite?-> %s", instbuf);
38117203Smckusick 		return;
38217203Smckusick 	}
38317203Smckusick }
38417203Smckusick 
38517203Smckusick /*
38617203Smckusick  * Do any necessary post expansion cleanup.
38717203Smckusick  */
cleanup(numargs)38817203Smckusick cleanup(numargs)
38917203Smckusick 	int numargs;
39017203Smckusick {
39124396Smckusick 	extern int lineno;
39217203Smckusick 
39317203Smckusick 	if (numargs == 0)
39417203Smckusick 		return;
39517203Smckusick 	/*
39617203Smckusick 	 * delete instruction to pop arguments.
39717203Smckusick 	 * TODO:
39817203Smckusick 	 *	CHECK FOR LABEL
39917203Smckusick 	 *	CHECK THAT INSTRUCTION IS A POP
40017203Smckusick 	 */
40117203Smckusick 	fgets(line[bufhead], MAXLINELEN, stdin);
40224396Smckusick 	lineno++;
40317203Smckusick }
40417203Smckusick #endif mc68000
405