bfd
[binutils.git] / opcodes / opc2c.c
blobbec361f32cb3edb270ee6cdf3261e999a82e5f76
1 /* opc2c.c --- generate C opcode decoder code from from .opc file
3 Copyright (C) 2005, 2007, 2008, 2009 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/>. */
22 #include <stdio.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <stdlib.h>
27 static char * line_buf = NULL;
28 static int line_buf_size = 0;
30 #define LBUFINCR 100
32 char *
33 safe_fgets (FILE * f)
35 char * line_ptr;
37 if (line_buf == NULL)
39 line_buf = (char *) malloc (LBUFINCR);
40 line_buf_size = LBUFINCR;
43 /* Points to last byte. */
44 line_ptr = line_buf + line_buf_size - 1;
46 /* So we can see if fgets put a 0 there. */
47 *line_ptr = 1;
48 if (fgets (line_buf, line_buf_size, f) == 0)
49 return NULL;
51 /* We filled the buffer? */
52 while (line_ptr[0] == 0 && line_ptr[-1] != '\n')
54 /* Make the buffer bigger and read more of the line. */
55 line_buf_size += LBUFINCR;
56 line_buf = (char *) realloc (line_buf, line_buf_size);
58 /* Points to last byte again. */
59 line_ptr = line_buf + line_buf_size - 1;
60 /* So we can see if fgets put a 0 there. */
61 *line_ptr = 1;
63 if (fgets (line_buf + line_buf_size - LBUFINCR - 1, LBUFINCR + 1, f) == 0)
64 return NULL;
67 return line_buf;
71 static int errors = 0;
73 #define MAX_BYTES 10
75 typedef struct
77 int varyno:16;
78 int byte:8;
79 int shift:8;
80 } VaryRef;
82 typedef struct
84 char nbytes;
85 char dbytes;
86 char id[MAX_BYTES * 8 + 1];
87 unsigned char var_start[MAX_BYTES * 8 + 1];
88 struct
90 unsigned char decodable_mask;
91 unsigned char decodable_bits;
92 } b[MAX_BYTES];
93 char * comment;
94 char * syntax;
95 int lineno;
96 int nlines;
97 char ** lines;
98 struct Indirect * last_ind;
99 int semantics_label;
100 int nvaries;
101 VaryRef * vary;
102 } opcode;
104 int n_opcodes;
105 opcode ** opcodes;
106 opcode * op;
108 typedef struct
110 char * name;
111 int nlen;
112 unsigned char mask;
113 int n_patterns;
114 unsigned char * patterns;
115 } Vary;
117 Vary ** vary = 0;
118 int n_varies = 0;
120 unsigned char cur_bits[MAX_BYTES + 1];
122 char * orig_filename;
124 FILE * sim_log = NULL;
125 #define lprintf if (sim_log) fprintf
127 opcode prefix_text, suffix_text;
129 typedef enum
131 T_unused,
132 T_op,
133 T_indirect,
134 T_done
135 } OpType;
137 typedef struct Indirect
139 OpType type;
140 union
142 struct Indirect * ind;
143 opcode * op;
144 } u;
145 } Indirect;
147 Indirect indirect[256];
149 static int
150 next_varybits (int bits, opcode * op, int byte)
152 int mask = op->b[byte].decodable_mask;
153 int i;
155 for (i = 0; i < 8; i++)
156 if (!(mask & (1 << i)))
158 if (bits & (1 << i))
160 bits &= ~(1 << i);
162 else
164 bits |= (1 << i);
165 return bits;
168 return 0;
171 static int
172 valid_varybits (int bits, opcode * op, int byte)
174 if (op->nvaries)
176 int vn;
178 for (vn = 0; vn < op->nvaries; vn++)
180 Vary * v;
181 int found = 0;
182 int i;
183 int ob;
185 if (byte != op->vary[vn].byte)
186 continue;
187 v = vary[op->vary[vn].varyno];
188 ob = (bits >> op->vary[vn].shift) & v->mask;
189 lprintf (sim_log, "varybits: vary %s ob %x\n", v->name, ob);
191 for (i = 0; i < v->n_patterns; i++)
192 if (ob == v->patterns[i])
194 lprintf (sim_log, " found at %d\n", i);
195 found = 1;
196 break;
198 if (!found)
199 return 0;
202 return 1;
205 char *
206 prmb (int mask, int bits)
208 static char buf[8][30];
209 static int bn = 0;
210 char * bp;
212 bn = (bn + 1) % 8;
213 bp = buf[bn];
214 int i;
215 for (i = 0; i < 8; i++)
217 int bit = 0x80 >> i;
219 if (!(mask & bit))
220 *bp++ = '-';
221 else if (bits & bit)
222 *bp++ = '1';
223 else
224 *bp++ = '0';
225 if (i % 4 == 3)
226 *bp++ = ' ';
228 *--bp = 0;
229 return buf[bn];
232 static int
233 op_cmp (const void *va, const void *vb)
235 const opcode * a = *(const opcode **) va;
236 const opcode * b = *(const opcode **) vb;
238 if (a->nbytes != b->nbytes)
239 return a->nbytes - b->nbytes;
241 return strcmp (a->id, b->id);
244 void
245 dump_lines (opcode * op, int level, Indirect * ind)
247 char * varnames[40];
248 int i, vn = 0;
250 if (op->semantics_label)
252 printf ("%*sgoto op_semantics_%d;\n", level, "", op->semantics_label);
253 return;
256 if (ind != op->last_ind)
258 static int labelno = 0;
259 labelno++;
260 printf ("%*sop_semantics_%d:\n", level, "", labelno);
261 op->semantics_label = labelno;
264 if (op->comment)
266 level += 2;
267 printf ("%*s{\n", level, "");
268 printf ("%*s %s\n", level, "", op->comment);
271 for (i = 0; i < op->nbytes * 8;)
273 if (isalpha (op->id[i]))
275 int byte = i >> 3;
276 int mask = 0;
277 int shift = 0;
278 char name[33];
279 char * np = name;
281 while (op->id[i] && isalpha (op->id[i]))
283 mask = (mask << 1) | 1;
284 shift = 7 - (i & 7);
285 *np++ = op->id[i++];
286 if (op->var_start[i])
287 break;
289 *np = 0;
290 varnames[vn++] = strdup (name);
291 printf ("#line %d \"%s\"\n", op->lineno, orig_filename);
292 if (mask & ~0xff)
294 fprintf (stderr, "Error: variable %s spans bytes: %s\n",
295 name, op->comment);
296 errors++;
298 else if (shift && (mask != 0xff))
299 printf ("%*s int %s AU = (op[%d] >> %d) & 0x%02x;\n",
300 level, "", name, byte, shift, mask);
301 else if (mask != 0xff)
302 printf ("%*s int %s AU = op[%d] & 0x%02x;\n",
303 level, "", name, byte, mask);
304 else
305 printf ("%*s int %s AU = op[%d];\n", level, "", name, byte);
307 else
308 i++;
311 if (op->comment)
313 printf ("%*s if (trace)\n", level, "");
314 printf ("%*s {\n", level, "");
315 printf ("%*s printf (\"\\033[33m%%s\\033[0m ", level, "");
316 for (i = 0; i < op->nbytes; i++)
317 printf (" %%02x");
318 printf ("\\n\"");
319 printf (",\n%*s \"%s\"", level, "", op->comment);
320 for (i = 0; i < op->nbytes; i++)
322 if (i == 0)
323 printf (",\n%*s op[%d]", level, "", i);
324 else
325 printf (", op[%d]", i);
327 printf (");\n");
328 for (i = 0; i < vn; i++)
329 printf ("%*s printf (\" %s = 0x%%x%s\", %s);\n", level, "",
330 varnames[i], (i < vn - 1) ? "," : "\\n", varnames[i]);
331 printf ("%*s }\n", level, "");
334 if (op->syntax)
335 printf ("%*s SYNTAX(\"%s\");\n", level, "", op->syntax);
337 printf ("#line %d \"%s\"\n", op->lineno + 1, orig_filename);
339 for (i = 0; i < op->nlines; i++)
340 printf ("%*s%s", level, "", op->lines[i]);
342 if (op->comment)
343 printf ("%*s}\n", level, "");
346 void
347 store_opcode_bits (opcode * op, int byte, Indirect * ind)
349 int bits = op->b[byte].decodable_bits;
353 if (!valid_varybits (bits, op, byte))
354 continue;
356 switch (ind[bits].type)
358 case T_unused:
359 if (byte == op->dbytes - 1)
361 ind[bits].type = T_op;
362 ind[bits].u.op = op;
363 op->last_ind = ind;
364 break;
366 else
368 int i2;
370 ind[bits].type = T_indirect;
371 ind[bits].u.ind = (Indirect *) malloc (256 * sizeof (Indirect));
372 for (i2 = 0; i2 < 256; i2++)
373 ind[bits].u.ind[i2].type = T_unused;
374 store_opcode_bits (op, byte + 1, ind[bits].u.ind);
376 break;
378 case T_indirect:
379 if (byte < op->dbytes - 1)
380 store_opcode_bits (op, byte + 1, ind[bits].u.ind);
381 break;
383 case T_op:
384 break;
386 case T_done:
387 break;
390 while ((bits = next_varybits (bits, op, byte)) != 0);
393 void
394 emit_indirect (Indirect * ind, int byte)
396 int unsup = 0;
397 int j, n, mask;
399 mask = 0;
400 for (j = 0; j < 256; j++)
402 switch (ind[j].type)
404 case T_indirect:
405 mask = 0xff;
406 break;
407 case T_op:
408 mask |= ind[j].u.op->b[byte].decodable_mask;
409 break;
410 case T_done:
411 case T_unused:
412 break;
416 printf ("%*s GETBYTE ();\n", byte * 6, "");
417 printf ("%*s switch (op[%d] & 0x%02x)\n", byte * 6, "", byte, mask);
418 printf ("%*s {\n", byte * 6, "");
420 for (j = 0; j < 256; j++)
421 if ((j & ~mask) == 0)
423 switch (ind[j].type)
425 case T_done:
426 break;
427 case T_unused:
428 unsup = 1;
429 break;
430 case T_op:
431 for (n = j; n < 256; n++)
432 if ((n & ~mask) == 0
433 && ind[n].type == T_op && ind[n].u.op == ind[j].u.op)
435 ind[n].type = T_done;
436 printf ("%*s case 0x%02x:\n", byte * 6, "", n);
438 for (n = byte; n < ind[j].u.op->nbytes - 1; n++)
439 printf ("%*s GETBYTE();\n", byte * 6, "");
440 dump_lines (ind[j].u.op, byte * 6 + 6, ind);
441 printf ("%*s break;\n", byte * 6, "");
442 break;
443 case T_indirect:
444 printf ("%*s case 0x%02x:\n", byte * 6, "", j);
445 emit_indirect (ind[j].u.ind, byte + 1);
446 printf ("%*s break;\n", byte * 6, "");
447 break;
450 if (unsup)
451 printf ("%*s default: UNSUPPORTED(); break;\n", byte * 6, "");
452 printf ("%*s }\n", byte * 6, "");
455 static char *
456 pv_dup (char * p, char * ep)
458 int n = ep - p;
459 char *rv = (char *) malloc (n + 1);
461 memcpy (rv, p, n);
462 rv[n] = 0;
463 return rv;
466 static unsigned char
467 str2mask (char * str, char * ep)
469 unsigned char rv = 0;
471 while (str < ep)
473 rv *= 2;
474 if (*str == '1')
475 rv += 1;
476 str++;
478 return rv;
481 static void
482 process_vary (char * line)
484 char * cp;
485 char * ep;
486 Vary * v = (Vary *) malloc (sizeof (Vary));
488 n_varies++;
489 if (vary)
490 vary = (Vary **) realloc (vary, n_varies * sizeof (Vary *));
491 else
492 vary = (Vary **) malloc (n_varies * sizeof (Vary *));
493 vary[n_varies - 1] = v;
495 cp = line;
497 for (cp = line; isspace (*cp); cp++);
498 for (ep = cp; *ep && !isspace (*ep); ep++);
500 v->name = pv_dup (cp, ep);
501 v->nlen = strlen (v->name);
502 v->mask = (1 << v->nlen) - 1;
504 v->n_patterns = 0;
505 v->patterns = (unsigned char *) malloc (1);
506 while (1)
508 for (cp = ep; isspace (*cp); cp++);
509 if (!isdigit (*cp))
510 break;
511 for (ep = cp; *ep && !isspace (*ep); ep++);
512 v->n_patterns++;
513 v->patterns = (unsigned char *) realloc (v->patterns, v->n_patterns);
514 v->patterns[v->n_patterns - 1] = str2mask (cp, ep);
518 static int
519 fieldcmp (opcode * op, int bit, char *name)
521 int n = strlen (name);
523 if (memcmp (op->id + bit, name, n) == 0
524 && (!isalpha (op->id[bit + n]) || op->var_start[bit + n]))
525 return 1;
526 return 0;
529 static void
530 log_indirect (Indirect * ind, int byte)
532 int i, j;
533 char * last_c = 0;
535 for (i = 0; i < 256; i++)
538 for (j = 0; j < byte; j++)
539 fprintf (sim_log, "%s ", prmb (255, cur_bits[j]));
540 fprintf (sim_log, "%s ", prmb (255, i));
542 switch (ind[i].type)
544 case T_op:
545 case T_done:
546 if (last_c && (ind[i].u.op->comment == last_c))
547 fprintf (sim_log, "''\n");
548 else
549 fprintf (sim_log, "%s\n", ind[i].u.op->comment);
550 last_c = ind[i].u.op->comment;
551 break;
552 case T_unused:
553 fprintf (sim_log, "unused\n");
554 break;
555 case T_indirect:
556 fprintf (sim_log, "indirect\n");
557 cur_bits[byte] = i;
558 log_indirect (ind[i].u.ind, byte + 1);
559 last_c = 0;
560 break;
566 main (int argc, char ** argv)
568 char * line;
569 FILE * in;
570 int lineno = 0;
571 int i;
572 VaryRef * vlist;
573 int skipping_section = 0;
575 if (argc > 2 && strcmp (argv[1], "-l") == 0)
577 sim_log = fopen (argv[2], "w");
578 fprintf (stderr, "sim_log: %s\n", argv[2]);
579 argc -= 2;
580 argv += 2;
583 if (argc < 2)
585 fprintf (stderr, "usage: opc2c infile.opc > outfile.opc\n");
586 exit (1);
589 orig_filename = argv[1];
590 in = fopen (argv[1], "r");
591 if (!in)
593 fprintf (stderr, "Unable to open file %s for reading\n", argv[1]);
594 perror ("The error was");
595 exit (1);
598 n_opcodes = 0;
599 opcodes = (opcode **) malloc (sizeof (opcode *));
600 op = &prefix_text;
601 op->lineno = 1;
602 while ((line = safe_fgets (in)) != 0)
604 lineno++;
605 if (strncmp (line, "/*?* ", 5) == 0)
607 skipping_section = 1;
608 continue;
610 if (strncmp (line, " /** ", 6) == 0
611 && (isdigit (line[6]) || memcmp (line + 6, "VARY", 4) == 0))
612 line += 2;
613 if (line[0] == '/' && line[1] == '*' && line[2] == '*')
615 skipping_section = 0;
616 if (strncmp (line, "/** */", 6) == 0)
618 op = &suffix_text;
619 op->lineno = lineno;
621 else if (strncmp (line, "/** VARY ", 9) == 0)
622 process_vary (line + 9);
623 else
625 char * lp;
626 int i, bit, byte;
627 int var_start = 1;
629 n_opcodes++;
630 opcodes =
631 (opcode **) realloc (opcodes, n_opcodes * sizeof (opcode *));
632 op = (opcode *) malloc (sizeof (opcode));
633 opcodes[n_opcodes - 1] = op;
635 op->nbytes = op->dbytes = 0;
636 memset (op->id, 0, sizeof (op->id));
637 memset (op->var_start, 0, sizeof (op->var_start));
638 for (i = 0; i < MAX_BYTES; i++)
640 op->b[i].decodable_mask = 0;
641 op->b[i].decodable_bits = 0;
643 op->comment = strdup (line);
644 op->comment[strlen (op->comment) - 1] = 0;
645 while (op->comment[0] && isspace (op->comment[0]))
646 op->comment++;
647 op->lineno = lineno;
648 op->nlines = 0;
649 op->lines = 0;
650 op->last_ind = 0;
651 op->semantics_label = 0;
652 op->nvaries = 0;
653 op->vary = 0;
655 i = 0;
656 for (lp = line + 4; *lp; lp++)
658 bit = 7 - (i & 7);
659 byte = i >> 3;
661 if (strncmp (lp, "*/", 2) == 0)
662 break;
663 else if ((lp[0] == ' ' && lp[1] == ' ') || (lp[0] == '\t'))
665 while (*lp == ' ' || *lp == '\t')
666 lp ++;
667 op->syntax = strdup (lp);
668 lp = strstr (op->syntax, "*/");
669 if (lp)
671 *lp-- = 0;
672 while ((*lp == ' ' || *lp == '\t')
673 && lp > op->syntax)
674 *lp-- = 0;
676 break;
678 else if (*lp == ' ')
679 var_start = 1;
680 else
682 if (*lp == '0' || *lp == '1')
684 op->b[byte].decodable_mask |= 1 << bit;
685 var_start = 1;
686 if (op->dbytes < byte + 1)
687 op->dbytes = byte + 1;
689 else if (var_start)
691 op->var_start[i] = 1;
692 var_start = 0;
693 if (op->dbytes < byte + 1)
694 op->dbytes = byte + 1;
696 if (*lp == '1')
697 op->b[byte].decodable_bits |= 1 << bit;
699 op->nbytes = byte + 1;
700 op->id[i++] = *lp;
705 else if (!skipping_section)
707 op->nlines++;
708 if (op->lines)
709 op->lines =
710 (char **) realloc (op->lines, op->nlines * sizeof (char *));
711 else
712 op->lines = (char **) malloc (op->nlines * sizeof (char *));
713 op->lines[op->nlines - 1] = strdup (line);
718 int i, j;
719 for (i = 0; i < n_varies; i++)
721 Vary *v = vary[i];
722 lprintf (sim_log, "V[%s] %d\n", v->name, v->nlen);
723 for (j = 0; j < v->n_patterns; j++)
724 lprintf (sim_log, " P %02x\n", v->patterns[j]);
728 for (i = n_opcodes - 2; i >= 0; i--)
730 if (opcodes[i]->nlines == 0)
732 opcodes[i]->nlines = opcodes[i + 1]->nlines;
733 opcodes[i]->lines = opcodes[i + 1]->lines;
737 for (i = 0; i < 256; i++)
738 indirect[i].type = T_unused;
740 qsort (opcodes, n_opcodes, sizeof (opcodes[0]), op_cmp);
742 vlist = (VaryRef *) malloc (n_varies * sizeof (VaryRef));
744 for (i = 0; i < n_opcodes; i++)
746 int j, b, v;
748 for (j = 0; j < opcodes[i]->nbytes; j++)
749 lprintf (sim_log, "%s ",
750 prmb (opcodes[i]->b[j].decodable_mask,
751 opcodes[i]->b[j].decodable_bits));
752 lprintf (sim_log, " %s\n", opcodes[i]->comment);
754 for (j = 0; j < opcodes[i]->nbytes; j++)
756 for (b = 0; b < 8; b++)
757 if (isalpha (opcodes[i]->id[j * 8 + b]))
758 for (v = 0; v < n_varies; v++)
759 if (fieldcmp (opcodes[i], j * 8 + b, vary[v]->name))
761 int nv = opcodes[i]->nvaries++;
762 if (nv)
763 opcodes[i]->vary =
764 (VaryRef *) realloc (opcodes[i]->vary,
765 (nv + 1) * sizeof (VaryRef));
766 else
767 opcodes[i]->vary =
768 (VaryRef *) malloc ((nv + 1) * sizeof (VaryRef));
770 opcodes[i]->vary[nv].varyno = v;
771 opcodes[i]->vary[nv].byte = j;
772 opcodes[i]->vary[nv].shift = 8 - b - vary[v]->nlen;
773 lprintf (sim_log, "[vary %s shift %d]\n",
774 vary[v]->name, opcodes[i]->vary[nv].shift);
780 for (i = 0; i < n_opcodes; i++)
782 int i2;
783 int bytes = opcodes[i]->dbytes;
785 lprintf (sim_log, "\nmask:");
786 for (i2 = 0; i2 < opcodes[i]->nbytes; i2++)
787 lprintf (sim_log, " %02x", opcodes[i]->b[i2].decodable_mask);
788 lprintf (sim_log, "%*s%s\n", 13 - 3 * opcodes[i]->nbytes, "",
789 opcodes[i]->comment);
791 lprintf (sim_log, "bits:");
792 for (i2 = 0; i2 < opcodes[i]->nbytes; i2++)
793 lprintf (sim_log, " %02x", opcodes[i]->b[i2].decodable_bits);
794 lprintf (sim_log, "%*s(%s) %d byte%s\n", 13 - 3 * opcodes[i]->nbytes,
795 "", opcodes[i]->id, bytes, bytes == 1 ? "" : "s");
797 store_opcode_bits (opcodes[i], 0, indirect);
800 dump_lines (&prefix_text, 0, 0);
802 emit_indirect (indirect, 0);
804 dump_lines (&suffix_text, 0, 0);
806 if (sim_log)
807 log_indirect (indirect, 0);
809 return errors;