cuddExport.c 46.9 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9
/**CFile***********************************************************************

  FileName    [cuddExport.c]

  PackageName [cudd]

  Synopsis    [Export functions.]

  Description [External procedures included in this module:
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
                <ul>
                <li> Cudd_DumpBlif()
                <li> Cudd_DumpBlifBody()
                <li> Cudd_DumpDot()
                <li> Cudd_DumpDaVinci()
                <li> Cudd_DumpDDcal()
                <li> Cudd_DumpFactoredForm()
                </ul>
        Internal procedures included in this module:
                <ul>
                </ul>
        Static procedures included in this module:
                <ul>
                <li> ddDoDumpBlif()
                <li> ddDoDumpDaVinci()
                <li> ddDoDumpDDcal()
                <li> ddDoDumpFactoredForm()
                </ul>]
Alan Mishchenko committed
28 29 30

  Author      [Fabio Somenzi]

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  Copyright   [Copyright (c) 1995-2004, Regents of the University of Colorado

  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

  Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

  Neither the name of the University of Colorado nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.]
Alan Mishchenko committed
62 63 64

******************************************************************************/

65
#include "misc/util/util_hack.h"
Alan Mishchenko committed
66 67
#include "cuddInt.h"

68 69 70
ABC_NAMESPACE_IMPL_START


71

Alan Mishchenko committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
/* Stucture declarations                                                     */
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
/* Type declarations                                                         */
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
/* Variable declarations                                                     */
/*---------------------------------------------------------------------------*/

#ifndef lint
89
static char rcsid[] DD_UNUSED = "$Id: cuddExport.c,v 1.22 2009/03/08 02:49:02 fabio Exp $";
Alan Mishchenko committed
90 91 92 93 94 95 96 97 98 99 100 101
#endif

/*---------------------------------------------------------------------------*/
/* Macro declarations                                                        */
/*---------------------------------------------------------------------------*/

/**AutomaticStart*************************************************************/

/*---------------------------------------------------------------------------*/
/* Static function prototypes                                                */
/*---------------------------------------------------------------------------*/

102 103 104 105
static int ddDoDumpBlif (DdManager *dd, DdNode *f, FILE *fp, st_table *visited, char **names, int mv);
static int ddDoDumpDaVinci (DdManager *dd, DdNode *f, FILE *fp, st_table *visited, char **names, ptruint mask);
static int ddDoDumpDDcal (DdManager *dd, DdNode *f, FILE *fp, st_table *visited, char **names, ptruint mask);
static int ddDoDumpFactoredForm (DdManager *dd, DdNode *f, FILE *fp, char **names);
Alan Mishchenko committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

/**AutomaticEnd***************************************************************/


/*---------------------------------------------------------------------------*/
/* Definition of exported functions                                          */
/*---------------------------------------------------------------------------*/


/**Function********************************************************************

  Synopsis    [Writes a blif file representing the argument BDDs.]

  Description [Writes a blif file representing the argument BDDs as a
  network of multiplexers. One multiplexer is written for each BDD
  node. It returns 1 in case of success; 0 otherwise (e.g.,
  out-of-memory, file system full, or an ADD with constants different
  from 0 and 1).  Cudd_DumpBlif does not close the file: This is the
  caller responsibility. Cudd_DumpBlif uses a minimal unique subset of
  the hexadecimal address of a node as name for it.  If the argument
  inames is non-null, it is assumed to hold the pointers to the names
  of the inputs. Similarly for onames.]

  SideEffects [None]

  SeeAlso     [Cudd_DumpBlifBody Cudd_DumpDot Cudd_PrintDebug Cudd_DumpDDcal
  Cudd_DumpDaVinci Cudd_DumpFactoredForm]

******************************************************************************/
int
Cudd_DumpBlif(
  DdManager * dd /* manager */,
  int  n /* number of output nodes to be dumped */,
  DdNode ** f /* array of output nodes to be dumped */,
  char ** inames /* array of input names (or NULL) */,
  char ** onames /* array of output names (or NULL) */,
  char * mname /* model name (or NULL) */,
143 144
  FILE * fp /* pointer to the dump file */,
  int mv /* 0: blif, 1: blif-MV */)
Alan Mishchenko committed
145
{
146 147 148 149 150 151
    DdNode      *support = NULL;
    DdNode      *scan;
    int         *sorted = NULL;
    int         nvars = dd->size;
    int         retval;
    int         i;
Alan Mishchenko committed
152 153

    /* Build a bit array with the support of f. */
Alan Mishchenko committed
154
    sorted = ABC_ALLOC(int,nvars);
Alan Mishchenko committed
155
    if (sorted == NULL) {
156 157
        dd->errorCode = CUDD_MEMORY_OUT;
        goto failure;
Alan Mishchenko committed
158 159 160 161 162 163 164 165 166
    }
    for (i = 0; i < nvars; i++) sorted[i] = 0;

    /* Take the union of the supports of each output function. */
    support = Cudd_VectorSupport(dd,f,n);
    if (support == NULL) goto failure;
    cuddRef(support);
    scan = support;
    while (!cuddIsConstant(scan)) {
167 168
        sorted[scan->index] = 1;
        scan = cuddT(scan);
Alan Mishchenko committed
169 170
    }
    Cudd_RecursiveDeref(dd,support);
171
    support = NULL; /* so that we do not try to free it in case of failure */
Alan Mishchenko committed
172 173 174

    /* Write the header (.model .inputs .outputs). */
    if (mname == NULL) {
175
        retval = fprintf(fp,".model DD\n.inputs");
Alan Mishchenko committed
176
    } else {
177 178 179 180 181
        retval = fprintf(fp,".model %s\n.inputs",mname);
    }
    if (retval == EOF) {
        ABC_FREE(sorted);
        return(0);
Alan Mishchenko committed
182 183 184 185 186
    }

    /* Write the input list by scanning the support array. */
    for (i = 0; i < nvars; i++) {
        if (sorted[i]) {
187 188 189 190 191
            if (inames == NULL) {
                retval = fprintf(fp," %d", i);
            } else {
                retval = fprintf(fp," %s", inames[i]);
            }
Alan Mishchenko committed
192 193 194
            if (retval == EOF) goto failure;
        }
    }
Alan Mishchenko committed
195
    ABC_FREE(sorted);
Alan Mishchenko committed
196 197 198 199 200 201
    sorted = NULL;

    /* Write the .output line. */
    retval = fprintf(fp,"\n.outputs");
    if (retval == EOF) goto failure;
    for (i = 0; i < n; i++) {
202 203 204 205 206 207
        if (onames == NULL) {
            retval = fprintf(fp," f%d", i);
        } else {
            retval = fprintf(fp," %s", onames[i]);
        }
        if (retval == EOF) goto failure;
Alan Mishchenko committed
208 209 210 211
    }
    retval = fprintf(fp,"\n");
    if (retval == EOF) goto failure;

212
    retval = Cudd_DumpBlifBody(dd, n, f, inames, onames, fp, mv);
Alan Mishchenko committed
213 214 215 216 217 218 219 220 221
    if (retval == 0) goto failure;

    /* Write trailer and return. */
    retval = fprintf(fp,".end\n");
    if (retval == EOF) goto failure;

    return(1);

failure:
Alan Mishchenko committed
222
    if (sorted != NULL) ABC_FREE(sorted);
Alan Mishchenko committed
223 224 225 226 227 228 229 230 231 232 233
    if (support != NULL) Cudd_RecursiveDeref(dd,support);
    return(0);

} /* end of Cudd_DumpBlif */


