abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
giaGig.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [giaGig.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Scalable AIG package.]
8 
9  Synopsis [Parser for Gate-Inverter Graph by Niklas Een.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: giaGig.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "gia.h"
22 #include "misc/extra/extra.h"
23 #include "misc/util/utilTruth.h"
24 
26 
27 
28 ////////////////////////////////////////////////////////////////////////
29 /// DECLARATIONS ///
30 ////////////////////////////////////////////////////////////////////////
31 
32 enum {
33  GIG_NONE = 0,
34  GIG_RESET = 1,
35  GIG_PI = 2,
36  GIG_PO = 3,
37  GIG_SEQ = 4,
38  GIG_LUT = 5,
39  GIG_DELAY = 6,
40  GIG_BOX = 7,
41  GIG_SEL = 8,
42  GIG_BAR = 9,
43  GIG_UNUSED = 10
44 };
45 
46 static char * s_GigNames[GIG_UNUSED] =
47 {
48  "NONE", // GIG_NONE = 0
49  "Reset", // GIG_RESET = 1
50  "PI", // GIG_PI = 2
51  "PO", // GIG_PO = 3
52  "Seq", // GIG_SEQ = 4
53  "Lut4", // GIG_LUT = 5
54  "Delay", // GIG_DELAY = 6
55  "Box", // GIG_BOX = 7
56  "Sel", // GIG_SEL = 8
57  "Bar" // GIG_BAR = 9
58 };
59 
60 ////////////////////////////////////////////////////////////////////////
61 /// FUNCTION DEFINITIONS ///
62 ////////////////////////////////////////////////////////////////////////
63 
64 /**Function*************************************************************
65 
66  Synopsis []
67 
68  Description []
69 
70  SideEffects []
71 
72  SeeAlso []
73 
74 ***********************************************************************/
75 int * Gia_ManGigCount( Vec_Int_t * vObjs, Vec_Int_t * vStore )
76 {
77  static int nObjs[GIG_UNUSED]; int i;
78  for ( i = 0; i < GIG_UNUSED; i++ )
79  nObjs[i] = 0;
80  for ( i = 0; i < Vec_IntSize(vObjs); i++ )
81  nObjs[Vec_IntEntry(vStore, Vec_IntEntry(vObjs,i) + 1)]++;
82  return nObjs;
83 }
84 void Gia_ManGigPrint( int * nObjs )
85 {
86  int i;
87  printf( "Statistics: " );
88  for ( i = 1; i < GIG_UNUSED; i++ )
89  printf( "%s = %d ", s_GigNames[i], nObjs[i] );
90  printf( "\n" );
91 }
92 
93 /**Function*************************************************************
94 
95  Synopsis []
96 
97  Description []
98 
99  SideEffects []
100 
101  SeeAlso []
102 
103 ***********************************************************************/
104 void Gia_ManPrintDelays( Vec_Int_t * vObjs, Vec_Int_t * vStore )
105 {
106  Vec_Int_t * vFanCount = Vec_IntStart( Vec_IntSize(vObjs) + 100 );
107  int i, * pEntry;//, Counter = 0;
108  for ( i = 0; i < Vec_IntSize(vObjs); i++ )
109  {
110  pEntry = Vec_IntEntryP( vStore, Vec_IntEntry(vObjs,i) );
111  if ( pEntry[1] != GIG_SEL )
112  continue;
113  assert( pEntry[2] == 1 );
114  Vec_IntAddToEntry( vFanCount, pEntry[3], 1 );
115  }
116  for ( i = 0; i < Vec_IntSize(vObjs); i++ )
117  {
118  pEntry = Vec_IntEntryP( vStore, Vec_IntEntry(vObjs,i) );
119  if ( pEntry[1] != GIG_DELAY )
120  continue;
121  printf( "(%d,%d,%d) ", pEntry[2], Vec_IntEntry(vFanCount, pEntry[0]), pEntry[3+pEntry[2]] );
122  }
123  printf( "\n" );
124  Vec_IntFree( vFanCount );
125 }
126 
127 /**Function*************************************************************
128 
129  Synopsis []
130 
131  Description []
132 
133  SideEffects []
134 
135  SeeAlso []
136 
137 ***********************************************************************/
138 Gia_Man_t * Gia_ManBuildGig2( Vec_Int_t * vObjs, Vec_Int_t * vStore, char * pFileName )
139 {
140  Gia_Man_t * pNew, * pTemp;
141  //int * nObjs = Gia_ManGigCount( vObjs, vStore );
142  Vec_Int_t * vNets = Vec_IntAlloc( Vec_IntSize(vObjs) );
143  Vec_Int_t * vTypes = Vec_IntAlloc( Vec_IntSize(vObjs) );
144  Vec_Int_t * vMap;
145  int i, Type;
146  // connect net IDs
147  for ( i = 0; i < Vec_IntSize(vObjs); i++ )
148  {
149  Vec_IntPush( vNets, Vec_IntEntry(vStore, Vec_IntEntry(vObjs,i)) );
150  Vec_IntPush( vTypes, Vec_IntEntry(vStore, Vec_IntEntry(vObjs,i) + 1) );
151  }
152  // create mapping for net IDs into GIA IDs
153  vMap = Vec_IntStartFull( Vec_IntFindMax(vNets) + 1 );
154  Vec_IntWriteEntry( vMap, 0, 0 );
155  Vec_IntWriteEntry( vMap, 1, 1 );
156  // create new manager
157  pNew = Gia_ManStart( Vec_IntSize(vObjs) );
158  pNew->pName = Abc_UtilStrsav( pFileName );
159  pNew->pSpec = Abc_UtilStrsav( pFileName );
160  // create primary inputs
161  for ( i = 0; i < Vec_IntSize(vObjs); i++ )
162  if ( Vec_IntEntry(vTypes, i) == GIG_PI )
163  Vec_IntWriteEntry( vMap, Vec_IntEntry(vNets, i), Gia_ManAppendCi(pNew) );
164  // create box outputs
165  for ( i = 0; i < Vec_IntSize(vObjs); i++ )
166  if ( Vec_IntEntry(vTypes, i) == GIG_BOX )
167  Vec_IntWriteEntry( vMap, Vec_IntEntry(vNets, i), Gia_ManAppendCi(pNew) );
168  // create internal nodes
169  Gia_ManHashAlloc( pNew );
170  for ( i = 0; i < Vec_IntSize(vObjs); i++ )
171  {
172  Type = Vec_IntEntry(vTypes, i);
173  if ( Type != GIG_LUT && Type != GIG_DELAY && Type != GIG_BAR )
174  continue;
175 
176  }
177  Vec_IntFree( vMap );
178  Vec_IntFree( vNets );
179  Vec_IntFree( vTypes );
180  // rehash
181  pNew = Gia_ManCleanup( pTemp = pNew );
182  Gia_ManStop( pTemp );
183  return pNew;
184 }
185 Gia_Man_t * Gia_ManBuildGig( Vec_Int_t * vObjs, Vec_Int_t * vStore, char * pFileName )
186 {
187  printf( "Parsed %d objects and %d tokens.\n", Vec_IntSize(vObjs), Vec_IntSize(vStore) );
188  Gia_ManGigPrint( Gia_ManGigCount(vObjs, vStore) );
189  Gia_ManPrintDelays( vObjs, vStore );
190  return NULL;
191 }
192 
193 /**Function*************************************************************
194 
195  Synopsis []
196 
197  Description []
198 
199  SideEffects []
200 
201  SeeAlso []
202 
203 ***********************************************************************/
204 Gia_Man_t * Gia_ManReadGig( char * pFileName )
205 {
206  Gia_Man_t * pNew;
207  int Type, Offset, fEndOfLine, Digit, nObjs;
208  char * pChars = " w(-,)]\r\t";
209  char * pBuffer = Extra_FileReadContents( pFileName );
210  char * pStart = pBuffer, * pToken;
211  Vec_Int_t * vObjs, * vStore;
212  if ( pBuffer == NULL )
213  printf( "Cannot open input file %s\n", pFileName );
214  // count objects
215  for ( nObjs = 0, pToken = pBuffer; *pToken; pToken++ )
216  nObjs += (int)(*pToken == '\n');
217  // read objects
218  vObjs = Vec_IntAlloc( nObjs );
219  vStore = Vec_IntAlloc( 10*nObjs );
220  while ( 1 )
221  {
222  // read net ID
223  pToken = strtok( pStart, pChars );
224  pStart = NULL;
225  if ( pToken == NULL )
226  break;
227  // start new object
228  Vec_IntPush( vObjs, Vec_IntSize(vStore) );
229  // save net ID
230  assert( pToken[0] >= '0' && pToken[0] <= '9' );
231  Vec_IntPush( vStore, atoi(pToken) );
232  // read equal
233  pToken = strtok( pStart, pChars );
234  assert( pToken[0] == '=' );
235  // read type
236  pToken = strtok( pStart, pChars );
237  fEndOfLine = 0;
238  if ( pToken[strlen(pToken)-1] == '\n' )
239  {
240  pToken[strlen(pToken)-1] = 0;
241  fEndOfLine = 1;
242  }
243  for ( Type = GIG_RESET; Type < GIG_UNUSED; Type++ )
244  if ( !strcmp(pToken, s_GigNames[Type]) )
245  break;
246  assert( Type < GIG_UNUSED );
247  Vec_IntPush( vStore, Type );
248  if ( fEndOfLine )
249  continue;
250  // read fanins
251  Offset = Vec_IntSize(vStore);
252  Vec_IntPush( vStore, 0 );
253  while ( 1 )
254  {
255  pToken = strtok( pStart, pChars );
256  if ( pToken == NULL || pToken[0] == '\n' || pToken[0] == '[' )
257  break;
258  assert( pToken[0] >= '0' && pToken[0] <= '9' );
259  Vec_IntPush( vStore, atoi(pToken) );
260  Vec_IntAddToEntry( vStore, Offset, 1 );
261  }
262  assert( pToken != NULL );
263  if ( pToken[0] == '\n' )
264  continue;
265  assert( pToken[0] == '[' );
266  // read attribute
267  pToken++;
268  if ( Type == GIG_LUT )
269  {
270  assert( strlen(pToken) == 4 );
271  Digit = Abc_TtReadHexDigit(pToken[0]);
272  Digit |= Abc_TtReadHexDigit(pToken[1]) << 4;
273  Digit |= Abc_TtReadHexDigit(pToken[2]) << 8;
274  Digit |= Abc_TtReadHexDigit(pToken[3]) << 12;
275  Vec_IntPush( vStore, Digit );
276  }
277  else
278  {
279  assert( Type == GIG_DELAY );
280  Vec_IntPush( vStore, atoi(pToken) );
281  }
282  // read end of line
283  pToken = strtok( pStart, pChars );
284  assert( pToken[0] == '\n' );
285  }
286  ABC_FREE( pBuffer );
287  // create AIG
288  pNew = Gia_ManBuildGig( vObjs, vStore, pFileName );
289  // cleanup
290  Vec_IntFree( vObjs );
291  Vec_IntFree( vStore );
292  return pNew;
293 }
294 
295 ////////////////////////////////////////////////////////////////////////
296 /// END OF FILE ///
297 ////////////////////////////////////////////////////////////////////////
298 
299 
301 
void Gia_ManPrintDelays(Vec_Int_t *vObjs, Vec_Int_t *vStore)
Definition: giaGig.c:104
Definition: giaGig.c:38
void Gia_ManStop(Gia_Man_t *p)
Definition: giaMan.c:77
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition: bblif.c:37
static int Gia_ManAppendCi(Gia_Man_t *p)
Definition: gia.h:583
char * strtok()
static int Abc_TtReadHexDigit(char HexChar)
Definition: utilTruth.h:756
Definition: giaGig.c:37
int * Gia_ManGigCount(Vec_Int_t *vObjs, Vec_Int_t *vStore)
FUNCTION DEFINITIONS ///.
Definition: giaGig.c:75
static Vec_Int_t * Vec_IntStartFull(int nSize)
Definition: vecInt.h:119
int strcmp()
static void Vec_IntWriteEntry(Vec_Int_t *p, int i, int Entry)
Definition: bblif.c:285
static Vec_Int_t * Vec_IntStart(int nSize)
Definition: bblif.c:172
char * pName
Definition: gia.h:97
static Vec_Int_t * Vec_IntAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: bblif.c:149
void Gia_ManGigPrint(int *nObjs)
Definition: giaGig.c:84
static void Vec_IntAddToEntry(Vec_Int_t *p, int i, int Addition)
Definition: bblif.c:302
static int Vec_IntEntry(Vec_Int_t *p, int i)
Definition: bblif.c:268
static int Vec_IntFindMax(Vec_Int_t *p)
Definition: vecInt.h:996
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
char * pSpec
Definition: gia.h:98
Gia_Man_t * Gia_ManStart(int nObjsMax)
DECLARATIONS ///.
Definition: giaMan.c:52
static void Vec_IntPush(Vec_Int_t *p, int Entry)
Definition: bblif.c:468
Gia_Man_t * Gia_ManBuildGig(Vec_Int_t *vObjs, Vec_Int_t *vStore, char *pFileName)
Definition: giaGig.c:185
Gia_Man_t * Gia_ManBuildGig2(Vec_Int_t *vObjs, Vec_Int_t *vStore, char *pFileName)
Definition: giaGig.c:138
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
static int Vec_IntSize(Vec_Int_t *p)
Definition: bblif.c:252
#define ABC_FREE(obj)
Definition: abc_global.h:232
Definition: giaGig.c:42
Definition: gia.h:95
Definition: giaGig.c:36
Gia_Man_t * Gia_ManReadGig(char *pFileName)
Definition: giaGig.c:204
Definition: giaGig.c:35
#define assert(ex)
Definition: util_old.h:213
static int * Vec_IntEntryP(Vec_Int_t *p, int i)
Definition: vecInt.h:417
int strlen()
Definition: giaGig.c:40
void Gia_ManHashAlloc(Gia_Man_t *p)
Definition: giaHash.c:99
Definition: giaGig.c:41
static void Vec_IntFree(Vec_Int_t *p)
Definition: bblif.c:235
Gia_Man_t * Gia_ManCleanup(Gia_Man_t *p)
Definition: giaScl.c:84
char * Abc_UtilStrsav(char *s)
Definition: starter.c:47
char * Extra_FileReadContents(char *pFileName)
static char * s_GigNames[GIG_UNUSED]
Definition: giaGig.c:46