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