/**Function********************************************************************

  Synopsis    [Writes a blif body representing the argument BDDs.]

  Description [Writes a blif body representing the argument BDDs as a
234 235 236
  network of multiplexers.  No header (.model, .inputs, and .outputs) and
  footer (.end) are produced by this function.  One multiplexer is written
  for each BDD node. It returns 1 in case of success; 0 otherwise (e.g.,
Alan Mishchenko committed
237
  out-of-memory, file system full, or an ADD with constants different
238 239
  from 0 and 1).  Cudd_DumpBlifBody does not close the file: This is the
  caller responsibility. Cudd_DumpBlifBody uses a minimal unique subset of
Alan Mishchenko committed
240 241 242 243 244 245 246
  the hexadecimal address of a node as name for it.  If the argument
  inames is non-null, it is assumed to hold the pointers to the names
  of the inputs. Similarly for onames. This function prints out only
  .names part.]

  SideEffects [None]

247
  SeeAlso     [Cudd_DumpBlif Cudd_DumpDot Cudd_PrintDebug Cudd_DumpDDcal
Alan Mishchenko committed
248 249 250 251 252 253 254 255 256 257
  Cudd_DumpDaVinci Cudd_DumpFactoredForm]

******************************************************************************/
int
Cudd_DumpBlifBody(
  DdManager * dd /* manager */,
  int  n /* number of output nodes to be dumped */,
  DdNode ** f /* array of output nodes to be dumped */,
  char ** inames /* array of input names (or NULL) */,
  char ** onames /* array of output names (or NULL) */,
258 259
  FILE * fp /* pointer to the dump file */,
  int mv /* 0: blif, 1: blif-MV */)
Alan Mishchenko committed
260 261
{
    st_table    *visited = NULL;
262 263
    int         retval;
    int         i;
Alan Mishchenko committed
264 265 266 267 268 269 270

    /* Initialize symbol table for visited nodes. */
    visited = st_init_table(st_ptrcmp, st_ptrhash);
    if (visited == NULL) goto failure;

    /* Call the function that really gets the job done. */
    for (i = 0; i < n; i++) {
271 272
        retval = ddDoDumpBlif(dd,Cudd_Regular(f[i]),fp,visited,inames,mv);
        if (retval == 0) goto failure;
Alan Mishchenko committed
273 274 275 276 277 278 279
    }

    /* To account for the possible complement on the root,
    ** we put either a buffer or an inverter at the output of
    ** the multiplexer representing the top node.
    */
    for (i = 0; i < n; i++) {
280 281
        if (onames == NULL) {
            retval = fprintf(fp,
Alan Mishchenko committed
282
#if SIZEOF_VOID_P == 8
283
                ".names %lx f%d\n", (ptruint) f[i] / (ptruint) sizeof(DdNode), i);
Alan Mishchenko committed
284
#else
285
                ".names %x f%d\n", (ptruint) f[i] / (ptruint) sizeof(DdNode), i);
Alan Mishchenko committed
286
#endif
287 288
        } else {
            retval = fprintf(fp,
Alan Mishchenko committed
289
#if SIZEOF_VOID_P == 8
290
                ".names %lx %s\n", (ptruint) f[i] / (ptruint) sizeof(DdNode), onames[i]);
Alan Mishchenko committed
291
#else
292
                ".names %x %s\n", (ptruint) f[i] / (ptruint) sizeof(DdNode), onames[i]);
Alan Mishchenko committed
293
#endif
294 295 296 297 298 299 300 301
        }
        if (retval == EOF) goto failure;
        if (Cudd_IsComplement(f[i])) {
            retval = fprintf(fp,"%s0 1\n", mv ? ".def 0\n" : "");
        } else {
            retval = fprintf(fp,"%s1 1\n", mv ? ".def 0\n" : "");
        }
        if (retval == EOF) goto failure;
Alan Mishchenko committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
    }

    st_free_table(visited);
    return(1);

failure:
    if (visited != NULL) st_free_table(visited);
    return(0);

} /* end of Cudd_DumpBlifBody */


