1 /**CFile****************************************************************
2 
3   FileName    [decPrint.c]
4 
5   PackageName [MVSIS 2.0: Multi-valued logic synthesis system.]
6 
7   Synopsis    [Procedures to print the decomposition graphs (factored forms).]
8 
9   Author      [MVSIS Group]
10 
11   Affiliation [UC Berkeley]
12 
13   Date        [Ver. 1.0. Started - February 1, 2003.]
14 
15   Revision    [$Id: decPrint.c,v 1.1 2003/05/22 19:20:05 alanmi Exp $]
16 
17 ***********************************************************************/
18 
19 #include "base/abc/abc.h"
20 #include "dec.h"
21 
22 ABC_NAMESPACE_IMPL_START
23 
24 
25 ////////////////////////////////////////////////////////////////////////
26 ///                        DECLARATIONS                              ///
27 ////////////////////////////////////////////////////////////////////////
28 
29 static void   Dec_GraphPrint_rec( FILE * pFile, Dec_Graph_t * pGraph, Dec_Node_t * pNode, int fCompl, char * pNamesIn[], int * pPos, int LitSizeMax );
30 static int    Dec_GraphPrintGetLeafName( FILE * pFile, int iLeaf, int fCompl, char * pNamesIn[] );
31 static void   Dec_GraphPrintUpdatePos( FILE * pFile, int * pPos, int LitSizeMax );
32 static int    Dec_GraphPrintOutputName( FILE * pFile, char * pNameOut );
33 
34 ////////////////////////////////////////////////////////////////////////
35 ///                     FUNCTION DEFINITIONS                         ///
36 ////////////////////////////////////////////////////////////////////////
37 
38 /**Function*************************************************************
39 
40   Synopsis    [Prints the decomposition graph.]
41 
42   Description []
43 
44   SideEffects []
45 
46   SeeAlso     []
47 
48 ***********************************************************************/
Dec_GraphPrint(FILE * pFile,Dec_Graph_t * pGraph,char * pNamesIn[],char * pNameOut)49 void Dec_GraphPrint( FILE * pFile, Dec_Graph_t * pGraph, char * pNamesIn[], char * pNameOut )
50 {
51     Vec_Ptr_t * vNamesIn = NULL;
52     int LitSizeMax, LitSizeCur, Pos, i;
53 
54     // create the names if not given by the user
55     if ( pNamesIn == NULL )
56     {
57         vNamesIn = Abc_NodeGetFakeNames( Dec_GraphLeaveNum(pGraph) );
58         pNamesIn = (char **)vNamesIn->pArray;
59     }
60     if ( pNameOut == NULL )
61         pNameOut = "F";
62 
63     // get the size of the longest literal
64     LitSizeMax = 0;
65     for ( i = 0; i < Dec_GraphLeaveNum(pGraph); i++ )
66     {
67         LitSizeCur = strlen(pNamesIn[i]);
68         if ( LitSizeMax < LitSizeCur )
69             LitSizeMax = LitSizeCur;
70     }
71     if ( LitSizeMax > 50 )
72         LitSizeMax = 20;
73 
74     // write the decomposition graph (factored form)
75     if ( Dec_GraphIsConst(pGraph) ) // constant
76     {
77         Pos = Dec_GraphPrintOutputName( pFile, pNameOut );
78         fprintf( pFile, "Constant %d", !Dec_GraphIsComplement(pGraph) );
79     }
80     else if ( Dec_GraphIsVar(pGraph) ) // literal
81     {
82         Pos = Dec_GraphPrintOutputName( pFile, pNameOut );
83         Dec_GraphPrintGetLeafName( pFile, Dec_GraphVarInt(pGraph), Dec_GraphIsComplement(pGraph), pNamesIn );
84     }
85     else
86     {
87         Pos = Dec_GraphPrintOutputName( pFile, pNameOut );
88         Dec_GraphPrint_rec( pFile, pGraph, Dec_GraphNodeLast(pGraph), Dec_GraphIsComplement(pGraph), pNamesIn, &Pos, LitSizeMax );
89     }
90     fprintf( pFile, "\n" );
91 
92     if ( vNamesIn )
93         Abc_NodeFreeNames( vNamesIn );
94 }
95 
96 /**Function*************************************************************
97 
98   Synopsis    []
99 
100   Description []
101 
102   SideEffects []
103 
104   SeeAlso     []
105 
106 ***********************************************************************/
Dec_GraphPrint2_rec(FILE * pFile,Dec_Graph_t * pGraph,Dec_Node_t * pNode,int fCompl,char * pNamesIn[],int * pPos,int LitSizeMax)107 void Dec_GraphPrint2_rec( FILE * pFile, Dec_Graph_t * pGraph, Dec_Node_t * pNode, int fCompl, char * pNamesIn[], int * pPos, int LitSizeMax )
108 {
109     Dec_Node_t * pNode0, * pNode1;
110     pNode0 = Dec_GraphNode(pGraph, pNode->eEdge0.Node);
111     pNode1 = Dec_GraphNode(pGraph, pNode->eEdge1.Node);
112     if ( Dec_GraphNodeIsVar(pGraph, pNode) ) // FT_NODE_LEAF )
113     {
114         (*pPos) += Dec_GraphPrintGetLeafName( pFile, Dec_GraphNodeInt(pGraph,pNode), fCompl, pNamesIn );
115         return;
116     }
117     if ( !pNode->fNodeOr ) // FT_NODE_AND )
118     {
119         if ( !pNode0->fNodeOr ) // != FT_NODE_OR )
120             Dec_GraphPrint_rec( pFile, pGraph, pNode0, pNode->fCompl0, pNamesIn, pPos, LitSizeMax );
121         else
122         {
123             fprintf( pFile, "(" );
124             (*pPos)++;
125             Dec_GraphPrint_rec( pFile, pGraph, pNode0, pNode->fCompl0, pNamesIn, pPos, LitSizeMax );
126             fprintf( pFile, ")" );
127             (*pPos)++;
128         }
129         fprintf( pFile, " " );
130         (*pPos)++;
131 
132         Dec_GraphPrintUpdatePos( pFile, pPos, LitSizeMax );
133 
134         if ( !pNode1->fNodeOr ) // != FT_NODE_OR )
135             Dec_GraphPrint_rec( pFile, pGraph, pNode1, pNode->fCompl1, pNamesIn, pPos, LitSizeMax );
136         else
137         {
138             fprintf( pFile, "(" );
139             (*pPos)++;
140             Dec_GraphPrint_rec( pFile, pGraph, pNode1, pNode->fCompl1, pNamesIn, pPos, LitSizeMax );
141             fprintf( pFile, ")" );
142             (*pPos)++;
143         }
144         return;
145     }
146     if ( pNode->fNodeOr ) // FT_NODE_OR )
147     {
148         Dec_GraphPrint_rec( pFile, pGraph, pNode0, pNode->fCompl0, pNamesIn, pPos, LitSizeMax );
149         fprintf( pFile, " + " );
150         (*pPos) += 3;
151 
152         Dec_GraphPrintUpdatePos( pFile, pPos, LitSizeMax );
153 
154         Dec_GraphPrint_rec( pFile, pGraph, pNode1, pNode->fCompl1, pNamesIn, pPos, LitSizeMax );
155         return;
156     }
157     assert( 0 );
158 }
159 
160 /**Function*************************************************************
161 
162   Synopsis    []
163 
164   Description []
165 
166   SideEffects []
167 
168   SeeAlso     []
169 
170 ***********************************************************************/
Dec_GraphPrint_rec(FILE * pFile,Dec_Graph_t * pGraph,Dec_Node_t * pNode,int fCompl,char * pNamesIn[],int * pPos,int LitSizeMax)171 void Dec_GraphPrint_rec( FILE * pFile, Dec_Graph_t * pGraph, Dec_Node_t * pNode, int fCompl, char * pNamesIn[], int * pPos, int LitSizeMax )
172 {
173     Dec_Node_t * pNode0, * pNode1;
174     Dec_Node_t * pNode00, * pNode01, * pNode10, * pNode11;
175     pNode0 = Dec_GraphNode(pGraph, pNode->eEdge0.Node);
176     pNode1 = Dec_GraphNode(pGraph, pNode->eEdge1.Node);
177     if ( Dec_GraphNodeIsVar(pGraph, pNode) ) // FT_NODE_LEAF )
178     {
179         (*pPos) += Dec_GraphPrintGetLeafName( pFile, Dec_GraphNodeInt(pGraph,pNode), fCompl, pNamesIn );
180         return;
181     }
182     if ( !Dec_GraphNodeIsVar(pGraph, pNode0) && !Dec_GraphNodeIsVar(pGraph, pNode1) )
183     {
184         pNode00 = Dec_GraphNode(pGraph, pNode0->eEdge0.Node);
185         pNode01 = Dec_GraphNode(pGraph, pNode0->eEdge1.Node);
186         pNode10 = Dec_GraphNode(pGraph, pNode1->eEdge0.Node);
187         pNode11 = Dec_GraphNode(pGraph, pNode1->eEdge1.Node);
188         if ( (pNode00 == pNode10 || pNode00 == pNode11) && (pNode01 == pNode10 || pNode01 == pNode11) )
189         {
190             fprintf( pFile, "(" );
191             (*pPos)++;
192             Dec_GraphPrint_rec( pFile, pGraph, pNode00, pNode00->fCompl0, pNamesIn, pPos, LitSizeMax );
193             fprintf( pFile, " # " );
194             (*pPos) += 3;
195             Dec_GraphPrint_rec( pFile, pGraph, pNode01, pNode01->fCompl1, pNamesIn, pPos, LitSizeMax );
196             fprintf( pFile, ")" );
197             (*pPos)++;
198             return;
199         }
200     }
201     if ( fCompl )
202     {
203         fprintf( pFile, "(" );
204         (*pPos)++;
205         Dec_GraphPrint_rec( pFile, pGraph, pNode0, !pNode->eEdge0.fCompl, pNamesIn, pPos, LitSizeMax );
206         fprintf( pFile, " + " );
207         (*pPos) += 3;
208         Dec_GraphPrint_rec( pFile, pGraph, pNode1, !pNode->eEdge1.fCompl, pNamesIn, pPos, LitSizeMax );
209         fprintf( pFile, ")" );
210         (*pPos)++;
211     }
212     else
213     {
214         fprintf( pFile, "(" );
215         (*pPos)++;
216         Dec_GraphPrint_rec( pFile, pGraph, pNode0, pNode->eEdge0.fCompl, pNamesIn, pPos, LitSizeMax );
217         Dec_GraphPrint_rec( pFile, pGraph, pNode1, pNode->eEdge1.fCompl, pNamesIn, pPos, LitSizeMax );
218         fprintf( pFile, ")" );
219         (*pPos)++;
220     }
221 }
222 
223 /**Function*************************************************************
224 
225   Synopsis    []
226 
227   Description []
228 
229   SideEffects []
230 
231   SeeAlso     []
232 
233 ***********************************************************************/
Dec_GraphPrintGetLeafName(FILE * pFile,int iLeaf,int fCompl,char * pNamesIn[])234 int Dec_GraphPrintGetLeafName( FILE * pFile, int iLeaf, int fCompl, char * pNamesIn[] )
235 {
236     static char Buffer[100];
237     sprintf( Buffer, "%s%s", fCompl? "!" : "", pNamesIn[iLeaf] );
238     fprintf( pFile, "%s", Buffer );
239     return strlen( Buffer );
240 }
241 
242 /**Function*************************************************************
243 
244   Synopsis    []
245 
246   Description []
247 
248   SideEffects []
249 
250   SeeAlso     []
251 
252 ***********************************************************************/
Dec_GraphPrintUpdatePos(FILE * pFile,int * pPos,int LitSizeMax)253 void Dec_GraphPrintUpdatePos( FILE * pFile, int * pPos, int LitSizeMax )
254 {
255     int i;
256     if ( *pPos + LitSizeMax < 77 )
257         return;
258     fprintf( pFile, "\n" );
259     for ( i = 0; i < 10; i++ )
260         fprintf( pFile, " " );
261     *pPos = 10;
262 }
263 
264 /**Function*************************************************************
265 
266   Synopsis    [Starts the printout for a decomposition graph.]
267 
268   Description []
269 
270   SideEffects []
271 
272   SeeAlso     []
273 
274 ***********************************************************************/
Dec_GraphPrintOutputName(FILE * pFile,char * pNameOut)275 int Dec_GraphPrintOutputName( FILE * pFile, char * pNameOut )
276 {
277     if ( pNameOut == NULL )
278         return 0;
279     fprintf( pFile, "%6s = ", pNameOut );
280     return 10;
281 }
282 
283 ////////////////////////////////////////////////////////////////////////
284 ///                       END OF FILE                                ///
285 ////////////////////////////////////////////////////////////////////////
286 
287 
288 ABC_NAMESPACE_IMPL_END
289 
290