5baa7645f61c50e181c038cdd0a0fce94d0fb4e7
[external/binutils.git] / sim / m32c / opc2c.c
1 /* opc2c.c --- generate C simulator code from from .opc file
2
3 Copyright (C) 2005, 2007 Free Software Foundation, Inc.
4 Contributed by Red Hat, Inc.
5
6 This file is part of the GNU simulators.
7
8 The GNU simulators are free software; you can redistribute them and/or
9 modify them under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
12
13 The GNU simulators are distributed in the hope that they will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with the GNU simulators; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA  */
22
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <stdlib.h>
28
29 #include "safe-fgets.h"
30
31 static int errors = 0;
32
33 #define MAX_BYTES 10
34
35 typedef struct
36 {
37   int varyno:16;
38   int byte:8;
39   int shift:8;
40 } VaryRef;
41
42 typedef struct
43 {
44   char nbytes;
45   char dbytes;
46   char id[MAX_BYTES * 8 + 1];
47   unsigned char var_start[MAX_BYTES * 8 + 1];
48   struct
49   {
50     unsigned char decodable_mask;
51     unsigned char decodable_bits;
52   } b[MAX_BYTES];
53   char *comment;
54   int lineno;
55   int nlines;
56   char **lines;
57   struct Indirect *last_ind;
58   int semantics_label;
59   int nvaries;
60   VaryRef *vary;
61 } opcode;
62
63 int n_opcodes;
64 opcode **opcodes;
65 opcode *op;
66
67 typedef struct
68 {
69   char *name;
70   int nlen;
71   unsigned char mask;
72   int n_patterns;
73   unsigned char *patterns;
74 } Vary;
75
76 Vary **vary = 0;
77 int n_varies = 0;
78
79 unsigned char cur_bits[MAX_BYTES + 1];
80
81 char *orig_filename;
82
83 FILE *sim_log = 0;
84 #define lprintf if (sim_log) fprintf
85
86 opcode prefix_text, suffix_text;
87
88 typedef enum
89 {
90   T_unused,
91   T_op,
92   T_indirect,
93   T_done
94 } OpType;
95
96 typedef struct Indirect
97 {
98   OpType type;
99   union
100   {
101     struct Indirect *ind;
102     opcode *op;
103   } u;
104 } Indirect;
105
106 Indirect indirect[256];
107
108 static int
109 next_varybits (int bits, opcode * op, int byte)
110 {
111   int mask = op->b[byte].decodable_mask;
112   int i;
113
114   for (i = 0; i < 8; i++)
115     if (!(mask & (1 << i)))
116       {
117         if (bits & (1 << i))
118           {
119             bits &= ~(1 << i);
120           }
121         else
122           {
123             bits |= (1 << i);
124             return bits;
125           }
126       }
127   return 0;
128 }
129
130 static int
131 valid_varybits (int bits, opcode * op, int byte)
132 {
133   if (op->nvaries)
134     {
135       int vn;
136       for (vn = 0; vn < op->nvaries; vn++)
137         {
138           int found = 0;
139           int i;
140           int ob;
141
142           if (byte != op->vary[vn].byte)
143             continue;
144           Vary *v = vary[op->vary[vn].varyno];
145           ob = (bits >> op->vary[vn].shift) & v->mask;
146           lprintf (sim_log, "varybits: vary %s ob %x\n", v->name, ob);
147
148           for (i = 0; i < v->n_patterns; i++)
149             if (ob == v->patterns[i])
150               {
151                 lprintf (sim_log, "  found at %d\n", i);
152                 found = 1;
153                 break;
154               }
155           if (!found)
156             return 0;
157         }
158     }
159   return 1;
160 }
161
162 char *
163 prmb (int mask, int bits)
164 {
165   static char buf[8][30];
166   static int bn = 0;
167   char *bp;
168
169   bn = (bn + 1) % 8;
170   bp = buf[bn];
171   int i;
172   for (i = 0; i < 8; i++)
173     {
174       int bit = 0x80 >> i;
175       if (!(mask & bit))
176         *bp++ = '-';
177       else if (bits & bit)
178         *bp++ = '1';
179       else
180         *bp++ = '0';
181       if (i % 4 == 3)
182         *bp++ = ' ';
183     }
184   *--bp = 0;
185   return buf[bn];
186 }
187
188 static int
189 op_cmp (const void *va, const void *vb)
190 {
191   const opcode *a = *(const opcode **) va;
192   const opcode *b = *(const opcode **) vb;
193
194   if (a->nbytes != b->nbytes)
195     return a->nbytes - b->nbytes;
196
197   return strcmp (a->id, b->id);
198 }
199
200 void
201 dump_lines (opcode * op, int level, Indirect * ind)
202 {
203   char *varnames[40];
204   int i, vn = 0;
205
206   if (op->semantics_label)
207     {
208       printf ("%*sgoto op_semantics_%d;\n", level, "", op->semantics_label);
209       return;
210     }
211
212   if (ind != op->last_ind)
213     {
214       static int labelno = 0;
215       labelno++;
216       printf ("%*sop_semantics_%d:\n", level, "", labelno);
217       op->semantics_label = labelno;
218     }
219
220   if (op->comment)
221     {
222       level += 2;
223       printf ("%*s{\n", level, "");
224       printf ("%*s  %s\n", level, "", op->comment);
225     }
226
227   for (i = 0; i < op->nbytes * 8;)
228     {
229       if (isalpha (op->id[i]))
230         {
231           int byte = i >> 3;
232           int mask = 0;
233           int shift = 0;
234           char name[33];
235           char *np = name;
236           while (op->id[i] && isalpha (op->id[i]))
237             {
238               mask = (mask << 1) | 1;
239               shift = 7 - (i & 7);
240               *np++ = op->id[i++];
241               if (op->var_start[i])
242                 break;
243             }
244           *np = 0;
245           varnames[vn++] = strdup (name);
246           printf ("#line %d \"%s\"\n", op->lineno, orig_filename);
247           if (mask & ~0xff)
248             {
249               fprintf (stderr, "Error: variable %s spans bytes: %s\n",
250                        name, op->comment);
251               errors++;
252             }
253           else if (shift && (mask != 0xff))
254             printf ("%*s  int %s AU = (op[%d] >> %d) & 0x%02x;\n",
255                     level, "", name, byte, shift, mask);
256           else if (mask != 0xff)
257             printf ("%*s  int %s AU = op[%d] & 0x%02x;\n",
258                     level, "", name, byte, mask);
259           else
260             printf ("%*s  int %s AU = op[%d];\n", level, "", name, byte);
261         }
262       else
263         i++;
264     }
265   if (op->comment)
266     {
267       printf ("%*s  if (trace) {\n", level, "");
268       printf ("%*s      printf(\"\\033[33m%%s\\033[0m ", level, "");
269       for (i = 0; i < op->nbytes; i++)
270         printf (" %%02x");
271       printf ("\\n\"");
272       printf (",\n%*s             \"%s\"", level, "", op->comment);
273       for (i = 0; i < op->nbytes; i++)
274         {
275           if (i == 0)
276             printf (",\n%*s             op[%d]", level, "", i);
277           else
278             printf (", op[%d]", i);
279         }
280       printf (");\n");
281       for (i = 0; i < vn; i++)
282         printf ("%*s      printf(\"  %s = 0x%%x%s\", %s);\n", level, "",
283                 varnames[i], (i < vn - 1) ? "," : "\\n", varnames[i]);
284       printf ("%*s    }\n", level, "");
285     }
286   printf ("#line %d \"%s\"\n", op->lineno + 1, orig_filename);
287   for (i = 0; i < op->nlines; i++)
288     printf ("%*s%s", level, "", op->lines[i]);
289   if (op->comment)
290     printf ("%*s}\n", level, "");
291 }
292
293 void
294 store_opcode_bits (opcode * op, int byte, Indirect * ind)
295 {
296   int bits = op->b[byte].decodable_bits;
297
298   do
299     {
300       if (!valid_varybits (bits, op, byte))
301         continue;
302
303       switch (ind[bits].type)
304         {
305         case T_unused:
306           if (byte == op->dbytes - 1)
307             {
308               ind[bits].type = T_op;
309               ind[bits].u.op = op;
310               op->last_ind = ind;
311               break;
312             }
313           else
314             {
315               int i2;
316               ind[bits].type = T_indirect;
317               ind[bits].u.ind = (Indirect *) malloc (256 * sizeof (Indirect));
318               for (i2 = 0; i2 < 256; i2++)
319                 ind[bits].u.ind[i2].type = T_unused;
320               store_opcode_bits (op, byte + 1, ind[bits].u.ind);
321             }
322           break;
323
324         case T_indirect:
325           if (byte < op->dbytes - 1)
326             store_opcode_bits (op, byte + 1, ind[bits].u.ind);
327           break;
328
329         case T_op:
330           break;
331
332         case T_done:
333           break;
334         }
335     }
336   while ((bits = next_varybits (bits, op, byte)) != 0);
337 }
338
339 void
340 emit_indirect (Indirect * ind, int byte)
341 {
342   int unsup = 0;
343   int j, n, mask;
344
345   mask = 0;
346   for (j = 0; j < 256; j++)
347     {
348       switch (ind[j].type)
349         {
350         case T_indirect:
351           mask = 0xff;
352           break;
353         case T_op:
354           mask |= ind[j].u.op->b[byte].decodable_mask;
355           break;
356         case T_done:
357         case T_unused:
358           break;
359         }
360     }
361
362   printf ("%*s  GETBYTE();\n", byte * 6, "");
363   printf ("%*s  switch (op[%d] & 0x%02x) {\n", byte * 6, "", byte, mask);
364   for (j = 0; j < 256; j++)
365     if ((j & ~mask) == 0)
366       {
367         switch (ind[j].type)
368           {
369           case T_done:
370             break;
371           case T_unused:
372             unsup = 1;
373             break;
374           case T_op:
375             for (n = j; n < 256; n++)
376               if ((n & ~mask) == 0
377                   && ind[n].type == T_op && ind[n].u.op == ind[j].u.op)
378                 {
379                   ind[n].type = T_done;
380                   printf ("%*s    case 0x%02x:\n", byte * 6, "", n);
381                 }
382             for (n = byte; n < ind[j].u.op->nbytes - 1; n++)
383               printf ("%*s      GETBYTE();\n", byte * 6, "");
384             dump_lines (ind[j].u.op, byte * 6 + 6, ind);
385             printf ("%*s      break;\n", byte * 6, "");
386             break;
387           case T_indirect:
388             printf ("%*s    case 0x%02x:\n", byte * 6, "", j);
389             emit_indirect (ind[j].u.ind, byte + 1);
390             printf ("%*s      break;\n", byte * 6, "");
391             break;
392           }
393       }
394   if (unsup)
395     printf ("%*s    default: UNSUPPORTED(); break;\n", byte * 6, "");
396   printf ("%*s  }\n", byte * 6, "");
397 }
398
399 static char *
400 pv_dup (char *p, char *ep)
401 {
402   int n = ep - p;
403   char *rv = (char *) malloc (n + 1);
404   memcpy (rv, p, n);
405   rv[n] = 0;
406   return rv;
407 }
408
409 static unsigned char
410 str2mask (char *str, char *ep)
411 {
412   unsigned char rv = 0;
413   while (str < ep)
414     {
415       rv *= 2;
416       if (*str == '1')
417         rv += 1;
418       str++;
419     }
420   return rv;
421 }
422
423 static void
424 process_vary (char *line)
425 {
426   char *cp, *ep;
427   Vary *v = (Vary *) malloc (sizeof (Vary));
428
429   n_varies++;
430   if (vary)
431     vary = (Vary **) realloc (vary, n_varies * sizeof (Vary *));
432   else
433     vary = (Vary **) malloc (n_varies * sizeof (Vary *));
434   vary[n_varies - 1] = v;
435
436   cp = line;
437
438   for (cp = line; isspace (*cp); cp++);
439   for (ep = cp; *ep && !isspace (*ep); ep++);
440
441   v->name = pv_dup (cp, ep);
442   v->nlen = strlen (v->name);
443   v->mask = (1 << v->nlen) - 1;
444
445   v->n_patterns = 0;
446   v->patterns = (unsigned char *) malloc (1);
447   while (1)
448     {
449       for (cp = ep; isspace (*cp); cp++);
450       if (!isdigit (*cp))
451         break;
452       for (ep = cp; *ep && !isspace (*ep); ep++);
453       v->n_patterns++;
454       v->patterns = (unsigned char *) realloc (v->patterns, v->n_patterns);
455       v->patterns[v->n_patterns - 1] = str2mask (cp, ep);
456     }
457 }
458
459 static int
460 fieldcmp (opcode * op, int bit, char *name)
461 {
462   int n = strlen (name);
463   if (memcmp (op->id + bit, name, n) == 0
464       && (!isalpha (op->id[bit + n]) || op->var_start[bit + n]))
465     return 1;
466   return 0;
467 }
468
469 static void
470 log_indirect (Indirect * ind, int byte)
471 {
472   int i, j;
473   char *last_c = 0;
474
475   for (i = 0; i < 256; i++)
476     {
477       if (ind[i].type == T_unused)
478         continue;
479
480       for (j = 0; j < byte; j++)
481         fprintf (sim_log, "%s ", prmb (255, cur_bits[j]));
482       fprintf (sim_log, "%s ", prmb (255, i));
483
484       switch (ind[i].type)
485         {
486         case T_op:
487         case T_done:
488           if (last_c && (ind[i].u.op->comment == last_c))
489             fprintf (sim_log, "''\n");
490           else
491             fprintf (sim_log, "%s\n", ind[i].u.op->comment);
492           last_c = ind[i].u.op->comment;
493           break;
494         case T_unused:
495           fprintf (sim_log, "-\n");
496           break;
497         case T_indirect:
498           fprintf (sim_log, "indirect\n");
499           cur_bits[byte] = i;
500           log_indirect (ind[i].u.ind, byte + 1);
501           last_c = 0;
502           break;
503         }
504     }
505 }
506
507 int
508 main (int argc, char **argv)
509 {
510   char *line;
511   FILE *in;
512   int lineno = 0;
513   int i;
514   VaryRef *vlist;
515
516   if (argc > 2 && strcmp (argv[1], "-l") == 0)
517     {
518       sim_log = fopen (argv[2], "w");
519       fprintf (stderr, "sim_log: %s\n", argv[2]);
520       argc -= 2;
521       argv += 2;
522     }
523
524   if (argc < 2)
525     {
526       fprintf (stderr, "usage: opc2c infile.opc > outfile.opc\n");
527       exit (1);
528     }
529
530   orig_filename = argv[1];
531   in = fopen (argv[1], "r");
532   if (!in)
533     {
534       fprintf (stderr, "Unable to open file %s for reading\n", argv[1]);
535       perror ("The error was");
536       exit (1);
537     }
538
539   n_opcodes = 0;
540   opcodes = (opcode **) malloc (sizeof (opcode *));
541   op = &prefix_text;
542   op->lineno = 1;
543   while ((line = safe_fgets (in)) != 0)
544     {
545       lineno++;
546       if (strncmp (line, "  /** ", 6) == 0
547           && (isdigit (line[6]) || memcmp (line + 6, "VARY", 4) == 0))
548         line += 2;
549       if (line[0] == '/' && line[1] == '*' && line[2] == '*')
550         {
551           if (strncmp (line, "/** */", 6) == 0)
552             {
553               op = &suffix_text;
554               op->lineno = lineno;
555             }
556           else if (strncmp (line, "/** VARY ", 9) == 0)
557             process_vary (line + 9);
558           else
559             {
560               char *lp;
561               int i, bit, byte;
562               int var_start = 1;
563
564               n_opcodes++;
565               opcodes =
566                 (opcode **) realloc (opcodes, n_opcodes * sizeof (opcode *));
567               op = (opcode *) malloc (sizeof (opcode));
568               opcodes[n_opcodes - 1] = op;
569
570               op->nbytes = op->dbytes = 0;
571               memset (op->id, 0, sizeof (op->id));
572               memset (op->var_start, 0, sizeof (op->var_start));
573               for (i = 0; i < MAX_BYTES; i++)
574                 {
575                   op->b[i].decodable_mask = 0;
576                   op->b[i].decodable_bits = 0;
577                 }
578               op->comment = strdup (line);
579               op->comment[strlen (op->comment) - 1] = 0;
580               while (op->comment[0] && isspace (op->comment[0]))
581                 op->comment++;
582               op->lineno = lineno;
583               op->nlines = 0;
584               op->lines = 0;
585               op->last_ind = 0;
586               op->semantics_label = 0;
587               op->nvaries = 0;
588               op->vary = 0;
589
590               i = 0;
591               for (lp = line + 4; *lp; lp++)
592                 {
593                   bit = 7 - (i & 7);
594                   byte = i >> 3;
595
596                   if (strncmp (lp, "*/", 2) == 0)
597                     break;
598                   else if ((lp[0] == ' ' && lp[1] == ' ') || (lp[0] == '\t'))
599                     break;
600                   else if (*lp == ' ')
601                     var_start = 1;
602                   else
603                     {
604                       if (*lp == '0' || *lp == '1')
605                         {
606                           op->b[byte].decodable_mask |= 1 << bit;
607                           var_start = 1;
608                           if (op->dbytes < byte + 1)
609                             op->dbytes = byte + 1;
610                         }
611                       else if (var_start)
612                         {
613                           op->var_start[i] = 1;
614                           var_start = 0;
615                         }
616                       if (*lp == '1')
617                         op->b[byte].decodable_bits |= 1 << bit;
618
619                       op->nbytes = byte + 1;
620                       op->id[i++] = *lp;
621                     }
622                 }
623             }
624         }
625       else
626         {
627           op->nlines++;
628           if (op->lines)
629             op->lines =
630               (char **) realloc (op->lines, op->nlines * sizeof (char *));
631           else
632             op->lines = (char **) malloc (op->nlines * sizeof (char *));
633           op->lines[op->nlines - 1] = strdup (line);
634         }
635     }
636
637   {
638     int i, j;
639     for (i = 0; i < n_varies; i++)
640       {
641         Vary *v = vary[i];
642         lprintf (sim_log, "V[%s] %d\n", v->name, v->nlen);
643         for (j = 0; j < v->n_patterns; j++)
644           lprintf (sim_log, "  P %02x\n", v->patterns[j]);
645       }
646   }
647
648   for (i = n_opcodes - 2; i >= 0; i--)
649     {
650       if (opcodes[i]->nlines == 0)
651         {
652           opcodes[i]->nlines = opcodes[i + 1]->nlines;
653           opcodes[i]->lines = opcodes[i + 1]->lines;
654         }
655     }
656
657   for (i = 0; i < 256; i++)
658     indirect[i].type = T_unused;
659
660   qsort (opcodes, n_opcodes, sizeof (opcodes[0]), op_cmp);
661
662   vlist = (VaryRef *) malloc (n_varies * sizeof (VaryRef));
663
664   for (i = 0; i < n_opcodes; i++)
665     {
666       int j, b, v;
667
668       for (j = 0; j < opcodes[i]->nbytes; j++)
669         lprintf (sim_log, "%s ",
670                  prmb (opcodes[i]->b[j].decodable_mask,
671                        opcodes[i]->b[j].decodable_bits));
672       lprintf (sim_log, " %s\n", opcodes[i]->comment);
673
674       for (j = 0; j < opcodes[i]->nbytes; j++)
675         {
676           for (b = 0; b < 8; b++)
677             if (isalpha (opcodes[i]->id[j * 8 + b]))
678               for (v = 0; v < n_varies; v++)
679                 if (fieldcmp (opcodes[i], j * 8 + b, vary[v]->name))
680                   {
681                     int nv = opcodes[i]->nvaries++;
682                     if (nv)
683                       opcodes[i]->vary =
684                         (VaryRef *) realloc (opcodes[i]->vary,
685                                              (nv + 1) * sizeof (VaryRef));
686                     else
687                       opcodes[i]->vary =
688                         (VaryRef *) malloc ((nv + 1) * sizeof (VaryRef));
689
690                     opcodes[i]->vary[nv].varyno = v;
691                     opcodes[i]->vary[nv].byte = j;
692                     opcodes[i]->vary[nv].shift = 8 - b - vary[v]->nlen;
693                     lprintf (sim_log, "[vary %s shift %d]\n",
694                              vary[v]->name, opcodes[i]->vary[nv].shift);
695                   }
696
697         }
698     }
699
700   for (i = 0; i < n_opcodes; i++)
701     {
702       int i2;
703       int bytes = opcodes[i]->dbytes;
704
705       lprintf (sim_log, "\nmask:");
706       for (i2 = 0; i2 < opcodes[i]->nbytes; i2++)
707         lprintf (sim_log, " %02x", opcodes[i]->b[i2].decodable_mask);
708       lprintf (sim_log, "%*s%s\n", 13 - 3 * opcodes[i]->nbytes, "",
709                opcodes[i]->comment);
710
711       lprintf (sim_log, "bits:");
712       for (i2 = 0; i2 < opcodes[i]->nbytes; i2++)
713         lprintf (sim_log, " %02x", opcodes[i]->b[i2].decodable_bits);
714       lprintf (sim_log, "%*s(%s) %d byte%s\n", 13 - 3 * opcodes[i]->nbytes,
715                "", opcodes[i]->id, bytes, bytes == 1 ? "" : "s");
716
717       store_opcode_bits (opcodes[i], 0, indirect);
718     }
719
720   dump_lines (&prefix_text, 0, 0);
721
722   emit_indirect (indirect, 0);
723
724   dump_lines (&suffix_text, 0, 0);
725
726   if (sim_log)
727     log_indirect (indirect, 0);
728
729   return errors;
730 }