1*49430Sbostic /*-
2*49430Sbostic * Copyright (c) 1984 The Regents of the University of California.
3*49430Sbostic * All rights reserved.
4*49430Sbostic *
5*49430Sbostic * %sccs.include.redist.c%
624036Ssam */
724036Ssam
824036Ssam #ifndef lint
9*49430Sbostic static char sccsid[] = "@(#)machdep.c 1.4 (Berkeley) 05/08/91";
10*49430Sbostic #endif /* not lint */
1124036Ssam
1224036Ssam #include <stdio.h>
1324036Ssam #include <ctype.h>
1424036Ssam #include "inline.h"
1524036Ssam
1626405Ssam extern char *strcpy();
1726405Ssam extern char *strcat();
1826405Ssam extern char *index();
1926405Ssam
2024036Ssam /*
2124036Ssam * The routines and tables in this file must be rewritten
2224036Ssam * for each new machine that this program is ported to.
2324036Ssam */
2424036Ssam
2524036Ssam /*
2624036Ssam * Instruction stop table.
2724036Ssam * All instructions that implicitly modify any of the temporary
2824036Ssam * registers, change control flow, or implicitly loop must be
2924036Ssam * listed in this table. It is used to find the end of a basic
3024036Ssam * block when scanning backwards through the instruction stream
3124036Ssam * trying to merge the inline expansion.
3224036Ssam */
3324036Ssam struct inststoptbl inststoptable[] = {
3425692Ssam /* control */
3525692Ssam { "bbssi" }, { "bcc" }, { "bcs" }, { "beql" }, { "beqlu" },
3625692Ssam { "bgeq" }, { "bgequ" }, { "bgtr" }, { "bgtru" }, { "bleq" },
3725692Ssam { "blequ" }, { "blss" }, { "blssu" }, { "bneq" }, { "bnequ" },
3825692Ssam { "brb" }, { "brw" }, { "bvc" }, { "bvs" }, { "jmp" },
3925692Ssam /* jump versions of control */
4025692Ssam { "jbc" }, { "jbs" }, { "jeql" }, { "jeqlu" },
4125692Ssam { "jgeq" }, { "jgequ" }, { "jgtr" }, { "jgtru" }, { "jleq" },
4225692Ssam { "jlequ" }, { "jlss" }, { "jlssu" }, { "jneq" }, { "jnequ" },
4325692Ssam { "jcc" }, { "jcs" }, { "jvc" }, { "jvs" }, { "jbr" },
4425692Ssam /* multiple registers */
4525692Ssam { "loadr" },
4625692Ssam /* bit field */
4725692Ssam { "bbc" }, { "bbs" },
4825692Ssam /* character string and block move */
4925692Ssam { "cmps2" }, { "cmps3" }, { "movblk" }, { "movs2" }, { "movs3" },
5025692Ssam /* procedure call */
5125692Ssam { "callf" }, { "calls" }, { "ret" },
5225692Ssam /* loop control */
5325692Ssam { "aobleq" }, { "aoblss" }, { "casel" },
5425692Ssam /* privileged and miscellaneous */
5525692Ssam { "bpt" }, { "halt" }, { "kcall" }, { "ldpctx" }, { "rei" },
5625692Ssam { "svpctx" },
5725692Ssam { "" }
5825692Ssam };
5925692Ssam
6025692Ssam /*
6125692Ssam * Check to see if a line is a candidate for replacement.
6225692Ssam * Return pointer to name to be looked up in pattern table.
6325692Ssam */
6425692Ssam char *
doreplaceon(cp)6525692Ssam doreplaceon(cp)
6625692Ssam char *cp;
6725692Ssam {
6825692Ssam
6926405Ssam if (bcmp(cp, "callf\t", 6))
7026405Ssam return (0);
7126405Ssam if ((cp = index(cp + 6, ',')) == 0)
7226405Ssam return (0);
7326405Ssam return (++cp);
7425692Ssam }
7525692Ssam
7625692Ssam /*
7726405Ssam * Find out how many arguments the function is being called with.
7826405Ssam * A return value of -1 indicates that the count can't be determined.
7926405Ssam */
countargs(cp)8026405Ssam countargs(cp)
8126405Ssam char *cp;
8226405Ssam {
8326405Ssam int i;
8426405Ssam
8526405Ssam if ((cp = index(cp, '$')) == 0)
8626405Ssam return (-1);
8726405Ssam if (!isdigit(*++cp) || (i = atoi(cp)) == -1)
8826405Ssam return (-1);
8926405Ssam return (i/4 - 1);
9026405Ssam }
9126405Ssam
9226405Ssam /*
9324036Ssam * Find the next argument to the function being expanded.
9424036Ssam */
nextarg(argc,argv)9524036Ssam nextarg(argc, argv)
9624036Ssam int argc;
9724036Ssam char *argv[];
9824036Ssam {
9924036Ssam register char *lastarg = argv[2];
10024036Ssam
10124036Ssam if (argc == 3 &&
10224036Ssam bcmp(argv[0], "mov", 3) == 0 &&
10324036Ssam bcmp(argv[1], "(sp)+", 6) == 0 &&
10424036Ssam lastarg[0] == 'r' && isdigit(lastarg[1]) && lastarg[2] == '\0')
10524036Ssam return (lastarg[1] - '0');
10624036Ssam return (-1);
10724036Ssam }
10824036Ssam
10924036Ssam /*
11024036Ssam * Determine whether the current line pushes an argument.
11124036Ssam */
ispusharg(argc,argv)11225692Ssam ispusharg(argc, argv)
11324036Ssam int argc;
11424036Ssam char *argv[];
11524036Ssam {
11624036Ssam
11724036Ssam if (argc < 2)
11824036Ssam return (0);
11924036Ssam if (argc == 2 && bcmp(argv[0], "push", 4) == 0)
12024036Ssam return (1);
12124036Ssam if (bcmp(argv[argc - 1], "-(sp)", 6) == 0)
12224036Ssam return (1);
12324036Ssam return (0);
12424036Ssam }
12524036Ssam
12624036Ssam /*
12724036Ssam * Determine which (if any) registers are modified
12824036Ssam * Return register number that is modified, -1 if none are modified.
12924036Ssam */
modifies(argc,argv)13024036Ssam modifies(argc, argv)
13124036Ssam int argc;
13224036Ssam char *argv[];
13324036Ssam {
13425692Ssam register char *lastarg = argv[argc - 1];
13525692Ssam
13624036Ssam /*
13726405Ssam * For the tahoe all we care about are r0 to r5
13824036Ssam */
13924036Ssam if (lastarg[0] == 'r' && isdigit(lastarg[1]) && lastarg[2] == '\0')
14024036Ssam return (lastarg[1] - '0');
14124036Ssam return (-1);
14224036Ssam }
14324036Ssam
14424036Ssam /*
14524036Ssam * Rewrite the instruction in (argc, argv) to store its
14624036Ssam * contents into arg instead of onto the stack. The new
14724036Ssam * instruction is placed in the buffer that is provided.
14824036Ssam */
rewrite(instbuf,argc,argv,target)14924036Ssam rewrite(instbuf, argc, argv, target)
15024036Ssam char *instbuf;
15124036Ssam int argc;
15224036Ssam char *argv[];
15324036Ssam int target;
15424036Ssam {
15524036Ssam
15624036Ssam switch (argc) {
15724036Ssam case 0:
15824036Ssam instbuf[0] = '\0';
15924036Ssam fprintf(stderr, "blank line to rewrite?\n");
16024036Ssam return;
16124036Ssam case 1:
16224036Ssam sprintf(instbuf, "\t%s\n", argv[0]);
16324036Ssam fprintf(stderr, "rewrite?-> %s", instbuf);
16424036Ssam return;
16524036Ssam case 2:
16624036Ssam if (bcmp(argv[0], "push", 4) == 0) {
16724036Ssam sprintf(instbuf, "\tmov%s\t%s,r%d\n",
16824036Ssam &argv[0][4], argv[1], target);
16924036Ssam return;
17024036Ssam }
17124036Ssam sprintf(instbuf, "\t%s\tr%d\n", argv[0], target);
17224036Ssam return;
17324036Ssam case 3:
17424036Ssam sprintf(instbuf, "\t%s\t%s,r%d\n", argv[0], argv[1], target);
17524036Ssam return;
17624036Ssam case 4:
17724036Ssam sprintf(instbuf, "\t%s\t%s,%s,r%d\n",
17824036Ssam argv[0], argv[1], argv[2], target);
17924036Ssam return;
18024036Ssam case 5:
18124036Ssam sprintf(instbuf, "\t%s\t%s,%s,%s,r%d\n",
18224036Ssam argv[0], argv[1], argv[2], argv[3], target);
18324036Ssam return;
18424036Ssam default:
18524036Ssam sprintf(instbuf, "\t%s\t%s", argv[0], argv[1]);
18624036Ssam argc -= 2, argv += 2;
18724036Ssam while (argc-- > 0) {
18824036Ssam strcat(instbuf, ",");
18924036Ssam strcat(instbuf, *argv++);
19024036Ssam }
19124036Ssam strcat(instbuf, "\n");
19224036Ssam fprintf(stderr, "rewrite?-> %s", instbuf);
19324036Ssam return;
19424036Ssam }
19524036Ssam }
19624036Ssam
19724036Ssam /*
19824036Ssam * Do any necessary post expansion cleanup.
19924036Ssam */
cleanup(numargs)20024036Ssam cleanup(numargs)
20124036Ssam int numargs;
20224036Ssam {
20324036Ssam
20424036Ssam }
205