amapRead.c 15.1 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/**CFile****************************************************************

  FileName    [amapRead.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Technology mapper for standard cells.]

  Synopsis    []

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: amapRead.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

***********************************************************************/

#include "amapInt.h"
22
#include "base/io/ioAbc.h"
23 24 25

ABC_NAMESPACE_IMPL_START

Alan Mishchenko committed
26 27 28 29 30 31 32 33 34 35 36 37 38

////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

#define AMAP_STRING_GATE       "GATE"
#define AMAP_STRING_PIN        "PIN"
#define AMAP_STRING_NONINV     "NONINV"
#define AMAP_STRING_INV        "INV"
#define AMAP_STRING_UNKNOWN    "UNKNOWN"

// these symbols (and no other) can appear in the formulas
#define AMAP_SYMB_AND    '*'
39
#define AMAP_SYMB_AND2   '&'
Alan Mishchenko committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#define AMAP_SYMB_OR1    '+'
#define AMAP_SYMB_OR2    '|'
#define AMAP_SYMB_XOR    '^'
#define AMAP_SYMB_NOT    '!'
#define AMAP_SYMB_AFTNOT '\''
#define AMAP_SYMB_OPEN   '('
#define AMAP_SYMB_CLOSE  ')'

typedef enum { 
    AMAP_PHASE_UNKNOWN, 
    AMAP_PHASE_INV, 
    AMAP_PHASE_NONINV 
} Amap_PinPhase_t;

