decPrint.c 9.13 KB
Newer Older
Alan Mishchenko committed
1 2
/**CFile****************************************************************

Alan Mishchenko committed
3
  FileName    [decPrint.c]
Alan Mishchenko committed
4 5 6

  PackageName [MVSIS 2.0: Multi-valued logic synthesis system.]

Alan Mishchenko committed
7
  Synopsis    [Procedures to print the decomposition graphs (factored forms).]
Alan Mishchenko committed
8 9 10 11 12 13 14

  Author      [MVSIS Group]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - February 1, 2003.]

Alan Mishchenko committed
15
  Revision    [$Id: decPrint.c,v 1.1 2003/05/22 19:20:05 alanmi Exp $]
Alan Mishchenko committed
16 17 18

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

19
#include "base/abc/abc.h"
Alan Mishchenko committed
20
#include "dec.h"
Alan Mishchenko committed
21

22 23 24
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
25 26 27 28
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

Alan Mishchenko committed
29 30 31
static void   Dec_GraphPrint_rec( FILE * pFile, Dec_Graph_t * pGraph, Dec_Node_t * pNode, int fCompl, char * pNamesIn[], int * pPos, int LitSizeMax );
static int    Dec_GraphPrintGetLeafName( FILE * pFile, int iLeaf, int fCompl, char * pNamesIn[] );
static void   Dec_GraphPrintUpdatePos( FILE * pFile, int * pPos, int LitSizeMax );
32
static int    Dec_GraphPrintOutputName( FILE * pFile, char * pNameOut );
Alan Mishchenko committed
33 34

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
35
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
36 37 38 39
////////////////////////////////////////////////////////////////////////

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

Alan Mishchenko committed
40
  Synopsis    [Prints the decomposition graph.]
Alan Mishchenko committed
41 42 43 44 45 46 47 48

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
49
void Dec_GraphPrint( FILE * pFile, Dec_Graph_t * pGraph, char * pNamesIn[], char * pNameOut )
Alan Mishchenko committed
50
{
Alan Mishchenko committed
51
    Vec_Ptr_t * vNamesIn = NULL;
Alan Mishchenko committed
52
    int LitSizeMax, LitSizeCur, Pos, i;
Alan Mishchenko committed
53 54 55 56

    // create the names if not given by the user
    if ( pNamesIn == NULL )
    {
Alan Mishchenko committed
57
        vNamesIn = Abc_NodeGetFakeNames( Dec_GraphLeaveNum(pGraph) );
Alan Mishchenko committed
58
        pNamesIn = (char **)vNamesIn->pArray;
Alan Mishchenko committed
59
    }
Alan Mishchenko committed
60 61
    if ( pNameOut == NULL )
        pNameOut = "F";
Alan Mishchenko committed
62 63 64

    // get the size of the longest literal
    LitSizeMax = 0;
Alan Mishchenko committed
65
    for ( i = 0; i < Dec_GraphLeaveNum(pGraph); i++ )
Alan Mishchenko committed
66 67 68 69 70 71 72 73
    {
        LitSizeCur = strlen(pNamesIn[i]);
        if ( LitSizeMax < LitSizeCur )
            LitSizeMax = LitSizeCur;
    }
    if ( LitSizeMax > 50 )
        LitSizeMax = 20;

Alan Mishchenko committed
74 75
    // write the decomposition graph (factored form)
    if ( Dec_GraphIsConst(pGraph) ) // constant
Alan Mishchenko committed
76
    {
77
        Pos = Dec_GraphPrintOutputName( pFile, pNameOut );
Alan Mishchenko committed
78
        fprintf( pFile, "Constant %d", !Dec_GraphIsComplement(pGraph) );
Alan Mishchenko committed
79
    }
Alan Mishchenko committed
80
    else if ( Dec_GraphIsVar(pGraph) ) // literal
Alan Mishchenko committed
81
    {
82
        Pos = Dec_GraphPrintOutputName( pFile, pNameOut );
Alan Mishchenko committed
83 84 85 86
        Dec_GraphPrintGetLeafName( pFile, Dec_GraphVarInt(pGraph), Dec_GraphIsComplement(pGraph), pNamesIn );
    }
    else
    {
87 88
        Pos = Dec_GraphPrintOutputName( pFile, pNameOut );
        Dec_GraphPrint_rec( pFile, pGraph, Dec_GraphNodeLast(pGraph), Dec_GraphIsComplement(pGraph), pNamesIn, &Pos, LitSizeMax );
Alan Mishchenko committed
89 90 91
    }
    fprintf( pFile, "\n" );

Alan Mishchenko committed
92 93
    if ( vNamesIn )
        Abc_NodeFreeNames( vNamesIn );
Alan Mishchenko committed
94 95 96 97 98 99 100 101 102 103 104 105 106
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
107
void Dec_GraphPrint2_rec( FILE * pFile, Dec_Graph_t * pGraph, Dec_Node_t * pNode, int fCompl, char * pNamesIn[], int * pPos, int LitSizeMax )
Alan Mishchenko committed
108
{
Alan Mishchenko committed
109 110 111 112
    Dec_Node_t * pNode0, * pNode1;
    pNode0 = Dec_GraphNode(pGraph, pNode->eEdge0.Node);
    pNode1 = Dec_GraphNode(pGraph, pNode->eEdge1.Node);
    if ( Dec_GraphNodeIsVar(pGraph, pNode) ) // FT_NODE_LEAF )
Alan Mishchenko committed
113
    {
Alan Mishchenko committed
114
        (*pPos) += Dec_GraphPrintGetLeafName( pFile, Dec_GraphNodeInt(pGraph,pNode), fCompl, pNamesIn );
Alan Mishchenko committed
115 116 117 118 119
        return;
    }
    if ( !pNode->fNodeOr ) // FT_NODE_AND )
    {
        if ( !pNode0->fNodeOr ) // != FT_NODE_OR )
Alan Mishchenko committed
120
            Dec_GraphPrint_rec( pFile, pGraph, pNode0, pNode->fCompl0, pNamesIn, pPos, LitSizeMax );
Alan Mishchenko committed
121 122 123 124
        else
        {
            fprintf( pFile, "(" );
            (*pPos)++;
Alan Mishchenko committed
125
            Dec_GraphPrint_rec( pFile, pGraph, pNode0, pNode->fCompl0, pNamesIn, pPos, LitSizeMax );
Alan Mishchenko committed
126 127 128 129 130 131
            fprintf( pFile, ")" );
            (*pPos)++;
        }
        fprintf( pFile, " " );
        (*pPos)++;

Alan Mishchenko committed
132
        Dec_GraphPrintUpdatePos( pFile, pPos, LitSizeMax );
Alan Mishchenko committed
133 134

        if ( !pNode1->fNodeOr ) // != FT_NODE_OR )
Alan Mishchenko committed
135
            Dec_GraphPrint_rec( pFile, pGraph, pNode1, pNode->fCompl1, pNamesIn, pPos, LitSizeMax );
Alan Mishchenko committed
136 137 138 139
        else
        {
            fprintf( pFile, "(" );
            (*pPos)++;
Alan Mishchenko committed
140
            Dec_GraphPrint_rec( pFile, pGraph, pNode1, pNode->fCompl1, pNamesIn, pPos, LitSizeMax );
Alan Mishchenko committed
141 142 143 144 145 146 147
            fprintf( pFile, ")" );
            (*pPos)++;
        }
        return;
    }
    if ( pNode->fNodeOr ) // FT_NODE_OR )
    {
Alan Mishchenko committed
148
        Dec_GraphPrint_rec( pFile, pGraph, pNode0, pNode->fCompl0, pNamesIn, pPos, LitSizeMax );
Alan Mishchenko committed
149 150 151
        fprintf( pFile, " + " );
        (*pPos) += 3;

Alan Mishchenko committed
152
        Dec_GraphPrintUpdatePos( pFile, pPos, LitSizeMax );
Alan Mishchenko committed
153

Alan Mishchenko committed
154
        Dec_GraphPrint_rec( pFile, pGraph, pNode1, pNode->fCompl1, pNamesIn, pPos, LitSizeMax );
Alan Mishchenko committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
        return;
    }
    assert( 0 );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
void Dec_GraphPrint_rec( FILE * pFile, Dec_Graph_t * pGraph, Dec_Node_t * pNode, int fCompl, char * pNamesIn[], int * pPos, int LitSizeMax )
{
    Dec_Node_t * pNode0, * pNode1;
    Dec_Node_t * pNode00, * pNode01, * pNode10, * pNode11;
    pNode0 = Dec_GraphNode(pGraph, pNode->eEdge0.Node);
    pNode1 = Dec_GraphNode(pGraph, pNode->eEdge1.Node);
    if ( Dec_GraphNodeIsVar(pGraph, pNode) ) // FT_NODE_LEAF )
    {
        (*pPos) += Dec_GraphPrintGetLeafName( pFile, Dec_GraphNodeInt(pGraph,pNode), fCompl, pNamesIn );
        return;
    }
    if ( !Dec_GraphNodeIsVar(pGraph, pNode0) && !Dec_GraphNodeIsVar(pGraph, pNode1) )
    {
        pNode00 = Dec_GraphNode(pGraph, pNode0->eEdge0.Node);
        pNode01 = Dec_GraphNode(pGraph, pNode0->eEdge1.Node);
        pNode10 = Dec_GraphNode(pGraph, pNode1->eEdge0.Node);
        pNode11 = Dec_GraphNode(pGraph, pNode1->eEdge1.Node);
        if ( (pNode00 == pNode10 || pNode00 == pNode11) && (pNode01 == pNode10 || pNode01 == pNode11) )
        {
            fprintf( pFile, "(" );
            (*pPos)++;
            Dec_GraphPrint_rec( pFile, pGraph, pNode00, pNode00->fCompl0, pNamesIn, pPos, LitSizeMax );
            fprintf( pFile, " # " );
            (*pPos) += 3;
            Dec_GraphPrint_rec( pFile, pGraph, pNode01, pNode01->fCompl1, pNamesIn, pPos, LitSizeMax );
            fprintf( pFile, ")" );
            (*pPos)++;
            return;
        }
    }
    if ( fCompl )
    {
        fprintf( pFile, "(" );
        (*pPos)++;
205
        Dec_GraphPrint_rec( pFile, pGraph, pNode0, !pNode->eEdge0.fCompl, pNamesIn, pPos, LitSizeMax );
Alan Mishchenko committed
206 207
        fprintf( pFile, " + " );
        (*pPos) += 3;
208
        Dec_GraphPrint_rec( pFile, pGraph, pNode1, !pNode->eEdge1.fCompl, pNamesIn, pPos, LitSizeMax );
Alan Mishchenko committed
209 210 211 212 213 214 215
        fprintf( pFile, ")" );
        (*pPos)++;
    }
    else
    {
        fprintf( pFile, "(" );
        (*pPos)++;
216 217
        Dec_GraphPrint_rec( pFile, pGraph, pNode0, pNode->eEdge0.fCompl, pNamesIn, pPos, LitSizeMax );
        Dec_GraphPrint_rec( pFile, pGraph, pNode1, pNode->eEdge1.fCompl, pNamesIn, pPos, LitSizeMax );
Alan Mishchenko committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
        fprintf( pFile, ")" );
        (*pPos)++;
    }
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
234
int Dec_GraphPrintGetLeafName( FILE * pFile, int iLeaf, int fCompl, char * pNamesIn[] )
Alan Mishchenko committed
235 236
{
    static char Buffer[100];
237
    sprintf( Buffer, "%s%s", fCompl? "!" : "", pNamesIn[iLeaf] );
Alan Mishchenko committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    fprintf( pFile, "%s", Buffer );
    return strlen( Buffer );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
253
void Dec_GraphPrintUpdatePos( FILE * pFile, int * pPos, int LitSizeMax )
Alan Mishchenko committed
254 255 256 257 258 259 260 261 262 263 264 265
{
    int i;
    if ( *pPos + LitSizeMax < 77 )
        return;
    fprintf( pFile, "\n" );
    for ( i = 0; i < 10; i++ )
        fprintf( pFile, " " );
    *pPos = 10;
}

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

Alan Mishchenko committed
266
  Synopsis    [Starts the printout for a decomposition graph.]
Alan Mishchenko committed
267 268 269 270 271 272 273 274

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
275
int Dec_GraphPrintOutputName( FILE * pFile, char * pNameOut )
Alan Mishchenko committed
276 277 278
{
    if ( pNameOut == NULL )
        return 0;
279
    fprintf( pFile, "%6s = ", pNameOut );
Alan Mishchenko committed
280 281 282 283 284 285 286 287
    return 10;
}

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


288 289
ABC_NAMESPACE_IMPL_END