1 /* s390-mkopc.c -- Generates opcode table out of s390-opc.txt
2 Copyright (C) 2000-2018 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. */
25 #include "opcode/s390.h"
36 unsigned long long sort_value;
40 struct op_struct *op_array;
48 op_array = malloc (max_ops * sizeof (struct op_struct));
52 /* `insertOpcode': insert an op_struct into sorted opcode array. */
55 insertOpcode (char *opcode, char *mnemonic, char *format,
56 int min_cpu, int mode_bits, int flags)
59 unsigned long long sort_value;
63 while (no_ops >= max_ops)
65 max_ops = max_ops * 2;
66 op_array = realloc (op_array, max_ops * sizeof (struct op_struct));
71 for (ix = 0; ix < 16; ix++)
73 if (*str >= '0' && *str <= '9')
74 sort_value = (sort_value << 4) + (*str - '0');
75 else if (*str >= 'a' && *str <= 'f')
76 sort_value = (sort_value << 4) + (*str - 'a' + 10);
77 else if (*str >= 'A' && *str <= 'F')
78 sort_value = (sort_value << 4) + (*str - 'A' + 10);
85 sort_value <<= 4*(16 - ix);
86 sort_value += (min_cpu << 8) + mode_bits;
88 for (ix = 0; ix < no_ops; ix++)
89 if (sort_value > op_array[ix].sort_value)
91 for (k = no_ops; k > ix; k--)
92 op_array[k] = op_array[k-1];
93 strcpy(op_array[ix].opcode, opcode);
94 strcpy(op_array[ix].mnemonic, mnemonic);
95 strcpy(op_array[ix].format, format);
96 op_array[ix].sort_value = sort_value;
97 op_array[ix].no_nibbles = no_nibbles;
98 op_array[ix].min_cpu = min_cpu;
99 op_array[ix].mode_bits = mode_bits;
100 op_array[ix].flags = flags;
104 struct s390_cond_ext_format
110 /* The mnemonic extensions for conditional jumps used to replace
112 #define NUM_COND_EXTENSIONS 20
113 const struct s390_cond_ext_format s390_cond_extensions[NUM_COND_EXTENSIONS] =
114 { { '1', "o" }, /* jump on overflow / if ones */
115 { '2', "h" }, /* jump on A high */
116 { '2', "p" }, /* jump on plus */
117 { '3', "nle" }, /* jump on not low or equal */
118 { '4', "l" }, /* jump on A low */
119 { '4', "m" }, /* jump on minus / if mixed */
120 { '5', "nhe" }, /* jump on not high or equal */
121 { '6', "lh" }, /* jump on low or high */
122 { '7', "ne" }, /* jump on A not equal B */
123 { '7', "nz" }, /* jump on not zero / if not zeros */
124 { '8', "e" }, /* jump on A equal B */
125 { '8', "z" }, /* jump on zero / if zeros */
126 { '9', "nlh" }, /* jump on not low or high */
127 { 'a', "he" }, /* jump on high or equal */
128 { 'b', "nl" }, /* jump on A not low */
129 { 'b', "nm" }, /* jump on not minus / if not mixed */
130 { 'c', "le" }, /* jump on low or equal */
131 { 'd', "nh" }, /* jump on A not high */
132 { 'd', "np" }, /* jump on not plus */
133 { 'e', "no" }, /* jump on not overflow / if not ones */
136 /* The mnemonic extensions for conditional branches used to replace
138 #define NUM_CRB_EXTENSIONS 12
139 const struct s390_cond_ext_format s390_crb_extensions[NUM_CRB_EXTENSIONS] =
140 { { '2', "h" }, /* jump on A high */
141 { '2', "nle" }, /* jump on not low or equal */
142 { '4', "l" }, /* jump on A low */
143 { '4', "nhe" }, /* jump on not high or equal */
144 { '6', "ne" }, /* jump on A not equal B */
145 { '6', "lh" }, /* jump on low or high */
146 { '8', "e" }, /* jump on A equal B */
147 { '8', "nlh" }, /* jump on not low or high */
148 { 'a', "nl" }, /* jump on A not low */
149 { 'a', "he" }, /* jump on high or equal */
150 { 'c', "nh" }, /* jump on A not high */
151 { 'c', "le" }, /* jump on low or equal */
154 /* As with insertOpcode instructions are added to the sorted opcode
155 array. Additionally mnemonics containing the '*<number>' tag are
156 expanded to the set of conditional instructions described by
157 s390_cond_extensions with the tag replaced by the respective
158 mnemonic extensions. */
161 insertExpandedMnemonic (char *opcode, char *mnemonic, char *format,
162 int min_cpu, int mode_bits, int flags)
168 int mask_start, i = 0, tag_found = 0, reading_number = 0;
169 int number_p = 0, suffix_p = 0, prefix_p = 0;
170 const struct s390_cond_ext_format *ext_table;
171 int ext_table_length;
173 if (!(tag = strpbrk (mnemonic, "*$")))
175 insertOpcode (opcode, mnemonic, format, min_cpu, mode_bits, flags);
179 while (mnemonic[i] != '\0')
181 if (mnemonic[i] == *tag)
184 goto malformed_mnemonic;
192 case '0': case '1': case '2': case '3': case '4':
193 case '5': case '6': case '7': case '8': case '9':
194 if (!tag_found || !reading_number)
195 goto malformed_mnemonic;
197 number[number_p++] = mnemonic[i];
204 goto malformed_mnemonic;
210 suffix[suffix_p++] = mnemonic[i];
212 prefix[prefix_p++] = mnemonic[i];
217 prefix[prefix_p] = '\0';
218 suffix[suffix_p] = '\0';
219 number[number_p] = '\0';
221 if (sscanf (number, "%d", &mask_start) != 1)
222 goto malformed_mnemonic;
226 fprintf (stderr, "Conditional mask not at nibble boundary in: %s\n",
236 ext_table = s390_cond_extensions;
237 ext_table_length = NUM_COND_EXTENSIONS;
240 ext_table = s390_crb_extensions;
241 ext_table_length = NUM_CRB_EXTENSIONS;
243 default: fprintf (stderr, "Unknown tag char: %c\n", *tag);
246 for (i = 0; i < ext_table_length; i++)
248 char new_mnemonic[15];
250 strcpy (new_mnemonic, prefix);
251 opcode[mask_start] = ext_table[i].nibble;
252 strcat (new_mnemonic, ext_table[i].extension);
253 strcat (new_mnemonic, suffix);
254 insertOpcode (opcode, new_mnemonic, format, min_cpu, mode_bits, flags);
259 fprintf (stderr, "Malformed mnemonic: %s\n", mnemonic);
262 static const char file_header[] =
263 "/* The opcode table. This file was generated by s390-mkopc.\n\n"
264 " The format of the opcode table is:\n\n"
265 " NAME OPCODE MASK OPERANDS\n\n"
266 " Name is the name of the instruction.\n"
267 " OPCODE is the instruction opcode.\n"
268 " MASK is the opcode mask; this is used to tell the disassembler\n"
269 " which bits in the actual opcode must match OPCODE.\n"
270 " OPERANDS is the list of operands.\n\n"
271 " The disassembler reads the table in order and prints the first\n"
272 " instruction which matches.\n"
273 " MODE_BITS - zarch or esa\n"
274 " MIN_CPU - number of the min cpu level required\n"
275 " FLAGS - instruction flags. */\n\n"
276 "const struct s390_opcode s390_opcodes[] =\n {\n";
278 /* `dumpTable': write opcode table. */
286 /* Write hash table entries (slots). */
287 printf ("%s", file_header);
289 for (ix = 0; ix < no_ops; ix++)
291 printf (" { \"%s\", ", op_array[ix].mnemonic);
292 for (str = op_array[ix].opcode; *str != 0; str++)
295 printf ("OP%i(0x%sLL), ",
296 op_array[ix].no_nibbles*4, op_array[ix].opcode);
297 printf ("MASK_%s, INSTR_%s, ",
298 op_array[ix].format, op_array[ix].format);
299 printf ("%i, ", op_array[ix].mode_bits);
300 printf ("%i, ", op_array[ix].min_cpu);
301 printf ("%i}", op_array[ix].flags);
308 printf ("const int s390_num_opcodes =\n");
309 printf (" sizeof (s390_opcodes) / sizeof (s390_opcodes[0]);\n\n");
315 char currentLine[256];
319 /* Read opcode descriptions from `stdin'. For each mnemonic,
320 make an entry into the opcode table. */
321 while (fgets (currentLine, sizeof (currentLine), stdin) != NULL)
326 char description[80];
328 char modes_string[16];
329 char flags_string[80];
336 if (currentLine[0] == '#' || currentLine[0] == '\n')
338 memset (opcode, 0, 8);
340 sscanf (currentLine, "%15s %15s %15s \"%79[^\"]\" %15s %15s %79[^\n]",
341 opcode, mnemonic, format, description,
342 cpu_string, modes_string, flags_string);
343 if (num_matched != 6 && num_matched != 7)
345 fprintf (stderr, "Couldn't scan line %s\n", currentLine);
349 if (strcmp (cpu_string, "g5") == 0
350 || strcmp (cpu_string, "arch3") == 0)
351 min_cpu = S390_OPCODE_G5;
352 else if (strcmp (cpu_string, "g6") == 0)
353 min_cpu = S390_OPCODE_G6;
354 else if (strcmp (cpu_string, "z900") == 0
355 || strcmp (cpu_string, "arch5") == 0)
356 min_cpu = S390_OPCODE_Z900;
357 else if (strcmp (cpu_string, "z990") == 0
358 || strcmp (cpu_string, "arch6") == 0)
359 min_cpu = S390_OPCODE_Z990;
360 else if (strcmp (cpu_string, "z9-109") == 0)
361 min_cpu = S390_OPCODE_Z9_109;
362 else if (strcmp (cpu_string, "z9-ec") == 0
363 || strcmp (cpu_string, "arch7") == 0)
364 min_cpu = S390_OPCODE_Z9_EC;
365 else if (strcmp (cpu_string, "z10") == 0
366 || strcmp (cpu_string, "arch8") == 0)
367 min_cpu = S390_OPCODE_Z10;
368 else if (strcmp (cpu_string, "z196") == 0
369 || strcmp (cpu_string, "arch9") == 0)
370 min_cpu = S390_OPCODE_Z196;
371 else if (strcmp (cpu_string, "zEC12") == 0
372 || strcmp (cpu_string, "arch10") == 0)
373 min_cpu = S390_OPCODE_ZEC12;
374 else if (strcmp (cpu_string, "z13") == 0
375 || strcmp (cpu_string, "arch11") == 0)
376 min_cpu = S390_OPCODE_Z13;
377 else if (strcmp (cpu_string, "z14") == 0
378 || strcmp (cpu_string, "arch12") == 0)
379 min_cpu = S390_OPCODE_ARCH12;
381 fprintf (stderr, "Couldn't parse cpu string %s\n", cpu_string);
388 if (strncmp (str, "esa", 3) == 0
389 && (str[3] == 0 || str[3] == ',')) {
390 mode_bits |= 1 << S390_OPCODE_ESA;
392 } else if (strncmp (str, "zarch", 5) == 0
393 && (str[5] == 0 || str[5] == ',')) {
394 mode_bits |= 1 << S390_OPCODE_ZARCH;
397 fprintf (stderr, "Couldn't parse modes string %s\n",
407 if (num_matched == 7)
411 if (strncmp (str, "optparm", 7) == 0
412 && (str[7] == 0 || str[7] == ',')) {
413 flag_bits |= S390_INSTR_FLAG_OPTPARM;
415 } else if (strncmp (str, "optparm2", 8) == 0
416 && (str[8] == 0 || str[8] == ',')) {
417 flag_bits |= S390_INSTR_FLAG_OPTPARM2;
419 } else if (strncmp (str, "htm", 3) == 0
420 && (str[3] == 0 || str[3] == ',')) {
421 flag_bits |= S390_INSTR_FLAG_HTM;
423 } else if (strncmp (str, "vx", 2) == 0
424 && (str[2] == 0 || str[2] == ',')) {
425 flag_bits |= S390_INSTR_FLAG_VX;
428 fprintf (stderr, "Couldn't parse flags string %s\n",
436 insertExpandedMnemonic (opcode, mnemonic, format, min_cpu, mode_bits, flag_bits);