ioReadBlifAig.c 31.5 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
/**CFile****************************************************************

  FileName    [ioReadBlifAig.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Command processing package.]

  Synopsis    [Procedures to read BLIF file into AIG.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - December 23, 2006.]

  Revision    [$Id: ioReadBlifAig.c,v 1.00 2006/12/23 00:00:00 alanmi Exp $]

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

21 22
#include "base/abc/abc.h"
#include "misc/vec/vecPtr.h"
Alan Mishchenko committed
23

24 25 26
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
27 28 29 30 31 32 33 34 35 36 37 38 39 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
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

// latch initial values
typedef enum { 
    IO_BLIF_INIT_NONE = 0,  // 0:  unknown
    IO_BLIF_INIT_ZERO,      // 1:  zero
    IO_BLIF_INIT_ONE,       // 2:  one
    IO_BLIF_INIT_DC         // 3:  don't-care
} Io_BlifInit_t;

typedef struct Io_BlifObj_t_ Io_BlifObj_t;  // parsing object
struct Io_BlifObj_t_
{
    unsigned             fPi    :  1;  // the object is a primary input
    unsigned             fPo    :  1;  // the object is a primary output
    unsigned             fLi    :  1;  // the object is a latch input
    unsigned             fLo    :  1;  // the object is a latch output
    unsigned             fDef   :  1;  // the object is defined as a table (node, PO, LI)
    unsigned             fLoop  :  1;  // flag for loop detection
    unsigned             Init   :  2;  // the latch initial state
    unsigned             Offset : 24;  // temporary number
    char *               pName;        // the name of this object
    void *               pEquiv;       // the AIG node representing this line    
    Io_BlifObj_t *       pNext;        // the next obj in the hash table
};

typedef struct Io_BlifMan_t_ Io_BlifMan_t;  // parsing manager
struct Io_BlifMan_t_
{
    // general info about file
    char *               pFileName;    // the name of the file
    char *               pBuffer;      // the begining of the file buffer
    Vec_Ptr_t *          vLines;       // the line beginnings
    // temporary objects
    Io_BlifObj_t *       pObjects;     // the storage for objects
    int                  nObjects;     // the number of objects allocated
65
    int                  iObjNext;     // the next free object
Alan Mishchenko committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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
    // file lines
    char *               pModel;       // .model line
    Vec_Ptr_t *          vInputs;      // .inputs lines
    Vec_Ptr_t *          vOutputs;     // .outputs lines
    Vec_Ptr_t *          vLatches;     // .latches lines
    Vec_Ptr_t *          vNames;       // .names lines
    // network objects
    Vec_Ptr_t *          vPis;         // the PI structures
    Vec_Ptr_t *          vPos;         // the PO structures
    Vec_Ptr_t *          vLis;         // the LI structures
    Vec_Ptr_t *          vLos;         // the LO structures
    // mapping of names into objects
    Io_BlifObj_t **      pTable;       // the hash table
    int                  nTableSize;   // the hash table size
    // current processing info
    Abc_Ntk_t *          pAig;         // the network under construction
    Vec_Ptr_t *          vTokens;      // the current tokens
    char                 sError[512];  // the error string generated during parsing
    // statistics 
    int                  nTablesRead;  // the number of processed tables
    int                  nTablesLeft;  // the number of dangling tables
};

// static functions
static Io_BlifMan_t *    Io_BlifAlloc();
static void              Io_BlifFree( Io_BlifMan_t * p );
static char *            Io_BlifLoadFile( char * pFileName );
static void              Io_BlifReadPreparse( Io_BlifMan_t * p );
static Abc_Ntk_t *       Io_BlifParse( Io_BlifMan_t * p );
static int               Io_BlifParseModel( Io_BlifMan_t * p, char * pLine );
static int               Io_BlifParseInputs( Io_BlifMan_t * p, char * pLine );
static int               Io_BlifParseOutputs( Io_BlifMan_t * p, char * pLine );
static int               Io_BlifParseLatch( Io_BlifMan_t * p, char * pLine );
static int               Io_BlifParseNames( Io_BlifMan_t * p, char * pLine );
static int               Io_BlifParseConstruct( Io_BlifMan_t * p );
static int               Io_BlifCharIsSpace( char s ) { return s == ' ' || s == '\t' || s == '\r' || s == '\n';  }

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

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

  Synopsis    [Reads the network from the BLIF file as an AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Io_ReadBlifAsAig( char * pFileName, int fCheck )
{
    FILE * pFile;
    Io_BlifMan_t * p;
    Abc_Ntk_t * pAig;

    // check that the file is available
    pFile = fopen( pFileName, "rb" );
    if ( pFile == NULL )
    {
        printf( "Io_Blif(): The file is unavailable (absent or open).\n" );
        return 0;
    }
    fclose( pFile );

    // start the file reader
    p = Io_BlifAlloc();
    p->pFileName = pFileName;
    p->pBuffer   = Io_BlifLoadFile( pFileName );
    if ( p->pBuffer == NULL )
    {
        Io_BlifFree( p );
        return NULL;
    }
    // prepare the file for parsing
    Io_BlifReadPreparse( p );
    // construct the network
    pAig = Io_BlifParse( p );
    if ( p->sError[0] )
        fprintf( stdout, "%s\n", p->sError );
    if ( pAig == NULL )
        return NULL;
    Io_BlifFree( p );

    // make sure that everything is okay with the network structure
    if ( fCheck && !Abc_NtkCheckRead( pAig ) )
    {
        printf( "Io_Blif: The network check has failed.\n" );
        Abc_NtkDelete( pAig );
        return NULL;
    }
    return pAig;
}

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

  Synopsis    [Allocates the BLIF parsing structure.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static Io_BlifMan_t * Io_BlifAlloc()
{
    Io_BlifMan_t * p;
Alan Mishchenko committed
176
    p = ABC_ALLOC( Io_BlifMan_t, 1 );
Alan Mishchenko committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    memset( p, 0, sizeof(Io_BlifMan_t) );
    p->vLines   = Vec_PtrAlloc( 512 );
    p->vInputs  = Vec_PtrAlloc( 512 );
    p->vOutputs = Vec_PtrAlloc( 512 );
    p->vLatches = Vec_PtrAlloc( 512 );
    p->vNames   = Vec_PtrAlloc( 512 );
    p->vTokens  = Vec_PtrAlloc( 512 );
    p->vPis     = Vec_PtrAlloc( 512 );
    p->vPos     = Vec_PtrAlloc( 512 );
    p->vLis     = Vec_PtrAlloc( 512 );
    p->vLos     = Vec_PtrAlloc( 512 );
    return p;
}

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

  Synopsis    [Frees the BLIF parsing structure.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static void Io_BlifFree( Io_BlifMan_t * p )
{
    if ( p->pAig )
        Abc_NtkDelete( p->pAig );
Alan Mishchenko committed
206 207 208
    if ( p->pBuffer )  ABC_FREE( p->pBuffer );
    if ( p->pObjects ) ABC_FREE( p->pObjects );
    if ( p->pTable )   ABC_FREE( p->pTable );
Alan Mishchenko committed
209 210 211 212 213 214 215 216 217 218
    Vec_PtrFree( p->vLines );
    Vec_PtrFree( p->vInputs );
    Vec_PtrFree( p->vOutputs );
    Vec_PtrFree( p->vLatches );
    Vec_PtrFree( p->vNames );
    Vec_PtrFree( p->vTokens );
    Vec_PtrFree( p->vPis );
    Vec_PtrFree( p->vPos );
    Vec_PtrFree( p->vLis );
    Vec_PtrFree( p->vLos );
Alan Mishchenko committed
219
    ABC_FREE( p );
Alan Mishchenko committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
}


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

  Synopsis    [Hashing for character strings.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static unsigned Io_BlifHashString( char * pName, int TableSize ) 
{
    static int s_Primes[10] = { 
        1291, 1699, 2357, 4177, 5147, 
        5647, 6343, 7103, 7873, 8147
    };
    unsigned i, Key = 0;
    for ( i = 0; pName[i] != '\0'; i++ )
        Key ^= s_Primes[i%10]*pName[i]*pName[i];
    return Key % TableSize;
}

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

  Synopsis    [Checks if the given name exists in the table.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static Io_BlifObj_t ** Io_BlifHashLookup( Io_BlifMan_t * p, char * pName )
{
    Io_BlifObj_t ** ppEntry;
    for ( ppEntry = p->pTable + Io_BlifHashString(pName, p->nTableSize); *ppEntry; ppEntry = &(*ppEntry)->pNext )
        if ( !strcmp((*ppEntry)->pName, pName) )
            return ppEntry;
    return ppEntry;
}

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

  Synopsis    [Finds or add the given name to the table.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static Io_BlifObj_t * Io_BlifHashFindOrAdd( Io_BlifMan_t * p, char * pName )
{
    Io_BlifObj_t ** ppEntry;
    ppEntry = Io_BlifHashLookup( p, pName );
    if ( *ppEntry == NULL )
    {
        assert( p->iObjNext < p->nObjects ); 
        *ppEntry = p->pObjects + p->iObjNext++;
        (*ppEntry)->pName = pName;
    }
    return *ppEntry;
}


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

  Synopsis    [Collects the already split tokens.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static void Io_BlifCollectTokens( Vec_Ptr_t * vTokens, char * pInput, char * pOutput )
{
    char * pCur;
    Vec_PtrClear( vTokens );
    for ( pCur = pInput; pCur < pOutput; pCur++ )
    {
        if ( *pCur == 0 )
            continue;
        Vec_PtrPush( vTokens, pCur );
        while ( *++pCur );
    }
}

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

  Synopsis    [Splits the line into tokens.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static void Io_BlifSplitIntoTokens( Vec_Ptr_t * vTokens, char * pLine, char Stop )
{
    char * pCur;
    // clear spaces
    for ( pCur = pLine; *pCur != Stop; pCur++ )
        if ( Io_BlifCharIsSpace(*pCur) )
            *pCur = 0;
    // collect tokens
    Io_BlifCollectTokens( vTokens, pLine, pCur );
}

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

  Synopsis    [Returns the 1-based number of the line in which the token occurs.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_BlifGetLine( Io_BlifMan_t * p, char * pToken )
{
    char * pLine;
    int i;
352
    Vec_PtrForEachEntry( char *, p->vLines, pLine, i )
Alan Mishchenko committed
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
        if ( pToken < pLine )
            return i;
    return -1;
}

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

  Synopsis    [Conservatively estimates the number of primary inputs.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_BlifEstimatePiNum( Io_BlifMan_t * p )
{
    char * pCur;
    int i, fSpaces;
    int Counter = 0;
374
    Vec_PtrForEachEntry( char *, p->vInputs, pCur, i )
Alan Mishchenko committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
        for ( fSpaces = 0; *pCur; pCur++ )
        {
            if ( Io_BlifCharIsSpace(*pCur) )
            {
                if ( !fSpaces )
                    Counter++;
                fSpaces = 1;
            }
            else
                fSpaces = 0;
        }
    return Counter;
}

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

  Synopsis    [Conservatively estimates the number of AIG nodes.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_BlifEstimateAndNum( Io_BlifMan_t * p )
{
    Io_BlifObj_t * pObj;
    char * pCur;
    int i, CounterOne, Counter = 0;
    for ( i = 0; i < p->iObjNext; i++ )
    {
        pObj = p->pObjects + i;
        if ( !pObj->fDef )
            continue;
        CounterOne = 0;
        for ( pCur = pObj->pName + strlen(pObj->pName); *pCur != '.'; pCur++ )
            if ( *pCur == '0' || *pCur == '1' )
                CounterOne++;
        if ( CounterOne )
            Counter += CounterOne - 1;
    }
    return Counter;
}

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

  Synopsis    [Reads the file into a character buffer.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static char * Io_BlifLoadFile( char * pFileName )
{
    FILE * pFile;
    int nFileSize;
    char * pContents;
436
    int RetValue;
Alan Mishchenko committed
437 438 439 440 441 442 443 444 445 446
    pFile = fopen( pFileName, "rb" );
    if ( pFile == NULL )
    {
        printf( "Io_BlifLoadFile(): The file is unavailable (absent or open).\n" );
        return NULL;
    }
    fseek( pFile, 0, SEEK_END );  
    nFileSize = ftell( pFile ); 
    if ( nFileSize == 0 )
    {
447
        fclose( pFile );
Alan Mishchenko committed
448 449 450
        printf( "Io_BlifLoadFile(): The file is empty.\n" );
        return NULL;
    }
Alan Mishchenko committed
451
    pContents = ABC_ALLOC( char, nFileSize + 10 );
Alan Mishchenko committed
452
    rewind( pFile );
453
    RetValue = fread( pContents, nFileSize, 1, pFile );
Alan Mishchenko committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
    fclose( pFile );
    // finish off the file with the spare .end line
    // some benchmarks suddenly break off without this line
    strcpy( pContents + nFileSize, "\n.end\n" );
    return pContents;
}

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

  Synopsis    [Prepares the parsing.]

  Description [Performs several preliminary operations:
  - Cuts the file buffer into separate lines.
  - Removes comments and line extenders.
  - Sorts lines by directives.
  - Estimates the number of objects.
  - Allocates room for the objects.
  - Allocates room for the hash table.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static void Io_BlifReadPreparse( Io_BlifMan_t * p )
{
    char * pCur, * pPrev;
    int i, fComment = 0;
    // parse the buffer into lines and remove comments
    Vec_PtrPush( p->vLines, p->pBuffer );
    for ( pCur = p->pBuffer; *pCur; pCur++ )
    {
        if ( *pCur == '\n' )
        {
            *pCur = 0;
            fComment = 0;
            Vec_PtrPush( p->vLines, pCur + 1 );
        }
        else if ( *pCur == '#' )
            fComment = 1;
        // remove comments
        if ( fComment )
            *pCur = 0;
    }

    // unfold the line extensions and sort lines by directive
500
    Vec_PtrForEachEntry( char *, p->vLines, pCur, i )
Alan Mishchenko committed
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
    {
        if ( *pCur == 0 )
            continue;
        // find previous non-space character
        for ( pPrev = pCur - 2; pPrev >= p->pBuffer; pPrev-- )
            if ( !Io_BlifCharIsSpace(*pPrev) )
                break;
        // if it is the line extender, overwrite it with spaces
        if ( *pPrev == '\\' )
        {
            for ( ; *pPrev; pPrev++ )
                *pPrev = ' ';
            *pPrev = ' ';
            continue;
        }
        // skip spaces at the beginning of the line
        while ( Io_BlifCharIsSpace(*pCur++) );
        // parse directives
        if ( *(pCur-1) != '.' )
            continue;
        if ( !strncmp(pCur, "names", 5) )
            Vec_PtrPush( p->vNames, pCur );
        else if ( !strncmp(pCur, "latch", 5) )
            Vec_PtrPush( p->vLatches, pCur );
        else if ( !strncmp(pCur, "inputs", 6) )
            Vec_PtrPush( p->vInputs, pCur );
        else if ( !strncmp(pCur, "outputs", 7) )
            Vec_PtrPush( p->vOutputs, pCur );
        else if ( !strncmp(pCur, "model", 5) ) 
            p->pModel = pCur;
        else if ( !strncmp(pCur, "end", 3) || !strncmp(pCur, "exdc", 4) )
            break;
        else
        {
            pCur--;
            if ( pCur[strlen(pCur)-1] == '\r' )
                pCur[strlen(pCur)-1] = 0;
            fprintf( stdout, "Line %d: Skipping line \"%s\".\n", Io_BlifGetLine(p, pCur), pCur );
        }
    }

    // count the number of objects
    p->nObjects = Io_BlifEstimatePiNum(p) + Vec_PtrSize(p->vLatches) + Vec_PtrSize(p->vNames) + 512;

    // allocate memory for objects
Alan Mishchenko committed
546
    p->pObjects = ABC_ALLOC( Io_BlifObj_t, p->nObjects );
Alan Mishchenko committed
547 548 549 550
    memset( p->pObjects, 0, p->nObjects * sizeof(Io_BlifObj_t) );

    // allocate memory for the hash table
    p->nTableSize = p->nObjects/2 + 1;
Alan Mishchenko committed
551
    p->pTable = ABC_ALLOC( Io_BlifObj_t *, p->nTableSize );
Alan Mishchenko committed
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
    memset( p->pTable, 0, p->nTableSize * sizeof(Io_BlifObj_t *) );
}


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

  Synopsis    [Reads the AIG in the binary AIGER format.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static Abc_Ntk_t * Io_BlifParse( Io_BlifMan_t * p )
{
    Abc_Ntk_t * pAig;
    char * pLine;
    int i;
    // parse the model
    if ( !Io_BlifParseModel( p, p->pModel ) )
        return NULL;
    // parse the inputs
576
    Vec_PtrForEachEntry( char *, p->vInputs, pLine, i )
Alan Mishchenko committed
577 578 579
        if ( !Io_BlifParseInputs( p, pLine ) )
            return NULL;
    // parse the outputs
580
    Vec_PtrForEachEntry( char *, p->vOutputs, pLine, i )
Alan Mishchenko committed
581 582 583
        if ( !Io_BlifParseOutputs( p, pLine ) )
            return NULL;
    // parse the latches
584
    Vec_PtrForEachEntry( char *, p->vLatches, pLine, i )
Alan Mishchenko committed
585 586 587
        if ( !Io_BlifParseLatch( p, pLine ) )
            return NULL;
    // parse the nodes
588
    Vec_PtrForEachEntry( char *, p->vNames, pLine, i )
Alan Mishchenko committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
        if ( !Io_BlifParseNames( p, pLine ) )
            return NULL;
    // reconstruct the network from the parsed data
    if ( !Io_BlifParseConstruct( p ) )
        return NULL;
    // return the network
    pAig = p->pAig;
    p->pAig = NULL;
    return pAig;
}

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

  Synopsis    [Parses the model line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_BlifParseModel( Io_BlifMan_t * p, char * pLine )
{
    char * pToken;
    Io_BlifSplitIntoTokens( p->vTokens, pLine, '\0' );
615
    pToken = (char *)Vec_PtrEntry( p->vTokens, 0 );
Alan Mishchenko committed
616 617 618 619 620 621
    assert( !strcmp(pToken, "model") );
    if ( Vec_PtrSize(p->vTokens) != 2 )
    {
        sprintf( p->sError, "Line %d: Model line has %d entries while it should have 2.", Io_BlifGetLine(p, pToken), Vec_PtrSize(p->vTokens) );
        return 0;
    }
622
    p->pModel = (char *)Vec_PtrEntry( p->vTokens, 1 );
Alan Mishchenko committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
    return 1;
}

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

  Synopsis    [Parses the inputs line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_BlifParseInputs( Io_BlifMan_t * p, char * pLine )
{
    Io_BlifObj_t * pObj;
    char * pToken;
    int i;
    Io_BlifSplitIntoTokens( p->vTokens, pLine, '\0' );
643
    pToken = (char *)Vec_PtrEntry(p->vTokens, 0);
Alan Mishchenko committed
644
    assert( !strcmp(pToken, "inputs") );
645
    Vec_PtrForEachEntryStart( char *, p->vTokens, pToken, i, 1 )
Alan Mishchenko committed
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
    {
        pObj = Io_BlifHashFindOrAdd( p, pToken );
        if ( pObj->fPi )
        {
            sprintf( p->sError, "Line %d: Primary input (%s) is defined more than once.", Io_BlifGetLine(p, pToken), pToken );
            return 0;
        }
        pObj->fPi = 1;
        Vec_PtrPush( p->vPis, pObj );
    }
    return 1;
}

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

  Synopsis    [Parses the outputs line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_BlifParseOutputs( Io_BlifMan_t * p, char * pLine )
{
    Io_BlifObj_t * pObj;
    char * pToken;
    int i;
    Io_BlifSplitIntoTokens( p->vTokens, pLine, '\0' );
676
    pToken = (char *)Vec_PtrEntry(p->vTokens, 0);
Alan Mishchenko committed
677
    assert( !strcmp(pToken, "outputs") );
678
    Vec_PtrForEachEntryStart( char *, p->vTokens, pToken, i, 1 )
Alan Mishchenko committed
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
    {
        pObj = Io_BlifHashFindOrAdd( p, pToken );
        if ( pObj->fPo )
            fprintf( stdout, "Line %d: Primary output (%s) is defined more than once (warning only).\n", Io_BlifGetLine(p, pToken), pToken );
        pObj->fPo = 1;
        Vec_PtrPush( p->vPos, pObj );
    }
    return 1;
}

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

  Synopsis    [Parses the latches line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_BlifParseLatch( Io_BlifMan_t * p, char * pLine )
{
    Io_BlifObj_t * pObj;
    char * pToken;
    int Init;
    Io_BlifSplitIntoTokens( p->vTokens, pLine, '\0' );
706
    pToken = (char *)Vec_PtrEntry(p->vTokens,0);
Alan Mishchenko committed
707 708 709 710 711 712 713 714
    assert( !strcmp(pToken, "latch") );
    if ( Vec_PtrSize(p->vTokens) < 3 )
    {
        sprintf( p->sError, "Line %d: Latch does not have input name and output name.", Io_BlifGetLine(p, pToken) );
        return 0;
    }
    // get initial value
    if ( Vec_PtrSize(p->vTokens) > 3 )
715
        Init = atoi( (char *)Vec_PtrEntry(p->vTokens,3) );
Alan Mishchenko committed
716 717 718 719
    else
        Init = 2;
    if ( Init < 0 || Init > 2 )
    {
Alan Mishchenko committed
720
        sprintf( p->sError, "Line %d: Initial state of the latch is incorrect (%s).", Io_BlifGetLine(p, pToken), (char*)Vec_PtrEntry(p->vTokens,3) );
Alan Mishchenko committed
721 722 723 724 725 726 727 728 729
        return 0;
    }
    if ( Init == 0 )
        Init = IO_BLIF_INIT_ZERO;
    else if ( Init == 1 )
        Init = IO_BLIF_INIT_ONE;
    else // if ( Init == 2 )
        Init = IO_BLIF_INIT_DC;
    // get latch input
730
    pObj = Io_BlifHashFindOrAdd( p, (char *)Vec_PtrEntry(p->vTokens,1) );
Alan Mishchenko committed
731 732 733 734
    pObj->fLi = 1;
    Vec_PtrPush( p->vLis, pObj );
    pObj->Init = Init;
    // get latch output
735
    pObj = Io_BlifHashFindOrAdd( p, (char *)Vec_PtrEntry(p->vTokens,2) );
Alan Mishchenko committed
736 737
    if ( pObj->fPi )
    {
Alan Mishchenko committed
738
        sprintf( p->sError, "Line %d: Primary input (%s) is also defined latch output.", Io_BlifGetLine(p, pToken), (char*)Vec_PtrEntry(p->vTokens,2) );
Alan Mishchenko committed
739 740 741 742
        return 0;
    }
    if ( pObj->fLo )
    {
Alan Mishchenko committed
743
        sprintf( p->sError, "Line %d: Latch output (%s) is defined as the output of another latch.", Io_BlifGetLine(p, pToken), (char*)Vec_PtrEntry(p->vTokens,2) );
Alan Mishchenko committed
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
        return 0;
    }
    pObj->fLo = 1;
    Vec_PtrPush( p->vLos, pObj );
    pObj->Init = Init;
    return 1;
}

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

  Synopsis    [Parses the nodes line.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_BlifParseNames( Io_BlifMan_t * p, char * pLine )
{
    Io_BlifObj_t * pObj;
    char * pName;
    Io_BlifSplitIntoTokens( p->vTokens, pLine, '\0' );
768 769
    assert( !strcmp((char *)Vec_PtrEntry(p->vTokens,0), "names") );
    pName = (char *)Vec_PtrEntryLast( p->vTokens );
Alan Mishchenko committed
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
    pObj = Io_BlifHashFindOrAdd( p, pName );
    if ( pObj->fPi )
    {
        sprintf( p->sError, "Line %d: Primary input (%s) has a table.", Io_BlifGetLine(p, pName), pName );
        return 0;
    }
    if ( pObj->fLo )
    {
        sprintf( p->sError, "Line %d: Latch output (%s) has a table.", Io_BlifGetLine(p, pName), pName );
        return 0;
    }
    if ( pObj->fDef )
    {
        sprintf( p->sError, "Line %d: Signal (%s) is defined more than once.", Io_BlifGetLine(p, pName), pName );
        return 0;
    }
    pObj->fDef = 1;
    // remember offset to the first fanin name
    pObj->pName = pName;
    pObj->Offset = pObj->pName - (char *)Vec_PtrEntry(p->vTokens,1);
    return 1;
}


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

  Synopsis    [Constructs the AIG from the file parsing info.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static Abc_Obj_t * Io_BlifParseTable( Io_BlifMan_t * p, char * pTable, Vec_Ptr_t * vFanins )
{
    char * pProduct, * pOutput;
    Abc_Obj_t * pRes, * pCube;
    int i, k, Polarity = -1;

    p->nTablesRead++;
    // get the tokens
    Io_BlifSplitIntoTokens( p->vTokens, pTable, '.' );
    if ( Vec_PtrSize(p->vTokens) == 0 )
        return Abc_ObjNot( Abc_AigConst1(p->pAig) );
    if ( Vec_PtrSize(p->vTokens) == 1 )
    {
818
        pOutput = (char *)Vec_PtrEntry( p->vTokens, 0 );
Alan Mishchenko committed
819 820 821 822 823 824 825
        if ( ((pOutput[0] - '0') & 0x8E) || pOutput[1] )
        {
            sprintf( p->sError, "Line %d: Constant table has wrong output value (%s).", Io_BlifGetLine(p, pOutput), pOutput );
            return NULL;
        }
        return Abc_ObjNotCond( Abc_AigConst1(p->pAig), pOutput[0] == '0' );
    }
826
    pProduct = (char *)Vec_PtrEntry( p->vTokens, 0 );
Alan Mishchenko committed
827 828 829 830 831 832 833 834 835
    if ( Vec_PtrSize(p->vTokens) % 2 == 1 )
    {
        sprintf( p->sError, "Line %d: Table has odd number of tokens (%d).", Io_BlifGetLine(p, pProduct), Vec_PtrSize(p->vTokens) );
        return NULL;
    }
    // parse the table
    pRes = Abc_ObjNot( Abc_AigConst1(p->pAig) );
    for ( i = 0; i < Vec_PtrSize(p->vTokens)/2; i++ )
    {
836 837
        pProduct = (char *)Vec_PtrEntry( p->vTokens, 2*i + 0 );
        pOutput  = (char *)Vec_PtrEntry( p->vTokens, 2*i + 1 );
Alan Mishchenko committed
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
        if ( strlen(pProduct) != (unsigned)Vec_PtrSize(vFanins) )
        {
            sprintf( p->sError, "Line %d: Cube (%s) has size different from the fanin count (%d).", Io_BlifGetLine(p, pProduct), pProduct, Vec_PtrSize(vFanins) );
            return NULL;
        }
        if ( ((pOutput[0] - '0') & 0x8E) || pOutput[1] )
        {
            sprintf( p->sError, "Line %d: Output value (%s) is incorrect.", Io_BlifGetLine(p, pProduct), pOutput );
            return NULL;
        }
        if ( Polarity == -1 )
            Polarity = pOutput[0] - '0';
        else if ( Polarity != pOutput[0] - '0' )
        {
            sprintf( p->sError, "Line %d: Output value (%s) differs from the value in the first line of the table (%d).", Io_BlifGetLine(p, pProduct), pOutput, Polarity );
            return NULL;
        }
        // parse one product product
        pCube = Abc_AigConst1(p->pAig);
        for ( k = 0; pProduct[k]; k++ )
        {
            if ( pProduct[k] == '0' )
860
                pCube = Abc_AigAnd( (Abc_Aig_t *)p->pAig->pManFunc, pCube, Abc_ObjNot((Abc_Obj_t *)Vec_PtrEntry(vFanins,k)) );
Alan Mishchenko committed
861
            else if ( pProduct[k] == '1' )
862
                pCube = Abc_AigAnd( (Abc_Aig_t *)p->pAig->pManFunc, pCube, (Abc_Obj_t *)Vec_PtrEntry(vFanins,k) );
Alan Mishchenko committed
863 864 865 866 867 868
            else if ( pProduct[k] != '-' )
            {
                sprintf( p->sError, "Line %d: Product term (%s) contains character (%c).", Io_BlifGetLine(p, pProduct), pProduct, pProduct[k] );
                return NULL;
            }
        }
869
        pRes = Abc_AigOr( (Abc_Aig_t *)p->pAig->pManFunc, pRes, pCube );
Alan Mishchenko committed
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
    }
    pRes = Abc_ObjNotCond( pRes, Polarity == 0 );
    return pRes;
}

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

  Synopsis    [Constructs the AIG from the file parsing info.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static Abc_Obj_t * Io_BlifParseConstruct_rec( Io_BlifMan_t * p, char * pName )
{
    Vec_Ptr_t * vFanins;
    Abc_Obj_t * pFaninAbc;
    Io_BlifObj_t * pObjIo;
    char * pNameFanin;
    int i;
    // get the IO object with this name
    pObjIo = *Io_BlifHashLookup( p, pName );
    if ( pObjIo == NULL )
    {
        sprintf( p->sError, "Line %d: Signal (%s) is not defined as a table.", Io_BlifGetLine(p, pName), pName );
        return NULL;
    }
    // loop detection
    if ( pObjIo->fLoop )
    {
        sprintf( p->sError, "Line %d: Signal (%s) appears twice on a combinational path.", Io_BlifGetLine(p, pName), pName );
        return NULL;
    }
    // check if the AIG is already constructed
    if ( pObjIo->pEquiv )
908
        return (Abc_Obj_t *)pObjIo->pEquiv;
Alan Mishchenko committed
909 910 911 912 913
    // mark this node on the path
    pObjIo->fLoop = 1;
    // construct the AIGs for the fanins
    vFanins = Vec_PtrAlloc( 8 );
    Io_BlifCollectTokens( vFanins, pObjIo->pName - pObjIo->Offset, pObjIo->pName );
914
    Vec_PtrForEachEntry( char *, vFanins, pNameFanin, i )
Alan Mishchenko committed
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
    {
        pFaninAbc = Io_BlifParseConstruct_rec( p, pNameFanin );
        if ( pFaninAbc == NULL )
        {
            Vec_PtrFree( vFanins );
            return NULL;
        }
        Vec_PtrWriteEntry( vFanins, i, pFaninAbc );
    }
    // construct the node
    pObjIo->pEquiv = Io_BlifParseTable( p, pObjIo->pName + strlen(pObjIo->pName), vFanins );
    Vec_PtrFree( vFanins );
    // unmark this node on the path
    pObjIo->fLoop = 0;
    // remember the new node
930
    return (Abc_Obj_t *)pObjIo->pEquiv;
Alan Mishchenko committed
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
}

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

  Synopsis    [Constructs the AIG from the file parsing info.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static int Io_BlifParseConstruct( Io_BlifMan_t * p )
{
    Abc_Ntk_t * pAig;
    Io_BlifObj_t * pObjIo, * pObjIoInput;
    Abc_Obj_t * pObj, * pLatch;
    int i;
    // allocate the empty AIG
    pAig = p->pAig = Abc_NtkAlloc( ABC_NTK_STRASH, ABC_FUNC_AIG, 1 );
    pAig->pName = Extra_UtilStrsav( p->pModel );
    pAig->pSpec = Extra_UtilStrsav( p->pFileName );
    // create PIs
955
    Vec_PtrForEachEntry( Io_BlifObj_t *, p->vPis, pObjIo, i )
Alan Mishchenko committed
956 957 958 959 960 961
    {
        pObj = Abc_NtkCreatePi( pAig );
        Abc_ObjAssignName( pObj, pObjIo->pName, NULL );
        pObjIo->pEquiv = pObj;
    }
    // create POs
962
    Vec_PtrForEachEntry( Io_BlifObj_t *, p->vPos, pObjIo, i )
Alan Mishchenko committed
963 964 965 966 967
    {
        pObj = Abc_NtkCreatePo( pAig );
        Abc_ObjAssignName( pObj, pObjIo->pName, NULL );
    }
    // create latches
968
    Vec_PtrForEachEntry( Io_BlifObj_t *, p->vLos, pObjIo, i )
Alan Mishchenko committed
969 970 971
    {
        // add the latch input terminal
        pObj = Abc_NtkCreateBi( pAig );
972
        pObjIoInput = (Io_BlifObj_t *)Vec_PtrEntry( p->vLis, i );
Alan Mishchenko committed
973 974 975 976
        Abc_ObjAssignName( pObj, pObjIoInput->pName, NULL );
        
        // add the latch box
        pLatch = Abc_NtkCreateLatch( pAig );
Alan Mishchenko committed
977
        pLatch->pData = (void *)(ABC_PTRUINT_T)pObjIo->Init;
Alan Mishchenko committed
978 979 980 981 982 983 984 985 986 987 988 989
        Abc_ObjAssignName( pLatch, pObjIo->pName, "L" );
        Abc_ObjAddFanin( pLatch, pObj  );

        // add the latch output terminal
        pObj = Abc_NtkCreateBo( pAig );
        Abc_ObjAssignName( pObj, pObjIo->pName, NULL );
        Abc_ObjAddFanin( pObj, pLatch );
        // set the value of the latch output
//        pObjIo->pEquiv = Abc_ObjNotCond( pObj, pObjIo->Init );
        pObjIo->pEquiv = pObj;
    }
    // traverse the nodes from the POs
990
    Vec_PtrForEachEntry( Io_BlifObj_t *, p->vPos, pObjIo, i )
Alan Mishchenko committed
991 992 993 994 995 996 997
    {
        pObj = Io_BlifParseConstruct_rec( p, pObjIo->pName );
        if ( pObj == NULL )
            return 0;
        Abc_ObjAddFanin( Abc_NtkPo(p->pAig, i), pObj );
    }
    // traverse the nodes from the latch inputs
998
    Vec_PtrForEachEntry( Io_BlifObj_t *, p->vLis, pObjIo, i )
Alan Mishchenko committed
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
    {
        pObj = Io_BlifParseConstruct_rec( p, pObjIo->pName );
        if ( pObj == NULL )
            return 0;
//        pObj = Abc_ObjNotCond( pObj, pObjIo->Init );
        Abc_ObjAddFanin( Abc_ObjFanin0(Abc_NtkBox(p->pAig, i)), pObj );
    }
    p->nTablesLeft = Vec_PtrSize(p->vNames) - p->nTablesRead; 
    if ( p->nTablesLeft ) 
        printf( "The number of dangling tables = %d.\n", p->nTablesLeft );
    printf( "AND nodes = %6d.  Estimate = %6d.\n", Abc_NtkNodeNum(p->pAig), Io_BlifEstimateAndNum(p) );
    return 1;
}

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


1018 1019
ABC_NAMESPACE_IMPL_END