1 /* opc2c.c --- generate C opcode decoder code from from .opc file
3 Copyright (C) 2005-2016 Free Software Foundation, Inc.
4 Contributed by Red Hat, Inc.
6 This file is part of the GNU opcode library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "libiberty.h"
29 static char * line_buf = NULL;
30 static int line_buf_size = 0;
41 line_buf = (char *) malloc (LBUFINCR);
42 line_buf_size = LBUFINCR;
45 /* Points to last byte. */
46 line_ptr = line_buf + line_buf_size - 1;
48 /* So we can see if fgets put a 0 there. */
50 if (fgets (line_buf, line_buf_size, f) == 0)
53 /* We filled the buffer? */
54 while (line_ptr[0] == 0 && line_ptr[-1] != '\n')
56 /* Make the buffer bigger and read more of the line. */
57 line_buf_size += LBUFINCR;
58 line_buf = (char *) realloc (line_buf, line_buf_size);
60 /* Points to last byte again. */
61 line_ptr = line_buf + line_buf_size - 1;
62 /* So we can see if fgets put a 0 there. */
65 if (fgets (line_buf + line_buf_size - LBUFINCR - 1, LBUFINCR + 1, f) == 0)
73 static int errors = 0;
88 char id[MAX_BYTES * 8 + 1];
89 unsigned char var_start[MAX_BYTES * 8 + 1];
92 unsigned char decodable_mask;
93 unsigned char decodable_bits;
100 struct Indirect * last_ind;
116 unsigned char * patterns;
122 unsigned char cur_bits[MAX_BYTES + 1];
124 const char * orig_filename;
126 FILE * sim_log = NULL;
127 #define lprintf if (sim_log) fprintf
129 opcode prefix_text, suffix_text;
139 typedef struct Indirect
144 struct Indirect * ind;
149 Indirect indirect[256];
152 next_varybits (int bits, opcode * op, int byte)
154 int mask = op->b[byte].decodable_mask;
157 for (i = 0; i < 8; i++)
158 if (!(mask & (1 << i)))
174 valid_varybits (int bits, opcode * op, int byte)
180 for (vn = 0; vn < op->nvaries; vn++)
187 if (byte != op->vary[vn].byte)
189 v = vary[op->vary[vn].varyno];
190 ob = (bits >> op->vary[vn].shift) & v->mask;
191 lprintf (sim_log, "varybits: vary %s ob %x\n", v->name, ob);
193 for (i = 0; i < v->n_patterns; i++)
194 if (ob == v->patterns[i])
196 lprintf (sim_log, " found at %d\n", i);
208 prmb (int mask, int bits)
210 static char buf[8][30];
217 for (i = 0; i < 8; i++)
235 op_cmp (const void *va, const void *vb)
237 const opcode * a = *(const opcode **) va;
238 const opcode * b = *(const opcode **) vb;
240 if (a->nbytes != b->nbytes)
241 return a->nbytes - b->nbytes;
243 return strcmp (a->id, b->id);
247 dump_lines (opcode * op, int level, Indirect * ind)
252 if (op->semantics_label)
254 printf ("%*sgoto op_semantics_%d;\n", level, "", op->semantics_label);
258 if (ind != op->last_ind)
260 static int labelno = 0;
262 printf ("%*sop_semantics_%d:\n", level, "", labelno);
263 op->semantics_label = labelno;
269 printf ("%*s{\n", level, "");
270 printf ("%*s %s\n", level, "", op->comment);
273 for (i = 0; i < op->nbytes * 8;)
275 if (isalpha (op->id[i]))
283 while (op->id[i] && isalpha (op->id[i]))
285 mask = (mask << 1) | 1;
288 if (op->var_start[i])
292 varnames[vn++] = strdup (name);
293 printf ("#line %d \"%s\"\n", op->lineno + 1, orig_filename);
296 fprintf (stderr, "Error: variable %s spans bytes: %s\n",
300 else if (shift && (mask != 0xff))
301 printf ("%*s int %s AU = (op[%d] >> %d) & 0x%02x;\n",
302 level, "", name, byte, shift, mask);
303 else if (mask != 0xff)
304 printf ("%*s int %s AU = op[%d] & 0x%02x;\n",
305 level, "", name, byte, mask);
307 printf ("%*s int %s AU = op[%d];\n", level, "", name, byte);
315 printf ("%*s if (trace)\n", level, "");
316 printf ("%*s {\n", level, "");
317 printf ("%*s printf (\"\\033[33m%%s\\033[0m ", level, "");
318 for (i = 0; i < op->nbytes; i++)
321 printf (",\n%*s \"%s\"", level, "", op->comment);
322 for (i = 0; i < op->nbytes; i++)
325 printf (",\n%*s op[%d]", level, "", i);
327 printf (", op[%d]", i);
330 for (i = 0; i < vn; i++)
331 printf ("%*s printf (\" %s = 0x%%x%s\", %s);\n", level, "",
332 varnames[i], (i < vn - 1) ? "," : "\\n", varnames[i]);
333 printf ("%*s }\n", level, "");
337 printf ("%*s SYNTAX(\"%s\");\n", level, "", op->syntax);
339 printf ("#line %d \"%s\"\n", op->lineno + 1, orig_filename);
341 for (i = 0; i < op->nlines; i++)
342 if (op->lines[i][0] == '\n')
343 printf ("%s", op->lines[i]);
345 printf ("%*s%s", level, "", op->lines[i]);
348 printf ("%*s}\n", level, "");
352 store_opcode_bits (opcode * op, int byte, Indirect * ind)
354 int bits = op->b[byte].decodable_bits;
358 if (!valid_varybits (bits, op, byte))
361 switch (ind[bits].type)
364 if (byte == op->dbytes - 1)
366 ind[bits].type = T_op;
375 ind[bits].type = T_indirect;
376 ind[bits].u.ind = (Indirect *) malloc (256 * sizeof (Indirect));
377 for (i2 = 0; i2 < 256; i2++)
378 ind[bits].u.ind[i2].type = T_unused;
379 store_opcode_bits (op, byte + 1, ind[bits].u.ind);
384 if (byte < op->dbytes - 1)
385 store_opcode_bits (op, byte + 1, ind[bits].u.ind);
395 while ((bits = next_varybits (bits, op, byte)) != 0);
399 emit_indirect (Indirect * ind, int byte)
405 for (j = 0; j < 256; j++)
413 mask |= ind[j].u.op->b[byte].decodable_mask;
421 printf ("%*s GETBYTE ();\n", byte * 6, "");
422 printf ("%*s switch (op[%d] & 0x%02x)\n", byte * 6, "", byte, mask);
423 printf ("%*s {\n", byte * 6, "");
425 for (j = 0; j < 256; j++)
426 if ((j & ~mask) == 0)
436 for (n = j; n < 256; n++)
438 && ind[n].type == T_op && ind[n].u.op == ind[j].u.op)
440 ind[n].type = T_done;
441 printf ("%*s case 0x%02x:\n", byte * 6, "", n);
443 for (n = byte; n < ind[j].u.op->nbytes - 1; n++)
444 printf ("%*s GETBYTE();\n", byte * 6, "");
445 dump_lines (ind[j].u.op, byte * 6 + 6, ind);
446 printf ("%*s break;\n", byte * 6, "");
449 printf ("%*s case 0x%02x:\n", byte * 6, "", j);
450 emit_indirect (ind[j].u.ind, byte + 1);
451 printf ("%*s break;\n", byte * 6, "");
456 printf ("%*s default: UNSUPPORTED(); break;\n", byte * 6, "");
457 printf ("%*s }\n", byte * 6, "");
461 pv_dup (char * p, char * ep)
464 char *rv = (char *) malloc (n + 1);
472 str2mask (char * str, char * ep)
474 unsigned char rv = 0;
487 process_vary (char * line)
491 Vary * v = (Vary *) malloc (sizeof (Vary));
495 vary = (Vary **) realloc (vary, n_varies * sizeof (Vary *));
497 vary = (Vary **) malloc (n_varies * sizeof (Vary *));
498 vary[n_varies - 1] = v;
502 for (cp = line; isspace (*cp); cp++);
503 for (ep = cp; *ep && !isspace (*ep); ep++);
505 v->name = pv_dup (cp, ep);
506 v->nlen = strlen (v->name);
507 v->mask = (1 << v->nlen) - 1;
510 v->patterns = (unsigned char *) malloc (1);
513 for (cp = ep; isspace (*cp); cp++);
516 for (ep = cp; *ep && !isspace (*ep); ep++);
518 v->patterns = (unsigned char *) realloc (v->patterns, v->n_patterns);
519 v->patterns[v->n_patterns - 1] = str2mask (cp, ep);
524 fieldcmp (opcode * op, int bit, char *name)
526 int n = strlen (name);
528 if (memcmp (op->id + bit, name, n) == 0
529 && (!isalpha (op->id[bit + n]) || op->var_start[bit + n]))
535 log_indirect (Indirect * ind, int byte)
540 for (i = 0; i < 256; i++)
543 for (j = 0; j < byte; j++)
544 fprintf (sim_log, "%s ", prmb (255, cur_bits[j]));
545 fprintf (sim_log, "%s ", prmb (255, i));
551 if (last_c && (ind[i].u.op->comment == last_c))
552 fprintf (sim_log, "''\n");
554 fprintf (sim_log, "%s\n", ind[i].u.op->comment);
555 last_c = ind[i].u.op->comment;
558 fprintf (sim_log, "unused\n");
561 fprintf (sim_log, "indirect\n");
563 log_indirect (ind[i].u.ind, byte + 1);
571 main (int argc, char ** argv)
578 int skipping_section = 0;
580 if (argc > 2 && strcmp (argv[1], "-l") == 0)
582 sim_log = fopen (argv[2], "w");
583 fprintf (stderr, "sim_log: %s\n", argv[2]);
590 fprintf (stderr, "usage: opc2c infile.opc > outfile.opc\n");
594 orig_filename = lbasename (argv[1]);
595 in = fopen (argv[1], "r");
598 fprintf (stderr, "Unable to open file %s for reading: %s\n", argv[1],
604 opcodes = (opcode **) malloc (sizeof (opcode *));
607 while ((line = safe_fgets (in)) != 0)
610 if (strncmp (line, "/*?* ", 5) == 0)
612 skipping_section = 1;
615 if (strncmp (line, " /** ", 6) == 0
616 && (isdigit (line[6]) || memcmp (line + 6, "VARY", 4) == 0))
618 if (line[0] == '/' && line[1] == '*' && line[2] == '*')
620 skipping_section = 0;
621 if (strncmp (line, "/** */", 6) == 0)
626 else if (strncmp (line, "/** VARY ", 9) == 0)
627 process_vary (line + 9);
636 (opcode **) realloc (opcodes, n_opcodes * sizeof (opcode *));
637 op = (opcode *) malloc (sizeof (opcode));
638 opcodes[n_opcodes - 1] = op;
640 op->nbytes = op->dbytes = 0;
641 memset (op->id, 0, sizeof (op->id));
642 memset (op->var_start, 0, sizeof (op->var_start));
643 for (i = 0; i < MAX_BYTES; i++)
645 op->b[i].decodable_mask = 0;
646 op->b[i].decodable_bits = 0;
648 op->comment = strdup (line);
649 op->comment[strlen (op->comment) - 1] = 0;
650 while (op->comment[0] && isspace (op->comment[0]))
656 op->semantics_label = 0;
661 for (lp = line + 4; *lp; lp++)
666 if (strncmp (lp, "*/", 2) == 0)
668 else if ((lp[0] == ' ' && lp[1] == ' ') || (lp[0] == '\t'))
670 while (*lp == ' ' || *lp == '\t')
672 op->syntax = strdup (lp);
673 lp = strstr (op->syntax, "*/");
677 while ((*lp == ' ' || *lp == '\t')
687 if (*lp == '0' || *lp == '1')
689 op->b[byte].decodable_mask |= 1 << bit;
691 if (op->dbytes < byte + 1)
692 op->dbytes = byte + 1;
696 op->var_start[i] = 1;
698 if (op->dbytes < byte + 1)
699 op->dbytes = byte + 1;
702 op->b[byte].decodable_bits |= 1 << bit;
704 op->nbytes = byte + 1;
710 else if (!skipping_section)
715 (char **) realloc (op->lines, op->nlines * sizeof (char *));
717 op->lines = (char **) malloc (op->nlines * sizeof (char *));
718 op->lines[op->nlines - 1] = strdup (line);
724 for (i = 0; i < n_varies; i++)
727 lprintf (sim_log, "V[%s] %d\n", v->name, v->nlen);
728 for (j = 0; j < v->n_patterns; j++)
729 lprintf (sim_log, " P %02x\n", v->patterns[j]);
733 for (i = n_opcodes - 2; i >= 0; i--)
735 if (opcodes[i]->nlines == 0)
737 opcodes[i]->nlines = opcodes[i + 1]->nlines;
738 opcodes[i]->lines = opcodes[i + 1]->lines;
742 for (i = 0; i < 256; i++)
743 indirect[i].type = T_unused;
745 qsort (opcodes, n_opcodes, sizeof (opcodes[0]), op_cmp);
747 vlist = (VaryRef *) malloc (n_varies * sizeof (VaryRef));
749 for (i = 0; i < n_opcodes; i++)
753 for (j = 0; j < opcodes[i]->nbytes; j++)
754 lprintf (sim_log, "%s ",
755 prmb (opcodes[i]->b[j].decodable_mask,
756 opcodes[i]->b[j].decodable_bits));
757 lprintf (sim_log, " %s\n", opcodes[i]->comment);
759 for (j = 0; j < opcodes[i]->nbytes; j++)
761 for (b = 0; b < 8; b++)
762 if (isalpha (opcodes[i]->id[j * 8 + b]))
763 for (v = 0; v < n_varies; v++)
764 if (fieldcmp (opcodes[i], j * 8 + b, vary[v]->name))
766 int nv = opcodes[i]->nvaries++;
769 (VaryRef *) realloc (opcodes[i]->vary,
770 (nv + 1) * sizeof (VaryRef));
773 (VaryRef *) malloc ((nv + 1) * sizeof (VaryRef));
775 opcodes[i]->vary[nv].varyno = v;
776 opcodes[i]->vary[nv].byte = j;
777 opcodes[i]->vary[nv].shift = 8 - b - vary[v]->nlen;
778 lprintf (sim_log, "[vary %s shift %d]\n",
779 vary[v]->name, opcodes[i]->vary[nv].shift);
785 for (i = 0; i < n_opcodes; i++)
788 int bytes = opcodes[i]->dbytes;
790 lprintf (sim_log, "\nmask:");
791 for (i2 = 0; i2 < opcodes[i]->nbytes; i2++)
792 lprintf (sim_log, " %02x", opcodes[i]->b[i2].decodable_mask);
793 lprintf (sim_log, "%*s%s\n", 13 - 3 * opcodes[i]->nbytes, "",
794 opcodes[i]->comment);
796 lprintf (sim_log, "bits:");
797 for (i2 = 0; i2 < opcodes[i]->nbytes; i2++)
798 lprintf (sim_log, " %02x", opcodes[i]->b[i2].decodable_bits);
799 lprintf (sim_log, "%*s(%s) %d byte%s\n", 13 - 3 * opcodes[i]->nbytes,
800 "", opcodes[i]->id, bytes, bytes == 1 ? "" : "s");
802 store_opcode_bits (opcodes[i], 0, indirect);
805 dump_lines (&prefix_text, 0, 0);
807 emit_indirect (indirect, 0);
809 dump_lines (&suffix_text, 0, 0);
812 log_indirect (indirect, 0);