2 * Generate opcode table initializers for the in-kernel disassembler.
4 * Copyright IBM Corp. 2017
13 #define STRING_SIZE_MAX 20
22 struct insn_type *type;
23 char opcode[STRING_SIZE_MAX];
24 char name[STRING_SIZE_MAX];
25 char upper[STRING_SIZE_MAX];
26 char format[STRING_SIZE_MAX];
27 unsigned int name_len;
31 struct insn_type *type;
45 struct insn_group *group;
50 * Table of instruction format types. Each opcode is defined with at
51 * least one byte (two nibbles), three nibbles, or two bytes (four
53 * The byte member of each instruction format type entry defines
54 * within which byte of an instruction the third (and fourth) nibble
55 * of an opcode can be found. The mask member is the and-mask that
56 * needs to be applied on this byte in order to get the third (and
57 * fourth) nibble of the opcode.
58 * The format array defines all instruction formats (as defined in the
59 * Principles of Operation) which have the same position of the opcode
61 * A special case are instruction formats with 1-byte opcodes. In this
62 * case the byte member always is zero, so that the mask is applied on
63 * the (only) byte that contains the opcode.
65 static struct insn_type insn_type_table[] = {
69 .format = (char *[]) {
84 .format = (char *[]) {
94 .format = (char *[]) {
109 .format = (char *[]) {
131 static struct insn_type *insn_format_to_type(char *format)
133 char tmp[STRING_SIZE_MAX];
134 char *base_format, **ptr;
139 base_format = strsep(&base_format, "_");
140 for (i = 0; i < sizeof(insn_type_table) / sizeof(insn_type_table[0]); i++) {
141 ptr = insn_type_table[i].format;
143 if (!strcmp(base_format, *ptr))
144 return &insn_type_table[i];
151 static void read_instructions(struct gen_opcode *desc)
157 rc = scanf("%s %s %s", insn.opcode, insn.name, insn.format);
162 insn.type = insn_format_to_type(insn.format);
163 insn.name_len = strlen(insn.name);
164 for (i = 0; i <= insn.name_len; i++)
165 insn.upper[i] = toupper((unsigned char)insn.name[i]);
167 desc->insn = realloc(desc->insn, desc->nr * sizeof(*desc->insn));
170 desc->insn[desc->nr - 1] = insn;
174 static int cmpformat(const void *a, const void *b)
176 return strcmp(((struct insn *)a)->format, ((struct insn *)b)->format);
179 static void print_formats(struct gen_opcode *desc)
184 qsort(desc->insn, desc->nr, sizeof(*desc->insn), cmpformat);
188 for (i = 0; i < desc->nr; i++) {
189 if (!strcmp(format, desc->insn[i].format))
192 format = desc->insn[i].format;
193 printf("\tINSTR_%s,\n", format);
195 printf("}; /* %d */\n\n", count);
198 static int cmp_long_insn(const void *a, const void *b)
200 return strcmp(((struct insn *)a)->name, ((struct insn *)b)->name);
203 static void print_long_insn(struct gen_opcode *desc)
208 qsort(desc->insn, desc->nr, sizeof(*desc->insn), cmp_long_insn);
211 for (i = 0; i < desc->nr; i++) {
212 insn = &desc->insn[i];
213 if (insn->name_len < 6)
215 printf("\tLONG_INSN_%s,\n", insn->upper);
218 printf("}; /* %d */\n\n", count);
220 printf("#define LONG_INSN_INITIALIZER { \\\n");
221 for (i = 0; i < desc->nr; i++) {
222 insn = &desc->insn[i];
223 if (insn->name_len < 6)
225 printf("\t[LONG_INSN_%s] = \"%s\", \\\n", insn->upper, insn->name);
230 static void print_opcode(struct insn *insn, int nr)
234 opcode = insn->opcode;
235 if (insn->type->byte != 0)
237 printf("\t[%4d] = { .opfrag = 0x%s, .format = INSTR_%s, ", nr, opcode, insn->format);
238 if (insn->name_len < 6)
239 printf(".name = \"%s\" ", insn->name);
241 printf(".offset = LONG_INSN_%s ", insn->upper);
245 static void add_to_group(struct gen_opcode *desc, struct insn *insn, int offset)
247 struct insn_group *group;
249 group = desc->group ? &desc->group[desc->nr_groups - 1] : NULL;
250 if (group && (!strncmp(group->opcode, insn->opcode, 2) || group->type->byte == 0)) {
255 desc->group = realloc(desc->group, desc->nr_groups * sizeof(*desc->group));
258 group = &desc->group[desc->nr_groups - 1];
259 strncpy(group->opcode, insn->opcode, 2);
260 group->type = insn->type;
261 group->offset = offset;
265 static int cmpopcode(const void *a, const void *b)
267 return strcmp(((struct insn *)a)->opcode, ((struct insn *)b)->opcode);
270 static void print_opcode_table(struct gen_opcode *desc)
276 qsort(desc->insn, desc->nr, sizeof(*desc->insn), cmpopcode);
277 printf("#define OPCODE_TABLE_INITIALIZER { \\\n");
279 for (i = 0; i < desc->nr; i++) {
280 insn = &desc->insn[i];
281 if (insn->type->byte == 0)
283 add_to_group(desc, insn, offset);
284 if (strncmp(opcode, insn->opcode, 2)) {
285 strncpy(opcode, insn->opcode, 2);
286 printf("\t/* %.2s */ \\\n", opcode);
288 print_opcode(insn, offset);
291 printf("\t/* 1-byte opcode instructions */ \\\n");
292 for (i = 0; i < desc->nr; i++) {
293 insn = &desc->insn[i];
294 if (insn->type->byte != 0)
296 add_to_group(desc, insn, offset);
297 print_opcode(insn, offset);
303 static void print_opcode_table_offsets(struct gen_opcode *desc)
305 struct insn_group *group;
308 printf("#define OPCODE_OFFSET_INITIALIZER { \\\n");
309 for (i = 0; i < desc->nr_groups; i++) {
310 group = &desc->group[i];
311 printf("\t{ .opcode = 0x%.2s, .mask = 0x%02x, .byte = %d, .offset = %d, .count = %d }, \\\n",
312 group->opcode, group->type->mask, group->type->byte, group->offset, group->count);
317 int main(int argc, char **argv)
319 struct gen_opcode _desc = { 0 };
320 struct gen_opcode *desc = &_desc;
322 read_instructions(desc);
323 printf("#ifndef __S390_GENERATED_DIS_H__\n");
324 printf("#define __S390_GENERATED_DIS_H__\n");
326 printf(" * DO NOT MODIFY.\n");
328 printf(" * This file was generated by %s\n", __FILE__);
331 print_long_insn(desc);
332 print_opcode_table(desc);
333 print_opcode_table_offsets(desc);