static inline Amap_Gat_t * Amap_ParseGateAlloc( Aig_MmFlex_t * p, int nPins ) 
{ return (Amap_Gat_t *)Aig_MmFlexEntryFetch( p, sizeof(Amap_Gat_t)+sizeof(Amap_Pin_t)*nPins ); }
static inline char * Amap_ParseStrsav( Aig_MmFlex_t * p, char * pStr ) 
{ return pStr ? strcpy(Aig_MmFlexEntryFetch(p, strlen(pStr)+1), pStr) : NULL; }

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

  Synopsis    [Loads the file into temporary buffer.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Amap_LoadFile( char * pFileName )
{
76
//    extern FILE * Io_FileOpen( const char * FileName, const char * PathVar, const char * Mode, int fVerbose );
Alan Mishchenko committed
77 78 79
    FILE * pFile;
    char * pBuffer;
    int nFileSize;
80
    int RetValue;
Alan Mishchenko committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    // open the BLIF file for binary reading
    pFile = Io_FileOpen( pFileName, "open_path", "rb", 1 );
//    pFile = fopen( FileName, "rb" );
    // if we got this far, file should be okay otherwise would
    // have been detected by caller
    if ( pFile == NULL )
    {
        printf( "Cannot open file \"%s\".\n", pFileName );
        return NULL;
    }
    assert ( pFile != NULL );
    // get the file size, in bytes
    fseek( pFile, 0, SEEK_END );  
    nFileSize = ftell( pFile );  
    // move the file current reading position to the beginning
    rewind( pFile ); 
    // load the contents of the file into memory
Alan Mishchenko committed
98
    pBuffer = ABC_ALLOC( char, nFileSize + 10 );
99
    RetValue = fread( pBuffer, nFileSize, 1, pFile );
Alan Mishchenko committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    // terminate the string with '\0'
    pBuffer[ nFileSize ] = '\0';
    strcat( pBuffer, "\n.end\n" );
    // close file
    fclose( pFile );
    return pBuffer;
}

/**Function*************************************************************

  Synopsis    [Eliminates comments from the input file.]

  Description [As a byproduct, this procedure also counts the number
  lines and dot-statements in the input file. This also joins non-comment 
  lines that are joined with a backspace '\']
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Amap_RemoveComments( char * pBuffer, int * pnDots, int * pnLines )
{
    char * pCur;
    int nDots, nLines;
    // scan through the buffer and eliminate comments
    // (in the BLIF file, comments are lines starting with "#")
    nDots = nLines = 0;
    for ( pCur = pBuffer; *pCur; pCur++ )
    {
        // if this is the beginning of comment
        // clean it with spaces until the new line statement
        if ( *pCur == '#' )
            while ( *pCur != '\n' )
                *pCur++ = ' ';
    
        // count the number of new lines and dots
        if ( *pCur == '\n' ) {
        if (*(pCur-1)=='\r') {
        // DOS(R) file support
        if (*(pCur-2)!='\\') nLines++;
        else {
            // rewind to backslash and overwrite with a space
            *(pCur-2) = ' ';
            *(pCur-1) = ' ';
            *pCur = ' ';
        }
        } else {
        // UNIX(TM) file support
        if (*(pCur-1)!='\\') nLines++;
        else {
            // rewind to backslash and overwrite with a space
            *(pCur-1) = ' ';
            *pCur = ' ';
        }
        }
    }
        else if ( *pCur == '.' )
            nDots++;
    }
    if ( pnDots )
        *pnDots = nDots; 
    if ( pnLines )
        *pnLines = nLines; 
}

/**Function*************************************************************

  Synopsis    [Splits the stream into tokens.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Amap_DeriveTokens( char * pBuffer )
{
    Vec_Ptr_t * vTokens;
    char * pToken;
    vTokens = Vec_PtrAlloc( 1000 );
Alan Mishchenko committed
182
    pToken = strtok( pBuffer, " =\t\r\n" );
Alan Mishchenko committed
183 184 185
    while ( pToken )
    {
        Vec_PtrPush( vTokens, pToken );
Alan Mishchenko committed
186
        pToken = strtok( NULL, " =\t\r\n" );
187 188 189 190
        // skip latches
        if ( pToken && strcmp( pToken, "LATCH" ) == 0 )
            while ( pToken && strcmp( pToken, "GATE" ) != 0 )
                pToken = strtok( NULL, " =\t\r\n" );
Alan Mishchenko committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    }
    return vTokens;
}

/**Function*************************************************************

  Synopsis    [Finds the number of pins.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Amap_ParseCountPins( Vec_Ptr_t * vTokens, int iPos )
{
    char * pToken;
    int i, Counter = 0;
210
    Vec_PtrForEachEntryStart( char *, vTokens, pToken, i, iPos )
Alan Mishchenko committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
        if ( !strcmp( pToken, AMAP_STRING_PIN ) )
            Counter++;
        else if ( !strcmp( pToken, AMAP_STRING_GATE ) )
            return Counter;
    return Counter;
}

/**Function*************************************************************

  Synopsis    [Collect the pin names used in the formula.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Amap_GateCollectNames( Aig_MmFlex_t * pMem, char * pForm, char * pPinNames[] )
{
    char Buffer[1000];
    char * pTemp;
    int nPins, i;
    // save the formula as it was
    strcpy( Buffer, pForm );
    // remove the non-name symbols
    for ( pTemp = Buffer; *pTemp; pTemp++ )
        if ( *pTemp == AMAP_SYMB_AND || *pTemp == AMAP_SYMB_OR1 || *pTemp == AMAP_SYMB_OR2 
          || *pTemp == AMAP_SYMB_XOR || *pTemp == AMAP_SYMB_NOT || *pTemp == AMAP_SYMB_OPEN 
240
          || *pTemp == AMAP_SYMB_CLOSE || *pTemp == AMAP_SYMB_AFTNOT || *pTemp == AMAP_SYMB_AND2 )
Alan Mishchenko committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
            *pTemp = ' ';
    // save the names
    nPins = 0;
    pTemp = strtok( Buffer, " " );
    while ( pTemp )
    {
        for ( i = 0; i < nPins; i++ )
            if ( strcmp( pTemp, pPinNames[i] ) == 0 )
                break;
        if ( i == nPins )
        { // cannot find this name; save it
            pPinNames[nPins++] = Amap_ParseStrsav( pMem, pTemp );
        }
        // get the next name
        pTemp = strtok( NULL, " " );
    }
    return nPins;
}

/**Function*************************************************************

  Synopsis    [Creates a duplicate gate with pins specified.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Amap_Gat_t * Amap_ParseGateWithSamePins( Amap_Gat_t * p )
{
    Amap_Gat_t * pGate;
    Amap_Pin_t * pPin;
    char * pPinNames[128];
    int nPinNames;
    assert( p->nPins == 1 && !strcmp( p->Pins->pName, "*" ) );
    nPinNames = Amap_GateCollectNames( p->pLib->pMemGates, p->pForm, pPinNames );
    pGate = Amap_ParseGateAlloc( p->pLib->pMemGates, nPinNames );
    *pGate = *p;
    pGate->nPins = nPinNames;
    Amap_GateForEachPin( pGate, pPin )
    {
        *pPin = *p->Pins;
        pPin->pName = pPinNames[pPin - pGate->Pins];
    }
    return pGate;
}

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
301 302 303 304 305 306 307
int Amap_CollectFormulaTokens( Vec_Ptr_t * vTokens, char * pToken, int iPos )
{
    char * pNext, * pPrev;
    pPrev = pToken + strlen(pToken);
    while ( *(pPrev-1) != ';' )
    {
        *pPrev++ = ' ';
308
        pNext = (char *)Vec_PtrEntry(vTokens, iPos++);
Alan Mishchenko committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
        while ( *pNext )
            *pPrev++ = *pNext++;
    }
    *(pPrev-1) = 0;
    return iPos;
}

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
327 328 329
Amap_Lib_t * Amap_ParseTokens( Vec_Ptr_t * vTokens, int fVerbose )
{
    Amap_Lib_t * p;
330
    Amap_Gat_t * pGate, * pPrev;
Alan Mishchenko committed
331
    Amap_Pin_t * pPin;
332 333
    char * pToken, * pMoGate = NULL;
    int i, nPins, iPos = 0, Count = 0;
Alan Mishchenko committed
334
    p = Amap_LibAlloc();
335
    pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
Alan Mishchenko committed
336 337 338 339
    do 
    {
        if ( strcmp( pToken, AMAP_STRING_GATE ) )
        {
340
            Amap_LibFree( p );
Alan Mishchenko committed
341 342 343 344 345 346 347 348 349 350 351 352
            printf( "The first line should begin with %s.\n", AMAP_STRING_GATE );
            return NULL;
        }
        // start gate
        nPins = Amap_ParseCountPins( vTokens, iPos );
        pGate = Amap_ParseGateAlloc( p->pMemGates, nPins );
        memset( pGate, 0, sizeof(Amap_Gat_t) );
        pGate->Id = Vec_PtrSize( p->vGates );
        Vec_PtrPush( p->vGates, pGate );
        pGate->pLib = p;
        pGate->nPins = nPins;
        // read gate
353
        pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
Alan Mishchenko committed
354
        pGate->pName = Amap_ParseStrsav( p->pMemGates, pToken );    
355
        pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
Alan Mishchenko committed
356
        pGate->dArea = atof( pToken );
357
        pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
Alan Mishchenko committed
358
        pGate->pOutName = Amap_ParseStrsav( p->pMemGates, pToken ); 
359
        pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
Alan Mishchenko committed
360
        iPos = Amap_CollectFormulaTokens( vTokens, pToken, iPos );
Alan Mishchenko committed
361 362 363 364
        pGate->pForm = Amap_ParseStrsav( p->pMemGates, pToken ); 
        // read pins
        Amap_GateForEachPin( pGate, pPin )
        {
365
            pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
Alan Mishchenko committed
366 367
            if ( strcmp( pToken, AMAP_STRING_PIN ) )
            {
368
                Amap_LibFree( p );
Alan Mishchenko committed
369 370 371 372
                printf( "Cannot parse gate %s.\n", pGate->pName );
                return NULL;
            }
            // read pin
373
            pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
Alan Mishchenko committed
374
            pPin->pName = Amap_ParseStrsav( p->pMemGates, pToken );   
375
            pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
Alan Mishchenko committed
376 377 378 379 380 381 382 383
            if ( strcmp( pToken, AMAP_STRING_UNKNOWN ) == 0 )
                pPin->Phase = AMAP_PHASE_UNKNOWN;
            else if ( strcmp( pToken, AMAP_STRING_INV ) == 0 )
                pPin->Phase = AMAP_PHASE_INV;
            else if ( strcmp( pToken, AMAP_STRING_NONINV ) == 0 )
                pPin->Phase = AMAP_PHASE_NONINV;
            else 
            {
384
                Amap_LibFree( p );
Alan Mishchenko committed
385 386 387
                printf( "Cannot read phase of pin %s of gate %s\n", pPin->pName, pGate->pName );
                return NULL;
            }
388
            pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
Alan Mishchenko committed
389
            pPin->dLoadInput = atof( pToken );
390
            pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
Alan Mishchenko committed
391
            pPin->dLoadMax = atof( pToken );
392
            pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
Alan Mishchenko committed
393
            pPin->dDelayBlockRise = atof( pToken );
394
            pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
Alan Mishchenko committed
395
            pPin->dDelayFanoutRise = atof( pToken );
396
            pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
Alan Mishchenko committed
397
            pPin->dDelayBlockFall = atof( pToken );
398
            pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
Alan Mishchenko committed
399 400 401 402 403 404 405 406 407 408 409 410 411
            pPin->dDelayFanoutFall = atof( pToken );
            if ( pPin->dDelayBlockRise > pPin->dDelayBlockFall )
                pPin->dDelayBlockMax = pPin->dDelayBlockRise;
            else
                pPin->dDelayBlockMax = pPin->dDelayBlockFall;
        }
        // fix the situation when all pins are represented as one
        if ( pGate->nPins == 1 && !strcmp( pGate->Pins->pName, "*" ) )
        {
            pGate = Amap_ParseGateWithSamePins( pGate );
            Vec_PtrPop( p->vGates );
            Vec_PtrPush( p->vGates, pGate );
        }
412
        pToken = (char *)Vec_PtrEntry(vTokens, iPos++);
413
//printf( "Finished reading gate %s (%s)\n", pGate->pName, pGate->pOutName );
Alan Mishchenko committed
414 415
    }
    while ( strcmp( pToken, ".end" ) );
416 417 418 419 420 421 422 423

    // check if there are gates with identical names
    pPrev = NULL;
    Amap_LibForEachGate( p, pGate, i )
    {
        if ( pPrev && !strcmp(pPrev->pName, pGate->pName) )
        {
            pPrev->pTwin = pGate, pGate->pTwin = pPrev;
424 425 426 427
//            printf( "Warning: Detected multi-output gate \"%s\".\n", pGate->pName );
            if ( pMoGate == NULL )
                pMoGate = pGate->pName;
            Count++;
428 429 430
        }
        pPrev = pGate;
    }
431 432
    if ( Count )
        printf( "Warning: Detected %d multi-output gates (for example, \"%s\").\n", Count, pMoGate );
Alan Mishchenko committed
433 434 435 436 437 438 439 440 441 442 443 444 445 446
    return p;
}

/**Function*************************************************************

  Synopsis    [Reads the library from the input file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
447
Amap_Lib_t * Amap_LibReadBuffer( char * pBuffer, int fVerbose )
Alan Mishchenko committed
448 449 450 451 452 453 454
{
    Amap_Lib_t * pLib;
    Vec_Ptr_t * vTokens;
    Amap_RemoveComments( pBuffer, NULL, NULL );
    vTokens = Amap_DeriveTokens( pBuffer );
    pLib = Amap_ParseTokens( vTokens, fVerbose );
    if ( pLib == NULL )
455 456
    {
        Vec_PtrFree( vTokens );
Alan Mishchenko committed
457
        return NULL;
458
    }
Alan Mishchenko committed
459
    Vec_PtrFree( vTokens );
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
    return pLib;
}

/**Function*************************************************************

  Synopsis    [Reads the library from the input file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Amap_Lib_t * Amap_LibReadFile( char * pFileName, int fVerbose )
{
    Amap_Lib_t * pLib;
    char * pBuffer;
    pBuffer = Amap_LoadFile( pFileName );
    if ( pBuffer == NULL )
        return NULL;
    pLib = Amap_LibReadBuffer( pBuffer, fVerbose );
    if ( pLib )
        pLib->pName = Abc_UtilStrsav( pFileName );
Alan Mishchenko committed
484
    ABC_FREE( pBuffer );
Alan Mishchenko committed
485 486 487 488 489 490 491 492
    return pLib;
}

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


493 494
ABC_NAMESPACE_IMPL_END