2 Generates contents of opcode switch from ip_spec.t
4 mkswitch prefix ip_spec.t cases [ functions ]
7 The u flag has been implemented by just copying and adjusting the
8 code for 2, which is almost identical to that for 4. When this has
12 /* $Id: mkswitch.c,v 2.3 1994/06/24 10:50:06 ceriel Exp $ */
20 FILE *ifp; /* Input File Pointer */
21 FILE *ofp; /* Output File Pointer */
22 FILE *ffp = 0; /* Function File Pointer */
23 char *Prefix; /* Prefix for function name */
29 char mnem[8]; /* Mnemonic */
30 char flgs[8]; /* Flags */
35 if (argc < 4 || argc >5) {
36 fatal("usage is: %s prefix ip_spec.t cases [ functions ]\n",
42 if ((ifp = fopen(argv[2], "r")) == 0) {
43 fatal("cannot open '%s' for reading\n", argv[2]);
46 if ((ofp = fopen(argv[3], "w")) == 0) {
47 fatal("cannot open '%s' for writing\n", argv[3]);
50 if (argc == 5) { /* Need to store functions */
51 sprintf(command, "sort | uniq > %s", argv[4]);
52 if ((ffp = popen(command, "w")) == 0) {
53 fatal("cannot popen '%s'\n", command);
57 /* Start reading the input file */
58 while (fscanf(ifp, "%s %s", mnem, flgs) >= 0) {
65 for (p = flgs; *p; p++) {
66 if (!in("-ms2u4eNPwo", *p)) {
67 fatal("bad flags ip_spec: %s\n", flgs);
79 fatal("duplicate or missing opcode flag ip_spec: %s\n",
87 fatal("duplicate restriction flags ip_spec: %s\n", flgs);
90 /* capitalize mnemonic */
91 for (p = mnem; *p; p++) {
95 /* scan rest of line */
101 fscanf(ifp, " %d \n", &first);
104 fscanf(ifp, " %d %d \n", &cnt, &first);
108 if (in(flgs, 'e')) /* Escaped (secondary) opcode */
111 if (in(flgs, '4')) /* Escaped (tertiary) opcode */
116 /* analyse the opcode */
117 if (in(flgs, '-')) { /* No arguments */
118 NoArgs(base, first, mnem);
119 } else if (in(flgs, 'm')) { /* Mini */
120 for (i = 0; i<cnt; i++) {
121 Mini(i, flgs, base, first, mnem);
123 } else if (in(flgs, 's')) { /* Shortie */
124 for (i = 0; i<cnt; i++) {
125 Shortie(i, flgs, base, first, mnem);
127 } else if (in(flgs, '2')) { /* Two byte signed */
128 TwoSgn(flgs, base, first, mnem);
129 } else if (in(flgs, 'u')) { /* Two byte unsigned */
130 TwoUns(flgs, base, first, mnem);
131 } else if (in(flgs, '4')) { /* Four byte signed */
132 FourSgn(flgs, base, first, mnem);
134 fatal("no opcode flag in ip_spec %s\n", flgs);
140 NoArgs(base, first, mnem)
145 fprintf(ofp, "\t\tcase %s+%d:\t%s%sz(); break;\n",
146 base, first, Prefix, mnem);
148 fprintf(ffp, "%s%sz() {", Prefix, mnem);
149 fprintf(ffp, "LOG((\"@ %s%sz()\"));}\n", Prefix, mnem);
153 Mini(i, flgs, base, first, mnem)
161 int newi = in(flgs, 'N') ? (-i-1) : in(flgs, 'o') ? (i+1) : i;
166 in(flgs, 'w') ? "-wsize" : "-1L");
173 in(flgs, 'w') ? "wsize" : "1L");
176 sprintf(arg, "%dL%s",
177 newi, in(flgs, 'w') ? "*wsize" : "");
180 fprintf(ofp, "\t\tcase %s+%d:\t%s%sm(%s); break;\n",
181 base, first+i, Prefix, mnem, arg);
183 fprintf(ffp, "%s%sm(arg) long arg; {",
185 fprintf(ffp, "LOG((\"@ %s%sm(%%d)\", arg));}\n",
190 Shortie(i, flgs, base, first, mnem)
198 int newi = in(flgs, 'N') ? (-i-1) : in(flgs, 'o') ? (i+1) : i;
200 sprintf(arg, "%dL, %s", newi, (in(flgs, 'w') ? "wsize" : "1L"));
201 fprintf(ofp, "\t\tcase %s+%d:\t%s%ss(%s); break;\n",
202 base, first+i, Prefix, mnem, arg);
204 fprintf(ffp, "%s%ss(hob, wfac) long hob; size wfac; {",
206 fprintf(ffp, "LOG((\"@ %s%ss(%%d)\", hob, wfac));",
208 fprintf(ffp, " newPC(PC+1);}\n");
212 TwoSgn(flgs, base, first, mnem)
218 char *xy = in(flgs, 'P') ? "p2" : in(flgs, 'N') ? "n2" : "l2";
220 fprintf(ofp, "\t\tcase %s+%d:\t%s%s%s(%s); break;\n",
221 base, first, Prefix, mnem, xy,
222 in(flgs, 'w') ? "wsize" : "1L");
224 fprintf(ffp, "%s%s%s(arg) long arg; {", Prefix, mnem, xy);
225 fprintf(ffp, "LOG((\"@ %s%s%s(%%d)\", arg));",
227 fprintf(ffp, " newPC(PC+2);}\n");
231 TwoUns(flgs, base, first, mnem)
239 fprintf(ofp, "\t\tcase %s+%d:\t%s%s%s(%s); break;\n",
240 base, first, Prefix, mnem, xy,
241 in(flgs, 'w') ? "wsize" : "1L");
243 fprintf(ffp, "%s%s%s(arg) long arg; {", Prefix, mnem, xy);
244 fprintf(ffp, "LOG((\"@ %s%s%s(%%d)\", arg));",
246 fprintf(ffp, " newPC(PC+2);}\n");
250 FourSgn(flgs, base, first, mnem)
256 char *xy = in(flgs, 'P') ? "p4" : in(flgs, 'N') ? "n4" : "l4";
258 fprintf(ofp, "\t\tcase %s+%d:\t%s%s%s(%s); break;\n",
259 base, first, Prefix, mnem, xy,
260 in(flgs, 'w') ? "wsize" : "1L");
262 fprintf(ffp, "%s%s%s(arg) long arg; {", Prefix, mnem, xy);
263 fprintf(ffp, "LOG((\"@ %s%s%s(%%d)\", arg));",
265 fprintf(ffp, " newPC(PC+4);}\n");
284 fprintf(stderr, "%s, (fatal error): ", progname);
285 fprintf(stderr, fmt, str);
286 fprintf(stderr, "\n");