abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
amapLib.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [amapLib.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Technology mapper for standard cells.]
8 
9  Synopsis [Standard-cell library.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: amapLib.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "amapInt.h"
22 
24 
25 
26 ////////////////////////////////////////////////////////////////////////
27 /// DECLARATIONS ///
28 ////////////////////////////////////////////////////////////////////////
29 
30 ////////////////////////////////////////////////////////////////////////
31 /// FUNCTION DEFINITIONS ///
32 ////////////////////////////////////////////////////////////////////////
33 
34 /**Function*************************************************************
35 
36  Synopsis [Allocs a library.]
37 
38  Description []
39 
40  SideEffects []
41 
42  SeeAlso []
43 
44 ***********************************************************************/
46 {
47  Amap_Lib_t * p;
48  p = (Amap_Lib_t *)ABC_ALLOC( Amap_Lib_t, 1 );
49  memset( p, 0, sizeof(Amap_Lib_t) );
50  p->vGates = Vec_PtrAlloc( 100 );
51  p->pMemGates = Aig_MmFlexStart();
52  p->pMemSet = Aig_MmFlexStart();
53  return p;
54 }
55 
56 /**Function*************************************************************
57 
58  Synopsis [Deallocs a library.]
59 
60  Description []
61 
62  SideEffects []
63 
64  SeeAlso []
65 
66 ***********************************************************************/
68 {
69  if ( p == NULL )
70  return;
71  if ( p->vSelect )
72  Vec_PtrFree( p->vSelect );
73  if ( p->vSorted )
74  Vec_PtrFree( p->vSorted );
75  if ( p->vGates )
76  Vec_PtrFree( p->vGates );
77  if ( p->vRules )
78  Vec_VecFree( (Vec_Vec_t *)p->vRules );
79  if ( p->vRulesX )
80  Vec_VecFree( (Vec_Vec_t *)p->vRulesX );
81  if ( p->vRules3 )
82  Vec_IntFree( p->vRules3 );
83  Aig_MmFlexStop( p->pMemGates, 0 );
84  Aig_MmFlexStop( p->pMemSet, 0 );
85  ABC_FREE( p->pRules );
86  ABC_FREE( p->pRulesX );
87  ABC_FREE( p->pNodes );
88  ABC_FREE( p->pName );
89  ABC_FREE( p );
90 }
91 
92 /**Function*************************************************************
93 
94  Synopsis [Returns the largest gate size.]
95 
96  Description []
97 
98  SideEffects []
99 
100  SeeAlso []
101 
102 ***********************************************************************/
104 {
105  Amap_Gat_t * pGate;
106  int i, Counter = 0;
107  Amap_LibForEachGate( p, pGate, i )
108  if ( Counter < (int)pGate->nPins )
109  Counter = pGate->nPins;
110  return Counter;
111 }
112 
113 /**Function*************************************************************
114 
115  Synopsis [Writes one pin.]
116 
117  Description []
118 
119  SideEffects []
120 
121  SeeAlso []
122 
123 ***********************************************************************/
124 void Amap_LibWritePin( FILE * pFile, Amap_Pin_t * pPin )
125 {
126  char * pPhaseNames[10] = { "UNKNOWN", "INV", "NONINV" };
127  fprintf( pFile, " PIN " );
128  fprintf( pFile, "%9s ", pPin->pName );
129  fprintf( pFile, "%10s ", pPhaseNames[pPin->Phase] );
130  fprintf( pFile, "%6d ", (int)pPin->dLoadInput );
131  fprintf( pFile, "%6d ", (int)pPin->dLoadMax );
132  fprintf( pFile, "%6.2f ", pPin->dDelayBlockRise );
133  fprintf( pFile, "%6.2f ", pPin->dDelayFanoutRise );
134  fprintf( pFile, "%6.2f ", pPin->dDelayBlockFall );
135  fprintf( pFile, "%6.2f", pPin->dDelayFanoutFall );
136  fprintf( pFile, "\n" );
137 }
138 
139 /**Function*************************************************************
140 
141  Synopsis [Writes one gate.]
142 
143  Description []
144 
145  SideEffects []
146 
147  SeeAlso []
148 
149 ***********************************************************************/
150 void Amap_LibWriteGate( FILE * pFile, Amap_Gat_t * pGate, int fPrintDsd )
151 {
152  Amap_Pin_t * pPin;
153  fprintf( pFile, "GATE " );
154  fprintf( pFile, "%12s ", pGate->pName );
155  fprintf( pFile, "%10.2f ", pGate->dArea );
156  fprintf( pFile, "%s=%s;\n", pGate->pOutName, pGate->pForm );
157  if ( fPrintDsd )
158  {
159  if ( pGate->pFunc == NULL )
160  printf( "Truth table is not available.\n" );
161  else
162  Kit_DsdPrintFromTruth( pGate->pFunc, pGate->nPins );
163  }
164  Amap_GateForEachPin( pGate, pPin )
165  Amap_LibWritePin( pFile, pPin );
166 }
167 
168 /**Function*************************************************************
169 
170  Synopsis [Writes library.]
171 
172  Description []
173 
174  SideEffects []
175 
176  SeeAlso []
177 
178 ***********************************************************************/
179 void Amap_LibWrite( FILE * pFile, Amap_Lib_t * pLib, int fPrintDsd )
180 {
181  Amap_Gat_t * pGate;
182  int i;
183  fprintf( pFile, "# The genlib library \"%s\".\n", pLib->pName );
184  Amap_LibForEachGate( pLib, pGate, i )
185  Amap_LibWriteGate( pFile, pGate, fPrintDsd );
186 }
187 
188 /**Function*************************************************************
189 
190  Synopsis [Compares two gates by area.]
191 
192  Description []
193 
194  SideEffects []
195 
196  SeeAlso []
197 
198 ***********************************************************************/
200 {
201  double Diff = (*pp1)->dArea - (*pp2)->dArea;
202  if ( Diff < 0.0 )
203  return -1;
204  if ( Diff > 0.0 )
205  return 1;
206  return 0;
207 }
208 
209 /**Function*************************************************************
210 
211  Synopsis [Compares gates by area.]
212 
213  Description []
214 
215  SideEffects []
216 
217  SeeAlso []
218 
219 ***********************************************************************/
221 {
222  Vec_Ptr_t * vSorted;
223  vSorted = Vec_PtrDup( pLib->vGates );
224  qsort( (void *)Vec_PtrArray(vSorted), Vec_PtrSize(vSorted), sizeof(void *),
225  (int (*)(const void *, const void *)) Amap_LibCompareGatesByArea );
226  return vSorted;
227 }
228 
229 /**Function*************************************************************
230 
231  Synopsis [Finds min-area gate with the given function.]
232 
233  Description []
234 
235  SideEffects []
236 
237  SeeAlso []
238 
239 ***********************************************************************/
240 Amap_Gat_t * Amap_LibFindGate( Amap_Lib_t * p, unsigned uTruth )
241 {
242  Amap_Gat_t * pGate;
243  int i;
244  Vec_PtrForEachEntry( Amap_Gat_t *, p->vSorted, pGate, i )
245  if ( pGate->nPins <= 5 && pGate->pFunc[0] == uTruth )
246  return pGate;
247  return NULL;
248 }
249 
250 /**Function*************************************************************
251 
252  Synopsis [Selects gates useful for area-only mapping.]
253 
254  Description []
255 
256  SideEffects []
257 
258  SeeAlso []
259 
260 ***********************************************************************/
262 {
263  Vec_Ptr_t * vSelect;
264  Amap_Gat_t * pGate, * pGate2;
265  int i, k;//, clk = Abc_Clock();
266  p->pGate0 = Amap_LibFindGate( p, 0 );
267  p->pGate1 = Amap_LibFindGate( p, ~0 );
268  p->pGateBuf = Amap_LibFindGate( p, 0xAAAAAAAA );
269  p->pGateInv = Amap_LibFindGate( p, ~0xAAAAAAAA );
270  vSelect = Vec_PtrAlloc( 100 );
271  Vec_PtrForEachEntry( Amap_Gat_t *, p->vSorted, pGate, i )
272  {
273  if ( pGate->pFunc == NULL || pGate->pTwin != NULL )
274  continue;
275  Vec_PtrForEachEntryStop( Amap_Gat_t *, p->vSorted, pGate2, k, i )
276  {
277  if ( pGate2->pFunc == NULL || pGate2->pTwin != NULL )
278  continue;
279  if ( pGate2->nPins != pGate->nPins )
280  continue;
281  if ( !memcmp( pGate2->pFunc, pGate->pFunc, sizeof(unsigned) * Abc_TruthWordNum(pGate->nPins) ) )
282  break;
283  }
284  if ( k < i )
285  continue;
286  Vec_PtrPush( vSelect, pGate );
287  }
288  return vSelect;
289 }
290 
291 /**Function*************************************************************
292 
293  Synopsis [Selects gates useful for area-only mapping.]
294 
295  Description []
296 
297  SideEffects []
298 
299  SeeAlso []
300 
301 ***********************************************************************/
302 void Amap_LibPrintSelectedGates( Amap_Lib_t * p, int fAllGates )
303 {
304  Vec_Ptr_t * vArray;
305  Amap_Gat_t * pGate;
306  int i;
307  vArray = fAllGates? p->vGates : p->vSelect;
308  Vec_PtrForEachEntry( Amap_Gat_t *, vArray, pGate, i )
309  {
310  printf( "%3d :%12s %d %9.2f ", i, pGate->pName, pGate->nPins, pGate->dArea );
311  printf( "%4s=%40s ", pGate->pOutName, pGate->pForm );
312  printf( "DSD: " );
313  Kit_DsdPrintFromTruth( pGate->pFunc, pGate->nPins );
314  printf( "\n" );
315  }
316 }
317 
318 /**Function*************************************************************
319 
320  Synopsis [Parses equations for the gates.]
321 
322  Description []
323 
324  SideEffects []
325 
326  SeeAlso []
327 
328 ***********************************************************************/
329 Amap_Lib_t * Amap_LibReadAndPrepare( char * pFileName, char * pBuffer, int fVerbose, int fVeryVerbose )
330 {
331  Amap_Lib_t * p;
332  abctime clk = Abc_Clock();
333  if ( pBuffer == NULL )
334  p = Amap_LibReadFile( pFileName, fVerbose );
335  else
336  {
337  p = Amap_LibReadBuffer( pBuffer, fVerbose );
338  if ( p )
339  p->pName = Abc_UtilStrsav( pFileName );
340  }
341  if ( fVerbose )
342  printf( "Read %d gates from file \"%s\".\n", Vec_PtrSize(p->vGates), pFileName );
343  if ( p == NULL )
344  return NULL;
345  if ( !Amap_LibParseEquations( p, fVerbose ) )
346  {
347  Amap_LibFree( p );
348  return NULL;
349  }
350  p->vSorted = Amap_LibSortGatesByArea( p );
351  p->vSelect = Amap_LibSelectGates( p, fVerbose );
352  if ( fVerbose )
353  {
354  printf( "Selected %d functionally unique gates. ", Vec_PtrSize(p->vSelect) );
355  ABC_PRT( "Time", Abc_Clock() - clk );
356 // Amap_LibPrintSelectedGates( p, 0 );
357  }
358  clk = Abc_Clock();
359  Amap_LibCreateRules( p, fVeryVerbose );
360  if ( fVerbose )
361  {
362  printf( "Created %d rules and %d matches. ", p->nNodes, p->nSets );
363  ABC_PRT( "Time", Abc_Clock() - clk );
364  }
365  return p;
366 }
367 
368 ////////////////////////////////////////////////////////////////////////
369 /// END OF FILE ///
370 ////////////////////////////////////////////////////////////////////////
371 
372 
374 
char * memset()
Amap_Lib_t * Amap_LibReadFile(char *pFileName, int fVerbose)
Definition: amapRead.c:473
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
Definition: vecPtr.h:42
#define Amap_LibForEachGate(pLib, pGate, i)
Definition: amapInt.h:293
double dLoadInput
Definition: amapInt.h:142
char * pForm
Definition: amapInt.h:157
typedefABC_NAMESPACE_HEADER_START struct Vec_Vec_t_ Vec_Vec_t
INCLUDES ///.
Definition: vecVec.h:42
#define Amap_GateForEachPin(pGate, pPin)
Definition: amapInt.h:296
Amap_Gat_t * Amap_LibFindGate(Amap_Lib_t *p, unsigned uTruth)
Definition: amapLib.c:240
static Llb_Mgr_t * p
Definition: llb3Image.c:950
Amap_Lib_t * Amap_LibReadAndPrepare(char *pFileName, char *pBuffer, int fVerbose, int fVeryVerbose)
Definition: amapLib.c:329
int Phase
Definition: amapInt.h:141
void Amap_LibFree(Amap_Lib_t *p)
Definition: amapLib.c:67
static void Vec_PtrPush(Vec_Ptr_t *p, void *Entry)
Definition: vecPtr.h:606
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
char * pOutName
Definition: amapInt.h:155
double dDelayBlockRise
Definition: amapInt.h:144
static Vec_Ptr_t * Vec_PtrDup(Vec_Ptr_t *pVec)
Definition: vecPtr.h:169
static int Abc_TruthWordNum(int nVars)
Definition: abc_global.h:256
double dLoadMax
Definition: amapInt.h:143
void Kit_DsdPrintFromTruth(unsigned *pTruth, int nVars)
Definition: kitDsd.c:490
static abctime Abc_Clock()
Definition: abc_global.h:279
static int Vec_PtrSize(Vec_Ptr_t *p)
Definition: vecPtr.h:295
static void Vec_VecFree(Vec_Vec_t *p)
Definition: vecVec.h:347
int Amap_LibCompareGatesByArea(Amap_Gat_t **pp1, Amap_Gat_t **pp2)
Definition: amapLib.c:199
void Amap_LibPrintSelectedGates(Amap_Lib_t *p, int fAllGates)
Definition: amapLib.c:302
Amap_Gat_t * pTwin
Definition: amapInt.h:153
Vec_Ptr_t * Amap_LibSortGatesByArea(Amap_Lib_t *pLib)
Definition: amapLib.c:220
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
int memcmp()
unsigned * pFunc
Definition: amapInt.h:158
static int Counter
unsigned nPins
Definition: amapInt.h:161
double dDelayFanoutFall
Definition: amapInt.h:147
char * pName
Definition: amapInt.h:140
double dArea
Definition: amapInt.h:156
void Amap_LibWritePin(FILE *pFile, Amap_Pin_t *pPin)
Definition: amapLib.c:124
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
#define Vec_PtrForEachEntryStop(Type, vVec, pEntry, i, Stop)
Definition: vecPtr.h:59
int Amap_LibNumPinsMax(Amap_Lib_t *p)
Definition: amapLib.c:103
typedefABC_NAMESPACE_HEADER_START struct Amap_Lib_t_ Amap_Lib_t
INCLUDES ///.
Definition: amap.h:42
static Vec_Ptr_t * Vec_PtrAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: vecPtr.h:83
Vec_Ptr_t * Amap_LibSelectGates(Amap_Lib_t *p, int fVerbose)
Definition: amapLib.c:261
Aig_MmFlex_t * Aig_MmFlexStart()
Definition: aigMem.c:305
#define ABC_FREE(obj)
Definition: abc_global.h:232
#define ABC_PRT(a, t)
Definition: abc_global.h:220
void Aig_MmFlexStop(Aig_MmFlex_t *p, int fVerbose)
Definition: aigMem.c:337
double dDelayFanoutRise
Definition: amapInt.h:145
double dDelayBlockFall
Definition: amapInt.h:146
Amap_Lib_t * Amap_LibReadBuffer(char *pBuffer, int fVerbose)
Definition: amapRead.c:446
int Amap_LibParseEquations(Amap_Lib_t *p, int fVerbose)
Definition: amapParse.c:392
ABC_NAMESPACE_IMPL_START Amap_Lib_t * Amap_LibAlloc()
DECLARATIONS ///.
Definition: amapLib.c:45
char * pName
Definition: amapInt.h:154
static void Vec_IntFree(Vec_Int_t *p)
Definition: bblif.c:235
#define Vec_PtrForEachEntry(Type, vVec, pEntry, i)
MACRO DEFINITIONS ///.
Definition: vecPtr.h:55
void Amap_LibCreateRules(Amap_Lib_t *p, int fVeryVerbose)
Definition: amapRule.c:426
void Amap_LibWrite(FILE *pFile, Amap_Lib_t *pLib, int fPrintDsd)
Definition: amapLib.c:179
ABC_INT64_T abctime
Definition: abc_global.h:278
char * Abc_UtilStrsav(char *s)
Definition: starter.c:47
static void ** Vec_PtrArray(Vec_Ptr_t *p)
Definition: vecPtr.h:279
static void Vec_PtrFree(Vec_Ptr_t *p)
Definition: vecPtr.h:223
void Amap_LibWriteGate(FILE *pFile, Amap_Gat_t *pGate, int fPrintDsd)
Definition: amapLib.c:150