You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1483 lines
45 KiB

  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Export functions.
  5. @author Fabio Somenzi
  6. @copyright@parblock
  7. Copyright (c) 1995-2015, Regents of the University of Colorado
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. Neither the name of the University of Colorado nor the names of its
  18. contributors may be used to endorse or promote products derived from
  19. this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. POSSIBILITY OF SUCH DAMAGE.
  32. @endparblock
  33. */
  34. #include "util.h"
  35. #include "cstringstream.h"
  36. #include "cuddInt.h"
  37. /*---------------------------------------------------------------------------*/
  38. /* Constant declarations */
  39. /*---------------------------------------------------------------------------*/
  40. /*---------------------------------------------------------------------------*/
  41. /* Stucture declarations */
  42. /*---------------------------------------------------------------------------*/
  43. /*---------------------------------------------------------------------------*/
  44. /* Type declarations */
  45. /*---------------------------------------------------------------------------*/
  46. /*---------------------------------------------------------------------------*/
  47. /* Variable declarations */
  48. /*---------------------------------------------------------------------------*/
  49. /*---------------------------------------------------------------------------*/
  50. /* Macro declarations */
  51. /*---------------------------------------------------------------------------*/
  52. /** \cond */
  53. /*---------------------------------------------------------------------------*/
  54. /* Static function prototypes */
  55. /*---------------------------------------------------------------------------*/
  56. static int ddDoDumpBlif (DdManager *dd, DdNode *f, FILE *fp, st_table *visited, char const * const *names, int mv);
  57. static int ddDoDumpDaVinci (DdManager *dd, DdNode *f, FILE *fp, st_table *visited, char const * const *names, ptruint mask);
  58. static int ddDoDumpDDcal (DdManager *dd, DdNode *f, FILE *fp, st_table *visited, char const * const *names, ptruint mask);
  59. static int ddDoDumpFactoredForm (DdManager *dd, DdNode *f, FILE *fp, char const * const *names);
  60. static int ddDoFactoredFormString(DdManager * dd, DdNode *f, cstringstream stream, char const * const * names);
  61. /** \endcond */
  62. /*---------------------------------------------------------------------------*/
  63. /* Definition of exported functions */
  64. /*---------------------------------------------------------------------------*/
  65. /**
  66. @brief Writes a blif file representing the argument BDDs.
  67. @details Each %BDD is written as a network of multiplexers.
  68. Cudd_DumpBlif does not close the file: This is the caller
  69. responsibility. Cudd_DumpBlif uses a minimal unique subset of the
  70. hexadecimal address of a node as name for it. If the argument
  71. inames is non-null, it is assumed to hold the pointers to the names
  72. of the inputs. Similarly for onames.
  73. @return 1 in case of success; 0 otherwise (e.g., out-of-memory, file
  74. system full, or an %ADD with constants different from 0 and 1).
  75. @sideeffect None
  76. @see Cudd_DumpBlifBody Cudd_DumpDot Cudd_PrintDebug Cudd_DumpDDcal
  77. Cudd_DumpDaVinci Cudd_DumpFactoredForm
  78. */
  79. int
  80. Cudd_DumpBlif(
  81. DdManager * dd /**< manager */,
  82. int n /**< number of output nodes to be dumped */,
  83. DdNode ** f /**< array of output nodes to be dumped */,
  84. char const * const * inames /**< array of input names (or NULL) */,
  85. char const * const * onames /**< array of output names (or NULL) */,
  86. char * mname /**< model name (or NULL) */,
  87. FILE * fp /**< pointer to the dump file */,
  88. int mv /**< 0: blif, 1: blif-MV */)
  89. {
  90. DdNode *support = NULL;
  91. DdNode *scan;
  92. int *sorted = NULL;
  93. int nvars = dd->size;
  94. int retval;
  95. int i;
  96. /* Build a bit array with the support of f. */
  97. sorted = ALLOC(int,nvars);
  98. if (sorted == NULL) {
  99. dd->errorCode = CUDD_MEMORY_OUT;
  100. goto failure;
  101. }
  102. for (i = 0; i < nvars; i++) sorted[i] = 0;
  103. /* Take the union of the supports of each output function. */
  104. support = Cudd_VectorSupport(dd,f,n);
  105. if (support == NULL) goto failure;
  106. cuddRef(support);
  107. scan = support;
  108. while (!cuddIsConstant(scan)) {
  109. sorted[scan->index] = 1;
  110. scan = cuddT(scan);
  111. }
  112. Cudd_RecursiveDeref(dd,support);
  113. support = NULL; /* so that we do not try to free it in case of failure */
  114. /* Write the header (.model .inputs .outputs). */
  115. if (mname == NULL) {
  116. retval = fprintf(fp,".model DD\n.inputs");
  117. } else {
  118. retval = fprintf(fp,".model %s\n.inputs",mname);
  119. }
  120. if (retval == EOF) {
  121. FREE(sorted);
  122. return(0);
  123. }
  124. /* Write the input list by scanning the support array. */
  125. for (i = 0; i < nvars; i++) {
  126. if (sorted[i]) {
  127. if (inames == NULL) {
  128. retval = fprintf(fp," %d", i);
  129. } else {
  130. retval = fprintf(fp," %s", inames[i]);
  131. }
  132. if (retval == EOF) goto failure;
  133. }
  134. }
  135. FREE(sorted);
  136. sorted = NULL;
  137. /* Write the .output line. */
  138. retval = fprintf(fp,"\n.outputs");
  139. if (retval == EOF) goto failure;
  140. for (i = 0; i < n; i++) {
  141. if (onames == NULL) {
  142. retval = fprintf(fp," f%d", i);
  143. } else {
  144. retval = fprintf(fp," %s", onames[i]);
  145. }
  146. if (retval == EOF) goto failure;
  147. }
  148. retval = fprintf(fp,"\n");
  149. if (retval == EOF) goto failure;
  150. retval = Cudd_DumpBlifBody(dd, n, f, inames, onames, fp, mv);
  151. if (retval == 0) goto failure;
  152. /* Write trailer and return. */
  153. retval = fprintf(fp,".end\n");
  154. if (retval == EOF) goto failure;
  155. return(1);
  156. failure:
  157. if (sorted != NULL) FREE(sorted);
  158. if (support != NULL) Cudd_RecursiveDeref(dd,support);
  159. return(0);
  160. } /* end of Cudd_DumpBlif */
  161. /**
  162. @brief Writes a blif body representing the argument BDDs.
  163. @details Each %BDD is written as a network of multiplexers. No
  164. header (.model, .inputs, and .outputs) and footer (.end) are
  165. produced by this function. One multiplexer is written for each %BDD
  166. node. Cudd_DumpBlifBody does not close the file: This is the caller
  167. responsibility. Cudd_DumpBlifBody uses a minimal unique subset of
  168. the hexadecimal address of a node as name for it. If the argument
  169. inames is non-null, it is assumed to hold the pointers to the names
  170. of the inputs. Similarly for onames. This function prints out only
  171. .names part.
  172. @return 1 in case of success; 0 otherwise (e.g., out-of-memory, file
  173. system full, or an %ADD with constants different from 0 and 1).
  174. @sideeffect None
  175. @see Cudd_DumpBlif Cudd_DumpDot Cudd_PrintDebug Cudd_DumpDDcal
  176. Cudd_DumpDaVinci Cudd_DumpFactoredForm
  177. */
  178. int
  179. Cudd_DumpBlifBody(
  180. DdManager * dd /**< manager */,
  181. int n /**< number of output nodes to be dumped */,
  182. DdNode ** f /**< array of output nodes to be dumped */,
  183. char const * const * inames /**< array of input names (or NULL) */,
  184. char const * const * onames /**< array of output names (or NULL) */,
  185. FILE * fp /**< pointer to the dump file */,
  186. int mv /**< 0: blif, 1: blif-MV */)
  187. {
  188. st_table *visited = NULL;
  189. int retval;
  190. int i;
  191. /* Initialize symbol table for visited nodes. */
  192. visited = st_init_table(st_ptrcmp, st_ptrhash);
  193. if (visited == NULL) goto failure;
  194. /* Call the function that really gets the job done. */
  195. for (i = 0; i < n; i++) {
  196. retval = ddDoDumpBlif(dd,Cudd_Regular(f[i]),fp,visited,inames,mv);
  197. if (retval == 0) goto failure;
  198. }
  199. /* To account for the possible complement on the root,
  200. ** we put either a buffer or an inverter at the output of
  201. ** the multiplexer representing the top node.
  202. */
  203. for (i = 0; i < n; i++) {
  204. if (onames == NULL) {
  205. retval = fprintf(fp, ".names %" PRIxPTR " f%d\n",
  206. (ptruint) f[i] / (ptruint) sizeof(DdNode), i);
  207. } else {
  208. retval = fprintf(fp, ".names %" PRIxPTR " %s\n",
  209. (ptruint) f[i] / (ptruint) sizeof(DdNode), onames[i]);
  210. }
  211. if (retval == EOF) goto failure;
  212. if (Cudd_IsComplement(f[i])) {
  213. retval = fprintf(fp,"%s0 1\n", mv ? ".def 0\n" : "");
  214. } else {
  215. retval = fprintf(fp,"%s1 1\n", mv ? ".def 0\n" : "");
  216. }
  217. if (retval == EOF) goto failure;
  218. }
  219. st_free_table(visited);
  220. return(1);
  221. failure:
  222. if (visited != NULL) st_free_table(visited);
  223. return(0);
  224. } /* end of Cudd_DumpBlifBody */
  225. /**
  226. @brief Writes a dot file representing the argument DDs.
  227. @details Writes a file representing the argument DDs in a format
  228. suitable for the graph drawing program dot.
  229. Cudd_DumpDot does not close the file: This is the caller
  230. responsibility. Cudd_DumpDot uses a minimal unique subset of the
  231. hexadecimal address of a node as name for it.
  232. If the argument inames is non-null, it is assumed to hold the pointers
  233. to the names of the inputs. Similarly for onames.
  234. Cudd_DumpDot uses the following convention to draw arcs:
  235. <ul>
  236. <li> solid line: THEN arcs;
  237. <li> dotted line: complement arcs;
  238. <li> dashed line: regular ELSE arcs.
  239. </ul>
  240. The dot options are chosen so that the drawing fits on a letter-size
  241. sheet.
  242. @return 1 in case of success; 0 otherwise (e.g., out-of-memory, file
  243. system full).
  244. @sideeffect None
  245. @see Cudd_DumpBlif Cudd_PrintDebug Cudd_DumpDDcal
  246. Cudd_DumpDaVinci Cudd_DumpFactoredForm
  247. */
  248. int
  249. Cudd_DumpDot(
  250. DdManager * dd /**< manager */,
  251. int n /**< number of output nodes to be dumped */,
  252. DdNode ** f /**< array of output nodes to be dumped */,
  253. char const * const * inames /**< array of input names (or NULL) */,
  254. char const * const * onames /**< array of output names (or NULL) */,
  255. FILE * fp /**< pointer to the dump file */)
  256. {
  257. DdNode *support = NULL;
  258. DdNode *scan;
  259. int *sorted = NULL;
  260. int nvars = dd->size;
  261. st_table *visited = NULL;
  262. st_generator *gen = NULL;
  263. int retval;
  264. int i, j;
  265. int slots;
  266. DdNodePtr *nodelist;
  267. ptruint refAddr, diff, mask = 0;
  268. /* Build a bit array with the support of f. */
  269. sorted = ALLOC(int,nvars);
  270. if (sorted == NULL) {
  271. dd->errorCode = CUDD_MEMORY_OUT;
  272. goto failure;
  273. }
  274. for (i = 0; i < nvars; i++) sorted[i] = 0;
  275. /* Take the union of the supports of each output function. */
  276. support = Cudd_VectorSupport(dd,f,n);
  277. if (support == NULL) goto failure;
  278. cuddRef(support);
  279. scan = support;
  280. while (!cuddIsConstant(scan)) {
  281. sorted[scan->index] = 1;
  282. scan = cuddT(scan);
  283. }
  284. Cudd_RecursiveDeref(dd,support);
  285. support = NULL; /* so that we do not try to free it in case of failure */
  286. /* Initialize symbol table for visited nodes. */
  287. visited = st_init_table(st_ptrcmp, st_ptrhash);
  288. if (visited == NULL) goto failure;
  289. /* Collect all the nodes of this DD in the symbol table. */
  290. for (i = 0; i < n; i++) {
  291. retval = cuddCollectNodes(Cudd_Regular(f[i]),visited);
  292. if (retval == 0) goto failure;
  293. }
  294. /* Find how many most significant hex digits are identical
  295. ** in the addresses of all the nodes. Build a mask based
  296. ** on this knowledge, so that digits that carry no information
  297. ** will not be printed. This is done in two steps.
  298. ** 1. We scan the symbol table to find the bits that differ
  299. ** in at least 2 addresses.
  300. ** 2. We choose one of the possible masks. There are 8 possible
  301. ** masks for 32-bit integer, and 16 possible masks for 64-bit
  302. ** integers.
  303. */
  304. /* Find the bits that are different. */
  305. refAddr = (ptruint) Cudd_Regular(f[0]);
  306. diff = 0;
  307. gen = st_init_gen(visited);
  308. if (gen == NULL) goto failure;
  309. while (st_gen(gen, (void **) &scan, NULL)) {
  310. diff |= refAddr ^ (ptruint) scan;
  311. }
  312. st_free_gen(gen); gen = NULL;
  313. /* Choose the mask. */
  314. for (i = 0; (unsigned) i < 8 * sizeof(ptruint); i += 4) {
  315. mask = ((ptruint) 1 << i) - 1;
  316. if (diff <= mask) break;
  317. }
  318. /* Write the header and the global attributes. */
  319. retval = fprintf(fp,"digraph \"DD\" {\n");
  320. if (retval == EOF) return(0);
  321. retval = fprintf(fp,
  322. "size = \"7.5,10\"\ncenter = true;\nedge [dir = none];\n");
  323. if (retval == EOF) return(0);
  324. /* Write the input name subgraph by scanning the support array. */
  325. retval = fprintf(fp,"{ node [shape = plaintext];\n");
  326. if (retval == EOF) goto failure;
  327. retval = fprintf(fp," edge [style = invis];\n");
  328. if (retval == EOF) goto failure;
  329. /* We use a name ("CONST NODES") with an embedded blank, because
  330. ** it is unlikely to appear as an input name.
  331. */
  332. retval = fprintf(fp," \"CONST NODES\" [style = invis];\n");
  333. if (retval == EOF) goto failure;
  334. for (i = 0; i < nvars; i++) {
  335. if (sorted[dd->invperm[i]]) {
  336. if (inames == NULL || inames[dd->invperm[i]] == NULL) {
  337. retval = fprintf(fp,"\" %d \" -> ", dd->invperm[i]);
  338. } else {
  339. retval = fprintf(fp,"\" %s \" -> ", inames[dd->invperm[i]]);
  340. }
  341. if (retval == EOF) goto failure;
  342. }
  343. }
  344. retval = fprintf(fp,"\"CONST NODES\"; \n}\n");
  345. if (retval == EOF) goto failure;
  346. /* Write the output node subgraph. */
  347. retval = fprintf(fp,"{ rank = same; node [shape = box]; edge [style = invis];\n");
  348. if (retval == EOF) goto failure;
  349. for (i = 0; i < n; i++) {
  350. if (onames == NULL) {
  351. retval = fprintf(fp,"\"F%d\"", i);
  352. } else {
  353. retval = fprintf(fp,"\" %s \"", onames[i]);
  354. }
  355. if (retval == EOF) goto failure;
  356. if (i == n - 1) {
  357. retval = fprintf(fp,"; }\n");
  358. } else {
  359. retval = fprintf(fp," -> ");
  360. }
  361. if (retval == EOF) goto failure;
  362. }
  363. /* Write rank info: All nodes with the same index have the same rank. */
  364. for (i = 0; i < nvars; i++) {
  365. if (sorted[dd->invperm[i]]) {
  366. retval = fprintf(fp,"{ rank = same; ");
  367. if (retval == EOF) goto failure;
  368. if (inames == NULL || inames[dd->invperm[i]] == NULL) {
  369. retval = fprintf(fp,"\" %d \";\n", dd->invperm[i]);
  370. } else {
  371. retval = fprintf(fp,"\" %s \";\n", inames[dd->invperm[i]]);
  372. }
  373. if (retval == EOF) goto failure;
  374. nodelist = dd->subtables[i].nodelist;
  375. slots = dd->subtables[i].slots;
  376. for (j = 0; j < slots; j++) {
  377. scan = nodelist[j];
  378. while (scan != NULL) {
  379. if (st_is_member(visited,scan)) {
  380. retval = fprintf(fp,"\"%#" PRIxPTR "\";\n",
  381. ((mask & (ptruint) scan) / sizeof(DdNode)));
  382. if (retval == EOF) goto failure;
  383. }
  384. scan = scan->next;
  385. }
  386. }
  387. retval = fprintf(fp,"}\n");
  388. if (retval == EOF) goto failure;
  389. }
  390. }
  391. /* All constants have the same rank. */
  392. retval = fprintf(fp,
  393. "{ rank = same; \"CONST NODES\";\n{ node [shape = box]; ");
  394. if (retval == EOF) goto failure;
  395. nodelist = dd->constants.nodelist;
  396. slots = dd->constants.slots;
  397. for (j = 0; j < slots; j++) {
  398. scan = nodelist[j];
  399. while (scan != NULL) {
  400. if (st_is_member(visited,scan)) {
  401. retval = fprintf(fp,"\"%#" PRIxPTR "\";\n",
  402. ((mask & (ptruint) scan) / sizeof(DdNode)));
  403. if (retval == EOF) goto failure;
  404. }
  405. scan = scan->next;
  406. }
  407. }
  408. retval = fprintf(fp,"}\n}\n");
  409. if (retval == EOF) goto failure;
  410. /* Write edge info. */
  411. /* Edges from the output nodes. */
  412. for (i = 0; i < n; i++) {
  413. if (onames == NULL) {
  414. retval = fprintf(fp,"\"F%d\"", i);
  415. } else {
  416. retval = fprintf(fp,"\" %s \"", onames[i]);
  417. }
  418. if (retval == EOF) goto failure;
  419. /* Account for the possible complement on the root. */
  420. if (Cudd_IsComplement(f[i])) {
  421. retval = fprintf(fp," -> \"%#" PRIxPTR "\" [style = dotted];\n",
  422. ((mask & (ptruint) f[i]) / sizeof(DdNode)));
  423. } else {
  424. retval = fprintf(fp," -> \"%#" PRIxPTR "\" [style = solid];\n",
  425. ((mask & (ptruint) f[i]) / sizeof(DdNode)));
  426. }
  427. if (retval == EOF) goto failure;
  428. }
  429. /* Edges from internal nodes. */
  430. for (i = 0; i < nvars; i++) {
  431. if (sorted[dd->invperm[i]]) {
  432. nodelist = dd->subtables[i].nodelist;
  433. slots = dd->subtables[i].slots;
  434. for (j = 0; j < slots; j++) {
  435. scan = nodelist[j];
  436. while (scan != NULL) {
  437. if (st_is_member(visited,scan)) {
  438. retval = fprintf(fp,
  439. "\"%#" PRIxPTR "\" -> \"%#" PRIxPTR "\";\n",
  440. ((mask & (ptruint) scan) / sizeof(DdNode)),
  441. ((mask & (ptruint) cuddT(scan)) / sizeof(DdNode)));
  442. if (retval == EOF) goto failure;
  443. if (Cudd_IsComplement(cuddE(scan))) {
  444. retval = fprintf(fp,
  445. "\"%#" PRIxPTR "\" -> \"%#" PRIxPTR
  446. "\" [style = dotted];\n",
  447. ((mask & (ptruint) scan) / sizeof(DdNode)),
  448. ((mask & (ptruint) cuddE(scan)) /
  449. sizeof(DdNode)));
  450. } else {
  451. retval = fprintf(fp,
  452. "\"%#" PRIxPTR "\" -> \"%#" PRIxPTR
  453. "\" [style = dashed];\n",
  454. ((mask & (ptruint) scan) / sizeof(DdNode)),
  455. ((mask & (ptruint) cuddE(scan)) /
  456. sizeof(DdNode)));
  457. }
  458. if (retval == EOF) goto failure;
  459. }
  460. scan = scan->next;
  461. }
  462. }
  463. }
  464. }
  465. /* Write constant labels. */
  466. nodelist = dd->constants.nodelist;
  467. slots = dd->constants.slots;
  468. for (j = 0; j < slots; j++) {
  469. scan = nodelist[j];
  470. while (scan != NULL) {
  471. if (st_is_member(visited,scan)) {
  472. retval = fprintf(fp,"\"%#" PRIxPTR "\" [label = \"%g\"];\n",
  473. ((mask & (ptruint) scan) / sizeof(DdNode)), cuddV(scan));
  474. if (retval == EOF) goto failure;
  475. }
  476. scan = scan->next;
  477. }
  478. }
  479. /* Write trailer and return. */
  480. retval = fprintf(fp,"}\n");
  481. if (retval == EOF) goto failure;
  482. st_free_table(visited);
  483. FREE(sorted);
  484. return(1);
  485. failure:
  486. if (sorted != NULL) FREE(sorted);
  487. if (support != NULL) Cudd_RecursiveDeref(dd,support);
  488. if (visited != NULL) st_free_table(visited);
  489. return(0);
  490. } /* end of Cudd_DumpDot */
  491. /**
  492. @brief Writes a daVinci file representing the argument BDDs.
  493. @details Writes a daVinci file representing the argument BDDs.
  494. Cudd_DumpDaVinci does not close the file: This is the caller
  495. responsibility. Cudd_DumpDaVinci uses a minimal unique subset of the
  496. hexadecimal address of a node as name for it. If the argument
  497. inames is non-null, it is assumed to hold the pointers to the names
  498. of the inputs. Similarly for onames.
  499. @return 1 in case of success; 0 otherwise (e.g., out-of-memory or
  500. file system full).
  501. @sideeffect None
  502. @see Cudd_DumpDot Cudd_PrintDebug Cudd_DumpBlif Cudd_DumpDDcal
  503. Cudd_DumpFactoredForm
  504. */
  505. int
  506. Cudd_DumpDaVinci(
  507. DdManager * dd /**< manager */,
  508. int n /**< number of output nodes to be dumped */,
  509. DdNode ** f /**< array of output nodes to be dumped */,
  510. char const * const * inames /**< array of input names (or NULL) */,
  511. char const * const * onames /**< array of output names (or NULL) */,
  512. FILE * fp /**< pointer to the dump file */)
  513. {
  514. DdNode *support = NULL;
  515. DdNode *scan;
  516. st_table *visited = NULL;
  517. int retval;
  518. int i;
  519. st_generator *gen;
  520. ptruint refAddr, diff, mask = 0;
  521. /* Initialize symbol table for visited nodes. */
  522. visited = st_init_table(st_ptrcmp, st_ptrhash);
  523. if (visited == NULL) goto failure;
  524. /* Collect all the nodes of this DD in the symbol table. */
  525. for (i = 0; i < n; i++) {
  526. retval = cuddCollectNodes(Cudd_Regular(f[i]),visited);
  527. if (retval == 0) goto failure;
  528. }
  529. /* Find how many most significant hex digits are identical
  530. ** in the addresses of all the nodes. Build a mask based
  531. ** on this knowledge, so that digits that carry no information
  532. ** will not be printed. This is done in two steps.
  533. ** 1. We scan the symbol table to find the bits that differ
  534. ** in at least 2 addresses.
  535. ** 2. We choose one of the possible masks. There are 8 possible
  536. ** masks for 32-bit integer, and 16 possible masks for 64-bit
  537. ** integers.
  538. */
  539. /* Find the bits that are different. */
  540. refAddr = (ptruint) Cudd_Regular(f[0]);
  541. diff = 0;
  542. gen = st_init_gen(visited);
  543. while (st_gen(gen, (void **) &scan, NULL)) {
  544. diff |= refAddr ^ (ptruint) scan;
  545. }
  546. st_free_gen(gen);
  547. /* Choose the mask. */
  548. for (i = 0; (unsigned) i < 8 * sizeof(ptruint); i += 4) {
  549. mask = ((ptruint) 1 << i) - 1;
  550. if (diff <= mask) break;
  551. }
  552. st_free_table(visited);
  553. /* Initialize symbol table for visited nodes. */
  554. visited = st_init_table(st_ptrcmp, st_ptrhash);
  555. if (visited == NULL) goto failure;
  556. retval = fprintf(fp, "[");
  557. if (retval == EOF) goto failure;
  558. /* Call the function that really gets the job done. */
  559. for (i = 0; i < n; i++) {
  560. if (onames == NULL) {
  561. retval = fprintf(fp,
  562. "l(\"f%d\",n(\"root\",[a(\"OBJECT\",\"f%d\")],",
  563. i,i);
  564. } else {
  565. retval = fprintf(fp,
  566. "l(\"%s\",n(\"root\",[a(\"OBJECT\",\"%s\")],",
  567. onames[i], onames[i]);
  568. }
  569. if (retval == EOF) goto failure;
  570. retval = fprintf(fp, "[e(\"edge\",[a(\"EDGECOLOR\",\"%s\"),a(\"_DIR\",\"none\")],",
  571. Cudd_IsComplement(f[i]) ? "red" : "blue");
  572. if (retval == EOF) goto failure;
  573. retval = ddDoDumpDaVinci(dd,Cudd_Regular(f[i]),fp,visited,inames,mask);
  574. if (retval == 0) goto failure;
  575. retval = fprintf(fp, ")]))%s", i == n-1 ? "" : ",");
  576. if (retval == EOF) goto failure;
  577. }
  578. /* Write trailer and return. */
  579. retval = fprintf(fp, "]\n");
  580. if (retval == EOF) goto failure;
  581. st_free_table(visited);
  582. return(1);
  583. failure:
  584. if (support != NULL) Cudd_RecursiveDeref(dd,support);
  585. if (visited != NULL) st_free_table(visited);
  586. return(0);
  587. } /* end of Cudd_DumpDaVinci */
  588. /**
  589. @brief Writes a DDcal file representing the argument BDDs.
  590. @details Writes a DDcal file representing the argument BDDs.
  591. Cudd_DumpDDcal does not close the file: This is the caller
  592. responsibility. Cudd_DumpDDcal uses a minimal unique subset of the
  593. hexadecimal address of a node as name for it. If the argument
  594. inames is non-null, it is assumed to hold the pointers to the names
  595. of the inputs. Similarly for onames.
  596. @return 1 in case of success; 0 otherwise (e.g., out-of-memory or
  597. file system full).
  598. @sideeffect None
  599. @see Cudd_DumpDot Cudd_PrintDebug Cudd_DumpBlif Cudd_DumpDaVinci
  600. Cudd_DumpFactoredForm
  601. */
  602. int
  603. Cudd_DumpDDcal(
  604. DdManager * dd /**< manager */,
  605. int n /**< number of output nodes to be dumped */,
  606. DdNode ** f /**< array of output nodes to be dumped */,
  607. char const * const * inames /**< array of input names (or NULL) */,
  608. char const * const * onames /**< array of output names (or NULL) */,
  609. FILE * fp /**< pointer to the dump file */)
  610. {
  611. DdNode *support = NULL;
  612. DdNode *scan;
  613. int *sorted = NULL;
  614. int nvars = dd->size;
  615. st_table *visited = NULL;
  616. int retval;
  617. int i;
  618. st_generator *gen;
  619. ptruint refAddr, diff, mask = 0;
  620. /* Initialize symbol table for visited nodes. */
  621. visited = st_init_table(st_ptrcmp, st_ptrhash);
  622. if (visited == NULL) goto failure;
  623. /* Collect all the nodes of this DD in the symbol table. */
  624. for (i = 0; i < n; i++) {
  625. retval = cuddCollectNodes(Cudd_Regular(f[i]),visited);
  626. if (retval == 0) goto failure;
  627. }
  628. /* Find how many most significant hex digits are identical
  629. ** in the addresses of all the nodes. Build a mask based
  630. ** on this knowledge, so that digits that carry no information
  631. ** will not be printed. This is done in two steps.
  632. ** 1. We scan the symbol table to find the bits that differ
  633. ** in at least 2 addresses.
  634. ** 2. We choose one of the possible masks. There are 8 possible
  635. ** masks for 32-bit integer, and 16 possible masks for 64-bit
  636. ** integers.
  637. */
  638. /* Find the bits that are different. */
  639. refAddr = (ptruint) Cudd_Regular(f[0]);
  640. diff = 0;
  641. gen = st_init_gen(visited);
  642. while (st_gen(gen, (void **) &scan, NULL)) {
  643. diff |= refAddr ^ (ptruint) scan;
  644. }
  645. st_free_gen(gen);
  646. /* Choose the mask. */
  647. for (i = 0; (unsigned) i < 8 * sizeof(ptruint); i += 4) {
  648. mask = ((ptruint) 1 << i) - 1;
  649. if (diff <= mask) break;
  650. }
  651. st_free_table(visited);
  652. /* Build a bit array with the support of f. */
  653. sorted = ALLOC(int,nvars);
  654. if (sorted == NULL) {
  655. dd->errorCode = CUDD_MEMORY_OUT;
  656. goto failure;
  657. }
  658. for (i = 0; i < nvars; i++) sorted[i] = 0;
  659. /* Take the union of the supports of each output function. */
  660. support = Cudd_VectorSupport(dd,f,n);
  661. if (support == NULL) goto failure;
  662. cuddRef(support);
  663. scan = support;
  664. while (!cuddIsConstant(scan)) {
  665. sorted[scan->index] = 1;
  666. scan = cuddT(scan);
  667. }
  668. Cudd_RecursiveDeref(dd,support);
  669. support = NULL; /* so that we do not try to free it in case of failure */
  670. for (i = 0; i < nvars; i++) {
  671. if (sorted[dd->invperm[i]]) {
  672. if (inames == NULL || inames[dd->invperm[i]] == NULL) {
  673. retval = fprintf(fp,"v%d", dd->invperm[i]);
  674. } else {
  675. retval = fprintf(fp,"%s", inames[dd->invperm[i]]);
  676. }
  677. if (retval == EOF) goto failure;
  678. }
  679. retval = fprintf(fp,"%s", i == nvars - 1 ? "\n" : " * ");
  680. if (retval == EOF) goto failure;
  681. }
  682. FREE(sorted);
  683. sorted = NULL;
  684. /* Initialize symbol table for visited nodes. */
  685. visited = st_init_table(st_ptrcmp, st_ptrhash);
  686. if (visited == NULL) goto failure;
  687. /* Call the function that really gets the job done. */
  688. for (i = 0; i < n; i++) {
  689. retval = ddDoDumpDDcal(dd,Cudd_Regular(f[i]),fp,visited,inames,mask);
  690. if (retval == 0) goto failure;
  691. if (onames == NULL) {
  692. retval = fprintf(fp, "f%d = ", i);
  693. } else {
  694. retval = fprintf(fp, "%s = ", onames[i]);
  695. }
  696. if (retval == EOF) goto failure;
  697. retval = fprintf(fp, "n%#" PRIxPTR "%s\n",
  698. (((ptruint) f[i] & mask) / sizeof(DdNode)),
  699. Cudd_IsComplement(f[i]) ? "'" : "");
  700. if (retval == EOF) goto failure;
  701. }
  702. /* Write trailer and return. */
  703. retval = fprintf(fp, "[");
  704. if (retval == EOF) goto failure;
  705. for (i = 0; i < n; i++) {
  706. if (onames == NULL) {
  707. retval = fprintf(fp, "f%d", i);
  708. } else {
  709. retval = fprintf(fp, "%s", onames[i]);
  710. }
  711. if (retval == EOF) goto failure;
  712. retval = fprintf(fp, "%s", i == n-1 ? "" : " ");
  713. if (retval == EOF) goto failure;
  714. }
  715. retval = fprintf(fp, "]\n");
  716. if (retval == EOF) goto failure;
  717. st_free_table(visited);
  718. return(1);
  719. failure:
  720. if (sorted != NULL) FREE(sorted);
  721. if (support != NULL) Cudd_RecursiveDeref(dd,support);
  722. if (visited != NULL) st_free_table(visited);
  723. return(0);
  724. } /* end of Cudd_DumpDDcal */
  725. /**
  726. @brief Writes factored forms representing the argument BDDs.
  727. @details Writes factored forms representing the argument BDDs. The
  728. format of the factored form is the one used in the genlib files for
  729. technology mapping in sis. Cudd_DumpFactoredForm does not close the
  730. file: This is the caller responsibility. Caution must be exercised
  731. because a factored form may be exponentially larger than the
  732. argument %BDD. If the argument inames is non-null, it is assumed to
  733. hold the pointers to the names of the inputs. Similarly for onames.
  734. If the number of output nodes is 0, it is interpreted as 1, but no
  735. output name followed by equal sign is printed before the factored
  736. form.
  737. @return 1 in case of success; 0 otherwise (e.g., file system full).
  738. @sideeffect None
  739. @see Cudd_DumpDot Cudd_PrintDebug Cudd_DumpBlif Cudd_DumpDaVinci
  740. Cudd_DumpDDcal
  741. */
  742. int
  743. Cudd_DumpFactoredForm(
  744. DdManager * dd /**< manager */,
  745. int n /**< number of output nodes to be dumped */,
  746. DdNode ** f /**< array of output nodes to be dumped */,
  747. char const * const * inames /**< array of input names (or NULL) */,
  748. char const * const * onames /**< array of output names (or NULL) */,
  749. FILE * fp /**< pointer to the dump file */)
  750. {
  751. int retval = 0;
  752. int i;
  753. int printName = n != 0;
  754. if (!printName) n = 1;
  755. /* Call the function that really gets the job done. */
  756. for (i = 0; i < n; i++) {
  757. if (printName) {
  758. if (onames == NULL) {
  759. retval = fprintf(fp, "f%d = ", i);
  760. } else {
  761. retval = fprintf(fp, "%s = ", onames[i]);
  762. }
  763. }
  764. if (retval == EOF) return(0);
  765. if (f[i] == DD_ONE(dd)) {
  766. retval = fprintf(fp, "CONST1");
  767. if (retval == EOF) return(0);
  768. } else if (f[i] == Cudd_Not(DD_ONE(dd)) || f[i] == DD_ZERO(dd)) {
  769. retval = fprintf(fp, "CONST0");
  770. if (retval == EOF) return(0);
  771. } else {
  772. retval = fprintf(fp, "%s", Cudd_IsComplement(f[i]) ? (Cudd_bddIsVar(dd, Cudd_Regular(f[i])) ? "!" : "!(") : "");
  773. if (retval == EOF) return(0);
  774. retval = ddDoDumpFactoredForm(dd,Cudd_Regular(f[i]),fp,inames);
  775. if (retval == 0) return(0);
  776. retval = fprintf(fp, "%s", Cudd_IsComplement(f[i]) && !Cudd_bddIsVar(dd, Cudd_Regular(f[i])) ? ")" : "");
  777. if (retval == EOF) return(0);
  778. }
  779. retval = fprintf(fp, "%s", i == n-1 ? "" : "\n");
  780. if (retval == EOF) return(0);
  781. }
  782. return(1);
  783. } /* end of Cudd_DumpFactoredForm */
  784. /**
  785. @brief Returns a string with the factored form of the argument BDDs
  786. @details The factored form uses & for conjunction, | for disjunction
  787. and ! for negation. Caution must be exercised because a factored
  788. form may be exponentially larger than the argument %BDD. If the
  789. argument inames is non-null, it is assumed to hold the pointers to
  790. the names of the inputs.
  791. @return a string in case of success; NULL otherwise.
  792. @sideeffect None
  793. @see Cudd_DumpDot Cudd_PrintDebug Cudd_DumpBlif Cudd_DumpDaVinci
  794. Cudd_DumpDDcal Cudd_DumpFactoredForm
  795. */
  796. char *
  797. Cudd_FactoredFormString(
  798. DdManager *dd,
  799. DdNode *f,
  800. char const * const * inames)
  801. {
  802. cstringstream stream = newStringStream();
  803. int err, retval;
  804. char * str;
  805. if (!stream) {
  806. return(0);
  807. }
  808. /* Call the function that really gets the job done. */
  809. if (f == DD_ONE(dd)) {
  810. err = appendStringStringStream(stream, "true");
  811. if (err) {
  812. deleteStringStream(stream);
  813. return(0);
  814. }
  815. } else if (f == Cudd_Not(DD_ONE(dd)) || f == DD_ZERO(dd)) {
  816. err = appendStringStringStream(stream, "false");
  817. if (err) {
  818. deleteStringStream(stream);
  819. return(0);
  820. }
  821. } else {
  822. err = appendStringStringStream(
  823. stream, Cudd_IsComplement(f) ?
  824. (Cudd_bddIsVar(dd, Cudd_Regular(f)) ? "!" : "!(") : "");
  825. if (err) {
  826. deleteStringStream(stream);
  827. return(0);
  828. }
  829. retval = ddDoFactoredFormString(dd,Cudd_Regular(f),stream,inames);
  830. if (retval == 0) {
  831. deleteStringStream(stream);
  832. return(0);
  833. }
  834. err = appendStringStringStream(
  835. stream, Cudd_IsComplement(f) &&
  836. !Cudd_bddIsVar(dd, Cudd_Regular(f)) ? ")" : "");
  837. if (err) {
  838. deleteStringStream(stream);
  839. return(0);
  840. }
  841. }
  842. str = stringFromStringStream(stream);
  843. deleteStringStream(stream);
  844. return(str);
  845. } /* end of Cudd_FactoredFormString */
  846. /*---------------------------------------------------------------------------*/
  847. /* Definition of internal functions */
  848. /*---------------------------------------------------------------------------*/
  849. /*---------------------------------------------------------------------------*/
  850. /* Definition of static functions */
  851. /*---------------------------------------------------------------------------*/
  852. /**
  853. @brief Performs the recursive step of Cudd_DumpBlif.
  854. @details Traverses the %BDD f and writes a multiplexer-network
  855. description to the file pointed by fp in blif format. f is assumed
  856. to be a regular pointer and ddDoDumpBlif guarantees this assumption
  857. in the recursive calls.
  858. @sideeffect None
  859. */
  860. static int
  861. ddDoDumpBlif(
  862. DdManager * dd,
  863. DdNode * f,
  864. FILE * fp,
  865. st_table * visited,
  866. char const * const * names,
  867. int mv)
  868. {
  869. DdNode *T, *E;
  870. int retval;
  871. #ifdef DD_DEBUG
  872. assert(!Cudd_IsComplement(f));
  873. #endif
  874. /* If already visited, nothing to do. */
  875. if (st_is_member(visited, f) == 1)
  876. return(1);
  877. /* Check for abnormal condition that should never happen. */
  878. if (f == NULL)
  879. return(0);
  880. /* Mark node as visited. */
  881. if (st_insert(visited, f, NULL) == ST_OUT_OF_MEM)
  882. return(0);
  883. /* Check for special case: If constant node, generate constant 1. */
  884. if (f == DD_ONE(dd)) {
  885. retval = fprintf(fp, ".names %" PRIxPTR "\n1\n",(ptruint) f / (ptruint) sizeof(DdNode));
  886. if (retval == EOF) {
  887. return(0);
  888. } else {
  889. return(1);
  890. }
  891. }
  892. /* Check whether this is an ADD. We deal with 0-1 ADDs, but not
  893. ** with the general case.
  894. */
  895. if (f == DD_ZERO(dd)) {
  896. retval = fprintf(fp, ".names %" PRIxPTR "\n%s",
  897. (ptruint) f / (ptruint) sizeof(DdNode),
  898. mv ? "0\n" : "");
  899. if (retval == EOF) {
  900. return(0);
  901. } else {
  902. return(1);
  903. }
  904. }
  905. if (cuddIsConstant(f))
  906. return(0);
  907. /* Recursive calls. */
  908. T = cuddT(f);
  909. retval = ddDoDumpBlif(dd,T,fp,visited,names,mv);
  910. if (retval != 1) return(retval);
  911. E = Cudd_Regular(cuddE(f));
  912. retval = ddDoDumpBlif(dd,E,fp,visited,names,mv);
  913. if (retval != 1) return(retval);
  914. /* Write multiplexer taking complement arc into account. */
  915. if (names != NULL) {
  916. retval = fprintf(fp,".names %s", names[f->index]);
  917. } else {
  918. #if SIZEOF_VOID_P == 8 && SIZEOF_INT == 4
  919. retval = fprintf(fp,".names %u", f->index);
  920. #else
  921. retval = fprintf(fp,".names %hu", f->index);
  922. #endif
  923. }
  924. if (retval == EOF)
  925. return(0);
  926. if (mv) {
  927. if (Cudd_IsComplement(cuddE(f))) {
  928. retval = fprintf(fp," %" PRIxPTR " %" PRIxPTR " %" PRIxPTR "\n.def 0\n1 1 - 1\n0 - 0 1\n",
  929. (ptruint) T / (ptruint) sizeof(DdNode),
  930. (ptruint) E / (ptruint) sizeof(DdNode),
  931. (ptruint) f / (ptruint) sizeof(DdNode));
  932. } else {
  933. retval = fprintf(fp," %" PRIxPTR " %" PRIxPTR " %" PRIxPTR "\n.def 0\n1 1 - 1\n0 - 1 1\n",
  934. (ptruint) T / (ptruint) sizeof(DdNode),
  935. (ptruint) E / (ptruint) sizeof(DdNode),
  936. (ptruint) f / (ptruint) sizeof(DdNode));
  937. }
  938. } else {
  939. if (Cudd_IsComplement(cuddE(f))) {
  940. retval = fprintf(fp," %" PRIxPTR " %" PRIxPTR " %" PRIxPTR "\n11- 1\n0-0 1\n",
  941. (ptruint) T / (ptruint) sizeof(DdNode),
  942. (ptruint) E / (ptruint) sizeof(DdNode),
  943. (ptruint) f / (ptruint) sizeof(DdNode));
  944. } else {
  945. retval = fprintf(fp," %" PRIxPTR " %" PRIxPTR " %" PRIxPTR "\n11- 1\n0-1 1\n",
  946. (ptruint) T / (ptruint) sizeof(DdNode),
  947. (ptruint) E / (ptruint) sizeof(DdNode),
  948. (ptruint) f / (ptruint) sizeof(DdNode));
  949. }
  950. }
  951. if (retval == EOF) {
  952. return(0);
  953. } else {
  954. return(1);
  955. }
  956. } /* end of ddDoDumpBlif */
  957. /**
  958. @brief Performs the recursive step of Cudd_DumpDaVinci.
  959. @details Traverses the %BDD f and writes a term expression to the
  960. file pointed by fp in daVinci format. f is assumed to be a regular
  961. pointer and ddDoDumpDaVinci guarantees this assumption in the
  962. recursive calls.
  963. @sideeffect None
  964. */
  965. static int
  966. ddDoDumpDaVinci(
  967. DdManager * dd,
  968. DdNode * f,
  969. FILE * fp,
  970. st_table * visited,
  971. char const * const * names,
  972. ptruint mask)
  973. {
  974. DdNode *T, *E;
  975. int retval;
  976. ptruint id;
  977. #ifdef DD_DEBUG
  978. assert(!Cudd_IsComplement(f));
  979. #endif
  980. id = ((ptruint) f & mask) / sizeof(DdNode);
  981. /* If already visited, insert a reference. */
  982. if (st_is_member(visited, f) == 1) {
  983. retval = fprintf(fp,"r(\"%#" PRIxPTR "\")", id);
  984. if (retval == EOF) {
  985. return(0);
  986. } else {
  987. return(1);
  988. }
  989. }
  990. /* Check for abnormal condition that should never happen. */
  991. if (f == NULL)
  992. return(0);
  993. /* Mark node as visited. */
  994. if (st_insert(visited, f, NULL) == ST_OUT_OF_MEM)
  995. return(0);
  996. /* Check for special case: If constant node, generate constant 1. */
  997. if (Cudd_IsConstantInt(f)) {
  998. retval = fprintf(fp,
  999. "l(\"%#" PRIxPTR
  1000. "\",n(\"constant\",[a(\"OBJECT\",\"%g\")],[]))",
  1001. id, cuddV(f));
  1002. if (retval == EOF) {
  1003. return(0);
  1004. } else {
  1005. return(1);
  1006. }
  1007. }
  1008. /* Recursive calls. */
  1009. if (names != NULL) {
  1010. retval = fprintf(fp, "l(\"%#" PRIxPTR
  1011. "\",n(\"internal\",[a(\"OBJECT\",\"%s\"),",
  1012. id, names[f->index]);
  1013. } else {
  1014. #if SIZEOF_VOID_P == 8
  1015. retval = fprintf(fp, "l(\"%#" PRIxPTR
  1016. "\",n(\"internal\",[a(\"OBJECT\",\"%u\"),",
  1017. id, f->index);
  1018. #else
  1019. retval = fprintf(fp, "l(\"%#"PRIxPTR
  1020. "\",n(\"internal\",[a(\"OBJECT\",\"%hu\"),",
  1021. id, f->index);
  1022. #endif
  1023. }
  1024. if (retval == EOF) return(0);
  1025. retval = fprintf(fp, "a(\"_GO\",\"ellipse\")],[e(\"then\",[a(\"EDGECOLOR\",\"blue\"),a(\"_DIR\",\"none\")],");
  1026. if (retval == EOF) return(0);
  1027. T = cuddT(f);
  1028. retval = ddDoDumpDaVinci(dd,T,fp,visited,names,mask);
  1029. if (retval != 1) return(retval);
  1030. retval = fprintf(fp, "),e(\"else\",[a(\"EDGECOLOR\",\"%s\"),a(\"_DIR\",\"none\")],",
  1031. Cudd_IsComplement(cuddE(f)) ? "red" : "green");
  1032. if (retval == EOF) return(0);
  1033. E = Cudd_Regular(cuddE(f));
  1034. retval = ddDoDumpDaVinci(dd,E,fp,visited,names,mask);
  1035. if (retval != 1) return(retval);
  1036. retval = fprintf(fp,")]))");
  1037. if (retval == EOF) {
  1038. return(0);
  1039. } else {
  1040. return(1);
  1041. }
  1042. } /* end of ddDoDumpDaVinci */
  1043. /**
  1044. @brief Performs the recursive step of Cudd_DumpDDcal.
  1045. @details Traverses the %BDD f and writes a line for each node to the
  1046. file pointed by fp in DDcal format. f is assumed to be a regular
  1047. pointer and ddDoDumpDDcal guarantees this assumption in the
  1048. recursive calls.
  1049. @sideeffect None
  1050. */
  1051. static int
  1052. ddDoDumpDDcal(
  1053. DdManager * dd,
  1054. DdNode * f,
  1055. FILE * fp,
  1056. st_table * visited,
  1057. char const * const * names,
  1058. ptruint mask)
  1059. {
  1060. DdNode *T, *E;
  1061. int retval;
  1062. ptruint id, idT, idE;
  1063. #ifdef DD_DEBUG
  1064. assert(!Cudd_IsComplement(f));
  1065. #endif
  1066. id = ((ptruint) f & mask) / sizeof(DdNode);
  1067. /* If already visited, do nothing. */
  1068. if (st_is_member(visited, f) == 1) {
  1069. return(1);
  1070. }
  1071. /* Check for abnormal condition that should never happen. */
  1072. if (f == NULL)
  1073. return(0);
  1074. /* Mark node as visited. */
  1075. if (st_insert(visited, f, NULL) == ST_OUT_OF_MEM)
  1076. return(0);
  1077. /* Check for special case: If constant node, assign constant. */
  1078. if (Cudd_IsConstantInt(f)) {
  1079. if (f != DD_ONE(dd) && f != DD_ZERO(dd))
  1080. return(0);
  1081. retval = fprintf(fp, "n%#" PRIxPTR" = %g\n", id, cuddV(f));
  1082. if (retval == EOF) {
  1083. return(0);
  1084. } else {
  1085. return(1);
  1086. }
  1087. }
  1088. /* Recursive calls. */
  1089. T = cuddT(f);
  1090. retval = ddDoDumpDDcal(dd,T,fp,visited,names,mask);
  1091. if (retval != 1) return(retval);
  1092. E = Cudd_Regular(cuddE(f));
  1093. retval = ddDoDumpDDcal(dd,E,fp,visited,names,mask);
  1094. if (retval != 1) return(retval);
  1095. idT = ((ptruint) T & mask) / sizeof(DdNode);
  1096. idE = ((ptruint) E & mask) / sizeof(DdNode);
  1097. if (names != NULL) {
  1098. retval = fprintf(fp, "n%#" PRIxPTR " = %s * n%#" PRIxPTR
  1099. " + %s' * n%#" PRIxPTR "%s\n",
  1100. id, names[f->index], idT, names[f->index],
  1101. idE, Cudd_IsComplement(cuddE(f)) ? "'" : "");
  1102. } else {
  1103. #if SIZEOF_VOID_P == 8
  1104. retval = fprintf(fp, "n%#" PRIxPTR " = v%u * n%#" PRIxPTR
  1105. " + v%u' * n%#" PRIxPTR "%s\n",
  1106. id, f->index, idT, f->index,
  1107. idE, Cudd_IsComplement(cuddE(f)) ? "'" : "");
  1108. #else
  1109. retval = fprintf(fp, "n%#"PRIxPTR" = v%hu * n%#"PRIxPTR
  1110. " + v%hu' * n%#"PRIxPTR"%s\n",
  1111. id, f->index, idT, f->index,
  1112. idE, Cudd_IsComplement(cuddE(f)) ? "'" : "");
  1113. #endif
  1114. }
  1115. if (retval == EOF) {
  1116. return(0);
  1117. } else {
  1118. return(1);
  1119. }
  1120. } /* end of ddDoDumpDDcal */
  1121. /**
  1122. @brief Performs the recursive step of Cudd_DumpFactoredForm.
  1123. @details Traverses the %BDD f and writes a factored form for each
  1124. node to the file pointed by fp in terms of the factored forms of the
  1125. children. Constants are propagated, and absorption is applied. f is
  1126. assumed to be a regular pointer and ddDoDumpFActoredForm guarantees
  1127. this assumption in the recursive calls.
  1128. @sideeffect None
  1129. @see Cudd_DumpFactoredForm
  1130. */
  1131. static int
  1132. ddDoDumpFactoredForm(
  1133. DdManager * dd,
  1134. DdNode * f,
  1135. FILE * fp,
  1136. char const * const * names)
  1137. {
  1138. DdNode *T, *E;
  1139. int retval;
  1140. #ifdef DD_DEBUG
  1141. assert(!Cudd_IsComplement(f));
  1142. assert(!cuddIsConstant(f));
  1143. #endif
  1144. /* Check for abnormal condition that should never happen. */
  1145. if (f == NULL)
  1146. return(0);
  1147. /* Recursive calls. */
  1148. T = cuddT(f);
  1149. E = cuddE(f);
  1150. if (T != DD_ZERO(dd)) {
  1151. if (E != DD_ONE(dd)) {
  1152. if (names != NULL) {
  1153. retval = fprintf(fp, "%s", names[f->index]);
  1154. } else {
  1155. #if SIZEOF_VOID_P == 8 && SIZEOF_INT == 4
  1156. retval = fprintf(fp, "x%u", f->index);
  1157. #else
  1158. retval = fprintf(fp, "x%hu", f->index);
  1159. #endif
  1160. }
  1161. if (retval == EOF) return(0);
  1162. }
  1163. if (T != DD_ONE(dd)) {
  1164. // retval = fprintf(fp, "%s(", E != DD_ONE(dd) ? " * " : "");
  1165. retval = fprintf(fp, "%s%s", E != DD_ONE(dd) ? " * " : "", Cudd_bddIsVar(dd, T) ? "" : "(");
  1166. if (retval == EOF) return(0);
  1167. retval = ddDoDumpFactoredForm(dd,T,fp,names);
  1168. if (retval != 1) return(retval);
  1169. retval = fprintf(fp, "%s", Cudd_bddIsVar(dd, T) ? "" : ")");
  1170. if (retval == EOF) return(0);
  1171. }
  1172. if (E == Cudd_Not(DD_ONE(dd)) || E == DD_ZERO(dd)) return(1);
  1173. retval = fprintf(fp, " + ");
  1174. if (retval == EOF) return(0);
  1175. }
  1176. E = Cudd_Regular(E);
  1177. if (T != DD_ONE(dd)) {
  1178. if (names != NULL) {
  1179. retval = fprintf(fp, "!%s", names[f->index]);
  1180. } else {
  1181. #if SIZEOF_VOID_P == 8 && SIZEOF_INT == 4
  1182. retval = fprintf(fp, "!x%u", f->index);
  1183. #else
  1184. retval = fprintf(fp, "!x%hu", f->index);
  1185. #endif
  1186. }
  1187. if (retval == EOF) return(0);
  1188. }
  1189. if (E != DD_ONE(dd)) {
  1190. retval = fprintf(fp, "%s%s%s", T != DD_ONE(dd) ? " * " : "",
  1191. E != cuddE(f) ? "!" : "", Cudd_bddIsVar(dd, E) ? "" : "(");
  1192. if (retval == EOF) return(0);
  1193. retval = ddDoDumpFactoredForm(dd,E,fp,names);
  1194. if (retval != 1) return(retval);
  1195. retval = fprintf(fp, "%s", Cudd_bddIsVar(dd, E) ? "" : "(");
  1196. if (retval == EOF) return(0);
  1197. }
  1198. return(1);
  1199. } /* end of ddDoDumpFactoredForm */
  1200. /**
  1201. @brief Performs the recursive step of Cudd_DumpFactoredForm.
  1202. @details Traverses the %BDD f and writes a factored form for each
  1203. node to the file pointed by fp in terms of the factored forms of the
  1204. children. Constants are propagated, and absorption is applied. f is
  1205. assumed to be a regular pointer and ddDoDumpFActoredForm guarantees
  1206. this assumption in the recursive calls.
  1207. @sideeffect None
  1208. @see Cudd_DumpFactoredForm
  1209. */
  1210. static int
  1211. ddDoFactoredFormString(
  1212. DdManager * dd,
  1213. DdNode * f,
  1214. cstringstream stream,
  1215. char const * const * names)
  1216. {
  1217. DdNode *T, *E;
  1218. int retval, err;
  1219. #ifdef DD_DEBUG
  1220. assert(!Cudd_IsComplement(f));
  1221. assert(!cuddIsConstant(f));
  1222. #endif
  1223. /* Check for abnormal condition that should never happen. */
  1224. if (f == NULL)
  1225. return(0);
  1226. /* Recursive calls. */
  1227. T = cuddT(f);
  1228. E = cuddE(f);
  1229. if (T != DD_ZERO(dd)) {
  1230. if (E != DD_ONE(dd)) {
  1231. if (names != NULL) {
  1232. err = appendStringStringStream(stream, names[f->index]);
  1233. } else {
  1234. err = appendCharStringStream(stream, 'x');
  1235. if (err) return(0);
  1236. err = appendUnsignedStringStream(stream, (unsigned) f->index);
  1237. }
  1238. if (err) return(0);
  1239. }
  1240. if (T != DD_ONE(dd)) {
  1241. err = appendStringStringStream(stream, E != DD_ONE(dd) ? " & " : "");
  1242. if (err) return(0);
  1243. err = appendStringStringStream(stream, Cudd_bddIsVar(dd, T) ? "" : "(");
  1244. if (err) return(0);
  1245. retval = ddDoFactoredFormString(dd,T,stream,names);
  1246. if (retval != 1) return(retval);
  1247. err = appendStringStringStream(stream, Cudd_bddIsVar(dd, T) ? "" : ")");
  1248. if (err) return(0);
  1249. }
  1250. if (E == Cudd_Not(DD_ONE(dd)) || E == DD_ZERO(dd)) return(1);
  1251. err = appendStringStringStream(stream, " | ");
  1252. if (err) return(0);
  1253. }
  1254. E = Cudd_Regular(E);
  1255. if (T != DD_ONE(dd)) {
  1256. err = appendCharStringStream(stream, '!');
  1257. if (err) return(0);
  1258. if (names != NULL) {
  1259. err = appendStringStringStream(stream, names[f->index]);
  1260. } else {
  1261. err = appendCharStringStream(stream, 'x');
  1262. if (err) return(0);
  1263. err = appendUnsignedStringStream(stream, (unsigned) f->index);
  1264. }
  1265. if (err) return(0);
  1266. }
  1267. if (E != DD_ONE(dd)) {
  1268. err = appendStringStringStream(stream, T != DD_ONE(dd) ? " & " : "");
  1269. if (err) return(0);
  1270. err = appendStringStringStream(stream, E != cuddE(f) ? "!" : "");
  1271. if (err) return(0);
  1272. err = appendStringStringStream(stream, Cudd_bddIsVar(dd, E) ? "" : "(");
  1273. if (err) return(0);
  1274. retval = ddDoFactoredFormString(dd,E,stream,names);
  1275. if (retval != 1) return(retval);
  1276. err = appendStringStringStream(stream, Cudd_bddIsVar(dd, E) ? "" : ")");
  1277. if (err) return(0);
  1278. }
  1279. return(1);
  1280. } /* end of ddDoFactoredFormString */