s390-mkopc.c revision 1.1 1 1.1 christos /* s390-mkopc.c -- Generates opcode table out of s390-opc.txt
2 1.1 christos Copyright 2000, 2001, 2003, 2005, 2007, 2008 Free Software Foundation, Inc.
3 1.1 christos Contributed by Martin Schwidefsky (schwidefsky (at) de.ibm.com).
4 1.1 christos
5 1.1 christos This file is part of the GNU opcodes library.
6 1.1 christos
7 1.1 christos This library is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3, or (at your option)
10 1.1 christos any later version.
11 1.1 christos
12 1.1 christos It is distributed in the hope that it will be useful, but WITHOUT
13 1.1 christos ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 1.1 christos or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 1.1 christos License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this file; see the file COPYING. If not, write to the
19 1.1 christos Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
20 1.1 christos MA 02110-1301, USA. */
21 1.1 christos
22 1.1 christos #include <stdio.h>
23 1.1 christos #include <stdlib.h>
24 1.1 christos #include <string.h>
25 1.1 christos
26 1.1 christos /* Taken from opcodes/s390.h */
27 1.1 christos enum s390_opcode_mode_val
28 1.1 christos {
29 1.1 christos S390_OPCODE_ESA = 0,
30 1.1 christos S390_OPCODE_ZARCH
31 1.1 christos };
32 1.1 christos
33 1.1 christos enum s390_opcode_cpu_val
34 1.1 christos {
35 1.1 christos S390_OPCODE_G5 = 0,
36 1.1 christos S390_OPCODE_G6,
37 1.1 christos S390_OPCODE_Z900,
38 1.1 christos S390_OPCODE_Z990,
39 1.1 christos S390_OPCODE_Z9_109,
40 1.1 christos S390_OPCODE_Z9_EC,
41 1.1 christos S390_OPCODE_Z10,
42 1.1 christos S390_OPCODE_Z196
43 1.1 christos };
44 1.1 christos
45 1.1 christos struct op_struct
46 1.1 christos {
47 1.1 christos char opcode[16];
48 1.1 christos char mnemonic[16];
49 1.1 christos char format[16];
50 1.1 christos int mode_bits;
51 1.1 christos int min_cpu;
52 1.1 christos
53 1.1 christos unsigned long long sort_value;
54 1.1 christos int no_nibbles;
55 1.1 christos };
56 1.1 christos
57 1.1 christos struct op_struct *op_array;
58 1.1 christos int max_ops;
59 1.1 christos int no_ops;
60 1.1 christos
61 1.1 christos static void
62 1.1 christos createTable (void)
63 1.1 christos {
64 1.1 christos max_ops = 256;
65 1.1 christos op_array = malloc (max_ops * sizeof (struct op_struct));
66 1.1 christos no_ops = 0;
67 1.1 christos }
68 1.1 christos
69 1.1 christos /* `insertOpcode': insert an op_struct into sorted opcode array. */
70 1.1 christos
71 1.1 christos static void
72 1.1 christos insertOpcode (char *opcode, char *mnemonic, char *format,
73 1.1 christos int min_cpu, int mode_bits)
74 1.1 christos {
75 1.1 christos char *str;
76 1.1 christos unsigned long long sort_value;
77 1.1 christos int no_nibbles;
78 1.1 christos int ix, k;
79 1.1 christos
80 1.1 christos while (no_ops >= max_ops)
81 1.1 christos {
82 1.1 christos max_ops = max_ops * 2;
83 1.1 christos op_array = realloc (op_array, max_ops * sizeof (struct op_struct));
84 1.1 christos }
85 1.1 christos
86 1.1 christos sort_value = 0;
87 1.1 christos str = opcode;
88 1.1 christos for (ix = 0; ix < 16; ix++)
89 1.1 christos {
90 1.1 christos if (*str >= '0' && *str <= '9')
91 1.1 christos sort_value = (sort_value << 4) + (*str - '0');
92 1.1 christos else if (*str >= 'a' && *str <= 'f')
93 1.1 christos sort_value = (sort_value << 4) + (*str - 'a' + 10);
94 1.1 christos else if (*str >= 'A' && *str <= 'F')
95 1.1 christos sort_value = (sort_value << 4) + (*str - 'A' + 10);
96 1.1 christos else if (*str == '?')
97 1.1 christos sort_value <<= 4;
98 1.1 christos else
99 1.1 christos break;
100 1.1 christos str ++;
101 1.1 christos }
102 1.1 christos sort_value <<= 4*(16 - ix);
103 1.1 christos sort_value += (min_cpu << 8) + mode_bits;
104 1.1 christos no_nibbles = ix;
105 1.1 christos for (ix = 0; ix < no_ops; ix++)
106 1.1 christos if (sort_value > op_array[ix].sort_value)
107 1.1 christos break;
108 1.1 christos for (k = no_ops; k > ix; k--)
109 1.1 christos op_array[k] = op_array[k-1];
110 1.1 christos strcpy(op_array[ix].opcode, opcode);
111 1.1 christos strcpy(op_array[ix].mnemonic, mnemonic);
112 1.1 christos strcpy(op_array[ix].format, format);
113 1.1 christos op_array[ix].sort_value = sort_value;
114 1.1 christos op_array[ix].no_nibbles = no_nibbles;
115 1.1 christos op_array[ix].min_cpu = min_cpu;
116 1.1 christos op_array[ix].mode_bits = mode_bits;
117 1.1 christos no_ops++;
118 1.1 christos }
119 1.1 christos
120 1.1 christos struct s390_cond_ext_format
121 1.1 christos {
122 1.1 christos char nibble;
123 1.1 christos char extension[4];
124 1.1 christos };
125 1.1 christos
126 1.1 christos /* The mnemonic extensions for conditional jumps used to replace
127 1.1 christos the '*' tag. */
128 1.1 christos #define NUM_COND_EXTENSIONS 20
129 1.1 christos const struct s390_cond_ext_format s390_cond_extensions[NUM_COND_EXTENSIONS] =
130 1.1 christos { { '1', "o" }, /* jump on overflow / if ones */
131 1.1 christos { '2', "h" }, /* jump on A high */
132 1.1 christos { '2', "p" }, /* jump on plus */
133 1.1 christos { '3', "nle" }, /* jump on not low or equal */
134 1.1 christos { '4', "l" }, /* jump on A low */
135 1.1 christos { '4', "m" }, /* jump on minus / if mixed */
136 1.1 christos { '5', "nhe" }, /* jump on not high or equal */
137 1.1 christos { '6', "lh" }, /* jump on low or high */
138 1.1 christos { '7', "ne" }, /* jump on A not equal B */
139 1.1 christos { '7', "nz" }, /* jump on not zero / if not zeros */
140 1.1 christos { '8', "e" }, /* jump on A equal B */
141 1.1 christos { '8', "z" }, /* jump on zero / if zeros */
142 1.1 christos { '9', "nlh" }, /* jump on not low or high */
143 1.1 christos { 'a', "he" }, /* jump on high or equal */
144 1.1 christos { 'b', "nl" }, /* jump on A not low */
145 1.1 christos { 'b', "nm" }, /* jump on not minus / if not mixed */
146 1.1 christos { 'c', "le" }, /* jump on low or equal */
147 1.1 christos { 'd', "nh" }, /* jump on A not high */
148 1.1 christos { 'd', "np" }, /* jump on not plus */
149 1.1 christos { 'e', "no" }, /* jump on not overflow / if not ones */
150 1.1 christos };
151 1.1 christos
152 1.1 christos /* The mnemonic extensions for conditional branches used to replace
153 1.1 christos the '$' tag. */
154 1.1 christos #define NUM_CRB_EXTENSIONS 12
155 1.1 christos const struct s390_cond_ext_format s390_crb_extensions[NUM_CRB_EXTENSIONS] =
156 1.1 christos { { '2', "h" }, /* jump on A high */
157 1.1 christos { '2', "nle" }, /* jump on not low or equal */
158 1.1 christos { '4', "l" }, /* jump on A low */
159 1.1 christos { '4', "nhe" }, /* jump on not high or equal */
160 1.1 christos { '6', "ne" }, /* jump on A not equal B */
161 1.1 christos { '6', "lh" }, /* jump on low or high */
162 1.1 christos { '8', "e" }, /* jump on A equal B */
163 1.1 christos { '8', "nlh" }, /* jump on not low or high */
164 1.1 christos { 'a', "nl" }, /* jump on A not low */
165 1.1 christos { 'a', "he" }, /* jump on high or equal */
166 1.1 christos { 'c', "nh" }, /* jump on A not high */
167 1.1 christos { 'c', "le" }, /* jump on low or equal */
168 1.1 christos };
169 1.1 christos
170 1.1 christos /* As with insertOpcode instructions are added to the sorted opcode
171 1.1 christos array. Additionally mnemonics containing the '*<number>' tag are
172 1.1 christos expanded to the set of conditional instructions described by
173 1.1 christos s390_cond_extensions with the tag replaced by the respective
174 1.1 christos mnemonic extensions. */
175 1.1 christos
176 1.1 christos static void
177 1.1 christos insertExpandedMnemonic (char *opcode, char *mnemonic, char *format,
178 1.1 christos int min_cpu, int mode_bits)
179 1.1 christos {
180 1.1 christos char *tag;
181 1.1 christos char prefix[15];
182 1.1 christos char suffix[15];
183 1.1 christos char number[15];
184 1.1 christos int mask_start, i = 0, tag_found = 0, reading_number = 0;
185 1.1 christos int number_p = 0, suffix_p = 0, prefix_p = 0;
186 1.1 christos const struct s390_cond_ext_format *ext_table;
187 1.1 christos int ext_table_length;
188 1.1 christos
189 1.1 christos if (!(tag = strpbrk (mnemonic, "*$")))
190 1.1 christos {
191 1.1 christos insertOpcode (opcode, mnemonic, format, min_cpu, mode_bits);
192 1.1 christos return;
193 1.1 christos }
194 1.1 christos
195 1.1 christos while (mnemonic[i] != '\0')
196 1.1 christos {
197 1.1 christos if (mnemonic[i] == *tag)
198 1.1 christos {
199 1.1 christos if (tag_found)
200 1.1 christos goto malformed_mnemonic;
201 1.1 christos
202 1.1 christos tag_found = 1;
203 1.1 christos reading_number = 1;
204 1.1 christos }
205 1.1 christos else
206 1.1 christos switch (mnemonic[i])
207 1.1 christos {
208 1.1 christos case '0': case '1': case '2': case '3': case '4':
209 1.1 christos case '5': case '6': case '7': case '8': case '9':
210 1.1 christos if (!tag_found || !reading_number)
211 1.1 christos goto malformed_mnemonic;
212 1.1 christos
213 1.1 christos number[number_p++] = mnemonic[i];
214 1.1 christos break;
215 1.1 christos
216 1.1 christos default:
217 1.1 christos if (reading_number)
218 1.1 christos {
219 1.1 christos if (!number_p)
220 1.1 christos goto malformed_mnemonic;
221 1.1 christos else
222 1.1 christos reading_number = 0;
223 1.1 christos }
224 1.1 christos
225 1.1 christos if (tag_found)
226 1.1 christos suffix[suffix_p++] = mnemonic[i];
227 1.1 christos else
228 1.1 christos prefix[prefix_p++] = mnemonic[i];
229 1.1 christos }
230 1.1 christos i++;
231 1.1 christos }
232 1.1 christos
233 1.1 christos prefix[prefix_p] = '\0';
234 1.1 christos suffix[suffix_p] = '\0';
235 1.1 christos number[number_p] = '\0';
236 1.1 christos
237 1.1 christos if (sscanf (number, "%d", &mask_start) != 1)
238 1.1 christos goto malformed_mnemonic;
239 1.1 christos
240 1.1 christos if (mask_start & 3)
241 1.1 christos {
242 1.1 christos fprintf (stderr, "Conditional mask not at nibble boundary in: %s\n",
243 1.1 christos mnemonic);
244 1.1 christos return;
245 1.1 christos }
246 1.1 christos
247 1.1 christos mask_start >>= 2;
248 1.1 christos
249 1.1 christos switch (*tag)
250 1.1 christos {
251 1.1 christos case '*':
252 1.1 christos ext_table = s390_cond_extensions;
253 1.1 christos ext_table_length = NUM_COND_EXTENSIONS;
254 1.1 christos break;
255 1.1 christos case '$':
256 1.1 christos ext_table = s390_crb_extensions;
257 1.1 christos ext_table_length = NUM_CRB_EXTENSIONS;
258 1.1 christos break;
259 1.1 christos default: fprintf (stderr, "Unknown tag char: %c\n", *tag);
260 1.1 christos }
261 1.1 christos
262 1.1 christos for (i = 0; i < ext_table_length; i++)
263 1.1 christos {
264 1.1 christos char new_mnemonic[15];
265 1.1 christos
266 1.1 christos strcpy (new_mnemonic, prefix);
267 1.1 christos opcode[mask_start] = ext_table[i].nibble;
268 1.1 christos strcat (new_mnemonic, ext_table[i].extension);
269 1.1 christos strcat (new_mnemonic, suffix);
270 1.1 christos insertOpcode (opcode, new_mnemonic, format, min_cpu, mode_bits);
271 1.1 christos }
272 1.1 christos return;
273 1.1 christos
274 1.1 christos malformed_mnemonic:
275 1.1 christos fprintf (stderr, "Malformed mnemonic: %s\n", mnemonic);
276 1.1 christos }
277 1.1 christos
278 1.1 christos static char file_header[] =
279 1.1 christos "/* The opcode table. This file was generated by s390-mkopc.\n\n"
280 1.1 christos " The format of the opcode table is:\n\n"
281 1.1 christos " NAME OPCODE MASK OPERANDS\n\n"
282 1.1 christos " Name is the name of the instruction.\n"
283 1.1 christos " OPCODE is the instruction opcode.\n"
284 1.1 christos " MASK is the opcode mask; this is used to tell the disassembler\n"
285 1.1 christos " which bits in the actual opcode must match OPCODE.\n"
286 1.1 christos " OPERANDS is the list of operands.\n\n"
287 1.1 christos " The disassembler reads the table in order and prints the first\n"
288 1.1 christos " instruction which matches. */\n\n"
289 1.1 christos "const struct s390_opcode s390_opcodes[] =\n {\n";
290 1.1 christos
291 1.1 christos /* `dumpTable': write opcode table. */
292 1.1 christos
293 1.1 christos static void
294 1.1 christos dumpTable (void)
295 1.1 christos {
296 1.1 christos char *str;
297 1.1 christos int ix;
298 1.1 christos
299 1.1 christos /* Write hash table entries (slots). */
300 1.1 christos printf (file_header);
301 1.1 christos
302 1.1 christos for (ix = 0; ix < no_ops; ix++)
303 1.1 christos {
304 1.1 christos printf (" { \"%s\", ", op_array[ix].mnemonic);
305 1.1 christos for (str = op_array[ix].opcode; *str != 0; str++)
306 1.1 christos if (*str == '?')
307 1.1 christos *str = '0';
308 1.1 christos printf ("OP%i(0x%sLL), ",
309 1.1 christos op_array[ix].no_nibbles*4, op_array[ix].opcode);
310 1.1 christos printf ("MASK_%s, INSTR_%s, ",
311 1.1 christos op_array[ix].format, op_array[ix].format);
312 1.1 christos printf ("%i, ", op_array[ix].mode_bits);
313 1.1 christos printf ("%i}", op_array[ix].min_cpu);
314 1.1 christos if (ix < no_ops-1)
315 1.1 christos printf (",\n");
316 1.1 christos else
317 1.1 christos printf ("\n");
318 1.1 christos }
319 1.1 christos printf ("};\n\n");
320 1.1 christos printf ("const int s390_num_opcodes =\n");
321 1.1 christos printf (" sizeof (s390_opcodes) / sizeof (s390_opcodes[0]);\n\n");
322 1.1 christos }
323 1.1 christos
324 1.1 christos int
325 1.1 christos main (void)
326 1.1 christos {
327 1.1 christos char currentLine[256];
328 1.1 christos
329 1.1 christos createTable ();
330 1.1 christos
331 1.1 christos /* Read opcode descriptions from `stdin'. For each mnemonic,
332 1.1 christos make an entry into the opcode table. */
333 1.1 christos while (fgets (currentLine, sizeof (currentLine), stdin) != NULL)
334 1.1 christos {
335 1.1 christos char opcode[16];
336 1.1 christos char mnemonic[16];
337 1.1 christos char format[16];
338 1.1 christos char description[80];
339 1.1 christos char cpu_string[16];
340 1.1 christos char modes_string[16];
341 1.1 christos int min_cpu;
342 1.1 christos int mode_bits;
343 1.1 christos char *str;
344 1.1 christos
345 1.1 christos if (currentLine[0] == '#')
346 1.1 christos continue;
347 1.1 christos memset (opcode, 0, 8);
348 1.1 christos if (sscanf (currentLine, "%15s %15s %15s \"%79[^\"]\" %15s %15s",
349 1.1 christos opcode, mnemonic, format, description,
350 1.1 christos cpu_string, modes_string) == 6)
351 1.1 christos {
352 1.1 christos if (strcmp (cpu_string, "g5") == 0)
353 1.1 christos min_cpu = S390_OPCODE_G5;
354 1.1 christos else if (strcmp (cpu_string, "g6") == 0)
355 1.1 christos min_cpu = S390_OPCODE_G6;
356 1.1 christos else if (strcmp (cpu_string, "z900") == 0)
357 1.1 christos min_cpu = S390_OPCODE_Z900;
358 1.1 christos else if (strcmp (cpu_string, "z990") == 0)
359 1.1 christos min_cpu = S390_OPCODE_Z990;
360 1.1 christos else if (strcmp (cpu_string, "z9-109") == 0)
361 1.1 christos min_cpu = S390_OPCODE_Z9_109;
362 1.1 christos else if (strcmp (cpu_string, "z9-ec") == 0)
363 1.1 christos min_cpu = S390_OPCODE_Z9_EC;
364 1.1 christos else if (strcmp (cpu_string, "z10") == 0)
365 1.1 christos min_cpu = S390_OPCODE_Z10;
366 1.1 christos else if (strcmp (cpu_string, "z196") == 0)
367 1.1 christos min_cpu = S390_OPCODE_Z196;
368 1.1 christos else {
369 1.1 christos fprintf (stderr, "Couldn't parse cpu string %s\n", cpu_string);
370 1.1 christos exit (1);
371 1.1 christos }
372 1.1 christos
373 1.1 christos str = modes_string;
374 1.1 christos mode_bits = 0;
375 1.1 christos do {
376 1.1 christos if (strncmp (str, "esa", 3) == 0
377 1.1 christos && (str[3] == 0 || str[3] == ',')) {
378 1.1 christos mode_bits |= 1 << S390_OPCODE_ESA;
379 1.1 christos str += 3;
380 1.1 christos } else if (strncmp (str, "zarch", 5) == 0
381 1.1 christos && (str[5] == 0 || str[5] == ',')) {
382 1.1 christos mode_bits |= 1 << S390_OPCODE_ZARCH;
383 1.1 christos str += 5;
384 1.1 christos } else {
385 1.1 christos fprintf (stderr, "Couldn't parse modes string %s\n",
386 1.1 christos modes_string);
387 1.1 christos exit (1);
388 1.1 christos }
389 1.1 christos if (*str == ',')
390 1.1 christos str++;
391 1.1 christos } while (*str != 0);
392 1.1 christos
393 1.1 christos insertExpandedMnemonic (opcode, mnemonic, format, min_cpu, mode_bits);
394 1.1 christos }
395 1.1 christos else
396 1.1 christos {
397 1.1 christos fprintf (stderr, "Couldn't scan line %s\n", currentLine);
398 1.1 christos exit (1);
399 1.1 christos }
400 1.1 christos }
401 1.1 christos
402 1.1 christos dumpTable ();
403 1.1 christos return 0;
404 1.1 christos }
405