/**Function********************************************************************

  Synopsis    [Writes a dot file representing the argument DDs.]

  Description [Writes a file representing the argument DDs in a format
  suitable for the graph drawing program dot.
  It returns 1 in case of success; 0 otherwise (e.g., out-of-memory,
  file system full).
  Cudd_DumpDot does not close the file: This is the caller
  responsibility. Cudd_DumpDot uses a minimal unique subset of the
  hexadecimal address of a node as name for it.
  If the argument inames is non-null, it is assumed to hold the pointers
  to the names of the inputs. Similarly for onames.
  Cudd_DumpDot uses the following convention to draw arcs:
    <ul>
    <li> solid line: THEN arcs;
    <li> dotted line: complement arcs;
    <li> dashed line: regular ELSE arcs.
    </ul>
  The dot options are chosen so that the drawing fits on a letter-size
  sheet.
  ]

  SideEffects [None]

  SeeAlso     [Cudd_DumpBlif Cudd_PrintDebug Cudd_DumpDDcal
  Cudd_DumpDaVinci Cudd_DumpFactoredForm]

******************************************************************************/
int
Cudd_DumpDot(
  DdManager * dd /* manager */,
  int  n /* number of output nodes to be dumped */,
  DdNode ** f /* array of output nodes to be dumped */,
  char ** inames /* array of input names (or NULL) */,
  char ** onames /* array of output names (or NULL) */,
  FILE * fp /* pointer to the dump file */)
{
352 353 354 355
    DdNode      *support = NULL;
    DdNode      *scan;
    int         *sorted = NULL;
    int         nvars = dd->size;
Alan Mishchenko committed
356 357
    st_table    *visited = NULL;
    st_generator *gen = NULL;
358 359 360 361 362
    int         retval;
    int         i, j;
    int         slots;
    DdNodePtr   *nodelist;
    long        refAddr, diff, mask;
Alan Mishchenko committed
363 364

    /* Build a bit array with the support of f. */
Alan Mishchenko committed
365
    sorted = ABC_ALLOC(int,nvars);
Alan Mishchenko committed
366
    if (sorted == NULL) {
367 368
        dd->errorCode = CUDD_MEMORY_OUT;
        goto failure;
Alan Mishchenko committed
369 370 371 372 373 374 375 376 377
    }
    for (i = 0; i < nvars; i++) sorted[i] = 0;

    /* Take the union of the supports of each output function. */
    support = Cudd_VectorSupport(dd,f,n);
    if (support == NULL) goto failure;
    cuddRef(support);
    scan = support;
    while (!cuddIsConstant(scan)) {
378 379
        sorted[scan->index] = 1;
        scan = cuddT(scan);
Alan Mishchenko committed
380 381
    }
    Cudd_RecursiveDeref(dd,support);
382
    support = NULL; /* so that we do not try to free it in case of failure */
Alan Mishchenko committed
383 384 385 386 387 388 389

    /* Initialize symbol table for visited nodes. */
    visited = st_init_table(st_ptrcmp, st_ptrhash);
    if (visited == NULL) goto failure;

    /* Collect all the nodes of this DD in the symbol table. */
    for (i = 0; i < n; i++) {
390 391
        retval = cuddCollectNodes(Cudd_Regular(f[i]),visited);
        if (retval == 0) goto failure;
Alan Mishchenko committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    }

    /* Find how many most significant hex digits are identical
    ** in the addresses of all the nodes. Build a mask based
    ** on this knowledge, so that digits that carry no information
    ** will not be printed. This is done in two steps.
    **  1. We scan the symbol table to find the bits that differ
    **     in at least 2 addresses.
    **  2. We choose one of the possible masks. There are 8 possible
    **     masks for 32-bit integer, and 16 possible masks for 64-bit
    **     integers.
    */

    /* Find the bits that are different. */
    refAddr = (long) Cudd_Regular(f[0]);
    diff = 0;
    gen = st_init_gen(visited);
    if (gen == NULL) goto failure;
410 411
    while (st_gen(gen, (const char **)&scan, NULL)) {
        diff |= refAddr ^ (long) scan;
Alan Mishchenko committed
412 413 414 415 416
    }
    st_free_gen(gen); gen = NULL;

    /* Choose the mask. */
    for (i = 0; (unsigned) i < 8 * sizeof(long); i += 4) {
417 418
        mask = (1 << i) - 1;
        if (diff <= mask) break;
Alan Mishchenko committed
419 420 421 422 423 424
    }

    /* Write the header and the global attributes. */
    retval = fprintf(fp,"digraph \"DD\" {\n");
    if (retval == EOF) return(0);
    retval = fprintf(fp,
425
        "size = \"7.5,10\"\ncenter = true;\nedge [dir = none];\n");
Alan Mishchenko committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439
    if (retval == EOF) return(0);

    /* Write the input name subgraph by scanning the support array. */
    retval = fprintf(fp,"{ node [shape = plaintext];\n");
    if (retval == EOF) goto failure;
    retval = fprintf(fp,"  edge [style = invis];\n");
    if (retval == EOF) goto failure;
    /* We use a name ("CONST NODES") with an embedded blank, because
    ** it is unlikely to appear as an input name.
    */
    retval = fprintf(fp,"  \"CONST NODES\" [style = invis];\n");
    if (retval == EOF) goto failure;
    for (i = 0; i < nvars; i++) {
        if (sorted[dd->invperm[i]]) {
440 441 442 443 444
            if (inames == NULL || inames[dd->invperm[i]] == NULL) {
                retval = fprintf(fp,"\" %d \" -> ", dd->invperm[i]);
            } else {
                retval = fprintf(fp,"\" %s \" -> ", inames[dd->invperm[i]]);
            }
Alan Mishchenko committed
445 446 447 448 449 450 451 452 453 454
            if (retval == EOF) goto failure;
        }
    }
    retval = fprintf(fp,"\"CONST NODES\"; \n}\n");
    if (retval == EOF) goto failure;

    /* Write the output node subgraph. */
    retval = fprintf(fp,"{ rank = same; node [shape = box]; edge [style = invis];\n");
    if (retval == EOF) goto failure;
    for (i = 0; i < n; i++) {
455 456 457 458 459 460 461 462 463 464 465 466
        if (onames == NULL) {
            retval = fprintf(fp,"\"F%d\"", i);
        } else {
            retval = fprintf(fp,"\"  %s  \"", onames[i]);
        }
        if (retval == EOF) goto failure;
        if (i == n - 1) {
            retval = fprintf(fp,"; }\n");
        } else {
            retval = fprintf(fp," -> ");
        }
        if (retval == EOF) goto failure;
Alan Mishchenko committed
467 468 469 470 471
    }

    /* Write rank info: All nodes with the same index have the same rank. */
    for (i = 0; i < nvars; i++) {
        if (sorted[dd->invperm[i]]) {
472
            retval = fprintf(fp,"{ rank = same; ");
Alan Mishchenko committed
473
            if (retval == EOF) goto failure;
474 475 476 477 478
            if (inames == NULL || inames[dd->invperm[i]] == NULL) {
                retval = fprintf(fp,"\" %d \";\n", dd->invperm[i]);
            } else {
                retval = fprintf(fp,"\" %s \";\n", inames[dd->invperm[i]]);
            }
Alan Mishchenko committed
479
            if (retval == EOF) goto failure;
480 481 482 483 484 485
            nodelist = dd->subtables[i].nodelist;
            slots = dd->subtables[i].slots;
            for (j = 0; j < slots; j++) {
                scan = nodelist[j];
                while (scan != NULL) {
                    if (st_is_member(visited,(char *) scan)) {
486
                        retval = fprintf(fp,"\"%lx\";\n", ((mask & (ptrint) scan) / sizeof(DdNode)));
487 488 489 490
                        if (retval == EOF) goto failure;
                    }
                    scan = scan->next;
                }
Alan Mishchenko committed
491
            }
492 493
            retval = fprintf(fp,"}\n");
            if (retval == EOF) goto failure;
Alan Mishchenko committed
494 495 496 497 498
        }
    }

    /* All constants have the same rank. */
    retval = fprintf(fp,
499
        "{ rank = same; \"CONST NODES\";\n{ node [shape = box]; ");
Alan Mishchenko committed
500 501 502 503
    if (retval == EOF) goto failure;
    nodelist = dd->constants.nodelist;
    slots = dd->constants.slots;
    for (j = 0; j < slots; j++) {
504 505 506
        scan = nodelist[j];
        while (scan != NULL) {
            if (st_is_member(visited,(char *) scan)) {
507
                retval = fprintf(fp,"\"%lx\";\n", ((mask & (ptrint) scan) / sizeof(DdNode)));
508 509 510
                if (retval == EOF) goto failure;
            }
            scan = scan->next;
Alan Mishchenko committed
511 512 513 514 515 516 517 518
        }
    }
    retval = fprintf(fp,"}\n}\n");
    if (retval == EOF) goto failure;

    /* Write edge info. */
    /* Edges from the output nodes. */
    for (i = 0; i < n; i++) {
519 520 521 522 523 524 525 526
        if (onames == NULL) {
            retval = fprintf(fp,"\"F%d\"", i);
        } else {
            retval = fprintf(fp,"\"  %s  \"", onames[i]);
        }
        if (retval == EOF) goto failure;
        /* Account for the possible complement on the root. */
        if (Cudd_IsComplement(f[i])) {
527
            retval = fprintf(fp," -> \"%lx\" [style = dotted];\n", ((mask & (ptrint) f[i]) / sizeof(DdNode)));
528
        } else {
529
            retval = fprintf(fp," -> \"%lx\" [style = solid];\n", ((mask & (ptrint) f[i]) / sizeof(DdNode)));
530 531
        }
        if (retval == EOF) goto failure;
Alan Mishchenko committed
532 533 534 535 536
    }

    /* Edges from internal nodes. */
    for (i = 0; i < nvars; i++) {
        if (sorted[dd->invperm[i]]) {
537 538 539 540 541 542
            nodelist = dd->subtables[i].nodelist;
            slots = dd->subtables[i].slots;
            for (j = 0; j < slots; j++) {
                scan = nodelist[j];
                while (scan != NULL) {
                    if (st_is_member(visited,(char *) scan)) {
543 544 545
                        retval = fprintf(fp, "\"%lx\" -> \"%lx\";\n", 
                            ((mask & (ptrint) scan) / sizeof(DdNode)),
                            ((mask & (ptrint) cuddT(scan)) / sizeof(DdNode)));
546 547
                        if (retval == EOF) goto failure;
                        if (Cudd_IsComplement(cuddE(scan))) {
548 549 550
                            retval = fprintf(fp,"\"%lx\" -> \"%lx\" [style = dotted];\n", 
                                ((mask & (ptrint) scan) / sizeof(DdNode)),
                                ((mask & (ptrint) cuddE(scan)) / sizeof(DdNode)));
551
                        } else {
552 553 554
                            retval = fprintf(fp, "\"%lx\" -> \"%lx\" [style = dashed];\n", 
                                ((mask & (ptrint) scan) / sizeof(DdNode)),
                                ((mask & (ptrint) cuddE(scan)) / sizeof(DdNode)));
555 556 557 558 559
                        }
                        if (retval == EOF) goto failure;
                    }
                    scan = scan->next;
                }
Alan Mishchenko committed
560 561 562 563 564 565 566 567
            }
        }
    }

    /* Write constant labels. */
    nodelist = dd->constants.nodelist;
    slots = dd->constants.slots;
    for (j = 0; j < slots; j++) {
568 569 570
        scan = nodelist[j];
        while (scan != NULL) {
            if (st_is_member(visited,(char *) scan)) {
571 572
                retval = fprintf(fp,"\"%lx\" [label = \"%g\"];\n", 
                    ((mask & (ptrint) scan) / sizeof(DdNode)), cuddV(scan));
573 574 575
                if (retval == EOF) goto failure;
            }
            scan = scan->next;
Alan Mishchenko committed
576 577 578 579 580 581 582 583
        }
    }

    /* Write trailer and return. */
    retval = fprintf(fp,"}\n");
    if (retval == EOF) goto failure;

    st_free_table(visited);
Alan Mishchenko committed
584
    ABC_FREE(sorted);
Alan Mishchenko committed
585 586 587
    return(1);

failure:
Alan Mishchenko committed
588
    if (sorted != NULL) ABC_FREE(sorted);
Alan Mishchenko committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
    if (support != NULL) Cudd_RecursiveDeref(dd,support);
    if (visited != NULL) st_free_table(visited);
    return(0);

} /* end of Cudd_DumpDot */


