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