opc2c.c 15 KB

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