/**Function********************************************************************

  Synopsis    [Writes a daVinci file representing the argument BDDs.]

  Description [Writes a daVinci file representing the argument BDDs.
  It returns 1 in case of success; 0 otherwise (e.g., out-of-memory or
  file system full).  Cudd_DumpDaVinci does not close the file: This
  is the caller responsibility. Cudd_DumpDaVinci uses a minimal unique
  subset of the hexadecimal address of a node as name for it.  If the
  argument inames is non-null, it is assumed to hold the pointers to
  the names of the inputs. Similarly for onames.]

  SideEffects [None]

  SeeAlso     [Cudd_DumpDot Cudd_PrintDebug Cudd_DumpBlif Cudd_DumpDDcal
  Cudd_DumpFactoredForm]

******************************************************************************/
int
Cudd_DumpDaVinci(
  DdManager * dd /* manager */,
  int  n /* number of output nodes to be dumped */,
  DdNode ** f /* array of output nodes to be dumped */,
  char ** inames /* array of input names (or NULL) */,
  char ** onames /* array of output names (or NULL) */,
  FILE * fp /* pointer to the dump file */)
{
623 624 625 626 627 628 629
    DdNode        *support = NULL;
    DdNode        *scan;
    st_table      *visited = NULL;
    int           retval;
    int           i;
    st_generator  *gen;
    ptruint       refAddr, diff, mask;
Alan Mishchenko committed
630 631 632 633 634 635 636

    /* Initialize symbol table for visited nodes. */
    visited = st_init_table(st_ptrcmp, st_ptrhash);
    if (visited == NULL) goto failure;

    /* Collect all the nodes of this DD in the symbol table. */
    for (i = 0; i < n; i++) {
637 638
        retval = cuddCollectNodes(Cudd_Regular(f[i]),visited);
        if (retval == 0) goto failure;
Alan Mishchenko committed
639 640 641 642 643 644 645 646 647 648 649 650 651 652
    }

    /* Find how many most significant hex digits are identical
    ** in the addresses of all the nodes. Build a mask based
    ** on this knowledge, so that digits that carry no information
    ** will not be printed. This is done in two steps.
    **  1. We scan the symbol table to find the bits that differ
    **     in at least 2 addresses.
    **  2. We choose one of the possible masks. There are 8 possible
    **     masks for 32-bit integer, and 16 possible masks for 64-bit
    **     integers.
    */

    /* Find the bits that are different. */
653
    refAddr = (ptruint) Cudd_Regular(f[0]);
Alan Mishchenko committed
654 655
    diff = 0;
    gen = st_init_gen(visited);
656 657
    while (st_gen(gen, (const char **)&scan, NULL)) {
        diff |= refAddr ^ (ptruint) scan;
Alan Mishchenko committed
658 659 660 661
    }
    st_free_gen(gen);

    /* Choose the mask. */
662 663 664
    for (i = 0; (unsigned) i < 8 * sizeof(ptruint); i += 4) {
        mask = (1 << i) - 1;
        if (diff <= mask) break;
Alan Mishchenko committed
665 666 667 668 669 670 671 672 673 674 675
    }
    st_free_table(visited);

    /* Initialize symbol table for visited nodes. */
    visited = st_init_table(st_ptrcmp, st_ptrhash);
    if (visited == NULL) goto failure;

    retval = fprintf(fp, "[");
    if (retval == EOF) goto failure;
    /* Call the function that really gets the job done. */
    for (i = 0; i < n; i++) {
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
        if (onames == NULL) {
            retval = fprintf(fp,
                             "l(\"f%d\",n(\"root\",[a(\"OBJECT\",\"f%d\")],",
                             i,i);
        } else {
            retval = fprintf(fp,
                             "l(\"%s\",n(\"root\",[a(\"OBJECT\",\"%s\")],",
                             onames[i], onames[i]);
        }
        if (retval == EOF) goto failure;
        retval = fprintf(fp, "[e(\"edge\",[a(\"EDGECOLOR\",\"%s\"),a(\"_DIR\",\"none\")],",
                         Cudd_IsComplement(f[i]) ? "red" : "blue");
        if (retval == EOF) goto failure;
        retval = ddDoDumpDaVinci(dd,Cudd_Regular(f[i]),fp,visited,inames,mask);
        if (retval == 0) goto failure;
        retval = fprintf(fp, ")]))%s", i == n-1 ? "" : ",");
        if (retval == EOF) goto failure;
Alan Mishchenko committed
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
    }

    /* Write trailer and return. */
    retval = fprintf(fp, "]\n");
    if (retval == EOF) goto failure;

    st_free_table(visited);
    return(1);

failure:
    if (support != NULL) Cudd_RecursiveDeref(dd,support);
    if (visited != NULL) st_free_table(visited);
    return(0);

} /* end of Cudd_DumpDaVinci */


