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.

1117 lines
32 KiB

  1. /**CFile**********************************************************************
  2. FileName [dddmpStoreBdd.c]
  3. PackageName [dddmp]
  4. Synopsis [Functions to write BDDs to file.]
  5. Description [Functions to write BDDs to file.
  6. BDDs are represended on file either in text or binary format under the
  7. following rules. A file contains a forest of BDDs (a vector of
  8. Boolean functions). BDD nodes are numbered with contiguous numbers,
  9. from 1 to NNodes (total number of nodes on a file). 0 is not used to
  10. allow negative node indexes for complemented edges. A file contains
  11. a header, including information about variables and roots to BDD
  12. functions, followed by the list of nodes. BDD nodes are listed
  13. according to their numbering, and in the present implementation
  14. numbering follows a post-order strategy, in such a way that a node
  15. is never listed before its Then/Else children.
  16. ]
  17. Author [Gianpiero Cabodi and Stefano Quer]
  18. Copyright [
  19. Copyright (c) 2004 by Politecnico di Torino.
  20. All Rights Reserved. This software is for educational purposes only.
  21. Permission is given to academic institutions to use, copy, and modify
  22. this software and its documentation provided that this introductory
  23. message is not removed, that this software and its documentation is
  24. used for the institutions' internal research and educational purposes,
  25. and that no monies are exchanged. No guarantee is expressed or implied
  26. by the distribution of this code.
  27. Send bug-reports and/or questions to:
  28. {gianpiero.cabodi,stefano.quer}@polito.it.
  29. ]
  30. ******************************************************************************/
  31. #include "dddmpInt.h"
  32. /*---------------------------------------------------------------------------*/
  33. /* Stucture declarations */
  34. /*---------------------------------------------------------------------------*/
  35. /*---------------------------------------------------------------------------*/
  36. /* Type declarations */
  37. /*---------------------------------------------------------------------------*/
  38. /*---------------------------------------------------------------------------*/
  39. /* Variable declarations */
  40. /*---------------------------------------------------------------------------*/
  41. /*---------------------------------------------------------------------------*/
  42. /* Macro declarations */
  43. /*---------------------------------------------------------------------------*/
  44. /**AutomaticStart*************************************************************/
  45. /*---------------------------------------------------------------------------*/
  46. /* Static function prototypes */
  47. /*---------------------------------------------------------------------------*/
  48. static int NodeStoreRecurBdd(DdManager *ddMgr, DdNode *f, int mode, int *supportids, char **varnames, int *outids, FILE *fp);
  49. static int NodeTextStoreBdd(DdManager *ddMgr, DdNode *f, int mode, int *supportids, char **varnames, int *outids, FILE *fp, int idf, int vf, int idT, int idE);
  50. static int NodeBinaryStoreBdd(DdManager *ddMgr, DdNode *f, int mode, int *supportids, char **varnames, int *outids, FILE *fp, int idf, int vf, int idT, int idE, int vT, int vE, DdNode *T, DdNode *E);
  51. /**AutomaticEnd***************************************************************/
  52. /*---------------------------------------------------------------------------*/
  53. /* Definition of exported functions */
  54. /*---------------------------------------------------------------------------*/
  55. /**Function********************************************************************
  56. Synopsis [Writes a dump file representing the argument BDD.]
  57. Description [Dumps the argument BDD to file. Dumping is done through
  58. Dddmp_cuddBddArrayStore. A dummy array of 1 BDD root is
  59. used for this purpose.
  60. ]
  61. SideEffects [Nodes are temporarily removed from unique hash. They are
  62. re-linked after the store operation in a modified order.
  63. ]
  64. SeeAlso [Dddmp_cuddBddLoad Dddmp_cuddBddArrayLoad]
  65. ******************************************************************************/
  66. int
  67. Dddmp_cuddBddStore (
  68. DdManager *ddMgr /* IN: DD Manager */,
  69. char *ddname /* IN: DD name (or NULL) */,
  70. DdNode *f /* IN: BDD root to be stored */,
  71. char **varnames /* IN: array of variable names (or NULL) */,
  72. int *auxids /* IN: array of converted var ids */,
  73. int mode /* IN: storing mode selector */,
  74. Dddmp_VarInfoType varinfo /* IN: extra info for variables in text mode */,
  75. char *fname /* IN: File name */,
  76. FILE *fp /* IN: File pointer to the store file */
  77. )
  78. {
  79. int retValue;
  80. DdNode *tmpArray[1];
  81. tmpArray[0] = f;
  82. retValue = Dddmp_cuddBddArrayStore (ddMgr,ddname,1,tmpArray,NULL,
  83. varnames, auxids, mode, varinfo, fname, fp);
  84. return (retValue);
  85. }
  86. /**Function********************************************************************
  87. Synopsis [Writes a dump file representing the argument Array of BDDs.]
  88. Description [Dumps the argument array of BDDs to file. Dumping is either
  89. in text or binary form. BDDs are stored to the fp (already
  90. open) file if not NULL. Otherwise the file whose name is
  91. fname is opened in write mode. The header has the same format
  92. for both textual and binary dump. Names are allowed for input
  93. variables (vnames) and for represented functions (rnames).
  94. For sake of generality and because of dynamic variable
  95. ordering both variable IDs and permuted IDs are included.
  96. New IDs are also supported (auxids). Variables are identified
  97. with incremental numbers. according with their positiom in
  98. the support set. In text mode, an extra info may be added,
  99. chosen among the following options: name, ID, PermID, or an
  100. auxiliary id. Since conversion from DD pointers to integers
  101. is required, DD nodes are temporarily removed from the unique
  102. hash table. This allows the use of the next field to store
  103. node IDs.
  104. ]
  105. SideEffects [Nodes are temporarily removed from the unique hash
  106. table. They are re-linked after the store operation in a
  107. modified order.
  108. ]
  109. SeeAlso [Dddmp_cuddBddStore, Dddmp_cuddBddLoad,
  110. Dddmp_cuddBddArrayLoad
  111. ]
  112. ******************************************************************************/
  113. int
  114. Dddmp_cuddBddArrayStore (
  115. DdManager *ddMgr /* IN: DD Manager */,
  116. char *ddname /* IN: dd name (or NULL) */,
  117. int nRoots /* IN: number of output BDD roots to be stored */,
  118. DdNode **f /* IN: array of BDD roots to be stored */,
  119. char **rootnames /* IN: array of root names (or NULL) */,
  120. char **varnames /* IN: array of variable names (or NULL) */,
  121. int *auxids /* IN: array of converted var IDs */,
  122. int mode /* IN: storing mode selector */,
  123. Dddmp_VarInfoType varinfo /* IN: extra info for variables in text mode */,
  124. char *fname /* IN: File name */,
  125. FILE *fp /* IN: File pointer to the store file */
  126. )
  127. {
  128. int retValue;
  129. #ifdef DDDMP_DEBUG
  130. #ifndef __alpha__
  131. int retValueBis;
  132. retValueBis = Cudd_DebugCheck (ddMgr);
  133. if (retValueBis == 1) {
  134. fprintf (stderr, "Inconsistency Found During BDD Store.\n");
  135. fflush (stderr);
  136. } else {
  137. if (retValueBis == CUDD_OUT_OF_MEM) {
  138. fprintf (stderr, "Out of Memory During BDD Store.\n");
  139. fflush (stderr);
  140. }
  141. }
  142. #endif
  143. #endif
  144. retValue = DddmpCuddBddArrayStore (DDDMP_BDD, ddMgr, ddname, nRoots, f,
  145. rootnames, varnames, auxids, mode, varinfo, fname, fp);
  146. #ifdef DDDMP_DEBUG
  147. #ifndef __alpha__
  148. retValueBis = Cudd_DebugCheck (ddMgr);
  149. if (retValueBis == 1) {
  150. fprintf (stderr, "Inconsistency Found During BDD Store.\n");
  151. fflush (stderr);
  152. } else {
  153. if (retValueBis == CUDD_OUT_OF_MEM) {
  154. fprintf (stderr, "Out of Memory During BDD Store.\n");
  155. fflush (stderr);
  156. }
  157. }
  158. #endif
  159. #endif
  160. return (retValue);
  161. }
  162. /*---------------------------------------------------------------------------*/
  163. /* Definition of internal functions */
  164. /*---------------------------------------------------------------------------*/
  165. /**Function********************************************************************
  166. Synopsis [Writes a dump file representing the argument Array of
  167. BDDs.
  168. ]
  169. Description [Dumps the argument array of BDDs to file.
  170. Internal function doing inner steps of store for BDDs.
  171. ]
  172. SideEffects [Nodes are temporarily removed from the unique hash
  173. table. They are re-linked after the store operation in a
  174. modified order.
  175. ]
  176. SeeAlso [Dddmp_cuddBddStore, Dddmp_cuddBddLoad,
  177. Dddmp_cuddBddArrayLoad
  178. ]
  179. ******************************************************************************/
  180. int
  181. DddmpCuddBddArrayStore (
  182. Dddmp_DecompType ddType /* IN: Selects the decomp type BDD */,
  183. DdManager *ddMgr /* IN: DD Manager */,
  184. char *ddname /* IN: DD name (or NULL) */,
  185. int nRoots /* IN: number of output BDD roots to be stored */,
  186. DdNode **f /* IN: array of DD roots to be stored */,
  187. char **rootnames /* IN: array of root names (or NULL) */,
  188. char **varnames /* IN: array of variable names (or NULL) */,
  189. int *auxids /* IN: array of converted var IDs */,
  190. int mode /* IN: storing mode selector */,
  191. Dddmp_VarInfoType varinfo /* IN: extra info for variables in text mode */,
  192. char *fname /* IN: File name */,
  193. FILE *fp /* IN: File pointer to the store file */
  194. )
  195. {
  196. DdNode *support = NULL;
  197. DdNode *scan;
  198. int *ids = NULL;
  199. int *permids = NULL;
  200. int *invpermids = NULL;
  201. int *supportids = NULL;
  202. int *outids = NULL;
  203. char **outvarnames = NULL;
  204. int nVars = ddMgr->size;
  205. int nnodes;
  206. int retValue;
  207. int i, var;
  208. int fileToClose = 0;
  209. /*
  210. * Check DD Type
  211. */
  212. Dddmp_CheckAndGotoLabel (ddType==DDDMP_ADD,
  213. "Error writing to file: ADD Type.", failure);
  214. /*
  215. * Check if File needs to be opened in the proper mode.
  216. */
  217. if (fp == NULL) {
  218. fp = fopen (fname, "w");
  219. Dddmp_CheckAndGotoLabel (fp==NULL, "Error opening file.",
  220. failure);
  221. fileToClose = 1;
  222. }
  223. /*
  224. * Force binary mode if automatic.
  225. */
  226. switch (mode) {
  227. case DDDMP_MODE_TEXT:
  228. case DDDMP_MODE_BINARY:
  229. break;
  230. case DDDMP_MODE_DEFAULT:
  231. mode = DDDMP_MODE_BINARY;
  232. break;
  233. default:
  234. mode = DDDMP_MODE_BINARY;
  235. break;
  236. }
  237. /*
  238. * Alloc vectors for variable IDs, perm IDs and support IDs.
  239. * +1 to include a slot for terminals.
  240. */
  241. ids = DDDMP_ALLOC (int, nVars);
  242. Dddmp_CheckAndGotoLabel (ids==NULL, "Error allocating memory.", failure);
  243. permids = DDDMP_ALLOC (int, nVars);
  244. Dddmp_CheckAndGotoLabel (permids==NULL, "Error allocating memory.", failure);
  245. invpermids = DDDMP_ALLOC (int, nVars);
  246. Dddmp_CheckAndGotoLabel (invpermids==NULL, "Error allocating memory.",
  247. failure);
  248. supportids = DDDMP_ALLOC (int, nVars+1);
  249. Dddmp_CheckAndGotoLabel (supportids==NULL, "Error allocating memory.",
  250. failure);
  251. for (i=0; i<nVars; i++) {
  252. ids[i] = permids[i] = invpermids[i] = supportids[i] = (-1);
  253. }
  254. /* StQ */
  255. supportids[nVars] = -1;
  256. /*
  257. * Take the union of the supports of each output function.
  258. * skip NULL functions.
  259. * Set permids and invpermids of support variables to the proper values.
  260. */
  261. for (i=0; i<nRoots; i++) {
  262. if (f[i] == NULL) {
  263. continue;
  264. }
  265. support = Cudd_Support (ddMgr, f[i]);
  266. Dddmp_CheckAndGotoLabel (support==NULL, "NULL support returned.",
  267. failure);
  268. cuddRef (support);
  269. scan = support;
  270. while (!cuddIsConstant(scan)) {
  271. ids[scan->index] = scan->index;
  272. permids[scan->index] = ddMgr->perm[scan->index];
  273. invpermids[ddMgr->perm[scan->index]] = scan->index;
  274. scan = cuddT (scan);
  275. }
  276. Cudd_RecursiveDeref (ddMgr, support);
  277. }
  278. /* so that we do not try to free it in case of failure */
  279. support = NULL;
  280. /*
  281. * Set supportids to incremental (shrinked) values following the ordering.
  282. */
  283. for (i=0, var=0; i<nVars; i++) {
  284. if (invpermids[i] >= 0) {
  285. supportids[invpermids[i]] = var++;
  286. }
  287. }
  288. /* set a dummy id for terminal nodes */
  289. supportids[nVars] = var;
  290. /*
  291. * Select conversion array for extra var info
  292. */
  293. switch (mode) {
  294. case DDDMP_MODE_TEXT:
  295. switch (varinfo) {
  296. case DDDMP_VARIDS:
  297. outids = ids;
  298. break;
  299. case DDDMP_VARPERMIDS:
  300. outids = permids;
  301. break;
  302. case DDDMP_VARAUXIDS:
  303. outids = auxids;
  304. break;
  305. case DDDMP_VARNAMES:
  306. outvarnames = varnames;
  307. break;
  308. case DDDMP_VARDEFAULT:
  309. break;
  310. }
  311. break;
  312. case DDDMP_MODE_BINARY:
  313. outids = NULL;
  314. break;
  315. }
  316. /*
  317. * Number dd nodes and count them (numbering is from 1 to nnodes)
  318. */
  319. nnodes = DddmpNumberBddNodes (ddMgr, f, nRoots);
  320. /*
  321. * Start Header
  322. */
  323. #ifdef DDDMP_VERSION
  324. retValue = fprintf (fp, ".ver %s\n", DDDMP_VERSION);
  325. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  326. failure);
  327. #endif
  328. retValue = fprintf (fp, ".mode %c\n", mode);
  329. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  330. failure);
  331. if (mode == DDDMP_MODE_TEXT) {
  332. retValue = fprintf (fp, ".varinfo %d\n", varinfo);
  333. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  334. failure);
  335. }
  336. if (ddname != NULL) {
  337. retValue = fprintf (fp, ".dd %s\n",ddname);
  338. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  339. failure);
  340. }
  341. retValue = fprintf (fp, ".nnodes %d\n", nnodes);
  342. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  343. failure);
  344. retValue = fprintf (fp, ".nvars %d\n", nVars);
  345. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  346. failure);
  347. retValue = fprintf (fp, ".nsuppvars %d\n", var);
  348. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  349. failure);
  350. /*------------ Write the Var Names by scanning the ids array -------------*/
  351. if (varnames != NULL) {
  352. retValue = fprintf (fp, ".suppvarnames");
  353. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  354. failure);
  355. for (i=0; i<nVars; i++) {
  356. if (ids[i] >= 0) {
  357. if (varnames[ids[i]] == NULL) {
  358. (void) fprintf (stderr,
  359. "DdStore Warning: null variable name. DUMMY%d generated\n", i);
  360. fflush (stderr);
  361. varnames[ids[i]] = DDDMP_ALLOC (char, 10);
  362. Dddmp_CheckAndGotoLabel (varnames[ids[i]] == NULL,
  363. "Error allocating memory.", failure);
  364. sprintf (varnames[ids[i]], "DUMMY%d", i);
  365. }
  366. retValue = fprintf (fp, " %s", varnames[ids[i]]);
  367. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  368. failure);
  369. }
  370. }
  371. retValue = fprintf (fp, "\n");
  372. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  373. failure);
  374. }
  375. /*--------- Write the Var SUPPORT Names by scanning the ids array ---------*/
  376. if (varnames != NULL) {
  377. retValue = fprintf (fp, ".orderedvarnames");
  378. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  379. failure);
  380. for (i=0; i<nVars; i++) {
  381. if (varnames[ddMgr->invperm[i]] == NULL) {
  382. (void) fprintf (stderr,
  383. "DdStore Warning: null variable name. DUMMY%d generated\n", i);
  384. fflush (stderr);
  385. varnames[ddMgr->invperm[i]] = DDDMP_ALLOC (char, 10);
  386. Dddmp_CheckAndGotoLabel (varnames[ddMgr->invperm[i]] == NULL,
  387. "Error allocating memory.", failure);
  388. sprintf (varnames[ddMgr->invperm[i]], "DUMMY%d", i);
  389. }
  390. retValue = fprintf (fp, " %s", varnames[ddMgr->invperm[i]]);
  391. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  392. failure);
  393. }
  394. retValue = fprintf (fp, "\n");
  395. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  396. failure);
  397. }
  398. /*------------ Write the var ids by scanning the ids array ---------------*/
  399. retValue = fprintf (fp, ".ids");
  400. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  401. failure);
  402. for (i=0; i<nVars; i++) {
  403. if (ids[i] >= 0) {
  404. retValue = fprintf (fp, " %d", i);
  405. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  406. failure);
  407. }
  408. }
  409. retValue = fprintf (fp, "\n");
  410. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  411. failure);
  412. /*
  413. * Write the var permids by scanning the permids array.
  414. */
  415. retValue = fprintf (fp, ".permids");
  416. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  417. failure);
  418. for (i = 0; i < nVars; i++) {
  419. if (permids[i] >= 0) {
  420. retValue = fprintf (fp, " %d", permids[i]);
  421. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  422. failure);
  423. }
  424. }
  425. retValue = fprintf (fp, "\n");
  426. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  427. failure);
  428. if (auxids != NULL) {
  429. /*
  430. * Write the var auxids by scanning the ids array.
  431. */
  432. retValue = fprintf (fp, ".auxids");
  433. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  434. failure);
  435. for (i = 0; i < nVars; i++) {
  436. if (ids[i] >= 0) {
  437. retValue = fprintf (fp, " %d", auxids[i]);
  438. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  439. failure);
  440. }
  441. }
  442. retValue = fprintf (fp, "\n");
  443. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  444. failure);
  445. }
  446. /*
  447. * Write the roots info.
  448. */
  449. retValue = fprintf (fp, ".nroots %d\n", nRoots);
  450. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  451. failure);
  452. if (rootnames != NULL) {
  453. /*
  454. * Write the root names.
  455. */
  456. retValue = fprintf (fp, ".rootnames");
  457. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  458. failure);
  459. for (i = 0; i < nRoots; i++) {
  460. if (rootnames[i] == NULL) {
  461. (void) fprintf (stderr,
  462. "DdStore Warning: null variable name. ROOT%d generated\n",i);
  463. fflush (stderr);
  464. rootnames[i] = DDDMP_ALLOC(char,10);
  465. Dddmp_CheckAndGotoLabel (rootnames[i]==NULL,
  466. "Error writing to file.", failure);
  467. sprintf(rootnames[ids[i]], "ROOT%d",i);
  468. }
  469. retValue = fprintf (fp, " %s", rootnames[i]);
  470. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  471. failure);
  472. }
  473. retValue = fprintf (fp, "\n");
  474. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  475. failure);
  476. }
  477. retValue = fprintf (fp, ".rootids");
  478. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  479. failure);
  480. /*
  481. * Write BDD indexes of function roots.
  482. * Use negative integers for complemented edges.
  483. */
  484. for (i = 0; i < nRoots; i++) {
  485. if (f[i] == NULL) {
  486. (void) fprintf (stderr, "DdStore Warning: %d-th root is NULL\n",i);
  487. fflush (stderr);
  488. retValue = fprintf (fp, " 0");
  489. }
  490. if (Cudd_IsComplement(f[i])) {
  491. retValue = fprintf (fp, " -%d",
  492. DddmpReadNodeIndexBdd (Cudd_Regular (f[i])));
  493. } else {
  494. retValue = fprintf (fp, " %d",
  495. DddmpReadNodeIndexBdd (Cudd_Regular (f[i])));
  496. }
  497. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  498. failure);
  499. }
  500. retValue = fprintf (fp, "\n");
  501. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  502. failure);
  503. retValue = fprintf (fp, ".nodes\n");
  504. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  505. failure);
  506. /*
  507. * END HEADER
  508. */
  509. /*
  510. * Call the function that really gets the job done.
  511. */
  512. for (i = 0; i < nRoots; i++) {
  513. if (f[i] != NULL) {
  514. retValue = NodeStoreRecurBdd (ddMgr, Cudd_Regular(f[i]),
  515. mode, supportids, outvarnames, outids, fp);
  516. Dddmp_CheckAndGotoLabel (retValue==DDDMP_FAILURE,
  517. "Error writing to file.", failure);
  518. }
  519. }
  520. /*
  521. * Write trailer and return.
  522. */
  523. retValue = fprintf (fp, ".end\n");
  524. Dddmp_CheckAndGotoLabel (retValue==EOF, "Error writing to file.",
  525. failure);
  526. if (fileToClose) {
  527. fclose (fp);
  528. }
  529. DddmpUnnumberBddNodes (ddMgr, f, nRoots);
  530. DDDMP_FREE (ids);
  531. DDDMP_FREE (permids);
  532. DDDMP_FREE (invpermids);
  533. DDDMP_FREE (supportids);
  534. return (DDDMP_SUCCESS);
  535. failure:
  536. if (ids != NULL) {
  537. DDDMP_FREE (ids);
  538. }
  539. if (permids != NULL) {
  540. DDDMP_FREE (permids);
  541. }
  542. if (invpermids != NULL) {
  543. DDDMP_FREE (invpermids);
  544. }
  545. if (supportids != NULL) {
  546. DDDMP_FREE (supportids);
  547. }
  548. if (support != NULL) {
  549. Cudd_RecursiveDeref (ddMgr, support);
  550. }
  551. return (DDDMP_FAILURE);
  552. }
  553. /*---------------------------------------------------------------------------*/
  554. /* Definition of static functions */
  555. /*---------------------------------------------------------------------------*/
  556. /**Function********************************************************************
  557. Synopsis [Performs the recursive step of Dddmp_bddStore.]
  558. Description [Stores a node to file in either test or binary mode.<l>
  559. In text mode a node is represented (on a text line basis) as
  560. <UL>
  561. <LI> node-index \[var-extrainfo\] var-index Then-index Else-index
  562. </UL>
  563. where all indexes are integer numbers and var-extrainfo
  564. (optional redundant field) is either an integer or a string
  565. (variable name). Node-index is redundant (due to the node
  566. ordering) but we keep it for readability.<p>
  567. In binary mode nodes are represented as a sequence of bytes,
  568. representing var-index, Then-index, and Else-index in an
  569. optimized way. Only the first byte (code) is mandatory.
  570. Integer indexes are represented in absolute or relative mode,
  571. where relative means offset wrt. a Then/Else node info.
  572. Suppose Var(NodeId), Then(NodeId) and Else(NodeId) represent
  573. infos about a given node.<p>
  574. The generic "NodeId" node is stored as
  575. <UL>
  576. <LI> code-byte
  577. <LI> \[var-info\]
  578. <LI> \[Then-info\]
  579. <LI> \[Else-info\]
  580. </UL>
  581. where code-byte contains bit fields
  582. <UL>
  583. <LI>Unused : 1 bit
  584. <LI>Variable: 2 bits, one of the following codes
  585. <UL>
  586. <LI>DDDMP_ABSOLUTE_ID var-info = Var(NodeId) follows
  587. <LI>DDDMP_RELATIVE_ID Var(NodeId) is represented in relative form as
  588. var-info = Min(Var(Then(NodeId)),Var(Else(NodeId))) -Var(NodeId)
  589. <LI>DDDMP_RELATIVE_1 No var-info follows, because
  590. Var(NodeId) = Min(Var(Then(NodeId)),Var(Else(NodeId)))-1
  591. <LI>DDDMP_TERMINAL Node is a terminal, no var info required
  592. </UL>
  593. <LI>T : 2 bits, with codes similar to V
  594. <UL>
  595. <LI>DDDMP_ABSOLUTE_ID Then-info = Then(NodeId) follows
  596. <LI>DDDMP_RELATIVE_ID Then(NodeId) is represented in relative form as
  597. Then-info = Nodeid-Then(NodeId)
  598. <LI>DDDMP_RELATIVE_1 No info on Then(NodeId) follows, because
  599. Then(NodeId) = NodeId-1
  600. <LI>DDDMP_TERMINAL Then Node is a terminal, no info required (for BDDs)
  601. </UL>
  602. <LI>Ecompl : 1 bit, if 1 means complemented edge
  603. <LI>E : 2 bits, with codes and meanings as for the Then edge
  604. </UL>
  605. var-info, Then-info, Else-info (if required) are represented as unsigned
  606. integer values on a sufficient set of bytes (MSByte first).
  607. ]
  608. SideEffects [None]
  609. SeeAlso []
  610. ******************************************************************************/
  611. static int
  612. NodeStoreRecurBdd (
  613. DdManager *ddMgr /* IN: DD Manager */,
  614. DdNode *f /* IN: DD node to be stored */,
  615. int mode /* IN: store mode */,
  616. int *supportids /* IN: internal ids for variables */,
  617. char **varnames /* IN: names of variables: to be stored with nodes */,
  618. int *outids /* IN: output ids for variables */,
  619. FILE *fp /* IN: store file */
  620. )
  621. {
  622. DdNode *T = NULL;
  623. DdNode *E = NULL;
  624. int idf = (-1);
  625. int idT = (-1);
  626. int idE = (-1);
  627. int vf = (-1);
  628. int vT = (-1);
  629. int vE = (-1);
  630. int retValue;
  631. int nVars;
  632. nVars = ddMgr->size;
  633. T = E = NULL;
  634. idf = idT = idE = (-1);
  635. #ifdef DDDMP_DEBUG
  636. assert(!Cudd_IsComplement(f));
  637. assert(f!=NULL);
  638. assert(supportids!=NULL);
  639. #endif
  640. /* If already visited, nothing to do. */
  641. if (DddmpVisitedBdd (f)) {
  642. return (DDDMP_SUCCESS);
  643. }
  644. /* Mark node as visited. */
  645. DddmpSetVisitedBdd (f);
  646. if (Cudd_IsConstant(f)) {
  647. /* Check for special case: don't recur */
  648. idf = DddmpReadNodeIndexBdd (f);
  649. } else {
  650. #ifdef DDDMP_DEBUG
  651. /* BDDs! Only one constant supported */
  652. assert (!cuddIsConstant(f));
  653. #endif
  654. /*
  655. * Recursive call for Then edge
  656. */
  657. T = cuddT(f);
  658. #ifdef DDDMP_DEBUG
  659. /* ROBDDs! No complemented Then edge */
  660. assert (!Cudd_IsComplement(T));
  661. #endif
  662. /* recur */
  663. retValue = NodeStoreRecurBdd (ddMgr, T, mode, supportids, varnames, outids,
  664. fp);
  665. if (retValue != DDDMP_SUCCESS) {
  666. return (retValue);
  667. }
  668. /*
  669. * Recursive call for Else edge
  670. */
  671. E = Cudd_Regular (cuddE (f));
  672. retValue = NodeStoreRecurBdd (ddMgr, E, mode, supportids, varnames, outids,
  673. fp);
  674. if (retValue != DDDMP_SUCCESS) {
  675. return (retValue);
  676. }
  677. /*
  678. * Obtain nodeids and variable ids of f, T, E
  679. */
  680. idf = DddmpReadNodeIndexBdd (f);
  681. vf = f->index;
  682. idT = DddmpReadNodeIndexBdd (T);
  683. if (Cudd_IsConstant(T)) {
  684. vT = nVars;
  685. } else {
  686. vT = T->index;
  687. }
  688. idE = DddmpReadNodeIndexBdd (E);
  689. if (Cudd_IsConstant(E)) {
  690. vE = nVars;
  691. } else {
  692. vE = E->index;
  693. }
  694. }
  695. switch (mode) {
  696. case DDDMP_MODE_TEXT:
  697. retValue = NodeTextStoreBdd (ddMgr, f, mode, supportids, varnames,
  698. outids, fp, idf, vf, idT, idE);
  699. break;
  700. case DDDMP_MODE_BINARY:
  701. retValue = NodeBinaryStoreBdd (ddMgr, f, mode, supportids, varnames,
  702. outids, fp, idf, vf, idT, idE, vT, vE, T, E);
  703. break;
  704. default:
  705. return (DDDMP_FAILURE);
  706. }
  707. return (retValue);
  708. }
  709. /**Function********************************************************************
  710. Synopsis [Store One Single Node in Text Format.]
  711. Description [Store 1 0 0 for the terminal node.
  712. Store id, left child pointer, right pointer for all the other nodes.
  713. ]
  714. SideEffects [None]
  715. SeeAlso [NodeBinaryStoreBdd]
  716. ******************************************************************************/
  717. static int
  718. NodeTextStoreBdd (
  719. DdManager *ddMgr /* IN: DD Manager */,
  720. DdNode *f /* IN: DD node to be stored */,
  721. int mode /* IN: store mode */,
  722. int *supportids /* IN: internal ids for variables */,
  723. char **varnames /* IN: names of variables: to be stored with nodes */,
  724. int *outids /* IN: output ids for variables */,
  725. FILE *fp /* IN: Store file */,
  726. int idf /* IN: index of the current node */,
  727. int vf /* IN: variable of the current node */,
  728. int idT /* IN: index of the Then node */,
  729. int idE /* IN: index of the Else node */
  730. )
  731. {
  732. int retValue = EOF;
  733. (void) mode; /* avoid warning */
  734. /*
  735. * Check for Constant
  736. */
  737. if (Cudd_IsConstant(f)) {
  738. if (f == Cudd_ReadOne(ddMgr)) {
  739. if ((varnames != NULL) || (outids != NULL)) {
  740. retValue = fprintf (fp, "%d T 1 0 0\n", idf);
  741. } else {
  742. retValue = fprintf (fp, "%d 1 0 0\n", idf);
  743. }
  744. if (retValue == EOF) {
  745. return (DDDMP_FAILURE);
  746. } else {
  747. return (DDDMP_SUCCESS);
  748. }
  749. }
  750. if (f == Cudd_ReadZero(ddMgr)) {
  751. if ((varnames != NULL) || (outids != NULL)) {
  752. retValue = fprintf (fp, "%d T 0 0 0\n", idf);
  753. } else {
  754. retValue = fprintf (fp, "%d 0 0 0\n", idf);
  755. }
  756. if (retValue == EOF) {
  757. return (DDDMP_FAILURE);
  758. } else {
  759. return (DDDMP_SUCCESS);
  760. }
  761. }
  762. /*
  763. * A constant node different from 1: an ADD constant
  764. */
  765. Dddmp_CheckAndReturn (((varnames!=NULL)||(outids!=NULL)),
  766. "Error writing to file: ADD Type.");
  767. if (retValue == EOF) {
  768. return (DDDMP_FAILURE);
  769. } else {
  770. return (DDDMP_SUCCESS);
  771. }
  772. }
  773. /*
  774. * ... Not A Constant
  775. */
  776. if (Cudd_IsComplement (cuddE(f))) {
  777. idE = -idE;
  778. }
  779. if (varnames != NULL) {
  780. retValue = fprintf (fp, "%d %s %d %d %d\n",
  781. idf, varnames[vf], supportids[vf], idT, idE);
  782. if (retValue == EOF) {
  783. return (DDDMP_FAILURE);
  784. } else {
  785. return (DDDMP_SUCCESS);
  786. }
  787. }
  788. if (outids != NULL) {
  789. retValue = fprintf (fp, "%d %d %d %d %d\n",
  790. idf, outids[vf], supportids[vf], idT, idE);
  791. if (retValue == EOF) {
  792. return (DDDMP_FAILURE);
  793. } else {
  794. return (DDDMP_SUCCESS);
  795. }
  796. }
  797. retValue = fprintf (fp, "%d %d %d %d\n",
  798. idf, supportids[vf], idT, idE);
  799. if (retValue == EOF) {
  800. return (DDDMP_FAILURE);
  801. } else {
  802. return (DDDMP_SUCCESS);
  803. }
  804. }
  805. /**Function********************************************************************
  806. Synopsis [Store One Single Node in Binary Format.]
  807. Description [Store 1 0 0 for the terminal node.
  808. Store id, left child pointer, right pointer for all the other nodes.
  809. Store every information as coded binary values.]
  810. SideEffects [None]
  811. SeeAlso [NodeTextStoreBdd]
  812. ******************************************************************************/
  813. static int
  814. NodeBinaryStoreBdd (
  815. DdManager *ddMgr /* IN: DD Manager */,
  816. DdNode *f /* IN: DD node to be stored */,
  817. int mode /* IN: store mode */,
  818. int *supportids /* IN: internal ids for variables */,
  819. char **varnames /* IN: names of variables: to be stored with nodes */,
  820. int *outids /* IN: output ids for variables */,
  821. FILE *fp /* IN: store file */,
  822. int idf /* IN: index of the node */,
  823. int vf /* IN: variable of the node */,
  824. int idT /* IN: index of the Then node */,
  825. int idE /* IN: index of the Else node */,
  826. int vT /* IN: variable of the Then node */,
  827. int vE /* IN: variable of the Else node */,
  828. DdNode *T /* IN: Then node */,
  829. DdNode *E /* IN: Else node */
  830. )
  831. {
  832. int retValue, diff, var;
  833. struct binary_dd_code code;
  834. (void) mode; /* avoid warning */
  835. (void) varnames; /* avoid warning */
  836. (void) outids; /* avoid warning */
  837. /*
  838. * Check for Constant
  839. */
  840. /* only integer ids used, varnames ignored */
  841. /* Terminal one is coded as DDDMP_TERMINAL, all other fields are 0 */
  842. if (Cudd_IsConstant(f)) {
  843. code.Unused = 0;
  844. code.V = DDDMP_TERMINAL;
  845. code.T = 0;
  846. code.E = 0;
  847. code.Ecompl = 0;
  848. retValue = DddmpWriteCode (fp,code);
  849. if (retValue == EOF) {
  850. return (DDDMP_FAILURE);
  851. } else {
  852. return (DDDMP_SUCCESS);
  853. }
  854. }
  855. /*
  856. * Non terminal: output variable id
  857. */
  858. var = supportids[vf];
  859. diff = (supportids[vT]<supportids[vE]) ?
  860. (supportids[vT]-var) : (supportids[vE]-var);
  861. code.V = DDDMP_ABSOLUTE_ID;
  862. if (diff <= var) {
  863. if (diff == 1) {
  864. code.V = DDDMP_RELATIVE_1;
  865. } else {
  866. code.V = DDDMP_RELATIVE_ID;
  867. var = diff;
  868. }
  869. }
  870. if (T == DD_ONE(ddMgr)) {
  871. code.T = DDDMP_TERMINAL;
  872. } else {
  873. /* compute displacement */
  874. diff = idf - idT;
  875. code.T = DDDMP_ABSOLUTE_ID;
  876. if (diff <= idT) {
  877. if (diff == 1) {
  878. code.T = DDDMP_RELATIVE_1;
  879. } else {
  880. code.T = DDDMP_RELATIVE_ID;
  881. idT = diff;
  882. }
  883. }
  884. }
  885. if (E == DD_ONE(ddMgr)) {
  886. code.E = DDDMP_TERMINAL;
  887. } else {
  888. /* compute displacement */
  889. diff = idf - idE;
  890. code.E = DDDMP_ABSOLUTE_ID;
  891. if (diff <= idE) {
  892. if (diff == 1) {
  893. code.E = DDDMP_RELATIVE_1;
  894. } else {
  895. code.E = DDDMP_RELATIVE_ID;
  896. idE = diff;
  897. }
  898. }
  899. }
  900. if (Cudd_IsComplement(cuddE(f))) {
  901. code.Ecompl = 1;
  902. } else {
  903. code.Ecompl = 0;
  904. }
  905. retValue = DddmpWriteCode (fp,code);
  906. if (retValue == EOF) {
  907. return (DDDMP_FAILURE);
  908. }
  909. if ((code.V == DDDMP_ABSOLUTE_ID) || (code.V == DDDMP_RELATIVE_ID)) {
  910. retValue = DddmpWriteInt (fp, var);
  911. if (retValue == EOF) {
  912. return (DDDMP_FAILURE);
  913. }
  914. }
  915. if ((code.T == DDDMP_ABSOLUTE_ID) || (code.T == DDDMP_RELATIVE_ID)) {
  916. retValue = DddmpWriteInt(fp,idT);
  917. if (retValue == EOF) {
  918. return (DDDMP_FAILURE);
  919. }
  920. }
  921. if ((code.E == DDDMP_ABSOLUTE_ID) || (code.E == DDDMP_RELATIVE_ID)) {
  922. retValue = DddmpWriteInt(fp,idE);
  923. if (retValue == EOF) {
  924. return (DDDMP_FAILURE);
  925. }
  926. }
  927. return (retValue);
  928. }