1 /* opc2c.c --- generate C simulator code from from .opc file
3 Copyright (C) 2005-2021 Free Software Foundation, Inc.
4 Contributed by Red Hat, Inc.
6 This file is part of the GNU simulators.
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 "safe-fgets.h"
29 static int errors
= 0;
44 char id
[MAX_BYTES
* 8 + 1];
45 unsigned char var_start
[MAX_BYTES
* 8 + 1];
48 unsigned char decodable_mask
;
49 unsigned char decodable_bits
;
55 struct Indirect
*last_ind
;
71 unsigned char *patterns
;
77 unsigned char cur_bits
[MAX_BYTES
+ 1];
82 #define lprintf if (sim_log) fprintf
84 opcode prefix_text
, suffix_text
;
94 typedef struct Indirect
104 Indirect indirect
[256];
107 next_varybits (int bits
, opcode
* op
, int byte
)
109 int mask
= op
->b
[byte
].decodable_mask
;
112 for (i
= 0; i
< 8; i
++)
113 if (!(mask
& (1 << i
)))
129 valid_varybits (int bits
, opcode
* op
, int byte
)
134 for (vn
= 0; vn
< op
->nvaries
; vn
++)
140 if (byte
!= op
->vary
[vn
].byte
)
142 Vary
*v
= vary
[op
->vary
[vn
].varyno
];
143 ob
= (bits
>> op
->vary
[vn
].shift
) & v
->mask
;
144 lprintf (sim_log
, "varybits: vary %s ob %x\n", v
->name
, ob
);
146 for (i
= 0; i
< v
->n_patterns
; i
++)
147 if (ob
== v
->patterns
[i
])
149 lprintf (sim_log
, " found at %d\n", i
);
161 prmb (int mask
, int bits
)
163 static char buf
[8][30];
170 for (i
= 0; i
< 8; i
++)
187 op_cmp (const void *va
, const void *vb
)
189 const opcode
*a
= *(const opcode
**) va
;
190 const opcode
*b
= *(const opcode
**) vb
;
192 if (a
->nbytes
!= b
->nbytes
)
193 return a
->nbytes
- b
->nbytes
;
195 return strcmp (a
->id
, b
->id
);
199 dump_lines (opcode
* op
, int level
, Indirect
* ind
)
204 if (op
->semantics_label
)
206 printf ("%*sgoto op_semantics_%d;\n", level
, "", op
->semantics_label
);
210 if (ind
!= op
->last_ind
)
212 static int labelno
= 0;
214 printf ("%*sop_semantics_%d:\n", level
, "", labelno
);
215 op
->semantics_label
= labelno
;
221 printf ("%*s{\n", level
, "");
222 printf ("%*s %s\n", level
, "", op
->comment
);
225 for (i
= 0; i
< op
->nbytes
* 8;)
227 if (isalpha (op
->id
[i
]))
234 while (op
->id
[i
] && isalpha (op
->id
[i
]))
236 mask
= (mask
<< 1) | 1;
239 if (op
->var_start
[i
])
243 varnames
[vn
++] = strdup (name
);
244 printf ("#line %d \"%s\"\n", op
->lineno
, orig_filename
);
247 fprintf (stderr
, "Error: variable %s spans bytes: %s\n",
251 else if (shift
&& (mask
!= 0xff))
252 printf ("%*s int %s AU = (op[%d] >> %d) & 0x%02x;\n",
253 level
, "", name
, byte
, shift
, mask
);
254 else if (mask
!= 0xff)
255 printf ("%*s int %s AU = op[%d] & 0x%02x;\n",
256 level
, "", name
, byte
, mask
);
258 printf ("%*s int %s AU = op[%d];\n", level
, "", name
, byte
);
265 printf ("%*s if (trace) {\n", level
, "");
266 printf ("%*s printf(\"\\033[33m%%s\\033[0m ", level
, "");
267 for (i
= 0; i
< op
->nbytes
; i
++)
270 printf (",\n%*s \"%s\"", level
, "", op
->comment
);
271 for (i
= 0; i
< op
->nbytes
; i
++)
274 printf (",\n%*s op[%d]", level
, "", i
);
276 printf (", op[%d]", i
);
279 for (i
= 0; i
< vn
; i
++)
280 printf ("%*s printf(\" %s = 0x%%x%s\", %s);\n", level
, "",
281 varnames
[i
], (i
< vn
- 1) ? "," : "\\n", varnames
[i
]);
282 printf ("%*s }\n", level
, "");
284 printf ("#line %d \"%s\"\n", op
->lineno
+ 1, orig_filename
);
285 for (i
= 0; i
< op
->nlines
; i
++)
286 printf ("%*s%s", level
, "", op
->lines
[i
]);
288 printf ("%*s}\n", level
, "");
292 store_opcode_bits (opcode
* op
, int byte
, Indirect
* ind
)
294 int bits
= op
->b
[byte
].decodable_bits
;
298 if (!valid_varybits (bits
, op
, byte
))
301 switch (ind
[bits
].type
)
304 if (byte
== op
->dbytes
- 1)
306 ind
[bits
].type
= T_op
;
314 ind
[bits
].type
= T_indirect
;
315 ind
[bits
].u
.ind
= (Indirect
*) malloc (256 * sizeof (Indirect
));
316 for (i2
= 0; i2
< 256; i2
++)
317 ind
[bits
].u
.ind
[i2
].type
= T_unused
;
318 store_opcode_bits (op
, byte
+ 1, ind
[bits
].u
.ind
);
323 if (byte
< op
->dbytes
- 1)
324 store_opcode_bits (op
, byte
+ 1, ind
[bits
].u
.ind
);
334 while ((bits
= next_varybits (bits
, op
, byte
)) != 0);
338 emit_indirect (Indirect
* ind
, int byte
)
344 for (j
= 0; j
< 256; j
++)
352 mask
|= ind
[j
].u
.op
->b
[byte
].decodable_mask
;
360 printf ("%*s GETBYTE();\n", byte
* 6, "");
361 printf ("%*s switch (op[%d] & 0x%02x) {\n", byte
* 6, "", byte
, mask
);
362 for (j
= 0; j
< 256; j
++)
363 if ((j
& ~mask
) == 0)
373 for (n
= j
; n
< 256; n
++)
375 && ind
[n
].type
== T_op
&& ind
[n
].u
.op
== ind
[j
].u
.op
)
377 ind
[n
].type
= T_done
;
378 printf ("%*s case 0x%02x:\n", byte
* 6, "", n
);
380 for (n
= byte
; n
< ind
[j
].u
.op
->nbytes
- 1; n
++)
381 printf ("%*s GETBYTE();\n", byte
* 6, "");
382 dump_lines (ind
[j
].u
.op
, byte
* 6 + 6, ind
);
383 printf ("%*s break;\n", byte
* 6, "");
386 printf ("%*s case 0x%02x:\n", byte
* 6, "", j
);
387 emit_indirect (ind
[j
].u
.ind
, byte
+ 1);
388 printf ("%*s break;\n", byte
* 6, "");
393 printf ("%*s default: UNSUPPORTED(); break;\n", byte
* 6, "");
394 printf ("%*s }\n", byte
* 6, "");
398 pv_dup (char *p
, char *ep
)
401 char *rv
= (char *) malloc (n
+ 1);
408 str2mask (char *str
, char *ep
)
410 unsigned char rv
= 0;
422 process_vary (char *line
)
425 Vary
*v
= (Vary
*) malloc (sizeof (Vary
));
429 vary
= (Vary
**) realloc (vary
, n_varies
* sizeof (Vary
*));
431 vary
= (Vary
**) malloc (n_varies
* sizeof (Vary
*));
432 vary
[n_varies
- 1] = v
;
436 for (cp
= line
; isspace (*cp
); cp
++);
437 for (ep
= cp
; *ep
&& !isspace (*ep
); ep
++);
439 v
->name
= pv_dup (cp
, ep
);
440 v
->nlen
= strlen (v
->name
);
441 v
->mask
= (1 << v
->nlen
) - 1;
444 v
->patterns
= (unsigned char *) malloc (1);
447 for (cp
= ep
; isspace (*cp
); cp
++);
450 for (ep
= cp
; *ep
&& !isspace (*ep
); ep
++);
452 v
->patterns
= (unsigned char *) realloc (v
->patterns
, v
->n_patterns
);
453 v
->patterns
[v
->n_patterns
- 1] = str2mask (cp
, ep
);
458 fieldcmp (opcode
* op
, int bit
, char *name
)
460 int n
= strlen (name
);
461 if (memcmp (op
->id
+ bit
, name
, n
) == 0
462 && (!isalpha (op
->id
[bit
+ n
]) || op
->var_start
[bit
+ n
]))
468 log_indirect (Indirect
* ind
, int byte
)
473 for (i
= 0; i
< 256; i
++)
476 for (j
= 0; j
< byte
; j
++)
477 fprintf (sim_log
, "%s ", prmb (255, cur_bits
[j
]));
478 fprintf (sim_log
, "%s ", prmb (255, i
));
484 if (last_c
&& (ind
[i
].u
.op
->comment
== last_c
))
485 fprintf (sim_log
, "''\n");
487 fprintf (sim_log
, "%s\n", ind
[i
].u
.op
->comment
);
488 last_c
= ind
[i
].u
.op
->comment
;
491 fprintf (sim_log
, "unused\n");
494 fprintf (sim_log
, "indirect\n");
496 log_indirect (ind
[i
].u
.ind
, byte
+ 1);
504 main (int argc
, char **argv
)
512 if (argc
> 2 && strcmp (argv
[1], "-l") == 0)
514 sim_log
= fopen (argv
[2], "w");
515 fprintf (stderr
, "sim_log: %s\n", argv
[2]);
522 fprintf (stderr
, "usage: opc2c infile.opc > outfile.opc\n");
526 orig_filename
= argv
[1];
527 in
= fopen (argv
[1], "r");
530 fprintf (stderr
, "Unable to open file %s for reading\n", argv
[1]);
531 perror ("The error was");
536 opcodes
= (opcode
**) malloc (sizeof (opcode
*));
539 while ((line
= safe_fgets (in
)) != 0)
542 if (strncmp (line
, " /** ", 6) == 0
543 && (isdigit (line
[6]) || memcmp (line
+ 6, "VARY", 4) == 0))
545 if (line
[0] == '/' && line
[1] == '*' && line
[2] == '*')
547 if (strncmp (line
, "/** */", 6) == 0)
552 else if (strncmp (line
, "/** VARY ", 9) == 0)
553 process_vary (line
+ 9);
562 (opcode
**) realloc (opcodes
, n_opcodes
* sizeof (opcode
*));
563 op
= (opcode
*) malloc (sizeof (opcode
));
564 opcodes
[n_opcodes
- 1] = op
;
566 op
->nbytes
= op
->dbytes
= 0;
567 memset (op
->id
, 0, sizeof (op
->id
));
568 memset (op
->var_start
, 0, sizeof (op
->var_start
));
569 for (i
= 0; i
< MAX_BYTES
; i
++)
571 op
->b
[i
].decodable_mask
= 0;
572 op
->b
[i
].decodable_bits
= 0;
574 op
->comment
= strdup (line
);
575 op
->comment
[strlen (op
->comment
) - 1] = 0;
576 while (op
->comment
[0] && isspace (op
->comment
[0]))
582 op
->semantics_label
= 0;
587 for (lp
= line
+ 4; *lp
; lp
++)
592 if (strncmp (lp
, "*/", 2) == 0)
594 else if ((lp
[0] == ' ' && lp
[1] == ' ') || (lp
[0] == '\t'))
600 if (*lp
== '0' || *lp
== '1')
602 op
->b
[byte
].decodable_mask
|= 1 << bit
;
604 if (op
->dbytes
< byte
+ 1)
605 op
->dbytes
= byte
+ 1;
609 op
->var_start
[i
] = 1;
613 op
->b
[byte
].decodable_bits
|= 1 << bit
;
615 op
->nbytes
= byte
+ 1;
626 (char **) realloc (op
->lines
, op
->nlines
* sizeof (char *));
628 op
->lines
= (char **) malloc (op
->nlines
* sizeof (char *));
629 op
->lines
[op
->nlines
- 1] = strdup (line
);
635 for (i
= 0; i
< n_varies
; i
++)
638 lprintf (sim_log
, "V[%s] %d\n", v
->name
, v
->nlen
);
639 for (j
= 0; j
< v
->n_patterns
; j
++)
640 lprintf (sim_log
, " P %02x\n", v
->patterns
[j
]);
644 for (i
= n_opcodes
- 2; i
>= 0; i
--)
646 if (opcodes
[i
]->nlines
== 0)
648 opcodes
[i
]->nlines
= opcodes
[i
+ 1]->nlines
;
649 opcodes
[i
]->lines
= opcodes
[i
+ 1]->lines
;
653 for (i
= 0; i
< 256; i
++)
654 indirect
[i
].type
= T_unused
;
656 qsort (opcodes
, n_opcodes
, sizeof (opcodes
[0]), op_cmp
);
658 vlist
= (VaryRef
*) malloc (n_varies
* sizeof (VaryRef
));
660 for (i
= 0; i
< n_opcodes
; i
++)
664 for (j
= 0; j
< opcodes
[i
]->nbytes
; j
++)
665 lprintf (sim_log
, "%s ",
666 prmb (opcodes
[i
]->b
[j
].decodable_mask
,
667 opcodes
[i
]->b
[j
].decodable_bits
));
668 lprintf (sim_log
, " %s\n", opcodes
[i
]->comment
);
670 for (j
= 0; j
< opcodes
[i
]->nbytes
; j
++)
672 for (b
= 0; b
< 8; b
++)
673 if (isalpha (opcodes
[i
]->id
[j
* 8 + b
]))
674 for (v
= 0; v
< n_varies
; v
++)
675 if (fieldcmp (opcodes
[i
], j
* 8 + b
, vary
[v
]->name
))
677 int nv
= opcodes
[i
]->nvaries
++;
680 (VaryRef
*) realloc (opcodes
[i
]->vary
,
681 (nv
+ 1) * sizeof (VaryRef
));
684 (VaryRef
*) malloc ((nv
+ 1) * sizeof (VaryRef
));
686 opcodes
[i
]->vary
[nv
].varyno
= v
;
687 opcodes
[i
]->vary
[nv
].byte
= j
;
688 opcodes
[i
]->vary
[nv
].shift
= 8 - b
- vary
[v
]->nlen
;
689 lprintf (sim_log
, "[vary %s shift %d]\n",
690 vary
[v
]->name
, opcodes
[i
]->vary
[nv
].shift
);
696 for (i
= 0; i
< n_opcodes
; i
++)
699 int bytes
= opcodes
[i
]->dbytes
;
701 lprintf (sim_log
, "\nmask:");
702 for (i2
= 0; i2
< opcodes
[i
]->nbytes
; i2
++)
703 lprintf (sim_log
, " %02x", opcodes
[i
]->b
[i2
].decodable_mask
);
704 lprintf (sim_log
, "%*s%s\n", 13 - 3 * opcodes
[i
]->nbytes
, "",
705 opcodes
[i
]->comment
);
707 lprintf (sim_log
, "bits:");
708 for (i2
= 0; i2
< opcodes
[i
]->nbytes
; i2
++)
709 lprintf (sim_log
, " %02x", opcodes
[i
]->b
[i2
].decodable_bits
);
710 lprintf (sim_log
, "%*s(%s) %d byte%s\n", 13 - 3 * opcodes
[i
]->nbytes
,
711 "", opcodes
[i
]->id
, bytes
, bytes
== 1 ? "" : "s");
713 store_opcode_bits (opcodes
[i
], 0, indirect
);
716 dump_lines (&prefix_text
, 0, 0);
718 emit_indirect (indirect
, 0);
720 dump_lines (&suffix_text
, 0, 0);
723 log_indirect (indirect
, 0);