1 /* s390-mkopc.c -- Generates opcode table out of s390-opc.txt
2 Copyright 2000, 2001, 2003, 2007 Free Software Foundation, Inc.
3 Contributed by Martin Schwidefsky (schwidefsky@de.ibm.com).
5 This file is part of the GNU opcodes library.
7 This library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 It is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this file; see the file COPYING. If not, write to the
19 Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
26 /* Taken from opcodes/s390.h */
27 enum s390_opcode_mode_val
33 enum s390_opcode_cpu_val
51 unsigned long long sort_value;
55 struct op_struct *op_array;
63 op_array = malloc (max_ops * sizeof (struct op_struct));
67 /* `insertOpcode': insert an op_struct into sorted opcode array. */
70 insertOpcode (char *opcode, char *mnemonic, char *format,
71 int min_cpu, int mode_bits)
74 unsigned long long sort_value;
78 while (no_ops >= max_ops)
80 max_ops = max_ops * 2;
81 op_array = realloc (op_array, max_ops * sizeof (struct op_struct));
86 for (ix = 0; ix < 16; ix++)
88 if (*str >= '0' && *str <= '9')
89 sort_value = (sort_value << 4) + (*str - '0');
90 else if (*str >= 'a' && *str <= 'f')
91 sort_value = (sort_value << 4) + (*str - 'a' + 10);
92 else if (*str >= 'A' && *str <= 'F')
93 sort_value = (sort_value << 4) + (*str - 'A' + 10);
100 sort_value <<= 4*(16 - ix);
101 sort_value += (min_cpu << 8) + mode_bits;
103 for (ix = 0; ix < no_ops; ix++)
104 if (sort_value > op_array[ix].sort_value)
106 for (k = no_ops; k > ix; k--)
107 op_array[k] = op_array[k-1];
108 strcpy(op_array[ix].opcode, opcode);
109 strcpy(op_array[ix].mnemonic, mnemonic);
110 strcpy(op_array[ix].format, format);
111 op_array[ix].sort_value = sort_value;
112 op_array[ix].no_nibbles = no_nibbles;
113 op_array[ix].min_cpu = min_cpu;
114 op_array[ix].mode_bits = mode_bits;
118 struct s390_cond_ext_format
124 #define NUM_COND_EXTENSIONS 20
126 const struct s390_cond_ext_format s390_cond_extensions[NUM_COND_EXTENSIONS] =
128 { '1', "o" }, /* jump on overflow / if ones */
129 { '2', "h" }, /* jump on A high */
130 { '2', "p" }, /* jump on plus */
131 { '3', "nle" }, /* jump on not low or equal */
132 { '4', "l" }, /* jump on A low */
133 { '4', "m" }, /* jump on minus / if mixed */
134 { '5', "nhe" }, /* jump on not high or equal */
135 { '6', "lh" }, /* jump on low or high */
136 { '7', "ne" }, /* jump on A not equal B */
137 { '7', "nz" }, /* jump on not zero / if not zeros */
138 { '8', "e" }, /* jump on A equal B */
139 { '8', "z" }, /* jump on zero / if zeros */
140 { '9', "nlh" }, /* jump on not low or high */
141 { 'a', "he" }, /* jump on high or equal */
142 { 'b', "nl" }, /* jump on A not low */
143 { 'b', "nm" }, /* jump on not minus / if not mixed */
144 { 'c', "le" }, /* jump on low or equal */
145 { 'd', "nh" }, /* jump on A not high */
146 { 'd', "np" }, /* jump on not plus */
147 { 'e', "no" }, /* jump on not overflow / if not ones */
150 /* As with insertOpcode instructions are added to the sorted opcode
151 array. Additionally mnemonics containing the '*<number>' tag are
152 expanded to the set of conditional instructions described by
153 s390_cond_extensions with the '*<number>' tag replaced by the
154 respective mnemonic extensions. */
157 expandConditionalJump (char *opcode, char *mnemonic, char *format,
158 int min_cpu, int mode_bits)
163 int mask_start, i = 0, star_found = 0, reading_number = 0;
164 int number_p = 0, suffix_p = 0, prefix_p = 0;
166 while (mnemonic[i] != '\0')
172 goto malformed_mnemonic;
178 case '0': case '1': case '2': case '3': case '4':
179 case '5': case '6': case '7': case '8': case '9':
180 if (!star_found || !reading_number)
181 goto malformed_mnemonic;
183 number[number_p++] = mnemonic[i];
190 goto malformed_mnemonic;
196 suffix[suffix_p++] = mnemonic[i];
198 prefix[prefix_p++] = mnemonic[i];
203 prefix[prefix_p] = '\0';
204 suffix[suffix_p] = '\0';
205 number[number_p] = '\0';
207 if (sscanf (number, "%d", &mask_start) != 1)
208 goto malformed_mnemonic;
212 fprintf (stderr, "Conditional mask not at nibble boundary in: %s\n",
219 for (i = 0; i < NUM_COND_EXTENSIONS; i++)
221 char new_mnemonic[15];
223 strcpy (new_mnemonic, prefix);
224 strcat (new_mnemonic, s390_cond_extensions[i].extension);
225 strcat (new_mnemonic, suffix);
226 opcode[mask_start] = s390_cond_extensions[i].nibble;
227 insertOpcode (opcode, new_mnemonic, format, min_cpu, mode_bits);
232 fprintf (stderr, "Malformed mnemonic: %s\n", mnemonic);
235 static char file_header[] =
236 "/* The opcode table. This file was generated by s390-mkopc.\n\n"
237 " The format of the opcode table is:\n\n"
238 " NAME OPCODE MASK OPERANDS\n\n"
239 " Name is the name of the instruction.\n"
240 " OPCODE is the instruction opcode.\n"
241 " MASK is the opcode mask; this is used to tell the disassembler\n"
242 " which bits in the actual opcode must match OPCODE.\n"
243 " OPERANDS is the list of operands.\n\n"
244 " The disassembler reads the table in order and prints the first\n"
245 " instruction which matches. */\n\n"
246 "const struct s390_opcode s390_opcodes[] =\n {\n";
248 /* `dumpTable': write opcode table. */
256 /* Write hash table entries (slots). */
257 printf (file_header);
259 for (ix = 0; ix < no_ops; ix++)
261 printf (" { \"%s\", ", op_array[ix].mnemonic);
262 for (str = op_array[ix].opcode; *str != 0; str++)
265 printf ("OP%i(0x%sLL), ",
266 op_array[ix].no_nibbles*4, op_array[ix].opcode);
267 printf ("MASK_%s, INSTR_%s, ",
268 op_array[ix].format, op_array[ix].format);
269 printf ("%i, ", op_array[ix].mode_bits);
270 printf ("%i}", op_array[ix].min_cpu);
277 printf ("const int s390_num_opcodes =\n");
278 printf (" sizeof (s390_opcodes) / sizeof (s390_opcodes[0]);\n\n");
284 char currentLine[256];
288 /* Read opcode descriptions from `stdin'. For each mnemonic,
289 make an entry into the opcode table. */
290 while (fgets (currentLine, sizeof (currentLine), stdin) != NULL)
295 char description[64];
297 char modes_string[16];
302 if (currentLine[0] == '#')
304 memset (opcode, 0, 8);
305 if (sscanf (currentLine, "%15s %15s %15s \"%[^\"]\" %15s %15s",
306 opcode, mnemonic, format, description,
307 cpu_string, modes_string) == 6)
309 if (strcmp (cpu_string, "g5") == 0)
310 min_cpu = S390_OPCODE_G5;
311 else if (strcmp (cpu_string, "g6") == 0)
312 min_cpu = S390_OPCODE_G6;
313 else if (strcmp (cpu_string, "z900") == 0)
314 min_cpu = S390_OPCODE_Z900;
315 else if (strcmp (cpu_string, "z990") == 0)
316 min_cpu = S390_OPCODE_Z990;
317 else if (strcmp (cpu_string, "z9-109") == 0)
318 min_cpu = S390_OPCODE_Z9_109;
319 else if (strcmp (cpu_string, "z9-ec") == 0)
320 min_cpu = S390_OPCODE_Z9_EC;
322 fprintf (stderr, "Couldn't parse cpu string %s\n", cpu_string);
329 if (strncmp (str, "esa", 3) == 0
330 && (str[3] == 0 || str[3] == ',')) {
331 mode_bits |= 1 << S390_OPCODE_ESA;
333 } else if (strncmp (str, "zarch", 5) == 0
334 && (str[5] == 0 || str[5] == ',')) {
335 mode_bits |= 1 << S390_OPCODE_ZARCH;
338 fprintf (stderr, "Couldn't parse modes string %s\n",
346 if (!strchr (mnemonic, '*'))
347 insertOpcode (opcode, mnemonic, format, min_cpu, mode_bits);
349 expandConditionalJump (opcode, mnemonic, format,
353 fprintf (stderr, "Couldn't scan line %s\n", currentLine);