/**Function********************************************************************

  Synopsis    [Writes a DDcal file representing the argument BDDs.]

  Description [Writes a DDcal file representing the argument BDDs.
  It returns 1 in case of success; 0 otherwise (e.g., out-of-memory or
  file system full).  Cudd_DumpDDcal does not close the file: This
  is the caller responsibility. Cudd_DumpDDcal uses a minimal unique
  subset of the hexadecimal address of a node as name for it.  If the
  argument inames is non-null, it is assumed to hold the pointers to
  the names of the inputs. Similarly for onames.]

  SideEffects [None]

  SeeAlso     [Cudd_DumpDot Cudd_PrintDebug Cudd_DumpBlif Cudd_DumpDaVinci
  Cudd_DumpFactoredForm]

******************************************************************************/
int
Cudd_DumpDDcal(
  DdManager * dd /* manager */,
  int  n /* number of output nodes to be dumped */,
  DdNode ** f /* array of output nodes to be dumped */,
  char ** inames /* array of input names (or NULL) */,
  char ** onames /* array of output names (or NULL) */,
  FILE * fp /* pointer to the dump file */)
{
737 738 739 740 741 742 743 744 745
    DdNode        *support = NULL;
    DdNode        *scan;
    int           *sorted = NULL;
    int           nvars = dd->size;
    st_table      *visited = NULL;
    int           retval;
    int           i;
    st_generator  *gen;
    ptruint       refAddr, diff, mask;
Alan Mishchenko committed
746 747 748 749 750 751 752

    /* Initialize symbol table for visited nodes. */
    visited = st_init_table(st_ptrcmp, st_ptrhash);
    if (visited == NULL) goto failure;

    /* Collect all the nodes of this DD in the symbol table. */
    for (i = 0; i < n; i++) {
753 754
        retval = cuddCollectNodes(Cudd_Regular(f[i]),visited);
        if (retval == 0) goto failure;
Alan Mishchenko committed
755 756 757 758 759 760 761 762 763 764 765 766 767 768
    }

    /* Find how many most significant hex digits are identical
    ** in the addresses of all the nodes. Build a mask based
    ** on this knowledge, so that digits that carry no information
    ** will not be printed. This is done in two steps.
    **  1. We scan the symbol table to find the bits that differ
    **     in at least 2 addresses.
    **  2. We choose one of the possible masks. There are 8 possible
    **     masks for 32-bit integer, and 16 possible masks for 64-bit
    **     integers.
    */

    /* Find the bits that are different. */
769
    refAddr = (ptruint) Cudd_Regular(f[0]);
Alan Mishchenko committed
770 771
    diff = 0;
    gen = st_init_gen(visited);
772 773
    while (st_gen(gen, (const char **)&scan, NULL)) {
        diff |= refAddr ^ (ptruint) scan;
Alan Mishchenko committed
774 775 776 777
    }
    st_free_gen(gen);

    /* Choose the mask. */
778 779 780
    for (i = 0; (unsigned) i < 8 * sizeof(ptruint); i += 4) {
        mask = (1 << i) - 1;
        if (diff <= mask) break;
Alan Mishchenko committed
781 782
    }
    st_free_table(visited);
783
    visited = NULL;
Alan Mishchenko committed
784 785

    /* Build a bit array with the support of f. */
Alan Mishchenko committed
786
    sorted = ABC_ALLOC(int,nvars);
Alan Mishchenko committed
787
    if (sorted == NULL) {
788 789
        dd->errorCode = CUDD_MEMORY_OUT;
        goto failure;
Alan Mishchenko committed
790 791 792 793 794 795 796 797 798
    }
    for (i = 0; i < nvars; i++) sorted[i] = 0;

    /* Take the union of the supports of each output function. */
    support = Cudd_VectorSupport(dd,f,n);
    if (support == NULL) goto failure;
    cuddRef(support);
    scan = support;
    while (!cuddIsConstant(scan)) {
799 800
        sorted[scan->index] = 1;
        scan = cuddT(scan);
Alan Mishchenko committed
801 802
    }
    Cudd_RecursiveDeref(dd,support);
803
    support = NULL; /* so that we do not try to free it in case of failure */
Alan Mishchenko committed
804 805
    for (i = 0; i < nvars; i++) {
        if (sorted[dd->invperm[i]]) {
806 807 808 809 810
            if (inames == NULL || inames[dd->invperm[i]] == NULL) {
                retval = fprintf(fp,"v%d", dd->invperm[i]);
            } else {
                retval = fprintf(fp,"%s", inames[dd->invperm[i]]);
            }
Alan Mishchenko committed
811 812
            if (retval == EOF) goto failure;
        }
813 814
        retval = fprintf(fp,"%s", i == nvars - 1 ? "\n" : " * ");
        if (retval == EOF) goto failure;
Alan Mishchenko committed
815
    }
Alan Mishchenko committed
816
    ABC_FREE(sorted);
Alan Mishchenko committed
817 818 819 820 821 822 823 824
    sorted = NULL;

    /* Initialize symbol table for visited nodes. */
    visited = st_init_table(st_ptrcmp, st_ptrhash);
    if (visited == NULL) goto failure;

    /* Call the function that really gets the job done. */
    for (i = 0; i < n; i++) {
825 826 827 828 829 830 831 832 833 834 835 836 837
        retval = ddDoDumpDDcal(dd,Cudd_Regular(f[i]),fp,visited,inames,mask);
        if (retval == 0) goto failure;
        if (onames == NULL) {
            retval = fprintf(fp, "f%d = ", i);
        } else {
            retval = fprintf(fp, "%s = ", onames[i]);
        }
        if (retval == EOF) goto failure;
        retval = fprintf(fp, "n%p%s\n",
                         (void *) (((ptruint) f[i] & mask) /
                         (ptruint) sizeof(DdNode)),
                         Cudd_IsComplement(f[i]) ? "'" : "");
        if (retval == EOF) goto failure;
Alan Mishchenko committed
838 839 840 841 842 843
    }

    /* Write trailer and return. */
    retval = fprintf(fp, "[");
    if (retval == EOF) goto failure;
    for (i = 0; i < n; i++) {
844 845 846 847 848 849 850
        if (onames == NULL) {
            retval = fprintf(fp, "f%d", i);
        } else {
            retval = fprintf(fp, "%s", onames[i]);
        }
        retval = fprintf(fp, "%s", i == n-1 ? "" : " ");
        if (retval == EOF) goto failure;
Alan Mishchenko committed
851 852 853 854
    }
    retval = fprintf(fp, "]\n");
    if (retval == EOF) goto failure;

855 856
    if ( visited )
        st_free_table(visited);
Alan Mishchenko committed
857 858 859
    return(1);

failure:
Alan Mishchenko committed
860
    if (sorted != NULL) ABC_FREE(sorted);
Alan Mishchenko committed
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
    if (support != NULL) Cudd_RecursiveDeref(dd,support);
    if (visited != NULL) st_free_table(visited);
    return(0);

} /* end of Cudd_DumpDDcal */


