abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
amapRead.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [amapRead.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Technology mapper for standard cells.]
8 
9  Synopsis []
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: amapRead.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "amapInt.h"
22 #include "base/io/ioAbc.h"
23 
25 
26 
27 ////////////////////////////////////////////////////////////////////////
28 /// DECLARATIONS ///
29 ////////////////////////////////////////////////////////////////////////
30 
31 #define AMAP_STRING_GATE "GATE"
32 #define AMAP_STRING_PIN "PIN"
33 #define AMAP_STRING_NONINV "NONINV"
34 #define AMAP_STRING_INV "INV"
35 #define AMAP_STRING_UNKNOWN "UNKNOWN"
36 
37 // these symbols (and no other) can appear in the formulas
38 #define AMAP_SYMB_AND '*'
39 #define AMAP_SYMB_OR1 '+'
40 #define AMAP_SYMB_OR2 '|'
41 #define AMAP_SYMB_XOR '^'
42 #define AMAP_SYMB_NOT '!'
43 #define AMAP_SYMB_AFTNOT '\''
44 #define AMAP_SYMB_OPEN '('
45 #define AMAP_SYMB_CLOSE ')'
46 
47 typedef enum {
52 
53 static inline Amap_Gat_t * Amap_ParseGateAlloc( Aig_MmFlex_t * p, int nPins )
54 { return (Amap_Gat_t *)Aig_MmFlexEntryFetch( p, sizeof(Amap_Gat_t)+sizeof(Amap_Pin_t)*nPins ); }
55 static inline char * Amap_ParseStrsav( Aig_MmFlex_t * p, char * pStr )
56 { return pStr ? strcpy(Aig_MmFlexEntryFetch(p, strlen(pStr)+1), pStr) : NULL; }
57 
58 ////////////////////////////////////////////////////////////////////////
59 /// FUNCTION DEFINITIONS ///
60 ////////////////////////////////////////////////////////////////////////
61 
62 /**Function*************************************************************
63 
64  Synopsis [Loads the file into temporary buffer.]
65 
66  Description []
67 
68  SideEffects []
69 
70  SeeAlso []
71 
72 ***********************************************************************/
73 char * Amap_LoadFile( char * pFileName )
74 {
75 // extern FILE * Io_FileOpen( const char * FileName, const char * PathVar, const char * Mode, int fVerbose );
76  FILE * pFile;
77  char * pBuffer;
78  int nFileSize;
79  int RetValue;
80  // open the BLIF file for binary reading
81  pFile = Io_FileOpen( pFileName, "open_path", "rb", 1 );
82 // pFile = fopen( FileName, "rb" );
83  // if we got this far, file should be okay otherwise would
84  // have been detected by caller
85  if ( pFile == NULL )
86  {
87  printf( "Cannot open file \"%s\".\n", pFileName );
88  return NULL;
89  }
90  assert ( pFile != NULL );
91  // get the file size, in bytes
92  fseek( pFile, 0, SEEK_END );
93  nFileSize = ftell( pFile );
94  // move the file current reading position to the beginning
95  rewind( pFile );
96  // load the contents of the file into memory
97  pBuffer = ABC_ALLOC( char, nFileSize + 10 );
98  RetValue = fread( pBuffer, nFileSize, 1, pFile );
99  // terminate the string with '\0'
100  pBuffer[ nFileSize ] = '\0';
101  strcat( pBuffer, "\n.end\n" );
102  // close file
103  fclose( pFile );
104  return pBuffer;
105 }
106 
107 /**Function*************************************************************
108 
109  Synopsis [Eliminates comments from the input file.]
110 
111  Description [As a byproduct, this procedure also counts the number
112  lines and dot-statements in the input file. This also joins non-comment
113  lines that are joined with a backspace '\']
114 
115  SideEffects []
116 
117  SeeAlso []
118 
119 ***********************************************************************/
120 void Amap_RemoveComments( char * pBuffer, int * pnDots, int * pnLines )
121 {
122  char * pCur;
123  int nDots, nLines;
124  // scan through the buffer and eliminate comments
125  // (in the BLIF file, comments are lines starting with "#")
126  nDots = nLines = 0;
127  for ( pCur = pBuffer; *pCur; pCur++ )
128  {
129  // if this is the beginning of comment
130  // clean it with spaces until the new line statement
131  if ( *pCur == '#' )
132  while ( *pCur != '\n' )
133  *pCur++ = ' ';
134 
135  // count the number of new lines and dots
136  if ( *pCur == '\n' ) {
137  if (*(pCur-1)=='\r') {
138  // DOS(R) file support
139  if (*(pCur-2)!='\\') nLines++;
140  else {
141  // rewind to backslash and overwrite with a space
142  *(pCur-2) = ' ';
143  *(pCur-1) = ' ';
144  *pCur = ' ';
145  }
146  } else {
147  // UNIX(TM) file support
148  if (*(pCur-1)!='\\') nLines++;
149  else {
150  // rewind to backslash and overwrite with a space
151  *(pCur-1) = ' ';
152  *pCur = ' ';
153  }
154  }
155  }
156  else if ( *pCur == '.' )
157  nDots++;
158  }
159  if ( pnDots )
160  *pnDots = nDots;
161  if ( pnLines )
162  *pnLines = nLines;
163 }
164 
165 /**Function*************************************************************
166 
167  Synopsis [Splits the stream into tokens.]
168 
169  Description []
170 
171  SideEffects []
172 
173  SeeAlso []
174 
175 ***********************************************************************/
176 Vec_Ptr_t * Amap_DeriveTokens( char * pBuffer )
177 {
178  Vec_Ptr_t * vTokens;
179  char * pToken;
180  vTokens = Vec_PtrAlloc( 1000 );
181  pToken = strtok( pBuffer, " =\t\r\n" );
182  while ( pToken )
183  {
184  Vec_PtrPush( vTokens, pToken );
185  pToken = strtok( NULL, " =\t\r\n" );
186  // skip latches
187  if ( pToken && strcmp( pToken, "LATCH" ) == 0 )
188  while ( pToken && strcmp( pToken, "GATE" ) != 0 )
189  pToken = strtok( NULL, " =\t\r\n" );
190  }
191  return vTokens;
192 }
193 
194 /**Function*************************************************************
195 
196  Synopsis [Finds the number of pins.]
197 
198  Description []
199 
200  SideEffects []
201 
202  SeeAlso []
203 
204 ***********************************************************************/
205 int Amap_ParseCountPins( Vec_Ptr_t * vTokens, int iPos )
206 {
207  char * pToken;
208  int i, Counter = 0;
209  Vec_PtrForEachEntryStart( char *, vTokens, pToken, i, iPos )
210  if ( !strcmp( pToken, AMAP_STRING_PIN ) )
211  Counter++;
212  else if ( !strcmp( pToken, AMAP_STRING_GATE ) )
213  return Counter;
214  return Counter;
215 }
216 
217 /**Function*************************************************************
218 
219  Synopsis [Collect the pin names used in the formula.]
220 
221  Description []
222 
223  SideEffects []
224 
225  SeeAlso []
226 
227 ***********************************************************************/
228 int Amap_GateCollectNames( Aig_MmFlex_t * pMem, char * pForm, char * pPinNames[] )
229 {
230  char Buffer[1000];
231  char * pTemp;
232  int nPins, i;
233  // save the formula as it was
234  strcpy( Buffer, pForm );
235  // remove the non-name symbols
236  for ( pTemp = Buffer; *pTemp; pTemp++ )
237  if ( *pTemp == AMAP_SYMB_AND || *pTemp == AMAP_SYMB_OR1 || *pTemp == AMAP_SYMB_OR2
238  || *pTemp == AMAP_SYMB_XOR || *pTemp == AMAP_SYMB_NOT || *pTemp == AMAP_SYMB_OPEN
239  || *pTemp == AMAP_SYMB_CLOSE || *pTemp == AMAP_SYMB_AFTNOT )
240  *pTemp = ' ';
241  // save the names
242  nPins = 0;
243  pTemp = strtok( Buffer, " " );
244  while ( pTemp )
245  {
246  for ( i = 0; i < nPins; i++ )
247  if ( strcmp( pTemp, pPinNames[i] ) == 0 )
248  break;
249  if ( i == nPins )
250  { // cannot find this name; save it
251  pPinNames[nPins++] = Amap_ParseStrsav( pMem, pTemp );
252  }
253  // get the next name
254  pTemp = strtok( NULL, " " );
255  }
256  return nPins;
257 }
258 
259 /**Function*************************************************************
260 
261  Synopsis [Creates a duplicate gate with pins specified.]
262 
263  Description []
264 
265  SideEffects []
266 
267  SeeAlso []
268 
269 ***********************************************************************/
271 {
272  Amap_Gat_t * pGate;
273  Amap_Pin_t * pPin;
274  char * pPinNames[128];
275  int nPinNames;
276  assert( p->nPins == 1 && !strcmp( p->Pins->pName, "*" ) );
277  nPinNames = Amap_GateCollectNames( p->pLib->pMemGates, p->pForm, pPinNames );
278  pGate = Amap_ParseGateAlloc( p->pLib->pMemGates, nPinNames );
279  *pGate = *p;
280  pGate->nPins = nPinNames;
281  Amap_GateForEachPin( pGate, pPin )
282  {
283  *pPin = *p->Pins;
284  pPin->pName = pPinNames[pPin - pGate->Pins];
285  }
286  return pGate;
287 }
288 
289 /**Function*************************************************************
290 
291  Synopsis []
292 
293  Description []
294 
295  SideEffects []
296 
297  SeeAlso []
298 
299 ***********************************************************************/
300 int Amap_CollectFormulaTokens( Vec_Ptr_t * vTokens, char * pToken, int iPos )
301 {
302  char * pNext, * pPrev;
303  pPrev = pToken + strlen(pToken);
304  while ( *(pPrev-1) != ';' )
305  {
306  *pPrev++ = ' ';
307  pNext = (char *)Vec_PtrEntry(vTokens, iPos++);
308  while ( *pNext )
309  *pPrev++ = *pNext++;
310  }
311  *(pPrev-1) = 0;
312  return iPos;
313 }
314 
315 /**Function*************************************************************
316 
317  Synopsis []
318 
319  Description []
320 
321  SideEffects []
322 
323  SeeAlso []
324 
325 ***********************************************************************/
326 Amap_Lib_t * Amap_ParseTokens( Vec_Ptr_t * vTokens, int fVerbose )
327 {
328  Amap_Lib_t * p;
329  Amap_Gat_t * pGate, * pPrev;
330  Amap_Pin_t * pPin;
331  char * pToken, * pMoGate = NULL;
332  int i, nPins, iPos = 0, Count = 0;
333  p = Amap_LibAlloc();
334  pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
335  do
336  {
337  if ( strcmp( pToken, AMAP_STRING_GATE ) )
338  {
339  Amap_LibFree( p );
340  printf( "The first line should begin with %s.\n", AMAP_STRING_GATE );
341  return NULL;
342  }
343  // start gate
344  nPins = Amap_ParseCountPins( vTokens, iPos );
345  pGate = Amap_ParseGateAlloc( p->pMemGates, nPins );
346  memset( pGate, 0, sizeof(Amap_Gat_t) );
347  pGate->Id = Vec_PtrSize( p->vGates );
348  Vec_PtrPush( p->vGates, pGate );
349  pGate->pLib = p;
350  pGate->nPins = nPins;
351  // read gate
352  pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
353  pGate->pName = Amap_ParseStrsav( p->pMemGates, pToken );
354  pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
355  pGate->dArea = atof( pToken );
356  pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
357  pGate->pOutName = Amap_ParseStrsav( p->pMemGates, pToken );
358  pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
359  iPos = Amap_CollectFormulaTokens( vTokens, pToken, iPos );
360  pGate->pForm = Amap_ParseStrsav( p->pMemGates, pToken );
361  // read pins
362  Amap_GateForEachPin( pGate, pPin )
363  {
364  pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
365  if ( strcmp( pToken, AMAP_STRING_PIN ) )
366  {
367  Amap_LibFree( p );
368  printf( "Cannot parse gate %s.\n", pGate->pName );
369  return NULL;
370  }
371  // read pin
372  pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
373  pPin->pName = Amap_ParseStrsav( p->pMemGates, pToken );
374  pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
375  if ( strcmp( pToken, AMAP_STRING_UNKNOWN ) == 0 )
376  pPin->Phase = AMAP_PHASE_UNKNOWN;
377  else if ( strcmp( pToken, AMAP_STRING_INV ) == 0 )
378  pPin->Phase = AMAP_PHASE_INV;
379  else if ( strcmp( pToken, AMAP_STRING_NONINV ) == 0 )
380  pPin->Phase = AMAP_PHASE_NONINV;
381  else
382  {
383  Amap_LibFree( p );
384  printf( "Cannot read phase of pin %s of gate %s\n", pPin->pName, pGate->pName );
385  return NULL;
386  }
387  pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
388  pPin->dLoadInput = atof( pToken );
389  pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
390  pPin->dLoadMax = atof( pToken );
391  pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
392  pPin->dDelayBlockRise = atof( pToken );
393  pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
394  pPin->dDelayFanoutRise = atof( pToken );
395  pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
396  pPin->dDelayBlockFall = atof( pToken );
397  pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
398  pPin->dDelayFanoutFall = atof( pToken );
399  if ( pPin->dDelayBlockRise > pPin->dDelayBlockFall )
400  pPin->dDelayBlockMax = pPin->dDelayBlockRise;
401  else
402  pPin->dDelayBlockMax = pPin->dDelayBlockFall;
403  }
404  // fix the situation when all pins are represented as one
405  if ( pGate->nPins == 1 && !strcmp( pGate->Pins->pName, "*" ) )
406  {
407  pGate = Amap_ParseGateWithSamePins( pGate );
408  Vec_PtrPop( p->vGates );
409  Vec_PtrPush( p->vGates, pGate );
410  }
411  pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
412 //printf( "Finished reading gate %s (%s)\n", pGate->pName, pGate->pOutName );
413  }
414  while ( strcmp( pToken, ".end" ) );
415 
416  // check if there are gates with identical names
417  pPrev = NULL;
418  Amap_LibForEachGate( p, pGate, i )
419  {
420  if ( pPrev && !strcmp(pPrev->pName, pGate->pName) )
421  {
422  pPrev->pTwin = pGate, pGate->pTwin = pPrev;
423 // printf( "Warning: Detected multi-output gate \"%s\".\n", pGate->pName );
424  if ( pMoGate == NULL )
425  pMoGate = pGate->pName;
426  Count++;
427  }
428  pPrev = pGate;
429  }
430  if ( Count )
431  printf( "Warning: Detected %d multi-output gates (for example, \"%s\").\n", Count, pMoGate );
432  return p;
433 }
434 
435 /**Function*************************************************************
436 
437  Synopsis [Reads the library from the input file.]
438 
439  Description []
440 
441  SideEffects []
442 
443  SeeAlso []
444 
445 ***********************************************************************/
446 Amap_Lib_t * Amap_LibReadBuffer( char * pBuffer, int fVerbose )
447 {
448  Amap_Lib_t * pLib;
449  Vec_Ptr_t * vTokens;
450  Amap_RemoveComments( pBuffer, NULL, NULL );
451  vTokens = Amap_DeriveTokens( pBuffer );
452  pLib = Amap_ParseTokens( vTokens, fVerbose );
453  if ( pLib == NULL )
454  {
455  Vec_PtrFree( vTokens );
456  return NULL;
457  }
458  Vec_PtrFree( vTokens );
459  return pLib;
460 }
461 
462 /**Function*************************************************************
463 
464  Synopsis [Reads the library from the input file.]
465 
466  Description []
467 
468  SideEffects []
469 
470  SeeAlso []
471 
472 ***********************************************************************/
473 Amap_Lib_t * Amap_LibReadFile( char * pFileName, int fVerbose )
474 {
475  Amap_Lib_t * pLib;
476  char * pBuffer;
477  pBuffer = Amap_LoadFile( pFileName );
478  if ( pBuffer == NULL )
479  return NULL;
480  pLib = Amap_LibReadBuffer( pBuffer, fVerbose );
481  if ( pLib )
482  pLib->pName = Abc_UtilStrsav( pFileName );
483  ABC_FREE( pBuffer );
484  return pLib;
485 }
486 
487 ////////////////////////////////////////////////////////////////////////
488 /// END OF FILE ///
489 ////////////////////////////////////////////////////////////////////////
490 
491 
493 
char * memset()
Amap_Lib_t * pLib
Definition: amapInt.h:152
Amap_PinPhase_t
Definition: amapRead.c:47
FILE * Io_FileOpen(const char *FileName, const char *PathVar, const char *Mode, int fVerbose)
Definition: ioUtil.c:819
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
#define AMAP_STRING_UNKNOWN
Definition: amapRead.c:35
double dLoadInput
Definition: amapInt.h:142
char * pForm
Definition: amapInt.h:157
#define Vec_PtrForEachEntryStart(Type, vVec, pEntry, i, Start)
Definition: vecPtr.h:57
#define Amap_GateForEachPin(pGate, pPin)
Definition: amapInt.h:296
static Llb_Mgr_t * p
Definition: llb3Image.c:950
int Phase
Definition: amapInt.h:141
#define SEEK_END
Definition: zconf.h:392
#define AMAP_SYMB_XOR
Definition: amapRead.c:41
#define AMAP_STRING_PIN
Definition: amapRead.c:32
Amap_Lib_t * Amap_ParseTokens(Vec_Ptr_t *vTokens, int fVerbose)
Definition: amapRead.c:326
char * Aig_MmFlexEntryFetch(Aig_MmFlex_t *p, int nBytes)
Definition: aigMem.c:366
char * strtok()
int Amap_ParseCountPins(Vec_Ptr_t *vTokens, int iPos)
Definition: amapRead.c:205
Amap_Lib_t * Amap_LibAlloc()
DECLARATIONS ///.
Definition: amapLib.c:45
char * Amap_LoadFile(char *pFileName)
FUNCTION DEFINITIONS ///.
Definition: amapRead.c:73
static Amap_Gat_t * Amap_ParseGateAlloc(Aig_MmFlex_t *p, int nPins)
Definition: amapRead.c:53
static void Vec_PtrPush(Vec_Ptr_t *p, void *Entry)
Definition: vecPtr.h:606
Amap_Gat_t * Amap_ParseGateWithSamePins(Amap_Gat_t *p)
Definition: amapRead.c:270
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
char * pOutName
Definition: amapInt.h:155
double dDelayBlockRise
Definition: amapInt.h:144
double dLoadMax
Definition: amapInt.h:143
int Amap_GateCollectNames(Aig_MmFlex_t *pMem, char *pForm, char *pPinNames[])
Definition: amapRead.c:228
static int Vec_PtrSize(Vec_Ptr_t *p)
Definition: vecPtr.h:295
static void * Vec_PtrPop(Vec_Ptr_t *p)
Definition: vecPtr.h:677
Amap_Lib_t * Amap_LibReadFile(char *pFileName, int fVerbose)
Definition: amapRead.c:473
double dDelayBlockMax
Definition: amapInt.h:148
#define AMAP_SYMB_OR2
Definition: amapRead.c:40
static char * Amap_ParseStrsav(Aig_MmFlex_t *p, char *pStr)
Definition: amapRead.c:55
int strcmp()
Amap_Pin_t Pins[0]
Definition: amapInt.h:162
#define AMAP_SYMB_AFTNOT
Definition: amapRead.c:43
#define AMAP_SYMB_NOT
Definition: amapRead.c:42
void Amap_LibFree(Amap_Lib_t *p)
Definition: amapLib.c:67
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
if(last==0)
Definition: sparse_int.h:34
#define AMAP_SYMB_AND
Definition: amapRead.c:38
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
char * strcpy()
#define AMAP_SYMB_CLOSE
Definition: amapRead.c:45
double atof()
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
static void * Vec_PtrEntry(Vec_Ptr_t *p, int i)
Definition: vecPtr.h:362
int Amap_CollectFormulaTokens(Vec_Ptr_t *vTokens, char *pToken, int iPos)
Definition: amapRead.c:300
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_DeriveTokens(char *pBuffer)
Definition: amapRead.c:176
#define AMAP_SYMB_OR1
Definition: amapRead.c:39
#define ABC_FREE(obj)
Definition: abc_global.h:232
double dDelayFanoutRise
Definition: amapInt.h:145
#define AMAP_STRING_INV
Definition: amapRead.c:34
char * strcat()
double dDelayBlockFall
Definition: amapInt.h:146
#define assert(ex)
Definition: util_old.h:213
int strlen()
#define AMAP_STRING_NONINV
Definition: amapRead.c:33
#define AMAP_SYMB_OPEN
Definition: amapRead.c:44
void Amap_RemoveComments(char *pBuffer, int *pnDots, int *pnLines)
Definition: amapRead.c:120
char * pName
Definition: amapInt.h:154
char * Abc_UtilStrsav(char *s)
Definition: starter.c:47
unsigned Id
Definition: amapInt.h:159
VOID_HACK rewind()
Amap_Lib_t * Amap_LibReadBuffer(char *pBuffer, int fVerbose)
Definition: amapRead.c:446
#define AMAP_STRING_GATE
DECLARATIONS ///.
Definition: amapRead.c:31
static void Vec_PtrFree(Vec_Ptr_t *p)
Definition: vecPtr.h:223