1*3d8817e4Smiod /* s390-mkopc.c -- Generates opcode table out of s390-opc.txt
2*3d8817e4Smiod Copyright 2000, 2001, 2003 Free Software Foundation, Inc.
3*3d8817e4Smiod Contributed by Martin Schwidefsky (schwidefsky@de.ibm.com).
4*3d8817e4Smiod
5*3d8817e4Smiod This file is part of GDB, GAS, and the GNU binutils.
6*3d8817e4Smiod
7*3d8817e4Smiod This program is free software; you can redistribute it and/or modify
8*3d8817e4Smiod it under the terms of the GNU General Public License as published by
9*3d8817e4Smiod the Free Software Foundation; either version 2 of the License, or
10*3d8817e4Smiod (at your option) any later version.
11*3d8817e4Smiod
12*3d8817e4Smiod This program is distributed in the hope that it will be useful,
13*3d8817e4Smiod but WITHOUT ANY WARRANTY; without even the implied warranty of
14*3d8817e4Smiod MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*3d8817e4Smiod GNU General Public License for more details.
16*3d8817e4Smiod
17*3d8817e4Smiod You should have received a copy of the GNU General Public License
18*3d8817e4Smiod along with this program; if not, write to the Free Software
19*3d8817e4Smiod Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
20*3d8817e4Smiod 02110-1301, USA. */
21*3d8817e4Smiod
22*3d8817e4Smiod #include <stdio.h>
23*3d8817e4Smiod #include <stdlib.h>
24*3d8817e4Smiod #include <string.h>
25*3d8817e4Smiod
26*3d8817e4Smiod /* Taken from opcodes/s390.h */
27*3d8817e4Smiod enum s390_opcode_mode_val
28*3d8817e4Smiod {
29*3d8817e4Smiod S390_OPCODE_ESA = 0,
30*3d8817e4Smiod S390_OPCODE_ZARCH
31*3d8817e4Smiod };
32*3d8817e4Smiod
33*3d8817e4Smiod enum s390_opcode_cpu_val
34*3d8817e4Smiod {
35*3d8817e4Smiod S390_OPCODE_G5 = 0,
36*3d8817e4Smiod S390_OPCODE_G6,
37*3d8817e4Smiod S390_OPCODE_Z900,
38*3d8817e4Smiod S390_OPCODE_Z990,
39*3d8817e4Smiod S390_OPCODE_Z9_109
40*3d8817e4Smiod };
41*3d8817e4Smiod
42*3d8817e4Smiod struct op_struct
43*3d8817e4Smiod {
44*3d8817e4Smiod char opcode[16];
45*3d8817e4Smiod char mnemonic[16];
46*3d8817e4Smiod char format[16];
47*3d8817e4Smiod int mode_bits;
48*3d8817e4Smiod int min_cpu;
49*3d8817e4Smiod
50*3d8817e4Smiod unsigned long long sort_value;
51*3d8817e4Smiod int no_nibbles;
52*3d8817e4Smiod };
53*3d8817e4Smiod
54*3d8817e4Smiod struct op_struct *op_array;
55*3d8817e4Smiod int max_ops;
56*3d8817e4Smiod int no_ops;
57*3d8817e4Smiod
58*3d8817e4Smiod static void
createTable(void)59*3d8817e4Smiod createTable (void)
60*3d8817e4Smiod {
61*3d8817e4Smiod max_ops = 256;
62*3d8817e4Smiod op_array = malloc (max_ops * sizeof (struct op_struct));
63*3d8817e4Smiod no_ops = 0;
64*3d8817e4Smiod }
65*3d8817e4Smiod
66*3d8817e4Smiod /* `insertOpcode': insert an op_struct into sorted opcode array. */
67*3d8817e4Smiod
68*3d8817e4Smiod static void
insertOpcode(char * opcode,char * mnemonic,char * format,int min_cpu,int mode_bits)69*3d8817e4Smiod insertOpcode (char *opcode, char *mnemonic, char *format,
70*3d8817e4Smiod int min_cpu, int mode_bits)
71*3d8817e4Smiod {
72*3d8817e4Smiod char *str;
73*3d8817e4Smiod unsigned long long sort_value;
74*3d8817e4Smiod int no_nibbles;
75*3d8817e4Smiod int ix, k;
76*3d8817e4Smiod
77*3d8817e4Smiod while (no_ops >= max_ops)
78*3d8817e4Smiod {
79*3d8817e4Smiod max_ops = max_ops * 2;
80*3d8817e4Smiod op_array = realloc (op_array, max_ops * sizeof (struct op_struct));
81*3d8817e4Smiod }
82*3d8817e4Smiod
83*3d8817e4Smiod sort_value = 0;
84*3d8817e4Smiod str = opcode;
85*3d8817e4Smiod for (ix = 0; ix < 16; ix++)
86*3d8817e4Smiod {
87*3d8817e4Smiod if (*str >= '0' && *str <= '9')
88*3d8817e4Smiod sort_value = (sort_value << 4) + (*str - '0');
89*3d8817e4Smiod else if (*str >= 'a' && *str <= 'f')
90*3d8817e4Smiod sort_value = (sort_value << 4) + (*str - 'a' + 10);
91*3d8817e4Smiod else if (*str >= 'A' && *str <= 'F')
92*3d8817e4Smiod sort_value = (sort_value << 4) + (*str - 'A' + 10);
93*3d8817e4Smiod else if (*str == '?')
94*3d8817e4Smiod sort_value <<= 4;
95*3d8817e4Smiod else
96*3d8817e4Smiod break;
97*3d8817e4Smiod str ++;
98*3d8817e4Smiod }
99*3d8817e4Smiod sort_value <<= 4*(16 - ix);
100*3d8817e4Smiod sort_value += (min_cpu << 8) + mode_bits;
101*3d8817e4Smiod no_nibbles = ix;
102*3d8817e4Smiod for (ix = 0; ix < no_ops; ix++)
103*3d8817e4Smiod if (sort_value > op_array[ix].sort_value)
104*3d8817e4Smiod break;
105*3d8817e4Smiod for (k = no_ops; k > ix; k--)
106*3d8817e4Smiod op_array[k] = op_array[k-1];
107*3d8817e4Smiod strcpy(op_array[ix].opcode, opcode);
108*3d8817e4Smiod strcpy(op_array[ix].mnemonic, mnemonic);
109*3d8817e4Smiod strcpy(op_array[ix].format, format);
110*3d8817e4Smiod op_array[ix].sort_value = sort_value;
111*3d8817e4Smiod op_array[ix].no_nibbles = no_nibbles;
112*3d8817e4Smiod op_array[ix].min_cpu = min_cpu;
113*3d8817e4Smiod op_array[ix].mode_bits = mode_bits;
114*3d8817e4Smiod no_ops++;
115*3d8817e4Smiod }
116*3d8817e4Smiod
117*3d8817e4Smiod static char file_header[] =
118*3d8817e4Smiod "/* The opcode table. This file was generated by s390-mkopc.\n\n"
119*3d8817e4Smiod " The format of the opcode table is:\n\n"
120*3d8817e4Smiod " NAME OPCODE MASK OPERANDS\n\n"
121*3d8817e4Smiod " Name is the name of the instruction.\n"
122*3d8817e4Smiod " OPCODE is the instruction opcode.\n"
123*3d8817e4Smiod " MASK is the opcode mask; this is used to tell the disassembler\n"
124*3d8817e4Smiod " which bits in the actual opcode must match OPCODE.\n"
125*3d8817e4Smiod " OPERANDS is the list of operands.\n\n"
126*3d8817e4Smiod " The disassembler reads the table in order and prints the first\n"
127*3d8817e4Smiod " instruction which matches. */\n\n"
128*3d8817e4Smiod "const struct s390_opcode s390_opcodes[] =\n {\n";
129*3d8817e4Smiod
130*3d8817e4Smiod /* `dumpTable': write opcode table. */
131*3d8817e4Smiod
132*3d8817e4Smiod static void
dumpTable(void)133*3d8817e4Smiod dumpTable (void)
134*3d8817e4Smiod {
135*3d8817e4Smiod char *str;
136*3d8817e4Smiod int ix;
137*3d8817e4Smiod
138*3d8817e4Smiod /* Write hash table entries (slots). */
139*3d8817e4Smiod printf (file_header);
140*3d8817e4Smiod
141*3d8817e4Smiod for (ix = 0; ix < no_ops; ix++)
142*3d8817e4Smiod {
143*3d8817e4Smiod printf (" { \"%s\", ", op_array[ix].mnemonic);
144*3d8817e4Smiod for (str = op_array[ix].opcode; *str != 0; str++)
145*3d8817e4Smiod if (*str == '?')
146*3d8817e4Smiod *str = '0';
147*3d8817e4Smiod printf ("OP%i(0x%sLL), ",
148*3d8817e4Smiod op_array[ix].no_nibbles*4, op_array[ix].opcode);
149*3d8817e4Smiod printf ("MASK_%s, INSTR_%s, ",
150*3d8817e4Smiod op_array[ix].format, op_array[ix].format);
151*3d8817e4Smiod printf ("%i, ", op_array[ix].mode_bits);
152*3d8817e4Smiod printf ("%i}", op_array[ix].min_cpu);
153*3d8817e4Smiod if (ix < no_ops-1)
154*3d8817e4Smiod printf (",\n");
155*3d8817e4Smiod else
156*3d8817e4Smiod printf ("\n");
157*3d8817e4Smiod }
158*3d8817e4Smiod printf ("};\n\n");
159*3d8817e4Smiod printf ("const int s390_num_opcodes =\n");
160*3d8817e4Smiod printf (" sizeof (s390_opcodes) / sizeof (s390_opcodes[0]);\n\n");
161*3d8817e4Smiod }
162*3d8817e4Smiod
163*3d8817e4Smiod int
main(void)164*3d8817e4Smiod main (void)
165*3d8817e4Smiod {
166*3d8817e4Smiod char currentLine[256];
167*3d8817e4Smiod
168*3d8817e4Smiod createTable ();
169*3d8817e4Smiod
170*3d8817e4Smiod /* Read opcode descriptions from `stdin'. For each mnemonic,
171*3d8817e4Smiod make an entry into the opcode table. */
172*3d8817e4Smiod while (fgets (currentLine, sizeof (currentLine), stdin) != NULL)
173*3d8817e4Smiod {
174*3d8817e4Smiod char opcode[16];
175*3d8817e4Smiod char mnemonic[16];
176*3d8817e4Smiod char format[16];
177*3d8817e4Smiod char description[64];
178*3d8817e4Smiod char cpu_string[16];
179*3d8817e4Smiod char modes_string[16];
180*3d8817e4Smiod int min_cpu;
181*3d8817e4Smiod int mode_bits;
182*3d8817e4Smiod char *str;
183*3d8817e4Smiod
184*3d8817e4Smiod if (currentLine[0] == '#')
185*3d8817e4Smiod continue;
186*3d8817e4Smiod memset (opcode, 0, 8);
187*3d8817e4Smiod if (sscanf (currentLine, "%15s %15s %15s \"%[^\"]\" %15s %15s",
188*3d8817e4Smiod opcode, mnemonic, format, description,
189*3d8817e4Smiod cpu_string, modes_string) == 6)
190*3d8817e4Smiod {
191*3d8817e4Smiod if (strcmp (cpu_string, "g5") == 0)
192*3d8817e4Smiod min_cpu = S390_OPCODE_G5;
193*3d8817e4Smiod else if (strcmp (cpu_string, "g6") == 0)
194*3d8817e4Smiod min_cpu = S390_OPCODE_G6;
195*3d8817e4Smiod else if (strcmp (cpu_string, "z900") == 0)
196*3d8817e4Smiod min_cpu = S390_OPCODE_Z900;
197*3d8817e4Smiod else if (strcmp (cpu_string, "z990") == 0)
198*3d8817e4Smiod min_cpu = S390_OPCODE_Z990;
199*3d8817e4Smiod else if (strcmp (cpu_string, "z9-109") == 0)
200*3d8817e4Smiod min_cpu = S390_OPCODE_Z9_109;
201*3d8817e4Smiod else {
202*3d8817e4Smiod fprintf (stderr, "Couldn't parse cpu string %s\n", cpu_string);
203*3d8817e4Smiod exit (1);
204*3d8817e4Smiod }
205*3d8817e4Smiod
206*3d8817e4Smiod str = modes_string;
207*3d8817e4Smiod mode_bits = 0;
208*3d8817e4Smiod do {
209*3d8817e4Smiod if (strncmp (str, "esa", 3) == 0
210*3d8817e4Smiod && (str[3] == 0 || str[3] == ',')) {
211*3d8817e4Smiod mode_bits |= 1 << S390_OPCODE_ESA;
212*3d8817e4Smiod str += 3;
213*3d8817e4Smiod } else if (strncmp (str, "zarch", 5) == 0
214*3d8817e4Smiod && (str[5] == 0 || str[5] == ',')) {
215*3d8817e4Smiod mode_bits |= 1 << S390_OPCODE_ZARCH;
216*3d8817e4Smiod str += 5;
217*3d8817e4Smiod } else {
218*3d8817e4Smiod fprintf (stderr, "Couldn't parse modes string %s\n",
219*3d8817e4Smiod modes_string);
220*3d8817e4Smiod exit (1);
221*3d8817e4Smiod }
222*3d8817e4Smiod if (*str == ',')
223*3d8817e4Smiod str++;
224*3d8817e4Smiod } while (*str != 0);
225*3d8817e4Smiod insertOpcode (opcode, mnemonic, format, min_cpu, mode_bits);
226*3d8817e4Smiod }
227*3d8817e4Smiod else
228*3d8817e4Smiod fprintf (stderr, "Couldn't scan line %s\n", currentLine);
229*3d8817e4Smiod }
230*3d8817e4Smiod
231*3d8817e4Smiod dumpTable ();
232*3d8817e4Smiod return 0;
233*3d8817e4Smiod }
234