/**Function********************************************************************

  Synopsis    [Writes factored forms representing the argument BDDs.]

  Description [Writes factored forms representing the argument BDDs.
  The format of the factored form is the one used in the genlib files
  for technology mapping in sis.  It returns 1 in case of success; 0
  otherwise (e.g., file system full).  Cudd_DumpFactoredForm does not
  close the file: This is the caller responsibility. Caution must be
  exercised because a factored form may be exponentially larger than
  the argument BDD.  If the argument inames is non-null, it is assumed
  to hold the pointers to the names of the inputs. Similarly for
  onames.]

  SideEffects [None]

  SeeAlso     [Cudd_DumpDot Cudd_PrintDebug Cudd_DumpBlif Cudd_DumpDaVinci
  Cudd_DumpDDcal]

******************************************************************************/
int
Cudd_DumpFactoredForm(
  DdManager * dd /* manager */,
  int  n /* number of output nodes to be dumped */,
  DdNode ** f /* array of output nodes to be dumped */,
  char ** inames /* array of input names (or NULL) */,
  char ** onames /* array of output names (or NULL) */,
  FILE * fp /* pointer to the dump file */)
{
897 898
    int         retval;
    int         i;
Alan Mishchenko committed
899 900 901

    /* Call the function that really gets the job done. */
    for (i = 0; i < n; i++) {
902 903 904 905 906
        if (onames == NULL) {
            retval = fprintf(fp, "f%d = ", i);
        } else {
            retval = fprintf(fp, "%s = ", onames[i]);
        }
Alan Mishchenko committed
907
        if (retval == EOF) return(0);
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
        if (f[i] == DD_ONE(dd)) {
            retval = fprintf(fp, "CONST1");
            if (retval == EOF) return(0);
        } else if (f[i] == Cudd_Not(DD_ONE(dd)) || f[i] == DD_ZERO(dd)) {
            retval = fprintf(fp, "CONST0");
            if (retval == EOF) return(0);
        } else {
            retval = fprintf(fp, "%s", Cudd_IsComplement(f[i]) ? "!(" : "");
            if (retval == EOF) return(0);
            retval = ddDoDumpFactoredForm(dd,Cudd_Regular(f[i]),fp,inames);
            if (retval == 0) return(0);
            retval = fprintf(fp, "%s", Cudd_IsComplement(f[i]) ? ")" : "");
            if (retval == EOF) return(0);
        }
        retval = fprintf(fp, "%s", i == n-1 ? "" : "\n");
Alan Mishchenko committed
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
        if (retval == EOF) return(0);
    }

    return(1);

} /* end of Cudd_DumpFactoredForm */


/*---------------------------------------------------------------------------*/
/* Definition of internal functions                                          */
/*---------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------*/
/* Definition of static functions                                            */
/*---------------------------------------------------------------------------*/


/**Function********************************************************************

  Synopsis    [Performs the recursive step of Cudd_DumpBlif.]

  Description [Performs the recursive step of Cudd_DumpBlif. Traverses
  the BDD f and writes a multiplexer-network description to the file
  pointed by fp in blif format. f is assumed to be a regular pointer
  and ddDoDumpBlif guarantees this assumption in the recursive calls.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
ddDoDumpBlif(
  DdManager * dd,
  DdNode * f,
  FILE * fp,
  st_table * visited,
961 962
  char ** names,
  int mv)
Alan Mishchenko committed
963
{
964 965
    DdNode      *T, *E;
    int         retval;
Alan Mishchenko committed
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985

#ifdef DD_DEBUG
    assert(!Cudd_IsComplement(f));
#endif

    /* If already visited, nothing to do. */
    if (st_is_member(visited, (char *) f) == 1)
        return(1);

    /* Check for abnormal condition that should never happen. */
    if (f == NULL)
        return(0);

    /* Mark node as visited. */
    if (st_insert(visited, (char *) f, NULL) == ST_OUT_OF_MEM)
        return(0);

    /* Check for special case: If constant node, generate constant 1. */
    if (f == DD_ONE(dd)) {
#if SIZEOF_VOID_P == 8
986
        retval = fprintf(fp, ".names %lx\n1\n",(ptruint) f / (ptruint) sizeof(DdNode));
Alan Mishchenko committed
987
#else
988
        retval = fprintf(fp, ".names %x\n1\n",(ptruint) f / (ptruint) sizeof(DdNode));
Alan Mishchenko committed
989 990 991 992 993 994 995 996 997 998 999 1000 1001
#endif
        if (retval == EOF) {
            return(0);
        } else {
            return(1);
        }
    }

    /* Check whether this is an ADD. We deal with 0-1 ADDs, but not
    ** with the general case.
    */
    if (f == DD_ZERO(dd)) {
#if SIZEOF_VOID_P == 8
1002 1003 1004
        retval = fprintf(fp, ".names %lx\n%s",
                         (ptruint) f / (ptruint) sizeof(DdNode),
                         mv ? "0\n" : "");
Alan Mishchenko committed
1005
#else
1006 1007 1008
        retval = fprintf(fp, ".names %x\n%s",
                         (ptruint) f / (ptruint) sizeof(DdNode),
                         mv ? "0\n" : "");
Alan Mishchenko committed
1009 1010 1011 1012 1013 1014 1015 1016
#endif
        if (retval == EOF) {
            return(0);
        } else {
            return(1);
        }
    }
    if (cuddIsConstant(f))
1017
        return(0);
Alan Mishchenko committed
1018 1019 1020

    /* Recursive calls. */
    T = cuddT(f);
1021
    retval = ddDoDumpBlif(dd,T,fp,visited,names,mv);
Alan Mishchenko committed
1022 1023
    if (retval != 1) return(retval);
    E = Cudd_Regular(cuddE(f));
1024
    retval = ddDoDumpBlif(dd,E,fp,visited,names,mv);
Alan Mishchenko committed
1025 1026 1027 1028
    if (retval != 1) return(retval);

    /* Write multiplexer taking complement arc into account. */
    if (names != NULL) {
1029
        retval = fprintf(fp,".names %s", names[f->index]);
Alan Mishchenko committed
1030
    } else {
1031 1032 1033 1034 1035
#if SIZEOF_VOID_P == 8 && SIZEOF_INT == 4
        retval = fprintf(fp,".names %u", f->index);
#else
        retval = fprintf(fp,".names %hu", f->index);
#endif
Alan Mishchenko committed
1036 1037
    }
    if (retval == EOF)
