ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
amapRead.c
Go to the documentation of this file.
1
20
21#include "amapInt.h"
22#include "base/io/ioAbc.h"
23
25
26
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_AND2 '&'
40#define AMAP_SYMB_OR1 '+'
41#define AMAP_SYMB_OR2 '|'
42#define AMAP_SYMB_XOR '^'
43#define AMAP_SYMB_NOT '!'
44#define AMAP_SYMB_AFTNOT '\''
45#define AMAP_SYMB_OPEN '('
46#define AMAP_SYMB_CLOSE ')'
47
53
54static inline Amap_Gat_t * Amap_ParseGateAlloc( Aig_MmFlex_t * p, int nPins )
55{ return (Amap_Gat_t *)Aig_MmFlexEntryFetch( p, sizeof(Amap_Gat_t)+sizeof(Amap_Pin_t)*nPins ); }
56static inline char * Amap_ParseStrsav( Aig_MmFlex_t * p, char * pStr )
57{ return pStr ? strcpy(Aig_MmFlexEntryFetch(p, strlen(pStr)+1), pStr) : NULL; }
58
62
74char * Amap_LoadFile( char * pFileName )
75{
76// extern FILE * Io_FileOpen( const char * FileName, const char * PathVar, const char * Mode, int fVerbose );
77 FILE * pFile;
78 char * pBuffer;
79 int nFileSize;
80 int RetValue;
81 // open the BLIF file for binary reading
82 pFile = Io_FileOpen( pFileName, "open_path", "rb", 1 );
83// pFile = fopen( FileName, "rb" );
84 // if we got this far, file should be okay otherwise would
85 // have been detected by caller
86 if ( pFile == NULL )
87 {
88 printf( "Cannot open file \"%s\".\n", pFileName );
89 return NULL;
90 }
91 assert ( pFile != NULL );
92 // get the file size, in bytes
93 fseek( pFile, 0, SEEK_END );
94 nFileSize = ftell( pFile );
95 // move the file current reading position to the beginning
96 rewind( pFile );
97 // load the contents of the file into memory
98 pBuffer = ABC_ALLOC( char, nFileSize + 10 );
99 RetValue = fread( pBuffer, nFileSize, 1, pFile );
100 // terminate the string with '\0'
101 pBuffer[ nFileSize ] = '\0';
102 strcat( pBuffer, "\n.end\n" );
103 // close file
104 fclose( pFile );
105 return pBuffer;
106}
107
121void Amap_RemoveComments( char * pBuffer, int * pnDots, int * pnLines )
122{
123 char * pCur;
124 int nDots, nLines;
125 // scan through the buffer and eliminate comments
126 // (in the BLIF file, comments are lines starting with "#")
127 nDots = nLines = 0;
128 for ( pCur = pBuffer; *pCur; pCur++ )
129 {
130 // if this is the beginning of comment
131 // clean it with spaces until the new line statement
132 if ( *pCur == '#' ) {
133 while ( *pCur != '\n' ) {
134 *pCur++ = ' ';
135 }
136 }
137 // count the number of new lines and dots
138 if ( *pCur == '\n' ) {
139 if (pCur > pBuffer) {
140 if (*(pCur - 1) == '\r') {
141 // DOS(R) file support
142 if (pCur > (pBuffer + 1)) {
143 if (*(pCur - 2)!='\\') {
144 nLines++;
145 }
146 else {
147 // rewind to backslash and overwrite with a space
148 *(pCur - 2) = ' ';
149 *(pCur - 1) = ' ';
150 *pCur = ' ';
151 }
152 }
153 } else {
154 // UNIX(TM) file support
155 if (*(pCur - 1) != '\\') {
156 nLines++;
157 }
158 else {
159 // rewind to backslash and overwrite with a space
160 *(pCur-1) = ' ';
161 *pCur = ' ';
162 }
163 }
164 }
165 }
166 else if ( *pCur == '.' ) {
167 nDots++;
168 }
169 }
170
171 if ( pnDots )
172 *pnDots = nDots;
173 if ( pnLines )
174 *pnLines = nLines;
175}
176
188Vec_Ptr_t * Amap_DeriveTokens( char * pBuffer )
189{
190 Vec_Ptr_t * vTokens;
191 char * pToken;
192 vTokens = Vec_PtrAlloc( 1000 );
193 pToken = strtok( pBuffer, " =\t\r\n" );
194 while ( pToken )
195 {
196 Vec_PtrPush( vTokens, pToken );
197 pToken = strtok( NULL, " =\t\r\n" );
198 // skip latches
199 if ( pToken && strcmp( pToken, "LATCH" ) == 0 )
200 while ( pToken && strcmp( pToken, "GATE" ) != 0 )
201 pToken = strtok( NULL, " =\t\r\n" );
202 }
203 return vTokens;
204}
205
217int Amap_ParseCountPins( Vec_Ptr_t * vTokens, int iPos )
218{
219 char * pToken;
220 int i, Counter = 0;
221 Vec_PtrForEachEntryStart( char *, vTokens, pToken, i, iPos )
222 if ( !strcmp( pToken, AMAP_STRING_PIN ) )
223 Counter++;
224 else if ( !strcmp( pToken, AMAP_STRING_GATE ) )
225 return Counter;
226 return Counter;
227}
228
240int Amap_GateCollectNames( Aig_MmFlex_t * pMem, char * pForm, char * pPinNames[] )
241{
242 char Buffer[1000];
243 char * pTemp;
244 int nPins, i;
245 // save the formula as it was
246 strcpy( Buffer, pForm );
247 // remove the non-name symbols
248 for ( pTemp = Buffer; *pTemp; pTemp++ )
249 if ( *pTemp == AMAP_SYMB_AND || *pTemp == AMAP_SYMB_OR1 || *pTemp == AMAP_SYMB_OR2
250 || *pTemp == AMAP_SYMB_XOR || *pTemp == AMAP_SYMB_NOT || *pTemp == AMAP_SYMB_OPEN
251 || *pTemp == AMAP_SYMB_CLOSE || *pTemp == AMAP_SYMB_AFTNOT || *pTemp == AMAP_SYMB_AND2 )
252 *pTemp = ' ';
253 // save the names
254 nPins = 0;
255 pTemp = strtok( Buffer, " " );
256 while ( pTemp )
257 {
258 for ( i = 0; i < nPins; i++ )
259 if ( strcmp( pTemp, pPinNames[i] ) == 0 )
260 break;
261 if ( i == nPins )
262 { // cannot find this name; save it
263 pPinNames[nPins++] = Amap_ParseStrsav( pMem, pTemp );
264 }
265 // get the next name
266 pTemp = strtok( NULL, " " );
267 }
268 return nPins;
269}
270
283{
284 Amap_Gat_t * pGate;
285 Amap_Pin_t * pPin;
286 char * pPinNames[128];
287 int nPinNames;
288 assert( p->nPins == 1 && !strcmp( p->Pins->pName, "*" ) );
289 nPinNames = Amap_GateCollectNames( p->pLib->pMemGates, p->pForm, pPinNames );
290 pGate = Amap_ParseGateAlloc( p->pLib->pMemGates, nPinNames );
291 *pGate = *p;
292 pGate->nPins = nPinNames;
293 Amap_GateForEachPin( pGate, pPin )
294 {
295 *pPin = *p->Pins;
296 pPin->pName = pPinNames[pPin - pGate->Pins];
297 }
298 return pGate;
299}
300
312int Amap_CollectFormulaTokens( Vec_Ptr_t * vTokens, char * pToken, int iPos )
313{
314 char * pNext, * pPrev;
315 pPrev = pToken + strlen(pToken);
316 while ( *(pPrev-1) != ';' )
317 {
318 *pPrev++ = ' ';
319 pNext = (char *)Vec_PtrEntry(vTokens, iPos++);
320 while ( *pNext )
321 *pPrev++ = *pNext++;
322 }
323 *(pPrev-1) = 0;
324 return iPos;
325}
326
338Amap_Lib_t * Amap_ParseTokens( Vec_Ptr_t * vTokens, int fVerbose )
339{
340 Amap_Lib_t * p;
341 Amap_Gat_t * pGate, * pPrev;
342 Amap_Pin_t * pPin;
343 char * pToken, * pMoGate = NULL;
344 int i, nPins, iPos = 0, Count = 0;
345 p = Amap_LibAlloc();
346 pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
347 do
348 {
349 if ( strcmp( pToken, AMAP_STRING_GATE ) )
350 {
351 Amap_LibFree( p );
352 printf( "The first line should begin with %s.\n", AMAP_STRING_GATE );
353 return NULL;
354 }
355 // start gate
356 nPins = Amap_ParseCountPins( vTokens, iPos );
357 pGate = Amap_ParseGateAlloc( p->pMemGates, nPins );
358 memset( pGate, 0, sizeof(Amap_Gat_t) );
359 pGate->Id = Vec_PtrSize( p->vGates );
360 Vec_PtrPush( p->vGates, pGate );
361 pGate->pLib = p;
362 pGate->nPins = nPins;
363 // read gate
364 pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
365 pGate->pName = Amap_ParseStrsav( p->pMemGates, pToken );
366 pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
367 pGate->dArea = atof( pToken );
368 pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
369 pGate->pOutName = Amap_ParseStrsav( p->pMemGates, pToken );
370 pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
371 iPos = Amap_CollectFormulaTokens( vTokens, pToken, iPos );
372 pGate->pForm = Amap_ParseStrsav( p->pMemGates, pToken );
373 // read pins
374 Amap_GateForEachPin( pGate, pPin )
375 {
376 pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
377 if ( strcmp( pToken, AMAP_STRING_PIN ) )
378 {
379 Amap_LibFree( p );
380 printf( "Cannot parse cell %s.\n", pGate->pName );
381 return NULL;
382 }
383 // read pin
384 pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
385 pPin->pName = Amap_ParseStrsav( p->pMemGates, pToken );
386 pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
387 if ( strcmp( pToken, AMAP_STRING_UNKNOWN ) == 0 )
389 else if ( strcmp( pToken, AMAP_STRING_INV ) == 0 )
390 pPin->Phase = AMAP_PHASE_INV;
391 else if ( strcmp( pToken, AMAP_STRING_NONINV ) == 0 )
392 pPin->Phase = AMAP_PHASE_NONINV;
393 else
394 {
395 Amap_LibFree( p );
396 printf( "Cannot read phase of pin %s of cell %s\n", pPin->pName, pGate->pName );
397 return NULL;
398 }
399 pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
400 pPin->dLoadInput = atof( pToken );
401 pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
402 pPin->dLoadMax = atof( pToken );
403 pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
404 pPin->dDelayBlockRise = atof( pToken );
405 pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
406 pPin->dDelayFanoutRise = atof( pToken );
407 pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
408 pPin->dDelayBlockFall = atof( pToken );
409 pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
410 pPin->dDelayFanoutFall = atof( pToken );
411 if ( pPin->dDelayBlockRise > pPin->dDelayBlockFall )
412 pPin->dDelayBlockMax = pPin->dDelayBlockRise;
413 else
414 pPin->dDelayBlockMax = pPin->dDelayBlockFall;
415 }
416 // fix the situation when all pins are represented as one
417 if ( pGate->nPins == 1 && !strcmp( pGate->Pins->pName, "*" ) )
418 {
419 pGate = Amap_ParseGateWithSamePins( pGate );
420 Vec_PtrPop( p->vGates );
421 Vec_PtrPush( p->vGates, pGate );
422 }
423 pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
424//printf( "Finished reading cell %s (%s)\n", pGate->pName, pGate->pOutName );
425 }
426 while ( strcmp( pToken, ".end" ) );
427
428 // check if there are gates with identical names
429 pPrev = NULL;
430 Amap_LibForEachGate( p, pGate, i )
431 {
432 if ( pPrev && !strcmp(pPrev->pName, pGate->pName) )
433 {
434 pPrev->pTwin = pGate, pGate->pTwin = pPrev;
435// printf( "Warning: Detected multi-output cell \"%s\".\n", pGate->pName );
436 if ( pMoGate == NULL )
437 pMoGate = pGate->pName;
438 Count++;
439 }
440 pPrev = pGate;
441 }
442 if ( Count )
443 printf( "Warning: Detected %d multi-output cells (for example, \"%s\").\n", Count, pMoGate );
444 return p;
445}
446
458Amap_Lib_t * Amap_LibReadBuffer( char * pBuffer, int fVerbose )
459{
460 Amap_Lib_t * pLib;
461 Vec_Ptr_t * vTokens;
462 Amap_RemoveComments( pBuffer, NULL, NULL );
463 vTokens = Amap_DeriveTokens( pBuffer );
464 pLib = Amap_ParseTokens( vTokens, fVerbose );
465 if ( pLib == NULL )
466 {
467 Vec_PtrFree( vTokens );
468 return NULL;
469 }
470 Vec_PtrFree( vTokens );
471 return pLib;
472}
473
485Amap_Lib_t * Amap_LibReadFile( char * pFileName, int fVerbose )
486{
487 Amap_Lib_t * pLib;
488 char * pBuffer;
489 pBuffer = Amap_LoadFile( pFileName );
490 if ( pBuffer == NULL )
491 return NULL;
492 pLib = Amap_LibReadBuffer( pBuffer, fVerbose );
493 if ( pLib )
494 pLib->pName = Abc_UtilStrsav( pFileName );
495 ABC_FREE( pBuffer );
496 return pLib;
497}
498
502
503
#define ABC_ALLOC(type, num)
Definition abc_global.h:264
#define ABC_FREE(obj)
Definition abc_global.h:267
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
char * Aig_MmFlexEntryFetch(Aig_MmFlex_t *p, int nBytes)
Definition aigMem.c:366
struct Aig_MmFlex_t_ Aig_MmFlex_t
Definition aig.h:53
#define Amap_GateForEachPin(pGate, pPin)
Definition amapInt.h:297
Amap_Lib_t * Amap_LibAlloc()
DECLARATIONS ///.
Definition amapLib.c:45
struct Amap_Gat_t_ Amap_Gat_t
Definition amapInt.h:66
#define Amap_LibForEachGate(pLib, pGate, i)
Definition amapInt.h:294
struct Amap_Pin_t_ Amap_Pin_t
BASIC TYPES ///.
Definition amapInt.h:65
int Amap_GateCollectNames(Aig_MmFlex_t *pMem, char *pForm, char *pPinNames[])
Definition amapRead.c:240
#define AMAP_STRING_UNKNOWN
Definition amapRead.c:35
#define AMAP_SYMB_AND
Definition amapRead.c:38
Amap_Lib_t * Amap_LibReadFile(char *pFileName, int fVerbose)
Definition amapRead.c:485
int Amap_CollectFormulaTokens(Vec_Ptr_t *vTokens, char *pToken, int iPos)
Definition amapRead.c:312
#define AMAP_SYMB_AND2
Definition amapRead.c:39
#define AMAP_SYMB_OR1
Definition amapRead.c:40
void Amap_RemoveComments(char *pBuffer, int *pnDots, int *pnLines)
Definition amapRead.c:121
#define AMAP_SYMB_NOT
Definition amapRead.c:43
#define AMAP_STRING_NONINV
Definition amapRead.c:33
#define AMAP_SYMB_AFTNOT
Definition amapRead.c:44
int Amap_ParseCountPins(Vec_Ptr_t *vTokens, int iPos)
Definition amapRead.c:217
#define AMAP_SYMB_OPEN
Definition amapRead.c:45
#define AMAP_STRING_INV
Definition amapRead.c:34
#define AMAP_STRING_GATE
DECLARATIONS ///.
Definition amapRead.c:31
#define AMAP_SYMB_OR2
Definition amapRead.c:41
Amap_Lib_t * Amap_LibReadBuffer(char *pBuffer, int fVerbose)
Definition amapRead.c:458
Vec_Ptr_t * Amap_DeriveTokens(char *pBuffer)
Definition amapRead.c:188
#define AMAP_STRING_PIN
Definition amapRead.c:32
#define AMAP_SYMB_CLOSE
Definition amapRead.c:46
char * Amap_LoadFile(char *pFileName)
FUNCTION DEFINITIONS ///.
Definition amapRead.c:74
Amap_Gat_t * Amap_ParseGateWithSamePins(Amap_Gat_t *p)
Definition amapRead.c:282
Amap_Lib_t * Amap_ParseTokens(Vec_Ptr_t *vTokens, int fVerbose)
Definition amapRead.c:338
Amap_PinPhase_t
Definition amapRead.c:48
@ AMAP_PHASE_UNKNOWN
Definition amapRead.c:49
@ AMAP_PHASE_NONINV
Definition amapRead.c:51
@ AMAP_PHASE_INV
Definition amapRead.c:50
#define AMAP_SYMB_XOR
Definition amapRead.c:42
typedefABC_NAMESPACE_HEADER_START struct Amap_Lib_t_ Amap_Lib_t
INCLUDES ///.
Definition amap.h:42
void Amap_LibFree(Amap_Lib_t *p)
Definition amapLib.c:67
Cube * p
Definition exorList.c:222
FILE * Io_FileOpen(const char *FileName, const char *PathVar, const char *Mode, int fVerbose)
Definition ioUtil.c:828
char * pForm
Definition amapInt.h:158
Amap_Lib_t * pLib
Definition amapInt.h:153
double dArea
Definition amapInt.h:157
unsigned nPins
Definition amapInt.h:162
Amap_Gat_t * pTwin
Definition amapInt.h:154
char * pOutName
Definition amapInt.h:156
char * pName
Definition amapInt.h:155
Amap_Pin_t Pins[0]
Definition amapInt.h:163
unsigned Id
Definition amapInt.h:160
double dDelayBlockMax
Definition amapInt.h:149
double dLoadMax
Definition amapInt.h:144
double dDelayBlockRise
Definition amapInt.h:145
double dDelayFanoutFall
Definition amapInt.h:148
double dLoadInput
Definition amapInt.h:143
double dDelayBlockFall
Definition amapInt.h:147
char * pName
Definition amapInt.h:141
double dDelayFanoutRise
Definition amapInt.h:146
#define assert(ex)
Definition util_old.h:213
char * memset()
int strlen()
int strcmp()
char * strtok()
char * strcpy()
VOID_HACK rewind()
double atof()
char * strcat()
#define Vec_PtrForEachEntryStart(Type, vVec, pEntry, i, Start)
Definition vecPtr.h:57
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
Definition vecPtr.h:42
#define SEEK_END
Definition zconf.h:392