arm: Support pac_key_* register operand for MRS/MSR in Armv8.1-M Mainline
[binutils-gdb.git] / sim / m32c / opc2c.c
blob9b24f5f17ec36e3c46bceaeeae7bb00f0a9beb04
1 /* opc2c.c --- generate C simulator code from from .opc file
3 Copyright (C) 2005-2024 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/>. */
21 /* This must come before any other includes. */
22 #include "defs.h"
24 #include <stdio.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <stdlib.h>
29 static int errors = 0;
31 #define MAX_BYTES 10
33 typedef struct
35 int varyno:16;
36 int byte:8;
37 int shift:8;
38 } VaryRef;
40 typedef struct
42 char nbytes;
43 char dbytes;
44 char id[MAX_BYTES * 8 + 1];
45 unsigned char var_start[MAX_BYTES * 8 + 1];
46 struct
48 unsigned char decodable_mask;
49 unsigned char decodable_bits;
50 } b[MAX_BYTES];
51 char *comment;
52 int lineno;
53 int nlines;
54 char **lines;
55 struct Indirect *last_ind;
56 int semantics_label;
57 int nvaries;
58 VaryRef *vary;
59 } opcode;
61 static int n_opcodes;
62 static opcode **opcodes;
63 static opcode *op;
65 typedef struct
67 char *name;
68 int nlen;
69 unsigned char mask;
70 int n_patterns;
71 unsigned char *patterns;
72 } Vary;
74 static Vary **vary = 0;
75 static int n_varies = 0;
77 static unsigned char cur_bits[MAX_BYTES + 1];
79 static char *orig_filename;
81 static FILE *sim_log = 0;
82 #define lprintf if (sim_log) fprintf
84 static opcode prefix_text, suffix_text;
86 typedef enum
88 T_unused,
89 T_op,
90 T_indirect,
91 T_done
92 } OpType;
94 typedef struct Indirect
96 OpType type;
97 union
99 struct Indirect *ind;
100 opcode *op;
101 } u;
102 } Indirect;
104 static Indirect indirect[256];
106 static int
107 next_varybits (int bits, opcode * op, int byte)
109 int mask = op->b[byte].decodable_mask;
110 int i;
112 for (i = 0; i < 8; i++)
113 if (!(mask & (1 << i)))
115 if (bits & (1 << i))
117 bits &= ~(1 << i);
119 else
121 bits |= (1 << i);
122 return bits;
125 return 0;
128 static int
129 valid_varybits (int bits, opcode * op, int byte)
131 if (op->nvaries)
133 int vn;
134 for (vn = 0; vn < op->nvaries; vn++)
136 int found = 0;
137 int i;
138 int ob;
139 Vary *v;
141 if (byte != op->vary[vn].byte)
142 continue;
143 v = vary[op->vary[vn].varyno];
144 ob = (bits >> op->vary[vn].shift) & v->mask;
145 lprintf (sim_log, "varybits: vary %s ob %x\n", v->name, ob);
147 for (i = 0; i < v->n_patterns; i++)
148 if (ob == v->patterns[i])
150 lprintf (sim_log, " found at %d\n", i);
151 found = 1;
152 break;
154 if (!found)
155 return 0;
158 return 1;
161 static char *
162 prmb (int mask, int bits)
164 static char buf[8][30];
165 static int bn = 0;
166 char *bp;
167 int i;
169 bn = (bn + 1) % 8;
170 bp = buf[bn];
171 for (i = 0; i < 8; i++)
173 int bit = 0x80 >> i;
174 if (!(mask & bit))
175 *bp++ = '-';
176 else if (bits & bit)
177 *bp++ = '1';
178 else
179 *bp++ = '0';
180 if (i % 4 == 3)
181 *bp++ = ' ';
183 *--bp = 0;
184 return buf[bn];
187 static int
188 op_cmp (const void *va, const void *vb)
190 const opcode *a = *(const opcode **) va;
191 const opcode *b = *(const opcode **) vb;
193 if (a->nbytes != b->nbytes)
194 return a->nbytes - b->nbytes;
196 return strcmp (a->id, b->id);
199 static void
200 dump_lines (opcode * op, int level, Indirect * ind)
202 char *varnames[40];
203 int i, vn = 0;
205 if (op->semantics_label)
207 printf ("%*sgoto op_semantics_%d;\n", level, "", op->semantics_label);
208 return;
211 if (ind != op->last_ind)
213 static int labelno = 0;
214 labelno++;
215 printf ("%*sop_semantics_%d:\n", level, "", labelno);
216 op->semantics_label = labelno;
219 if (op->comment)
221 level += 2;
222 printf ("%*s{\n", level, "");
223 printf ("%*s %s\n", level, "", op->comment);
226 for (i = 0; i < op->nbytes * 8;)
228 if (isalpha (op->id[i]))
230 int byte = i >> 3;
231 int mask = 0;
232 int shift = 0;
233 char name[33];
234 char *np = name;
235 while (op->id[i] && isalpha (op->id[i]))
237 mask = (mask << 1) | 1;
238 shift = 7 - (i & 7);
239 *np++ = op->id[i++];
240 if (op->var_start[i])
241 break;
243 *np = 0;
244 varnames[vn++] = strdup (name);
245 printf ("#line %d \"%s\"\n", op->lineno, orig_filename);
246 if (mask & ~0xff)
248 fprintf (stderr, "Error: variable %s spans bytes: %s\n",
249 name, op->comment);
250 errors++;
252 else if (shift && (mask != 0xff))
253 printf ("%*s int %s ATTRIBUTE_UNUSED = (op[%d] >> %d) & 0x%02x;\n",
254 level, "", name, byte, shift, mask);
255 else if (mask != 0xff)
256 printf ("%*s int %s ATTRIBUTE_UNUSED = op[%d] & 0x%02x;\n",
257 level, "", name, byte, mask);
258 else
259 printf ("%*s int %s ATTRIBUTE_UNUSED = op[%d];\n", level, "", name,
260 byte);
262 else
263 i++;
265 if (op->comment)
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++)
275 if (i == 0)
276 printf (",\n%*s op[%d]", level, "", i);
277 else
278 printf (", op[%d]", i);
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, "");
286 printf ("#line %d \"%s\"\n", op->lineno, 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, "");
293 static void
294 store_opcode_bits (opcode * op, int byte, Indirect * ind)
296 int bits = op->b[byte].decodable_bits;
300 if (!valid_varybits (bits, op, byte))
301 continue;
303 switch (ind[bits].type)
305 case T_unused:
306 if (byte == op->dbytes - 1)
308 ind[bits].type = T_op;
309 ind[bits].u.op = op;
310 op->last_ind = ind;
311 break;
313 else
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);
322 break;
324 case T_indirect:
325 if (byte < op->dbytes - 1)
326 store_opcode_bits (op, byte + 1, ind[bits].u.ind);
327 break;
329 case T_op:
330 break;
332 case T_done:
333 break;
336 while ((bits = next_varybits (bits, op, byte)) != 0);
339 static void
340 emit_indirect (Indirect * ind, int byte)
342 int unsup = 0;
343 int j, n, mask;
345 mask = 0;
346 for (j = 0; j < 256; j++)
348 switch (ind[j].type)
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;
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)
367 switch (ind[j].type)
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)
379 ind[n].type = T_done;
380 printf ("%*s case 0x%02x:\n", byte * 6, "", n);
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;
394 if (unsup)
395 printf ("%*s default: UNSUPPORTED(); break;\n", byte * 6, "");
396 printf ("%*s }\n", byte * 6, "");
399 static char *
400 pv_dup (char *p, char *ep)
402 int n = ep - p;
403 char *rv = (char *) malloc (n + 1);
404 memcpy (rv, p, n);
405 rv[n] = 0;
406 return rv;
409 static unsigned char
410 str2mask (char *str, char *ep)
412 unsigned char rv = 0;
413 while (str < ep)
415 rv *= 2;
416 if (*str == '1')
417 rv += 1;
418 str++;
420 return rv;
423 static void
424 process_vary (char *line)
426 char *cp, *ep;
427 Vary *v = (Vary *) malloc (sizeof (Vary));
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;
436 cp = line;
438 for (cp = line; isspace (*cp); cp++);
439 for (ep = cp; *ep && !isspace (*ep); ep++);
441 v->name = pv_dup (cp, ep);
442 v->nlen = strlen (v->name);
443 v->mask = (1 << v->nlen) - 1;
445 v->n_patterns = 0;
446 v->patterns = (unsigned char *) malloc (1);
447 while (1)
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);
459 static int
460 fieldcmp (opcode * op, int bit, char *name)
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;
469 static void
470 log_indirect (Indirect * ind, int byte)
472 int i, j;
473 char *last_c = 0;
475 for (i = 0; i < 256; i++)
478 for (j = 0; j < byte; j++)
479 fprintf (sim_log, "%s ", prmb (255, cur_bits[j]));
480 fprintf (sim_log, "%s ", prmb (255, i));
482 switch (ind[i].type)
484 case T_op:
485 case T_done:
486 if (last_c && (ind[i].u.op->comment == last_c))
487 fprintf (sim_log, "''\n");
488 else
489 fprintf (sim_log, "%s\n", ind[i].u.op->comment);
490 last_c = ind[i].u.op->comment;
491 break;
492 case T_unused:
493 fprintf (sim_log, "unused\n");
494 break;
495 case T_indirect:
496 fprintf (sim_log, "indirect\n");
497 cur_bits[byte] = i;
498 log_indirect (ind[i].u.ind, byte + 1);
499 last_c = 0;
500 break;
506 main (int argc, char **argv)
508 char *linebuf;
509 FILE *in;
510 int lineno = 0;
511 int i;
512 size_t len;
514 if (argc > 2 && strcmp (argv[1], "-l") == 0)
516 sim_log = fopen (argv[2], "w");
517 argc -= 2;
518 argv += 2;
521 if (argc < 2)
523 fprintf (stderr, "usage: opc2c infile.opc > outfile.opc\n");
524 exit (1);
527 orig_filename = argv[1];
528 in = fopen (argv[1], "r");
529 if (!in)
531 fprintf (stderr, "Unable to open file %s for reading\n", argv[1]);
532 perror ("The error was");
533 exit (1);
536 n_opcodes = 0;
537 opcodes = (opcode **) malloc (sizeof (opcode *));
538 op = &prefix_text;
539 op->lineno = 1;
540 linebuf = NULL;
541 len = 0;
542 while (getline (&linebuf, &len, in) >= 0)
544 char *line = linebuf;
546 lineno++;
547 if (strncmp (line, " /** ", 6) == 0
548 && (isdigit (line[6]) || memcmp (line + 6, "VARY", 4) == 0))
549 line += 2;
550 if (line[0] == '/' && line[1] == '*' && line[2] == '*')
552 if (strncmp (line, "/** */", 6) == 0)
554 op = &suffix_text;
555 op->lineno = lineno;
557 else if (strncmp (line, "/** VARY ", 9) == 0)
558 process_vary (line + 9);
559 else
561 char *lp;
562 int i, bit, byte;
563 int var_start = 1;
565 n_opcodes++;
566 opcodes =
567 (opcode **) realloc (opcodes, n_opcodes * sizeof (opcode *));
568 op = (opcode *) malloc (sizeof (opcode));
569 opcodes[n_opcodes - 1] = op;
571 op->nbytes = op->dbytes = 0;
572 memset (op->id, 0, sizeof (op->id));
573 memset (op->var_start, 0, sizeof (op->var_start));
574 for (i = 0; i < MAX_BYTES; i++)
576 op->b[i].decodable_mask = 0;
577 op->b[i].decodable_bits = 0;
579 op->comment = strdup (line);
580 op->comment[strlen (op->comment) - 1] = 0;
581 while (op->comment[0] && isspace (op->comment[0]))
582 op->comment++;
583 op->lineno = lineno;
584 op->nlines = 0;
585 op->lines = 0;
586 op->last_ind = 0;
587 op->semantics_label = 0;
588 op->nvaries = 0;
589 op->vary = 0;
591 i = 0;
592 for (lp = line + 4; *lp; lp++)
594 bit = 7 - (i & 7);
595 byte = i >> 3;
597 if (strncmp (lp, "*/", 2) == 0)
598 break;
599 else if ((lp[0] == ' ' && lp[1] == ' ') || (lp[0] == '\t'))
600 break;
601 else if (*lp == ' ')
602 var_start = 1;
603 else
605 if (*lp == '0' || *lp == '1')
607 op->b[byte].decodable_mask |= 1 << bit;
608 var_start = 1;
609 if (op->dbytes < byte + 1)
610 op->dbytes = byte + 1;
612 else if (var_start)
614 op->var_start[i] = 1;
615 var_start = 0;
617 if (*lp == '1')
618 op->b[byte].decodable_bits |= 1 << bit;
620 op->nbytes = byte + 1;
621 op->id[i++] = *lp;
626 else
628 op->nlines++;
629 if (op->lines)
630 op->lines =
631 (char **) realloc (op->lines, op->nlines * sizeof (char *));
632 else
633 op->lines = (char **) malloc (op->nlines * sizeof (char *));
634 op->lines[op->nlines - 1] = strdup (line);
637 free (linebuf);
640 int i, j;
641 for (i = 0; i < n_varies; i++)
643 Vary *v = vary[i];
644 lprintf (sim_log, "V[%s] %d\n", v->name, v->nlen);
645 for (j = 0; j < v->n_patterns; j++)
646 lprintf (sim_log, " P %02x\n", v->patterns[j]);
650 for (i = n_opcodes - 2; i >= 0; i--)
652 if (opcodes[i]->nlines == 0)
654 opcodes[i]->nlines = opcodes[i + 1]->nlines;
655 opcodes[i]->lines = opcodes[i + 1]->lines;
659 for (i = 0; i < 256; i++)
660 indirect[i].type = T_unused;
662 qsort (opcodes, n_opcodes, sizeof (opcodes[0]), op_cmp);
664 for (i = 0; i < n_opcodes; i++)
666 int j, b, v;
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);
674 for (j = 0; j < opcodes[i]->nbytes; j++)
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))
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));
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);
700 for (i = 0; i < n_opcodes; i++)
702 int i2;
703 int bytes = opcodes[i]->dbytes;
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);
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");
717 store_opcode_bits (opcodes[i], 0, indirect);
720 dump_lines (&prefix_text, 0, 0);
722 emit_indirect (indirect, 0);
724 dump_lines (&suffix_text, 0, 0);
726 if (sim_log)
727 log_indirect (indirect, 0);
729 return errors;