1038
        return(0);
Alan Mishchenko committed
1039 1040

#if SIZEOF_VOID_P == 8
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
    if (mv) {
        if (Cudd_IsComplement(cuddE(f))) {
            retval = fprintf(fp," %lx %lx %lx\n.def 0\n1 1 - 1\n0 - 0 1\n",
                (ptruint) T / (ptruint) sizeof(DdNode),
                (ptruint) E / (ptruint) sizeof(DdNode),
                (ptruint) f / (ptruint) sizeof(DdNode));
        } else {
            retval = fprintf(fp," %lx %lx %lx\n.def 0\n1 1 - 1\n0 - 1 1\n",
                (ptruint) T / (ptruint) sizeof(DdNode),
                (ptruint) E / (ptruint) sizeof(DdNode),
                (ptruint) f / (ptruint) sizeof(DdNode));
        }
Alan Mishchenko committed
1053
    } else {
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
        if (Cudd_IsComplement(cuddE(f))) {
            retval = fprintf(fp," %lx %lx %lx\n11- 1\n0-0 1\n",
                (ptruint) T / (ptruint) sizeof(DdNode),
                (ptruint) E / (ptruint) sizeof(DdNode),
                (ptruint) f / (ptruint) sizeof(DdNode));
        } else {
            retval = fprintf(fp," %lx %lx %lx\n11- 1\n0-1 1\n",
                (ptruint) T / (ptruint) sizeof(DdNode),
                (ptruint) E / (ptruint) sizeof(DdNode),
                (ptruint) f / (ptruint) sizeof(DdNode));
        }
Alan Mishchenko committed
1065 1066
    }
#else
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
    if (mv) {
        if (Cudd_IsComplement(cuddE(f))) {
            retval = fprintf(fp," %x %x %x\n.def 0\n1 1 - 1\n0 - 0 1\n",
                (ptruint) T / (ptruint) sizeof(DdNode),
                (ptruint) E / (ptruint) sizeof(DdNode),
                (ptruint) f / (ptruint) sizeof(DdNode));
        } else {
            retval = fprintf(fp," %x %x %x\n.def 0\n1 1 - 1\n0 - 1 1\n",
                (ptruint) T / (ptruint) sizeof(DdNode),
                (ptruint) E / (ptruint) sizeof(DdNode),
                (ptruint) f / (ptruint) sizeof(DdNode));
        }
Alan Mishchenko committed
1079
    } else {
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
        if (Cudd_IsComplement(cuddE(f))) {
            retval = fprintf(fp," %x %x %x\n11- 1\n0-0 1\n",
                (ptruint) T / (ptruint) sizeof(DdNode),
                (ptruint) E / (ptruint) sizeof(DdNode),
                (ptruint) f / (ptruint) sizeof(DdNode));
        } else {
            retval = fprintf(fp," %x %x %x\n11- 1\n0-1 1\n",
                (ptruint) T / (ptruint) sizeof(DdNode),
                (ptruint) E / (ptruint) sizeof(DdNode),
                (ptruint) f / (ptruint) sizeof(DdNode));
        }
Alan Mishchenko committed
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
    }
#endif
    if (retval == EOF) {
        return(0);
    } else {
        return(1);
    }

} /* end of ddDoDumpBlif */


/**Function********************************************************************

  Synopsis    [Performs the recursive step of Cudd_DumpDaVinci.]

  Description [Performs the recursive step of Cudd_DumpDaVinci. Traverses
  the BDD f and writes a term expression to the file
  pointed by fp in daVinci format. f is assumed to be a regular pointer
  and ddDoDumpDaVinci guarantees this assumption in the recursive calls.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
ddDoDumpDaVinci(
  DdManager * dd,
  DdNode * f,
  FILE * fp,
  st_table * visited,
  char ** names,
1123
  ptruint mask)
Alan Mishchenko committed
1124
{
1125 1126 1127
    DdNode  *T, *E;
    int     retval;
    ptruint id;
Alan Mishchenko committed
1128 1129 1130 1131 1132

#ifdef DD_DEBUG
    assert(!Cudd_IsComplement(f));
#endif

1133
    id = ((ptruint) f & mask) / sizeof(DdNode);
Alan Mishchenko committed
1134 1135 1136

    /* If already visited, insert a reference. */
    if (st_is_member(visited, (char *) f) == 1) {
1137
        retval = fprintf(fp,"r(\"%p\")", (void *) id);
Alan Mishchenko committed
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
        if (retval == EOF) {
            return(0);
        } else {
            return(1);
        }
    }

    /* Check for abnormal condition that should never happen. */
    if (f == NULL)
        return(0);

    /* Mark node as visited. */
    if (st_insert(visited, (char *) f, NULL) == ST_OUT_OF_MEM)
        return(0);

    /* Check for special case: If constant node, generate constant 1. */
    if (Cudd_IsConstant(f)) {
1155 1156 1157
        retval = fprintf(fp,
                         "l(\"%p\",n(\"constant\",[a(\"OBJECT\",\"%g\")],[]))",
                         (void *) id, cuddV(f));
Alan Mishchenko committed
1158 1159 1160 1161 1162 1163 1164 1165 1166
        if (retval == EOF) {
            return(0);
        } else {
            return(1);
        }
    }

    /* Recursive calls. */
    if (names != NULL) {
1167 1168 1169
        retval = fprintf(fp,
                         "l(\"%p\",n(\"internal\",[a(\"OBJECT\",\"%s\"),",
                         (void *) id, names[f->index]);
Alan Mishchenko committed
1170
    } else {
1171 1172 1173 1174 1175 1176 1177
        retval = fprintf(fp,
#if SIZEOF_VOID_P == 8
                         "l(\"%p\",n(\"internal\",[a(\"OBJECT\",\"%u\"),",
#else
                         "l(\"%p\",n(\"internal\",[a(\"OBJECT\",\"%hu\"),",
#endif
                         (void *) id, f->index);
Alan Mishchenko committed
1178 1179 1180 1181 1182 1183 1184
    }
    retval = fprintf(fp, "a(\"_GO\",\"ellipse\")],[e(\"then\",[a(\"EDGECOLOR\",\"blue\"),a(\"_DIR\",\"none\")],");
    if (retval == EOF) return(0);
    T = cuddT(f);
    retval = ddDoDumpDaVinci(dd,T,fp,visited,names,mask);
    if (retval != 1) return(retval);
    retval = fprintf(fp, "),e(\"else\",[a(\"EDGECOLOR\",\"%s\"),a(\"_DIR\",\"none\")],",
1185
                     Cudd_IsComplement(cuddE(f)) ? "red" : "green");
Alan Mishchenko committed
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
    if (retval == EOF) return(0);
    E = Cudd_Regular(cuddE(f));
    retval = ddDoDumpDaVinci(dd,E,fp,visited,names,mask);
    if (retval != 1) return(retval);

    retval = fprintf(fp,")]))");
    if (retval == EOF) {
        return(0);
    } else {
        return(1);
    }

} /* end of ddDoDumpDaVinci */


