bec361f32cb3edb270ee6cdf3261e999a82e5f76
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/>. */
27 static char * line_buf
= NULL
;
28 static int line_buf_size
= 0;
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. */
48 if (fgets (line_buf
, line_buf_size
, f
) == 0)
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. */
63 if (fgets (line_buf
+ line_buf_size
- LBUFINCR
- 1, LBUFINCR
+ 1, f
) == 0)
71 static int errors
= 0;
86 char id
[MAX_BYTES
* 8 + 1];
87 unsigned char var_start
[MAX_BYTES
* 8 + 1];
90 unsigned char decodable_mask
;
91 unsigned char decodable_bits
;
98 struct Indirect
* last_ind
;
114 unsigned char * patterns
;
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
;
137 typedef struct Indirect
142 struct Indirect
* ind
;
147 Indirect indirect
[256];
150 next_varybits (int bits
, opcode
* op
, int byte
)
152 int mask
= op
->b
[byte
].decodable_mask
;
155 for (i
= 0; i
< 8; i
++)
156 if (!(mask
& (1 << i
)))
172 valid_varybits (int bits
, opcode
* op
, int byte
)
178 for (vn
= 0; vn
< op
->nvaries
; vn
++)
185 if (byte
!= op
->vary
[vn
].byte
)
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
);
206 prmb (int mask
, int bits
)
208 static char buf
[8][30];
215 for (i
= 0; i
< 8; i
++)
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
);
245 dump_lines (opcode
* op
, int level
, Indirect
* ind
)
250 if (op
->semantics_label
)
252 printf ("%*sgoto op_semantics_%d;\n", level
, "", op
->semantics_label
);
256 if (ind
!= op
->last_ind
)
258 static int labelno
= 0;
260 printf ("%*sop_semantics_%d:\n", level
, "", labelno
);
261 op
->semantics_label
= labelno
;
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
]))
281 while (op
->id
[i
] && isalpha (op
->id
[i
]))
283 mask
= (mask
<< 1) | 1;
286 if (op
->var_start
[i
])
290 varnames
[vn
++] = strdup (name
);
291 printf ("#line %d \"%s\"\n", op
->lineno
, orig_filename
);
294 fprintf (stderr
, "Error: variable %s spans bytes: %s\n",
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
);
305 printf ("%*s int %s AU = op[%d];\n", level
, "", name
, byte
);
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
++)
319 printf (",\n%*s \"%s\"", level
, "", op
->comment
);
320 for (i
= 0; i
< op
->nbytes
; i
++)
323 printf (",\n%*s op[%d]", level
, "", i
);
325 printf (", op[%d]", i
);
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
, "");
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
]);
343 printf ("%*s}\n", level
, "");
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
))
356 switch (ind
[bits
].type
)
359 if (byte
== op
->dbytes
- 1)
361 ind
[bits
].type
= T_op
;
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
);
379 if (byte
< op
->dbytes
- 1)
380 store_opcode_bits (op
, byte
+ 1, ind
[bits
].u
.ind
);
390 while ((bits
= next_varybits (bits
, op
, byte
)) != 0);
394 emit_indirect (Indirect
* ind
, int byte
)
400 for (j
= 0; j
< 256; j
++)
408 mask
|= ind
[j
].u
.op
->b
[byte
].decodable_mask
;
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)
431 for (n
= j
; n
< 256; n
++)
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, "");
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, "");
451 printf ("%*s default: UNSUPPORTED(); break;\n", byte
* 6, "");
452 printf ("%*s }\n", byte
* 6, "");
456 pv_dup (char * p
, char * ep
)
459 char *rv
= (char *) malloc (n
+ 1);
467 str2mask (char * str
, char * ep
)
469 unsigned char rv
= 0;
482 process_vary (char * line
)
486 Vary
* v
= (Vary
*) malloc (sizeof (Vary
));
490 vary
= (Vary
**) realloc (vary
, n_varies
* sizeof (Vary
*));
492 vary
= (Vary
**) malloc (n_varies
* sizeof (Vary
*));
493 vary
[n_varies
- 1] = v
;
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;
505 v
->patterns
= (unsigned char *) malloc (1);
508 for (cp
= ep
; isspace (*cp
); cp
++);
511 for (ep
= cp
; *ep
&& !isspace (*ep
); ep
++);
513 v
->patterns
= (unsigned char *) realloc (v
->patterns
, v
->n_patterns
);
514 v
->patterns
[v
->n_patterns
- 1] = str2mask (cp
, ep
);
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
]))
530 log_indirect (Indirect
* ind
, int byte
)
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
));
546 if (last_c
&& (ind
[i
].u
.op
->comment
== last_c
))
547 fprintf (sim_log
, "''\n");
549 fprintf (sim_log
, "%s\n", ind
[i
].u
.op
->comment
);
550 last_c
= ind
[i
].u
.op
->comment
;
553 fprintf (sim_log
, "unused\n");
556 fprintf (sim_log
, "indirect\n");
558 log_indirect (ind
[i
].u
.ind
, byte
+ 1);
566 main (int argc
, char ** argv
)
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]);
585 fprintf (stderr
, "usage: opc2c infile.opc > outfile.opc\n");
589 orig_filename
= argv
[1];
590 in
= fopen (argv
[1], "r");
593 fprintf (stderr
, "Unable to open file %s for reading\n", argv
[1]);
594 perror ("The error was");
599 opcodes
= (opcode
**) malloc (sizeof (opcode
*));
602 while ((line
= safe_fgets (in
)) != 0)
605 if (strncmp (line
, "/*?* ", 5) == 0)
607 skipping_section
= 1;
610 if (strncmp (line
, " /** ", 6) == 0
611 && (isdigit (line
[6]) || memcmp (line
+ 6, "VARY", 4) == 0))
613 if (line
[0] == '/' && line
[1] == '*' && line
[2] == '*')
615 skipping_section
= 0;
616 if (strncmp (line
, "/** */", 6) == 0)
621 else if (strncmp (line
, "/** VARY ", 9) == 0)
622 process_vary (line
+ 9);
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]))
651 op
->semantics_label
= 0;
656 for (lp
= line
+ 4; *lp
; lp
++)
661 if (strncmp (lp
, "*/", 2) == 0)
663 else if ((lp
[0] == ' ' && lp
[1] == ' ') || (lp
[0] == '\t'))
665 while (*lp
== ' ' || *lp
== '\t')
667 op
->syntax
= strdup (lp
);
668 lp
= strstr (op
->syntax
, "*/");
672 while ((*lp
== ' ' || *lp
== '\t')
682 if (*lp
== '0' || *lp
== '1')
684 op
->b
[byte
].decodable_mask
|= 1 << bit
;
686 if (op
->dbytes
< byte
+ 1)
687 op
->dbytes
= byte
+ 1;
691 op
->var_start
[i
] = 1;
693 if (op
->dbytes
< byte
+ 1)
694 op
->dbytes
= byte
+ 1;
697 op
->b
[byte
].decodable_bits
|= 1 << bit
;
699 op
->nbytes
= byte
+ 1;
705 else if (!skipping_section
)
710 (char **) realloc (op
->lines
, op
->nlines
* sizeof (char *));
712 op
->lines
= (char **) malloc (op
->nlines
* sizeof (char *));
713 op
->lines
[op
->nlines
- 1] = strdup (line
);
719 for (i
= 0; i
< n_varies
; 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
++)
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
++;
764 (VaryRef
*) realloc (opcodes
[i
]->vary
,
765 (nv
+ 1) * sizeof (VaryRef
));
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
++)
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);
807 log_indirect (indirect
, 0);
This page took 0.055843 seconds and 3 git commands to generate.