abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
abcAttach.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [abcAttach.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Network and node package.]
8 
9  Synopsis [Attaches the library gates to the current network.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: abcAttach.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "base/abc/abc.h"
22 #include "base/main/main.h"
23 #include "map/mio/mio.h"
24 
26 
27 
28 ////////////////////////////////////////////////////////////////////////
29 /// DECLARATIONS ///
30 ////////////////////////////////////////////////////////////////////////
31 
32 #define ATTACH_FULL (~((unsigned)0))
33 #define ATTACH_MASK(n) ((~((unsigned)0)) >> (32-(n)))
34 
35 static void Abc_AttachSetupTruthTables( unsigned uTruths[][2] );
36 static void Abc_AttachComputeTruth( char * pSop, unsigned uTruthsIn[][2], unsigned * uTruthNode );
37 static Mio_Gate_t * Abc_AttachFind( Mio_Gate_t ** ppGates, unsigned ** puTruthGates, int nGates, unsigned * uTruthNode, int * Perm );
38 static int Abc_AttachCompare( unsigned ** puTruthGates, int nGates, unsigned * uTruthNode );
39 static int Abc_NodeAttach( Abc_Obj_t * pNode, Mio_Gate_t ** ppGates, unsigned ** puTruthGates, int nGates, unsigned uTruths[][2] );
40 static void Abc_TruthPermute( char * pPerm, int nVars, unsigned * uTruthNode, unsigned * uTruthPerm );
41 
42 static char ** s_pPerms = NULL;
43 static int s_nPerms;
44 
45 ////////////////////////////////////////////////////////////////////////
46 /// FUNCTION DEFINITIONS ///
47 ////////////////////////////////////////////////////////////////////////
48 
49 /**Function*************************************************************
50 
51  Synopsis [Attaches gates from the current library to the internal nodes.]
52 
53  Description []
54 
55  SideEffects []
56 
57  SeeAlso []
58 
59 ***********************************************************************/
60 int Abc_NtkAttach( Abc_Ntk_t * pNtk )
61 {
62  Mio_Library_t * pGenlib;
63  unsigned ** puTruthGates;
64  unsigned uTruths[6][2];
65  Abc_Obj_t * pNode;
66  Mio_Gate_t ** ppGates;
67  int nGates, nFanins, i;
68 
69  assert( Abc_NtkIsSopLogic(pNtk) );
70 
71  // check that the library is available
72  pGenlib = (Mio_Library_t *)Abc_FrameReadLibGen();
73  if ( pGenlib == NULL )
74  {
75  printf( "The current library is not available.\n" );
76  return 0;
77  }
78 
79  // start the truth tables
80  Abc_AttachSetupTruthTables( uTruths );
81 
82  // collect all the gates
83  ppGates = Mio_CollectRoots( pGenlib, 6, (float)1.0e+20, 1, &nGates, 0 );
84 
85  // derive the gate truth tables
86  puTruthGates = ABC_ALLOC( unsigned *, nGates );
87  puTruthGates[0] = ABC_ALLOC( unsigned, 2 * nGates );
88  for ( i = 1; i < nGates; i++ )
89  puTruthGates[i] = puTruthGates[i-1] + 2;
90  for ( i = 0; i < nGates; i++ )
91  Mio_DeriveTruthTable( ppGates[i], uTruths, Mio_GateReadPinNum(ppGates[i]), 6, puTruthGates[i] );
92 
93  // assign the gates to pNode->pCopy
94  Abc_NtkCleanCopy( pNtk );
95  Abc_NtkForEachNode( pNtk, pNode, i )
96  {
97  nFanins = Abc_ObjFaninNum(pNode);
98  if ( nFanins == 0 )
99  {
100  if ( Abc_SopIsConst1((char *)pNode->pData) )
101  pNode->pCopy = (Abc_Obj_t *)Mio_LibraryReadConst1(pGenlib);
102  else
103  pNode->pCopy = (Abc_Obj_t *)Mio_LibraryReadConst0(pGenlib);
104  }
105  else if ( nFanins == 1 )
106  {
107  if ( Abc_SopIsBuf((char *)pNode->pData) )
108  pNode->pCopy = (Abc_Obj_t *)Mio_LibraryReadBuf(pGenlib);
109  else
110  pNode->pCopy = (Abc_Obj_t *)Mio_LibraryReadInv(pGenlib);
111  }
112  else if ( nFanins > 6 )
113  {
114  printf( "Cannot attach gate with more than 6 inputs to node %s.\n", Abc_ObjName(pNode) );
115  ABC_FREE( puTruthGates[0] );
116  ABC_FREE( puTruthGates );
117  ABC_FREE( ppGates );
118  return 0;
119  }
120  else if ( !Abc_NodeAttach( pNode, ppGates, puTruthGates, nGates, uTruths ) )
121  {
122  printf( "Could not attach the library gate to node %s.\n", Abc_ObjName(pNode) );
123  ABC_FREE( puTruthGates[0] );
124  ABC_FREE( puTruthGates );
125  ABC_FREE( ppGates );
126  return 0;
127  }
128  }
129  ABC_FREE( puTruthGates[0] );
130  ABC_FREE( puTruthGates );
131  ABC_FREE( ppGates );
132  ABC_FREE( s_pPerms );
133 
134  // perform the final transformation
135  Abc_NtkForEachNode( pNtk, pNode, i )
136  {
137  if ( pNode->pCopy == NULL )
138  {
139  printf( "Some elementary gates (constant, buffer, or inverter) are missing in the library.\n" );
140  return 0;
141  }
142  }
143 
144  // replace SOP representation by the gate representation
145  Abc_NtkForEachNode( pNtk, pNode, i )
146  pNode->pData = pNode->pCopy, pNode->pCopy = NULL;
147  pNtk->ntkFunc = ABC_FUNC_MAP;
149  pNtk->pManFunc = pGenlib;
150 
151  printf( "Library gates are successfully attached to the nodes.\n" );
152 
153  // make sure that everything is okay
154  if ( !Abc_NtkCheck( pNtk ) )
155  {
156  printf( "Abc_NtkAttach: The network check has failed.\n" );
157  return 0;
158  }
159  return 1;
160 }
161 
162 /**Function*************************************************************
163 
164  Synopsis []
165 
166  Description []
167 
168  SideEffects []
169 
170  SeeAlso []
171 
172 ***********************************************************************/
173 int Abc_NodeAttach( Abc_Obj_t * pNode, Mio_Gate_t ** ppGates, unsigned ** puTruthGates, int nGates, unsigned uTruths[][2] )
174 {
175  int Perm[10];
176  int pTempInts[10];
177  unsigned uTruthNode[2];
178  Abc_Obj_t * pFanin;
179  Mio_Gate_t * pGate;
180  int nFanins, i;
181 
182  // compute the node's truth table
183  Abc_AttachComputeTruth( (char *)pNode->pData, uTruths, uTruthNode );
184  // find the matching gate and permutation
185  pGate = Abc_AttachFind( ppGates, puTruthGates, nGates, uTruthNode, Perm );
186  if ( pGate == NULL )
187  return 0;
188  // permute the fanins
189  nFanins = Abc_ObjFaninNum(pNode);
190  Abc_ObjForEachFanin( pNode, pFanin, i )
191  pTempInts[i] = pFanin->Id;
192  for ( i = 0; i < nFanins; i++ )
193  pNode->vFanins.pArray[Perm[i]] = pTempInts[i];
194  // set the gate
195  pNode->pCopy = (Abc_Obj_t *)pGate;
196  return 1;
197 }
198 
199 /**Function*************************************************************
200 
201  Synopsis [Sets up the truth tables.]
202 
203  Description []
204 
205  SideEffects []
206 
207  SeeAlso []
208 
209 ***********************************************************************/
210 void Abc_AttachSetupTruthTables( unsigned uTruths[][2] )
211 {
212  int m, v;
213  for ( v = 0; v < 5; v++ )
214  uTruths[v][0] = 0;
215  // set up the truth tables
216  for ( m = 0; m < 32; m++ )
217  for ( v = 0; v < 5; v++ )
218  if ( m & (1 << v) )
219  uTruths[v][0] |= (1 << m);
220  // make adjustments for the case of 6 variables
221  for ( v = 0; v < 5; v++ )
222  uTruths[v][1] = uTruths[v][0];
223  uTruths[5][0] = 0;
224  uTruths[5][1] = ATTACH_FULL;
225 }
226 
227 /**Function*************************************************************
228 
229  Synopsis [Compute the truth table of the node's cover.]
230 
231  Description []
232 
233  SideEffects []
234 
235  SeeAlso []
236 
237 ***********************************************************************/
238 void Abc_AttachComputeTruth( char * pSop, unsigned uTruthsIn[][2], unsigned * uTruthRes )
239 {
240 // Mvc_Cube_t * pCube;
241  unsigned uSignCube[2];
242  int Value;
243 // int nInputs = pCover->nBits/2;
244  int nInputs = 6;
245  int nFanins = Abc_SopGetVarNum(pSop);
246  char * pCube;
247  int k;
248 
249  // make sure that the number of input truth tables in equal to the number of gate inputs
250  assert( nInputs < 7 );
251 
252  // clean the resulting truth table
253  uTruthRes[0] = 0;
254  uTruthRes[1] = 0;
255  if ( nInputs < 6 )
256  {
257  // consider the case when only one unsigned can be used
258 // Mvc_CoverForEachCube( pCover, pCube )
259  Abc_SopForEachCube( pSop, nFanins, pCube )
260  {
261  uSignCube[0] = ATTACH_FULL;
262 // Mvc_CubeForEachVarValue( pCover, pCube, Var, Value )
263  Abc_CubeForEachVar( pCube, Value, k )
264  {
265  if ( Value == '0' )
266  uSignCube[0] &= ~uTruthsIn[k][0];
267  else if ( Value == '1' )
268  uSignCube[0] &= uTruthsIn[k][0];
269  }
270  uTruthRes[0] |= uSignCube[0];
271  }
272  if ( Abc_SopGetPhase(pSop) == 0 )
273  uTruthRes[0] = ~uTruthRes[0];
274  if ( nInputs < 5 )
275  uTruthRes[0] &= ATTACH_MASK(1<<nInputs);
276  }
277  else
278  {
279  // consider the case when two unsigneds should be used
280 // Mvc_CoverForEachCube( pCover, pCube )
281  Abc_SopForEachCube( pSop, nFanins, pCube )
282  {
283  uSignCube[0] = ATTACH_FULL;
284  uSignCube[1] = ATTACH_FULL;
285 // Mvc_CubeForEachVarValue( pCover, pCube, Var, Value )
286  Abc_CubeForEachVar( pCube, Value, k )
287  {
288  if ( Value == '0' )
289  {
290  uSignCube[0] &= ~uTruthsIn[k][0];
291  uSignCube[1] &= ~uTruthsIn[k][1];
292  }
293  else if ( Value == '1' )
294  {
295  uSignCube[0] &= uTruthsIn[k][0];
296  uSignCube[1] &= uTruthsIn[k][1];
297  }
298  }
299  uTruthRes[0] |= uSignCube[0];
300  uTruthRes[1] |= uSignCube[1];
301  }
302 
303  // complement if the SOP is complemented
304  if ( Abc_SopGetPhase(pSop) == 0 )
305  {
306  uTruthRes[0] = ~uTruthRes[0];
307  uTruthRes[1] = ~uTruthRes[1];
308  }
309  }
310 }
311 
312 /**Function*************************************************************
313 
314  Synopsis [Find the gate by truth table.]
315 
316  Description []
317 
318  SideEffects []
319 
320  SeeAlso []
321 
322 ***********************************************************************/
323 Mio_Gate_t * Abc_AttachFind( Mio_Gate_t ** ppGates, unsigned ** puTruthGates, int nGates, unsigned * uTruthNode, int * Perm )
324 {
325  unsigned uTruthPerm[2];
326  int i, v, iNum;
327 
328  // try the gates without permutation
329  if ( (iNum = Abc_AttachCompare( puTruthGates, nGates, uTruthNode )) >= 0 )
330  {
331  for ( v = 0; v < 6; v++ )
332  Perm[v] = v;
333  return ppGates[iNum];
334  }
335  // get permutations
336  if ( s_pPerms == NULL )
337  {
339  s_nPerms = Extra_Factorial( 6 );
340  }
341  // try permutations
342  for ( i = 0; i < s_nPerms; i++ )
343  {
344  Abc_TruthPermute( s_pPerms[i], 6, uTruthNode, uTruthPerm );
345  if ( (iNum = Abc_AttachCompare( puTruthGates, nGates, uTruthPerm )) >= 0 )
346  {
347  for ( v = 0; v < 6; v++ )
348  Perm[v] = (int)s_pPerms[i][v];
349  return ppGates[iNum];
350  }
351  }
352  return NULL;
353 }
354 
355 /**Function*************************************************************
356 
357  Synopsis [Find the gate by truth table.]
358 
359  Description []
360 
361  SideEffects []
362 
363  SeeAlso []
364 
365 ***********************************************************************/
366 int Abc_AttachCompare( unsigned ** puTruthGates, int nGates, unsigned * uTruthNode )
367 {
368  int i;
369  for ( i = 0; i < nGates; i++ )
370  if ( puTruthGates[i][0] == uTruthNode[0] && puTruthGates[i][1] == uTruthNode[1] )
371  return i;
372  return -1;
373 }
374 
375 /**Function*************************************************************
376 
377  Synopsis [Permutes the 6-input truth table.]
378 
379  Description []
380 
381  SideEffects []
382 
383  SeeAlso []
384 
385 ***********************************************************************/
386 void Abc_TruthPermute( char * pPerm, int nVars, unsigned * uTruthNode, unsigned * uTruthPerm )
387 {
388  int nMints, iMintPerm, iMint, v;
389  uTruthPerm[0] = uTruthPerm[1] = 0;
390  nMints = (1 << nVars);
391  for ( iMint = 0; iMint < nMints; iMint++ )
392  {
393  if ( (uTruthNode[iMint>>5] & (1 << (iMint&31))) == 0 )
394  continue;
395  iMintPerm = 0;
396  for ( v = 0; v < nVars; v++ )
397  if ( iMint & (1 << v) )
398  iMintPerm |= (1 << pPerm[v]);
399  uTruthPerm[iMintPerm>>5] |= (1 << (iMintPerm&31));
400  }
401 }
402 
403 ////////////////////////////////////////////////////////////////////////
404 /// END OF FILE ///
405 ////////////////////////////////////////////////////////////////////////
406 
407 
409 
Mio_Gate_t * Mio_LibraryReadBuf(Mio_Library_t *pLib)
Definition: mioApi.c:47
ABC_DLL void * Abc_FrameReadLibGen()
Definition: mainFrame.c:56
#define Abc_SopForEachCube(pSop, nFanins, pCube)
Definition: abc.h:531
static int s_nPerms
Definition: abcAttach.c:43
static int Abc_AttachCompare(unsigned **puTruthGates, int nGates, unsigned *uTruthNode)
Definition: abcAttach.c:366
static int Abc_ObjFaninNum(Abc_Obj_t *pObj)
Definition: abc.h:364
#define ATTACH_MASK(n)
Definition: abcAttach.c:33
Vec_Int_t vFanins
Definition: abc.h:143
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
ABC_DLL int Abc_NtkCheck(Abc_Ntk_t *pNtk)
FUNCTION DEFINITIONS ///.
Definition: abcCheck.c:61
static int Abc_NtkIsSopLogic(Abc_Ntk_t *pNtk)
Definition: abc.h:264
#define Abc_CubeForEachVar(pCube, Value, i)
Definition: abc.h:529
static char ** s_pPerms
Definition: abcAttach.c:42
ABC_DLL int Abc_SopIsBuf(char *pSop)
Definition: abcSop.c:708
char ** Extra_Permutations(int n)
Mio_Gate_t * Mio_LibraryReadConst1(Mio_Library_t *pLib)
Definition: mioApi.c:50
int Mio_GateReadPinNum(Mio_Gate_t *pGate)
Definition: mioApi.c:151
void * pManFunc
Definition: abc.h:191
Abc_Obj_t * pCopy
Definition: abc.h:148
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
STRUCTURE DEFINITIONS ///.
Definition: mioInt.h:61
ABC_DLL int Abc_SopIsConst1(char *pSop)
Definition: abcSop.c:692
static Mio_Gate_t * Abc_AttachFind(Mio_Gate_t **ppGates, unsigned **puTruthGates, int nGates, unsigned *uTruthNode, int *Perm)
Definition: abcAttach.c:323
static void Abc_AttachComputeTruth(char *pSop, unsigned uTruthsIn[][2], unsigned *uTruthNode)
Definition: abcAttach.c:238
static int pPerm[13719]
Definition: rwrTemp.c:32
void Mio_DeriveTruthTable(Mio_Gate_t *pGate, unsigned uTruthsIn[][2], int nSigns, int nInputs, unsigned uTruthRes[])
Definition: mioUtils.c:608
static void Abc_AttachSetupTruthTables(unsigned uTruths[][2])
Definition: abcAttach.c:210
#define Abc_NtkForEachNode(pNtk, pNode, i)
Definition: abc.h:461
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
Abc_NtkFunc_t ntkFunc
Definition: abc.h:157
#define Abc_ObjForEachFanin(pObj, pFanin, i)
Definition: abc.h:524
#define ATTACH_FULL
DECLARATIONS ///.
Definition: abcAttach.c:32
#define ABC_FREE(obj)
Definition: abc_global.h:232
int Id
Definition: abc.h:132
ABC_DLL char * Abc_ObjName(Abc_Obj_t *pNode)
DECLARATIONS ///.
Definition: abcNames.c:48
Mio_Gate_t ** Mio_CollectRoots(Mio_Library_t *pLib, int nInputs, float tDelay, int fSkipInv, int *pnGates, int fVerbose)
Definition: mioUtils.c:302
static int Abc_NodeAttach(Abc_Obj_t *pNode, Mio_Gate_t **ppGates, unsigned **puTruthGates, int nGates, unsigned uTruths[][2])
Definition: abcAttach.c:173
int Extra_Factorial(int n)
ABC_DLL int Abc_SopGetVarNum(char *pSop)
Definition: abcSop.c:536
int Abc_NtkAttach(Abc_Ntk_t *pNtk)
FUNCTION DEFINITIONS ///.
Definition: abcAttach.c:60
ABC_DLL void Abc_NtkCleanCopy(Abc_Ntk_t *pNtk)
Definition: abcUtil.c:507
#define assert(ex)
Definition: util_old.h:213
static void Abc_TruthPermute(char *pPerm, int nVars, unsigned *uTruthNode, unsigned *uTruthPerm)
Definition: abcAttach.c:386
void * pData
Definition: abc.h:145
Mio_Gate_t * Mio_LibraryReadConst0(Mio_Library_t *pLib)
Definition: mioApi.c:49
Mio_Gate_t * Mio_LibraryReadInv(Mio_Library_t *pLib)
Definition: mioApi.c:48
ABC_DLL int Abc_SopGetPhase(char *pSop)
Definition: abcSop.c:556
void Extra_MmFlexStop(Extra_MmFlex_t *p)