/**Function********************************************************************

  Synopsis    [Performs the recursive step of Cudd_DumpDDcal.]

  Description [Performs the recursive step of Cudd_DumpDDcal. Traverses
  the BDD f and writes a line for each node to the file
  pointed by fp in DDcal format. f is assumed to be a regular pointer
  and ddDoDumpDDcal guarantees this assumption in the recursive calls.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
ddDoDumpDDcal(
  DdManager * dd,
  DdNode * f,
  FILE * fp,
  st_table * visited,
  char ** names,
1222
  ptruint mask)
Alan Mishchenko committed
1223
{
1224 1225 1226
    DdNode  *T, *E;
    int     retval;
    ptruint id, idT, idE;
Alan Mishchenko committed
1227 1228 1229 1230 1231

#ifdef DD_DEBUG
    assert(!Cudd_IsComplement(f));
#endif

1232
    id = ((ptruint) f & mask) / sizeof(DdNode);
Alan Mishchenko committed
1233 1234 1235

    /* If already visited, do nothing. */
    if (st_is_member(visited, (char *) f) == 1) {
1236
        return(1);
Alan Mishchenko committed
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
    }

    /* Check for abnormal condition that should never happen. */
    if (f == NULL)
        return(0);

    /* Mark node as visited. */
    if (st_insert(visited, (char *) f, NULL) == ST_OUT_OF_MEM)
        return(0);

    /* Check for special case: If constant node, assign constant. */
    if (Cudd_IsConstant(f)) {
1249 1250 1251
        if (f != DD_ONE(dd) && f != DD_ZERO(dd))
            return(0);
        retval = fprintf(fp, "n%p = %g\n", (void *) id, cuddV(f));
Alan Mishchenko committed
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
        if (retval == EOF) {
            return(0);
        } else {
            return(1);
        }
    }

    /* Recursive calls. */
    T = cuddT(f);
    retval = ddDoDumpDDcal(dd,T,fp,visited,names,mask);
    if (retval != 1) return(retval);
    E = Cudd_Regular(cuddE(f));
    retval = ddDoDumpDDcal(dd,E,fp,visited,names,mask);
    if (retval != 1) return(retval);
1266 1267
    idT = ((ptruint) T & mask) / sizeof(DdNode);
    idE = ((ptruint) E & mask) / sizeof(DdNode);
Alan Mishchenko committed
1268
    if (names != NULL) {
1269 1270 1271 1272
        retval = fprintf(fp, "n%p = %s * n%p + %s' * n%p%s\n",
                         (void *) id, names[f->index],
                         (void *) idT, names[f->index],
                         (void *) idE, Cudd_IsComplement(cuddE(f)) ? "'" : "");
Alan Mishchenko committed
1273
    } else {
1274 1275 1276 1277 1278 1279 1280 1281
#if SIZEOF_VOID_P == 8
        retval = fprintf(fp, "n%p = v%u * n%p + v%u' * n%p%s\n",
#else
        retval = fprintf(fp, "n%p = v%hu * n%p + v%hu' * n%p%s\n",
#endif
                         (void *) id, f->index,
                         (void *) idT, f->index,
                         (void *) idE, Cudd_IsComplement(cuddE(f)) ? "'" : "");
Alan Mishchenko committed
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
    }
    if (retval == EOF) {
        return(0);
    } else {
        return(1);
    }

} /* end of ddDoDumpDDcal */


/**Function********************************************************************

  Synopsis    [Performs the recursive step of Cudd_DumpFactoredForm.]

  Description [Performs the recursive step of
  Cudd_DumpFactoredForm. Traverses the BDD f and writes a factored
  form for each node to the file pointed by fp in terms of the
  factored forms of the children. Constants are propagated, and
  absorption is applied.  f is assumed to be a regular pointer and
  ddDoDumpFActoredForm guarantees this assumption in the recursive
  calls.]

  SideEffects [None]

  SeeAlso     [Cudd_DumpFactoredForm]

******************************************************************************/
static int
ddDoDumpFactoredForm(
  DdManager * dd,
  DdNode * f,
  FILE * fp,
  char ** names)
{
1316 1317
    DdNode      *T, *E;
    int         retval;
Alan Mishchenko committed
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331

#ifdef DD_DEBUG
    assert(!Cudd_IsComplement(f));
    assert(!Cudd_IsConstant(f));
#endif

    /* Check for abnormal condition that should never happen. */
    if (f == NULL)
        return(0);

    /* Recursive calls. */
    T = cuddT(f);
    E = cuddE(f);
    if (T != DD_ZERO(dd)) {
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
        if (E != DD_ONE(dd)) {
            if (names != NULL) {
                retval = fprintf(fp, "%s", names[f->index]);
            } else {
#if SIZEOF_VOID_P == 8 && SIZEOF_INT == 4
                retval = fprintf(fp, "x%u", f->index);
#else
                retval = fprintf(fp, "x%hu", f->index);
#endif
            }
            if (retval == EOF) return(0);
        }
        if (T != DD_ONE(dd)) {
            retval = fprintf(fp, "%s(", E != DD_ONE(dd) ? " * " : "");
            if (retval == EOF) return(0);
            retval = ddDoDumpFactoredForm(dd,T,fp,names);
            if (retval != 1) return(retval);
            retval = fprintf(fp, ")");
            if (retval == EOF) return(0);
        }
        if (E == Cudd_Not(DD_ONE(dd)) || E == DD_ZERO(dd)) return(1);
        retval = fprintf(fp, " + ");
        if (retval == EOF) return(0);
    }
    E = Cudd_Regular(E);
    if (T != DD_ONE(dd)) {
Alan Mishchenko committed
1358
        if (names != NULL) {
1359
            retval = fprintf(fp, "!%s", names[f->index]);
Alan Mishchenko committed
1360
        } else {
1361 1362 1363 1364 1365
#if SIZEOF_VOID_P == 8 && SIZEOF_INT == 4
            retval = fprintf(fp, "!x%u", f->index);
#else
            retval = fprintf(fp, "!x%hu", f->index);
#endif
Alan Mishchenko committed
1366 1367 1368
        }
        if (retval == EOF) return(0);
    }
1369 1370 1371
    if (E != DD_ONE(dd)) {
        retval = fprintf(fp, "%s%s(", T != DD_ONE(dd) ? " * " : "",
                         E != cuddE(f) ? "!" : "");
Alan Mishchenko committed
1372
        if (retval == EOF) return(0);
1373
        retval = ddDoDumpFactoredForm(dd,E,fp,names);
Alan Mishchenko committed
1374 1375 1376 1377 1378 1379 1380 1381
        if (retval != 1) return(retval);
        retval = fprintf(fp, ")");
        if (retval == EOF) return(0);
    }
    return(1);

} /* end of ddDoDumpFactoredForm */

1382

1383 1384
ABC_NAMESPACE_IMPL_END