OSDN Git Service

bfd
[pf3gnuchains/pf3gnuchains3x.git] / opcodes / opc2c.c
1 /* opc2c.c --- generate C opcode decoder code from from .opc file
2
3    Copyright (C) 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4    Contributed by Red Hat, Inc.
5
6    This file is part of the GNU opcode library.
7
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.
12
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.
17
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/>.  */
20
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <stdlib.h>
26
27 static char * line_buf = NULL;
28 static int line_buf_size = 0;
29
30 #define LBUFINCR 100
31
32 char *
33 safe_fgets (FILE * f)
34 {
35   char * line_ptr;
36
37   if (line_buf == NULL)
38     {
39       line_buf = (char *) malloc (LBUFINCR);
40       line_buf_size = LBUFINCR;
41     }
42
43   /* Points to last byte.  */
44   line_ptr = line_buf + line_buf_size - 1;
45
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;
50
51   /* We filled the buffer?  */
52   while (line_ptr[0] == 0 && line_ptr[-1] != '\n')
53     {
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);
57
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;
62
63       if (fgets (line_buf + line_buf_size - LBUFINCR - 1, LBUFINCR + 1, f) == 0)
64         return NULL;
65     }
66
67   return line_buf;
68 }
69
70
71 static int errors = 0;
72
73 #define MAX_BYTES 10
74
75 typedef struct
76 {
77   int varyno:16;
78   int byte:8;
79   int shift:8;
80 } VaryRef;
81
82 typedef struct
83 {
84   char nbytes;
85   char dbytes;
86   char id[MAX_BYTES * 8 + 1];
87   unsigned char var_start[MAX_BYTES * 8 + 1];
88   struct
89   {
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;
103
104 int n_opcodes;
105 opcode ** opcodes;
106 opcode * op;
107
108 typedef struct
109 {
110   char * name;
111   int nlen;
112   unsigned char mask;
113   int n_patterns;
114   unsigned char * patterns;
115 } Vary;
116
117 Vary ** vary = 0;
118 int n_varies = 0;
119
120 unsigned char cur_bits[MAX_BYTES + 1];
121
122 char * orig_filename;
123
124 FILE * sim_log = NULL;
125 #define lprintf if (sim_log) fprintf
126
127 opcode prefix_text, suffix_text;
128
129 typedef enum
130 {
131   T_unused,
132   T_op,
133   T_indirect,
134   T_done
135 } OpType;
136
137 typedef struct Indirect
138 {
139   OpType type;
140   union
141   {
142     struct Indirect * ind;
143     opcode * op;
144   } u;
145 } Indirect;
146
147 Indirect indirect[256];
148
149 static int
150 next_varybits (int bits, opcode * op, int byte)
151 {
152   int mask = op->b[byte].decodable_mask;
153   int i;
154
155   for (i = 0; i < 8; i++)
156     if (!(mask & (1 << i)))
157       {
158         if (bits & (1 << i))
159           {
160             bits &= ~(1 << i);
161           }
162         else
163           {
164             bits |= (1 << i);
165             return bits;
166           }
167       }
168   return 0;
169 }
170
171 static int
172 valid_varybits (int bits, opcode * op, int byte)
173 {
174   if (op->nvaries)
175     {
176       int vn;
177
178       for (vn = 0; vn < op->nvaries; vn++)
179         {
180           Vary * v;
181           int found = 0;
182           int i;
183           int ob;
184
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);
190
191           for (i = 0; i < v->n_patterns; i++)
192             if (ob == v->patterns[i])
193               {
194                 lprintf (sim_log, "  found at %d\n", i);
195                 found = 1;
196                 break;
197               }
198           if (!found)
199             return 0;
200         }
201     }
202   return 1;
203 }
204
205 char *
206 prmb (int mask, int bits)
207 {
208   static char buf[8][30];
209   static int bn = 0;
210   char * bp;
211
212   bn = (bn + 1) % 8;
213   bp = buf[bn];
214   int i;
215   for (i = 0; i < 8; i++)
216     {
217       int bit = 0x80 >> i;
218
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++ = ' ';
227     }
228   *--bp = 0;
229   return buf[bn];
230 }
231
232 static int
233 op_cmp (const void *va, const void *vb)
234 {
235   const opcode * a = *(const opcode **) va;
236   const opcode * b = *(const opcode **) vb;
237
238   if (a->nbytes != b->nbytes)
239     return a->nbytes - b->nbytes;
240
241   return strcmp (a->id, b->id);
242 }
243
244 void
245 dump_lines (opcode * op, int level, Indirect * ind)
246 {
247   char * varnames[40];
248   int i, vn = 0;
249
250   if (op->semantics_label)
251     {
252       printf ("%*sgoto op_semantics_%d;\n", level, "", op->semantics_label);
253       return;
254     }
255
256   if (ind != op->last_ind)
257     {
258       static int labelno = 0;
259       labelno++;
260       printf ("%*sop_semantics_%d:\n", level, "", labelno);
261       op->semantics_label = labelno;
262     }
263
264   if (op->comment)
265     {
266       level += 2;
267       printf ("%*s{\n", level, "");
268       printf ("%*s  %s\n", level, "", op->comment);
269     }
270
271   for (i = 0; i < op->nbytes * 8;)
272     {
273       if (isalpha (op->id[i]))
274         {
275           int byte = i >> 3;
276           int mask = 0;
277           int shift = 0;
278           char name[33];
279           char * np = name;
280
281           while (op->id[i] && isalpha (op->id[i]))
282             {
283               mask = (mask << 1) | 1;
284               shift = 7 - (i & 7);
285               *np++ = op->id[i++];
286               if (op->var_start[i])
287                 break;
288             }
289           *np = 0;
290           varnames[vn++] = strdup (name);
291           printf ("#line %d \"%s\"\n", op->lineno, orig_filename);
292           if (mask & ~0xff)
293             {
294               fprintf (stderr, "Error: variable %s spans bytes: %s\n",
295                        name, op->comment);
296               errors++;
297             }
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);
306         }
307       else
308         i++;
309     }
310
311   if (op->comment)
312     {
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++)
321         {
322           if (i == 0)
323             printf (",\n%*s             op[%d]", level, "", i);
324           else
325             printf (", op[%d]", i);
326         }
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, "");
332     }
333
334   if (op->syntax)
335     printf ("%*s  SYNTAX(\"%s\");\n", level, "", op->syntax);
336
337   printf ("#line %d \"%s\"\n", op->lineno + 1, orig_filename);
338
339   for (i = 0; i < op->nlines; i++)
340     printf ("%*s%s", level, "", op->lines[i]);
341
342   if (op->comment)
343     printf ("%*s}\n", level, "");
344 }
345
346 void
347 store_opcode_bits (opcode * op, int byte, Indirect * ind)
348 {
349   int bits = op->b[byte].decodable_bits;
350
351   do
352     {
353       if (!valid_varybits (bits, op, byte))
354         continue;
355
356       switch (ind[bits].type)
357         {
358         case T_unused:
359           if (byte == op->dbytes - 1)
360             {
361               ind[bits].type = T_op;
362               ind[bits].u.op = op;
363               op->last_ind = ind;
364               break;
365             }
366           else
367             {
368               int i2;
369
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);
375             }
376           break;
377
378         case T_indirect:
379           if (byte < op->dbytes - 1)
380             store_opcode_bits (op, byte + 1, ind[bits].u.ind);
381           break;
382
383         case T_op:
384           break;
385
386         case T_done:
387           break;
388         }
389     }
390   while ((bits = next_varybits (bits, op, byte)) != 0);
391 }
392
393 void
394 emit_indirect (Indirect * ind, int byte)
395 {
396   int unsup = 0;
397   int j, n, mask;
398
399   mask = 0;
400   for (j = 0; j < 256; j++)
401     {
402       switch (ind[j].type)
403         {
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;
413         }
414     }
415
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, "");
419
420   for (j = 0; j < 256; j++)
421     if ((j & ~mask) == 0)
422       {
423         switch (ind[j].type)
424           {
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)
434                 {
435                   ind[n].type = T_done;
436                   printf ("%*s    case 0x%02x:\n", byte * 6, "", n);
437                 }
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;
448           }
449       }
450   if (unsup)
451     printf ("%*s    default: UNSUPPORTED(); break;\n", byte * 6, "");
452   printf ("%*s  }\n", byte * 6, "");
453 }
454
455 static char *
456 pv_dup (char * p, char * ep)
457 {
458   int n = ep - p;
459   char *rv = (char *) malloc (n + 1);
460
461   memcpy (rv, p, n);
462   rv[n] = 0;
463   return rv;
464 }
465
466 static unsigned char
467 str2mask (char * str, char * ep)
468 {
469   unsigned char rv = 0;
470
471   while (str < ep)
472     {
473       rv *= 2;
474       if (*str == '1')
475         rv += 1;
476       str++;
477     }
478   return rv;
479 }
480
481 static void
482 process_vary (char * line)
483 {
484   char * cp;
485   char * ep;
486   Vary * v = (Vary *) malloc (sizeof (Vary));
487
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;
494
495   cp = line;
496
497   for (cp = line; isspace (*cp); cp++);
498   for (ep = cp; *ep && !isspace (*ep); ep++);
499
500   v->name = pv_dup (cp, ep);
501   v->nlen = strlen (v->name);
502   v->mask = (1 << v->nlen) - 1;
503
504   v->n_patterns = 0;
505   v->patterns = (unsigned char *) malloc (1);
506   while (1)
507     {
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);
515     }
516 }
517
518 static int
519 fieldcmp (opcode * op, int bit, char *name)
520 {
521   int n = strlen (name);
522
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;
527 }
528
529 static void
530 log_indirect (Indirect * ind, int byte)
531 {
532   int i, j;
533   char * last_c = 0;
534
535   for (i = 0; i < 256; i++)
536     {
537
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));
541
542       switch (ind[i].type)
543         {
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;
561         }
562     }
563 }
564
565 int
566 main (int argc, char ** argv)
567 {
568   char * line;
569   FILE * in;
570   int lineno = 0;
571   int i;
572   VaryRef * vlist;
573   int skipping_section = 0;
574
575   if (argc > 2 && strcmp (argv[1], "-l") == 0)
576     {
577       sim_log = fopen (argv[2], "w");
578       fprintf (stderr, "sim_log: %s\n", argv[2]);
579       argc -= 2;
580       argv += 2;
581     }
582
583   if (argc < 2)
584     {
585       fprintf (stderr, "usage: opc2c infile.opc > outfile.opc\n");
586       exit (1);
587     }
588
589   orig_filename = argv[1];
590   in = fopen (argv[1], "r");
591   if (!in)
592     {
593       fprintf (stderr, "Unable to open file %s for reading\n", argv[1]);
594       perror ("The error was");
595       exit (1);
596     }
597
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)
603     {
604       lineno++;
605       if (strncmp (line, "/*?* ", 5) == 0)
606         {
607           skipping_section = 1;
608           continue;
609         }
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] == '*')
614         {
615           skipping_section = 0;
616           if (strncmp (line, "/** */", 6) == 0)
617             {
618               op = &suffix_text;
619               op->lineno = lineno;
620             }
621           else if (strncmp (line, "/** VARY ", 9) == 0)
622             process_vary (line + 9);
623           else
624             {
625               char * lp;
626               int i, bit, byte;
627               int var_start = 1;
628
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;
634
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++)
639                 {
640                   op->b[i].decodable_mask = 0;
641                   op->b[i].decodable_bits = 0;
642                 }
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;
654
655               i = 0;
656               for (lp = line + 4; *lp; lp++)
657                 {
658                   bit = 7 - (i & 7);
659                   byte = i >> 3;
660
661                   if (strncmp (lp, "*/", 2) == 0)
662                     break;
663                   else if ((lp[0] == ' ' && lp[1] == ' ') || (lp[0] == '\t'))
664                     {
665                       while (*lp == ' ' || *lp == '\t')
666                         lp ++;
667                       op->syntax = strdup (lp);
668                       lp = strstr (op->syntax, "*/");
669                       if (lp)
670                         {
671                           *lp-- = 0;
672                           while ((*lp == ' ' || *lp == '\t')
673                                  && lp > op->syntax)
674                             *lp-- = 0;
675                         }
676                       break;
677                     }
678                   else if (*lp == ' ')
679                     var_start = 1;
680                   else
681                     {
682                       if (*lp == '0' || *lp == '1')
683                         {
684                           op->b[byte].decodable_mask |= 1 << bit;
685                           var_start = 1;
686                           if (op->dbytes < byte + 1)
687                             op->dbytes = byte + 1;
688                         }
689                       else if (var_start)
690                         {
691                           op->var_start[i] = 1;
692                           var_start = 0;
693                           if (op->dbytes < byte + 1)
694                             op->dbytes = byte + 1;
695                         }
696                       if (*lp == '1')
697                         op->b[byte].decodable_bits |= 1 << bit;
698
699                       op->nbytes = byte + 1;
700                       op->id[i++] = *lp;
701                     }
702                 }
703             }
704         }
705       else if (!skipping_section)
706         {
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);
714         }
715     }
716
717   {
718     int i, j;
719     for (i = 0; i < n_varies; i++)
720       {
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]);
725       }
726   }
727
728   for (i = n_opcodes - 2; i >= 0; i--)
729     {
730       if (opcodes[i]->nlines == 0)
731         {
732           opcodes[i]->nlines = opcodes[i + 1]->nlines;
733           opcodes[i]->lines = opcodes[i + 1]->lines;
734         }
735     }
736
737   for (i = 0; i < 256; i++)
738     indirect[i].type = T_unused;
739
740   qsort (opcodes, n_opcodes, sizeof (opcodes[0]), op_cmp);
741
742   vlist = (VaryRef *) malloc (n_varies * sizeof (VaryRef));
743
744   for (i = 0; i < n_opcodes; i++)
745     {
746       int j, b, v;
747
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);
753
754       for (j = 0; j < opcodes[i]->nbytes; j++)
755         {
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))
760                   {
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));
769
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);
775                   }
776
777         }
778     }
779
780   for (i = 0; i < n_opcodes; i++)
781     {
782       int i2;
783       int bytes = opcodes[i]->dbytes;
784
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);
790
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");
796
797       store_opcode_bits (opcodes[i], 0, indirect);
798     }
799
800   dump_lines (&prefix_text, 0, 0);
801
802   emit_indirect (indirect, 0);
803
804   dump_lines (&suffix_text, 0, 0);
805
806   if (sim_log)
807     log_indirect (indirect, 0);
808
809   return errors;
810 }