PageRenderTime 58ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/sim/m32c/opc2c.c

https://bitbucket.org/teawater/gdb
C | 727 lines | 624 code | 83 blank | 20 comment | 158 complexity | 3eb4c6791831b344079475ad45f37781 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0, LGPL-2.1, GPL-2.0, LGPL-2.0
  1. /* opc2c.c --- generate C simulator code from from .opc file
  2. Copyright (C) 2005, 2007, 2008, 2009, 2010, 2011
  3. Free Software Foundation, Inc.
  4. Contributed by Red Hat, Inc.
  5. This file is part of the GNU simulators.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <ctype.h>
  19. #include <stdlib.h>
  20. #include "safe-fgets.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 AU = (op[%d] >> %d) & 0x%02x;\n",
  217. level, "", name, byte, shift, mask);
  218. else if (mask != 0xff)
  219. printf ("%*s int %s AU = op[%d] & 0x%02x;\n",
  220. level, "", name, byte, mask);
  221. else
  222. printf ("%*s int %s AU = op[%d];\n", level, "", name, byte);
  223. }
  224. else
  225. i++;
  226. }
  227. if (op->comment)
  228. {
  229. printf ("%*s if (trace) {\n", level, "");
  230. printf ("%*s printf(\"\\033[33m%%s\\033[0m ", level, "");
  231. for (i = 0; i < op->nbytes; i++)
  232. printf (" %%02x");
  233. printf ("\\n\"");
  234. printf (",\n%*s \"%s\"", level, "", op->comment);
  235. for (i = 0; i < op->nbytes; i++)
  236. {
  237. if (i == 0)
  238. printf (",\n%*s op[%d]", level, "", i);
  239. else
  240. printf (", op[%d]", i);
  241. }
  242. printf (");\n");
  243. for (i = 0; i < vn; i++)
  244. printf ("%*s printf(\" %s = 0x%%x%s\", %s);\n", level, "",
  245. varnames[i], (i < vn - 1) ? "," : "\\n", varnames[i]);
  246. printf ("%*s }\n", level, "");
  247. }
  248. printf ("#line %d \"%s\"\n", op->lineno + 1, orig_filename);
  249. for (i = 0; i < op->nlines; i++)
  250. printf ("%*s%s", level, "", op->lines[i]);
  251. if (op->comment)
  252. printf ("%*s}\n", level, "");
  253. }
  254. void
  255. store_opcode_bits (opcode * op, int byte, Indirect * ind)
  256. {
  257. int bits = op->b[byte].decodable_bits;
  258. do
  259. {
  260. if (!valid_varybits (bits, op, byte))
  261. continue;
  262. switch (ind[bits].type)
  263. {
  264. case T_unused:
  265. if (byte == op->dbytes - 1)
  266. {
  267. ind[bits].type = T_op;
  268. ind[bits].u.op = op;
  269. op->last_ind = ind;
  270. break;
  271. }
  272. else
  273. {
  274. int i2;
  275. ind[bits].type = T_indirect;
  276. ind[bits].u.ind = (Indirect *) malloc (256 * sizeof (Indirect));
  277. for (i2 = 0; i2 < 256; i2++)
  278. ind[bits].u.ind[i2].type = T_unused;
  279. store_opcode_bits (op, byte + 1, ind[bits].u.ind);
  280. }
  281. break;
  282. case T_indirect:
  283. if (byte < op->dbytes - 1)
  284. store_opcode_bits (op, byte + 1, ind[bits].u.ind);
  285. break;
  286. case T_op:
  287. break;
  288. case T_done:
  289. break;
  290. }
  291. }
  292. while ((bits = next_varybits (bits, op, byte)) != 0);
  293. }
  294. void
  295. emit_indirect (Indirect * ind, int byte)
  296. {
  297. int unsup = 0;
  298. int j, n, mask;
  299. mask = 0;
  300. for (j = 0; j < 256; j++)
  301. {
  302. switch (ind[j].type)
  303. {
  304. case T_indirect:
  305. mask = 0xff;
  306. break;
  307. case T_op:
  308. mask |= ind[j].u.op->b[byte].decodable_mask;
  309. break;
  310. case T_done:
  311. case T_unused:
  312. break;
  313. }
  314. }
  315. printf ("%*s GETBYTE();\n", byte * 6, "");
  316. printf ("%*s switch (op[%d] & 0x%02x) {\n", byte * 6, "", byte, mask);
  317. for (j = 0; j < 256; j++)
  318. if ((j & ~mask) == 0)
  319. {
  320. switch (ind[j].type)
  321. {
  322. case T_done:
  323. break;
  324. case T_unused:
  325. unsup = 1;
  326. break;
  327. case T_op:
  328. for (n = j; n < 256; n++)
  329. if ((n & ~mask) == 0
  330. && ind[n].type == T_op && ind[n].u.op == ind[j].u.op)
  331. {
  332. ind[n].type = T_done;
  333. printf ("%*s case 0x%02x:\n", byte * 6, "", n);
  334. }
  335. for (n = byte; n < ind[j].u.op->nbytes - 1; n++)
  336. printf ("%*s GETBYTE();\n", byte * 6, "");
  337. dump_lines (ind[j].u.op, byte * 6 + 6, ind);
  338. printf ("%*s break;\n", byte * 6, "");
  339. break;
  340. case T_indirect:
  341. printf ("%*s case 0x%02x:\n", byte * 6, "", j);
  342. emit_indirect (ind[j].u.ind, byte + 1);
  343. printf ("%*s break;\n", byte * 6, "");
  344. break;
  345. }
  346. }
  347. if (unsup)
  348. printf ("%*s default: UNSUPPORTED(); break;\n", byte * 6, "");
  349. printf ("%*s }\n", byte * 6, "");
  350. }
  351. static char *
  352. pv_dup (char *p, char *ep)
  353. {
  354. int n = ep - p;
  355. char *rv = (char *) malloc (n + 1);
  356. memcpy (rv, p, n);
  357. rv[n] = 0;
  358. return rv;
  359. }
  360. static unsigned char
  361. str2mask (char *str, char *ep)
  362. {
  363. unsigned char rv = 0;
  364. while (str < ep)
  365. {
  366. rv *= 2;
  367. if (*str == '1')
  368. rv += 1;
  369. str++;
  370. }
  371. return rv;
  372. }
  373. static void
  374. process_vary (char *line)
  375. {
  376. char *cp, *ep;
  377. Vary *v = (Vary *) malloc (sizeof (Vary));
  378. n_varies++;
  379. if (vary)
  380. vary = (Vary **) realloc (vary, n_varies * sizeof (Vary *));
  381. else
  382. vary = (Vary **) malloc (n_varies * sizeof (Vary *));
  383. vary[n_varies - 1] = v;
  384. cp = line;
  385. for (cp = line; isspace (*cp); cp++);
  386. for (ep = cp; *ep && !isspace (*ep); ep++);
  387. v->name = pv_dup (cp, ep);
  388. v->nlen = strlen (v->name);
  389. v->mask = (1 << v->nlen) - 1;
  390. v->n_patterns = 0;
  391. v->patterns = (unsigned char *) malloc (1);
  392. while (1)
  393. {
  394. for (cp = ep; isspace (*cp); cp++);
  395. if (!isdigit (*cp))
  396. break;
  397. for (ep = cp; *ep && !isspace (*ep); ep++);
  398. v->n_patterns++;
  399. v->patterns = (unsigned char *) realloc (v->patterns, v->n_patterns);
  400. v->patterns[v->n_patterns - 1] = str2mask (cp, ep);
  401. }
  402. }
  403. static int
  404. fieldcmp (opcode * op, int bit, char *name)
  405. {
  406. int n = strlen (name);
  407. if (memcmp (op->id + bit, name, n) == 0
  408. && (!isalpha (op->id[bit + n]) || op->var_start[bit + n]))
  409. return 1;
  410. return 0;
  411. }
  412. static void
  413. log_indirect (Indirect * ind, int byte)
  414. {
  415. int i, j;
  416. char *last_c = 0;
  417. for (i = 0; i < 256; i++)
  418. {
  419. for (j = 0; j < byte; j++)
  420. fprintf (sim_log, "%s ", prmb (255, cur_bits[j]));
  421. fprintf (sim_log, "%s ", prmb (255, i));
  422. switch (ind[i].type)
  423. {
  424. case T_op:
  425. case T_done:
  426. if (last_c && (ind[i].u.op->comment == last_c))
  427. fprintf (sim_log, "''\n");
  428. else
  429. fprintf (sim_log, "%s\n", ind[i].u.op->comment);
  430. last_c = ind[i].u.op->comment;
  431. break;
  432. case T_unused:
  433. fprintf (sim_log, "unused\n");
  434. break;
  435. case T_indirect:
  436. fprintf (sim_log, "indirect\n");
  437. cur_bits[byte] = i;
  438. log_indirect (ind[i].u.ind, byte + 1);
  439. last_c = 0;
  440. break;
  441. }
  442. }
  443. }
  444. int
  445. main (int argc, char **argv)
  446. {
  447. char *line;
  448. FILE *in;
  449. int lineno = 0;
  450. int i;
  451. VaryRef *vlist;
  452. if (argc > 2 && strcmp (argv[1], "-l") == 0)
  453. {
  454. sim_log = fopen (argv[2], "w");
  455. fprintf (stderr, "sim_log: %s\n", argv[2]);
  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. while ((line = safe_fgets (in)) != 0)
  477. {
  478. lineno++;
  479. if (strncmp (line, " /** ", 6) == 0
  480. && (isdigit (line[6]) || memcmp (line + 6, "VARY", 4) == 0))
  481. line += 2;
  482. if (line[0] == '/' && line[1] == '*' && line[2] == '*')
  483. {
  484. if (strncmp (line, "/** */", 6) == 0)
  485. {
  486. op = &suffix_text;
  487. op->lineno = lineno;
  488. }
  489. else if (strncmp (line, "/** VARY ", 9) == 0)
  490. process_vary (line + 9);
  491. else
  492. {
  493. char *lp;
  494. int i, bit, byte;
  495. int var_start = 1;
  496. n_opcodes++;
  497. opcodes =
  498. (opcode **) realloc (opcodes, n_opcodes * sizeof (opcode *));
  499. op = (opcode *) malloc (sizeof (opcode));
  500. opcodes[n_opcodes - 1] = op;
  501. op->nbytes = op->dbytes = 0;
  502. memset (op->id, 0, sizeof (op->id));
  503. memset (op->var_start, 0, sizeof (op->var_start));
  504. for (i = 0; i < MAX_BYTES; i++)
  505. {
  506. op->b[i].decodable_mask = 0;
  507. op->b[i].decodable_bits = 0;
  508. }
  509. op->comment = strdup (line);
  510. op->comment[strlen (op->comment) - 1] = 0;
  511. while (op->comment[0] && isspace (op->comment[0]))
  512. op->comment++;
  513. op->lineno = lineno;
  514. op->nlines = 0;
  515. op->lines = 0;
  516. op->last_ind = 0;
  517. op->semantics_label = 0;
  518. op->nvaries = 0;
  519. op->vary = 0;
  520. i = 0;
  521. for (lp = line + 4; *lp; lp++)
  522. {
  523. bit = 7 - (i & 7);
  524. byte = i >> 3;
  525. if (strncmp (lp, "*/", 2) == 0)
  526. break;
  527. else if ((lp[0] == ' ' && lp[1] == ' ') || (lp[0] == '\t'))
  528. break;
  529. else if (*lp == ' ')
  530. var_start = 1;
  531. else
  532. {
  533. if (*lp == '0' || *lp == '1')
  534. {
  535. op->b[byte].decodable_mask |= 1 << bit;
  536. var_start = 1;
  537. if (op->dbytes < byte + 1)
  538. op->dbytes = byte + 1;
  539. }
  540. else if (var_start)
  541. {
  542. op->var_start[i] = 1;
  543. var_start = 0;
  544. }
  545. if (*lp == '1')
  546. op->b[byte].decodable_bits |= 1 << bit;
  547. op->nbytes = byte + 1;
  548. op->id[i++] = *lp;
  549. }
  550. }
  551. }
  552. }
  553. else
  554. {
  555. op->nlines++;
  556. if (op->lines)
  557. op->lines =
  558. (char **) realloc (op->lines, op->nlines * sizeof (char *));
  559. else
  560. op->lines = (char **) malloc (op->nlines * sizeof (char *));
  561. op->lines[op->nlines - 1] = strdup (line);
  562. }
  563. }
  564. {
  565. int i, j;
  566. for (i = 0; i < n_varies; i++)
  567. {
  568. Vary *v = vary[i];
  569. lprintf (sim_log, "V[%s] %d\n", v->name, v->nlen);
  570. for (j = 0; j < v->n_patterns; j++)
  571. lprintf (sim_log, " P %02x\n", v->patterns[j]);
  572. }
  573. }
  574. for (i = n_opcodes - 2; i >= 0; i--)
  575. {
  576. if (opcodes[i]->nlines == 0)
  577. {
  578. opcodes[i]->nlines = opcodes[i + 1]->nlines;
  579. opcodes[i]->lines = opcodes[i + 1]->lines;
  580. }
  581. }
  582. for (i = 0; i < 256; i++)
  583. indirect[i].type = T_unused;
  584. qsort (opcodes, n_opcodes, sizeof (opcodes[0]), op_cmp);
  585. vlist = (VaryRef *) malloc (n_varies * sizeof (VaryRef));
  586. for (i = 0; i < n_opcodes; i++)
  587. {
  588. int j, b, v;
  589. for (j = 0; j < opcodes[i]->nbytes; j++)
  590. lprintf (sim_log, "%s ",
  591. prmb (opcodes[i]->b[j].decodable_mask,
  592. opcodes[i]->b[j].decodable_bits));
  593. lprintf (sim_log, " %s\n", opcodes[i]->comment);
  594. for (j = 0; j < opcodes[i]->nbytes; j++)
  595. {
  596. for (b = 0; b < 8; b++)
  597. if (isalpha (opcodes[i]->id[j * 8 + b]))
  598. for (v = 0; v < n_varies; v++)
  599. if (fieldcmp (opcodes[i], j * 8 + b, vary[v]->name))
  600. {
  601. int nv = opcodes[i]->nvaries++;
  602. if (nv)
  603. opcodes[i]->vary =
  604. (VaryRef *) realloc (opcodes[i]->vary,
  605. (nv + 1) * sizeof (VaryRef));
  606. else
  607. opcodes[i]->vary =
  608. (VaryRef *) malloc ((nv + 1) * sizeof (VaryRef));
  609. opcodes[i]->vary[nv].varyno = v;
  610. opcodes[i]->vary[nv].byte = j;
  611. opcodes[i]->vary[nv].shift = 8 - b - vary[v]->nlen;
  612. lprintf (sim_log, "[vary %s shift %d]\n",
  613. vary[v]->name, opcodes[i]->vary[nv].shift);
  614. }
  615. }
  616. }
  617. for (i = 0; i < n_opcodes; i++)
  618. {
  619. int i2;
  620. int bytes = opcodes[i]->dbytes;
  621. lprintf (sim_log, "\nmask:");
  622. for (i2 = 0; i2 < opcodes[i]->nbytes; i2++)
  623. lprintf (sim_log, " %02x", opcodes[i]->b[i2].decodable_mask);
  624. lprintf (sim_log, "%*s%s\n", 13 - 3 * opcodes[i]->nbytes, "",
  625. opcodes[i]->comment);
  626. lprintf (sim_log, "bits:");
  627. for (i2 = 0; i2 < opcodes[i]->nbytes; i2++)
  628. lprintf (sim_log, " %02x", opcodes[i]->b[i2].decodable_bits);
  629. lprintf (sim_log, "%*s(%s) %d byte%s\n", 13 - 3 * opcodes[i]->nbytes,
  630. "", opcodes[i]->id, bytes, bytes == 1 ? "" : "s");
  631. store_opcode_bits (opcodes[i], 0, indirect);
  632. }
  633. dump_lines (&prefix_text, 0, 0);
  634. emit_indirect (indirect, 0);
  635. dump_lines (&suffix_text, 0, 0);
  636. if (sim_log)
  637. log_indirect (indirect, 0);
  638. return errors;
  639. }